Newer
Older
import { Meteor } from 'meteor/meteor';
import logger from './logger';
import { Messages, Subscriptions } from '../../models';
Marcos Spessatto Defendi
committed
Meteor.methods({
unreadMessages(firstUnreadMessage, room) {
const userId = Meteor.userId();
if (!userId) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
if (room && typeof room === 'string') {
const lastMessage = Messages.findVisibleByRoomId(room, { limit: 1, sort: { ts: -1 } }).fetch()[0];
if (lastMessage == null) {
Lucas Sartor Chauvin
committed
throw new Meteor.Error('error-no-message-for-unread', 'There are no messages to mark unread', {
method: 'unreadMessages',
Marcos Spessatto Defendi
committed
return Subscriptions.setAsUnreadByRoomIdAndUserId(lastMessage.rid, userId, lastMessage.ts);
if (typeof firstUnreadMessage?._id !== 'string') {
throw new Meteor.Error('error-action-not-allowed', 'Not allowed', {
method: 'unreadMessages',
action: 'Unread_messages',
});
}
Marcos Spessatto Defendi
committed
const originalMessage = Messages.findOneById(firstUnreadMessage._id, {
fields: {
u: 1,
rid: 1,
file: 1,
if (originalMessage == null || userId === originalMessage.u._id) {
throw new Meteor.Error('error-action-not-allowed', 'Not allowed', {
method: 'unreadMessages',
Marcos Spessatto Defendi
committed
const lastSeen = Subscriptions.findOneByRoomIdAndUserId(originalMessage.rid, userId).ls;
if (firstUnreadMessage.ts >= lastSeen) {
return logger.debug('Provided message is already marked as unread');
logger.debug(`Updating unread message of ${ originalMessage.ts } as the first unread`);
Marcos Spessatto Defendi
committed
return Subscriptions.setAsUnreadByRoomIdAndUserId(originalMessage.rid, userId, originalMessage.ts);