-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
204 lines (179 loc) · 6.58 KB
/
index.js
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
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
* File Name : index.js
* Created at : 2020-11-04
* Updated at : 2020-12-03
* Author : jeefo
* Purpose :
* Description :
* Reference :
.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.*/
// ignore:start
"use strict";
/* globals*/
/* exported*/
// ignore:end
const is = require("@jeefo/utils/is");
const EventEmitter = require("@jeefo/utils/event_emitter");
const http = require("http");
const url_parse = require("url").parse;
const {google} = require("googleapis");
const {isArray} = Array;
const get_message = async (gmail, message) => {
const res = await gmail.users.messages.get({id: message.id, userId: "me"});
if (res.status === 200) {
const mail = res.data;
const {data} = mail.payload.body;
mail.payload.body = Buffer.from(data, "base64").toString("utf8");
return mail;
}
};
const unknown_error = res => {
const error = new Error("Unknown response");
error.response = res;
return error;
};
module.exports = class GmailWatcher extends EventEmitter {
constructor (config) {
super(true);
if (! is.object(config)) {
throw new TypeError(`GmailWatcher(config) is not an object.`);
}
if (! is.object(config.credentials)) {
throw new TypeError(
`GmailWatcher(config.credentials) is not an object.`
);
}
if (! is.string(config.topic_name)) {
throw new TypeError(
`GmailWatcher(config.topic_name) is not a string.`
);
}
if (! isArray(config.label_ids)) {
throw new TypeError(
`GmailWatcher(config.label_ids) is not an array.`
);
}
if (! is.string(config.label_filter_action)) {
throw new TypeError(
`GmailWatcher(config.label_filter_action) is not an array.`
);
}
if (config.history) this.history = config.history;
const {client_id, client_secret, redirect_uris} = config.credentials;
this.config = Object.assign({}, config);
if (! is.number(this.config.pull_interval)) {
this.config.pull_interval = 5e3;
}
this.client = new google.auth.OAuth2(
client_id,
client_secret,
redirect_uris,
);
this.client.on("tokens", async tokens => {
this.emit("tokens", tokens);
this.set_credentials(tokens);
});
}
set_credentials (tokens) {
this.client.setCredentials(tokens);
this.gmail = google.gmail({
auth : this.client,
version : "v1",
});
}
async authorize () {
if (this.config.tokens) {
return this.set_credentials(this.config.tokens);
}
return new Promise(async resolve => {
const server = http.createServer(async (req, res) => {
const {code} = url_parse(req.url, true).query;
if (code) {
console.log(`Got code: '${code}'`);
const {tokens} = await this.client.getToken(code);
this.set_credentials(tokens);
res.end("<h1>Done.</h1>");
server.close();
resolve();
} else {
res.statusCode = 404;
res.end("Not found");
}
});
const {port} = this.config;
server.listen(port, () => {
const options = {
// 'online' (default) or 'offline' (gets refresh_token)
access_type: "offline",
// If you only need one scope you can pass it as a string
scope: "https://www.googleapis.com/auth/gmail.readonly"
};
const url = this.client.generateAuthUrl(options);
console.log(`HTTP Server listening on port: ${port}`);
console.log(url);
});
});
}
async watch () {
const {gmail} = this;
if (! this.history) {
const res = await gmail.users.watch({
userId : "me",
requestBody : {
labelIds : this.config.label_ids,
topicName : this.config.topic_name,
labelFilterAction : this.config.label_filter_action,
}
});
switch (res.status) {
case 200 :
this.history = res.data;
this.emit("history_data", this.history);
break;
case 400 :
await this.stop();
return this.watch();
default:
throw unknown_error(res);
}
}
// Ready to pull request
return new Promise((resolve, reject) => {
const {pull_interval} = this.config;
const retrieve_mails = async histories => {
for (const history of histories) {
for (const message of history.messages) {
this.emit("message", message);
const mail = await get_message(gmail, message);
this.emit("mail", mail);
}
}
};
const pull_request = async () => {
try {
const res = await gmail.users.history.list({
userId : "me",
startHistoryId : this.history.historyId
});
switch (res.status) {
case 200 :
if (res.data.history) {
await retrieve_mails(res.data.history);
}
this.history.historyId = res.data.historyId;
this.emit("history_data", this.history);
this.timeout_id = setTimeout(
pull_request, pull_interval
);
break;
default: throw unknown_error(res);
}
} catch (e) {
reject(e);
}
};
this.timeout_id = setTimeout(pull_request, pull_interval);
});
}
stop () { return this.gmail.users.stop({userId: "me"}); }
};