Skip to content

Commit

Permalink
refactor(models): Use Messages Raw model (9/N) (#28693)
Browse files Browse the repository at this point in the history
  • Loading branch information
sampaiodiego authored Mar 29, 2023
1 parent c00eb4b commit a0f4358
Show file tree
Hide file tree
Showing 30 changed files with 135 additions and 701 deletions.
4 changes: 2 additions & 2 deletions apps/meteor/app/federation/server/endpoints/dispatch.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EJSON } from 'meteor/ejson';
import { FederationServers, FederationRoomEvents, Rooms as RoomsRaw, Subscriptions } from '@rocket.chat/models';
import { FederationServers, FederationRoomEvents, Rooms as RoomsRaw, Messages as MessagesRaw, Subscriptions } from '@rocket.chat/models';
import { api } from '@rocket.chat/core-services';
import { eventTypes } from '@rocket.chat/core-typings';

Expand Down Expand Up @@ -325,7 +325,7 @@ const eventHandlers = {
} = event;

// Remove the message
Messages.removeById(messageId);
await MessagesRaw.removeById(messageId);

// Notify the room
void api.broadcast('notify.deleteMessage', roomId, { _id: messageId });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { IUser } from '@rocket.chat/core-typings';
import { Subscriptions } from '@rocket.chat/models';
import { Message } from '@rocket.chat/core-services';

import { Rooms, Messages } from '../../../models/server';
import { Rooms } from '../../../models/server';
import { callbacks } from '../../../../lib/callbacks';

export const addUserToDefaultChannels = async function (user: IUser, silenced?: boolean): Promise<void> {
Expand All @@ -24,7 +25,7 @@ export const addUserToDefaultChannels = async function (user: IUser, silenced?:

// Insert user joined message
if (!silenced) {
Messages.createUserJoinWithRoomIdAndUser(room._id, user);
await Message.saveSystemMessage('uj', room._id, user.username || '', user);
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions apps/meteor/app/lib/server/functions/addUserToRoom.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { AppsEngineException } from '@rocket.chat/apps-engine/definition/exceptions';
import { Meteor } from 'meteor/meteor';
import type { IUser, IRoom } from '@rocket.chat/core-typings';
import { Team } from '@rocket.chat/core-services';
import { Message, Team } from '@rocket.chat/core-services';
import { Subscriptions } from '@rocket.chat/models';

import { AppEvents, Apps } from '../../../../ee/server/apps';
import { callbacks } from '../../../../lib/callbacks';
import { Messages, Rooms, Users } from '../../../models/server';
import { Rooms, Users } from '../../../models/server';
import { roomCoordinator } from '../../../../server/lib/rooms/roomCoordinator';
import { RoomMemberActions } from '../../../../definition/IRoomTypeConfig';

Expand Down Expand Up @@ -85,16 +85,16 @@ export const addUserToRoom = async function (
},
};
if (room.teamMain) {
Messages.createUserAddedToTeamWithRoomIdAndUser(rid, userToBeAdded, extraData);
await Message.saveSystemMessage('added-user-to-team', rid, userToBeAdded.username, userToBeAdded, extraData);
} else {
Messages.createUserAddedWithRoomIdAndUser(rid, userToBeAdded, extraData);
await Message.saveSystemMessage('au', rid, userToBeAdded.username, userToBeAdded, extraData);
}
} else if (room.prid) {
Messages.createUserJoinWithRoomIdAndUserDiscussion(rid, userToBeAdded, { ts: now });
await Message.saveSystemMessage('ut', rid, userToBeAdded.username, userToBeAdded, { ts: now });
} else if (room.teamMain) {
Messages.createUserJoinTeamWithRoomIdAndUser(rid, userToBeAdded, { ts: now });
await Message.saveSystemMessage('ujt', rid, userToBeAdded.username, userToBeAdded, { ts: now });
} else {
Messages.createUserJoinWithRoomIdAndUser(rid, userToBeAdded, { ts: now });
await Message.saveSystemMessage('uj', rid, userToBeAdded.username, userToBeAdded, { ts: now });
}
}

Expand Down
8 changes: 5 additions & 3 deletions apps/meteor/app/lib/server/functions/createRoom.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { AppsEngineException } from '@rocket.chat/apps-engine/definition/exceptions';
import { Meteor } from 'meteor/meteor';
import type { ICreatedRoom, IUser, IRoom, RoomType } from '@rocket.chat/core-typings';
import { Team } from '@rocket.chat/core-services';
import { Message, Team } from '@rocket.chat/core-services';
import type { ICreateRoomParams, ISubscriptionExtraData } from '@rocket.chat/core-services';
import { Rooms, Subscriptions } from '@rocket.chat/models';

import { Apps } from '../../../../ee/server/apps';
import { addUserRolesAsync } from '../../../../server/lib/roles/addUserRoles';
import { callbacks } from '../../../../lib/callbacks';
import { Messages, Users } from '../../../models/server';
import { Users } from '../../../models/server';
import { getValidRoomName } from '../../../utils/server';
import { createDirectRoom } from './createDirectRoom';

