Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 16 additions & 0 deletions apps/meteor/app/lib/server/functions/loadMessageHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import type { FindOptions } from 'mongodb';
import { settings } from '../../../settings/server/cached';
import { normalizeMessagesForUser } from '../../../utils/server/lib/normalizeMessagesForUser';
import { getHiddenSystemMessages } from '../lib/getHiddenSystemMessages';
import { getMessageReadCount } from '../../../read-counter/server/getMessageReadCount';
import { SystemLogger } from '../../../../server/lib/logger/system';

export async function loadMessageHistory({
userId,
Expand Down Expand Up @@ -52,6 +54,20 @@ export async function loadMessageHistory({
).toArray()
: await Messages.findVisibleByRoomIdNotContainingTypes(rid, hiddenMessageTypes, options, showThreadMessages).toArray();
const messages = await normalizeMessagesForUser(records, userId);

if (messages[0]?._id && userId) {
const readCountResult = await getMessageReadCount(messages[0]._id, userId);

if (readCountResult) {
SystemLogger.info({
msg: '[read-count demo] newest message readCount',
rid,
mid: messages[0]._id,
readCount: readCountResult.readCount,
uid: userId,
});
}
}
let unreadNotLoaded = 0;
let firstUnread;

Expand Down
45 changes: 45 additions & 0 deletions apps/meteor/app/read-counter/server/getMessageReadCount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { IMessage, IRoom, IUser } from '@rocket.chat/core-typings';
import { Authorization } from '@rocket.chat/core-services';
import { Messages, Rooms, Subscriptions } from '@rocket.chat/models';

export const getMessageReadCount = async (
messageId: IMessage['_id'],
userId: IUser['_id'],
): Promise<{ readCount: number } | null> => {
const message = await Messages.findOneById(messageId, {
projection: { _id: 1, rid: 1, ts: 1, 'u._id': 1 },
});

if (!message) {
return null;
}

const room = await Rooms.findOneById(message.rid as IRoom['_id'], {
projection: { _id: 1, t: 1 },
});

if (!room) {
return null;
}

// Exclude DMs from read counter
if (room.t === 'd') {
return null;
}

// Ensure the requesting user can access the room
if (!(await Authorization.canReadRoom(room, { _id: userId }))) {
return null;
}

if (!message.ts) {
return { readCount: 0 };
}

const excludeUserId = message.u?._id;

const readCount = await Subscriptions.countReadersByRoomIdAndMessageTs(message.rid, message.ts, excludeUserId);

return { readCount };
};

2 changes: 2 additions & 0 deletions packages/model-typings/src/models/ISubscriptionsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import type { DocumentWithProjection } from '../types/DocumentWithProjection';
export interface ISubscriptionsModel extends IBaseModel<ISubscription> {
getBadgeCount(uid: string): Promise<number>;

countReadersByRoomIdAndMessageTs(rid: string, messageTs: Date, excludeUserId?: string): Promise<number>;

findOneByRoomIdAndUserId(rid: string, uid: string, options?: FindOptions<ISubscription>): Promise<ISubscription | null>;

findByUserIdAndRoomIds(userId: string, roomIds: Array<string>, options?: FindOptions<ISubscription>): FindCursor<ISubscription>;
Expand Down
14 changes: 14 additions & 0 deletions packages/models/src/models/Subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@
return result?.total || 0;
}

countReadersByRoomIdAndMessageTs(rid: string, messageTs: Date, excludeUserId?: string): Promise<number> {
const query: Filter<ISubscription> = {
rid,
archived: { $ne: true },

Check failure on line 92 in packages/models/src/models/Subscriptions.ts

View workflow job for this annotation

GitHub Actions / 🔎 Code Check / Code Lint

Replace `archived` with `'archived'`
ls: { $gte: messageTs },

Check failure on line 93 in packages/models/src/models/Subscriptions.ts

View workflow job for this annotation

GitHub Actions / 🔎 Code Check / Code Lint

Replace `ls` with `'ls'`
'u._id': {
...(excludeUserId ? { $ne: excludeUserId } : {}),
$exists: true,
},
};

return this.countDocuments(query);
}

findOneByRoomIdAndUserId(rid: string, uid: string, options: FindOptions<ISubscription> = {}): Promise<ISubscription | null> {
const query = {
rid,
Expand Down
Loading