Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(models): Use Messages Raw model (4/N) #28558

Merged
merged 26 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
dafe9de
updateThreadLastMessageAndCountByThreadId
sampaiodiego Mar 22, 2023
e29c6a7
addThreadFollowerByThreadId
sampaiodiego Mar 22, 2023
b80f9c9
addThreadFollowerByThreadId
sampaiodiego Mar 22, 2023
a1a4ca2
addThreadFollowerByThreadId
sampaiodiego Mar 22, 2023
e809df4
addThreadFollowerByThreadId
sampaiodiego Mar 22, 2023
e20910a
removeThreadFollowerByThreadId
sampaiodiego Mar 22, 2023
e0d87a2
getFirstReplyTsByThreadId
sampaiodiego Mar 22, 2023
ff9243d
getThreadFollowsByThreadId
sampaiodiego Mar 22, 2023
102d9f1
findThreadsByRoomId
sampaiodiego Mar 22, 2023
2f67874
findAgentLastMessageByVisitorLastMessageTs
sampaiodiego Mar 22, 2023
86051b9
decreaseReplyCountById
sampaiodiego Mar 22, 2023
9fab9e1
updateRepliesByThreadId
sampaiodiego Mar 22, 2023
12934b8
removeThreadRefByThreadId
sampaiodiego Mar 22, 2023
a3d1cd9
countThreads
sampaiodiego Mar 22, 2023
0bf3072
findUnreadThreadMessagesByDate
sampaiodiego Mar 22, 2023
1c56f91
findVisibleUnreadMessagesByRoomAndDate
sampaiodiego Mar 22, 2023
28fe6e0
setAsReadById
sampaiodiego Mar 22, 2023
2b8d4ee
getMessageByFileId
sampaiodiego Mar 22, 2023
8579362
removeByUserId
sampaiodiego Mar 22, 2023
b1977a2
removeByRoomIds
sampaiodiego Mar 22, 2023
67c2cc3
findForUpdates
sampaiodiego Mar 22, 2023
add0d8f
it's all raw
sampaiodiego Mar 22, 2023
18dc7f0
it is all raw
sampaiodiego Mar 22, 2023
68b6dcf
Merge branch 'develop' into use-messages-raw-part4
sampaiodiego Mar 22, 2023
3f002c6
Merge remote-tracking branch 'origin/develop' into use-messages-raw-p…
sampaiodiego Mar 23, 2023
ac0c414
use async in callback
sampaiodiego Mar 23, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/meteor/app/federation/server/endpoints/dispatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ const eventHandlers = {
try {
Messages.insert(denormalizedMessage);

processThreads(denormalizedMessage, room);
await processThreads(denormalizedMessage, room);

// Notify users
await notifyUsersOnMessage(denormalizedMessage, room);
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 @@ -24,7 +24,7 @@ export async function deleteMessage(message: IMessage, user: IUser): Promise<voi
}

if (deletedMsg.tmid) {
MessagesSync.decreaseReplyCountById(deletedMsg.tmid, -1);
await Messages.decreaseReplyCountById(deletedMsg.tmid, -1);
}

const files = (message.files || [message.file]).filter(Boolean); // Keep compatibility with old messages
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/lib/server/functions/deleteUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export async function deleteUser(userId: string, confirmRelinquish = false): Pro
Messages.findFilesByUserId(userId).forEach(function ({ file }: { file: FileProp }) {
store.deleteById(file._id);
});
Messages.removeByUserId(userId);
await MessagesRaw.removeByUserId(userId);
break;
case 'Unlink':
const rocketCat = Users.findOneById('rocket.cat');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Roles } from '@rocket.chat/models';
import { Messages, Roles } from '@rocket.chat/models';

import { FileUpload } from '../../../file-upload/server';
import { Subscriptions, Messages, Rooms } from '../../../models/server';
import { Subscriptions, Rooms } from '../../../models/server';
import type { SubscribedRoomsForUserWithDetails } from './getRoomsWithSingleOwner';

const bulkRoomCleanUp = async (rids: string[]): Promise<unknown> => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import moment from 'moment';
import { LivechatBusinessHours, LivechatDepartment } from '@rocket.chat/models';
import { LivechatBusinessHours, LivechatDepartment, Messages } from '@rocket.chat/models';

import { settings } from '../../../settings/server';
import { callbacks } from '../../../../lib/callbacks';
import { LivechatRooms, Messages } from '../../../models/server';
import { LivechatRooms } from '../../../models/server';
import { businessHourManager } from '../business-hour';

