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/home
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/home')
-rw-r--r--pkg/app/home/home.go71
-rw-r--r--pkg/app/home/utils.go64
2 files changed, 135 insertions, 0 deletions
diff --git a/pkg/app/home/home.go b/pkg/app/home/home.go
new file mode 100644
index 0000000..e2d3955
--- /dev/null
+++ b/pkg/app/home/home.go
@@ -0,0 +1,71 @@
+// Used to show the landing page of the application
+
+package home
+
+import (
+ "archives/pkg/app/popular"
+ "archives/pkg/config"
+ "archives/pkg/database"
+ "archives/pkg/models"
+ "github.com/go-pg/pg/v10/orm"
+ "net/http"
+ "time"
+)
+
+// Show renders a template to show the landing page of the application
+func Show(w http.ResponseWriter, r *http.Request) {
+
+ var mailingLists []models.MailingList
+
+ for _, mailingList := range config.IndexMailingLists() {
+ var messages []*models.Message
+ database.DBCon.Model(&messages).
+ WhereGroup(func(q *orm.Query) (*orm.Query, error) {
+ q = q.WhereOr(`(headers::jsonb->>'Subject')::jsonb->>0 LIKE '[` + mailingList[0] + `]%'`).
+ WhereOr(`(headers::jsonb->>'Subject')::jsonb->>0 LIKE 'Re: [` + mailingList[0] + `]%'`)
+ return q, nil
+ }).
+ WhereGroup(func(q *orm.Query) (*orm.Query, error) {
+ q = q.WhereOr(`headers::jsonb->>'To' LIKE '%` + mailingList[0] + `@lists.gentoo.org%'`).
+ WhereOr(`headers::jsonb->>'Cc' LIKE '%` + mailingList[0] + `@lists.gentoo.org%'`).
+ WhereOr(`headers::jsonb->>'To' LIKE '%` + mailingList[0] + `@gentoo.org%'`).
+ WhereOr(`headers::jsonb->>'Cc' LIKE '%` + mailingList[0] + `@gentoo.org%'`)
+ return q, nil
+ }).
+ Order("date DESC").
+ Limit(5).
+ Select()
+
+ mailingLists = append(mailingLists, models.MailingList{
+ Name: mailingList[0],
+ Description: mailingList[1],
+ Messages: messages,
+ })
+ }
+
+ //
+ // Get popular threads
+ //
+ popularThreads, err := popular.GetPopularThreads(10, "2020-06-01")
+ if err != nil {
+ http.NotFound(w, r)
+ return
+ }
+ if len(popularThreads) > 5 {
+ popularThreads = popularThreads[:5]
+ }
+
+ templateData := struct {
+ MailingLists []models.MailingList
+ PopularThreads models.Threads
+ MessageCount string
+ CurrentMonth string
+ }{
+ MailingLists: mailingLists,
+ PopularThreads: popularThreads,
+ MessageCount: formatMessageCount(getAllMessagesCount()),
+ CurrentMonth: time.Now().Format("2006-01"),
+ }
+
+ renderIndexTemplate(w, templateData)
+}
diff --git a/pkg/app/home/utils.go b/pkg/app/home/utils.go
new file mode 100644
index 0000000..daab2de
--- /dev/null
+++ b/pkg/app/home/utils.go
@@ -0,0 +1,64 @@
+// miscellaneous utility functions used for the landing page of the application
+
+package home
+
+import (
+ "archives/pkg/database"
+ "archives/pkg/models"
+ "github.com/go-pg/pg/v10"
+ "html/template"
+ "net/http"
+ "strconv"
+)
+
+// renderIndexTemplate renders all templates used for the landing page
+func renderIndexTemplate(w http.ResponseWriter, templateData interface{}) {
+ templates := template.Must(
+ template.Must(
+ template.New("Show").
+ Funcs(template.FuncMap{
+ "makeMessage" : func(headers map[string][]string) models.Message {
+ return models.Message{
+ Headers: headers,
+ }
+ },
+ }).
+ ParseGlob("web/templates/layout/*.tmpl")).
+ ParseGlob("web/templates/home/*.tmpl"))
+
+ templates.ExecuteTemplate(w, "home.tmpl", templateData)
+}
+
+// utility methods
+
+func getAllMessagesCount() int {
+ var messsageCount int
+ database.DBCon.Model((*models.Message)(nil)).QueryOne(pg.Scan(&messsageCount), `
+ SELECT
+ count(DISTINCT messages.headers->>'Message-Id')
+ FROM
+ messages;
+ `)
+ return messsageCount
+}
+
+// formatMessageCount returns the formatted number of
+// messages containing a thousands comma
+func formatMessageCount(messageCount int) string {
+ packages := strconv.Itoa(messageCount)
+ if len(string(messageCount)) == 9 {
+ return packages[:3] + "," + packages[3:6] + "," + packages[6:]
+ } else if len(packages) == 8 {
+ return packages[:2] + "," + packages[2:5] + "," + packages[5:]
+ } else if len(packages) == 7 {
+ return packages[:1] + "," + packages[1:4] + "," + packages[4:]
+ } else if len(packages) == 6 {
+ return packages[:3] + "," + packages[3:]
+ } else if len(packages) == 5 {
+ return packages[:2] + "," + packages[2:]
+ } else if len(packages) == 4 {
+ return packages[:1] + "," + packages[1:]
+ } else {
+ return packages
+ }
+}