Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 2 additions & 3 deletions app/lib/methods/enterpriseModules.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import gte from 'semver/functions/gte';
import coerce from 'semver/functions/coerce';
import { isServerVersionGreaterThan } from '../utils';

import reduxStore from '../createStore';
import database from '../database';
Expand Down Expand Up @@ -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 (serverVersion && isServerVersionGreaterThan(serverVersion, '3.1.0')) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gerzonc One more thing here.
We're repeating ourselves on serverVersion && in all calls.
Do you agree that should be inside of the util functions? :)

// RC 3.1.0
const enterpriseModules = await this.methodCallWrapper('license:getModules');
if (enterpriseModules) {
Expand Down
4 changes: 2 additions & 2 deletions app/lib/methods/getCustomEmojis.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { InteractionManager } from 'react-native';
import lt from 'semver/functions/lt';
import orderBy from 'lodash/orderBy';
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';

import { isServerVersionLowerThan } from '../utils';
import reduxStore from '../createStore';
import database from '../database';
import log from '../../utils/log';
Expand Down Expand Up @@ -91,7 +91,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 (serverVersion && isServerVersionLowerThan(serverVersion, '0.75.0')) {
// RC 0.61.0
const result = await this.sdk.get('emoji-custom');

Expand Down
4 changes: 2 additions & 2 deletions app/lib/methods/getPermissions.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { InteractionManager } from 'react-native';
import lt from 'semver/functions/lt';
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
import orderBy from 'lodash/orderBy';

import { isServerVersionLowerThan } from '../utils';
import database from '../database';
import log from '../../utils/log';
import reduxStore from '../createStore';
Expand Down Expand Up @@ -79,7 +79,7 @@ export default function() {
const allRecords = await permissionsCollection.query().fetch();

// if server version is lower than 0.73.0, fetches from old api
if (serverVersion && lt(serverVersion, '0.73.0')) {
if (serverVersion && isServerVersionLowerThan(serverVersion, '0.73.0')) {
// RC 0.66.0
const result = await this.sdk.get('permissions.list');
if (!result.success) {
Expand Down
9 changes: 4 additions & 5 deletions app/lib/methods/getUsersPresence.js
Original file line number Diff line number Diff line change
@@ -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 { isServerVersionLowerThan, isServerVersionGreaterThan } from '../utils';
import reduxStore from '../createStore';
import { setActiveUsers } from '../../actions/activeUsers';
import { setUser } from '../../actions/login';
Expand All @@ -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 (serverVersion && isServerVersionLowerThan(serverVersion, '1.1.0')) {
if (this.activeUsersSubTimeout) {
clearTimeout(this.activeUsersSubTimeout);
this.activeUsersSubTimeout = false;
Expand All @@ -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 (serverVersion && isServerVersionGreaterThan(serverVersion, '1.1.0')) {
let params = {};

// if server is greather than or equal 3.0.0
if (serverVersion && gte(serverVersion, '3.0.0')) {
if (isServerVersionGreaterThan(serverVersion, '3.0.0')) {
// if not have any id
if (!ids.length) {
return;
Expand Down
10 changes: 4 additions & 6 deletions app/lib/rocketchat.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 { isServerVersionGreaterThan, isServerVersionLowerThan } from './utils';
import reduxStore from './createStore';
import defaultSettings from '../constants/settings';
import database from './database';
Expand Down Expand Up @@ -134,7 +132,7 @@ const RocketChat = {
message: I18n.t('Not_RC_Server', { contact: I18n.t('Contact_your_server_admin') })
};
}
if (lt(jsonRes.version, MIN_ROCKETCHAT_VERSION)) {
if (isServerVersionLowerThan(jsonRes.version, MIN_ROCKETCHAT_VERSION)) {
return {
success: false,
message: I18n.t('Invalid_server_version', {
Expand Down Expand Up @@ -467,7 +465,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 (serverVersion && isServerVersionGreaterThan(serverVersion, '3.9.0')) {
const user = params.user ?? params.username;
const password = params.password ?? params.ldapPass ?? params.crowdPassword;
params = { user, password };
Expand Down Expand Up @@ -1374,7 +1372,7 @@ const RocketChat = {
},
readThreads(tmid) {
const serverVersion = reduxStore.getState().server.version;
if (serverVersion && gte(coerce(serverVersion), '3.4.0')) {
if (serverVersion && isServerVersionGreaterThan(serverVersion, '3.4.0')) {
// RC 3.4.0
return this.methodCallWrapper('readThreads', tmid);
}
Expand Down
6 changes: 6 additions & 0 deletions app/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { lt, gte, coerce } from 'semver';

export const formatAttachmentUrl = (attachmentUrl, userId, token, server) => {
if (attachmentUrl.startsWith('http')) {
if (attachmentUrl.includes('rc_token')) {
Expand All @@ -7,3 +9,7 @@ export const formatAttachmentUrl = (attachmentUrl, userId, token, server) => {
}
return encodeURI(`${ server }${ attachmentUrl }?rc_uid=${ userId }&rc_token=${ token }`);
};

export const isServerVersionLowerThan = (currentServerVersion, oldServerVersion) => lt(coerce(currentServerVersion), oldServerVersion);

export const isServerVersionGreaterThan = (currentServerVersion, oldServerVersion) => gte(coerce(currentServerVersion), oldServerVersion);
5 changes: 2 additions & 3 deletions app/utils/avatar.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import lt from 'semver/functions/lt';
import coerce from 'semver/functions/coerce';
import { isServerVersionLowerThan } from '../lib/utils';

const formatUrl = (url, size, query) => `${ url }?format=png&size=${ size }${ query }`;

Expand All @@ -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 && !(serverVersion && isServerVersionLowerThan(serverVersion, '3.6.0'))) {
room = `room/${ rid }`;
} else {
room = `@${ text }`;
Expand Down
5 changes: 2 additions & 3 deletions app/views/RoomActionsView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 { isServerVersionLowerThan } from '../../lib/utils';

import Touch from '../../utils/touch';
import { setLoading as setLoadingAction } from '../../actions/selectedUsers';
Expand Down Expand Up @@ -252,7 +251,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 (serverVersion && isServerVersionLowerThan(serverVersion, '3.11.0')) {
hasPermission = canEdit;
} else {
hasPermission = canToggleEncryption;
Expand Down
7 changes: 3 additions & 4 deletions app/views/RoomInfoEditView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import { BLOCK_CONTEXT } from '@rocket.chat/ui-kit';
import ImagePicker from 'react-native-image-crop-picker';
import isEqual from 'lodash/isEqual';
import isEmpty from 'lodash/isEmpty';
import lt from 'semver/functions/lt';
import coerce from 'semver/functions/coerce';
import { isServerVersionLowerThan } from '../../lib/utils';


import database from '../../lib/database';
Expand Down Expand Up @@ -409,7 +408,7 @@ class RoomInfoEditView extends React.Component {

isServerVersionLowerThan = (version) => {
const { serverVersion } = this.props;
return serverVersion && lt(coerce(serverVersion), version);
return serverVersion && isServerVersionLowerThan(serverVersion, version);
}

render() {
Expand Down Expand Up @@ -549,7 +548,7 @@ class RoomInfoEditView extends React.Component {
]
: null
}
{serverVersion && !lt(serverVersion, '3.0.0') ? (
{serverVersion && !isServerVersionLowerThan(serverVersion, '3.0.0') ? (
<SwitchContainer
value={enableSysMes}
leftLabelPrimary={I18n.t('Hide_System_Messages')}
Expand Down