Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Remove starMessage client meteor method #33817

Merged
merged 6 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { MessageAction } from '../../../../app/ui-utils/client/lib/MessageAction
import { useEmojiPickerData } from '../../../contexts/EmojiPickerContext';
import { useMessageActionAppsActionButtons } from '../../../hooks/useAppActionButtons';
import { useEmbeddedLayout } from '../../../hooks/useEmbeddedLayout';
import { roomsQueryKeys } from '../../../lib/queryKeys';
import EmojiElement from '../../../views/composer/EmojiPicker/EmojiElement';
import { useIsSelecting } from '../../../views/room/MessageList/contexts/SelectedMessagesContext';
import { useAutoTranslate } from '../../../views/room/MessageList/hooks/useAutoTranslate';
Expand Down Expand Up @@ -88,16 +89,20 @@ const MessageToolbar = ({
useWebDAVMessageAction();
useNewDiscussionMessageAction();

const actionsQueryResult = useQuery(['rooms', room._id, 'messages', message._id, 'actions'] as const, async () => {
const props = { message, room, user, subscription, settings: mapSettings, chat };
const actionsQueryResult = useQuery({
queryKey: roomsQueryKeys.messageActionsWithParameters(room._id, message),
queryFn: async () => {
const props = { message, room, user, subscription, settings: mapSettings, chat };

const toolboxItems = await MessageAction.getAll(props, context, 'message');
const menuItems = await MessageAction.getAll(props, context, 'menu');
const toolboxItems = await MessageAction.getAll(props, context, 'message');
const menuItems = await MessageAction.getAll(props, context, 'menu');

return {
message: toolboxItems.filter((action) => !hiddenActions.includes(action.id)),
menu: menuItems.filter((action) => !(isLayoutEmbedded && action.id === 'reply-directly') && !hiddenActions.includes(action.id)),
};
return {
message: toolboxItems.filter((action) => !hiddenActions.includes(action.id)),
menu: menuItems.filter((action) => !(isLayoutEmbedded && action.id === 'reply-directly') && !hiddenActions.includes(action.id)),
};
},
keepPreviousData: true,
});

const toolbox = useRoomToolbox();
Expand Down
33 changes: 33 additions & 0 deletions apps/meteor/client/lib/mutationEffects/starredMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { IMessage } from '@rocket.chat/core-typings';
import { Meteor } from 'meteor/meteor';

import { Messages } from '../../../app/models/client';
import { t } from '../../../app/utils/lib/i18n';
import { dispatchToastMessage } from '../toast';

export const toggleStarredMessage = (message: IMessage, starred: boolean) => {
const uid = Meteor.userId()!;

if (starred) {
Messages.update(
{ _id: message._id },
{
$addToSet: {
starred: { _id: uid },
},
},
);
dispatchToastMessage({ type: 'success', message: t('Message_has_been_starred') });
return;
}

Messages.update(
{ _id: message._id },
{
$pull: {
starred: { _id: uid },
},
},
);
dispatchToastMessage({ type: 'success', message: t('Message_has_been_unstarred') });
};
12 changes: 12 additions & 0 deletions apps/meteor/client/lib/queryKeys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { IMessage, IRoom, Serialized } from '@rocket.chat/core-typings';

export const roomsQueryKeys = {
all: ['rooms'] as const,
room: (rid: IRoom['_id']) => ['rooms', rid] as const,
starredMessages: (rid: IRoom['_id']) => [...roomsQueryKeys.room(rid), 'starred-messages'] as const,
messages: (rid: IRoom['_id']) => [...roomsQueryKeys.room(rid), 'messages'] as const,
message: (rid: IRoom['_id'], mid: IMessage['_id']) => [...roomsQueryKeys.messages(rid), mid] as const,
messageActions: (rid: IRoom['_id'], mid: IMessage['_id']) => [...roomsQueryKeys.message(rid, mid), 'actions'] as const,
messageActionsWithParameters: (rid: IRoom['_id'], message: IMessage | Serialized<IMessage>) =>
[...roomsQueryKeys.messageActions(rid, message._id), message] as const,
};
1 change: 0 additions & 1 deletion apps/meteor/client/methods/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import './hideRoom';
import './openRoom';
import './pinMessage';
import './starMessage';
import './toggleFavorite';
import './unpinMessage';
import './updateMessage';
60 changes: 0 additions & 60 deletions apps/meteor/client/methods/starMessage.ts

This file was deleted.

13 changes: 8 additions & 5 deletions apps/meteor/client/startup/actionButtons/starMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { Meteor } from 'meteor/meteor';
import { settings } from '../../../app/settings/client';
import { MessageAction } from '../../../app/ui-utils/client';
import { sdk } from '../../../app/utils/client/lib/SDKClient';
import { toggleStarredMessage } from '../../lib/mutationEffects/starredMessage';
import { queryClient } from '../../lib/queryClient';
import { roomsQueryKeys } from '../../lib/queryKeys';
import { roomCoordinator } from '../../lib/rooms/roomCoordinator';
import { dispatchToastMessage } from '../../lib/toast';

Expand All @@ -16,12 +18,13 @@ Meteor.startup(() => {
context: ['starred', 'message', 'message-mobile', 'threads', 'federated', 'videoconf', 'videoconf-threads'],
async action(_, { message }) {
try {
await sdk.call('starMessage', { ...message, starred: true });
queryClient.invalidateQueries(['rooms', message.rid, 'starred-messages']);
await sdk.rest.post('/v1/chat.starMessage', { messageId: message._id });
toggleStarredMessage(message, true);
} catch (error) {
if (error) {
dispatchToastMessage({ type: 'error', message: error });
}
dispatchToastMessage({ type: 'error', message: error });
} finally {
queryClient.invalidateQueries(roomsQueryKeys.starredMessages(message.rid));
queryClient.invalidateQueries(roomsQueryKeys.messageActions(message.rid, message._id));
}
},
condition({ message, subscription, user, room }) {
Expand Down
9 changes: 7 additions & 2 deletions apps/meteor/client/startup/actionButtons/unstarMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { Meteor } from 'meteor/meteor';
import { settings } from '../../../app/settings/client';
import { MessageAction } from '../../../app/ui-utils/client';
import { sdk } from '../../../app/utils/client/lib/SDKClient';
import { toggleStarredMessage } from '../../lib/mutationEffects/starredMessage';
import { queryClient } from '../../lib/queryClient';
import { roomsQueryKeys } from '../../lib/queryKeys';
import { dispatchToastMessage } from '../../lib/toast';

Meteor.startup(() => {
Expand All @@ -15,10 +17,13 @@ Meteor.startup(() => {
context: ['starred', 'message', 'message-mobile', 'threads', 'federated', 'videoconf', 'videoconf-threads'],
async action(_, { message }) {
try {
await sdk.call('starMessage', { ...message, starred: false });
queryClient.invalidateQueries(['rooms', message.rid, 'starred-messages']);
await sdk.rest.post('/v1/chat.unStarMessage', { messageId: message._id });
toggleStarredMessage(message, false);
} catch (error) {
dispatchToastMessage({ type: 'error', message: error });
} finally {
queryClient.invalidateQueries(roomsQueryKeys.starredMessages(message.rid));
queryClient.invalidateQueries(roomsQueryKeys.messageActions(message.rid, message._id));
}
},
condition({ message, subscription, user }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useTranslation } from 'react-i18next';

import MessageListTab from './MessageListTab';
import { onClientMessageReceived } from '../../../lib/onClientMessageReceived';
import { roomsQueryKeys } from '../../../lib/queryKeys';
import { mapMessageFromApi } from '../../../lib/utils/mapMessageFromApi';
import { useRoom } from '../contexts/RoomContext';

Expand All @@ -14,19 +15,22 @@ const StarredMessagesTab = () => {

const room = useRoom();

const starredMessagesQueryResult = useQuery(['rooms', room._id, 'starred-messages'] as const, async () => {
const messages: IMessage[] = [];

for (
let offset = 0, result = await getStarredMessages({ roomId: room._id, offset: 0 });
result.count > 0;
// eslint-disable-next-line no-await-in-loop
offset += result.count, result = await getStarredMessages({ roomId: room._id, offset })
) {
messages.push(...result.messages.map(mapMessageFromApi));
}

return Promise.all(messages.map(onClientMessageReceived));
const starredMessagesQueryResult = useQuery({
queryKey: roomsQueryKeys.starredMessages(room._id),
queryFn: async () => {
const messages: IMessage[] = [];

for (
let offset = 0, result = await getStarredMessages({ roomId: room._id, offset: 0 });
result.count > 0;
// eslint-disable-next-line no-await-in-loop
offset += result.count, result = await getStarredMessages({ roomId: room._id, offset })
) {
messages.push(...result.messages.map(mapMessageFromApi));
}

return Promise.all(messages.map(onClientMessageReceived));
},
});

const { t } = useTranslation();
Expand Down
Loading