Skip to content

Commit

Permalink
refactor: add new hooks for notifications/websockets
Browse files Browse the repository at this point in the history
filter:sockets.sendNewNotificationToUid - fires before emitting "event:new_notification"

filter:sockets.sendNewPostToUid - fires before emitting "event:new_post"

filter:sockets.sendNewTopicToUid - fires before emitting "event:new_topic"

filter:user.toggleFollow- fires before follow/unfollow
  • Loading branch information
barisusakli committed Apr 15, 2024
1 parent 8f317c0 commit d2e042d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 25 deletions.
13 changes: 10 additions & 3 deletions src/notifications.js
Expand Up @@ -197,9 +197,13 @@ async function pushToUids(uids, notification) {
await db.sortedSetsRemoveRangeByScore(unreadKeys.concat(readKeys), '-inf', cutoff);
const websockets = require('./socket.io');
if (websockets.server) {
uids.forEach((uid) => {
await Promise.all(uids.map(async (uid) => {
await plugins.hooks.fire('filter:sockets.sendNewNoticationToUid', {
uid,
notification,
});
websockets.in(`uid_${uid}`).emit('event:new_notification', notification);
});
}));
}
}

Expand All @@ -223,7 +227,10 @@ async function pushToUids(uids, notification) {

// Remove uid from recipients list if they have blocked the user triggering the notification
uids = await User.blocks.filterUids(notification.from, uids);
const data = await plugins.hooks.fire('filter:notification.push', { notification: notification, uids: uids });
const data = await plugins.hooks.fire('filter:notification.push', {
notification,
uids,
});
if (!data || !data.notification || !data.uids || !data.uids.length) {
return;
}
Expand Down
31 changes: 22 additions & 9 deletions src/socket.io/helpers.js
Expand Up @@ -28,8 +28,7 @@ SocketHelpers.notifyNew = async function (uid, type, result) {

async function notifyUids(uid, uids, type, result) {
const post = result.posts[0];
const { tid } = post.topic;
const { cid } = post.topic;
const { tid, cid } = post.topic;
uids = await privileges.topics.filterUids('topics:read', tid, uids);
const watchStateUids = uids;

Expand All @@ -49,14 +48,28 @@ async function notifyUids(uid, uids, type, result) {

post.ip = undefined;

data.uidsTo.forEach((toUid) => {
post.categoryWatchState = categoryWatchStates[toUid];
post.topic.isFollowing = topicFollowState[toUid];
websockets.in(`uid_${toUid}`).emit('event:new_post', result);
if (result.topic && type === 'newTopic') {
websockets.in(`uid_${toUid}`).emit('event:new_topic', result.topic);
await Promise.all(data.uidsTo.map(async (toUid) => {
const copyResult = _.cloneDeep(result);
const postToUid = copyResult.posts[0];
postToUid.categoryWatchState = categoryWatchStates[toUid];
postToUid.topic.isFollowing = topicFollowState[toUid];

await plugins.hooks.fire('filter:sockets.sendNewPostToUid', {
uid: toUid,
uidFrom: uid,
post: postToUid,
});

websockets.in(`uid_${toUid}`).emit('event:new_post', copyResult);
if (copyResult.topic && type === 'newTopic') {
await plugins.hooks.fire('filter:sockets.sendNewTopicToUid', {
uid: toUid,
uidFrom: uid,
topic: copyResult.topic,
});
websockets.in(`uid_${toUid}`).emit('event:new_topic', copyResult.topic);
}
});
}));
}

async function getWatchStates(uids, tid, cid) {
Expand Down
30 changes: 18 additions & 12 deletions src/user/follow.js
Expand Up @@ -21,31 +21,37 @@ module.exports = function (User) {
if (parseInt(uid, 10) === parseInt(theiruid, 10)) {
throw new Error('[[error:you-cant-follow-yourself]]');
}
const exists = await User.exists(theiruid);
const [exists, isFollowing] = await Promise.all([
User.exists(theiruid),
User.isFollowing(uid, theiruid),
]);
if (!exists) {
throw new Error('[[error:no-user]]');
}
const isFollowing = await User.isFollowing(uid, theiruid);

await plugins.hooks.fire('filter:user.toggleFollow', {
type,
uid,
theiruid,
isFollowing,
});

if (type === 'follow') {
if (isFollowing) {
throw new Error('[[error:already-following]]');
}
const now = Date.now();
await Promise.all([
db.sortedSetAddBulk([
[`following:${uid}`, now, theiruid],
[`followers:${theiruid}`, now, uid],
]),
await db.sortedSetAddBulk([
[`following:${uid}`, now, theiruid],
[`followers:${theiruid}`, now, uid],
]);
} else {
if (!isFollowing) {
throw new Error('[[error:not-following]]');
}
await Promise.all([
db.sortedSetRemoveBulk([
[`following:${uid}`, theiruid],
[`followers:${theiruid}`, uid],
]),
await db.sortedSetRemoveBulk([
[`following:${uid}`, theiruid],
[`followers:${theiruid}`, uid],
]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/user/notifications.js
Expand Up @@ -214,7 +214,7 @@ UserNotifications.sendTopicNotificationToFollowers = async function (uid, topicD

const notifObj = await notifications.create({
type: 'new-topic',
bodyShort: `[[notifications:user-posted-topic, ${postData.user.displayname}, ${title}]]`,
bodyShort: translator.compile('notifications:user-posted-topic', postData.user.displayname, title),
bodyLong: postData.content,
pid: postData.pid,
path: `/post/${postData.pid}`,
Expand Down

0 comments on commit d2e042d

Please sign in to comment.