forked from zenhack/go.notmuch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread.go
126 lines (109 loc) · 3.41 KB
/
thread.go
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
118
119
120
121
122
123
124
125
126
package notmuch
// Copyright © 2015 The go.notmuch Authors. Authors can be found in the AUTHORS file.
// Licensed under the GPLv3 or later.
// See COPYING at the root of the repository for details.
// #cgo LDFLAGS: -lnotmuch
// #include <stdlib.h>
// #include <notmuch.h>
import "C"
import (
"strings"
"time"
"unsafe"
)
// Thread represents a notmuch thread.
type Thread cStruct
func (t *Thread) toC() *C.notmuch_thread_t {
return (*C.notmuch_thread_t)(t.cptr)
}
func (t *Thread) Close() error {
return (*cStruct)(t).doClose(func() error {
C.notmuch_thread_destroy(t.toC())
return nil
})
}
// Subject returns the subject of a thread.
func (t *Thread) Subject() string {
cstr := C.notmuch_thread_get_subject(t.toC())
str := C.GoString(cstr)
return str
}
// ID returns the ID of the thread.
func (t *Thread) ID() string {
return C.GoString(C.notmuch_thread_get_thread_id(t.toC()))
}
// Count returns the total number of messages in the current thread.
func (t *Thread) Count() int {
return int(C.notmuch_thread_get_total_messages(t.toC()))
}
// CountMatched returns the total number of messages in the current thread that
// matched the search.
func (t *Thread) CountMatched() int {
return int(C.notmuch_thread_get_matched_messages(t.toC()))
}
// TopLevelMessages returns an iterator for the top-level messages in the
// current thread in oldest-first order.
func (t *Thread) TopLevelMessages() *Messages {
ret := &Messages{
cptr: unsafe.Pointer(C.notmuch_thread_get_toplevel_messages(t.toC())),
parent: (*cStruct)(t),
}
setGcClose(ret)
return ret
}
// Messages returns an iterator for all messages in the current thread in
// oldest-first order.
func (t *Thread) Messages() *Messages {
msgs := &Messages{
cptr: unsafe.Pointer(C.notmuch_thread_get_messages(t.toC())),
parent: (*cStruct)(t),
}
setGcClose(msgs)
return msgs
}
// Authors returns the list of authors, the first are the authors that matched
// the query whilst the second return are the rest of the authors. All authors
// are ordered by date.
func (t *Thread) Authors() ([]string, []string) {
var matched, unmatched []string
as := C.GoString(C.notmuch_thread_get_authors(t.toC()))
munm := strings.Split(as, "|")
if len(munm) > 1 {
matched = strings.Split(munm[0], ",")
unmatched = strings.Split(munm[1], ",")
} else {
unmatched = strings.Split(munm[0], ",")
}
for i, s := range matched {
matched[i] = strings.Trim(s, " ")
}
for i, s := range unmatched {
unmatched[i] = strings.Trim(s, " ")
}
return matched, unmatched
}
// OldestDate returns the date of the oldest message in the thread.
func (t *Thread) OldestDate() time.Time {
ctime := C.notmuch_thread_get_oldest_date(t.toC())
return time.Unix(int64(ctime), 0)
}
// NewestDate returns the date of the oldest message in the thread.
func (t *Thread) NewestDate() time.Time {
ctime := C.notmuch_thread_get_newest_date(t.toC())
return time.Unix(int64(ctime), 0)
}
// Tags returns the tags for the current thread, returning a *Tags which can be
// used to iterate over all tags using `Tags.Next(Tag)`
//
// Note: In the Notmuch database, tags are stored on individual messages, not
// on threads. So the tags returned here will be all tags of the messages which
// matched the search and which belong to this thread.
func (t *Thread) Tags() *Tags {
ctags := C.notmuch_thread_get_tags(t.toC())
tags := &Tags{
cptr: unsafe.Pointer(ctags),
parent: (*cStruct)(t),
}
setGcClose(tags)
return tags
}