aboutsummaryrefslogtreecommitdiffstats
path: root/worker/notmuch/search.go
blob: b3592d41ce5213902ad0a5520e76099f05d7290c (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//go:build notmuch
// +build notmuch

package notmuch

import (
	"fmt"

	"git.sr.ht/~rjarry/aerc/models"
	"git.sr.ht/~rjarry/aerc/worker/types"
	"git.sr.ht/~rjarry/go-opt"
)

type queryBuilder struct {
	s string
}

func (q *queryBuilder) and(s string) {
	if len(s) == 0 {
		return
	}
	if len(q.s) != 0 {
		q.s += " and "
	}
	q.s += "(" + s + ")"
}

func (q *queryBuilder) or(s string) {
	if len(s) == 0 {
		return
	}
	if len(q.s) != 0 {
		q.s += " or "
	}
	q.s += "(" + s + ")"
}

func translate(crit *types.SearchCriteria) string {
	if crit == nil {
		return ""
	}
	var base queryBuilder

	// recipients
	var from queryBuilder
	for _, f := range crit.From {
		from.or("from:" + opt.QuoteArg(f))
	}
	if from.s != "" {
		base.and(from.s)
	}

	var to queryBuilder
	for _, t := range crit.To {
		to.or("to:" + opt.QuoteArg(t))
	}
	if to.s != "" {
		base.and(to.s)
	}

	var cc queryBuilder
	for _, c := range crit.Cc {
		cc.or("cc:" + opt.QuoteArg(c))
	}
	if cc.s != "" {
		base.and(cc.s)
	}

	// flags
	for _, f := range []models.Flags{models.SeenFlag, models.AnsweredFlag, models.FlaggedFlag} {
		if crit.WithFlags.Has(f) {
			base.and(getParsedFlag(f, false))
		}
		if crit.WithoutFlags.Has(f) {
			base.and(getParsedFlag(f, true))
		}
	}

	// dates
	switch {
	case !crit.StartDate.IsZero() && !crit.EndDate.IsZero():
		base.and(fmt.Sprintf("date:@%d..@%d",
			crit.StartDate.Unix(), crit.EndDate.Unix()))
	case !crit.StartDate.IsZero():
		base.and(fmt.Sprintf("date:@%d..", crit.StartDate.Unix()))
	case !crit.EndDate.IsZero():
		base.and(fmt.Sprintf("date:..@%d", crit.EndDate.Unix()))
	}

	// other terms
	if crit.Terms != "" {
		if crit.SearchBody {
			base.and("body:" + opt.QuoteArg(crit.Terms))
		} else {
			base.and(crit.Terms)
		}
	}

	return base.s
}

func getParsedFlag(flag models.Flags, inverse bool) string {
	name := ""
	switch flag {
	case models.AnsweredFlag:
		name = "tag:replied"
	case models.SeenFlag:
		name = "tag:unread"
		inverse = !inverse
	case models.FlaggedFlag:
		name = "tag:flagged"
	}
	if inverse {
		name = "not " + name
	}
	return name
}