[FIX] Marking room as read with unread threads still (#18410)
parent
3ce8e656a8
commit
ef251eeb07
@ -0,0 +1,21 @@ |
||||
import { callbacks } from '../../app/callbacks/server'; |
||||
import { NotificationQueue, Subscriptions } from '../../app/models/server/raw'; |
||||
|
||||
export async function markRoomAsRead(rid: string, uid: string): Promise<void> { |
||||
callbacks.run('beforeReadMessages', rid, uid); |
||||
|
||||
const projection = { ls: 1, tunread: 1, alert: 1 }; |
||||
const sub = await Subscriptions.findOneByRoomIdAndUserId(rid, uid, { projection }); |
||||
if (!sub) { |
||||
throw new Error('error-invalid-subscription'); |
||||
} |
||||
|
||||
// do not mark room as read if there are still unread threads
|
||||
const alert = sub.alert && sub.tunread?.length > 0; |
||||
|
||||
await Subscriptions.setAsReadByRoomIdAndUserId(rid, uid, alert); |
||||
|
||||
await NotificationQueue.clearQueueByUserId(uid); |
||||
|
||||
callbacks.runAsync('afterReadMessages', rid, { uid, lastSeen: sub.ls }); |
||||
} |
@ -1,39 +1,27 @@ |
||||
import { Meteor } from 'meteor/meteor'; |
||||
import { check } from 'meteor/check'; |
||||
|
||||
import { callbacks } from '../../app/callbacks/server'; |
||||
import { Subscriptions } from '../../app/models/server'; |
||||
import { NotificationQueue } from '../../app/models/server/raw'; |
||||
import { markRoomAsRead } from '../lib/markRoomAsRead'; |
||||
import { canAccessRoom } from '../../app/authorization/server'; |
||||
import { Rooms } from '../../app/models/server'; |
||||
|
||||
Meteor.methods({ |
||||
readMessages(rid) { |
||||
check(rid, String); |
||||
|
||||
const userId = Meteor.userId(); |
||||
|
||||
if (!userId) { |
||||
throw new Meteor.Error('error-invalid-user', 'Invalid user', { |
||||
method: 'readMessages', |
||||
}); |
||||
} |
||||
|
||||
callbacks.run('beforeReadMessages', rid, userId); |
||||
|
||||
// TODO: move this calls to an exported function
|
||||
const userSubscription = Subscriptions.findOneByRoomIdAndUserId(rid, userId, { fields: { ls: 1 } }); |
||||
|
||||
if (!userSubscription) { |
||||
throw new Meteor.Error('error-invalid-subscription', 'Invalid subscription', { |
||||
method: 'readMessages', |
||||
}); |
||||
const user = Meteor.user(); |
||||
const room = Rooms.findOneById(rid); |
||||
if (!canAccessRoom(room, user)) { |
||||
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'readMessages' }); |
||||
} |
||||
|
||||
Subscriptions.setAsReadByRoomIdAndUserId(rid, userId); |
||||
|
||||
NotificationQueue.clearQueueByUserId(userId); |
||||
|
||||
Meteor.defer(() => { |
||||
callbacks.run('afterReadMessages', rid, { userId, lastSeen: userSubscription.ls }); |
||||
}); |
||||
markRoomAsRead(rid, userId); |
||||
}, |
||||
}); |
||||
|
Loading…
Reference in new issue