From d73da2942b9f1433317a822ffe539d08ab8c21a7 Mon Sep 17 00:00:00 2001 From: Alex Junior Date: Thu, 10 Mar 2022 12:29:33 -0300 Subject: [PATCH] Chore: Migrate REST API - getFiles to Typescript (#3873) --- app/definitions/IAttachment.ts | 29 ++++++++++++++++++++++++++ app/definitions/rest/v1/channels.ts | 13 +++++------- app/definitions/rest/v1/groups.ts | 9 +++++--- app/definitions/rest/v1/im.ts | 9 +++++--- app/lib/rocketchat/services/restApi.ts | 8 +++---- app/views/MessagesView/index.tsx | 5 +++-- 6 files changed, 53 insertions(+), 20 deletions(-) diff --git a/app/definitions/IAttachment.ts b/app/definitions/IAttachment.ts index 788d5876..06ad1859 100644 --- a/app/definitions/IAttachment.ts +++ b/app/definitions/IAttachment.ts @@ -1,3 +1,5 @@ +import { IUser } from './IUser'; + export interface IAttachment { ts: string | Date; title: string; @@ -23,3 +25,30 @@ export interface IAttachment { color?: string; thumb_url?: string; } + +export interface IServerAttachment { + _id: string; + name: string; + size: number; + type: string; + rid: string; + userId: string; + AmazonS3: { path: string }; + store: string; + identify: { + format: string; + size: { + width: number; + height: number; + }; + }; + complete: boolean; + etag: string; + path: string; + progress: boolean; + token: string; + uploadedAt: string | Date; + uploading: boolean; + url: string; + user: Pick; +} diff --git a/app/definitions/rest/v1/channels.ts b/app/definitions/rest/v1/channels.ts index caeb02d0..6e2462a2 100644 --- a/app/definitions/rest/v1/channels.ts +++ b/app/definitions/rest/v1/channels.ts @@ -1,18 +1,15 @@ import { ITeam } from '../../ITeam'; -import type { IMessage, IMessageFromServer } from '../../IMessage'; +import type { IMessageFromServer } from '../../IMessage'; import type { IServerRoom } from '../../IRoom'; import type { IUser } from '../../IUser'; +import { IServerAttachment } from '../../IAttachment'; export type ChannelsEndpoints = { 'channels.files': { - GET: (params: { - roomId: IServerRoom['_id']; - offset: number; + GET: (params: { roomId: IServerRoom['_id']; offset: number; sort: string | { uploadedAt: number } }) => { + files: IServerAttachment[]; count: number; - sort: string | { uploadedAt: number }; - query: string; - }) => { - files: IMessage[]; + offset: number; total: number; }; }; diff --git a/app/definitions/rest/v1/groups.ts b/app/definitions/rest/v1/groups.ts index 62be346d..dd032e45 100644 --- a/app/definitions/rest/v1/groups.ts +++ b/app/definitions/rest/v1/groups.ts @@ -1,12 +1,15 @@ import { ITeam } from '../../ITeam'; -import type { IMessage, IMessageFromServer } from '../../IMessage'; +import type { IMessageFromServer } from '../../IMessage'; import type { IServerRoom } from '../../IRoom'; import type { IUser } from '../../IUser'; +import { IServerAttachment } from '../../IAttachment'; export type GroupsEndpoints = { 'groups.files': { - GET: (params: { roomId: IServerRoom['_id']; count: number; sort: string | { uploadedAt: number }; query: string }) => { - files: IMessage[]; + GET: (params: { roomId: IServerRoom['_id']; offset: number; sort: string | { uploadedAt: number } }) => { + files: IServerAttachment[]; + count: number; + offset: number; total: number; }; }; diff --git a/app/definitions/rest/v1/im.ts b/app/definitions/rest/v1/im.ts index 3a136c3c..2383fe50 100644 --- a/app/definitions/rest/v1/im.ts +++ b/app/definitions/rest/v1/im.ts @@ -1,6 +1,7 @@ -import type { IMessage, IMessageFromServer } from '../../IMessage'; +import type { IMessageFromServer } from '../../IMessage'; import type { IServerRoom, RoomID, RoomType } from '../../IRoom'; import type { IUser } from '../../IUser'; +import { IServerAttachment } from '../../IAttachment'; export type ImEndpoints = { 'im.create': { @@ -25,8 +26,10 @@ export type ImEndpoints = { }; }; 'im.files': { - GET: (params: { roomId: IServerRoom['_id']; count: number; sort: string | { uploadedAt: number }; query: string }) => { - files: IMessage[]; + GET: (params: { roomId: IServerRoom['_id']; offset: number; sort: string | { uploadedAt: number } }) => { + files: IServerAttachment[]; + count: number; + offset: number; total: number; }; }; diff --git a/app/lib/rocketchat/services/restApi.ts b/app/lib/rocketchat/services/restApi.ts index 9d486d7e..738e323a 100644 --- a/app/lib/rocketchat/services/restApi.ts +++ b/app/lib/rocketchat/services/restApi.ts @@ -590,15 +590,15 @@ export const getUsernameSuggestion = () => // RC 0.65.0 sdk.get('users.getUsernameSuggestion'); -export const getFiles = (roomId: string, type: RoomTypes, offset: number): any => +export const getFiles = (roomId: string, type: SubscriptionType, 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)}.files`, { + return sdk.get(`${roomTypeToApiType(t)}.files`, { roomId, offset, sort: { uploadedAt: -1 } }); +}; export const getMessages = ( roomId: string, diff --git a/app/views/MessagesView/index.tsx b/app/views/MessagesView/index.tsx index 53822161..66070d1f 100644 --- a/app/views/MessagesView/index.tsx +++ b/app/views/MessagesView/index.tsx @@ -196,9 +196,10 @@ class MessagesView extends React.Component { name: I18n.t('Files'), fetchFunc: async () => { const { messages } = this.state; - // @ts-ignore const result = await RocketChat.getFiles(this.rid, this.t, messages.length); - return { ...result, messages: result.files }; + if (result.success) { + return { ...result, messages: result.files }; + } }, noDataMsg: I18n.t('No_files'), testID: 'room-files-view',