aboutsummaryrefslogtreecommitdiffstats
path: root/worker/types/search.go
blob: 93a0053d1a4e0094e97d642728d7baf70221186b (plain) (blame)
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
package types

import (
	"net/textproto"
	"strings"
	"time"

	"git.sr.ht/~rjarry/aerc/models"
)

type SearchCriteria struct {
	WithFlags    models.Flags
	WithoutFlags models.Flags
	From         []string
	To           []string
	Cc           []string
	Headers      textproto.MIMEHeader
	StartDate    time.Time
	EndDate      time.Time
	SearchBody   bool
	SearchAll    bool
	Terms        string
}

func (c *SearchCriteria) PrepareHeader() {
	if c == nil {
		return
	}
	if c.Headers == nil {
		c.Headers = make(textproto.MIMEHeader)
	}
	for _, from := range c.From {
		c.Headers.Add("From", from)
	}
	for _, to := range c.To {
		c.Headers.Add("To", to)
	}
	for _, cc := range c.Cc {
		c.Headers.Add("Cc", cc)
	}
}

func (c *SearchCriteria) Combine(other *SearchCriteria) *SearchCriteria {
	if c == nil {
		return other
	}
	headers := make(textproto.MIMEHeader)
	for k, v := range c.Headers {
		headers[k] = v
	}
	for k, v := range other.Headers {
		headers[k] = v
	}
	start := c.StartDate
	if !other.StartDate.IsZero() {
		start = other.StartDate
	}
	end := c.EndDate
	if !other.EndDate.IsZero() {
		end = other.EndDate
	}
	from := make([]string, len(c.From)+len(other.From))
	copy(from[:len(c.From)], c.From)
	copy(from[len(c.From):], other.From)
	to := make([]string, len(c.To)+len(other.To))
	copy(to[:len(c.To)], c.To)
	copy(to[len(c.To):], other.To)
	cc := make([]string, len(c.Cc)+len(other.Cc))
	copy(cc[:len(c.Cc)], c.Cc)
	copy(cc[len(c.Cc):], other.Cc)
	return &SearchCriteria{
		WithFlags:    c.WithFlags | other.WithFlags,
		WithoutFlags: c.WithoutFlags | other.WithoutFlags,
		From:         from,
		To:           to,
		Cc:           cc,
		Headers:      headers,
		StartDate:    start,
		EndDate:      end,
		SearchBody:   c.SearchBody || other.SearchBody,
		SearchAll:    c.SearchAll || other.SearchAll,
		Terms:        strings.Join([]string{c.Terms, other.Terms}, " "),
	}
}