aboutsummaryrefslogtreecommitdiff
blob: c0696735e6fe83ac55803f1c56129d7c958afb9d (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package list

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

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

	urlParts := strings.Split(r.URL.Path, "/threads/")
	if len(urlParts) != 2 {
		http.NotFound(w, r)
		return
	}

	listName := strings.ReplaceAll(urlParts[0], "/", "")
	trailingUrlParts := strings.Split(urlParts[1], "/")
	combinedDate := trailingUrlParts[0]
	currentPage := 1
	if len(trailingUrlParts) > 1 {
		parsedCurrentPage, err := strconv.Atoi(trailingUrlParts[1])
		if err == nil {
			currentPage = parsedCurrentPage
		}
	}
	offset := (currentPage - 1) * 50

	var messages []*models.Message
	query := database.DBCon.Model(&messages).
		Column("id", "subject", "from", "date").
		Where("to_char(date, 'YYYY-MM') = ?", combinedDate).
		Where(`starts_thread = TRUE`).
		WhereGroup(func(q *orm.Query) (*orm.Query, error) {
			q = q.WhereOr(`subject LIKE '[` + listName + `]%'`).
				WhereOr(`subject 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
		//}).
		Order("date DESC")

	messagesCount, _ := query.Count()
	query.Limit(50).Offset(offset).Select()

	maxPages := int(math.Ceil(float64(messagesCount) / float64(50)))

	renderThreadsTemplate(w, listName, combinedDate, currentPage, maxPages, messages)

}