-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathon-xapplepushservice.js
65 lines (54 loc) · 1.47 KB
/
on-xapplepushservice.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
/**
* Copyright (c) Forward Email LLC
* SPDX-License-Identifier: BUSL-1.1
*/
const Aliases = require('#models/aliases');
const getApnCerts = require('#helpers/get-apn-certs');
const refineAndLogError = require('#helpers/refine-and-log-error');
// <https://github.com/nodemailer/wildduck/issues/711>
// eslint-disable-next-line max-params
async function onXAPPLEPUSHSERVICE(
accountID,
deviceToken,
subTopic,
mailboxes,
session,
fn
) {
this.logger.debug('XAPPLEPUSHSERVICE', {
accountID,
deviceToken,
subTopic,
mailboxes,
session
});
try {
await this.refreshSession(session, 'XAPPLEPUSHSERVICE');
// update the alias with the provided data
const alias = await Aliases.findOne({ id: session.user.alias_id })
.select('+aps')
.exec();
if (!alias) throw new TypeError('Alias does not exist');
if (!Array.isArray(alias.aps)) alias.aps = [];
const match = alias.aps.find(
(a) => a.account_id === accountID && a.device_token === deviceToken
);
if (match) {
match.subtopic = subTopic;
match.mailboxes = mailboxes;
} else {
alias.aps.push({
account_id: accountID,
device_token: deviceToken,
subtopic: subTopic,
mailboxes
});
}
await alias.save();
const certs = await getApnCerts(this.client);
fn(null, certs.Mail.topic);
} catch (err) {
fn(refineAndLogError(err, session, true, this));
}
}
module.exports = onXAPPLEPUSHSERVICE;