Skip to content
Snippets Groups Projects
Unverified Commit ae135d6a authored by Marcos Spessatto Defendi's avatar Marcos Spessatto Defendi Committed by GitHub
Browse files

refactor: remove subscriptions method calls (server) (#35016)

parent 94315564
No related branches found
No related tags found
No related merge requests found
......@@ -8,6 +8,8 @@ import {
import { Meteor } from 'meteor/meteor';
import { readMessages } from '../../../../server/lib/readMessages';
import { getSubscriptions } from '../../../../server/publications/subscription';
import { unreadMessages } from '../../../message-mark-as-unread/server/unreadMessages';
import { API } from '../api';
API.v1.addRoute(
......@@ -28,7 +30,7 @@ API.v1.addRoute(
updatedSinceDate = new Date(updatedSince as string);
}
const result = await Meteor.callAsync('subscriptions/get', updatedSinceDate);
const result = await getSubscriptions(this.userId, updatedSinceDate);
return API.v1.success(
Array.isArray(result)
......@@ -98,7 +100,7 @@ API.v1.addRoute(
},
{
async post() {
await Meteor.callAsync('unreadMessages', (this.bodyParams as any).firstUnreadMessage, (this.bodyParams as any).roomId);
await unreadMessages(this.userId, (this.bodyParams as any).firstUnreadMessage, (this.bodyParams as any).roomId);
return API.v1.success();
},
......
......@@ -13,75 +13,78 @@ declare module '@rocket.chat/ddp-client' {
}
}
Meteor.methods<ServerMethods>({
async unreadMessages(firstUnreadMessage, room) {
const userId = Meteor.userId();
if (!userId) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
export const unreadMessages = async (userId: string, firstUnreadMessage?: IMessage, room?: IRoom['_id']): Promise<void> => {
if (room && typeof room === 'string') {
const lastMessage = (
await Messages.findVisibleByRoomId(room, {
limit: 1,
sort: { ts: -1 },
}).toArray()
)[0];
if (!lastMessage) {
throw new Meteor.Error('error-no-message-for-unread', 'There are no messages to mark unread', {
method: 'unreadMessages',
action: 'Unread_messages',
});
}
if (room && typeof room === 'string') {
const lastMessage = (
await Messages.findVisibleByRoomId(room, {
limit: 1,
sort: { ts: -1 },
}).toArray()
)[0];
const setAsUnreadResponse = await Subscriptions.setAsUnreadByRoomIdAndUserId(lastMessage.rid, userId, lastMessage.ts);
if (setAsUnreadResponse.modifiedCount) {
void notifyOnSubscriptionChangedByRoomIdAndUserId(lastMessage.rid, userId);
}
if (!lastMessage) {
throw new Meteor.Error('error-no-message-for-unread', 'There are no messages to mark unread', {
method: 'unreadMessages',
action: 'Unread_messages',
});
}
return;
}
const setAsUnreadResponse = await Subscriptions.setAsUnreadByRoomIdAndUserId(lastMessage.rid, userId, lastMessage.ts);
if (setAsUnreadResponse.modifiedCount) {
void notifyOnSubscriptionChangedByRoomIdAndUserId(lastMessage.rid, userId);
}
if (typeof firstUnreadMessage?._id !== 'string') {
throw new Meteor.Error('error-action-not-allowed', 'Not allowed', {
method: 'unreadMessages',
action: 'Unread_messages',
});
}
return;
}
const originalMessage = await Messages.findOneById(firstUnreadMessage._id, {
projection: {
u: 1,
rid: 1,
ts: 1,
},
});
if (!originalMessage || userId === originalMessage.u._id) {
throw new Meteor.Error('error-action-not-allowed', 'Not allowed', {
method: 'unreadMessages',
action: 'Unread_messages',
});
}
const lastSeen = (await Subscriptions.findOneByRoomIdAndUserId(originalMessage.rid, userId))?.ls;
if (!lastSeen) {
throw new Meteor.Error('error-subscription-not-found', 'Subscription not found', {
method: 'unreadMessages',
action: 'Unread_messages',
});
}
if (typeof firstUnreadMessage?._id !== 'string') {
throw new Meteor.Error('error-action-not-allowed', 'Not allowed', {
method: 'unreadMessages',
action: 'Unread_messages',
});
}
if (firstUnreadMessage.ts >= lastSeen) {
return logger.debug('Provided message is already marked as unread');
}
const originalMessage = await Messages.findOneById(firstUnreadMessage._id, {
projection: {
u: 1,
rid: 1,
file: 1,
ts: 1,
},
});
if (!originalMessage || userId === originalMessage.u._id) {
throw new Meteor.Error('error-action-not-allowed', 'Not allowed', {
method: 'unreadMessages',
action: 'Unread_messages',
});
}
const lastSeen = (await Subscriptions.findOneByRoomIdAndUserId(originalMessage.rid, userId))?.ls;
if (!lastSeen) {
throw new Meteor.Error('error-subscription-not-found', 'Subscription not found', {
logger.debug(`Updating unread message of ${originalMessage.ts} as the first unread`);
const setAsUnreadResponse = await Subscriptions.setAsUnreadByRoomIdAndUserId(originalMessage.rid, userId, originalMessage.ts);
if (setAsUnreadResponse.modifiedCount) {
void notifyOnSubscriptionChangedByRoomIdAndUserId(originalMessage.rid, userId);
}
};
Meteor.methods<ServerMethods>({
async unreadMessages(firstUnreadMessage, room) {
const userId = Meteor.userId();
if (!userId) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'unreadMessages',
action: 'Unread_messages',
});
}
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`);
const setAsUnreadResponse = await Subscriptions.setAsUnreadByRoomIdAndUserId(originalMessage.rid, userId, originalMessage.ts);
if (setAsUnreadResponse.modifiedCount) {
void notifyOnSubscriptionChangedByRoomIdAndUserId(originalMessage.rid, userId);
}
return unreadMessages(userId, firstUnreadMessage, room);
},
});
......@@ -12,6 +12,37 @@ declare module '@rocket.chat/ddp-client' {
}
}
export const getSubscriptions = async (
uid: string,
updatedAt?: Date,
): Promise<ISubscription[] | { update: ISubscription[]; remove: { _id: string; _deletedAt: Date }[] }> => {
const options = { projection: subscriptionFields };
const records: ISubscription[] = await Subscriptions.findByUserId(uid, options).toArray();
if (updatedAt instanceof Date) {
return {
update: records.filter((record) => {
return record._updatedAt > updatedAt;
}),
remove: await Subscriptions.trashFindDeletedAfter(
updatedAt,
{
'u._id': uid,
},
{
projection: {
_id: 1,
_deletedAt: 1,
},
},
).toArray(),
};
}
return records;
};
Meteor.methods<ServerMethods>({
async 'subscriptions/get'(updatedAt) {
const uid = Meteor.userId();
......@@ -19,30 +50,6 @@ Meteor.methods<ServerMethods>({
return [];
}
const options = { projection: subscriptionFields };
const records: ISubscription[] = await Subscriptions.findByUserId(uid, options).toArray();
if (updatedAt instanceof Date) {
return {
update: records.filter((record) => {
return record._updatedAt > updatedAt;
}),
remove: await Subscriptions.trashFindDeletedAfter(
updatedAt,
{
'u._id': uid,
},
{
projection: {
_id: 1,
_deletedAt: 1,
},
},
).toArray(),
};
}
return records;
return getSubscriptions(uid, updatedAt);
},
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment