aboutsummaryrefslogtreecommitdiff
blob: 8db8778f228321f1de4ec3a005f24478f9b1eb49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package list

import (
	"archives/pkg/database"
	"archives/pkg/models"
	"github.com/go-pg/pg/v10/orm"
	"net/http"
	"strings"
)

func Show(w http.ResponseWriter, r *http.Request) {

	listName := strings.ReplaceAll(r.URL.Path, "/", "")

	var res []struct {
		CombinedDate string
		MessageCount int
	}
	err := database.DBCon.Model((*models.Message)(nil)).
		WhereGroup(func(q *orm.Query) (*orm.Query, error) {
			q = q.WhereOr(`(headers::jsonb->>'Subject')::jsonb->>0 LIKE '[` + listName + `]%'`).
				WhereOr(`(headers::jsonb->>'Subject')::jsonb->>0 LIKE 'Re: [` + listName + `]%'`)
			return q, nil
		}).
		WhereGroup(func(q *orm.Query) (*orm.Query, error) {
			q = q.WhereOr(`headers::jsonb->>'To' LIKE '%` + listName + `@lists.gentoo.org%'`).
				WhereOr(`headers::jsonb->>'Cc' LIKE '%` + listName + `@lists.gentoo.org%'`).
				WhereOr(`headers::jsonb->>'To' LIKE '%` + listName + `@gentoo.org%'`).
				WhereOr(`headers::jsonb->>'Cc' LIKE '%` + listName + `@gentoo.org%'`)
			return q, nil
		}).
		ColumnExpr("to_char(date, 'YYYY-MM') AS combined_date").
		ColumnExpr("count(*) AS message_count").
		Group("combined_date").
		Order("combined_date DESC").
		Select(&res)

	if err != nil {
		http.NotFound(w, r)
		return
	}

	renderShowTemplate(w, listName, res)
}