@plusscommunities/pluss-core-aws 1.2.5 → 1.3.2
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/db/notifications/getNotificationSetting.js +18 -0
- package/db/notifications/publishNotifications.js +10 -2
- package/helper/audience/filterByAudienceType.js +38 -0
- package/helper/audience/getAudience.js +3 -0
- package/notification/prepNotification.js +5 -0
- package/notification/sendNotifications.js +72 -14
- package/package.json +1 -1
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const indexQuery = require("../common/indexQuery");
|
|
2
|
+
|
|
3
|
+
module.exports = async (userId, type = null, id = null) => {
|
|
4
|
+
const query = {
|
|
5
|
+
IndexName: "UserIndex",
|
|
6
|
+
KeyConditionExpression: "UserId = :userId",
|
|
7
|
+
ExpressionAttributeValues: {
|
|
8
|
+
":userId": userId,
|
|
9
|
+
},
|
|
10
|
+
};
|
|
11
|
+
const { Items } = await indexQuery("notificationsettings", query);
|
|
12
|
+
if (type) {
|
|
13
|
+
return Items.find(
|
|
14
|
+
(i) => i.EntityType === type && (!id || i.EntityId === id)
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
return Items;
|
|
18
|
+
};
|
|
@@ -21,7 +21,15 @@ const saveNotification = (receiver, notification) => {
|
|
|
21
21
|
});
|
|
22
22
|
};
|
|
23
23
|
|
|
24
|
-
module.exports = (
|
|
24
|
+
module.exports = (
|
|
25
|
+
receivers,
|
|
26
|
+
type,
|
|
27
|
+
site,
|
|
28
|
+
id,
|
|
29
|
+
data,
|
|
30
|
+
sendPush,
|
|
31
|
+
config = { type: "app", id: null, ignoreMute: false }
|
|
32
|
+
) => {
|
|
25
33
|
const notification = {};
|
|
26
34
|
notification.Timestamp = moment.utc().valueOf();
|
|
27
35
|
notification.Type = type;
|
|
@@ -34,6 +42,6 @@ module.exports = (receivers, type, site, id, data, sendPush) => {
|
|
|
34
42
|
});
|
|
35
43
|
if (sendPush) {
|
|
36
44
|
const notiData = prepNotification(notification);
|
|
37
|
-
sendNotifications(receivers, notiData.Text, type, id, notiData);
|
|
45
|
+
sendNotifications(receivers, notiData.Text, type, id, notiData, config);
|
|
38
46
|
}
|
|
39
47
|
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const _ = require("lodash");
|
|
2
|
+
const getSessionUser = require("../auth/getSessionUser");
|
|
3
|
+
const getMatchingAudienceTypes = require("./getMatchingAudienceTypes");
|
|
4
|
+
const getMatchingTags = require("./getMatchingTags");
|
|
5
|
+
const isValidAudience = require("./isValidAudience");
|
|
6
|
+
|
|
7
|
+
module.exports = async (items, authkey, site) => {
|
|
8
|
+
const validTypes = await getMatchingAudienceTypes(authkey);
|
|
9
|
+
let validTags = [];
|
|
10
|
+
if (site) {
|
|
11
|
+
const userId = await getSessionUser(authkey);
|
|
12
|
+
validTags = await getMatchingTags(userId, site);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return _.filter(items, (item) => {
|
|
16
|
+
if (!item.AudienceType) return true;
|
|
17
|
+
|
|
18
|
+
if (item.AudienceType === "Custom") {
|
|
19
|
+
return _.some(item.AudienceTypeSelection, (at) =>
|
|
20
|
+
isValidAudience(
|
|
21
|
+
site,
|
|
22
|
+
at.AudienceType,
|
|
23
|
+
at.AudienceTypeSelection,
|
|
24
|
+
validTypes,
|
|
25
|
+
validTags
|
|
26
|
+
)
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return isValidAudience(
|
|
31
|
+
site,
|
|
32
|
+
item.AudienceType,
|
|
33
|
+
item.AudienceTypeSelection,
|
|
34
|
+
validTypes,
|
|
35
|
+
validTags
|
|
36
|
+
);
|
|
37
|
+
});
|
|
38
|
+
};
|
|
@@ -132,6 +132,9 @@ module.exports = async (site, audienceType, audienceTypeSelection) => {
|
|
|
132
132
|
site
|
|
133
133
|
);
|
|
134
134
|
|
|
135
|
+
// console.log("categoryMatches", categoryMatches);
|
|
136
|
+
// console.log("typeMatches", typeMatches);
|
|
137
|
+
// console.log("tagMatches", tagMatches);
|
|
135
138
|
return _.unionBy(categoryMatches, typeMatches, tagMatches, "id");
|
|
136
139
|
case "Category":
|
|
137
140
|
return await getCategoryMatches(users, audienceTypeSelection, site);
|
|
@@ -14,6 +14,11 @@ module.exports = (noti) => {
|
|
|
14
14
|
thisNoti.Subtext = noti.Data.title || noti.Data.description;
|
|
15
15
|
thisNoti.Icon = "wrench";
|
|
16
16
|
break;
|
|
17
|
+
case "EntityCommented":
|
|
18
|
+
thisNoti.Text = noti.Data.comment;
|
|
19
|
+
thisNoti.Subtext = noti.Data.title || noti.Data.description;
|
|
20
|
+
thisNoti.Icon = "commenting";
|
|
21
|
+
break;
|
|
17
22
|
case "EventRepChange":
|
|
18
23
|
let eventChangeText = "";
|
|
19
24
|
if (noti.Data.LocationChanged && noti.Data.TimeChanged) {
|
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
const { Expo } = require("expo-server-sdk");
|
|
2
2
|
const _ = require("lodash");
|
|
3
|
+
const moment = require("moment");
|
|
3
4
|
const getUser = require("../db/users/getUser");
|
|
5
|
+
const getNotificationSetting = require("../db/notifications/getNotificationSetting");
|
|
4
6
|
const { log } = require("../helper");
|
|
5
7
|
|
|
6
|
-
const SendNotification = (tokens, message, type, key, params) => {
|
|
8
|
+
const SendNotification = (tokens, message, type, key, params, config) => {
|
|
7
9
|
const logId = log("SendNotification", "input", {
|
|
8
10
|
tokens,
|
|
9
11
|
message,
|
|
10
12
|
type,
|
|
11
13
|
key,
|
|
12
14
|
params,
|
|
15
|
+
config,
|
|
13
16
|
});
|
|
14
17
|
|
|
15
18
|
const expo = new Expo();
|
|
@@ -21,17 +24,24 @@ const SendNotification = (tokens, message, type, key, params) => {
|
|
|
21
24
|
Object.keys(params).forEach((paramKey) => {
|
|
22
25
|
data[paramKey] = params[paramKey];
|
|
23
26
|
});
|
|
27
|
+
data.muteConfig = config;
|
|
24
28
|
log("SendNotification", "params", data, logId);
|
|
25
29
|
|
|
26
30
|
for (const token of tokens) {
|
|
27
31
|
if (Expo.isExpoPushToken(token)) {
|
|
28
|
-
|
|
32
|
+
const msg = {
|
|
29
33
|
to: token,
|
|
30
34
|
sound: "default",
|
|
31
35
|
body: message,
|
|
32
36
|
data,
|
|
33
37
|
badge: 1,
|
|
34
|
-
}
|
|
38
|
+
};
|
|
39
|
+
if (data.muteConfig && !data.muteConfig.ignoreMute) {
|
|
40
|
+
msg.categoryId = data.muteConfig.type
|
|
41
|
+
? `mute_${data.muteConfig.type === "app" ? "app" : "entity"}`
|
|
42
|
+
: "mute_app";
|
|
43
|
+
}
|
|
44
|
+
messages.push(msg);
|
|
35
45
|
} else {
|
|
36
46
|
log(
|
|
37
47
|
"SendNotification",
|
|
@@ -42,6 +52,7 @@ const SendNotification = (tokens, message, type, key, params) => {
|
|
|
42
52
|
}
|
|
43
53
|
}
|
|
44
54
|
|
|
55
|
+
log("SendNotification", "messages", messages);
|
|
45
56
|
const chunks = expo.chunkPushNotifications(messages);
|
|
46
57
|
for (const chunk of chunks) {
|
|
47
58
|
expo
|
|
@@ -59,7 +70,7 @@ const SendNotification = (tokens, message, type, key, params) => {
|
|
|
59
70
|
);
|
|
60
71
|
// send separately
|
|
61
72
|
_.values(error.details).forEach((appTokens) => {
|
|
62
|
-
SendNotification(appTokens, message, type, key, params);
|
|
73
|
+
SendNotification(appTokens, message, type, key, params, config);
|
|
63
74
|
});
|
|
64
75
|
} else {
|
|
65
76
|
log("SendNotification", "SendError", error, logId);
|
|
@@ -68,22 +79,69 @@ const SendNotification = (tokens, message, type, key, params) => {
|
|
|
68
79
|
}
|
|
69
80
|
};
|
|
70
81
|
|
|
71
|
-
|
|
82
|
+
const checkMuted = async (userId, config) => {
|
|
83
|
+
if (!config.ignoreMute) {
|
|
84
|
+
const notiSettings = await getNotificationSetting(userId);
|
|
85
|
+
const logId = log("checkMuted", "notiSettings", notiSettings);
|
|
86
|
+
|
|
87
|
+
const appSetting = notiSettings.find((n) => n.EntityType === "app");
|
|
88
|
+
const isAppMuted = appSetting
|
|
89
|
+
? moment() <= moment(appSetting.Expiry)
|
|
90
|
+
: false;
|
|
91
|
+
log("checkMuted", "isAppMuted", isAppMuted, logId);
|
|
92
|
+
if (isAppMuted) return true;
|
|
93
|
+
|
|
94
|
+
const entitySetting = notiSettings.find(
|
|
95
|
+
(n) => n.EntityType === config.type && n.EntityId === config.id
|
|
96
|
+
);
|
|
97
|
+
const isEntityMuted = entitySetting
|
|
98
|
+
? moment() <= moment(entitySetting.Expiry)
|
|
99
|
+
: false;
|
|
100
|
+
log("checkMuted", "isEntityMuted", isEntityMuted, logId);
|
|
101
|
+
if (isEntityMuted) return true;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return false;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
module.exports = (
|
|
108
|
+
receiverKeys,
|
|
109
|
+
message,
|
|
110
|
+
type,
|
|
111
|
+
key,
|
|
112
|
+
value,
|
|
113
|
+
config = { type: "app", id: null, ignoreMute: false }
|
|
114
|
+
) => {
|
|
115
|
+
const logId = log("sendNotifications", "input", {
|
|
116
|
+
receiverKeys,
|
|
117
|
+
message,
|
|
118
|
+
type,
|
|
119
|
+
key,
|
|
120
|
+
value,
|
|
121
|
+
config,
|
|
122
|
+
});
|
|
72
123
|
const tokens = [];
|
|
73
124
|
const promises = [];
|
|
74
125
|
receiverKeys.forEach((uid) => {
|
|
75
126
|
promises.push(
|
|
76
127
|
new Promise((resolve, reject) => {
|
|
77
128
|
getUser(uid)
|
|
78
|
-
.then((user) => {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
129
|
+
.then(async (user) => {
|
|
130
|
+
try {
|
|
131
|
+
const isMuted = await checkMuted(user.Id, config);
|
|
132
|
+
if (!isMuted) {
|
|
133
|
+
if (!user.tokens) {
|
|
134
|
+
if (user.token) {
|
|
135
|
+
tokens.push(user.token);
|
|
136
|
+
}
|
|
137
|
+
} else {
|
|
138
|
+
_.values(user.tokens).forEach((entry) => {
|
|
139
|
+
tokens.push(entry.Token);
|
|
140
|
+
});
|
|
141
|
+
}
|
|
82
142
|
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
tokens.push(entry.Token);
|
|
86
|
-
});
|
|
143
|
+
} catch (error) {
|
|
144
|
+
log("sendNotifications", "error", error.toString());
|
|
87
145
|
}
|
|
88
146
|
resolve();
|
|
89
147
|
})
|
|
@@ -94,6 +152,6 @@ module.exports = (receiverKeys, message, type, key, value) => {
|
|
|
94
152
|
);
|
|
95
153
|
});
|
|
96
154
|
Promise.all(promises).then((res) => {
|
|
97
|
-
SendNotification(tokens, message, type, key, value);
|
|
155
|
+
SendNotification(tokens, message, type, key, value, config);
|
|
98
156
|
});
|
|
99
157
|
};
|