-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGApiWrapper.js
More file actions
108 lines (91 loc) · 2.45 KB
/
Copy pathGApiWrapper.js
File metadata and controls
108 lines (91 loc) · 2.45 KB
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
//gapi wrapper
var util = require('util');
var google = require('googleapis');
var promise_ize = require('./promise_ize').promise_ize;
var CLIENT_ID = process.env.CLIENT_ID;
var CLIENT_SECRET = process.env.CLIENT_SECRET;
var REDIRECT_URL = process.env.REDIRECT_URL;
var OAuth2 = google.auth.OAuth2;
var oauth2Client = new OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);
var gmail = google.gmail('v1');
var extend = util._extend;
function getOptions(options) {
var baseOptions = extend({}, {auth: oauth2Client, userId: 'me'});
if (options) {
return extend(baseOptions, options)
} else {
return baseOptions;
}
}
function genericGApiAction(fn, options, handler) {
var options = getOptions(options);
return promise_ize(fn, [options], handler);
}
/* oauth2Client specific */
function getToken(code) {
return promise_ize(oauth2Client.getToken, [code], oauth2Client);
}
function setCredentials () {
return oauth2Client.setCredentials.apply(oauth2Client, arguments);
}
function generateAuthUrl () {
return oauth2Client.generateAuthUrl.apply(oauth2Client, arguments);
}
/* gmail api specific */
function sendMessage(email, threadId) {
var options = {
resource: {
raw: email,
threadId: threadId
}
};
return genericGApiAction(gmail.users.messages.send, options);
}
function listMessages(emailAddress) {
var options = {
q: util.format('to:%s is:unread', emailAddress)
};
return genericGApiAction(gmail.users.messages.list, options);
}
function getMessage(id) {
var options = {
id: id
};
return genericGApiAction(gmail.users.messages.get, options);
}
function modifyMessage(id, addLabelIds, removeLabelIds) {
var options = {
id: id,
resource: {
addLabelIds: addLabelIds || [],
removeLabelIds: removeLabelIds || []
}
};
return genericGApiAction(gmail.users.messages.modify, options);
}
function setMessageRead(id) {
return modifyMessage(id, null, ['UNREAD']);
}
function getProfile() {
return genericGApiAction(gmail.users.getProfile);
}
function watch(topicName) {
var options = {
resource: {
topicName: topicName
}
};
return genericGApiAction(gmail.users.watch, options)
}
module.exports = {
setCredentials: setCredentials,
generateAuthUrl: generateAuthUrl,
getToken: getToken,
getProfile: getProfile,
watch: watch,
sendMessage: sendMessage,
listMessages: listMessages,
getMessage: getMessage,
modifyMessage: modifyMessage,
setMessageRead: setMessageRead
}