aboutsummaryrefslogtreecommitdiff
blob: bc2a12f6120823d35763200afa27648febb7ebf6 (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
package popular

import (
	"archives/pkg/database"
	"archives/pkg/models"
	"html/template"
	"net/http"
	"strconv"
)

// renderIndexTemplate renders all templates used for the landing page
func renderPopularThreads(w http.ResponseWriter, templateData interface{}) {
	templates := template.Must(
		template.Must(
			template.New("Popular").
				Funcs(template.FuncMap{
					"makeMessage": func(headers map[string][]string) models.Message {
						return models.Message{
							//Headers: headers,
						}
					},
				}).
				ParseGlob("web/templates/layout/*.tmpl")).
			ParseGlob("web/templates/popular/*.tmpl"))

	templates.ExecuteTemplate(w, "threads.tmpl", templateData)
}

// utility methods

func GetPopularThreads(n int, date string) ([]*models.Message, error) {

	var recentMessages []*models.Message

	err := database.DBCon.Model(&recentMessages).
		OrderExpr("date DESC").
		Limit(n).
		Select()

	return recentMessages, err
}

func GetMessagesFromPopularThreads(threads models.Threads) []*models.Message {
	var popularThreads []*models.Message
	for _, thread := range threads {
		var messages []*models.Message
		err := database.DBCon.Model(&messages).
			Where(`headers::jsonb->>'Subject' LIKE '%` + thread.Id + `%'`).
			Select()
		if err == nil && len(messages) > 0 {
			messages[0].Comment = strconv.Itoa(thread.Count)
			popularThreads = append(popularThreads, messages[0])
		}
	}
	return popularThreads
}