Chore: Remove settings Fibers usage (#26465)
Co-authored-by: Diego Sampaio <chinello@gmail.com>pull/26133/head^2
parent
d164c06df3
commit
f45b9563f4
@ -1,13 +1,14 @@ |
||||
import { Settings } from '@rocket.chat/models'; |
||||
|
||||
import { retrieveRegistrationStatus } from './retrieveRegistrationStatus'; |
||||
import { Settings } from '../../../models/server'; |
||||
|
||||
export function disconnectWorkspace() { |
||||
export async function disconnectWorkspace() { |
||||
const { connectToCloud } = retrieveRegistrationStatus(); |
||||
if (!connectToCloud) { |
||||
return true; |
||||
} |
||||
|
||||
Settings.updateValueById('Register_Server', false); |
||||
await Settings.updateValueById('Register_Server', false); |
||||
|
||||
return true; |
||||
} |
||||
@ -1,20 +1,20 @@ |
||||
import { Random } from 'meteor/random'; |
||||
import { Settings } from '@rocket.chat/models'; |
||||
|
||||
import { getRedirectUri } from './getRedirectUri'; |
||||
import { Settings } from '../../../models/server'; |
||||
import { settings } from '../../../settings/server'; |
||||
import { userScopes } from '../oauthScopes'; |
||||
|
||||
export function getOAuthAuthorizationUrl() { |
||||
export async function getOAuthAuthorizationUrl() { |
||||
const state = Random.id(); |
||||
|
||||
Settings.updateValueById('Cloud_Workspace_Registration_State', state); |
||||
await Settings.updateValueById('Cloud_Workspace_Registration_State', state); |
||||
|
||||
const cloudUrl = settings.get('Cloud_Url'); |
||||
const client_id = settings.get('Cloud_Workspace_Client_Id'); |
||||
const clientId = settings.get('Cloud_Workspace_Client_Id'); |
||||
const redirectUri = getRedirectUri(); |
||||
|
||||
const scope = userScopes.join(' '); |
||||
|
||||
return `${cloudUrl}/authorize?response_type=code&client_id=${client_id}&redirect_uri=${redirectUri}&scope=${scope}&state=${state}`; |
||||
return `${cloudUrl}/authorize?response_type=code&client_id=${clientId}&redirect_uri=${redirectUri}&scope=${scope}&state=${state}`; |
||||
} |
||||
@ -1,19 +0,0 @@ |
||||
import { retrieveRegistrationStatus } from './retrieveRegistrationStatus'; |
||||
import { Settings } from '../../../models/server'; |
||||
|
||||
export function unregisterWorkspace() { |
||||
const { workspaceRegistered } = retrieveRegistrationStatus(); |
||||
if (!workspaceRegistered) { |
||||
return true; |
||||
} |
||||
|
||||
Settings.updateValueById('Cloud_Workspace_Id', null); |
||||
Settings.updateValueById('Cloud_Workspace_Name', null); |
||||
Settings.updateValueById('Cloud_Workspace_Client_Id', null); |
||||
Settings.updateValueById('Cloud_Workspace_Client_Secret', null); |
||||
Settings.updateValueById('Cloud_Workspace_Client_Secret_Expires_At', null); |
||||
Settings.updateValueById('Cloud_Workspace_PublicKey', null); |
||||
Settings.updateValueById('Cloud_Workspace_Registration_Client_Uri', null); |
||||
|
||||
return true; |
||||
} |
||||
@ -0,0 +1,22 @@ |
||||
import { Settings } from '@rocket.chat/models'; |
||||
|
||||
import { retrieveRegistrationStatus } from './retrieveRegistrationStatus'; |
||||
|
||||
export async function unregisterWorkspace() { |
||||
const { workspaceRegistered } = retrieveRegistrationStatus(); |
||||
if (!workspaceRegistered) { |
||||
return true; |
||||
} |
||||
|
||||
await Promise.all([ |
||||
Settings.updateValueById('Cloud_Workspace_Id', null), |
||||
Settings.updateValueById('Cloud_Workspace_Name', null), |
||||
Settings.updateValueById('Cloud_Workspace_Client_Id', null), |
||||
Settings.updateValueById('Cloud_Workspace_Client_Secret', null), |
||||
Settings.updateValueById('Cloud_Workspace_Client_Secret_Expires_At', null), |
||||
Settings.updateValueById('Cloud_Workspace_PublicKey', null), |
||||
Settings.updateValueById('Cloud_Workspace_Registration_Client_Uri', null), |
||||
]); |
||||
|
||||
return true; |
||||
} |
||||
@ -1,55 +0,0 @@ |
||||
import { Meteor } from 'meteor/meteor'; |
||||
import s from 'underscore.string'; |
||||
|
||||
import { hasPermission } from '../../../authorization'; |
||||
import { Settings } from '../../../models/server'; |
||||
|
||||
Meteor.methods({ |
||||
'livechat:saveIntegration'(values) { |
||||
if (!Meteor.userId() || !hasPermission(Meteor.userId(), 'view-livechat-manager')) { |
||||
throw new Meteor.Error('error-not-allowed', 'Not allowed', { |
||||
method: 'livechat:saveIntegration', |
||||
}); |
||||
} |
||||
|
||||
if (typeof values.Livechat_webhookUrl !== 'undefined') { |
||||
Settings.updateValueById('Livechat_webhookUrl', s.trim(values.Livechat_webhookUrl)); |
||||
} |
||||
|
||||
if (typeof values.Livechat_secret_token !== 'undefined') { |
||||
Settings.updateValueById('Livechat_secret_token', s.trim(values.Livechat_secret_token)); |
||||
} |
||||
|
||||
if (typeof values.Livechat_webhook_on_start !== 'undefined') { |
||||
Settings.updateValueById('Livechat_webhook_on_start', !!values.Livechat_webhook_on_start); |
||||
} |
||||
|
||||
if (typeof values.Livechat_webhook_on_close !== 'undefined') { |
||||
Settings.updateValueById('Livechat_webhook_on_close', !!values.Livechat_webhook_on_close); |
||||
} |
||||
|
||||
if (typeof values.Livechat_webhook_on_chat_taken !== 'undefined') { |
||||
Settings.updateValueById('Livechat_webhook_on_chat_taken', !!values.Livechat_webhook_on_chat_taken); |
||||
} |
||||
|
||||
if (typeof values.Livechat_webhook_on_chat_queued !== 'undefined') { |
||||
Settings.updateValueById('Livechat_webhook_on_chat_queued', !!values.Livechat_webhook_on_chat_queued); |
||||
} |
||||
|
||||
if (typeof values.Livechat_webhook_on_forward !== 'undefined') { |
||||
Settings.updateValueById('Livechat_webhook_on_forward', !!values.Livechat_webhook_on_forward); |
||||
} |
||||
|
||||
if (typeof values.Livechat_webhook_on_offline_msg !== 'undefined') { |
||||
Settings.updateValueById('Livechat_webhook_on_offline_msg', !!values.Livechat_webhook_on_offline_msg); |
||||
} |
||||
|
||||
if (typeof values.Livechat_webhook_on_visitor_message !== 'undefined') { |
||||
Settings.updateValueById('Livechat_webhook_on_visitor_message', !!values.Livechat_webhook_on_visitor_message); |
||||
} |
||||
|
||||
if (typeof values.Livechat_webhook_on_agent_message !== 'undefined') { |
||||
Settings.updateValueById('Livechat_webhook_on_agent_message', !!values.Livechat_webhook_on_agent_message); |
||||
} |
||||
}, |
||||
}); |
||||
@ -0,0 +1,56 @@ |
||||
import { Settings } from '@rocket.chat/models'; |
||||
import { Meteor } from 'meteor/meteor'; |
||||
import s from 'underscore.string'; |
||||
|
||||
import { hasPermission } from '../../../authorization/server'; |
||||
|
||||
Meteor.methods({ |
||||
async 'livechat:saveIntegration'(values) { |
||||
const uid = Meteor.userId(); |
||||
if (!uid || !hasPermission(uid, 'view-livechat-manager')) { |
||||
throw new Meteor.Error('error-not-allowed', 'Not allowed', { |
||||
method: 'livechat:saveIntegration', |
||||
}); |
||||
} |
||||
|
||||
if (typeof values.Livechat_webhookUrl !== 'undefined') { |
||||
await Settings.updateValueById('Livechat_webhookUrl', s.trim(values.Livechat_webhookUrl)); |
||||
} |
||||
|
||||
if (typeof values.Livechat_secret_token !== 'undefined') { |
||||
await Settings.updateValueById('Livechat_secret_token', s.trim(values.Livechat_secret_token)); |
||||
} |
||||
|
||||
if (typeof values.Livechat_webhook_on_start !== 'undefined') { |
||||
await Settings.updateValueById('Livechat_webhook_on_start', !!values.Livechat_webhook_on_start); |
||||
} |
||||
|
||||
if (typeof values.Livechat_webhook_on_close !== 'undefined') { |
||||
await Settings.updateValueById('Livechat_webhook_on_close', !!values.Livechat_webhook_on_close); |
||||
} |
||||
|
||||
if (typeof values.Livechat_webhook_on_chat_taken !== 'undefined') { |
||||
await Settings.updateValueById('Livechat_webhook_on_chat_taken', !!values.Livechat_webhook_on_chat_taken); |
||||
} |
||||
|
||||
if (typeof values.Livechat_webhook_on_chat_queued !== 'undefined') { |
||||
await Settings.updateValueById('Livechat_webhook_on_chat_queued', !!values.Livechat_webhook_on_chat_queued); |
||||
} |
||||
|
||||
if (typeof values.Livechat_webhook_on_forward !== 'undefined') { |
||||
await Settings.updateValueById('Livechat_webhook_on_forward', !!values.Livechat_webhook_on_forward); |
||||
} |
||||
|
||||
if (typeof values.Livechat_webhook_on_offline_msg !== 'undefined') { |
||||
await Settings.updateValueById('Livechat_webhook_on_offline_msg', !!values.Livechat_webhook_on_offline_msg); |
||||
} |
||||
|
||||
if (typeof values.Livechat_webhook_on_visitor_message !== 'undefined') { |
||||
await Settings.updateValueById('Livechat_webhook_on_visitor_message', !!values.Livechat_webhook_on_visitor_message); |
||||
} |
||||
|
||||
if (typeof values.Livechat_webhook_on_agent_message !== 'undefined') { |
||||
await Settings.updateValueById('Livechat_webhook_on_agent_message', !!values.Livechat_webhook_on_agent_message); |
||||
} |
||||
}, |
||||
}); |
||||
@ -1,280 +0,0 @@ |
||||
import { Base } from './_Base'; |
||||
|
||||
export class Settings extends Base { |
||||
constructor(...args) { |
||||
super(...args); |
||||
|
||||
this.tryEnsureIndex({ blocked: 1 }, { sparse: 1 }); |
||||
this.tryEnsureIndex({ hidden: 1 }, { sparse: 1 }); |
||||
} |
||||
|
||||
// FIND
|
||||
findById(_id) { |
||||
const query = { _id }; |
||||
|
||||
return this.find(query); |
||||
} |
||||
|
||||
findOneNotHiddenById(_id) { |
||||
const query = { |
||||
_id, |
||||
hidden: { $ne: true }, |
||||
}; |
||||
|
||||
return this.findOne(query); |
||||
} |
||||
|
||||
findByIds(_id = []) { |
||||
_id = [].concat(_id); |
||||
|
||||
const query = { |
||||
_id: { |
||||
$in: _id, |
||||
}, |
||||
}; |
||||
|
||||
return this.find(query); |
||||
} |
||||
|
||||
findPublic(options) { |
||||
const query = { public: true }; |
||||
|
||||
return this.find(query, options); |
||||
} |
||||
|
||||
findNotHiddenPublic(ids = []) { |
||||
const filter = { |
||||
hidden: { $ne: true }, |
||||
public: true, |
||||
}; |
||||
|
||||
if (ids.length > 0) { |
||||
filter._id = { $in: ids }; |
||||
} |
||||
|
||||
return this.find(filter, { |
||||
fields: { |
||||
_id: 1, |
||||
value: 1, |
||||
editor: 1, |
||||
enterprise: 1, |
||||
invalidValue: 1, |
||||
modules: 1, |
||||
requiredOnWizard: 1, |
||||
}, |
||||
}); |
||||
} |
||||
|
||||
findNotHiddenPublicUpdatedAfter(updatedAt) { |
||||
const filter = { |
||||
hidden: { $ne: true }, |
||||
public: true, |
||||
_updatedAt: { |
||||
$gt: updatedAt, |
||||
}, |
||||
}; |
||||
|
||||
return this.find(filter, { |
||||
fields: { |
||||
_id: 1, |
||||
value: 1, |
||||
editor: 1, |
||||
enterprise: 1, |
||||
invalidValue: 1, |
||||
modules: 1, |
||||
requiredOnWizard: 1, |
||||
}, |
||||
}); |
||||
} |
||||
|
||||
findNotHiddenPrivate() { |
||||
return this.find({ |
||||
hidden: { $ne: true }, |
||||
public: { $ne: true }, |
||||
}); |
||||
} |
||||
|
||||
findNotHidden({ updatedAfter, ...options } = {}) { |
||||
const query = { |
||||
hidden: { $ne: true }, |
||||
}; |
||||
|
||||
if (updatedAfter) { |
||||
query._updatedAt = { $gt: updatedAfter }; |
||||
} |
||||
|
||||
return this.find(query, options); |
||||
} |
||||
|
||||
findNotHiddenUpdatedAfter(updatedAt) { |
||||
return this.find({ |
||||
hidden: { $ne: true }, |
||||
_updatedAt: { |
||||
$gt: updatedAt, |
||||
}, |
||||
}); |
||||
} |
||||
|
||||
findSetupWizardSettings() { |
||||
return this.find({ wizard: { $exists: true, $ne: null } }); |
||||
} |
||||
|
||||
findEnterpriseSettings() { |
||||
return this.find({ enterprise: true }); |
||||
} |
||||
|
||||
// UPDATE
|
||||
updateValueById(_id, value) { |
||||
const query = { |
||||
blocked: { $ne: true }, |
||||
value: { $ne: value }, |
||||
_id, |
||||
}; |
||||
|
||||
const update = { |
||||
$set: { |
||||
value, |
||||
}, |
||||
}; |
||||
|
||||
return this.update(query, update); |
||||
} |
||||
|
||||
incrementValueById(_id, value = 1) { |
||||
const query = { |
||||
blocked: { $ne: true }, |
||||
_id, |
||||
}; |
||||
|
||||
const update = { |
||||
$inc: { |
||||
value, |
||||
}, |
||||
}; |
||||
|
||||
return this.update(query, update); |
||||
} |
||||
|
||||
updateValueAndEditorById(_id, value, editor) { |
||||
const query = { |
||||
blocked: { $ne: true }, |
||||
value: { $ne: value }, |
||||
_id, |
||||
}; |
||||
|
||||
const update = { |
||||
$set: { |
||||
value, |
||||
editor, |
||||
}, |
||||
}; |
||||
|
||||
return this.update(query, update); |
||||
} |
||||
|
||||
updateValueNotHiddenById(_id, value) { |
||||
const query = { |
||||
_id, |
||||
hidden: { $ne: true }, |
||||
blocked: { $ne: true }, |
||||
}; |
||||
|
||||
const update = { |
||||
$set: { |
||||
value, |
||||
}, |
||||
}; |
||||
|
||||
return this.update(query, update); |
||||
} |
||||
|
||||
updateOptionsById(_id, options) { |
||||
const query = { |
||||
blocked: { $ne: true }, |
||||
_id, |
||||
}; |
||||
|
||||
const update = { $set: options }; |
||||
|
||||
return this.update(query, update); |
||||
} |
||||
|
||||
addOptionValueById(_id, option = {}) { |
||||
const query = { |
||||
blocked: { $ne: true }, |
||||
_id, |
||||
}; |
||||
|
||||
const { key, i18nLabel } = option; |
||||
const update = { |
||||
$addToSet: { |
||||
values: { |
||||
key, |
||||
i18nLabel, |
||||
}, |
||||
}, |
||||
}; |
||||
|
||||
return this.update(query, update); |
||||
} |
||||
|
||||
removeOptionValueByIdAndKey(_id, key) { |
||||
const query = { |
||||
blocked: { $ne: true }, |
||||
_id, |
||||
}; |
||||
|
||||
const update = { |
||||
$pull: { |
||||
values: { key }, |
||||
}, |
||||
}; |
||||
|
||||
return this.update(query, update); |
||||
} |
||||
|
||||
// INSERT
|
||||
createWithIdAndValue(_id, value) { |
||||
const record = { |
||||
_id, |
||||
value, |
||||
_createdAt: new Date(), |
||||
}; |
||||
|
||||
return this.insert(record); |
||||
} |
||||
|
||||
// REMOVE
|
||||
removeById(_id) { |
||||
const query = { |
||||
blocked: { $ne: true }, |
||||
_id, |
||||
}; |
||||
|
||||
return this.remove(query); |
||||
} |
||||
|
||||
// RENAME SETTING
|
||||
renameSetting(oldId, newId) { |
||||
const oldSetting = this.findById(oldId).fetch()[0]; |
||||
if (oldSetting) { |
||||
this.removeById(oldSetting._id); |
||||
// there has been some problem with upsert() when changing the complete doc, so decide explicitly for insert or update
|
||||
let newSetting = this.findById(newId).fetch()[0]; |
||||
if (newSetting) { |
||||
this.updateValueById(newId, oldSetting.value); |
||||
} else { |
||||
newSetting = oldSetting; |
||||
newSetting._id = newId; |
||||
delete newSetting.$loki; |
||||
this.insert(newSetting); |
||||
} |
||||
} |
||||
} |
||||
|
||||
insert(record, ...args) { |
||||
return super.insert({ createdAt: new Date(), ...record }, ...args); |
||||
} |
||||
} |
||||
|
||||
export default new Settings('settings', true); |
||||
@ -1,13 +1,23 @@ |
||||
import SettingsModel from '../../models/server/models/Settings'; |
||||
import { Settings } from '@rocket.chat/models'; |
||||
|
||||
import { SettingsRegistry } from './SettingsRegistry'; |
||||
import { initializeSettings } from './startup'; |
||||
import { settings } from './cached'; |
||||
import './applyMiddlewares'; |
||||
import { use } from './Middleware'; |
||||
|
||||
export { SettingsEvents } from './SettingsRegistry'; |
||||
|
||||
export { settings }; |
||||
|
||||
export const settingsRegistry = new SettingsRegistry({ store: settings, model: SettingsModel }); |
||||
export const settingsRegistry = new SettingsRegistry({ store: settings, model: Settings }); |
||||
|
||||
settingsRegistry.add = use(settingsRegistry.add, (context, next) => { |
||||
return Promise.await(next(...context)) as any; |
||||
}); |
||||
|
||||
settingsRegistry.addGroup = use(settingsRegistry.addGroup, (context, next) => { |
||||
return Promise.await(next(...context)) as any; |
||||
}); |
||||
|
||||
initializeSettings({ SettingsModel, settings }); |
||||
Promise.await(initializeSettings({ model: Settings, settings })); |
||||
|
||||
@ -1,31 +0,0 @@ |
||||
import Settings from '../../models/server/models/Settings'; |
||||
|
||||
const cache = new Map(); |
||||
|
||||
export const setValue = (_id, value) => cache.set(_id, value); |
||||
|
||||
const setFromDB = async (_id) => { |
||||
const setting = Settings.findOneById(_id, { fields: { value: 1 } }); |
||||
if (!setting) { |
||||
return; |
||||
} |
||||
|
||||
setValue(_id, setting.value); |
||||
|
||||
return setting.value; |
||||
}; |
||||
|
||||
export const getValue = async (_id) => { |
||||
if (!cache.has(_id)) { |
||||
return setFromDB(_id); |
||||
} |
||||
|
||||
return cache.get(_id); |
||||
}; |
||||
|
||||
export const updateValue = (id, fields) => { |
||||
if (typeof fields.value === 'undefined') { |
||||
return; |
||||
} |
||||
setValue(id, fields.value); |
||||
}; |
||||
@ -0,0 +1,32 @@ |
||||
import { SettingValue } from '@rocket.chat/core-typings'; |
||||
import { Settings } from '@rocket.chat/models'; |
||||
|
||||
const cache = new Map(); |
||||
|
||||
export const setValue = (_id: string, value: SettingValue) => cache.set(_id, value); |
||||
|
||||
const setFromDB = async (_id: string) => { |
||||
const setting = await Settings.findOneById(_id, { projection: { value: 1 } }); |
||||
if (!setting) { |
||||
return; |
||||
} |
||||
|
||||
setValue(_id, setting.value); |
||||
|
||||
return setting.value; |
||||
}; |
||||
|
||||
export const getValue = async (_id: string) => { |
||||
if (!cache.has(_id)) { |
||||
return setFromDB(_id); |
||||
} |
||||
|
||||
return cache.get(_id); |
||||
}; |
||||
|
||||
export const updateValue = <T extends { value: SettingValue }>(id: string, fields: T) => { |
||||
if (typeof fields.value === 'undefined') { |
||||
return; |
||||
} |
||||
setValue(id, fields.value); |
||||
}; |
||||
@ -1,13 +1,13 @@ |
||||
import type { ISetting } from '@rocket.chat/core-typings'; |
||||
import type { Settings } from '@rocket.chat/models'; |
||||
|
||||
import { Settings } from '../../models/server/models/Settings'; |
||||
import { ICachedSettings } from './CachedSettings'; |
||||
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
export function initializeSettings({ SettingsModel, settings }: { SettingsModel: Settings; settings: ICachedSettings }): void { |
||||
SettingsModel.find().forEach((record: ISetting) => { |
||||
export async function initializeSettings({ model, settings }: { model: typeof Settings; settings: ICachedSettings }): Promise<void> { |
||||
await model.find().forEach((record: ISetting) => { |
||||
settings.set(record); |
||||
}); |
||||
|
||||
settings.initilized(); |
||||
settings.initialized(); |
||||
} |
||||
|
||||
@ -1,93 +0,0 @@ |
||||
import { TAPi18n } from 'meteor/rocketchat:tap-i18n'; |
||||
import semver from 'semver'; |
||||
|
||||
import getNewUpdates from './getNewUpdates'; |
||||
import { settings } from '../../../settings/server'; |
||||
import { Info } from '../../../utils'; |
||||
import { Users, Settings } from '../../../models/server'; |
||||
import logger from '../logger'; |
||||
import { sendMessagesToAdmins } from '../../../../server/lib/sendMessagesToAdmins'; |
||||
// import getNewUpdates from '../sampleUpdateData';
|
||||
|
||||
export default () => { |
||||
logger.info('Checking for version updates'); |
||||
|
||||
const { versions, alerts } = getNewUpdates(); |
||||
|
||||
const update = { |
||||
exists: false, |
||||
lastestVersion: null, |
||||
security: false, |
||||
}; |
||||
|
||||
const lastCheckedVersion = settings.get('Update_LatestAvailableVersion'); |
||||
versions.forEach((version) => { |
||||
if (semver.lte(version.version, lastCheckedVersion)) { |
||||
return; |
||||
} |
||||
|
||||
if (semver.lte(version.version, Info.version)) { |
||||
return; |
||||
} |
||||
|
||||
update.exists = true; |
||||
update.lastestVersion = version; |
||||
|
||||
if (version.security === true) { |
||||
update.security = true; |
||||
} |
||||
}); |
||||
|
||||
if (update.exists) { |
||||
Settings.updateValueById('Update_LatestAvailableVersion', update.lastestVersion.version); |
||||
|
||||
Promise.await( |
||||
sendMessagesToAdmins({ |
||||
msgs: ({ adminUser }) => [ |
||||
{ |
||||
msg: `*${TAPi18n.__('Update_your_RocketChat', adminUser.language)}*\n${TAPi18n.__( |
||||
'New_version_available_(s)', |
||||
update.lastestVersion.version, |
||||
adminUser.language, |
||||
)}\n${update.lastestVersion.infoUrl}`,
|
||||
}, |
||||
], |
||||
banners: [ |
||||
{ |
||||
id: `versionUpdate-${update.lastestVersion.version}`.replace(/\./g, '_'), |
||||
priority: 10, |
||||
title: 'Update_your_RocketChat', |
||||
text: 'New_version_available_(s)', |
||||
textArguments: [update.lastestVersion.version], |
||||
link: update.lastestVersion.infoUrl, |
||||
}, |
||||
], |
||||
}), |
||||
); |
||||
} |
||||
|
||||
if (alerts && alerts.length) { |
||||
Promise.await( |
||||
sendMessagesToAdmins({ |
||||
msgs: ({ adminUser }) => |
||||
alerts |
||||
.filter((alert) => !Users.bannerExistsById(adminUser._id, `alert-${alert.id}`)) |
||||
.map((alert) => ({ |
||||
msg: `*${TAPi18n.__('Rocket_Chat_Alert', adminUser.language)}:*\n\n*${TAPi18n.__( |
||||
alert.title, |
||||
adminUser.language, |
||||
)}*\n${TAPi18n.__(alert.text, ...(alert.textArguments || []), adminUser.language)}\n${alert.infoUrl}`,
|
||||
})), |
||||
banners: alerts.map((alert) => ({ |
||||
id: `alert-${alert.id}`.replace(/\./g, '_'), |
||||
priority: 10, |
||||
title: alert.title, |
||||
text: alert.text, |
||||
textArguments: alert.textArguments, |
||||
modifiers: alert.modifiers, |
||||
link: alert.infoUrl, |
||||
})), |
||||
}), |
||||
); |
||||
} |
||||
}; |
||||
@ -0,0 +1,90 @@ |
||||
import { TAPi18n } from 'meteor/rocketchat:tap-i18n'; |
||||
import semver from 'semver'; |
||||
import { Settings } from '@rocket.chat/models'; |
||||
|
||||
import { getNewUpdates } from './getNewUpdates'; |
||||
import { settings } from '../../../settings/server'; |
||||
import { Info } from '../../../utils/server'; |
||||
import { Users } from '../../../models/server'; |
||||
import logger from '../logger'; |
||||
import { sendMessagesToAdmins } from '../../../../server/lib/sendMessagesToAdmins'; |
||||
// import getNewUpdates from '../sampleUpdateData';
|
||||
|
||||
export const checkVersionUpdate = async () => { |
||||
logger.info('Checking for version updates'); |
||||
|
||||
const { versions, alerts } = await getNewUpdates(); |
||||
|
||||
const lastCheckedVersion = settings.get<string>('Update_LatestAvailableVersion'); |
||||
|
||||
for await (const version of versions) { |
||||
if (!lastCheckedVersion) { |
||||
break; |
||||
} |
||||
if (semver.lte(version.version, lastCheckedVersion)) { |
||||
continue; |
||||
} |
||||
|
||||
if (semver.lte(version.version, Info.version)) { |
||||
continue; |
||||
} |
||||
|
||||
await Settings.updateValueById('Update_LatestAvailableVersion', version.version); |
||||
|
||||
await sendMessagesToAdmins({ |
||||
msgs: ({ adminUser }) => [ |
||||
{ |
||||
msg: `*${TAPi18n.__('Update_your_RocketChat', { ...(adminUser.language && { lng: adminUser.language }) })}*\n${TAPi18n.__( |
||||
'New_version_available_(s)', |
||||
{ |
||||
postProcess: 'sprintf', |
||||
sprintf: [version.version], |
||||
}, |
||||
)}\n${version.infoUrl}`,
|
||||
}, |
||||
], |
||||
banners: [ |
||||
{ |
||||
id: `versionUpdate-${version.version}`.replace(/\./g, '_'), |
||||
priority: 10, |
||||
title: 'Update_your_RocketChat', |
||||
text: 'New_version_available_(s)', |
||||
textArguments: [version.version], |
||||
link: version.infoUrl, |
||||
modifiers: [], |
||||
}, |
||||
], |
||||
}); |
||||
break; |
||||
} |
||||
|
||||
if (alerts && alerts.length) { |
||||
await sendMessagesToAdmins({ |
||||
msgs: ({ adminUser }) => |
||||
alerts |
||||
.filter((alert) => !Users.bannerExistsById(adminUser._id, `alert-${alert.id}`)) |
||||
.map((alert) => ({ |
||||
msg: `*${TAPi18n.__('Rocket_Chat_Alert', { ...(adminUser.language && { lng: adminUser.language }) })}:*\n\n*${TAPi18n.__( |
||||
alert.title, |
||||
{ ...(adminUser.language && { lng: adminUser.language }) }, |
||||
)}*\n${TAPi18n.__(alert.text, { |
||||
...(adminUser.language && { lng: adminUser.language }), |
||||
...(Array.isArray(alert.textArguments) && { |
||||
postProcess: 'sprintf', |
||||
sprintf: alert.textArguments, |
||||
}), |
||||
...((!Array.isArray(alert.textArguments) && alert.textArguments) || {}), // bien dormido
|
||||
})}\n${alert.infoUrl}`,
|
||||
})), |
||||
banners: alerts.map((alert) => ({ |
||||
id: `alert-${alert.id}`.replace(/\./g, '_'), |
||||
priority: 10, |
||||
title: alert.title, |
||||
text: alert.text, |
||||
textArguments: alert.textArguments, |
||||
modifiers: alert.modifiers, |
||||
link: alert.infoUrl, |
||||
})), |
||||
}); |
||||
} |
||||
}; |
||||
@ -1,72 +0,0 @@ |
||||
import os from 'os'; |
||||
|
||||
import { HTTP } from 'meteor/http'; |
||||
import { check, Match } from 'meteor/check'; |
||||
|
||||
import { Settings } from '../../../models/server'; |
||||
import { Info } from '../../../utils'; |
||||
import { getWorkspaceAccessToken } from '../../../cloud/server'; |
||||
|
||||
export default () => { |
||||
try { |
||||
const uniqueID = Settings.findOne('uniqueID'); |
||||
|
||||
const params = { |
||||
uniqueId: uniqueID.value, |
||||
installedAt: uniqueID.createdAt.toJSON(), |
||||
version: Info.version, |
||||
osType: os.type(), |
||||
osPlatform: os.platform(), |
||||
osArch: os.arch(), |
||||
osRelease: os.release(), |
||||
nodeVersion: process.version, |
||||
deployMethod: process.env.DEPLOY_METHOD || 'tar', |
||||
deployPlatform: process.env.DEPLOY_PLATFORM || 'selfinstall', |
||||
}; |
||||
|
||||
const headers = {}; |
||||
const token = getWorkspaceAccessToken(); |
||||
if (token) { |
||||
headers.Authorization = `Bearer ${token}`; |
||||
} |
||||
|
||||
const { data } = HTTP.get('https://releases.rocket.chat/updates/check', { |
||||
params, |
||||
headers, |
||||
}); |
||||
|
||||
check( |
||||
data, |
||||
Match.ObjectIncluding({ |
||||
versions: [ |
||||
Match.ObjectIncluding({ |
||||
version: Match.Optional(String), |
||||
security: Match.Optional(Boolean), |
||||
infoUrl: Match.Optional(String), |
||||
}), |
||||
], |
||||
alerts: Match.Optional([ |
||||
Match.ObjectIncluding({ |
||||
id: Match.Optional(String), |
||||
title: Match.Optional(String), |
||||
text: Match.Optional(String), |
||||
textArguments: Match.Optional([Match.Any]), |
||||
modifiers: Match.Optional([String]), |
||||
infoUrl: Match.Optional(String), |
||||
}), |
||||
]), |
||||
}), |
||||
); |
||||
|
||||
return data; |
||||
} catch (error) { |
||||
// There's no need to log this error
|
||||
// as it's pointless and the user
|
||||
// can't do anything about it anyways
|
||||
|
||||
return { |
||||
versions: [], |
||||
alerts: [], |
||||
}; |
||||
} |
||||
}; |
||||
@ -0,0 +1,93 @@ |
||||
import os from 'os'; |
||||
|
||||
import { Settings } from '@rocket.chat/models'; |
||||
import { HTTP } from 'meteor/http'; |
||||
import { check, Match } from 'meteor/check'; |
||||
|
||||
import { Info } from '../../../utils/server'; |
||||
import { getWorkspaceAccessToken } from '../../../cloud/server'; |
||||
|
||||
export const getNewUpdates = async () => { |
||||
try { |
||||
const uniqueID = await Settings.findOne('uniqueID'); |
||||
|
||||
if (!uniqueID) { |
||||
throw new Error('uniqueID not found'); |
||||
} |
||||
|
||||
const params = { |
||||
uniqueId: String(uniqueID.value), |
||||
installedAt: uniqueID.createdAt.toJSON(), |
||||
version: Info.version, |
||||
osType: os.type(), |
||||
osPlatform: os.platform(), |
||||
osArch: os.arch(), |
||||
osRelease: os.release(), |
||||
nodeVersion: process.version, |
||||
deployMethod: process.env.DEPLOY_METHOD || 'tar', |
||||
deployPlatform: process.env.DEPLOY_PLATFORM || 'selfinstall', |
||||
}; |
||||
|
||||
const token = await getWorkspaceAccessToken(); |
||||
const headers = { |
||||
...(token && { Authorization: `Bearer ${token}` }), |
||||
}; |
||||
|
||||
const { data } = HTTP.get('https://releases.rocket.chat/updates/check', { |
||||
params, |
||||
headers, |
||||
}); |
||||
|
||||
check( |
||||
data, |
||||
Match.ObjectIncluding({ |
||||
versions: [ |
||||
Match.ObjectIncluding({ |
||||
version: String, |
||||
security: Match.Optional(Boolean), |
||||
infoUrl: String, |
||||
}), |
||||
], |
||||
alerts: [ |
||||
Match.Optional([ |
||||
Match.ObjectIncluding({ |
||||
id: String, |
||||
title: String, |
||||
text: String, |
||||
textArguments: [Match.Any], |
||||
modifiers: [String], |
||||
infoUrl: String, |
||||
}), |
||||
]), |
||||
], |
||||
}), |
||||
); |
||||
|
||||
return data as { |
||||
versions: { |
||||
version: string; |
||||
security: boolean; |
||||
infoUrl: string; |
||||
}[]; |
||||
|
||||
alerts: { |
||||
id: string; |
||||
priority: number; |
||||
title: string; |
||||
text: string; |
||||
textArguments?: string[]; |
||||
modifiers: string[]; |
||||
infoUrl: string; |
||||
}[]; |
||||
}; |
||||
} catch (error) { |
||||
// There's no need to log this error
|
||||
// as it's pointless and the user
|
||||
// can't do anything about it anyways
|
||||
|
||||
return { |
||||
versions: [], |
||||
alerts: [], |
||||
}; |
||||
} |
||||
}; |
||||
@ -1,11 +1,11 @@ |
||||
import { Settings } from '@rocket.chat/models'; |
||||
import { Meteor } from 'meteor/meteor'; |
||||
|
||||
import { Settings } from '../../app/models/server'; |
||||
import { settings } from '../../app/settings/server'; |
||||
|
||||
Meteor.methods({ |
||||
getSetupWizardParameters() { |
||||
const setupWizardSettings = Settings.findSetupWizardSettings().fetch(); |
||||
async getSetupWizardParameters() { |
||||
const setupWizardSettings = await Settings.findSetupWizardSettings().toArray(); |
||||
const serverAlreadyRegistered = !!settings.get('Cloud_Workspace_Client_Id') || process.env.DEPLOY_PLATFORM === 'rocket-cloud'; |
||||
|
||||
return { |
||||
@ -1,45 +0,0 @@ |
||||
import { expect, spy } from 'chai'; |
||||
import rewire from 'rewire'; |
||||
|
||||
describe('Raw Settings', () => { |
||||
let rawModule; |
||||
const cache = new Map(); |
||||
|
||||
before('rewire deps', () => { |
||||
const spied = spy(async (id) => { |
||||
if (id === '1') { |
||||
return 'some-setting-value'; |
||||
} |
||||
return null; |
||||
}); |
||||
|
||||
rawModule = rewire('../../../../../app/settings/server/raw'); |
||||
rawModule.__set__('setFromDB', spied); |
||||
rawModule.__set__('cache', cache); |
||||
}); |
||||
|
||||
it('should get the value from database when it isnt in cache', async () => { |
||||
const setting = await rawModule.getValue('1'); |
||||
|
||||
expect(setting).to.be.equal('some-setting-value'); |
||||
}); |
||||
|
||||
it('should get the value from cache when its available', async () => { |
||||
cache.set('2', 'supeer-setting'); |
||||
const setting = await rawModule.getValue('2'); |
||||
|
||||
expect(setting).to.be.equal('supeer-setting'); |
||||
}); |
||||
|
||||
it('should update the value in cache', async () => { |
||||
await rawModule.updateValue('2', { value: 'not-super-setting' }); |
||||
|
||||
expect(cache.get('2')).to.be.equal('not-super-setting'); |
||||
}); |
||||
|
||||
it('should not update the setting if the new value is undefined', async () => { |
||||
await rawModule.updateValue('2', {}); |
||||
|
||||
expect(cache.get('2')).to.be.equal('not-super-setting'); |
||||
}); |
||||
}); |
||||
Loading…
Reference in new issue