aboutsummaryrefslogtreecommitdiff
blob: 25b296638fa21c389558a27662d0633d43d9ba0d (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
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
99
package models

import (
	"net/mail"
	"strings"
	"time"
)

type Message struct {
	Id       string `pg:",pk"`
	MessageId string
	Filename string

	List string

	From string
	To []string
	Cc []string

	Subject string
	Body string

	Date time.Time

	// fk
	InReplyTo *Message `pg:"fk:in_reply_to_id"` // fk specifies foreign key
	InReplyToId string

	// many to many
	//References []string
	References []Message `pg:"many2many:message_to_references,joinFK:reference_id"`

	Attachments []Attachment

	StartsThread bool

	Comment string
	Hidden  bool

}

type Header struct {
	Name    string
	Content string
}

type Body struct {
	ContentType string
	Content     string
}

type Attachment struct {
	Filename string
	Mime     string
	Content  string
}

type MessageToReferences struct {
	MessageId string
	ReferenceId  string
}

func (m Message) GetListNameFromSubject() string {
	subject := m.Subject
	listName := strings.Split(subject, "]")[0]
	listName = strings.ReplaceAll(listName, "[", "")
	listName = strings.ReplaceAll(listName, "Re:", "")
	listName = strings.TrimSpace(listName)
	return listName
}

func (m Message) GetAuthorName() string {
	addr, err := mail.ParseAddress(m.From)
	if err != nil {
		return ""
	}
	if addr.Name == "" {
		return addr.Address
	}
	return addr.Name
}

func (m Message) GetMessageId() string {
	messageId := m.MessageId
	messageId = strings.ReplaceAll(messageId, "<", "")
	messageId = strings.ReplaceAll(messageId, ">", "")
	messageId = strings.ReplaceAll(messageId, "\"", "")
	return messageId
}

func (m Message) GetInReplyTo() string {
	inReplyTo := m.InReplyTo.MessageId
	inReplyTo = strings.ReplaceAll(inReplyTo, "<", "")
	inReplyTo = strings.ReplaceAll(inReplyTo, ">", "")
	inReplyTo = strings.ReplaceAll(inReplyTo, " ", "")
	return inReplyTo
}