Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: convert communication methods to Typescript #25503

Merged
merged 16 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from 12 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
Original file line number Diff line number Diff line change
@@ -1,49 +1,54 @@
import { Meteor } from 'meteor/meteor';
import { SettingValue } from '@rocket.chat/core-typings';

import { Settings } from '../../../models/server/raw';
import { hasPermission } from '../../../authorization/server';
import { twoFactorRequired } from '../../../2fa/server/twoFactorRequired';
import { AppServerOrchestrator } from '../orchestrator';

const waitToLoad = function (orch) {
return new Promise((resolve) => {
const waitToLoad = function (orch: AppServerOrchestrator): unknown {
return new Promise<void>((resolve) => {
let id = setInterval(() => {
if (orch.isEnabled() && orch.isLoaded()) {
clearInterval(id);
id = -1;
id = -1 as unknown as NodeJS.Timeout;
resolve();
}
}, 100);
});
};

const waitToUnload = function (orch) {
return new Promise((resolve) => {
const waitToUnload = function (orch: AppServerOrchestrator): unknown {
return new Promise<void>((resolve) => {
let id = setInterval(() => {
if (!orch.isEnabled() && !orch.isLoaded()) {
clearInterval(id);
id = -1;
id = -1 as unknown as NodeJS.Timeout;
resolve();
}
}, 100);
});
};

export class AppMethods {
constructor(orch) {
_orch: AppServerOrchestrator;
ggazzo marked this conversation as resolved.
Show resolved Hide resolved

constructor(orch: AppServerOrchestrator) {
this._orch = orch;

this._addMethods();
}

isEnabled() {
isEnabled(): SettingValue {
return typeof this._orch !== 'undefined' && this._orch.isEnabled();
}

isLoaded() {
return typeof this._orch !== 'undefined' && this._orch.isEnabled() && this._orch.isLoaded();
isLoaded(): boolean {
return Boolean(typeof this._orch !== 'undefined' && this._orch.isEnabled() && this._orch.isLoaded());
}

_addMethods() {
_addMethods(): void {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const instance = this;

Meteor.methods({
Expand All @@ -56,13 +61,14 @@ export class AppMethods {
},

'apps/go-enable': twoFactorRequired(function _appsGoEnable() {
if (!Meteor.userId()) {
const uid = Meteor.userId();
if (!uid) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'apps/go-enable',
});
}

if (!hasPermission(Meteor.userId(), 'manage-apps')) {
if (!hasPermission(uid, 'manage-apps')) {
throw new Meteor.Error('error-action-not-allowed', 'Not allowed', {
method: 'apps/go-enable',
});
Expand All @@ -74,13 +80,14 @@ export class AppMethods {
}),

'apps/go-disable': twoFactorRequired(function _appsGoDisable() {
if (!Meteor.userId()) {
const uid = Meteor.userId();
if (!uid) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'apps/go-enable',
});
}

if (!hasPermission(Meteor.userId(), 'manage-apps')) {
if (!hasPermission(uid, 'manage-apps')) {
ggazzo marked this conversation as resolved.
Show resolved Hide resolved
throw new Meteor.Error('error-action-not-allowed', 'Not allowed', {
method: 'apps/go-enable',
});
Expand Down
Loading