From 69cf9b638e7092a9e4a781e695ba575f8c25dff0 Mon Sep 17 00:00:00 2001 From: Alex Junior Date: Thu, 10 Mar 2022 12:00:50 -0300 Subject: [PATCH] Chore: Migrate REST API - getMessages to Typescript (#3875) --- app/definitions/IMessage.ts | 17 ++++++++++++++++- app/definitions/rest/v1/channels.ts | 10 ++++++++++ app/definitions/rest/v1/groups.ts | 10 ++++++++++ app/definitions/rest/v1/im.ts | 10 ++++++++++ app/lib/rocketchat/services/restApi.ts | 13 +++++++++---- app/views/MessagesView/index.tsx | 3 --- 6 files changed, 55 insertions(+), 8 deletions(-) diff --git a/app/definitions/IMessage.ts b/app/definitions/IMessage.ts index d836400f7..538d4d258 100644 --- a/app/definitions/IMessage.ts +++ b/app/definitions/IMessage.ts @@ -87,6 +87,17 @@ export interface IMessageFromServer { drid?: string; dcount?: number; dml: string | Date; + starred?: + | { + _id: string; + } + | boolean; + pinned?: boolean; + pinnedAt?: string | Date; + pinnedBy?: { + _id: string; + username: string; + }; } export interface ILoadMoreMessage { @@ -106,7 +117,11 @@ export interface IMessage extends IMessageFromServer { emoji?: string; status?: number; pinned?: boolean; - starred?: boolean; + starred?: + | { + _id: string; + } + | boolean; editedBy?: IEditedBy; reactions?: IReaction[]; role?: string; diff --git a/app/definitions/rest/v1/channels.ts b/app/definitions/rest/v1/channels.ts index 3e0c72e8a..caeb02d04 100644 --- a/app/definitions/rest/v1/channels.ts +++ b/app/definitions/rest/v1/channels.ts @@ -99,4 +99,14 @@ export type ChannelsEndpoints = { 'channels.removeLeader': { POST: (params: { roomId: string; userId: string }) => {}; }; + 'channels.messages': { + GET: (params: { + roomId: IServerRoom['_id']; + query: { 'mentions._id': { $in: string[] } } | { 'starred._id': { $in: string[] } } | { pinned: boolean }; + offset: number; + sort: { ts: number }; + }) => { + messages: IMessageFromServer[]; + }; + }; }; diff --git a/app/definitions/rest/v1/groups.ts b/app/definitions/rest/v1/groups.ts index 22ff41495..62be346d0 100644 --- a/app/definitions/rest/v1/groups.ts +++ b/app/definitions/rest/v1/groups.ts @@ -69,4 +69,14 @@ export type GroupsEndpoints = { 'groups.leave': { POST: (params: { roomId: string }) => {}; }; + 'groups.messages': { + GET: (params: { + roomId: IServerRoom['_id']; + query: { 'mentions._id': { $in: string[] } } | { 'starred._id': { $in: string[] } } | { pinned: boolean }; + offset: number; + sort: { ts: number }; + }) => { + messages: IMessageFromServer[]; + }; + }; }; diff --git a/app/definitions/rest/v1/im.ts b/app/definitions/rest/v1/im.ts index 9b2de6c60..3a136c3cd 100644 --- a/app/definitions/rest/v1/im.ts +++ b/app/definitions/rest/v1/im.ts @@ -55,4 +55,14 @@ export type ImEndpoints = { 'im.leave': { POST: (params: { roomId: string }) => {}; }; + 'im.messages': { + GET: (params: { + roomId: IServerRoom['_id']; + query: { 'mentions._id': { $in: string[] } } | { 'starred._id': { $in: string[] } } | { pinned: boolean }; + offset: number; + sort: { ts: number }; + }) => { + messages: IMessageFromServer[]; + }; + }; }; diff --git a/app/lib/rocketchat/services/restApi.ts b/app/lib/rocketchat/services/restApi.ts index cc5f9e361..efe25d39f 100644 --- a/app/lib/rocketchat/services/restApi.ts +++ b/app/lib/rocketchat/services/restApi.ts @@ -600,16 +600,21 @@ export const getFiles = (roomId: string, type: RoomTypes, offset: number): any = sort: { uploadedAt: -1 } }); -export const getMessages = (roomId: string, type: RoomTypes, query: any, offset: number): any => +export const getMessages = ( + roomId: string, + type: SubscriptionType, + query: { 'mentions._id': { $in: string[] } } | { 'starred._id': { $in: string[] } } | { pinned: boolean }, + offset: number +) => { + const t = type as SubscriptionType.DIRECT | SubscriptionType.CHANNEL | SubscriptionType.GROUP; // RC 0.59.0 - // TODO: missing definitions from server - // @ts-ignore - sdk.get(`${roomTypeToApiType(type)}.messages`, { + return sdk.get(`${roomTypeToApiType(t)}.messages`, { roomId, query, offset, sort: { ts: -1 } }); +}; export const getReadReceipts = (messageId: string): any => // RC 0.63.0 diff --git a/app/views/MessagesView/index.tsx b/app/views/MessagesView/index.tsx index b3b6a8494..538221615 100644 --- a/app/views/MessagesView/index.tsx +++ b/app/views/MessagesView/index.tsx @@ -226,7 +226,6 @@ class MessagesView extends React.Component { name: I18n.t('Mentions'), fetchFunc: () => { const { messages } = this.state; - // @ts-ignore return RocketChat.getMessages(this.rid, this.t, { 'mentions._id': { $in: [user.id] } }, messages.length); }, noDataMsg: I18n.t('No_mentioned_messages'), @@ -239,7 +238,6 @@ class MessagesView extends React.Component { name: I18n.t('Starred'), fetchFunc: () => { const { messages } = this.state; - // @ts-ignore return RocketChat.getMessages(this.rid, this.t, { 'starred._id': { $in: [user.id] } }, messages.length); }, noDataMsg: I18n.t('No_starred_messages'), @@ -260,7 +258,6 @@ class MessagesView extends React.Component { name: I18n.t('Pinned'), fetchFunc: () => { const { messages } = this.state; - // @ts-ignore return RocketChat.getMessages(this.rid, this.t, { pinned: true }, messages.length); }, noDataMsg: I18n.t('No_pinned_messages'),