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

Chore: Replace promises helper #23488

Merged
merged 2 commits into from
Oct 20, 2021
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: 2 additions & 2 deletions app/lib/client/methods/sendMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import s from 'underscore.string';
import { ChatMessage } from '../../../models';
import { settings } from '../../../settings';
import { callbacks } from '../../../callbacks';
import { promises } from '../../../promises/client';
import { t } from '../../../utils/client';
import { dispatchToastMessage } from '../../../../client/lib/toast';
import { onClientMessageReceived } from '../../../../client/lib/onClientMessageReceived';

Meteor.methods({
sendMessage(message) {
Expand All @@ -32,7 +32,7 @@ Meteor.methods({
message.unread = true;
}
message = callbacks.run('beforeSaveMessage', message);
promises.run('onClientMessageReceived', message).then(function(message) {
onClientMessageReceived(message).then(function(message) {
ChatMessage.insert(message);
return callbacks.run('afterSaveMessage', message);
});
Expand Down
11 changes: 6 additions & 5 deletions app/otr/client/rocketchat.otr.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { ReactiveVar } from 'meteor/reactive-var';
import { Tracker } from 'meteor/tracker';

import { Subscriptions } from '../../models';
import { promises } from '../../promises/client';
import { Notifications } from '../../notifications';
import { t } from '../../utils';
import { onClientMessageReceived } from '../../../client/lib/onClientMessageReceived';
import { onClientBeforeSendMessage } from '../../../client/lib/onClientBeforeSendMessage';

class OTRClass {
constructor() {
Expand Down Expand Up @@ -54,7 +55,7 @@ Meteor.startup(function() {
}
});

promises.add('onClientBeforeSendMessage', function(message) {
onClientBeforeSendMessage.use(function(message) {
if (message.rid && OTR.getInstanceByRoomId(message.rid) && OTR.getInstanceByRoomId(message.rid).established.get()) {
return OTR.getInstanceByRoomId(message.rid).encrypt(message)
.then((msg) => {
Expand All @@ -64,9 +65,9 @@ Meteor.startup(function() {
});
}
return Promise.resolve(message);
}, promises.priority.HIGH);
});

promises.add('onClientMessageReceived', function(message) {
onClientMessageReceived.use(function(message) {
if (message.rid && OTR.getInstanceByRoomId(message.rid) && OTR.getInstanceByRoomId(message.rid).established.get()) {
if (message.notification) {
message.msg = t('Encrypted_message');
Expand Down Expand Up @@ -105,5 +106,5 @@ Meteor.startup(function() {
message.msg = '';
}
return Promise.resolve(message);
}, promises.priority.HIGH);
});
});
5 changes: 0 additions & 5 deletions app/promises/client/index.js

This file was deleted.

84 changes: 0 additions & 84 deletions app/promises/lib/promises.js

This file was deleted.

5 changes: 0 additions & 5 deletions app/promises/server/index.js

This file was deleted.

4 changes: 2 additions & 2 deletions app/ui-utils/client/lib/RoomHistoryManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import differenceInMilliseconds from 'date-fns/differenceInMilliseconds';
import { Emitter } from '@rocket.chat/emitter';
import { escapeHTML } from '@rocket.chat/string-helpers';

import { promises } from '../../../promises/client';
import { RoomManager } from './RoomManager';
import { readMessage } from './readMessages';
import { renderMessageBody } from '../../../../client/lib/utils/renderMessageBody';
Expand All @@ -16,6 +15,7 @@ import { ChatMessage, ChatSubscription, ChatRoom } from '../../../models';
import { callWithErrorHandling } from '../../../../client/lib/utils/callWithErrorHandling';
import { filterMarkdown } from '../../../markdown/lib/markdown';
import { getUserPreference } from '../../../utils/client';
import { onClientMessageReceived } from '../../../../client/lib/onClientMessageReceived';

export const normalizeThreadMessage = ({ ...message }) => {
if (message.msg) {
Expand Down Expand Up @@ -69,7 +69,7 @@ export const upsertMessage = async ({ msg, subscription, uid = Tracker.nonreacti
if (msg.t === 'e2e' && !msg.file) {
msg.e2e = 'pending';
}
msg = await promises.run('onClientMessageReceived', msg) || msg;
msg = await onClientMessageReceived(msg) || msg;

const { _id, ...messageToUpsert } = msg;

Expand Down
4 changes: 2 additions & 2 deletions app/ui/client/lib/chatMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
} from '../../../ui-utils/client';
import { settings } from '../../../settings/client';
import { callbacks } from '../../../callbacks/client';
import { promises } from '../../../promises/client';
import { hasAtLeastOnePermission } from '../../../authorization/client';
import { Messages, Rooms, ChatMessage, ChatSubscription } from '../../../models/client';
import { emoji } from '../../../emoji/client';
Expand All @@ -30,6 +29,7 @@ import { prependReplies } from '../../../../client/lib/utils/prependReplies';
import { callWithErrorHandling } from '../../../../client/lib/utils/callWithErrorHandling';
import { handleError } from '../../../../client/lib/utils/handleError';
import { dispatchToastMessage } from '../../../../client/lib/toast';
import { onClientBeforeSendMessage } from '../../../../client/lib/onClientBeforeSendMessage';


const messageBoxState = {
Expand Down Expand Up @@ -284,7 +284,7 @@ export class ChatMessages {
readMessage.readNow(rid);
readMessage.refreshUnreadMark(rid);

const message = await promises.run('onClientBeforeSendMessage', {
const message = await onClientBeforeSendMessage({
_id: Random.id(),
rid,
tshow,
Expand Down
4 changes: 2 additions & 2 deletions app/ui/client/lib/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { e2e } from '../../../e2e/client';
import { Users, ChatSubscription } from '../../../models';
import { getUserPreference } from '../../../utils';
import { getUserAvatarURL } from '../../../utils/lib/getUserAvatarURL';
import { promises } from '../../../promises/client';
import { CustomSounds } from '../../../custom-sounds/client/lib/CustomSounds';
import { getAvatarAsPng } from '../../../../client/lib/utils/getAvatarAsPng';
import { onClientMessageReceived } from '../../../../client/lib/onClientMessageReceived';

export const KonchatNotification = {
notificationStatus: new ReactiveVar(),
Expand All @@ -34,7 +34,7 @@ export const KonchatNotification = {
notify(notification) {
if (window.Notification && Notification.permission === 'granted') {
const message = { rid: notification.payload != null ? notification.payload.rid : undefined, msg: notification.text, notification: true };
return promises.run('onClientMessageReceived', message).then(function(message) {
return onClientMessageReceived(message).then(function(message) {
const requireInteraction = getUserPreference(Meteor.userId(), 'desktopNotificationRequireInteraction');
const n = new Notification(notification.title, {
icon: notification.icon || getUserAvatarURL(notification.payload.sender.username),
Expand Down
4 changes: 2 additions & 2 deletions app/ui/client/views/app/lib/getCommonRoomEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
addMessageToList,
} from '../../../../../ui-utils/client/lib/MessageAction';
import { callWithErrorHandling } from '../../../../../../client/lib/utils/callWithErrorHandling';
import { promises } from '../../../../../promises/client';
import { isURL } from '../../../../../utils/lib/isURL';
import { openUserCard } from '../../../lib/UserCard';
import { messageArgs } from '../../../../../ui-utils/client/lib/messageArgs';
Expand All @@ -22,6 +21,7 @@ import { EmojiEvents } from '../../../../../reactions/client/init';
import { goToRoomById } from '../../../../../../client/lib/utils/goToRoomById';
import { fireGlobalEvent } from '../../../../../../client/lib/utils/fireGlobalEvent';
import { isLayoutEmbedded } from '../../../../../../client/lib/utils/isLayoutEmbedded';
import { onClientBeforeSendMessage } from '../../../../../../client/lib/onClientBeforeSendMessage';

const mountPopover = (e, i, outerContext) => {
let context = $(e.target).parents('.message').data('context');
Expand Down Expand Up @@ -252,7 +252,7 @@ export const getCommonRoomEvents = () => ({
return;
}

msgObject = await promises.run('onClientBeforeSendMessage', msgObject);
msgObject = await onClientBeforeSendMessage(msgObject);

const _chatMessages = chatMessages[rid];
if (_chatMessages && await _chatMessages.processSlashCommand(msgObject)) {
Expand Down
1 change: 0 additions & 1 deletion client/importPackages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ import '../app/settings/client';
import '../app/models/client';
import '../app/callbacks/client';
import '../app/notifications/client';
import '../app/promises/client';
import '../app/ui-utils/client';
import '../app/ui-cached-collection/client';
import '../app/action-links/client';
Expand Down
4 changes: 4 additions & 0 deletions client/lib/onClientBeforeSendMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { IMessage } from '../../definition/IMessage';
import { createAsyncTransformChain } from '../../lib/transforms';

export const onClientBeforeSendMessage = createAsyncTransformChain<IMessage>();
4 changes: 4 additions & 0 deletions client/lib/onClientMessageReceived.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { IMessage } from '../../definition/IMessage';
import { createAsyncTransformChain } from '../../lib/transforms';

export const onClientMessageReceived = createAsyncTransformChain<IMessage>();
68 changes: 31 additions & 37 deletions client/startup/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import { Tracker } from 'meteor/tracker';
import { e2e } from '../../app/e2e/client/rocketchat.e2e';
import { Subscriptions, Rooms } from '../../app/models/client';
import { Notifications } from '../../app/notifications/client';
import { promises } from '../../app/promises/client';
import { settings } from '../../app/settings/client';
import { IMessage } from '../../definition/IMessage';
import { IRoom } from '../../definition/IRoom';
import { ISubscription } from '../../definition/ISubscription';
import { onClientBeforeSendMessage } from '../lib/onClientBeforeSendMessage';
import { onClientMessageReceived } from '../lib/onClientMessageReceived';
import { isLayoutEmbedded } from '../lib/utils/isLayoutEmbedded';
import { waitUntilFind } from '../lib/utils/waitUntilFind';

Expand Down Expand Up @@ -40,12 +41,15 @@ Meteor.startup(() => {
});

let observable: Meteor.LiveQueryHandle | null = null;
let offClientMessageReceived: undefined | (() => void);
let offClientBeforeSendMessage: undefined | (() => void);
Tracker.autorun(() => {
if (!e2e.isReady()) {
promises.remove('onClientMessageReceived', 'e2e-decript-message');
offClientMessageReceived?.();
Notifications.unUser('e2ekeyRequest', handle);
observable?.stop();
return promises.remove('onClientBeforeSendMessage', 'e2e');
offClientBeforeSendMessage?.();
return;
}

Notifications.onUser('e2ekeyRequest', handle);
Expand Down Expand Up @@ -92,47 +96,37 @@ Meteor.startup(() => {
},
});

promises.add(
'onClientMessageReceived',
async (msg: IMessage) => {
const e2eRoom = await e2e.getInstanceByRoomId(msg.rid);
if (!e2eRoom || !e2eRoom.shouldConvertReceivedMessages()) {
return msg;
}
return e2e.decryptMessage(msg);
},
promises.priority.HIGH,
'e2e-decript-message',
);
offClientMessageReceived = onClientMessageReceived.use(async (msg: IMessage) => {
const e2eRoom = await e2e.getInstanceByRoomId(msg.rid);
if (!e2eRoom || !e2eRoom.shouldConvertReceivedMessages()) {
return msg;
}
return e2e.decryptMessage(msg);
});

// Encrypt messages before sending
promises.add(
'onClientBeforeSendMessage',
async (message: IMessage) => {
const e2eRoom = await e2e.getInstanceByRoomId(message.rid);
offClientBeforeSendMessage = onClientBeforeSendMessage.use(async (message: IMessage) => {
const e2eRoom = await e2e.getInstanceByRoomId(message.rid);

if (!e2eRoom) {
return message;
}
if (!e2eRoom) {
return message;
}

const subscription = await waitUntilFind(() => Rooms.findOne({ _id: message.rid }));
const subscription = await waitUntilFind(() => Rooms.findOne({ _id: message.rid }));

subscription.encrypted ? e2eRoom.resume() : e2eRoom.pause();
subscription.encrypted ? e2eRoom.resume() : e2eRoom.pause();

if (!(await e2eRoom.shouldConvertSentMessages())) {
return message;
}
if (!(await e2eRoom.shouldConvertSentMessages())) {
return message;
}

// Should encrypt this message.
const msg = await e2eRoom.encrypt(message);
// Should encrypt this message.
const msg = await e2eRoom.encrypt(message);

message.msg = msg;
message.t = 'e2e';
message.e2e = 'pending';
return message;
},
promises.priority.HIGH,
'e2e',
);
message.msg = msg;
message.t = 'e2e';
message.e2e = 'pending';
return message;
});
});
});
Loading