aboutsummaryrefslogtreecommitdiff
blob: 07dbc65e0c8d48a44928f626ca3be57f920edf83 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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()
}