-
Notifications
You must be signed in to change notification settings - Fork 18
/
InboxScanner.js
118 lines (113 loc) · 4.69 KB
/
InboxScanner.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
/*
* Copyright (c) 2017 Gnome Email Notifications contributors
*
* Gnome Email Notifications Extension is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* Gnome Email Notifications Extension is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with Gnome Documents; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
"use strict";
const Main = imports.ui.main;
const Me = imports.misc.extensionUtils.getCurrentExtension();
const Console = Me.imports.console.Console;
const GLib = imports.gi.GLib;
const Soup = imports.gi.Soup;
const OutlookScanner = Me.imports.OutlookScanner.OutlookScanner;
const GmailScanner = Me.imports.GmailScanner.GmailScanner;
const Gettext = imports.gettext.domain('gmail_notify');
const _ = Gettext.gettext;
/**
* Scans an email account of any supported type using online APIs
*/
var InboxScanner = class {
/**
* Creates a new scanner using a Gnome Online Account
* @param account - Gnome Online Account
* @param {Conf} config - the extension configuration
* @param {number} [timeout=1] - the request timeout in seconds (optional, default is 1 second)
*/
constructor(account, config, timeout = 1) {
this._config = config;
this._account = account;
this._mailbox = account.get_account().presentation_identity;
this._provider = this._account.get_account().provider_type;
this._scanner = this._createScanner();
this._sess = new Soup.Session();
this._console = new Console();
this._sess.set_timeout(timeout);
}
/**
* A callback to execute after the GET request is complete
* @callback requestCallback
* @param {Error} err - any error that occurred
* @param {Array} [folders] - a list of folders containing unread emails
* @param [account] - the Gnome Online Account of the request
*/
/**
* Scans the inbox and returns a callback
* @param {requestCallback} callback
*/
scanInbox(callback) {
const msg = Soup.Message.new("GET", this._scanner.getApiURL());
this._getCurrentToken(token => {
msg.request_headers.append('Authorization', 'Bearer ' + token);
this._sess.send_and_read_async(msg, GLib.PRIORITY_DEFAULT, null, (sess, result) => {
if (msg.get_status() === 200) {
const bytes = sess.send_and_read_finish(result);
const decoder = new TextDecoder('utf-8');
const body = decoder.decode(bytes.get_data());
const folders = this._scanner.parseResponse(body, callback);
callback(null, folders, this._account);
} else if (msg.get_status() !== 2 && msg.get_status() !== 3) {
const err = new Error('Status ' + msg.get_status() + ': ' + msg.get_reason_phrase());
callback(err);
}
});
});
}
/**
* Create a new scanner chosen by the current provider
* @returns {GmailScanner|OutlookScanner} the scanner created
* @private
*/
_createScanner() {
switch (this._provider) {
case 'google':
return new GmailScanner(this._mailbox, this._config);
case 'windows_live':
return new OutlookScanner();
default:
throw new Error("Provider type not found");
}
}
/**
* Returns the most recent auth token for the current Gnome Online Account
* @param callback - a callback that is called with the token as a parameter
* @returns {string} the auth token
* @private
*/
_getCurrentToken(callback) {
this._account.get_oauth2_based().call_get_access_token(null, (proxy, asyncResult) => {
try {
const [, token] = this._account.get_oauth2_based().call_get_access_token_finish(asyncResult);
callback(token);
} catch (err) {
if (!err.message.includes("Goa.Error.Failed")) {
const message = _("Failed to get Authorization for {0}");
Main.notifyError(message.replace("{0}", this._mailbox));
}
this._console.error(err);
}
});
}
};