Skip to content

Commit

Permalink
Chore: Remove method meteor call on message action (#26213)
Browse files Browse the repository at this point in the history
Co-authored-by: Guilherme Gazzo <guilherme@gazzo.xyz>
Co-authored-by: Yash Rajpal <58601732+yash-rajpal@users.noreply.github.com>
Co-authored-by: Guilherme Gazzo <guilhermegazzo@gmail.com>
Co-authored-by: yash-rajpal <rajpal.yash03@gmail.com>
Co-authored-by: dougfabris <devfabris@gmail.com>
  • Loading branch information
6 people committed Dec 6, 2022
1 parent 7527ee3 commit 146b096
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 24 deletions.
12 changes: 8 additions & 4 deletions apps/meteor/app/message-pin/client/actionButton.ts
Expand Up @@ -106,10 +106,14 @@ Meteor.startup(function () {
// classes: 'clipboard',
context: ['pinned'],
async action(_, props) {
const { message = messageArgs(this).msg } = props;
const permalink = await MessageAction.getPermaLink(message._id);
navigator.clipboard.writeText(permalink);
dispatchToastMessage({ type: 'success', message: TAPi18n.__('Copied') });
try {
const { message = messageArgs(this).msg } = props;
const permalink = await MessageAction.getPermaLink(message._id);
navigator.clipboard.writeText(permalink);
dispatchToastMessage({ type: 'success', message: TAPi18n.__('Copied') });
} catch (e) {
dispatchToastMessage({ type: 'error', message: e });
}
},
condition({ subscription }) {
return !!subscription;
Expand Down
12 changes: 8 additions & 4 deletions apps/meteor/app/message-star/client/actionButton.ts
Expand Up @@ -110,10 +110,14 @@ Meteor.startup(function () {
// classes: 'clipboard',
context: ['starred', 'threads'],
async action(_, props) {
const { message = messageArgs(this).msg } = props;
const permalink = await MessageAction.getPermaLink(message._id);
navigator.clipboard.writeText(permalink);
dispatchToastMessage({ type: 'success', message: TAPi18n.__('Copied') });
try {
const { message = messageArgs(this).msg } = props;
const permalink = await MessageAction.getPermaLink(message._id);
navigator.clipboard.writeText(permalink);
dispatchToastMessage({ type: 'success', message: TAPi18n.__('Copied') });
} catch (e) {
dispatchToastMessage({ type: 'error', message: e });
}
},
condition({ message, subscription, user }) {
if (subscription == null) {
Expand Down
24 changes: 12 additions & 12 deletions apps/meteor/app/ui-utils/client/lib/MessageAction.ts
Expand Up @@ -5,23 +5,23 @@ import { Meteor } from 'meteor/meteor';
import { ReactiveVar } from 'meteor/reactive-var';
import { Tracker } from 'meteor/tracker';
import type { Icon } from '@rocket.chat/fuselage';
import type { IMessage, IUser, ISubscription, IRoom, SettingValue } from '@rocket.chat/core-typings';
import type { IMessage, IUser, ISubscription, IRoom, SettingValue, Serialized } from '@rocket.chat/core-typings';
import type { TranslationKey } from '@rocket.chat/ui-contexts';

import { Messages, Rooms, Subscriptions } from '../../../models/client';
import { roomCoordinator } from '../../../../client/lib/rooms/roomCoordinator';
import type { ToolboxContextValue } from '../../../../client/views/room/contexts/ToolboxContext';
import type { ChatContext } from '../../../../client/views/room/contexts/ChatContext';

const call = (method: string, ...args: any[]): Promise<any> =>
new Promise((resolve, reject) => {
Meteor.call(method, ...args, function (err: any, data: any) {
if (err) {
return reject(err);
}
resolve(data);
});
});
import { APIClient } from '../../../utils/client';

const getMessage = async (msgId: string): Promise<Serialized<IMessage> | null> => {
try {
const { message } = await APIClient.get('/v1/chat.getMessage', { msgId });
return message;
} catch {
return null;
}
};

export const addMessageToList = (messagesList: IMessage[], message: IMessage): IMessage[] => {
// checks if the message is not already on the list
Expand Down Expand Up @@ -176,7 +176,7 @@ export const MessageAction = new (class {
throw new Error('invalid-parameter');
}

const msg = Messages.findOne(msgId) || (await call('getSingleMessage', msgId));
const msg = Messages.findOne(msgId) || (await getMessage(msgId));
if (!msg) {
throw new Error('message-not-found');
}
Expand Down
12 changes: 8 additions & 4 deletions apps/meteor/app/ui-utils/client/lib/messageActionDefault.ts
Expand Up @@ -97,10 +97,14 @@ Meteor.startup(async function () {
// classes: 'clipboard',
context: ['message', 'message-mobile', 'threads', 'federated'],
async action(_, props) {
const { message = messageArgs(this).msg } = props;
const permalink = await MessageAction.getPermaLink(message._id);
navigator.clipboard.writeText(permalink);
dispatchToastMessage({ type: 'success', message: TAPi18n.__('Copied') });
try {
const { message = messageArgs(this).msg } = props;
const permalink = await MessageAction.getPermaLink(message._id);
navigator.clipboard.writeText(permalink);
dispatchToastMessage({ type: 'success', message: TAPi18n.__('Copied') });
} catch (e) {
dispatchToastMessage({ type: 'error', message: e });
}
},
condition({ subscription }) {
return !!subscription;
Expand Down

0 comments on commit 146b096

Please sign in to comment.