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

[FIX] Read receipts showing first messages of the room as read even if not read by everyone #24508

Merged
merged 6 commits into from
Feb 17, 2022
Merged
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
3 changes: 0 additions & 3 deletions app/models/server/models/Subscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,9 +556,6 @@ export class Subscriptions extends Base {
return this.db.findOne(
{
rid,
ls: {
$exists: true,
},
},
{
sort: {
Expand Down
24 changes: 0 additions & 24 deletions client/hooks/useFormatDateAndTime.js

This file was deleted.

29 changes: 29 additions & 0 deletions client/hooks/useFormatDateAndTime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import moment, { MomentInput } from 'moment';
import { useCallback } from 'react';

import { useSetting } from '../contexts/SettingsContext';
import { useUserPreference } from '../contexts/UserContext';

type UseFormatDateAndTimeParams = {
withSeconds?: boolean;
};

export const useFormatDateAndTime = ({ withSeconds }: UseFormatDateAndTimeParams = {}): ((input: MomentInput) => string) => {
const clockMode = useUserPreference('clockMode');
const format = useSetting('Message_TimeAndDateFormat') as string;

return useCallback(
(time) => {
switch (clockMode) {
case 1:
return moment(time).format(withSeconds ? 'MMMM D, Y h:mm:ss A' : 'MMMM D, Y h:mm A');
case 2:
return moment(time).format(withSeconds ? 'MMMM D, Y H:mm:ss' : 'MMMM D, Y H:mm');

default:
return moment(time).format(withSeconds ? 'L LTS' : format);
}
},
[clockMode, format, withSeconds],
);
};
3 changes: 2 additions & 1 deletion client/views/account/tokens/AccountTokensRow.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Button, ButtonGroup, Icon, Table } from '@rocket.chat/fuselage';
import type { MomentInput } from 'moment';
import React, { useCallback, FC } from 'react';

import { useTranslation } from '../../../contexts/TranslationContext';
import { useFormatDateAndTime } from '../../../hooks/useFormatDateAndTime';

type AccountTokensRowProps = {
bypassTwoFactor: unknown;
createdAt: unknown;
createdAt: MomentInput;
isMedium: boolean;
lastTokenPart: string;
name: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const hoverStyle = css`

const ReadReceiptRow = ({ user, ts }: ReadReceipt): ReactElement => {
const displayName = useUserDisplayName(user);
const formatDateAndTime = useFormatDateAndTime();
const formatDateAndTime = useFormatDateAndTime({ withSeconds: true });

return (
<Box
Expand Down
2 changes: 1 addition & 1 deletion imports/message-read-receipt/server/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ callbacks.add(
}

// mark message as read as well
ReadReceipt.markMessageAsReadBySender(message, room._id, message.u._id);
ReadReceipt.markMessageAsReadBySender(message, room, message.u._id);

return message;
},
Expand Down
25 changes: 13 additions & 12 deletions imports/message-read-receipt/server/lib/ReadReceipt.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const updateMessages = debounceByRoomId(
Meteor.bindEnvironment(({ _id, lm }) => {
// @TODO maybe store firstSubscription in room object so we don't need to call the above update method
const firstSubscription = Subscriptions.getMinimumLastSeenByRoomId(_id);
if (!firstSubscription) {
if (!firstSubscription || !firstSubscription.ls) {
return;
}

Expand All @@ -42,31 +42,32 @@ export const ReadReceipt = {

const room = Rooms.findOneById(roomId, { fields: { lm: 1 } });

// if users last seen is greadebounceByRoomIdter than room's last message, it means the user already have this room marked as read
// if users last seen is greater than room's last message, it means the user already have this room marked as read
if (userLastSeen > room.lm) {
return;
}

if (userLastSeen) {
this.storeReadReceipts(Messages.findUnreadMessagesByRoomAndDate(roomId, userLastSeen), roomId, userId);
}
this.storeReadReceipts(Messages.findUnreadMessagesByRoomAndDate(roomId, userLastSeen), roomId, userId);

updateMessages(room);
},

markMessageAsReadBySender(message, roomId, userId) {
markMessageAsReadBySender(message, { _id: roomId, t }, userId) {
if (!settings.get('Message_Read_Receipt_Enabled')) {
return;
}

// this will usually happens if the message sender is the only one on the room
const firstSubscription = Subscriptions.getMinimumLastSeenByRoomId(roomId);
if (firstSubscription && message.unread && message.ts < firstSubscription.ls) {
Messages.setAsReadById(message._id, firstSubscription.ls);
if (!message.unread) {
return;
}

// mark message as read if the sender is the only one in the room
const isUserAlone = Subscriptions.findByRoomIdAndNotUserId(roomId, userId, { fields: { _id: 1 } }).count() === 0;
if (isUserAlone) {
Messages.setAsReadById(message._id);
}

const room = Rooms.findOneById(roomId, { fields: { t: 1 } });
const extraData = roomTypes.getConfig(room.t).getReadReceiptsExtraData(message);
const extraData = roomTypes.getConfig(t).getReadReceiptsExtraData(message);

this.storeReadReceipts([{ _id: message._id }], roomId, userId, extraData);
},
Expand Down