aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Magorsch <arzano@gentoo.org>2020-06-19 15:51:41 +0200
committerMax Magorsch <arzano@gentoo.org>2020-06-19 15:51:41 +0200
commit21181c518cf41828917d36005b726f9452fde657 (patch)
tree38fab1b3c86a41383e48be6b2686d92efd86db62 /pkg/app/list/threads.go
downloadarchives-21181c518cf41828917d36005b726f9452fde657.tar.gz
archives-21181c518cf41828917d36005b726f9452fde657.tar.bz2
archives-21181c518cf41828917d36005b726f9452fde657.zip
Initial version
Signed-off-by: Max Magorsch <arzano@gentoo.org>
Diffstat (limited to 'pkg/app/list/threads.go')
-rw-r--r--pkg/app/list/threads.go60
1 files changed, 60 insertions, 0 deletions
diff --git a/pkg/app/list/threads.go b/pkg/app/list/threads.go
new file mode 100644
index 0000000..33ade3c
--- /dev/null
+++ b/pkg/app/list/threads.go
@@ -0,0 +1,60 @@
+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", "headers", "date").
+ Where("to_char(date, 'YYYY-MM') = ?", combinedDate).
+ Where(`NOT headers::jsonb ? 'References'`).
+ Where(`NOT headers::jsonb ? 'In-Reply-To'`).
+ 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
+ }).
+ 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)
+
+}