The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Rocket.Chat/server/methods/readMessages.js

40 lines
1.0 KiB

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';
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',
});
}
Subscriptions.setAsReadByRoomIdAndUserId(rid, userId);
NotificationQueue.clearQueueByUserId(userId);
Meteor.defer(() => {
callbacks.run('afterReadMessages', rid, { userId, lastSeen: userSubscription.ls });
});
},
});