Skip to content
This repository was archived by the owner on Jun 27, 2019. It is now read-only.

Commit 6ef66d5

Browse files
extended notifications to also trigger on new contributions from followed organizations
1 parent 945627e commit 6ef66d5

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

server/models/notifications.model.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ module.exports = function (app) {
1717
'following-contribution'
1818
]
1919
},
20-
relatedUserId: { type: String },
21-
relatedContributionId: { type: String },
20+
relatedUserId: { type: String, index: true },
21+
relatedContributionId: { type: String, index: true },
22+
relatedOrganizationId: { type: String, index: true },
2223
relatedCommentId: { type: String },
2324
unseen: { type: Boolean, default: true, index: true },
2425
createdAt: { type: Date, default: Date.now },

server/services/contributions/hooks/notify-followers.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ module.exports = function () {
1515
}
1616

1717
const contribution = hook.result;
18-
const creatorId = hook.result.userId;
18+
const creatorType = hook.result.organizationId ? 'organizations' : 'users';
19+
const creatorId = hook.result.organizationId || hook.result.userId;
1920

2021
// get all followers
2122
const followers = await hook.app.service('follows').find({
2223
query: {
23-
$limit: 1000,
24-
foreignService: 'users',
24+
$limit: 5000,
25+
foreignService: creatorType,
2526
foreignId: creatorId
2627
}
2728
});
@@ -34,6 +35,7 @@ module.exports = function () {
3435
userId: follower.userId,
3536
type: 'following-contribution',
3637
relatedUserId: creatorId,
38+
relatedOrganizationId: hook.result.organizationId || null,
3739
relatedContributionId: contribution._id
3840
});
3941
});

server/services/notifications/notifications.hooks.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ const userSchema = {
5454
}
5555
};
5656

57+
const organizationSchema = {
58+
include: {
59+
service: 'organizations',
60+
nameAs: 'organization',
61+
parentField: 'relatedOrganizationId',
62+
childField: '_id',
63+
query: {
64+
$limit: 1,
65+
$select: ['_id', 'userId', 'name', 'slug', 'logo', 'thumbnails']
66+
}
67+
}
68+
};
69+
5770
module.exports = {
5871
before: {
5972
all: [
@@ -73,6 +86,7 @@ module.exports = {
7386

7487
after: {
7588
all: [
89+
populate({ schema: organizationSchema }),
7690
populate({ schema: contributionSchema }),
7791
populate({ schema: commentSchema }),
7892
populate({ schema: userSchema })

0 commit comments

Comments
 (0)