-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
113 lines (96 loc) · 4.15 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
const proactive = require('@ask-utils/proactive-event');
const moment = require("moment");
const axios = require('axios');
const AWS = require('aws-sdk');
const paramsDDB = {
TableName: process.env.DDB_NAME,
Limit: 5000
};
const docClient = new AWS.DynamoDB.DocumentClient();
const Client = proactive.Client;
const MessageAlert = proactive.MessageAlert;
const client = new Client({
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
apiRegion: process.env.API_REGION
})
const PayloadBuilder = MessageAlert.Activated.PayloadFactory.init()
let getSubscriptionEndpoint = `${process.env.SERVER_URL}/api/v1/subscriptions.get?updatedSince=`;
async function sendNotifications() {
let userList = await getUsers();
for (let i = 0; i < userList.length; i++) {
try {
if (userList[i].attributes.hasOwnProperty("personalAccessToken")) {
let headers = {
'X-Auth-Token': userList[i].attributes.personalAccessToken,
'X-User-Id': userList[i].attributes.profileId
};
let userId = userList[i].id;
let total = await getSubscriptionCount(headers);
if (userList[i].attributes.notificationsSettings == 'userMentions' && total.userMentions > 0) {
PayloadBuilder.setMessageCreator(`All Channels, Groups and DMs and ${total.userMentions} user mentions`)
.setMessageCount(total.unreads)
.setMessageStatus('UNREAD')
.getParameter()
client.setEvent(PayloadBuilder.getParameter())
.setReferenceId(userId.slice(-40))
.setExpiryTime(moment().add(1, 'hour').toISOString())
.setRelevantAudience('Unicast', { "user": userId })
.requestEvent()
.then(result => console.log(result))
.catch(result => console.log(result))
}
else if (userList[i].attributes.notificationsSettings == 'unreads' && total.unreads > 0) {
PayloadBuilder.setMessageCreator(`All Channels, Groups and DMs ${total.userMentions == 0 ? `` : ` and ${total.userMentions} user mentions `} `)
.setMessageCount(total.unreads)
.setMessageStatus('UNREAD')
.getParameter()
client.setEvent(PayloadBuilder.getParameter())
.setReferenceId(userId.slice(-40))
.setExpiryTime(moment().add(1, 'hour').toISOString())
.setRelevantAudience('Unicast', { "user": userId })
.requestEvent()
.then(result => console.log(result))
.catch(result => console.log(result))
}
else {
console.log("No new notification right now!")
}
}
} catch (error) {
console.error(error);
}
}
}
async function getSubscriptionCount(headers) {
let updatedSince = moment().subtract(6, 'minutes').toISOString();
return await axios.get(`${getSubscriptionEndpoint}${updatedSince}`,
{ params: {}, headers: headers })
.then(res => res.data)
.then(res => {
if (res.update.length > 0) {
let total = { unreads: 0, userMentions: 0 }
for (let i = 0; i < res.update.length; i++) {
total.unreads += res.update[i].unread;
total.userMentions += res.update[i].userMentions;
}
return total;
}
else{
return { unreads: 0, userMentions: 0 }
}
})
.catch(err => {
return { unreads: 0, userMentions: 0 };
});
}
async function getUsers() {
return docClient.scan(paramsDDB).promise().then((data) => {
return data.Items;
})
.catch(err => console.log(err));
}
module.exports.handler = (event, context, callback) => {
sendNotifications();
callback(null);
};