Skip to content

Commit

Permalink
Replace Meteor.call with call in client/startup
Browse files Browse the repository at this point in the history
  • Loading branch information
tassoevan committed Mar 14, 2023
1 parent 8c3094b commit 98b91f4
Show file tree
Hide file tree
Showing 33 changed files with 269 additions and 167 deletions.
2 changes: 1 addition & 1 deletion apps/meteor/.eslintcache

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { Meteor } from 'meteor/meteor';
import type { IMessage } from '@rocket.chat/core-typings';

import { Rooms } from '../../../models/server';
import { TranslationProviderRegistry } from '..';

declare module '@rocket.chat/ui-contexts' {
// eslint-disable-next-line @typescript-eslint/naming-convention
interface ServerMethods {
'autoTranslate.translateMessage': (message?: IMessage, targetLanguage?: string) => void;
}
}

Meteor.methods({
'autoTranslate.translateMessage'(message, targetLanguage) {
'autoTranslate.translateMessage'(message?: IMessage, targetLanguage?: string) {
if (!TranslationProviderRegistry.enabled) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,41 @@ import { userLogout } from './functions/userLogout';
import { hasPermission } from '../../authorization/server';
import { buildWorkspaceRegistrationData } from './functions/buildRegistrationData';

declare module '@rocket.chat/ui-contexts' {
// eslint-disable-next-line @typescript-eslint/naming-convention
interface ServerMethods {
'cloud:checkRegisterStatus': () => {
connectToCloud: boolean;
workspaceRegistered: boolean;
workspaceId: string;
uniqueId: string;
token: string;
email: string;
};
'cloud:getWorkspaceRegisterData': () => string;
'cloud:registerWorkspace': () => boolean;
'cloud:syncWorkspace': () => boolean;
'cloud:connectWorkspace': (token: string) => boolean | Error;
'cloud:reconnectWorkspace': () => boolean;
'cloud:disconnectWorkspace': () => boolean;
'cloud:getOAuthAuthorizationUrl': () => string;
'cloud:finishOAuthAuthorization': (code: string, state: string) => boolean;
'cloud:checkUserLoggedIn': () => boolean;
'cloud:logout': () => boolean | '';
}
}

Meteor.methods({
'cloud:checkRegisterStatus'() {
if (!Meteor.userId()) {
const uid = Meteor.userId();

if (!uid) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'cloud:checkRegisterStatus',
});
}

if (!hasPermission(Meteor.userId(), 'manage-cloud')) {
if (!hasPermission(uid, 'manage-cloud')) {
throw new Meteor.Error('error-not-authorized', 'Not authorized', {
method: 'cloud:checkRegisterStatus',
});
Expand All @@ -31,28 +57,32 @@ Meteor.methods({
return retrieveRegistrationStatus();
},
async 'cloud:getWorkspaceRegisterData'() {
if (!Meteor.userId()) {
const uid = Meteor.userId();

if (!uid) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'cloud:getWorkspaceRegisterData',
});
}

if (!hasPermission(Meteor.userId(), 'manage-cloud')) {
if (!hasPermission(uid, 'manage-cloud')) {
throw new Meteor.Error('error-not-authorized', 'Not authorized', {
method: 'cloud:getWorkspaceRegisterData',
});
}

return Buffer.from(JSON.stringify(await buildWorkspaceRegistrationData())).toString('base64');
return Buffer.from(JSON.stringify(await buildWorkspaceRegistrationData(undefined))).toString('base64');
},
async 'cloud:registerWorkspace'() {
if (!Meteor.userId()) {
const uid = Meteor.userId();

if (!uid) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'cloud:registerWorkspace',
});
}

if (!hasPermission(Meteor.userId(), 'manage-cloud')) {
if (!hasPermission(uid, 'manage-cloud')) {
throw new Meteor.Error('error-not-authorized', 'Not authorized', {
method: 'cloud:registerWorkspace',
});
Expand All @@ -61,13 +91,15 @@ Meteor.methods({
return startRegisterWorkspace();
},
async 'cloud:syncWorkspace'() {
if (!Meteor.userId()) {
const uid = Meteor.userId();

if (!uid) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'cloud:syncWorkspace',
});
}

if (!hasPermission(Meteor.userId(), 'manage-cloud')) {
if (!hasPermission(uid, 'manage-cloud')) {
throw new Meteor.Error('error-not-authorized', 'Not authorized', {
method: 'cloud:syncWorkspace',
});
Expand All @@ -78,13 +110,15 @@ Meteor.methods({
'cloud:connectWorkspace'(token) {
check(token, String);

if (!Meteor.userId()) {
const uid = Meteor.userId();

if (!uid) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'cloud:connectWorkspace',
});
}

if (!hasPermission(Meteor.userId(), 'manage-cloud')) {
if (!hasPermission(uid, 'manage-cloud')) {
throw new Meteor.Error('error-not-authorized', 'Not authorized', {
method: 'cloud:connectWorkspace',
});
Expand All @@ -99,13 +133,14 @@ Meteor.methods({
return connectWorkspace(token);
},
'cloud:disconnectWorkspace'() {
if (!Meteor.userId()) {
const uid = Meteor.userId();
if (!uid) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'cloud:connectServer',
});
}

if (!hasPermission(Meteor.userId(), 'manage-cloud')) {
if (!hasPermission(uid, 'manage-cloud')) {
throw new Meteor.Error('error-not-authorized', 'Not authorized', {
method: 'cloud:connectServer',
});
Expand All @@ -114,13 +149,14 @@ Meteor.methods({
return disconnectWorkspace();
},
'cloud:reconnectWorkspace'() {
if (!Meteor.userId()) {
const uid = Meteor.userId();
if (!uid) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'cloud:reconnectWorkspace',
});
}

if (!hasPermission(Meteor.userId(), 'manage-cloud')) {
if (!hasPermission(uid, 'manage-cloud')) {
throw new Meteor.Error('error-not-authorized', 'Not authorized', {
method: 'cloud:reconnectWorkspace',
});
Expand All @@ -130,13 +166,14 @@ Meteor.methods({
},
// Currently unused but will link local account to Rocket.Chat Cloud account.
'cloud:getOAuthAuthorizationUrl'() {
if (!Meteor.userId()) {
const uid = Meteor.userId();
if (!uid) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'cloud:getOAuthAuthorizationUrl',
});
}

if (!hasPermission(Meteor.userId(), 'manage-cloud')) {
if (!hasPermission(uid, 'manage-cloud')) {
throw new Meteor.Error('error-not-authorized', 'Not authorized', {
method: 'cloud:getOAuthAuthorizationUrl',
});
Expand All @@ -148,13 +185,15 @@ Meteor.methods({
check(code, String);
check(state, String);

if (!Meteor.userId()) {
const uid = Meteor.userId();

if (!uid) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'cloud:finishOAuthAuthorization',
});
}

if (!hasPermission(Meteor.userId(), 'manage-cloud')) {
if (!hasPermission(uid, 'manage-cloud')) {
throw new Meteor.Error('error-not-authorized', 'Not authorized', {
method: 'cloud:finishOAuthAuthorization',
});
Expand All @@ -163,33 +202,35 @@ Meteor.methods({
return finishOAuthAuthorization(code, state);
},
'cloud:checkUserLoggedIn'() {
if (!Meteor.userId()) {
const uid = Meteor.userId();
if (!uid) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'cloud:checkUserLoggedIn',
});
}

if (!hasPermission(Meteor.userId(), 'manage-cloud')) {
if (!hasPermission(uid, 'manage-cloud')) {
throw new Meteor.Error('error-not-authorized', 'Not authorized', {
method: 'cloud:checkUserLoggedIn',
});
}

return checkUserHasCloudLogin(Meteor.userId());
return checkUserHasCloudLogin(uid);
},
'cloud:logout'() {
if (!Meteor.userId()) {
const uid = Meteor.userId();
if (!uid) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'cloud:logout',
});
}

if (!hasPermission(Meteor.userId(), 'manage-cloud')) {
if (!hasPermission(uid, 'manage-cloud')) {
throw new Meteor.Error('error-not-authorized', 'Not authorized', {
method: 'cloud:logout',
});
}

return userLogout(Meteor.userId());
return userLogout(uid);
},
});

This file was deleted.

16 changes: 16 additions & 0 deletions apps/meteor/app/custom-sounds/server/methods/listCustomSounds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Meteor } from 'meteor/meteor';
import { CustomSounds } from '@rocket.chat/models';
import type { ICustomSound } from '@rocket.chat/core-typings';

declare module '@rocket.chat/ui-contexts' {
// eslint-disable-next-line @typescript-eslint/naming-convention
interface ServerMethods {
listCustomSounds: () => ICustomSound[];
}
}

Meteor.methods({
async listCustomSounds() {
return CustomSounds.find({}).toArray();
},
});
5 changes: 2 additions & 3 deletions apps/meteor/app/lib/server/functions/isTheLastMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ import type { IMessage, IRoom } from '@rocket.chat/core-typings';

import { settings } from '../../../settings/server';

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export const isTheLastMessage = (room: IRoom, message: IMessage) =>
settings.get('Store_Last_Message') && (!room.lastMessage || room.lastMessage._id === message._id);
export const isTheLastMessage = (room: IRoom, message: Pick<IMessage, '_id'>) =>
settings.get<boolean>('Store_Last_Message') && (!room.lastMessage || room.lastMessage._id === message._id);
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { Meteor } from 'meteor/meteor';
import { Authorization } from '@rocket.chat/core-services';
import type { IUser } from '@rocket.chat/core-typings';

declare module '@rocket.chat/ui-contexts' {
// eslint-disable-next-line @typescript-eslint/naming-convention
interface ServerMethods {
getUserRoles: () => Pick<IUser, '_id' | 'username' | 'roles'>[];
}
}

Meteor.methods({
async getUserRoles() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { Meteor } from 'meteor/meteor';
import { Match, check } from 'meteor/check';
import { Settings } from '@rocket.chat/models';
import type { ISetting, ISettingColor } from '@rocket.chat/core-typings';

import { hasPermission, hasAllPermission } from '../../../authorization/server';
import { getSettingPermissionId } from '../../../authorization/lib';
import { twoFactorRequired } from '../../../2fa/server/twoFactorRequired';

declare module '@rocket.chat/ui-contexts' {
// eslint-disable-next-line @typescript-eslint/naming-convention
interface ServerMethods {
saveSetting: (_id: ISetting['_id'], value: ISetting['value'], editor?: ISettingColor['editor']) => boolean;
}
}

Meteor.methods({
saveSetting: twoFactorRequired(async function (_id, value, editor) {
const uid = Meteor.userId();
Expand All @@ -31,6 +39,14 @@ Meteor.methods({

const setting = await Settings.findOneById(_id);

// Verify the setting exists
if (!setting) {
throw new Meteor.Error('error-invalid-setting', 'Invalid setting', {
method: 'saveSetting',
settingId: _id,
});
}

// Verify the value is what it should be
switch (setting.type) {
case 'roomPick':
Expand All @@ -47,7 +63,7 @@ Meteor.methods({
break;
}

await Settings.updateValueAndEditorById(_id, value, editor);
await Settings.updateValueAndEditorById(_id, value as any, editor); // TODO: fix this
return true;
}),
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { Meteor } from 'meteor/meteor';
import type { IMessage } from '@rocket.chat/core-typings';

import logger from './logger';
import { Messages, Subscriptions } from '../../models/server';

declare module '@rocket.chat/ui-contexts' {
// eslint-disable-next-line @typescript-eslint/naming-convention
interface ServerMethods {
unreadMessages: (firstUnreadMessage?: Pick<IMessage, '_id' | 'ts'> | null, room?: string) => void;
}
}

Meteor.methods({
unreadMessages(firstUnreadMessage, room) {
unreadMessages(firstUnreadMessage?: Pick<IMessage, '_id' | 'ts'> | null, room?: string) {
const userId = Meteor.userId();
if (!userId) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
Expand Down
Loading

0 comments on commit 98b91f4

Please sign in to comment.