snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
This commit is contained in:
349
apps/meteor/client/lib/chats/data.ts
Normal file
349
apps/meteor/client/lib/chats/data.ts
Normal file
@@ -0,0 +1,349 @@
|
||||
import { isEncryptedMessageContent, type IEditedMessage, type IMessage, type IRoom, type ISubscription } from '@rocket.chat/core-typings';
|
||||
import { MessageTypes } from '@rocket.chat/message-types';
|
||||
import { Random } from '@rocket.chat/random';
|
||||
import { differenceInMinutes } from 'date-fns';
|
||||
|
||||
import type { DataAPI } from './ChatAPI';
|
||||
import { Messages, Rooms, Subscriptions } from '../../stores';
|
||||
import { sdk } from '../SDKClient';
|
||||
import { hasAtLeastOnePermission, hasPermission } from '../authorization';
|
||||
import { settings } from '../settings';
|
||||
import { getUserId } from '../user';
|
||||
import { mapMessageFromApi } from '../utils/mapMessageFromApi';
|
||||
import { prependReplies } from '../utils/prependReplies';
|
||||
import { upsertThreadMessageInCache } from '../utils/threadMessageUtils';
|
||||
|
||||
export const createDataAPI = ({ rid, tmid }: { rid: IRoom['_id']; tmid: IMessage['_id'] | undefined }): DataAPI => {
|
||||
const composeMessage = async (
|
||||
text: string,
|
||||
{ sendToChannel, quotedMessages, originalMessage }: { sendToChannel?: boolean; quotedMessages: IMessage[]; originalMessage?: IMessage },
|
||||
): Promise<IMessage> => {
|
||||
const msg = await prependReplies(text, quotedMessages);
|
||||
|
||||
const effectiveRID = originalMessage?.rid ?? rid;
|
||||
const effectiveTMID = originalMessage ? originalMessage.tmid : tmid;
|
||||
|
||||
return {
|
||||
_id: originalMessage?._id ?? Random.id(),
|
||||
rid: effectiveRID,
|
||||
...(effectiveTMID && {
|
||||
tmid: effectiveTMID,
|
||||
...(sendToChannel && { tshow: sendToChannel }),
|
||||
}),
|
||||
msg,
|
||||
} as IMessage;
|
||||
};
|
||||
|
||||
const findMessageByID = async (mid: IMessage['_id']): Promise<IMessage | null> =>
|
||||
Messages.state.find((record) => record._id === mid && record._hidden !== true) ??
|
||||
sdk.rest.get('/v1/chat.getMessage', { msgId: mid }).then((response) => mapMessageFromApi(response.message));
|
||||
|
||||
const getMessageByID = async (mid: IMessage['_id']): Promise<IMessage> => {
|
||||
const message = await findMessageByID(mid);
|
||||
|
||||
if (!message) {
|
||||
throw new Error('Message not found');
|
||||
}
|
||||
|
||||
return message;
|
||||
};
|
||||
|
||||
const findLastMessage = async (): Promise<IMessage | undefined> =>
|
||||
Messages.state.findFirst(
|
||||
(record) => record.rid === rid && (tmid ? record.tmid === tmid : !record.tmid) && record._hidden !== true,
|
||||
(a, b) => b.ts.getTime() - a.ts.getTime(),
|
||||
);
|
||||
|
||||
const getLastMessage = async (): Promise<IMessage> => {
|
||||
const message = await findLastMessage();
|
||||
|
||||
if (!message) {
|
||||
throw new Error('Message not found');
|
||||
}
|
||||
|
||||
return message;
|
||||
};
|
||||
|
||||
const canUpdateMessage = async (message: IMessage): Promise<boolean> => {
|
||||
if (MessageTypes.isSystemMessage(message)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const canEditMessage = hasAtLeastOnePermission('edit-message', message.rid);
|
||||
const editAllowed = settings.peek('Message_AllowEditing') ?? false;
|
||||
const editOwn = message?.u && message.u._id === getUserId();
|
||||
|
||||
if (!canEditMessage && (!editAllowed || !editOwn)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const blockEditInMinutes = settings.peek('Message_AllowEditing_BlockEditInMinutes');
|
||||
const bypassBlockTimeLimit = hasPermission('bypass-time-limit-edit-and-delete', message.rid);
|
||||
|
||||
const elapsedMinutes = differenceInMinutes(new Date(), message.ts);
|
||||
if (!bypassBlockTimeLimit && elapsedMinutes && blockEditInMinutes && elapsedMinutes > blockEditInMinutes) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
const findPreviousOwnMessage = async (message?: IMessage): Promise<IMessage | undefined> => {
|
||||
const uid = getUserId();
|
||||
|
||||
if (!uid) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const msg = Messages.state.findFirst(
|
||||
(record) =>
|
||||
record.rid === rid &&
|
||||
(tmid ? record.tmid === tmid : !record.tmid) &&
|
||||
record.u._id === uid &&
|
||||
record._hidden !== true &&
|
||||
record.ts.getTime() < (message?.ts.getTime() ?? Date.now()),
|
||||
(a, b) => b.ts.getTime() - a.ts.getTime(),
|
||||
);
|
||||
|
||||
if (!msg) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (await canUpdateMessage(msg)) {
|
||||
return msg;
|
||||
}
|
||||
|
||||
return findPreviousOwnMessage(msg);
|
||||
};
|
||||
|
||||
const getPreviousOwnMessage = async (message: IMessage): Promise<IMessage> => {
|
||||
const previousMessage = await findPreviousOwnMessage(message);
|
||||
|
||||
if (!previousMessage) {
|
||||
throw new Error('Message not found');
|
||||
}
|
||||
|
||||
return previousMessage;
|
||||
};
|
||||
|
||||
const findNextOwnMessage = async (message: IMessage): Promise<IMessage | undefined> => {
|
||||
const uid = getUserId();
|
||||
|
||||
if (!uid) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const msg = Messages.state.findFirst(
|
||||
(record) =>
|
||||
record.rid === rid &&
|
||||
(tmid ? record.tmid === tmid : !record.tmid) &&
|
||||
record.u._id === uid &&
|
||||
record._hidden !== true &&
|
||||
record.ts.getTime() > message.ts.getTime(),
|
||||
(a, b) => a.ts.getTime() - b.ts.getTime(),
|
||||
);
|
||||
|
||||
if (!msg) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (await canUpdateMessage(msg)) {
|
||||
return msg;
|
||||
}
|
||||
|
||||
return findNextOwnMessage(msg);
|
||||
};
|
||||
|
||||
const getNextOwnMessage = async (message: IMessage): Promise<IMessage> => {
|
||||
const nextMessage = await findNextOwnMessage(message);
|
||||
|
||||
if (!nextMessage) {
|
||||
throw new Error('Message not found');
|
||||
}
|
||||
|
||||
return nextMessage;
|
||||
};
|
||||
|
||||
const pushEphemeralMessage = async (message: Omit<IMessage, 'rid' | 'tmid'>): Promise<void> => {
|
||||
const fullMessage = { ...message, rid, ...(tmid && { tmid }) };
|
||||
Messages.state.store(fullMessage);
|
||||
|
||||
if (tmid) {
|
||||
upsertThreadMessageInCache(fullMessage, rid, tmid);
|
||||
}
|
||||
};
|
||||
|
||||
const updateMessage = async (message: IEditedMessage, previewUrls?: string[]): Promise<void> => {
|
||||
const params = isEncryptedMessageContent(message)
|
||||
? {
|
||||
msgId: message._id,
|
||||
roomId: message.rid,
|
||||
content: message.content,
|
||||
e2eMentions: message.e2eMentions,
|
||||
}
|
||||
: {
|
||||
previewUrls,
|
||||
msgId: message._id,
|
||||
roomId: message.rid,
|
||||
customFields: message.customFields,
|
||||
text: message.msg,
|
||||
};
|
||||
|
||||
await sdk.rest.post('/v1/chat.update', params);
|
||||
};
|
||||
|
||||
const canDeleteMessage = async (message: IMessage): Promise<boolean> => {
|
||||
const uid = getUserId();
|
||||
|
||||
if (!uid) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (MessageTypes.isSystemMessage(message)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const forceDeleteAllowed = hasPermission('force-delete-message', message.rid);
|
||||
if (forceDeleteAllowed) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const deletionEnabled = settings.peek('Message_AllowDeleting');
|
||||
if (!deletionEnabled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const deleteAnyAllowed = hasPermission('delete-message', rid);
|
||||
const deleteOwnAllowed = hasPermission('delete-own-message');
|
||||
const deleteAllowed = deleteAnyAllowed || (deleteOwnAllowed && message?.u && message.u._id === getUserId());
|
||||
|
||||
if (!deleteAllowed) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const blockDeleteInMinutes = settings.peek('Message_AllowDeleting_BlockDeleteInMinutes');
|
||||
const bypassBlockTimeLimit = hasPermission('bypass-time-limit-edit-and-delete', message.rid);
|
||||
const elapsedMinutes = differenceInMinutes(new Date(), message.ts);
|
||||
const onTimeForDelete = bypassBlockTimeLimit || !blockDeleteInMinutes || !elapsedMinutes || elapsedMinutes <= blockDeleteInMinutes;
|
||||
|
||||
return deleteAllowed && onTimeForDelete;
|
||||
};
|
||||
|
||||
const deleteMessage = async (msgIdOrMsg: IMessage | IMessage['_id']): Promise<void> => {
|
||||
let msgId: string;
|
||||
let roomId: string;
|
||||
if (typeof msgIdOrMsg === 'string') {
|
||||
msgId = msgIdOrMsg;
|
||||
const msg = await findMessageByID(msgId);
|
||||
if (!msg) {
|
||||
throw new Error('Message not found');
|
||||
}
|
||||
roomId = msg.rid;
|
||||
} else {
|
||||
msgId = msgIdOrMsg._id;
|
||||
roomId = msgIdOrMsg.rid;
|
||||
}
|
||||
|
||||
await sdk.rest.post('/v1/chat.delete', { msgId, roomId });
|
||||
};
|
||||
|
||||
const drafts = new Map<IMessage['_id'] | undefined, string>();
|
||||
|
||||
const getDraft = async (mid: IMessage['_id'] | undefined): Promise<string | undefined> => drafts.get(mid);
|
||||
|
||||
const discardDraft = async (mid: IMessage['_id'] | undefined): Promise<void> => {
|
||||
drafts.delete(mid);
|
||||
};
|
||||
|
||||
const saveDraft = async (mid: IMessage['_id'] | undefined, draft: string): Promise<void> => {
|
||||
drafts.set(mid, draft);
|
||||
};
|
||||
|
||||
const findRoom = async (): Promise<IRoom | undefined> => Rooms.state.get(rid);
|
||||
|
||||
const getRoom = async (): Promise<IRoom> => {
|
||||
const room = await findRoom();
|
||||
|
||||
if (!room) {
|
||||
throw new Error('Room not found');
|
||||
}
|
||||
|
||||
return room;
|
||||
};
|
||||
|
||||
const isSubscribedToRoom = async (): Promise<boolean> => !!Subscriptions.state.find((record) => record.rid === rid);
|
||||
|
||||
const joinRoom = async (): Promise<void> => {
|
||||
await sdk.rest.post('/v1/rooms.join', { roomId: rid });
|
||||
};
|
||||
|
||||
const findDiscussionByID = async (drid: IRoom['_id']): Promise<IRoom | undefined> =>
|
||||
Rooms.state.find((record) => Boolean(record._id === drid && record.prid));
|
||||
|
||||
const getDiscussionByID = async (drid: IRoom['_id']): Promise<IRoom> => {
|
||||
const discussion = await findDiscussionByID(drid);
|
||||
|
||||
if (!discussion) {
|
||||
throw new Error('Discussion not found');
|
||||
}
|
||||
|
||||
return discussion;
|
||||
};
|
||||
|
||||
const createStrictGetter = <TFind extends (...args: any[]) => Promise<any>>(
|
||||
find: TFind,
|
||||
errorMessage: string,
|
||||
): ((...args: Parameters<TFind>) => Promise<Exclude<Awaited<ReturnType<TFind>>, undefined>>) => {
|
||||
return async (...args) => {
|
||||
const result = await find(...args);
|
||||
|
||||
if (!result) {
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
};
|
||||
|
||||
const findSubscription = async (): Promise<ISubscription | undefined> => {
|
||||
return Subscriptions.state.find((record) => record.rid === rid);
|
||||
};
|
||||
|
||||
const getSubscription = createStrictGetter(findSubscription, 'Subscription not found');
|
||||
|
||||
const findSubscriptionFromMessage = async (message: IMessage): Promise<ISubscription | undefined> => {
|
||||
return Subscriptions.state.find((record) => record.rid === message.rid);
|
||||
};
|
||||
|
||||
const getSubscriptionFromMessage = createStrictGetter(findSubscriptionFromMessage, 'Subscription not found');
|
||||
|
||||
return {
|
||||
composeMessage,
|
||||
findMessageByID,
|
||||
getMessageByID,
|
||||
findLastMessage,
|
||||
getLastMessage,
|
||||
findPreviousOwnMessage,
|
||||
getPreviousOwnMessage,
|
||||
findNextOwnMessage,
|
||||
getNextOwnMessage,
|
||||
pushEphemeralMessage,
|
||||
canUpdateMessage,
|
||||
updateMessage,
|
||||
canDeleteMessage,
|
||||
deleteMessage,
|
||||
getDraft,
|
||||
saveDraft,
|
||||
discardDraft,
|
||||
findRoom,
|
||||
getRoom,
|
||||
isSubscribedToRoom,
|
||||
joinRoom,
|
||||
findDiscussionByID,
|
||||
getDiscussionByID,
|
||||
findSubscription,
|
||||
getSubscription,
|
||||
findSubscriptionFromMessage,
|
||||
getSubscriptionFromMessage,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user