-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathgm_message.R
421 lines (388 loc) · 12.8 KB
/
gm_message.R
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#' Get a single message
#'
#' Function to retrieve a given message by id
#' @param id message id to access
#' @param user_id gmail user_id to access, special value of 'me' indicates the authenticated user.
#' @param format format of the message returned
#' @references <https://developers.google.com/gmail/api/reference/rest/v1/users.messages>
#' @family message
#' @export
#' @examples
#' \dontrun{
#' my_message <- gm_message(12345)
#' }
gm_message <- function(id,
user_id = "me",
format = c("full", "metadata", "minimal", "raw")) {
stopifnot(
is_string(id),
is_string(user_id)
)
format <- match.arg(format)
gmailr_GET(c("messages", id), user_id,
class = "gmail_message",
query = list(format = format)
)
}
#' Get a list of messages
#'
#' Get a list of messages possibly matching a given query string.
#' @export
#' @param search query to use, same format as gmail search box.
#' @param num_results the number of results to return.
#' @param page_token retrieve a specific page of results
#' @param label_ids restrict search to given labels
#' @param include_spam_trash boolean whether to include the spam and trash folders in the search
#' @inheritParams gm_thread
#' @references <https://developers.google.com/gmail/api/reference/rest/v1/users.messages/list>
#' @family message
#' @examples
#' \dontrun{
#' # Search for R, return 10 results using label 1 including spam and trash folders
#' my_messages <- gm_messages("R", 10, "label_1", TRUE)
#' }
gm_messages <- function(search = NULL,
num_results = NULL,
label_ids = NULL,
include_spam_trash = NULL,
page_token = NULL,
user_id = "me") {
stopifnot(
nullable(is_string)(search),
nullable(is_number)(num_results),
nullable(is_strings)(label_ids),
nullable(is_boolean)(include_spam_trash),
nullable(is_string)(page_token),
is_string(user_id)
)
page_and_trim("messages", user_id, num_results, search, page_token, label_ids, include_spam_trash)
}
#' Send a single message to the trash
#'
#' Function to trash a given message by id. This can be undone by [gm_untrash_message()].
#' @inheritParams gm_message
#' @references <https://developers.google.com/gmail/api/reference/rest/v1/users.messages/trash>
#' @export
#' @family message
#' @examples
#' \dontrun{
#' gm_trash_message("12345")
#' }
gm_trash_message <- function(id, user_id = "me") {
stopifnot(
is_string(id),
is_string(user_id)
)
gmailr_POST(c("messages", id, "trash"), user_id, class = "gmail_message")
}
#' Remove a single message from the trash
#'
#' Function to trash a given message by id. This can be undone by [gm_untrash_message()].
#' @inheritParams gm_message
#' @references <https://developers.google.com/gmail/api/reference/rest/v1/users.messages/trash>
#' @family message
#' @export
#' @examples
#' \dontrun{
#' gm_untrash_message("12345")
#' }
gm_untrash_message <- function(id, user_id = "me") {
stopifnot(
is_string(id),
is_string(user_id)
)
gmailr_POST(c("messages", id, "untrash"), user_id, class = "gmail_message")
}
#' Permanently delete a single message
#'
#' Function to delete a given message by id. This cannot be undone!
#' @inheritParams gm_message
#' @references <https://developers.google.com/gmail/api/reference/rest/v1/users.messages/delete>
#' @family message
#' @export
#' @examples
#' \dontrun{
#' gm_delete_message("12345")
#' }
gm_delete_message <- function(id, user_id = "me") {
stopifnot(
is_string(id),
is_string(user_id)
)
gmailr_DELETE(c("messages", id), user_id, class = "gmail_message")
}
#' Modify the labels on a message
#'
#' Function to modify the labels on a given message by id. Note you need to
#' use the label ID as arguments to this function, not the label name.
#' @param add_labels label IDs to add to the specified message
#' @param remove_labels label IDs to remove from the specified message
#' @inheritParams gm_message
#' @references <https://developers.google.com/gmail/api/reference/rest/v1/users.messages/modify>
#' @family message
#' @export
#' @examples
#' \dontrun{
#' gm_modify_message(12345, add_labels = "label_1")
#' gm_modify_message(12345, remove_labels = "label_1")
#' # add and remove at the same time
#' gm_modify_message(12345, add_labels = "label_2", remove_labels = "label_1")
#' }
gm_modify_message <- function(id,
add_labels = NULL,
remove_labels = NULL,
user_id = "me") {
stopifnot(
is_string(id),
nullable(is_strings)(add_labels),
nullable(is_strings)(remove_labels),
is_string(user_id)
)
gmailr_POST(c("messages", id, "modify"), user_id,
class = "gmail_message",
body = rename("add_labels" = as.list(add_labels), "remove_labels" = as.list(remove_labels)),
encode = "json"
)
}
#' Retrieve an attachment to a message
#'
#' This is a low level function to retrieve an attachment to a message by id of the attachment
#' and message. Most users are better off using [gm_save_attachments()] to
#' automatically save all the attachments in a given message.
#' @param id id of the attachment
#' @param message_id id of the parent message
#' @inheritParams gm_message
#' @references <https://developers.google.com/gmail/api/reference/rest/v1/users.messages.attachments/get>
#' @family message
#' @export
#' @examples
#' \dontrun{
#' my_attachment <- gm_attachment("a32e324b", "12345")
#' # save attachment to a file
#' gm_save_attachment(my_attachment, "photo.jpg")
#' }
gm_attachment <- function(id,
message_id,
user_id = "me") {
stopifnot(
is_string(id),
is_string(message_id),
is_string(user_id)
)
gmailr_GET(c("messages", message_id, "attachments", id),
user_id,
class = "gmail_attachment"
)
}
#' Save the attachment to a file
#'
#' This is a low level function that only works on attachments retrieved with [gm_attachment()].
#' To save an attachment directly from a message see [gm_save_attachments()],
#' which is a higher level interface more suitable for most uses.
#' @param x attachment to save
#' @param filename location to save to
#' @family message
#' @export
#' @examples
#' \dontrun{
#' my_attachment <- gm_attachment("a32e324b", "12345")
#' # save attachment to a file
#' gm_save_attachment(my_attachment, "photo.jpg")
#' }
gm_save_attachment <- function(x, filename) {
stopifnot(
has_class(x, "gmail_attachment"),
is_string(filename)
)
data <- base64url_decode(x$data)
writeBin(object = data, con = filename)
invisible(filename)
}
#' Save attachments to a message
#'
#' Function to retrieve and save all of the attachments to a message by id of the message.
#' @param x message with attachment
#' @param attachment_id id of the attachment to save, if none specified saves all attachments
#' @param path where to save the attachments
#' @inheritParams gm_message
#' @references <https://developers.google.com/gmail/api/reference/rest/v1/users.messages.attachments/get>
#' @family message
#' @export
#' @examples
#' \dontrun{
#' # save all attachments
#' gm_save_attachments(my_message)
#' # save a specific attachment
#' gm_save_attachments(my_message, "a32e324b")
#' }
gm_save_attachments <- function(x,
attachment_id = NULL,
path = ".",
user_id = "me") {
stopifnot(
has_class(x, "gmail_message"),
nullable(is_string)(attachment_id),
valid_path(path),
is_string(user_id)
)
attachments_parts <- if (!is.null(attachment_id)) {
Find(
function(part) {
identical(part$body$attachmentId, attachment_id)
},
x$payload$parts
)
} else {
Filter(
function(part) {
!is.null(part$filename) && part$filename != ""
},
x$payload$parts
)
}
invisible(vapply(attachments_parts, function(part) {
att <- gm_attachment(part$body$attachmentId, x$id, user_id)
gm_save_attachment(att, file.path(path, part$filename))
}, character(1L)))
}
#' Retrieve information about attachments
#'
#' @inheritParams gm_body
#' @param x An object from which to retrieve the attachment information.
#' @return A data.frame with the `filename`, `type`, `size` and `id` of each
#' attachment in the message.
#' @export
gm_attachments <- function(x, ...) {
UseMethod("gm_attachments")
}
#' @export
gm_attachments.gmail_message <- function(x, ...) {
has_attachments <- vlapply(x$payload$parts, function(part) {
!is.null(part$filename) && part$filename != ""
})
filename <- vcapply(x$payload$parts[has_attachments], function(part) part$filename)
type <- vcapply(x$payload$parts[has_attachments], function(part) part$mimeType)
size <- vnapply(x$payload$parts[has_attachments], function(part) as.numeric(part$body$size))
id <- vcapply(x$payload$parts[has_attachments], function(part) part$body$attachmentId)
return(data.frame(filename = filename, type = type, size = size, id = id))
}
#' @export
gm_attachments.gmail_draft <- function(x, ...) {
gm_attachments.gmail_message(x$message)
}
#' Insert a message into the gmail mailbox from a mime message
#'
#' @param mail mime mail message created by mime
#' @param label_ids optional label ids to apply to the message
#' @param type the type of upload to perform
#' @param internal_date_source whether to date the object based on the date of
#' the message or when it was received by gmail.
#' @inheritParams gm_message
#' @references <https://developers.google.com/gmail/api/reference/rest/v1/users.messages/insert>
#' @family message
#' @export
#' @examples
#' \dontrun{
#' gm_insert_message(gm_mime(
#' From = "you@@me.com", To = "any@@one.com",
#' Subject = "hello", "how are you doing?"
#' ))
#' }
gm_insert_message <- function(mail,
label_ids,
type = c("multipart", "media", "resumable"),
internal_date_source = c("dateHeader", "recievedTime"),
user_id = "me") {
mail <- as.character(mail)
stopifnot(
nullable(is_strings)(label_ids),
is_string(user_id)
)
type <- match.arg(type)
internal_date_source <- match.arg(internal_date_source)
gmailr_POST("messages", user_id,
class = "gmail_message",
query = list(uploadType = type, interalDateSource = internal_date_source),
body = jsonlite::toJSON(
auto_unbox = TRUE,
c(not_null(rename(label_ids)),
raw = base64url_encode(mail)
)
),
add_headers("Content-Type" = "application/json")
)
}
#' Import a message into the gmail mailbox from a mime message
#'
#' @inheritParams gm_insert_message
#' @references <https://developers.google.com/gmail/api/reference/rest/v1/users.messages/import>
#' @family message
#' @export
#' @examples
#' \dontrun{
#' gm_import_message(gm_mime(
#' From = "you@@me.com", To = "any@@one.com",
#' Subject = "hello", "how are you doing?"
#' ))
#' }
gm_import_message <- function(mail,
label_ids,
type = c("multipart", "media", "resumable"),
internal_date_source = c("dateHeader", "recievedTime"),
user_id = "me") {
mail <- as.character(mail)
stopifnot(
nullable(is_strings)(label_ids),
is_string(user_id)
)
type <- match.arg(type)
internal_date_source <- match.arg(internal_date_source)
gmailr_POST(c("messages", "import"), user_id,
class = "gmail_message",
query = list(uploadType = type, interalDateSource = internal_date_source),
body = jsonlite::toJSON(
auto_unbox = TRUE,
c(not_null(rename(label_ids)),
raw = base64url_encode(mail)
)
),
add_headers("Content-Type" = "application/json")
)
}
#' Send a message from a mime message
#'
#' @param thread_id the id of the thread to send from.
#' @inheritParams gm_insert_message
#' @references <https://developers.google.com/gmail/api/reference/rest/v1/users.messages/send>
#' @family message
#' @export
#' @examples
#' \dontrun{
#' gm_send_message(gm_mime(
#' from = "you@@me.com", to = "any@@one.com",
#' subject = "hello", "how are you doing?"
#' ))
#' }
gm_send_message <- function(mail,
type = c("multipart", "media", "resumable"),
thread_id = NULL,
user_id = "me") {
mail <- as.character(mail)
stopifnot(
nullable(is_string)(thread_id),
is_string(user_id)
)
type <- match.arg(type)
gmailr_POST(c("messages", "send"), user_id,
class = "gmail_message",
query = list(uploadType = type),
body = jsonlite::toJSON(
auto_unbox = TRUE, null = "null",
c(
threadId = thread_id,
list(raw = base64url_encode(mail))
)
),
add_headers("Content-Type" = "application/json")
)
}