const getSecondsWhenOfficeHoursIsDisabled = (room, agentLastMessage) =>
Expand Down Expand Up @@ -58,15 +58,15 @@ const getSecondsSinceLastAgentResponse = async (room, agentLastMessage) => {

callbacks.add(
'livechat.closeRoom',
async (params) => {
async function (params) {
const { room } = params;

const closedByAgent = room.closer !== 'visitor';
const wasTheLastMessageSentByAgent = room.lastMessage && !room.lastMessage.token;
if (!closedByAgent || !wasTheLastMessageSentByAgent) {
return params;
}
const agentLastMessage = Messages.findAgentLastMessageByVisitorLastMessageTs(room._id, room.v.lastMessageTs);
const agentLastMessage = await Messages.findAgentLastMessageByVisitorLastMessageTs(room._id, room.v.lastMessageTs);
if (!agentLastMessage) {
return params;
}
Expand Down
217 changes: 0 additions & 217 deletions apps/meteor/app/models/server/models/Messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,19 +212,6 @@ export class Messages extends Base {
return this.find(query, options);
}

findForUpdates(roomId, timestamp, options) {
const query = {
_hidden: {
$ne: true,
},
rid: roomId,
_updatedAt: {
$gt: timestamp,
},
};
return this.find(query, options);
}

findVisibleByRoomIdBeforeTimestampNotContainingTypes(roomId, timestamp, types, options, showThreadMessages = true, inclusive = false) {
const query = {
_hidden: {
Expand Down Expand Up @@ -565,10 +552,6 @@ export class Messages extends Base {
return this.remove(query);
}

removeByRoomIds(rids) {
return this.remove({ rid: { $in: rids } });
}

findThreadsByRoomIdPinnedTimestampAndUsers({ rid, pinned, ignoreDiscussion = true, ts, users = [] }, options) {
const query = {
rid,
Expand All @@ -592,16 +575,6 @@ export class Messages extends Base {
return this.find(query, options);
}

removeByUserId(userId) {
const query = { 'u._id': userId };

return this.remove(query);
}

getMessageByFileId(fileID) {
return this.findOne({ 'file._id': fileID });
}

setVisibleMessagesAsRead(rid, until) {
return this.update(
{
Expand All @@ -628,186 +601,6 @@ export class Messages extends Base {
);
}

setAsReadById(_id) {
return this.update(
{
_id,
},
{
$unset: {
unread: 1,
},
},
);
}

findVisibleUnreadMessagesByRoomAndDate(rid, after) {
const query = {
unread: true,
rid,
$or: [
{
tmid: { $exists: false },
},
{
tshow: true,
},
],
};

if (after) {
query.ts = { $gt: after };
}

return this.find(query, {
fields: {
_id: 1,
},
});
}

findUnreadThreadMessagesByDate(tmid, userId, after) {
const query = {
'u._id': { $ne: userId },
'unread': true,
tmid,
'tshow': { $exists: false },
};

if (after) {
query.ts = { $gt: after };
}

return this.find(query, {
fields: {
_id: 1,
},
});
}

// //////////////////////////////////////////////////////////////////
// threads

countThreads() {
return this.find({ tcount: { $exists: true } }).count();
}

removeThreadRefByThreadId(tmid) {
const query = { tmid };
const update = {
$unset: {
tmid: 1,
},
};
return this.update(query, update, { multi: true });
}

updateRepliesByThreadId(tmid, replies, ts) {
const query = {
_id: tmid,
};

const update = {
$addToSet: {
replies: {
$each: replies,
},
},
$set: {
tlm: ts,
},
$inc: {
tcount: 1,
},
};

return this.update(query, update);
}

getThreadFollowsByThreadId(tmid) {
const msg = this.findOneById(tmid, { fields: { replies: 1 } });
return msg && msg.replies;
}

getFirstReplyTsByThreadId(tmid) {
return this.findOne({ tmid }, { fields: { ts: 1 }, sort: { ts: 1 } });
}

unsetThreadByThreadId(tmid) {
const query = {
_id: tmid,
};

const update = {
$unset: {
tcount: 1,
tlm: 1,
replies: 1,
},
};

return this.update(query, update);
}

updateThreadLastMessageAndCountByThreadId(tmid, tlm, tcount) {
const query = {
_id: tmid,
};

const update = {
$set: {
tlm,
},
$inc: {
tcount,
},
};

return this.update(query, update);
}

addThreadFollowerByThreadId(tmid, userId) {
const query = {
_id: tmid,
};

const update = {
$addToSet: {
replies: userId,
},
};

return this.update(query, update);
}

removeThreadFollowerByThreadId(tmid, userId) {
const query = {
_id: tmid,
};

const update = {
$pull: {
replies: userId,
},
};

return this.update(query, update);
}

findThreadsByRoomId(rid, skip, limit) {
return this.find({ rid, tcount: { $exists: true } }, { sort: { tlm: -1 }, skip, limit });
}

findAgentLastMessageByVisitorLastMessageTs(roomId, visitorLastMessageTs) {
const query = {
rid: roomId,
ts: { $gt: visitorLastMessageTs },
token: { $exists: false },
};

return this.findOne(query, { sort: { ts: 1 } });
}

findAllImportedMessagesWithFilesToDownload() {
const query = {
'_importFile.downloadUrl': {
Expand All @@ -826,16 +619,6 @@ export class Messages extends Base {

return this.find(query);
}

decreaseReplyCountById(_id, inc = -1) {
const query = { _id };
const update = {
$inc: {
tcount: inc,
},
};
return this.update(query, update);
}
}

export default new Messages();
Loading