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
|
package search
import (
"archives/pkg/config"
"archives/pkg/database"
"archives/pkg/models"
"math"
"net/http"
"strconv"
"strings"
)
func Search(w http.ResponseWriter, r *http.Request) {
//
// Parse search params
//
searchTerm := getParameterValue("q", r)
showThreads := getParameterValue("threads", r) != ""
page, err := strconv.Atoi(getParameterValue("page", r))
var currentPage int
var offset int
if err != nil {
currentPage = 1
offset = 0
} else {
currentPage = page
offset = 50 * (page - 1)
}
//
// Step 1: Search for List with the same name and redirect
//
for _, list := range config.AllPublicMailingLists() {
if strings.TrimSpace(searchTerm) == list {
http.Redirect(w, r, "/"+list+"/", http.StatusMovedPermanently)
return
}
}
//
// Step 2: Search by Author
//
var searchResults []*models.Message
query := database.DBCon.Model(&searchResults).
WhereOr(`headers::jsonb->>'From' LIKE ?`, "%"+searchTerm+"%").
Order("date DESC")
if showThreads {
query = query.Where(`NOT headers::jsonb ? 'References'`).Where(`NOT headers::jsonb ? 'In-Reply-To'`)
}
messagesCount, _ := query.Count()
err = query.Limit(50).Offset(offset).Select()
if err == nil && messagesCount > 0 && strings.TrimSpace(searchTerm) != "gentoo" {
maxPages := int(math.Ceil(float64(messagesCount) / float64(50)))
renderSearchTemplate(w, showThreads, searchTerm, messagesCount, currentPage, maxPages, searchResults)
return
}
//
// Step 3: Search by Subject
//
query = database.DBCon.Model(&searchResults).
Where(`tsv_subject @@ to_tsquery(''?'')`, searchTerm)
if showThreads {
query = query.Where(`NOT headers::jsonb ? 'References'`).Where(`NOT headers::jsonb ? 'In-Reply-To'`)
}
messagesCount, _ = query.Count()
err = query.Limit(50).Offset(offset).Select()
if err == nil && messagesCount > 0 {
maxPages := int(math.Ceil(float64(messagesCount) / float64(50)))
renderSearchTemplate(w, showThreads, searchTerm, messagesCount, currentPage, maxPages, searchResults)
return
}
//
// Step 4: Search by Message Body
//
query = database.DBCon.Model(&searchResults).
Where(`tsv_body @@ to_tsquery(''?'')`, searchTerm)
if showThreads {
query = query.Where(`NOT headers::jsonb ? 'References'`).Where(`NOT headers::jsonb ? 'In-Reply-To'`)
}
messagesCount, _ = query.Count()
err = query.Limit(50).Offset(offset).Select()
if err != nil {
http.NotFound(w, r)
return
}
maxPages := int(math.Ceil(float64(messagesCount) / float64(50)))
renderSearchTemplate(w, showThreads, searchTerm, messagesCount, currentPage, maxPages, searchResults)
}
|