diff options
author | 2020-06-19 18:33:21 +0200 | |
---|---|---|
committer | 2020-06-19 18:33:21 +0200 | |
commit | 2d98fcffdf895a6a23a34e2bfc0b1430488f2e0e (patch) | |
tree | 98736560da67cf06188ecb21787146c8ad73916c | |
parent | Remove duplicate lists (diff) | |
download | archives-2d98fcffdf895a6a23a34e2bfc0b1430488f2e0e.tar.gz archives-2d98fcffdf895a6a23a34e2bfc0b1430488f2e0e.tar.bz2 archives-2d98fcffdf895a6a23a34e2bfc0b1430488f2e0e.zip |
Speed up the /lists page
Signed-off-by: Max Magorsch <arzano@gentoo.org>
-rw-r--r-- | pkg/app/list/browse.go | 52 | ||||
-rw-r--r-- | pkg/importer/utils.go | 9 | ||||
-rw-r--r-- | pkg/models/message.go | 1 |
3 files changed, 46 insertions, 16 deletions
diff --git a/pkg/app/list/browse.go b/pkg/app/list/browse.go index 7d046e6..fdc3ab1 100644 --- a/pkg/app/list/browse.go +++ b/pkg/app/list/browse.go @@ -2,30 +2,42 @@ package list import ( "archives/pkg/config" + "archives/pkg/database" "archives/pkg/models" "net/http" ) func Browse(w http.ResponseWriter, r *http.Request) { - // Count number of messages in the current mailing lists - var currentMailingLists []models.MailingList - for _, listName := range config.CurrentMailingLists() { - messageCount, _ := countMessages(listName) - currentMailingLists = append(currentMailingLists, models.MailingList{ - Name: listName, - MessageCount: messageCount, - }) + var res []struct { + Name string + MessageCount int + } + err := database.DBCon.Model((*models.Message)(nil)). + ColumnExpr("list as name, count(*) as message_count"). + Group("list"). + Select(&res) + + if err != nil { + http.NotFound(w,r) + return } - // Count number of messages in the frozen archives + var currentMailingLists []models.MailingList var frozenArchives []models.MailingList - for _, listName := range config.FrozenArchives() { - messageCount, _ := countMessages(listName) - frozenArchives = append(frozenArchives, models.MailingList{ - Name: listName, - MessageCount: messageCount, - }) + + for _, list := range res { + if contains(config.CurrentMailingLists(), list.Name) { + currentMailingLists = append(currentMailingLists, models.MailingList{ + Name: list.Name, + MessageCount: list.MessageCount, + }) + }else if contains(config.FrozenArchives(), list.Name) { + frozenArchives = append(frozenArchives, models.MailingList{ + Name: list.Name, + MessageCount: list.MessageCount, + }) + } } browseData := struct { @@ -38,3 +50,13 @@ func Browse(w http.ResponseWriter, r *http.Request) { renderBrowseTemplate(w, browseData) } + + +func contains(s []string, e string) bool { + for _, a := range s { + if a == e { + return true + } + } + return false +} diff --git a/pkg/importer/utils.go b/pkg/importer/utils.go index c2d38fc..1af34ed 100644 --- a/pkg/importer/utils.go +++ b/pkg/importer/utils.go @@ -28,6 +28,7 @@ func importMail(name, path, maildirPath string) { Body: getBody(m.Header, m.Body), Date: getDate(m.Header), Lists: getLists(m.Header), + List: getListName(path), Comment: "", Hidden: false, } @@ -116,6 +117,12 @@ func getLists(header mail.Header) []string { return lists } +func getListName(path string) string { + listName := strings.ReplaceAll(path, config.MailDirPath() + ".", "") + listName = strings.Split(listName, "/")[0] + return listName +} + func insertMessage(message models.Message) error { _, err := database.DBCon.Model(&message). Value("tsv_subject", "to_tsvector(?)", message.GetSubject()). @@ -127,7 +134,7 @@ func insertMessage(message models.Message) error { func isPublicList(path string) bool { for _, publicList := range config.AllPublicMailingLists(){ - if strings.HasPrefix(path, config.MailDirPath() + "." + publicList + "/") { + if publicList == getListName(path) { return true } } diff --git a/pkg/models/message.go b/pkg/models/message.go index 17bbb9d..6d8b299 100644 --- a/pkg/models/message.go +++ b/pkg/models/message.go @@ -16,6 +16,7 @@ type Message struct { Attachments []Attachment Lists []string + List string Date time.Time //Search types.ValueAppender // tsvector |