diff options
author | Max Magorsch <arzano@gentoo.org> | 2020-06-19 15:51:41 +0200 |
---|---|---|
committer | Max Magorsch <arzano@gentoo.org> | 2020-06-19 15:51:41 +0200 |
commit | 21181c518cf41828917d36005b726f9452fde657 (patch) | |
tree | 38fab1b3c86a41383e48be6b2686d92efd86db62 /pkg/app/list/utils.go | |
download | archives-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/utils.go')
-rw-r--r-- | pkg/app/list/utils.go | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/pkg/app/list/utils.go b/pkg/app/list/utils.go new file mode 100644 index 0000000..07dbc65 --- /dev/null +++ b/pkg/app/list/utils.go @@ -0,0 +1,137 @@ +// miscellaneous utility functions used for the landing page of the application + +package list + +import ( + "archives/pkg/database" + "archives/pkg/models" + "github.com/go-pg/pg/v10/orm" + "html/template" + "net/http" +) + +type ListData struct { + ListName string + Date string + CurrentPage int + MaxPages int + Messages []*models.Message +} + +// renderIndexTemplate renders all templates used for the landing page +func renderShowTemplate(w http.ResponseWriter, listName string, messageData interface{}) { + templates := template.Must( + template.Must( + template.New("Show"). + ParseGlob("web/templates/layout/*.tmpl")). + ParseGlob("web/templates/list/*.tmpl")) + + templteData := struct { + ListName string + MessageData interface{} + }{ + ListName: listName, + MessageData: messageData, + } + + templates.ExecuteTemplate(w, "show.tmpl", templteData) +} + +// renderIndexTemplate renders all templates used for the landing page +func renderMessagesTemplate(w http.ResponseWriter, listName string, date string, currentPage int, maxPages int, messages []*models.Message) { + templates := template.Must( + template.Must( + template.Must( + template.New("Show"). + Funcs(getFuncMap()). + ParseGlob("web/templates/layout/*.tmpl")). + ParseGlob("web/templates/list/components/*.tmpl")). + ParseGlob("web/templates/list/*.tmpl")) + + templates.ExecuteTemplate(w, "messages.tmpl", buildListData(listName, date, currentPage, maxPages, messages)) +} + +// renderIndexTemplate renders all templates used for the landing page +func renderThreadsTemplate(w http.ResponseWriter, listName string, date string, currentPage int, maxPages int, messages []*models.Message) { + templates := template.Must( + template.Must( + template.Must( + template.New("Show"). + Funcs(getFuncMap()). + ParseGlob("web/templates/layout/*.tmpl")). + ParseGlob("web/templates/list/components/*.tmpl")). + ParseGlob("web/templates/list/*.tmpl")) + + templates.ExecuteTemplate(w, "threads.tmpl", buildListData(listName, date, currentPage, maxPages, messages)) +} + +// renderIndexTemplate renders all templates used for the landing page +func renderBrowseTemplate(w http.ResponseWriter, lists interface{}) { + templates := template.Must( + template.Must( + template.New("Show"). + ParseGlob("web/templates/layout/*.tmpl")). + ParseGlob("web/templates/list/*.tmpl")) + + templates.ExecuteTemplate(w, "browse.tmpl", lists) +} + +// utility methods + +func getFuncMap() template.FuncMap { + return template.FuncMap{ + "min": func(a, b int) int { + if a < b { + return a + } + return b + }, + "max": func(a, b int) int { + if a < b { + return b + } + return a + }, + "add": func(a, b int) int { + return a + b + }, + "sub": func(a, b int) int { + return a - b + }, + "makeRange": makeRange, + } +} + +func buildListData(listName string, date string, currentPage int, maxPages int, messages []*models.Message) ListData { + return ListData{ + ListName: listName, + Date: date, + CurrentPage: currentPage, + MaxPages: maxPages, + Messages: messages, + } +} + +func makeRange(min, max int) []int { + a := make([]int, max-min+1) + for i := range a { + a[i] = min + i + } + return a +} + +func countMessages(listName string) (int, error) { + return 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 + }).Count() +} |