Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Remove old setreaction callbacks and use new after/before callbacks #33309

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions apps/meteor/app/reactions/client/methods/setReaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { ServerMethods } from '@rocket.chat/ddp-client';
import { Meteor } from 'meteor/meteor';

import { roomCoordinator } from '../../../../client/lib/rooms/roomCoordinator';
import { callbacks } from '../../../../lib/callbacks';
import { emoji } from '../../../emoji/client';
import { Messages, ChatRoom, Subscriptions } from '../../../models/client';

Expand Down Expand Up @@ -55,10 +54,8 @@ Meteor.methods<ServerMethods>({
if (!message.reactions || typeof message.reactions !== 'object' || Object.keys(message.reactions).length === 0) {
delete message.reactions;
Messages.update({ _id: messageId }, { $unset: { reactions: 1 } });
await callbacks.run('unsetReaction', messageId, reaction);
} else {
Messages.update({ _id: messageId }, { $set: { reactions: message.reactions } });
await callbacks.run('setReaction', messageId, reaction);
}
} else {
if (!message.reactions) {
Expand All @@ -72,7 +69,6 @@ Meteor.methods<ServerMethods>({
message.reactions[reaction].usernames.push(user.username);

Messages.update({ _id: messageId }, { $set: { reactions: message.reactions } });
await callbacks.run('setReaction', messageId, reaction);
}
},
});
2 changes: 0 additions & 2 deletions apps/meteor/app/reactions/server/setReaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ async function setReaction(room: IRoom, user: IUser, message: IMessage, reaction
await Rooms.setReactionsInLastMessage(room._id, message.reactions);
}
}
await callbacks.run('unsetReaction', message._id, reaction);
await callbacks.run('afterUnsetReaction', message, { user, reaction, shouldReact, oldMessage });

isReacted = false;
Expand All @@ -104,7 +103,6 @@ async function setReaction(room: IRoom, user: IUser, message: IMessage, reaction
await Rooms.setReactionsInLastMessage(room._id, message.reactions);
void notifyOnRoomChangedById(room._id);
}
await callbacks.run('setReaction', message._id, reaction);
await callbacks.run('afterSetReaction', message, { user, reaction, shouldReact });

isReacted = true;
Expand Down
22 changes: 10 additions & 12 deletions apps/meteor/app/slackbridge/server/RocketAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ export default class RocketAdapter {
rocketLogger.debug('Register for events');
callbacks.add('afterSaveMessage', this.onMessage.bind(this), callbacks.priority.LOW, 'SlackBridge_Out');
callbacks.add('afterDeleteMessage', this.onMessageDelete.bind(this), callbacks.priority.LOW, 'SlackBridge_Delete');
callbacks.add('setReaction', this.onSetReaction.bind(this), callbacks.priority.LOW, 'SlackBridge_SetReaction');
callbacks.add('unsetReaction', this.onUnSetReaction.bind(this), callbacks.priority.LOW, 'SlackBridge_UnSetReaction');
callbacks.add('afterSetReaction', this.onSetReaction.bind(this), callbacks.priority.LOW, 'SlackBridge_SetReaction');
callbacks.add('afterUnsetReaction', this.onUnSetReaction.bind(this), callbacks.priority.LOW, 'SlackBridge_UnSetReaction');
}

unregisterForEvents() {
rocketLogger.debug('Unregister for events');
callbacks.remove('afterSaveMessage', 'SlackBridge_Out');
callbacks.remove('afterDeleteMessage', 'SlackBridge_Delete');
callbacks.remove('setReaction', 'SlackBridge_SetReaction');
callbacks.remove('unsetReaction', 'SlackBridge_UnSetReaction');
callbacks.remove('afterSetReaction', 'SlackBridge_SetReaction');
callbacks.remove('afterUnsetReaction', 'SlackBridge_UnSetReaction');
}

async onMessageDelete(rocketMessageDeleted) {
Expand All @@ -72,20 +72,19 @@ export default class RocketAdapter {
}
}

async onSetReaction(rocketMsgID, reaction) {
async onSetReaction(rocketMsg, { reaction }) {
try {
if (!this.slackBridge.isReactionsEnabled) {
return;
}

rocketLogger.debug('onRocketSetReaction');

if (rocketMsgID && reaction) {
if (this.slackBridge.reactionsMap.delete(`set${rocketMsgID}${reaction}`)) {
if (rocketMsg._id && reaction) {
if (this.slackBridge.reactionsMap.delete(`set${rocketMsg._id}${reaction}`)) {
// This was a Slack reaction, we don't need to tell Slack about it
return;
}
const rocketMsg = await Messages.findOneById(rocketMsgID);
if (rocketMsg) {
for await (const slack of this.slackAdapters) {
const slackChannel = slack.getSlackChannel(rocketMsg.rid);
Expand All @@ -101,21 +100,20 @@ export default class RocketAdapter {
}
}

async onUnSetReaction(rocketMsgID, reaction) {
async onUnSetReaction(rocketMsg, { reaction }) {
try {
if (!this.slackBridge.isReactionsEnabled) {
return;
}

rocketLogger.debug('onRocketUnSetReaction');

if (rocketMsgID && reaction) {
if (this.slackBridge.reactionsMap.delete(`unset${rocketMsgID}${reaction}`)) {
if (rocketMsg._id && reaction) {
if (this.slackBridge.reactionsMap.delete(`unset${rocketMsg._id}${reaction}`)) {
// This was a Slack unset reaction, we don't need to tell Slack about it
return;
}

const rocketMsg = await Messages.findOneById(rocketMsgID);
if (rocketMsg) {
for await (const slack of this.slackAdapters) {
const slackChannel = slack.getSlackChannel(rocketMsg.rid);
Expand Down
2 changes: 0 additions & 2 deletions apps/meteor/lib/callbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,8 @@ export type Hook =
| 'onValidateLogin'
| 'openBroadcast'
| 'renderNotification'
| 'setReaction'
| 'streamMessage'
| 'streamNewMessage'
| 'unsetReaction'
| 'userAvatarSet'
| 'userConfirmationEmailRequested'
| 'userForgotPasswordEmailRequested'
Expand Down
Loading