aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Magorsch <arzano@gentoo.org>2020-06-24 21:54:58 +0000
committerMax Magorsch <arzano@gentoo.org>2020-06-24 21:54:58 +0000
commitfea5c85fc458a521b3eff9e48cce55f435a4c9f4 (patch)
treeb9b17e847f451d33c0e5e6d24769c6a1253e49ad
parentFix the computation of replies to a message (diff)
downloadarchives-fea5c85fc458a521b3eff9e48cce55f435a4c9f4.tar.gz
archives-fea5c85fc458a521b3eff9e48cce55f435a4c9f4.tar.bz2
archives-fea5c85fc458a521b3eff9e48cce55f435a4c9f4.zip
Add an option to recompute the thread references
Signed-off-by: Max Magorsch <arzano@gentoo.org>
-rw-r--r--archives.go5
-rw-r--r--pkg/importer/importer.go16
-rw-r--r--pkg/importer/utils.go15
-rw-r--r--pkg/models/message.go1
4 files changed, 35 insertions, 2 deletions
diff --git a/archives.go b/archives.go
index 7cbba67..e9adb99 100644
--- a/archives.go
+++ b/archives.go
@@ -23,6 +23,7 @@ func main() {
fullImport := flag.Bool("full-import", false, "Start a full import, importing all mails")
incrementalImport := flag.Bool("incremental-import", false, "Start a incremental import, importing only new mails")
+ computeThreads := flag.Bool("compute-threads", false, "Recompute the thread references that are used to link messages")
serve := flag.Bool("serve", false, "Start serving the web application")
flag.Parse()
@@ -35,6 +36,10 @@ func main() {
importer.IncrementalImport()
}
+ if *computeThreads{
+ importer.RecomputeThreads()
+ }
+
if *serve {
app.Serve()
}
diff --git a/pkg/importer/importer.go b/pkg/importer/importer.go
index 76ba8e7..e83bd9f 100644
--- a/pkg/importer/importer.go
+++ b/pkg/importer/importer.go
@@ -79,6 +79,20 @@ func IncrementalImport() {
fmt.Println("Finished incremental import. Imported " + strconv.Itoa(importedCounter) + " new messages.")
}
+func RecomputeThreads() {
+
+ fmt.Println("Init thread computation...")
+ filepath.Walk(config.MailDirPath(), initImport)
+
+ for _, mail := range mails {
+ insertReferencesToMail(mail.RawReferences, mail.Id, mail.From)
+ }
+
+ fmt.Println("Finished thread computation.")
+}
+
+// utility methods
+
func fileIsAlreadyPresent(path string, messages []*models.Message) bool {
for _, message := range messages {
if strings.Contains(strings.TrimRight(path, ",S"), strings.TrimRight(message.Filename, ",S")){
@@ -86,4 +100,4 @@ func fileIsAlreadyPresent(path string, messages []*models.Message) bool {
}
}
return false
-} \ No newline at end of file
+}
diff --git a/pkg/importer/utils.go b/pkg/importer/utils.go
index ab9072f..7b750b5 100644
--- a/pkg/importer/utils.go
+++ b/pkg/importer/utils.go
@@ -48,6 +48,9 @@ func initImport(path string, info os.FileInfo, err error) error {
return err
}
+ references := strings.FieldsFunc(m.Header.Get("References"), func(r rune) bool {
+ return r == ',' || r == ' '
+ })
mails = append(mails, &models.Message{
Id: m.Header.Get("X-Archives-Hash"),
Filename: info.Name(),
@@ -55,6 +58,7 @@ func initImport(path string, info os.FileInfo, err error) error {
To: strings.Split(m.Header.Get("To"), ","),
Subject: m.Header.Get("Subject"),
MessageIdField: m.Header.Get("Message-Id"),
+ RawReferences: references,
})
}
return nil
@@ -111,7 +115,10 @@ func importIntoDatabase(path, filename string, m *mail.Message) {
fmt.Println(err)
}
- insertReferencesToMail(strings.Split(m.Header.Get("References"), ","), m.Header.Get("X-Archives-Hash"), m.Header.Get("From"))
+ references := strings.FieldsFunc(m.Header.Get("References"), func(r rune) bool {
+ return r == ',' || r == ' '
+ })
+ insertReferencesToMail(references, m.Header.Get("X-Archives-Hash"), m.Header.Get("From"))
WaitGroup.Done()
}
@@ -125,6 +132,9 @@ func parseAddressList(addressList string) []string {
}
func getInReplyToMail(messageId, from string) string {
+ if messageId == "" {
+ return ""
+ }
// step 1 TODO add description
for _, mail := range mails {
if mail.MessageId() == messageId && strings.Contains(strings.Join(mail.To, ", "), from) {
@@ -144,6 +154,9 @@ func getInReplyToMail(messageId, from string) string {
func insertReferencesToMail(references []string, messageId, from string) []*models.Message {
var referencesToMail []*models.Message
for _, reference := range references {
+ if strings.TrimSpace(reference) == "" {
+ continue
+ }
// step 1 TODO add description
for _, mail := range mails {
if mail.MessageId() == reference && strings.Contains(strings.Join(mail.To, ", "), from) {
diff --git a/pkg/models/message.go b/pkg/models/message.go
index e769319..bcd6067 100644
--- a/pkg/models/message.go
+++ b/pkg/models/message.go
@@ -31,6 +31,7 @@ type Message struct {
// many to many
//References []string
+ RawReferences []string `pg:"-"`
References []Message `pg:"many2many:message_to_references,joinFK:reference_id"`
Attachments []Attachment