Skip to content
Snippets Groups Projects
Unverified Commit 81808186 authored by Kevin Aleman's avatar Kevin Aleman Committed by GitHub
Browse files

Chore: Rewrite action-links to ts (#25418)

parent c3de37f6
No related branches found
No related tags found
No related merge requests found
......@@ -11,7 +11,16 @@ Meteor.methods({
const message = actionLinks.getMessage(name, messageId);
const actionLink = message.actionLinks[name];
if (!message) {
throw new Meteor.Error('error-invalid-message', 'Invalid message', { method: 'actionLinkHandler' });
}
// NOTE: based on types (and how FE uses it) this should be the way of doing it
const actionLink = message.actionLinks?.find((action) => action.method_id === name);
if (!actionLink) {
throw new Meteor.Error('error-invalid-actionlink', 'Invalid action link', { method: 'actionLinkHandler' });
}
actionLinks.actions[actionLink.method_id](message, actionLink.params);
},
......
import { IMessage } from '@rocket.chat/core-typings';
import { Meteor } from 'meteor/meteor';
import { getMessageForUser } from '../../../../server/lib/messages/getMessageForUser';
function getMessageById(messageId) {
function getMessageById(messageId: IMessage['_id']): IMessage | undefined {
try {
return Promise.await(getMessageForUser(messageId, Meteor.userId()));
const user = Meteor.userId();
if (!user) {
return;
}
return Promise.await(getMessageForUser(messageId, user));
} catch (e) {
throw new Meteor.Error(e.message, 'Invalid message', {
function: 'actionLinks.getMessage',
......@@ -12,13 +17,15 @@ function getMessageById(messageId) {
}
}
type ActionLinkHandler = (message: IMessage, params?: string, instance?: undefined) => void;
// Action Links namespace creation.
export const actionLinks = {
actions: {},
register(name, funct) {
actions: {} as { [key: string]: ActionLinkHandler },
register(name: string, funct: ActionLinkHandler): void {
actionLinks.actions[name] = funct;
},
getMessage(name, messageId) {
getMessage(name: string, messageId: IMessage['_id']): IMessage | undefined {
const message = getMessageById(messageId);
if (!message) {
......@@ -27,7 +34,7 @@ export const actionLinks = {
});
}
if (!message.actionLinks || !message.actionLinks[name]) {
if (!message.actionLinks?.some((action) => action.method_id === name)) {
throw new Meteor.Error('error-invalid-actionlink', 'Invalid action link', {
function: 'actionLinks.getMessage',
});
......
......@@ -3,7 +3,7 @@ import type { IUser, IMessage } from '@rocket.chat/core-typings';
import { Messages } from '../../../app/models/server/raw';
import { canAccessRoomId } from '../../../app/authorization/server';
export async function getMessageForUser(messageId: IMessage['_id'], uid: IUser['_id']): Promise<IMessage | undefined> {
export async function getMessageForUser(messageId: IMessage['_id'], uid: IUser['_id'] | null): Promise<IMessage | undefined> {
if (!uid) {
throw new Error('error-invalid-user');
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment