mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-08-26 19:07:17 +02:00
refactor: add new hooks for notifications/websockets
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
This commit is contained in:
@@ -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],
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -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}`,
|
||||
|
||||
Reference in New Issue
Block a user