parent
85940503a2
commit
1bda97d87b
@ -0,0 +1,5 @@ |
|||||||
|
RocketChat.models.Messages.tryEnsureIndex({ |
||||||
|
unread: 1 |
||||||
|
}, { |
||||||
|
sparse: true |
||||||
|
}); |
||||||
@ -0,0 +1,2 @@ |
|||||||
|
import './dbIndexes'; |
||||||
|
import './settings'; |
||||||
@ -0,0 +1,55 @@ |
|||||||
|
import { Random } from 'meteor/random'; |
||||||
|
import ModelReadReceipts from '../models/ReadReceipts'; |
||||||
|
|
||||||
|
const rawReadReceipts = ModelReadReceipts.model.rawCollection(); |
||||||
|
|
||||||
|
// @TODO create a debounced function by roomId, so multiple calls to same roomId runs only once
|
||||||
|
|
||||||
|
export const ReadReceipt = { |
||||||
|
markMessagesAsRead(roomId, userId, userLastSeen) { |
||||||
|
if (!RocketChat.settings.get('Message_Read_Receipt_Enabled')) { |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
const room = RocketChat.models.Rooms.findOneById(roomId, { fields: { lm: 1 } }); |
||||||
|
|
||||||
|
// 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; |
||||||
|
} |
||||||
|
|
||||||
|
const firstSubscription = RocketChat.models.Subscriptions.getMinimumLastSeenByRoomId(roomId); |
||||||
|
// console.log('userLastSeen ->', userLastSeen);
|
||||||
|
// console.log('firstSubscription ->', firstSubscription);
|
||||||
|
// console.log('room ->', room);
|
||||||
|
|
||||||
|
// last time room was read is already past room's last message, so does nothing everybody have this room already
|
||||||
|
// if (firstSubscription.ls > room.lm) {
|
||||||
|
// console.log('already read by everyone');
|
||||||
|
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// @TODO maybe store firstSubscription in room object so we don't need to call the above update method
|
||||||
|
// if firstSubscription on room didn't change
|
||||||
|
|
||||||
|
if (RocketChat.settings.get('Message_Read_Receipt_Store_Users')) { |
||||||
|
const receipts = RocketChat.models.Messages.findUnreadMessagesByRoomAndDate(roomId, userLastSeen).map(message => { |
||||||
|
return { |
||||||
|
_id: Random.id(), |
||||||
|
roomId, |
||||||
|
userId, |
||||||
|
messageId: message._id |
||||||
|
}; |
||||||
|
}); |
||||||
|
|
||||||
|
try { |
||||||
|
rawReadReceipts.insertMany(receipts); |
||||||
|
} catch (e) { |
||||||
|
console.error('Error inserting read receipts per user'); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
RocketChat.models.Messages.setAsRead(roomId, firstSubscription.ls); |
||||||
|
} |
||||||
|
}; |
||||||
@ -0,0 +1,15 @@ |
|||||||
|
class ModelReadReceipts extends RocketChat.models._Base { |
||||||
|
constructor() { |
||||||
|
super(...arguments); |
||||||
|
|
||||||
|
this.tryEnsureIndex({ |
||||||
|
roomId: 1, |
||||||
|
userId: 1, |
||||||
|
messageId: 1 |
||||||
|
}, { |
||||||
|
unique: 1 |
||||||
|
}); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export default new ModelReadReceipts('message_read_receipt', true); |
||||||
@ -0,0 +1,12 @@ |
|||||||
|
RocketChat.settings.add('Message_Read_Receipt_Enabled', false, { |
||||||
|
group: 'Message', |
||||||
|
type: 'boolean', |
||||||
|
public: true |
||||||
|
}); |
||||||
|
|
||||||
|
RocketChat.settings.add('Message_Read_Receipt_Store_Users', false, { |
||||||
|
group: 'Message', |
||||||
|
type: 'boolean', |
||||||
|
public: false, |
||||||
|
enableQuery: { _id: 'Message_Read_Receipt_Enabled', value: true } |
||||||
|
}); |
||||||
@ -0,0 +1 @@ |
|||||||
|
import '../../message-read-receipt/server'; |
||||||
|
Before Width: | Height: | Size: 48 KiB After Width: | Height: | Size: 48 KiB |
@ -0,0 +1 @@ |
|||||||
|
import '/imports/startup/server'; |
||||||
@ -1,13 +1,26 @@ |
|||||||
|
import { ReadReceipt } from '../../imports/message-read-receipt/server/lib/ReadReceipt'; |
||||||
|
|
||||||
Meteor.methods({ |
Meteor.methods({ |
||||||
readMessages(rid) { |
readMessages(rid) { |
||||||
check(rid, String); |
check(rid, String); |
||||||
|
|
||||||
if (!Meteor.userId()) { |
const userId = Meteor.userId(); |
||||||
|
|
||||||
|
if (!userId) { |
||||||
throw new Meteor.Error('error-invalid-user', 'Invalid user', { |
throw new Meteor.Error('error-invalid-user', 'Invalid user', { |
||||||
method: 'readMessages' |
method: 'readMessages' |
||||||
}); |
}); |
||||||
} |
} |
||||||
|
|
||||||
return RocketChat.models.Subscriptions.setAsReadByRoomIdAndUserId(rid, Meteor.userId()); |
const userSubscription = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(rid, userId); |
||||||
|
|
||||||
|
// this prevents cache from updating object reference/pointer
|
||||||
|
const { ls: lastSeen } = userSubscription; |
||||||
|
|
||||||
|
RocketChat.models.Subscriptions.setAsReadByRoomIdAndUserId(rid, userId); |
||||||
|
|
||||||
|
Meteor.defer(() => { |
||||||
|
ReadReceipt.markMessagesAsRead(rid, userId, lastSeen); |
||||||
|
}); |
||||||
} |
} |
||||||
}); |
}); |
||||||
|
|||||||
Loading…
Reference in new issue