Author: Adam <git@apiote.xyz>
archive messages in mímir
db.go | 29 +++++++++++++++++++++-------- mímir.go | 2 +-
diff --git a/db.go b/db.go index 56f3ecb37b62925c7d85bac9107e7cae51aaa3b7..4eed9bd7afe4360eb3a555f3f089fa4d9b242314 100644 --- a/db.go +++ b/db.go @@ -2,7 +2,6 @@ package main import ( "database/sql" - "fmt" "strings" "time" @@ -21,7 +20,7 @@ _, err = db.Exec(`create table tyr_locks(address text unique, token text, date date)`) if err != nil && err.Error() != "table tyr_locks already exists" { return nil, err } - _, err = db.Exec(`create table mimir_archive(message_id text primary key, subject text, body text, date datetime, in_reply_to text, dkim_status bool, sender text, recipients text, foreign key(in_reply_to) references mimir_archive(message_id))`) + _, err = db.Exec(`create table mimir_archive(message_id text primary key, subject text, body text, date datetime, in_reply_to text, dkim_status bool, sender text, recipients text, root_id text, foreign key(in_reply_to) references mimir_archive(message_id))`) if err != nil && err.Error() != "table mimir_archive already exists" { return nil, err } @@ -123,10 +122,24 @@ address.addressFrom, address.addressTo, address.ban) return err } -func addArchiveEntry(messageID, category, subject string, body []byte, date time.Time, inReplyTo string, dkim bool, sender *imap.Address, recipients []*imap.Address) { - // todo column rootID with rootID of parent or this.messageID - - fmt.Printf("adding Archive Entry for %s “%s”, %v, in reply to %s with dkim %v from %s\n", messageID, subject, date, inReplyTo, dkim, sender) - fmt.Printf("body: %s\n", body) - // todo +func addArchiveEntry(db *sql.DB, messageID, category, subject string, body []byte, date time.Time, inReplyTo string, dkim bool, sender *imap.Address, recipients []*imap.Address) error { + recipientsAddresses := []string{} + for _, recipient := range recipients { + recipientsAddresses = append(recipientsAddresses, recipient.Address()) + } + recipientsJoined := strings.Join(recipientsAddresses, ", ") + + var rootID string + row := db.QueryRow(`select root_id from mimir_archive where message_id = ?`, inReplyTo) + err := row.Scan(&rootID) + if err != nil { + if err == (sql.ErrNoRows) { + rootID = messageID + } else { + return err + } + } + db.Exec(`insert inti mimir_archive values(?, ?, ?, ?, ?, ?, ?, ?, ?)`, messageID, subject, body, date, inReplyTo, dkim, sender, recipientsJoined, rootID) + + return nil } diff --git a/mímir.go b/mímir.go index 6b356863b11c0508b353380ca5b990a4fa4fd9b1..aba05f8985932247021fc3fa91f51c631e853013 100644 --- a/mímir.go +++ b/mímir.go @@ -135,7 +135,7 @@ if err != nil { return err } - addArchiveEntry(messageID, project, subject, body, date, inReplyTo, dkimStatus, sender, recipients) + addArchiveEntry(db, messageID, category, subject, body, date, inReplyTo, dkimStatus, sender, recipients) } if err := <-done; err != nil {