From 460e82f759a5e8e42b10107508945eeb28b86bc4 Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Thu, 18 Mar 2021 09:33:35 -0400 Subject: [PATCH] [CHORE] Simplify server version comparison (#2922) * Simplify server version where needed * Added lte and gte functions and updated imports * Updated functions names * Update util functions * Update util function and added methods * Remove lt and coerce from getPermissions and mergeSubscriptionsRooms * Fix comparison * Update getPermissions.js * Remove unused import * Fix lint * Fix lint Co-authored-by: Diego Mello --- app/lib/methods/enterpriseModules.js | 5 ++--- app/lib/methods/getCustomEmojis.js | 4 ++-- app/lib/methods/getPermissions.js | 5 ++--- app/lib/methods/getUsersPresence.js | 9 ++++----- app/lib/methods/helpers/mergeSubscriptionsRooms.js | 4 ++-- app/lib/rocketchat.js | 10 ++++------ app/lib/utils.js | 13 +++++++++++++ app/utils/avatar.js | 5 ++--- app/views/RoomActionsView/index.js | 5 ++--- app/views/RoomInfoEditView/index.js | 14 ++++---------- 10 files changed, 37 insertions(+), 37 deletions(-) diff --git a/app/lib/methods/enterpriseModules.js b/app/lib/methods/enterpriseModules.js index 35c6672d2..d86ae26fd 100644 --- a/app/lib/methods/enterpriseModules.js +++ b/app/lib/methods/enterpriseModules.js @@ -1,5 +1,4 @@ -import gte from 'semver/functions/gte'; -import coerce from 'semver/functions/coerce'; +import { compareServerVersion, methods } from '../utils'; import reduxStore from '../createStore'; import database from '../database'; @@ -34,7 +33,7 @@ export function getEnterpriseModules() { return new Promise(async(resolve) => { try { const { version: serverVersion, server: serverId } = reduxStore.getState().server; - if (serverVersion && gte(coerce(serverVersion), '3.1.0')) { + if (compareServerVersion(serverVersion, '3.1.0', methods.greaterThanOrEqualTo)) { // 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 31108fb62..d56a20bac 100644 --- a/app/lib/methods/getCustomEmojis.js +++ b/app/lib/methods/getCustomEmojis.js @@ -1,7 +1,7 @@ -import lt from 'semver/functions/lt'; import orderBy from 'lodash/orderBy'; import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; +import { compareServerVersion, methods } from '../utils'; import reduxStore from '../createStore'; import database from '../database'; import log from '../../utils/log'; @@ -90,7 +90,7 @@ export function getCustomEmojis() { const updatedSince = await getUpdatedSince(allRecords); // if server version is lower than 0.75.0, fetches from old api - if (serverVersion && lt(serverVersion, '0.75.0')) { + if (compareServerVersion(serverVersion, '0.75.0', methods.lowerThan)) { // 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 15ee221b8..09b91aa63 100644 --- a/app/lib/methods/getPermissions.js +++ b/app/lib/methods/getPermissions.js @@ -1,9 +1,8 @@ -import lt from 'semver/functions/lt'; import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; import { Q } from '@nozbe/watermelondb'; -import coerce from 'semver/functions/coerce'; import orderBy from 'lodash/orderBy'; +import { compareServerVersion, methods } from '../utils'; import database from '../database'; import log from '../../utils/log'; import reduxStore from '../createStore'; @@ -123,7 +122,7 @@ export function getPermissions() { const allRecords = await permissionsCollection.query().fetch(); // if server version is lower than 0.73.0, fetches from old api - if (serverVersion && lt(coerce(serverVersion), '0.73.0')) { + if (compareServerVersion(serverVersion, '0.73.0', methods.lowerThan)) { // 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 83e5ce815..edbd16792 100644 --- a/app/lib/methods/getUsersPresence.js +++ b/app/lib/methods/getUsersPresence.js @@ -1,8 +1,7 @@ import { InteractionManager } from 'react-native'; -import lt from 'semver/functions/lt'; -import gte from 'semver/functions/gte'; import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; +import { compareServerVersion, methods } from '../utils'; import reduxStore from '../createStore'; import { setActiveUsers } from '../../actions/activeUsers'; import { setUser } from '../../actions/login'; @@ -12,7 +11,7 @@ export function subscribeUsersPresence() { const serverVersion = reduxStore.getState().server.version; // if server is lower than 1.1.0 - if (serverVersion && lt(serverVersion, '1.1.0')) { + if (compareServerVersion(serverVersion, '1.1.0', methods.lowerThan)) { if (this.activeUsersSubTimeout) { clearTimeout(this.activeUsersSubTimeout); this.activeUsersSubTimeout = false; @@ -37,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 (serverVersion && gte(serverVersion, '1.1.0')) { + if (compareServerVersion(serverVersion, '1.1.0', methods.greaterThanOrEqualTo)) { let params = {}; // if server is greather than or equal 3.0.0 - if (serverVersion && gte(serverVersion, '3.0.0')) { + if (compareServerVersion(serverVersion, '3.0.0', methods.greaterThanOrEqualTo)) { // 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 1e5afa3b2..895f2a8a4 100644 --- a/app/lib/methods/helpers/mergeSubscriptionsRooms.js +++ b/app/lib/methods/helpers/mergeSubscriptionsRooms.js @@ -1,10 +1,10 @@ import EJSON from 'ejson'; -import { lt, coerce } from 'semver'; import normalizeMessage from './normalizeMessage'; import findSubscriptionsRooms from './findSubscriptionsRooms'; import { Encryption } from '../../encryption'; import reduxStore from '../../createStore'; +import { compareServerVersion, methods } from '../../utils'; // TODO: delete and update export const merge = (subscription, room) => { @@ -28,7 +28,7 @@ export const merge = (subscription, room) => { subscription.usernames = room.usernames; subscription.uids = room.uids; } - if (serverVersion && lt(coerce(serverVersion), '3.7.0')) { + if (compareServerVersion(serverVersion, '3.7.0', methods.lowerThan)) { 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 693c5eb78..a6e9137ae 100644 --- a/app/lib/rocketchat.js +++ b/app/lib/rocketchat.js @@ -1,7 +1,4 @@ import { InteractionManager } from 'react-native'; -import lt from 'semver/functions/lt'; -import gte from 'semver/functions/gte'; -import coerce from 'semver/functions/coerce'; import { Rocketchat as RocketchatClient, settings as RocketChatSettings @@ -11,6 +8,7 @@ import AsyncStorage from '@react-native-community/async-storage'; import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; import RNFetchBlob from 'rn-fetch-blob'; +import { compareServerVersion, methods } from './utils'; import reduxStore from './createStore'; import defaultSettings from '../constants/settings'; import database from './database'; @@ -133,7 +131,7 @@ const RocketChat = { message: I18n.t('Not_RC_Server', { contact: I18n.t('Contact_your_server_admin') }) }; } - if (lt(jsonRes.version, MIN_ROCKETCHAT_VERSION)) { + if (compareServerVersion(jsonRes.version, MIN_ROCKETCHAT_VERSION, methods.lowerThan)) { return { success: false, message: I18n.t('Invalid_server_version', { @@ -472,7 +470,7 @@ const RocketChat = { // Force normalized params for 2FA starting RC 3.9.0. const serverVersion = reduxStore.getState().server.version; - if (serverVersion && gte(coerce(serverVersion), '3.9.0')) { + if (compareServerVersion(serverVersion, '3.9.0', methods.greaterThanOrEqualTo)) { const user = params.user ?? params.username; const password = params.password ?? params.ldapPass ?? params.crowdPassword; params = { user, password }; @@ -1364,7 +1362,7 @@ const RocketChat = { }, readThreads(tmid) { const serverVersion = reduxStore.getState().server.version; - if (serverVersion && gte(coerce(serverVersion), '3.4.0')) { + if (compareServerVersion(serverVersion, '3.4.0', methods.greaterThanOrEqualTo)) { // RC 3.4.0 return this.methodCallWrapper('readThreads', tmid); } diff --git a/app/lib/utils.js b/app/lib/utils.js index 617501b2c..769fd6d76 100644 --- a/app/lib/utils.js +++ b/app/lib/utils.js @@ -1,3 +1,7 @@ +import { + lt, lte, gt, gte, coerce +} from 'semver'; + export const formatAttachmentUrl = (attachmentUrl, userId, token, server) => { if (attachmentUrl.startsWith('http')) { if (attachmentUrl.includes('rc_token')) { @@ -7,3 +11,12 @@ export const formatAttachmentUrl = (attachmentUrl, userId, token, server) => { } 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); diff --git a/app/utils/avatar.js b/app/utils/avatar.js index 64f6e951a..c0b4e2db9 100644 --- a/app/utils/avatar.js +++ b/app/utils/avatar.js @@ -1,5 +1,4 @@ -import lt from 'semver/functions/lt'; -import coerce from 'semver/functions/coerce'; +import { compareServerVersion, methods } from '../lib/utils'; const formatUrl = (url, size, query) => `${ url }?format=png&size=${ size }${ query }`; @@ -9,7 +8,7 @@ export const avatarURL = ({ let room; if (type === 'd') { room = text; - } else if (rid && !(serverVersion && lt(coerce(serverVersion), '3.6.0'))) { + } else if (rid && !(compareServerVersion(serverVersion, '3.6.0', methods.lowerThan))) { room = `room/${ rid }`; } else { room = `@${ text }`; diff --git a/app/views/RoomActionsView/index.js b/app/views/RoomActionsView/index.js index 5d3591d6a..41707ee46 100644 --- a/app/views/RoomActionsView/index.js +++ b/app/views/RoomActionsView/index.js @@ -5,8 +5,7 @@ import { } from 'react-native'; import { connect } from 'react-redux'; import isEmpty from 'lodash/isEmpty'; -import lt from 'semver/functions/lt'; -import coerce from 'semver/functions/coerce'; +import { compareServerVersion, methods } from '../../lib/utils'; import Touch from '../../utils/touch'; import { setLoading as setLoadingAction } from '../../actions/selectedUsers'; @@ -256,7 +255,7 @@ class RoomActionsView extends React.Component { const { encrypted } = room; const { serverVersion } = this.props; let hasPermission = false; - if (serverVersion && lt(coerce(serverVersion), '3.11.0')) { + if (compareServerVersion(serverVersion, '3.11.0', methods.lowerThan)) { hasPermission = canEdit; } else { hasPermission = canToggleEncryption; diff --git a/app/views/RoomInfoEditView/index.js b/app/views/RoomInfoEditView/index.js index 04472c82e..7552b093a 100644 --- a/app/views/RoomInfoEditView/index.js +++ b/app/views/RoomInfoEditView/index.js @@ -8,8 +8,7 @@ import { BLOCK_CONTEXT } from '@rocket.chat/ui-kit'; import ImagePicker from 'react-native-image-crop-picker'; import { dequal } from 'dequal'; import isEmpty from 'lodash/isEmpty'; -import lt from 'semver/functions/lt'; -import coerce from 'semver/functions/coerce'; +import { compareServerVersion, methods } from '../../lib/utils'; import database from '../../lib/database'; import { deleteRoom as deleteRoomAction } from '../../actions/room'; @@ -418,11 +417,6 @@ class RoomInfoEditView extends React.Component { this.setState({ encrypted: value }); } - isServerVersionLowerThan = (version) => { - const { serverVersion } = this.props; - return serverVersion && lt(coerce(serverVersion), version); - } - render() { const { name, nameError, description, topic, announcement, t, ro, reactWhenReadOnly, room, joinCode, saving, permissions, archived, enableSysMes, encrypted, avatar @@ -449,7 +443,7 @@ class RoomInfoEditView extends React.Component { - {this.isServerVersionLowerThan('3.6.0') + {compareServerVersion(serverVersion, '3.6.0', methods.lowerThan) ? null : ( @@ -560,7 +554,7 @@ class RoomInfoEditView extends React.Component { ] : null } - {serverVersion && !lt(serverVersion, '3.0.0') ? ( + {!compareServerVersion(serverVersion, '3.0.0', methods.lowerThan) ? (