Newer
Older
import { Meteor } from 'meteor/meteor';
import { Messages, Subscriptions } from '../../models';
import logger from './logger';
Marcos Spessatto Defendi
committed
Meteor.methods({
unreadMessages(firstUnreadMessage, room) {
const userId = Meteor.userId();
if (!userId) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
Marcos Spessatto Defendi
committed
const lastMessage = Messages.findVisibleByRoomId(room, { limit: 1, sort: { ts: -1 } }).fetch()[0];
if (lastMessage == null) {
throw new Meteor.Error('error-action-not-allowed', 'Not allowed', {
method: 'unreadMessages',
Marcos Spessatto Defendi
committed
return Subscriptions.setAsUnreadByRoomIdAndUserId(lastMessage.rid, userId, lastMessage.ts);
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.connection.debug('Provided message is already marked as unread');
}
Rodrigo Nascimento
committed
logger.connection.debug(`Updating unread message of ${ originalMessage.ts } as the first unread`);
Marcos Spessatto Defendi
committed
return Subscriptions.setAsUnreadByRoomIdAndUserId(originalMessage.rid, userId, originalMessage.ts);