@blocklet/launcher-util 1.9.29 → 1.9.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/notification/email.js +32 -0
- package/lib/notification/index.js +64 -0
- package/package.json +6 -4
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const nodemailer = require('nodemailer');
|
|
2
|
+
|
|
3
|
+
const send = (to, payload, { auth }) => {
|
|
4
|
+
const transporter = nodemailer.createTransport({
|
|
5
|
+
host: auth.host,
|
|
6
|
+
port: auth.port,
|
|
7
|
+
secure: false,
|
|
8
|
+
auth: { user: auth.user, pass: auth.pass },
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
return transporter.sendMail({ from: auth.from, to, subject: payload.subject, html: payload.html });
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const getEmailMessage = ({ title, body, actions }) => {
|
|
15
|
+
let html = `<p>${body}</p>`;
|
|
16
|
+
if (Array.isArray(actions) && actions.length > 0) {
|
|
17
|
+
html += '<p>';
|
|
18
|
+
|
|
19
|
+
actions.forEach((action) => {
|
|
20
|
+
html += `<p>${action.name}: <a href="${action.link}">${action.link}</a></p>`;
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
html += '</p>';
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return {
|
|
27
|
+
subject: title,
|
|
28
|
+
html,
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
module.exports = { send, getEmailMessage };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
const BlockletNotification = require('@blocklet/sdk/service/notification');
|
|
2
|
+
const AuthClient = require('@blocklet/sdk/service/auth');
|
|
3
|
+
|
|
4
|
+
const email = require('./email');
|
|
5
|
+
|
|
6
|
+
const authClient = new AuthClient();
|
|
7
|
+
|
|
8
|
+
class Notification {
|
|
9
|
+
constructor({ logger, auth }) {
|
|
10
|
+
this.auth = auth;
|
|
11
|
+
// eslint-disable-next-line no-console
|
|
12
|
+
this.logger = logger || { info: console.info, error: console.error };
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async sendNotification({ to, title, message, assetAddress, actions }) {
|
|
16
|
+
try {
|
|
17
|
+
const payload = { title, body: message, actions: actions || [] };
|
|
18
|
+
if (typeof assetAddress !== 'undefined') {
|
|
19
|
+
payload.attachments = [
|
|
20
|
+
{
|
|
21
|
+
type: 'asset',
|
|
22
|
+
data: {
|
|
23
|
+
chainHost: this.auth.chainHost,
|
|
24
|
+
did: assetAddress,
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
await this.send(to, payload);
|
|
31
|
+
|
|
32
|
+
this.logger.info('text message was sent', { to, payload: JSON.stringify(payload, null, 2) });
|
|
33
|
+
} catch (error) {
|
|
34
|
+
this.logger.error('send text message failed', { error, to, message });
|
|
35
|
+
throw error;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async send(to, { title, body, actions }) {
|
|
40
|
+
const payload = { title, body, actions };
|
|
41
|
+
|
|
42
|
+
const tasks = [BlockletNotification.sendToUser(to, payload)];
|
|
43
|
+
|
|
44
|
+
if (this.auth.email) {
|
|
45
|
+
const { user } = await authClient.getUser(to);
|
|
46
|
+
if (user && user.email) {
|
|
47
|
+
tasks.push(this.sendToEmail(user.email, email.getEmailMessage(payload), { auth: this.auth.email }));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
await Promise.all(tasks);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async sendToEmail(to, payload, emailConfig) {
|
|
55
|
+
try {
|
|
56
|
+
await email.send(to, payload, emailConfig);
|
|
57
|
+
this.logger.info('send email message successfully', { to });
|
|
58
|
+
} catch (error) {
|
|
59
|
+
this.logger.error('send email message failed', { error, to });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
module.exports = Notification;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blocklet/launcher-util",
|
|
3
|
-
"version": "1.9.
|
|
3
|
+
"version": "1.9.31",
|
|
4
4
|
"description": "Common constants",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"constant"
|
|
@@ -31,13 +31,15 @@
|
|
|
31
31
|
"url": "https://github.com/blocklet/launcher/issues"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@
|
|
34
|
+
"@blocklet/sdk": "^1.8.59",
|
|
35
|
+
"@ocap/util": "^1.18.35",
|
|
35
36
|
"axios": "^0.26.1",
|
|
36
37
|
"lodash.get": "^4.4.2",
|
|
37
38
|
"lodash.pick": "^4.4.0",
|
|
38
39
|
"moment": "^2.29.4",
|
|
39
|
-
"moment-timezone": "^0.5.
|
|
40
|
+
"moment-timezone": "^0.5.40",
|
|
41
|
+
"nodemailer": "^6.8.0",
|
|
40
42
|
"url-join": "^4.0.1"
|
|
41
43
|
},
|
|
42
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "f025b38312c5d9471209ee08151800a6068f0f80"
|
|
43
45
|
}
|