@plusscommunities/pluss-core-aws 1.2.3 → 1.3.0

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.
@@ -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 = (receivers, type, site, id, data, sendPush) => {
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
+ };
@@ -1,25 +1,36 @@
1
1
  const _ = require("lodash");
2
- const getMatchingAudienceTags = require("./getMatchingAudienceTags");
2
+ const getSessionUser = require("../auth/getSessionUser");
3
+ const getMatchingTags = require("./getMatchingTags");
4
+ const isValidAudience = require("./isValidAudience");
3
5
 
4
- module.exports = async (items, types, authkey) => {
5
- const matchingTags = authkey ? await getMatchingAudienceTags(authkey) : [];
6
- console.log("items", items);
7
- console.log("types", types);
8
- console.log("matchingTags", matchingTags);
6
+ module.exports = async (items, validTypes, authkey, site) => {
7
+ let validTags = [];
8
+ if (authkey) {
9
+ const userId = await getSessionUser(authkey);
10
+ validTags = await getMatchingTags(userId, site);
11
+ }
9
12
 
10
13
  return _.filter(items, (item) => {
11
- if (!item.AudienceType) {
12
- return true;
13
- }
14
- const matchingType = _.find(types, (t) => {
15
- return t.type === item.AudienceType;
16
- });
17
- if (!matchingType) {
18
- return false;
19
- }
20
- if (!matchingType.selection && !item.AudienceTypeSelection) {
21
- return true;
14
+ if (!item.AudienceType) return true;
15
+
16
+ if (item.AudienceType === "Custom") {
17
+ return _.some(item.AudienceTypeSelection, (at) =>
18
+ isValidAudience(
19
+ site,
20
+ at.AudienceType,
21
+ at.AudienceTypeSelection,
22
+ validTypes,
23
+ validTags
24
+ )
25
+ );
22
26
  }
23
- return matchingType.selection === item.AudienceTypeSelection;
27
+
28
+ return isValidAudience(
29
+ site,
30
+ item.AudienceType,
31
+ item.AudienceTypeSelection,
32
+ validTypes,
33
+ validTags
34
+ );
24
35
  });
25
36
  };
@@ -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);
@@ -0,0 +1,20 @@
1
+ const indexQuery = require("../../db/common/indexQuery");
2
+ const { log } = require("../");
3
+
4
+ module.exports = async (userId, site) => {
5
+ return new Promise(async (resolve) => {
6
+ const query = {
7
+ IndexName: "UserIndex",
8
+ KeyConditionExpression: `UserId = :userId${
9
+ !site ? "" : " AND Site = :site"
10
+ }`,
11
+ ExpressionAttributeValues: {
12
+ ":userId": userId,
13
+ ":site": site,
14
+ },
15
+ };
16
+ const result = await indexQuery("usertagusers", query);
17
+ log("getMatchingTags", "ResultLength", result.Items.length);
18
+ return resolve(result.Items);
19
+ });
20
+ };
@@ -0,0 +1,25 @@
1
+ const _ = require("lodash");
2
+
3
+ module.exports = (site, type, selection, validTypes, validTags) => {
4
+ // console.log("isValidAudience - site", site);
5
+ // console.log("isValidAudience - type", type);
6
+ // console.log("isValidAudience - selection", selection);
7
+ // console.log("isValidAudience - validTypes", validTypes);
8
+ // console.log("isValidAudience - validTags", validTags);
9
+
10
+ switch (type) {
11
+ case "Category":
12
+ case "UserType":
13
+ return _.some(
14
+ validTypes,
15
+ (t) =>
16
+ t.site === site &&
17
+ t.type === type &&
18
+ (_.isEmpty(selection) || t.selection === selection)
19
+ );
20
+ case "UserTags":
21
+ return _.some(validTags, (t) => t.Site === site && t.TagId === selection);
22
+ default:
23
+ return true;
24
+ }
25
+ };
@@ -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,16 +24,23 @@ 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)) {
32
+ const categoryId =
33
+ data.muteConfig && data.muteConfig.type
34
+ ? `mute_${data.muteConfig.type === "app" ? "app" : "entity"}`
35
+ : null;
36
+
28
37
  messages.push({
29
38
  to: token,
30
39
  sound: "default",
31
40
  body: message,
32
41
  data,
33
42
  badge: 1,
43
+ categoryId,
34
44
  });
35
45
  } else {
36
46
  log(
@@ -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
- module.exports = (receiverKeys, message, type, key, value) => {
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
- if (!user.tokens) {
80
- if (user.token) {
81
- tokens.push(user.token);
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
- } else {
84
- _.values(user.tokens).forEach((entry) => {
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
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plusscommunities/pluss-core-aws",
3
- "version": "1.2.3",
3
+ "version": "1.3.0",
4
4
  "description": "Core extension package for Pluss Communities platform",
5
5
  "scripts": {
6
6
  "patch": "npm version patch",
@@ -1,23 +0,0 @@
1
- const indexQuery = require("../../db/common/indexQuery");
2
- const getSessionUser = require("../auth/getSessionUser");
3
- const getUserPreview = require("../getUserPreview");
4
-
5
- const getUserTags = async (userId) => {
6
- const query = {
7
- IndexName: "UserIndex",
8
- KeyConditionExpression: "UserId = :userId",
9
- ExpressionAttributeValues: {
10
- ":userId": userId,
11
- },
12
- };
13
-
14
- const { Items } = await indexQuery("usertagusers", query);
15
- return Items;
16
- };
17
-
18
- module.exports = async (authkey) => {
19
- const uid = await getSessionUser(authkey);
20
- // const user = await getUserPreview(uid, true, true);
21
-
22
- return await getUserTags(uid);
23
- };