-
Notifications
You must be signed in to change notification settings - Fork 8
/
thread.go
168 lines (147 loc) · 5.53 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package nylas
import (
"context"
"net/http"
"net/url"
"github.com/google/go-querystring/query"
)
// Thread combines multiple messages from the same conversation into a single
// first-class object that is similar to what users expect from email clients.
// See: https://docs.nylas.com/reference#threads
type Thread struct {
ID string `json:"id"`
Object string `json:"object"`
AccountID string `json:"account_id"`
Folders []Folder `json:"folders"`
HasAttachments bool `json:"has_attachments"`
FirstMessageTimestamp int64 `json:"first_message_timestamp"`
LastMessageReceivedTimestamp int64 `json:"last_message_received_timestamp"`
LastMessageSentTimestamp int64 `json:"last_message_sent_timestamp"`
LastMessageTimestamp int64 `json:"last_message_timestamp"`
MessageIDs []string `json:"message_ids"`
DraftIDs []string `json:"draft_ids"`
// Only available in expanded view and the body will be missing, see:
// https://docs.nylas.com/reference#views
Messages []Message `json:"messages"`
Drafts []Message `json:"drafts"`
Participants []Participant `json:"participants"`
Labels []Label `json:"labels"`
Snippet string `json:"snippet"`
Starred bool `json:"starred"`
Subject string `json:"subject"`
Unread bool `json:"unread"`
Version int `json:"version"`
}
// ThreadsOptions provides optional parameters to the Threads method.
type ThreadsOptions struct {
View string `url:"view,omitempty"`
Limit int `url:"limit,omitempty"`
Offset int `url:"offset,omitempty"`
// Return threads with a matching literal subject
Subject string `url:"subject,omitempty"`
// Return threads that have been sent or received from the list of email
// addresses. A maximum of 25 emails may be specified
AnyEmail []string `url:"any_email,comma,omitempty"`
// Return threads containing messages sent to this email address
To string `url:"to,omitempty"`
// Return threads containing messages sent from this email address
From string `url:"from,omitempty"`
// Return threads containing messages that were CC'd to this email address
CC string `url:"cc,omitempty"`
// Return threads containing messages that were BCC'd to this email
// address, likely sent from the parent account. (Most SMTP gateways
// remove BCC information.)
BCC string `url:"bcc,omitempty"`
// Return threads in a given folder, or with a given label.
// This parameter supports the name, display_name, or id of a folder or
// label.
In string `url:"in,omitempty"`
// Return threads with one or more unread messages
Unread *bool `url:"unread,omitempty"`
Filename string `url:"filename,omitempty"`
// Return threads whose most recent message was received before this
// Unix-based timestamp.
LastMessageBefore int64 `url:"last_message_before,omitempty"`
// Return threads whose most recent message was received after this
// Unix-based timestamp.
LastMessageAfter int64 `url:"last_message_after,omitempty"`
// Return threads whose first message was received before this
// Unix-based timestamp.
StartedBefore int64 `url:"started_before,omitempty"`
// Return threads whose first message was received after this
// Unix-based timestamp.
StartedAfter int64 `url:"started_after,omitempty"`
}
// Threads returns threads which match the filter specified by parameters.
// See: https://docs.nylas.com/reference#get-threads
func (c *Client) Threads(ctx context.Context, opts *ThreadsOptions) ([]Thread, error) {
req, err := c.newUserRequest(ctx, http.MethodGet, "/threads", nil)
if err != nil {
return nil, err
}
if opts != nil {
vs, err := query.Values(opts)
if err != nil {
return nil, err
}
appendQueryValues(req, vs)
}
var resp []Thread
return resp, c.do(req, &resp)
}
// ThreadsCount returns the count of threads which match the filter specified by
// parameters.
// See: https://docs.nylas.com/reference#get-threads
func (c *Client) ThreadsCount(ctx context.Context, opts *ThreadsOptions) (int, error) {
req, err := c.newUserRequest(ctx, http.MethodGet, "/threads", nil)
if err != nil {
return 0, err
}
if opts == nil {
opts = &ThreadsOptions{}
}
vs, err := query.Values(opts)
if err != nil {
return 0, err
}
vs.Set("view", ViewCount)
appendQueryValues(req, vs)
var resp countResponse
return resp.Count, c.do(req, &resp)
}
// Thread returns a thread by id.
// See: https://docs.nylas.com/reference#threadsid
func (c *Client) Thread(ctx context.Context, id string, expanded bool) (Thread, error) {
req, err := c.newUserRequest(ctx, http.MethodGet, "/threads/"+id, nil)
if err != nil {
return Thread{}, err
}
if expanded {
appendQueryValues(req, url.Values{"view": {ViewExpanded}})
}
var resp Thread
return resp, c.do(req, &resp)
}
// UpdateThreadRequest contains the request parameters required to update a
// thread.
type UpdateThreadRequest struct {
Unread *bool `json:"unread,omitempty"`
Starred *bool `json:"starred,omitempty"`
// FolderID to move this thread to.
FolderID *string `json:"folder_id,omitempty"`
// LabelIDs to overwrite any previous labels with, you must provide
// existing labels such as sent/drafts.
LabelIDs *[]string `json:"label_ids,omitempty"`
}
// UpdateThread updates a thread with the id.
// See: https://docs.nylas.com/reference#threadsid-1
func (c *Client) UpdateThread(
ctx context.Context, id string, updateReq UpdateThreadRequest,
) (Thread, error) {
req, err := c.newUserRequest(ctx, http.MethodPut, "/threads/"+id, &updateReq)
if err != nil {
return Thread{}, err
}
var resp Thread
return resp, c.do(req, &resp)
}