From 2d1b0936663c9fbdbd08fa919b2d91a2cc5e8ac9 Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Mon, 7 Feb 2022 14:44:04 -0400 Subject: [PATCH] Chore: Migrate lib/utils to TypeScript (#3637) * Migrate utils to TypeScript * Add @types/semver * Refactor compareServerVersion(currentVersion, oldVersion, func) to compareServerVersion(current, func, oldVersion) Co-authored-by: Diego Mello --- app/containers/Avatar/interfaces.ts | 2 +- app/containers/message/Reply.tsx | 2 +- app/containers/message/Video.tsx | 2 +- app/lib/methods/enterpriseModules.js | 4 +-- app/lib/methods/getCustomEmojis.js | 4 +-- app/lib/methods/getPermissions.js | 4 +-- app/lib/methods/getUsersPresence.js | 8 +++--- .../helpers/mergeSubscriptionsRooms.js | 4 +-- app/lib/rocketchat.js | 10 +++---- app/lib/utils.js | 23 ---------------- app/lib/utils.ts | 27 +++++++++++++++++++ app/utils/avatar.ts | 4 +-- app/views/AttachmentView.tsx | 6 ++++- app/views/NewMessageView.tsx | 4 +-- app/views/RoomActionsView/index.js | 4 +-- app/views/RoomInfoEditView/index.js | 8 +++--- app/views/RoomView/List/index.js | 4 +-- app/views/SearchMessagesView/index.tsx | 4 +-- package.json | 1 + yarn.lock | 5 ++++ 20 files changed, 72 insertions(+), 58 deletions(-) delete mode 100644 app/lib/utils.js create mode 100644 app/lib/utils.ts diff --git a/app/containers/Avatar/interfaces.ts b/app/containers/Avatar/interfaces.ts index 78152e52..c9a45eb4 100644 --- a/app/containers/Avatar/interfaces.ts +++ b/app/containers/Avatar/interfaces.ts @@ -19,5 +19,5 @@ export interface IAvatar { isStatic?: boolean | string; rid?: string; blockUnauthenticatedAccess?: boolean; - serverVersion?: string; + serverVersion: string; } diff --git a/app/containers/message/Reply.tsx b/app/containers/message/Reply.tsx index 56fdc607..33d5f09f 100644 --- a/app/containers/message/Reply.tsx +++ b/app/containers/message/Reply.tsx @@ -214,7 +214,7 @@ const Reply = React.memo( if (!url) { return; } - if (attachment.type === 'file') { + if (attachment.type === 'file' && attachment.title_link) { setLoading(true); url = formatAttachmentUrl(attachment.title_link, user.id, user.token, baseUrl); await fileDownloadAndPreview(url, attachment); diff --git a/app/containers/message/Video.tsx b/app/containers/message/Video.tsx index 15990eeb..f2a976d5 100644 --- a/app/containers/message/Video.tsx +++ b/app/containers/message/Video.tsx @@ -50,7 +50,7 @@ const Video = React.memo( return showAttachment(file); } - if (!isIOS) { + if (!isIOS && file.video_url) { const uri = formatAttachmentUrl(file.video_url, user.id, user.token, baseUrl); await downloadVideo(uri); return; diff --git a/app/lib/methods/enterpriseModules.js b/app/lib/methods/enterpriseModules.js index 312a9925..16037ccf 100644 --- a/app/lib/methods/enterpriseModules.js +++ b/app/lib/methods/enterpriseModules.js @@ -1,4 +1,4 @@ -import { compareServerVersion, methods } from '../utils'; +import { compareServerVersion } from '../utils'; import reduxStore from '../createStore'; import database from '../database'; import log from '../../utils/log'; @@ -32,7 +32,7 @@ export function getEnterpriseModules() { return new Promise(async resolve => { try { const { version: serverVersion, server: serverId } = reduxStore.getState().server; - if (compareServerVersion(serverVersion, '3.1.0', methods.greaterThanOrEqualTo)) { + if (compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '3.1.0')) { // RC 3.1.0 const enterpriseModules = await this.methodCallWrapper('license:getModules'); if (enterpriseModules) { diff --git a/app/lib/methods/getCustomEmojis.js b/app/lib/methods/getCustomEmojis.js index 5e8a3e74..28c908e0 100644 --- a/app/lib/methods/getCustomEmojis.js +++ b/app/lib/methods/getCustomEmojis.js @@ -1,7 +1,7 @@ import orderBy from 'lodash/orderBy'; import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; -import { compareServerVersion, methods } from '../utils'; +import { compareServerVersion } from '../utils'; import reduxStore from '../createStore'; import database from '../database'; import log from '../../utils/log'; @@ -92,7 +92,7 @@ export function getCustomEmojis() { const updatedSince = await getUpdatedSince(allRecords); // if server version is lower than 0.75.0, fetches from old api - if (compareServerVersion(serverVersion, '0.75.0', methods.lowerThan)) { + if (compareServerVersion(serverVersion, 'lowerThan', '0.75.0')) { // RC 0.61.0 const result = await this.sdk.get('emoji-custom'); diff --git a/app/lib/methods/getPermissions.js b/app/lib/methods/getPermissions.js index 889aeaf1..6ca91cda 100644 --- a/app/lib/methods/getPermissions.js +++ b/app/lib/methods/getPermissions.js @@ -2,7 +2,7 @@ import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; import { Q } from '@nozbe/watermelondb'; import orderBy from 'lodash/orderBy'; -import { compareServerVersion, methods } from '../utils'; +import { compareServerVersion } from '../utils'; import database from '../database'; import log from '../../utils/log'; import reduxStore from '../createStore'; @@ -146,7 +146,7 @@ export function getPermissions() { const allRecords = await permissionsCollection.query().fetch(); RocketChat.subscribe('stream-notify-logged', 'permissions-changed'); // if server version is lower than 0.73.0, fetches from old api - if (compareServerVersion(serverVersion, '0.73.0', methods.lowerThan)) { + if (compareServerVersion(serverVersion, 'lowerThan', '0.73.0')) { // RC 0.66.0 const result = await this.sdk.get('permissions.list'); if (!result.success) { diff --git a/app/lib/methods/getUsersPresence.js b/app/lib/methods/getUsersPresence.js index 07a7b985..0e65a5f3 100644 --- a/app/lib/methods/getUsersPresence.js +++ b/app/lib/methods/getUsersPresence.js @@ -1,7 +1,7 @@ import { InteractionManager } from 'react-native'; import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; -import { compareServerVersion, methods } from '../utils'; +import { compareServerVersion } from '../utils'; import reduxStore from '../createStore'; import { setActiveUsers } from '../../actions/activeUsers'; import { setUser } from '../../actions/login'; @@ -11,7 +11,7 @@ export function subscribeUsersPresence() { const serverVersion = reduxStore.getState().server.version; // if server is lower than 1.1.0 - if (compareServerVersion(serverVersion, '1.1.0', methods.lowerThan)) { + if (compareServerVersion(serverVersion, 'lowerThan', '1.1.0')) { if (this.activeUsersSubTimeout) { clearTimeout(this.activeUsersSubTimeout); this.activeUsersSubTimeout = false; @@ -36,11 +36,11 @@ export default async function getUsersPresence() { const { user: loggedUser } = reduxStore.getState().login; // if server is greather than or equal 1.1.0 - if (compareServerVersion(serverVersion, '1.1.0', methods.greaterThanOrEqualTo)) { + if (compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '1.1.0')) { let params = {}; // if server is greather than or equal 3.0.0 - if (compareServerVersion(serverVersion, '3.0.0', methods.greaterThanOrEqualTo)) { + if (compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '3.0.0')) { // if not have any id if (!ids.length) { return; diff --git a/app/lib/methods/helpers/mergeSubscriptionsRooms.js b/app/lib/methods/helpers/mergeSubscriptionsRooms.js index 95a81001..d831c5f8 100644 --- a/app/lib/methods/helpers/mergeSubscriptionsRooms.js +++ b/app/lib/methods/helpers/mergeSubscriptionsRooms.js @@ -2,7 +2,7 @@ import EJSON from 'ejson'; import { Encryption } from '../../encryption'; import reduxStore from '../../createStore'; -import { compareServerVersion, methods } from '../../utils'; +import { compareServerVersion } from '../../utils'; import findSubscriptionsRooms from './findSubscriptionsRooms'; import normalizeMessage from './normalizeMessage'; // TODO: delete and update @@ -28,7 +28,7 @@ export const merge = (subscription, room) => { subscription.usernames = room.usernames; subscription.uids = room.uids; } - if (compareServerVersion(serverVersion, '3.7.0', methods.lowerThan)) { + if (compareServerVersion(serverVersion, 'lowerThan', '3.7.0')) { const updatedAt = room?._updatedAt ? new Date(room._updatedAt) : null; const lastMessageTs = subscription?.lastMessage?.ts ? new Date(subscription.lastMessage.ts) : null; subscription.roomUpdatedAt = Math.max(updatedAt, lastMessageTs); diff --git a/app/lib/rocketchat.js b/app/lib/rocketchat.js index 1cb627a4..f711edea 100644 --- a/app/lib/rocketchat.js +++ b/app/lib/rocketchat.js @@ -26,7 +26,7 @@ import EventEmitter from '../utils/events'; import { updatePermission } from '../actions/permissions'; import { TEAM_TYPE } from '../definitions/ITeam'; import { updateSettings } from '../actions/settings'; -import { compareServerVersion, methods } from './utils'; +import { compareServerVersion } from './utils'; import reduxStore from './createStore'; import database from './database'; import subscribeRooms from './methods/subscriptions/rooms'; @@ -138,7 +138,7 @@ const RocketChat = { message: I18n.t('Not_RC_Server', { contact: I18n.t('Contact_your_server_admin') }) }; } - if (compareServerVersion(jsonRes.version, MIN_ROCKETCHAT_VERSION, methods.lowerThan)) { + if (compareServerVersion(jsonRes.version, 'lowerThan', MIN_ROCKETCHAT_VERSION)) { return { success: false, message: I18n.t('Invalid_server_version', { @@ -549,7 +549,7 @@ const RocketChat = { // Force normalized params for 2FA starting RC 3.9.0. const serverVersion = reduxStore.getState().server.version; - if (compareServerVersion(serverVersion, '3.9.0', methods.greaterThanOrEqualTo)) { + if (compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '3.9.0')) { const user = params.user ?? params.username; const password = params.password ?? params.ldapPass ?? params.crowdPassword; params = { user, password }; @@ -1059,7 +1059,7 @@ const RocketChat = { }, async getRoomMembers({ rid, allUsers, roomType, type, filter, skip = 0, limit = 10 }) { const serverVersion = reduxStore.getState().server.version; - if (compareServerVersion(serverVersion, '3.16.0', methods.greaterThanOrEqualTo)) { + if (compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '3.16.0')) { const params = { roomId: rid, offset: skip, @@ -1587,7 +1587,7 @@ const RocketChat = { }, readThreads(tmid) { const serverVersion = reduxStore.getState().server.version; - if (compareServerVersion(serverVersion, '3.4.0', methods.greaterThanOrEqualTo)) { + if (compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '3.4.0')) { // RC 3.4.0 return this.methodCallWrapper('readThreads', tmid); } diff --git a/app/lib/utils.js b/app/lib/utils.js deleted file mode 100644 index 5776a338..00000000 --- a/app/lib/utils.js +++ /dev/null @@ -1,23 +0,0 @@ -import { coerce, gt, gte, lt, lte } from 'semver'; - -export const formatAttachmentUrl = (attachmentUrl, userId, token, server) => { - if (attachmentUrl.startsWith('http')) { - if (attachmentUrl.includes('rc_token')) { - return encodeURI(attachmentUrl); - } - return encodeURI(`${attachmentUrl}?rc_uid=${userId}&rc_token=${token}`); - } - return encodeURI(`${server}${attachmentUrl}?rc_uid=${userId}&rc_token=${token}`); -}; - -export const methods = { - lowerThan: lt, - lowerThanOrEqualTo: lte, - greaterThan: gt, - greaterThanOrEqualTo: gte -}; - -export const compareServerVersion = (currentServerVersion, versionToCompare, func) => - currentServerVersion && func(coerce(currentServerVersion), versionToCompare); - -export const generateLoadMoreId = id => `load-more-${id}`; diff --git a/app/lib/utils.ts b/app/lib/utils.ts new file mode 100644 index 00000000..bece818b --- /dev/null +++ b/app/lib/utils.ts @@ -0,0 +1,27 @@ +import { coerce, gt, gte, lt, lte, SemVer } from 'semver'; + +export const formatAttachmentUrl = (attachmentUrl: string, userId: string, token: string, server: string): string => { + if (attachmentUrl.startsWith('http')) { + if (attachmentUrl.includes('rc_token')) { + return encodeURI(attachmentUrl); + } + return encodeURI(`${attachmentUrl}?rc_uid=${userId}&rc_token=${token}`); + } + return encodeURI(`${server}${attachmentUrl}?rc_uid=${userId}&rc_token=${token}`); +}; + +const methods = { + lowerThan: lt, + lowerThanOrEqualTo: lte, + greaterThan: gt, + greaterThanOrEqualTo: gte +}; + +export const compareServerVersion = ( + currentServerVersion: string, + method: keyof typeof methods, + versionToCompare: string +): boolean => + (currentServerVersion && methods[method](coerce(currentServerVersion) as string | SemVer, versionToCompare)) as boolean; + +export const generateLoadMoreId = (id: string): string => `load-more-${id}`; diff --git a/app/utils/avatar.ts b/app/utils/avatar.ts index 7e4b2819..a2045f7d 100644 --- a/app/utils/avatar.ts +++ b/app/utils/avatar.ts @@ -1,4 +1,4 @@ -import { compareServerVersion, methods } from '../lib/utils'; +import { compareServerVersion } from '../lib/utils'; import { SubscriptionType } from '../definitions/ISubscription'; import { IAvatar } from '../containers/Avatar/interfaces'; @@ -19,7 +19,7 @@ export const avatarURL = ({ let room; if (type === SubscriptionType.DIRECT) { room = text; - } else if (rid && !compareServerVersion(serverVersion, '3.6.0', methods.lowerThan)) { + } else if (rid && !compareServerVersion(serverVersion, 'lowerThan', '3.6.0')) { room = `room/${rid}`; } else { room = `@${text}`; diff --git a/app/views/AttachmentView.tsx b/app/views/AttachmentView.tsx index d0bd021c..b4054bd2 100644 --- a/app/views/AttachmentView.tsx +++ b/app/views/AttachmentView.tsx @@ -107,8 +107,12 @@ class AttachmentView extends React.Component + disabled={compareServerVersion(serverVersion, 'lowerThan', '3.6.0')}> - {compareServerVersion(serverVersion, '3.6.0', methods.lowerThan) ? null : ( + {compareServerVersion(serverVersion, 'lowerThan', '3.6.0') ? null : ( @@ -670,7 +670,7 @@ class RoomInfoEditView extends React.Component { ] : null} - {!compareServerVersion(serverVersion, '3.0.0', methods.lowerThan) ? ( + {!compareServerVersion(serverVersion, 'lowerThan', '3.0.0') ? ( !m.t || !hideSystemMessages?.includes(m.t)); } diff --git a/app/views/SearchMessagesView/index.tsx b/app/views/SearchMessagesView/index.tsx index 70e924d2..c4cdd6de 100644 --- a/app/views/SearchMessagesView/index.tsx +++ b/app/views/SearchMessagesView/index.tsx @@ -29,7 +29,7 @@ import { sanitizeLikeString } from '../../lib/database/utils'; import getThreadName from '../../lib/methods/getThreadName'; import getRoomInfo from '../../lib/methods/getRoomInfo'; import { isIOS } from '../../utils/deviceInfo'; -import { compareServerVersion, methods } from '../../lib/utils'; +import { compareServerVersion } from '../../lib/utils'; import styles from './styles'; import { InsideStackParamList, ChatsStackParamList } from '../../stacks/types'; @@ -235,7 +235,7 @@ class SearchMessagesView extends React.Component