Expand Down Expand Up @@ -167,7 +167,9 @@ export const createRoom = async <T extends RoomType>(
if (type === 'c') {
if (room.teamId) {
const team = await Team.getOneById(room.teamId);
team && Messages.createUserAddRoomToTeamWithRoomIdAndUser(team.roomId, room.name, owner);
if (team) {
await Message.saveSystemMessage('user-added-room-to-team', team.roomId, room.name || '', owner);
}
}
callbacks.run('afterCreateChannel', owner, room);
} else if (type === 'p') {
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/lib/server/functions/deleteMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export async function deleteMessage(message: IMessage, user: IUser): Promise<voi
}
} else {
if (!showDeletedStatus) {
MessagesSync.removeById(message._id);
await Messages.removeById(message._id);
}

files.forEach((file) => {
Expand Down
5 changes: 2 additions & 3 deletions apps/meteor/app/lib/server/functions/deleteRoom.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Rooms, Subscriptions } from '@rocket.chat/models';
import { Messages, Rooms, Subscriptions } from '@rocket.chat/models';

import { Messages } from '../../../models/server';
import { callbacks } from '../../../../lib/callbacks';
import { FileUpload } from '../../../file-upload/server';

export const deleteRoom = function (rid: string): void {
FileUpload.removeFilesByRoomId(rid);
Messages.removeByRoomId(rid);
Promise.await(Messages.removeByRoomId(rid));
callbacks.run('beforeDeleteRoom', rid);
Promise.await(Subscriptions.removeByRoomId(rid));
FileUpload.getStore('Avatars').deleteByRoomId(rid);
Expand Down
14 changes: 7 additions & 7 deletions apps/meteor/app/lib/server/functions/removeUserFromRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import { AppsEngineException } from '@rocket.chat/apps-engine/definition/exceptions';
import { Meteor } from 'meteor/meteor';
import type { IUser } from '@rocket.chat/core-typings';
import { Team } from '@rocket.chat/core-services';
import { Message, Team } from '@rocket.chat/core-services';
import { Subscriptions } from '@rocket.chat/models';

import { Rooms, Messages } from '../../../models/server';
import { Rooms } from '../../../models/server';
import { AppEvents, Apps } from '../../../../ee/server/apps';
import { callbacks } from '../../../../lib/callbacks';

Expand Down Expand Up @@ -44,19 +44,19 @@ export const removeUserFromRoom = async function (
};

if (room.teamMain) {
Messages.createUserRemovedFromTeamWithRoomIdAndUser(rid, user, extraData);
await Message.saveSystemMessage('removed-user-from-team', rid, user.username || '', user, extraData);
} else {
Messages.createUserRemovedWithRoomIdAndUser(rid, user, extraData);
await Message.saveSystemMessage('ru', rid, user.username || '', user, extraData);
}
} else if (room.teamMain) {
Messages.createUserLeaveTeamWithRoomIdAndUser(rid, removedUser);
await Message.saveSystemMessage('ult', rid, removedUser.username || '', removedUser);
} else {
Messages.createUserLeaveWithRoomIdAndUser(rid, removedUser);
await Message.saveSystemMessage('ul', rid, removedUser.username || '', removedUser);
}
}

if (room.t === 'l') {
Messages.createCommandWithRoomIdAndUser('survey', rid, user);
await Message.saveSystemMessage('command', rid, 'survey', user);
}

await Subscriptions.removeByRoomIdAndUserId(rid, user._id);
Expand Down
13 changes: 5 additions & 8 deletions apps/meteor/app/livechat/server/lib/Helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Match, check } from 'meteor/check';
import { LivechatTransferEventType } from '@rocket.chat/apps-engine/definition/livechat';
import { OmnichannelSourceType, DEFAULT_SLA_CONFIG } from '@rocket.chat/core-typings';
import { LivechatPriorityWeight } from '@rocket.chat/core-typings/src/ILivechatPriority';
import { api } from '@rocket.chat/core-services';
import { api, Message } from '@rocket.chat/core-services';
import {
LivechatDepartmentAgents,
Users as UsersRaw,
Expand All @@ -15,7 +15,7 @@ import {
} from '@rocket.chat/models';

import { hasRoleAsync } from '../../../authorization/server/functions/hasRole';
import { Messages, Rooms, Subscriptions, Users } from '../../../models/server';
import { Rooms, Subscriptions, Users } from '../../../models/server';
import { Livechat } from './Livechat';
import { RoutingManager } from './RoutingManager';
import { callbacks } from '../../../../lib/callbacks';
Expand Down Expand Up @@ -214,7 +214,7 @@ export const removeAgentFromSubscription = async (rid, { _id, username }) => {
const user = Users.findOneById(_id);

await SubscriptionsRaw.removeByRoomIdAndUserId(rid, _id);
Messages.createUserLeaveWithRoomIdAndUser(rid, { _id, username });
await Message.saveSystemMessage('ul', rid, username, { _id: user._id, username: user.username, name: user.name });

Meteor.defer(() => {
Apps.triggerEvent(AppEvents.IPostLivechatAgentUnassigned, { room, user });
Expand Down Expand Up @@ -384,10 +384,7 @@ export const forwardRoomToAgent = async (room, transferData) => {
if (oldServedBy && servedBy._id !== oldServedBy._id) {
await RoutingManager.removeAllRoomSubscriptions(room, servedBy);
}
Messages.createUserJoinWithRoomIdAndUser(rid, {
_id: servedBy._id,
username: servedBy.username,
});
await Message.saveSystemMessage('uj', rid, servedBy.username, servedBy);

Meteor.defer(() => {
Apps.triggerEvent(AppEvents.IPostLivechatRoomTransferred, {
Expand Down Expand Up @@ -499,7 +496,7 @@ export const forwardRoomToDepartment = async (room, guest, transferData) => {
await RoutingManager.removeAllRoomSubscriptions(room, !chatQueued && servedBy);
}
if (!chatQueued && servedBy) {
Messages.createUserJoinWithRoomIdAndUser(rid, servedBy);
await Message.saveSystemMessage('uj', rid, servedBy.username, servedBy);
}

await updateChatDepartment({ rid, newDepartmentId: departmentId, oldDepartmentId });
Expand Down
7 changes: 4 additions & 3 deletions apps/meteor/app/livechat/server/lib/Livechat.js
Original file line number Diff line number Diff line change
Expand Up @@ -978,10 +978,11 @@ export const Livechat = {
const { token } = guest;
check(token, String);

await LivechatRooms.findByVisitorToken(token).forEach((room) => {
const cursor = LivechatRooms.findByVisitorToken(token);
for await (const room of cursor) {
FileUpload.removeFilesByRoomId(room._id);
Messages.removeByRoomId(room._id);
});
await MessagesRaw.removeByRoomId(room._id);
}

Subscriptions.removeByVisitorToken(token);
await LivechatRooms.removeByVisitorToken(token);
Expand Down
4 changes: 2 additions & 2 deletions apps/meteor/app/livechat/server/lib/LivechatTyped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import type {
} from '@rocket.chat/core-typings';
import { isOmnichannelRoom } from '@rocket.chat/core-typings';
import { LivechatDepartment, LivechatInquiry, LivechatRooms, Subscriptions, LivechatVisitors, Messages, Users } from '@rocket.chat/models';
import { Message } from '@rocket.chat/core-services';
import moment from 'moment-timezone';
import { TAPi18n } from 'meteor/rocketchat:tap-i18n';

import { callbacks } from '../../../../lib/callbacks';
import { Logger } from '../../../logger/server';
import { sendMessage } from '../../../lib/server/functions/sendMessage';
import { Apps, AppEvents } from '../../../../ee/server/apps';
import { Messages as LegacyMessage } from '../../../models/server';
import { getTimezone } from '../../../utils/server/lib/getTimezone';
import { settings } from '../../../settings/server';
import * as Mailer from '../../../mailer/server/api';
Expand Down Expand Up @@ -137,7 +137,7 @@ class LivechatClass {
this.logger.debug(`Sending closing message to room ${room._id}`);
await sendMessage(chatCloser, message, newRoom);

LegacyMessage.createCommandWithRoomIdAndUser('promptTranscript', rid, closeData.closedBy);
await Message.saveSystemMessage('command', rid, 'promptTranscript', closeData.closedBy);

this.logger.debug(`Running callbacks for room ${newRoom._id}`);

Expand Down
6 changes: 4 additions & 2 deletions apps/meteor/app/livechat/server/lib/RoutingManager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Meteor } from 'meteor/meteor';
import { Match, check } from 'meteor/check';
import { LivechatInquiry, LivechatRooms, Subscriptions } from '@rocket.chat/models';
import { Message } from '@rocket.chat/core-services';

import {
createLivechatSubscription,
Expand All @@ -14,7 +15,7 @@ import {
} from './Helper';
import { callbacks } from '../../../../lib/callbacks';
import { Logger } from '../../../../server/lib/logger/Logger';
import { Rooms, Messages, Users } from '../../../models/server';
import { Rooms, Users } from '../../../models/server';
import { Apps, AppEvents } from '../../../../ee/server/apps';

const logger = new Logger('RoutingManager');
Expand Down Expand Up @@ -106,7 +107,8 @@ export const RoutingManager = {
const user = Users.findOneById(agent.agentId);
const room = await LivechatRooms.findOneById(rid);

Messages.createCommandWithRoomIdAndUser('connected', rid, user);
await Message.saveSystemMessage('command', rid, 'connected', user);

dispatchAgentDelegated(rid, agent.agentId);
logger.debug(`Agent ${agent.agentId} assigned to inquriy ${inquiry._id}. Instances notified`);

Expand Down
Loading

0 comments on commit a0f4358

Please sign in to comment.