Skip to content

Commit

Permalink
Merge branch 'develop' into chore/create-edit-rooms
Browse files Browse the repository at this point in the history
  • Loading branch information
kodiakhq[bot] authored Apr 1, 2024
2 parents 2d4569d + 9b53024 commit 00c2392
Show file tree
Hide file tree
Showing 40 changed files with 222 additions and 132 deletions.
8 changes: 4 additions & 4 deletions apps/meteor/app/authentication/server/startup/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Apps, AppEvents } from '@rocket.chat/apps';
import { Roles, Settings, Users } from '@rocket.chat/models';
import { escapeRegExp, escapeHTML } from '@rocket.chat/string-helpers';
import { Accounts } from 'meteor/accounts-base';
import { Match } from 'meteor/check';
import { Meteor } from 'meteor/meteor';
import _ from 'underscore';

import { AppEvents, Apps } from '../../../../ee/server/apps/orchestrator';
import { callbacks } from '../../../../lib/callbacks';
import { beforeCreateUserCallback } from '../../../../lib/callbacks/beforeCreateUserCallback';
import { parseCSV } from '../../../../lib/utils/parseCSV';
Expand Down Expand Up @@ -350,8 +350,8 @@ const insertUserDocAsync = async function (options, user) {

if (!options.skipAppsEngineEvent) {
// `post` triggered events don't need to wait for the promise to resolve
Apps.triggerEvent(AppEvents.IPostUserCreated, { user, performedBy: await safeGetMeteorUser() }).catch((e) => {
Apps.getRocketChatLogger().error('Error while executing post user created event:', e);
Apps?.triggerEvent(AppEvents.IPostUserCreated, { user, performedBy: await safeGetMeteorUser() }).catch((e) => {
Apps?.getRocketChatLogger().error('Error while executing post user created event:', e);
});
}

Expand Down Expand Up @@ -424,7 +424,7 @@ const validateLoginAttemptAsync = async function (login) {
*/
if (login.type !== 'resume') {
// App IPostUserLoggedIn event hook
await Apps.triggerEvent(AppEvents.IPostUserLoggedIn, login.user);
await Apps?.triggerEvent(AppEvents.IPostUserLoggedIn, login.user);
}

return true;
Expand Down
4 changes: 2 additions & 2 deletions apps/meteor/app/file-upload/server/lib/FileUpload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import stream from 'stream';
import URL from 'url';

import { hashLoginToken } from '@rocket.chat/account-utils';
import { Apps, AppEvents } from '@rocket.chat/apps';
import { AppsEngineException } from '@rocket.chat/apps-engine/definition/exceptions';
import type { IUpload } from '@rocket.chat/core-typings';
import { Users, Avatars, UserDataFiles, Uploads, Settings, Subscriptions, Messages, Rooms } from '@rocket.chat/models';
Expand All @@ -21,7 +22,6 @@ import sharp from 'sharp';
import type { WritableStreamBuffer } from 'stream-buffers';
import streamBuffers from 'stream-buffers';

import { AppEvents, Apps } from '../../../../ee/server/apps';
import { i18n } from '../../../../server/lib/i18n';
import { SystemLogger } from '../../../../server/lib/logger/system';
import { roomCoordinator } from '../../../../server/lib/rooms/roomCoordinator';
Expand Down Expand Up @@ -177,7 +177,7 @@ export const FileUpload = {

// App IPreFileUpload event hook
try {
await Apps.triggerEvent(AppEvents.IPreFileUpload, { file, content: content || Buffer.from([]) });
await Apps?.triggerEvent(AppEvents.IPreFileUpload, { file, content: content || Buffer.from([]) });
} catch (error: any) {
if (error.name === AppsEngineException.name) {
throw new Meteor.Error('error-app-prevented', error.message);
Expand Down
6 changes: 3 additions & 3 deletions apps/meteor/app/lib/server/functions/addUserToRoom.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Apps, AppEvents } from '@rocket.chat/apps';
import { AppsEngineException } from '@rocket.chat/apps-engine/definition/exceptions';
import { Message, Team } from '@rocket.chat/core-services';
import type { IUser } from '@rocket.chat/core-typings';
import { Subscriptions, Users, Rooms } from '@rocket.chat/models';
import { Meteor } from 'meteor/meteor';

import { RoomMemberActions } from '../../../../definition/IRoomTypeConfig';
import { AppEvents, Apps } from '../../../../ee/server/apps';
import { callbacks } from '../../../../lib/callbacks';
import { getSubscriptionAutotranslateDefaultConfig } from '../../../../server/lib/getSubscriptionAutotranslateDefaultConfig';
import { roomCoordinator } from '../../../../server/lib/rooms/roomCoordinator';
Expand Down Expand Up @@ -54,7 +54,7 @@ export const addUserToRoom = async function (
}

try {
await Apps.triggerEvent(AppEvents.IPreRoomUserJoined, room, userToBeAdded, inviter);
await Apps?.triggerEvent(AppEvents.IPreRoomUserJoined, room, userToBeAdded, inviter);
} catch (error: any) {
if (error.name === AppsEngineException.name) {
throw new Meteor.Error('error-app-prevented', error.message);
Expand Down Expand Up @@ -118,7 +118,7 @@ export const addUserToRoom = async function (
// Keep the current event
await callbacks.run('afterJoinRoom', userToBeAdded, room);

void Apps.triggerEvent(AppEvents.IPostRoomUserJoined, room, userToBeAdded, inviter);
void Apps?.triggerEvent(AppEvents.IPostRoomUserJoined, room, userToBeAdded, inviter);
});
}

Expand Down
11 changes: 7 additions & 4 deletions apps/meteor/app/lib/server/functions/createDirectRoom.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AppEvents, Apps } from '@rocket.chat/apps';
import { AppsEngineException } from '@rocket.chat/apps-engine/definition/exceptions';
import type { ISubscriptionExtraData } from '@rocket.chat/core-services';
import type { ICreatedRoom, IRoom, ISubscription, IUser } from '@rocket.chat/core-typings';
Expand All @@ -6,7 +7,6 @@ import { Random } from '@rocket.chat/random';
import { Meteor } from 'meteor/meteor';
import type { MatchKeysAndValues } from 'mongodb';

import { Apps } from '../../../../ee/server/apps';
import { callbacks } from '../../../../lib/callbacks';
import { isTruthy } from '../../../../lib/isTruthy';
import { settings } from '../../../settings/server';
Expand Down Expand Up @@ -103,7 +103,7 @@ export async function createDirectRoom(
_USERNAMES: usernames,
};

const prevent = await Apps.triggerEvent('IPreRoomCreatePrevent', tmpRoom).catch((error) => {
const prevent = await Apps?.triggerEvent(AppEvents.IPreRoomCreatePrevent, tmpRoom).catch((error) => {
if (error.name === AppsEngineException.name) {
throw new Meteor.Error('error-app-prevented', error.message);
}
Expand All @@ -115,7 +115,10 @@ export async function createDirectRoom(
throw new Meteor.Error('error-app-prevented', 'A Rocket.Chat App prevented the room creation.');
}

const result = await Apps.triggerEvent('IPreRoomCreateModify', await Apps.triggerEvent('IPreRoomCreateExtend', tmpRoom));
const result = await Apps?.triggerEvent(
AppEvents.IPreRoomCreateModify,
await Apps?.triggerEvent(AppEvents.IPreRoomCreateExtend, tmpRoom),
);

if (typeof result === 'object') {
Object.assign(roomInfo, result);
Expand Down Expand Up @@ -169,7 +172,7 @@ export async function createDirectRoom(

await callbacks.run('afterCreateDirectRoom', insertedRoom, { members: roomMembers, creatorId: options?.creator });

void Apps.triggerEvent('IPostRoomCreate', insertedRoom);
void Apps?.triggerEvent(AppEvents.IPostRoomCreate, insertedRoom);
}

return {
Expand Down
11 changes: 7 additions & 4 deletions apps/meteor/app/lib/server/functions/createRoom.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* eslint-disable complexity */
import { AppEvents, Apps } from '@rocket.chat/apps';
import { AppsEngineException } from '@rocket.chat/apps-engine/definition/exceptions';
import { Message, Team } from '@rocket.chat/core-services';
import type { ICreateRoomParams, ISubscriptionExtraData } from '@rocket.chat/core-services';
import type { ICreatedRoom, IUser, IRoom, RoomType } from '@rocket.chat/core-typings';
import { Rooms, Subscriptions, Users } from '@rocket.chat/models';
import { Meteor } from 'meteor/meteor';

import { Apps } from '../../../../ee/server/apps/orchestrator';
import { callbacks } from '../../../../lib/callbacks';
import { beforeCreateRoomCallback } from '../../../../lib/callbacks/beforeCreateRoomCallback';
import { getSubscriptionAutotranslateDefaultConfig } from '../../../../server/lib/getSubscriptionAutotranslateDefaultConfig';
Expand Down Expand Up @@ -198,7 +198,7 @@ export const createRoom = async <T extends RoomType>(
_USERNAMES: members,
};

const prevent = await Apps.triggerEvent('IPreRoomCreatePrevent', tmp).catch((error) => {
const prevent = await Apps?.triggerEvent(AppEvents.IPreRoomCreatePrevent, tmp).catch((error) => {
if (error.name === AppsEngineException.name) {
throw new Meteor.Error('error-app-prevented', error.message);
}
Expand All @@ -210,7 +210,10 @@ export const createRoom = async <T extends RoomType>(
throw new Meteor.Error('error-app-prevented', 'A Rocket.Chat App prevented the room creation.');
}

const eventResult = await Apps.triggerEvent('IPreRoomCreateModify', await Apps.triggerEvent('IPreRoomCreateExtend', tmp));
const eventResult = await Apps?.triggerEvent(
AppEvents.IPreRoomCreateModify,
await Apps.triggerEvent(AppEvents.IPreRoomCreateExtend, tmp),
);

if (eventResult && typeof eventResult === 'object' && delete eventResult._USERNAMES) {
Object.assign(roomProps, eventResult);
Expand Down Expand Up @@ -242,7 +245,7 @@ export const createRoom = async <T extends RoomType>(
callbacks.runAsync('federation.afterCreateFederatedRoom', room, { owner, originalMemberList: members });
}

void Apps.triggerEvent('IPostRoomCreate', room);
void Apps?.triggerEvent(AppEvents.IPostRoomCreate, room);
return {
rid: room._id, // backwards compatible
inserted: true,
Expand Down
10 changes: 5 additions & 5 deletions apps/meteor/app/lib/server/functions/deleteMessage.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { AppEvents, Apps } from '@rocket.chat/apps';
import { api } from '@rocket.chat/core-services';
import type { AtLeast, IMessage, IUser } from '@rocket.chat/core-typings';
import { Messages, Rooms, Uploads, Users, ReadReceipts } from '@rocket.chat/models';
import { Meteor } from 'meteor/meteor';

import { Apps } from '../../../../ee/server/apps';
import { callbacks } from '../../../../lib/callbacks';
import { broadcastMessageFromData } from '../../../../server/modules/watchers/lib/messages';
import { canDeleteMessageAsync } from '../../../authorization/server/functions/canDeleteMessage';
Expand All @@ -29,14 +29,14 @@ export const deleteMessageValidatingPermission = async (message: AtLeast<IMessag
};

export async function deleteMessage(message: IMessage, user: IUser): Promise<void> {
const deletedMsg = await Messages.findOneById(message._id);
const deletedMsg: IMessage | null = await Messages.findOneById(message._id);
const isThread = (deletedMsg?.tcount || 0) > 0;
const keepHistory = settings.get('Message_KeepHistory') || isThread;
const showDeletedStatus = settings.get('Message_ShowDeletedStatus') || isThread;
const bridges = Apps?.isLoaded() && Apps.getBridges();

if (deletedMsg && bridges) {
const prevent = await bridges.getListenerBridge().messageEvent('IPreMessageDeletePrevent', deletedMsg);
const prevent = await bridges.getListenerBridge().messageEvent(AppEvents.IPreMessageDeletePrevent, deletedMsg);
if (prevent) {
throw new Meteor.Error('error-app-prevented-deleting', 'A Rocket.Chat App prevented the message deleting.');
}
Expand Down Expand Up @@ -95,7 +95,7 @@ export async function deleteMessage(message: IMessage, user: IUser): Promise<voi
});
}

if (bridges) {
void bridges.getListenerBridge().messageEvent('IPostMessageDeleted', deletedMsg, user);
if (bridges && deletedMsg) {
void bridges.getListenerBridge().messageEvent(AppEvents.IPostMessageDeleted, deletedMsg, user);
}
}
6 changes: 3 additions & 3 deletions apps/meteor/app/lib/server/functions/removeUserFromRoom.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Apps, AppEvents } from '@rocket.chat/apps';
import { AppsEngineException } from '@rocket.chat/apps-engine/definition/exceptions';
import { Message, Team } from '@rocket.chat/core-services';
import type { IUser } from '@rocket.chat/core-typings';
import { Subscriptions, Rooms } from '@rocket.chat/models';
import { Meteor } from 'meteor/meteor';

import { AppEvents, Apps } from '../../../../ee/server/apps/orchestrator';
import { afterLeaveRoomCallback } from '../../../../lib/callbacks/afterLeaveRoomCallback';
import { beforeLeaveRoomCallback } from '../../../../lib/callbacks/beforeLeaveRoomCallback';

Expand All @@ -20,7 +20,7 @@ export const removeUserFromRoom = async function (
}

try {
await Apps.triggerEvent(AppEvents.IPreRoomUserLeave, room, user);
await Apps?.triggerEvent(AppEvents.IPreRoomUserLeave, room, user);
} catch (error: any) {
if (error.name === AppsEngineException.name) {
throw new Meteor.Error('error-app-prevented', error.message);
Expand Down Expand Up @@ -67,5 +67,5 @@ export const removeUserFromRoom = async function (
// TODO: CACHE: maybe a queue?
await afterLeaveRoomCallback.run(user, room);

await Apps.triggerEvent(AppEvents.IPostRoomUserLeave, room, user);
await Apps?.triggerEvent(AppEvents.IPostRoomUserLeave, room, user);
};
4 changes: 2 additions & 2 deletions apps/meteor/app/lib/server/functions/saveUser.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Apps, AppEvents } from '@rocket.chat/apps';
import { isUserFederated } from '@rocket.chat/core-typings';
import { Users } from '@rocket.chat/models';
import Gravatar from 'gravatar';
import { Accounts } from 'meteor/accounts-base';
import { Meteor } from 'meteor/meteor';
import _ from 'underscore';

import { AppEvents, Apps } from '../../../../ee/server/apps/orchestrator';
import { callbacks } from '../../../../lib/callbacks';
import { trim } from '../../../../lib/utils/stringUtils';
import { getNewUserRoles } from '../../../../server/services/user/lib/getNewUserRoles';
Expand Down Expand Up @@ -434,7 +434,7 @@ export const saveUser = async function (userId, userData) {
oldUser: oldUserData,
});

await Apps.triggerEvent(AppEvents.IPostUserUpdated, {
await Apps?.triggerEvent(AppEvents.IPostUserUpdated, {
user: userUpdated,
previousUser: oldUserData,
performedBy: await safeGetMeteorUser(),
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/lib/server/functions/sendMessage.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Apps } from '@rocket.chat/apps';
import { Message } from '@rocket.chat/core-services';
import type { IMessage, IRoom } from '@rocket.chat/core-typings';
import { Messages } from '@rocket.chat/models';
import { Match, check } from 'meteor/check';

import { Apps } from '../../../../ee/server/apps';
import { callbacks } from '../../../../lib/callbacks';
import { isRelativeURL } from '../../../../lib/utils/isRelativeURL';
import { isURL } from '../../../../lib/utils/isURL';
Expand Down
35 changes: 18 additions & 17 deletions apps/meteor/app/lib/server/functions/updateMessage.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { AppEvents, Apps } from '@rocket.chat/apps';
import { Message } from '@rocket.chat/core-services';
import type { IEditedMessage, IMessage, IUser, AtLeast } from '@rocket.chat/core-typings';
import type { IMessage, IUser, AtLeast } from '@rocket.chat/core-typings';
import { Messages, Rooms } from '@rocket.chat/models';
import { Meteor } from 'meteor/meteor';

import { Apps } from '../../../../ee/server/apps';
import { callbacks } from '../../../../lib/callbacks';
import { broadcastMessageFromData } from '../../../../server/modules/watchers/lib/messages';
import { settings } from '../../../settings/server';
Expand All @@ -16,49 +16,50 @@ export const updateMessage = async function (
previewUrls?: string[],
): Promise<void> {
const originalMessage = originalMsg || (await Messages.findOneById(message._id));
if (!originalMessage) {
throw new Error('Invalid message ID.');
}

let messageData: IMessage = Object.assign({}, originalMessage, message);

// For the Rocket.Chat Apps :)
if (message && Apps && Apps.isLoaded()) {
const appMessage = Object.assign({}, originalMessage, message);

const prevent = await Apps.getBridges()?.getListenerBridge().messageEvent('IPreMessageUpdatedPrevent', appMessage);
const prevent = await Apps.getBridges().getListenerBridge().messageEvent(AppEvents.IPreMessageUpdatedPrevent, messageData);
if (prevent) {
throw new Meteor.Error('error-app-prevented-updating', 'A Rocket.Chat App prevented the message updating.');
}

let result;
result = await Apps.getBridges()?.getListenerBridge().messageEvent('IPreMessageUpdatedExtend', appMessage);
result = await Apps.getBridges()?.getListenerBridge().messageEvent('IPreMessageUpdatedModify', result);
let result = await Apps.getBridges().getListenerBridge().messageEvent(AppEvents.IPreMessageUpdatedExtend, messageData);
result = await Apps.getBridges().getListenerBridge().messageEvent(AppEvents.IPreMessageUpdatedModify, result);

if (typeof result === 'object') {
message = Object.assign(appMessage, result);
Object.assign(messageData, result);
}
}

// If we keep history of edits, insert a new message to store history information
if (settings.get('Message_KeepHistory')) {
await Messages.cloneAndSaveAsHistoryById(message._id, user as Required<Pick<IUser, '_id' | 'username' | 'name'>>);
await Messages.cloneAndSaveAsHistoryById(messageData._id, user as Required<Pick<IUser, '_id' | 'username' | 'name'>>);
}

Object.assign<AtLeast<IMessage, '_id' | 'rid' | 'msg'>, Omit<IEditedMessage, keyof IMessage>>(message, {
Object.assign(messageData, {
editedAt: new Date(),
editedBy: {
_id: user._id,
username: user.username,
},
});

parseUrlsInMessage(message, previewUrls);
parseUrlsInMessage(messageData, previewUrls);

const room = await Rooms.findOneById(message.rid);
const room = await Rooms.findOneById(messageData.rid);
if (!room) {
return;
}

// TODO remove type cast
message = await Message.beforeSave({ message: message as IMessage, room, user });
messageData = await Message.beforeSave({ message: messageData, room, user });

const { _id, ...editedMessage } = message;
const { _id, ...editedMessage } = messageData;

if (!editedMessage.msg) {
delete editedMessage.md;
Expand All @@ -78,7 +79,7 @@ export const updateMessage = async function (
if (Apps?.isLoaded()) {
// This returns a promise, but it won't mutate anything about the message
// so, we don't really care if it is successful or fails
void Apps.getBridges()?.getListenerBridge().messageEvent('IPostMessageUpdated', message);
void Apps.getBridges()?.getListenerBridge().messageEvent(AppEvents.IPostMessageUpdated, messageData);
}

setImmediate(async () => {
Expand Down
4 changes: 2 additions & 2 deletions apps/meteor/app/lib/server/methods/deleteUserOwnAccount.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Apps, AppEvents } from '@rocket.chat/apps';
import { Users } from '@rocket.chat/models';
import { SHA256 } from '@rocket.chat/sha256';
import type { ServerMethods } from '@rocket.chat/ui-contexts';
import { Accounts } from 'meteor/accounts-base';
import { check } from 'meteor/check';
import { Meteor } from 'meteor/meteor';

import { AppEvents, Apps } from '../../../../ee/server/apps/orchestrator';
import { trim } from '../../../../lib/utils/stringUtils';
import { settings } from '../../../settings/server';
import { deleteUser } from '../functions/deleteUser';
Expand Down Expand Up @@ -66,7 +66,7 @@ Meteor.methods<ServerMethods>({
await deleteUser(uid, confirmRelinquish);

// App IPostUserDeleted event hook
await Apps.triggerEvent(AppEvents.IPostUserDeleted, { user });
await Apps?.triggerEvent(AppEvents.IPostUserDeleted, { user });

return true;
},
Expand Down
Loading

0 comments on commit 00c2392

Please sign in to comment.