Skip to content

Commit b14a5c1

Browse files
author
Marcos Spessatto Defendi
authored
refactor: remove autotranslate internal Meteor methods calls (#34058)
1 parent 8942b00 commit b14a5c1

File tree

7 files changed

+129
-99
lines changed

7 files changed

+129
-99
lines changed

apps/meteor/app/api/server/v1/autotranslate.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import {
44
isAutotranslateTranslateMessageParamsPOST,
55
isAutotranslateGetSupportedLanguagesParamsGET,
66
} from '@rocket.chat/rest-typings';
7-
import { Meteor } from 'meteor/meteor';
87

8+
import { getSupportedLanguages } from '../../../autotranslate/server/functions/getSupportedLanguages';
9+
import { saveAutoTranslateSettings } from '../../../autotranslate/server/functions/saveSettings';
10+
import { translateMessage } from '../../../autotranslate/server/functions/translateMessage';
911
import { settings } from '../../../settings/server';
1012
import { API } from '../api';
1113

@@ -21,7 +23,7 @@ API.v1.addRoute(
2123
return API.v1.failure('AutoTranslate is disabled.');
2224
}
2325
const { targetLanguage } = this.queryParams;
24-
const languages = await Meteor.callAsync('autoTranslate.getSupportedLanguages', targetLanguage);
26+
const languages = await getSupportedLanguages(this.userId, targetLanguage);
2527

2628
return API.v1.success({ languages: languages || [] });
2729
},
@@ -58,8 +60,8 @@ API.v1.addRoute(
5860
return API.v1.failure('The bodyParam "autoTranslateLanguage" must be a string.');
5961
}
6062

61-
await Meteor.callAsync('autoTranslate.saveSettings', roomId, field, value === true ? '1' : String(value).valueOf(), {
62-
defaultLanguage,
63+
await saveAutoTranslateSettings(this.userId, roomId, field, value === true ? '1' : String(value).valueOf(), {
64+
defaultLanguage: defaultLanguage || '',
6365
});
6466

6567
return API.v1.success();
@@ -87,7 +89,7 @@ API.v1.addRoute(
8789
return API.v1.failure('Message not found.');
8890
}
8991

90-
const translatedMessage = await Meteor.callAsync('autoTranslate.translateMessage', message, targetLanguage);
92+
const translatedMessage = await translateMessage(targetLanguage, message);
9193

9294
return API.v1.success({ message: translatedMessage });
9395
},
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Meteor } from 'meteor/meteor';
2+
3+
import { TranslationProviderRegistry } from '..';
4+
import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission';
5+
import { settings } from '../../../settings/server';
6+
7+
export const getSupportedLanguages = async (userId: string, targetLanguage: string) => {
8+
if (!settings.get('AutoTranslate_Enabled')) {
9+
throw new Meteor.Error('error-autotranslate-disabled', 'Auto-Translate is disabled');
10+
}
11+
12+
if (!(await hasPermissionAsync(userId, 'auto-translate'))) {
13+
throw new Meteor.Error('error-action-not-allowed', 'Auto-Translate is not allowed', {
14+
method: 'autoTranslate.getSupportedLanguages',
15+
});
16+
}
17+
18+
return TranslationProviderRegistry.getSupportedLanguages(targetLanguage);
19+
};
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { Subscriptions, Rooms } from '@rocket.chat/models';
2+
import { check } from 'meteor/check';
3+
import { Meteor } from 'meteor/meteor';
4+
5+
import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission';
6+
import { notifyOnSubscriptionChangedById } from '../../../lib/server/lib/notifyListener';
7+
8+
export const saveAutoTranslateSettings = async (
9+
userId: string,
10+
rid: string,
11+
field: string,
12+
value: string,
13+
options: { defaultLanguage: string },
14+
) => {
15+
if (!(await hasPermissionAsync(userId, 'auto-translate'))) {
16+
throw new Meteor.Error('error-action-not-allowed', 'Auto-Translate is not allowed', {
17+
method: 'autoTranslate.saveSettings',
18+
});
19+
}
20+
21+
check(rid, String);
22+
check(field, String);
23+
check(value, String);
24+
25+
if (['autoTranslate', 'autoTranslateLanguage'].indexOf(field) === -1) {
26+
throw new Meteor.Error('error-invalid-settings', 'Invalid settings field', {
27+
method: 'saveAutoTranslateSettings',
28+
});
29+
}
30+
31+
const subscription = await Subscriptions.findOneByRoomIdAndUserId(rid, userId);
32+
if (!subscription) {
33+
throw new Meteor.Error('error-invalid-subscription', 'Invalid subscription', {
34+
method: 'saveAutoTranslateSettings',
35+
});
36+
}
37+
38+
let shouldNotifySubscriptionChanged = false;
39+
40+
switch (field) {
41+
case 'autoTranslate':
42+
const room = await Rooms.findE2ERoomById(rid, { projection: { _id: 1 } });
43+
if (room && value === '1') {
44+
throw new Meteor.Error('error-e2e-enabled', 'Enabling auto-translation in E2E encrypted rooms is not allowed', {
45+
method: 'saveAutoTranslateSettings',
46+
});
47+
}
48+
49+
const updateAutoTranslateResponse = await Subscriptions.updateAutoTranslateById(subscription._id, value === '1');
50+
if (updateAutoTranslateResponse.modifiedCount) {
51+
shouldNotifySubscriptionChanged = true;
52+
}
53+
54+
if (!subscription.autoTranslateLanguage && options.defaultLanguage) {
55+
const updateAutoTranslateLanguageResponse = await Subscriptions.updateAutoTranslateLanguageById(
56+
subscription._id,
57+
options.defaultLanguage,
58+
);
59+
if (updateAutoTranslateLanguageResponse.modifiedCount) {
60+
shouldNotifySubscriptionChanged = true;
61+
}
62+
}
63+
64+
break;
65+
case 'autoTranslateLanguage':
66+
const updateAutoTranslateLanguage = await Subscriptions.updateAutoTranslateLanguageById(subscription._id, value);
67+
if (updateAutoTranslateLanguage.modifiedCount) {
68+
shouldNotifySubscriptionChanged = true;
69+
}
70+
break;
71+
}
72+
73+
if (shouldNotifySubscriptionChanged) {
74+
void notifyOnSubscriptionChangedById(subscription._id);
75+
}
76+
77+
return true;
78+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { IMessage } from '@rocket.chat/core-typings';
2+
import { Rooms } from '@rocket.chat/models';
3+
4+
import { TranslationProviderRegistry } from '..';
5+
6+
export const translateMessage = async (targetLanguage?: string, message?: IMessage) => {
7+
if (!TranslationProviderRegistry.enabled) {
8+
return;
9+
}
10+
if (!message?.rid) {
11+
return;
12+
}
13+
14+
const room = await Rooms.findOneById(message?.rid);
15+
if (message && room) {
16+
await TranslationProviderRegistry.translateMessage(message, room, targetLanguage);
17+
}
18+
};

apps/meteor/app/autotranslate/server/methods/getSupportedLanguages.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ import type { ServerMethods } from '@rocket.chat/ddp-client';
33
import { DDPRateLimiter } from 'meteor/ddp-rate-limiter';
44
import { Meteor } from 'meteor/meteor';
55

6-
import { TranslationProviderRegistry } from '..';
7-
import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission';
8-
import { settings } from '../../../settings/server';
6+
import { getSupportedLanguages } from '../functions/getSupportedLanguages';
97

108
declare module '@rocket.chat/ddp-client' {
119
// eslint-disable-next-line @typescript-eslint/naming-convention
@@ -16,24 +14,15 @@ declare module '@rocket.chat/ddp-client' {
1614

1715
Meteor.methods<ServerMethods>({
1816
async 'autoTranslate.getSupportedLanguages'(targetLanguage) {
19-
if (!settings.get('AutoTranslate_Enabled')) {
20-
throw new Meteor.Error('error-autotranslate-disabled', 'Auto-Translate is disabled');
21-
}
22-
2317
const userId = Meteor.userId();
18+
2419
if (!userId) {
2520
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
2621
method: 'getSupportedLanguages',
2722
});
2823
}
2924

30-
if (!(await hasPermissionAsync(userId, 'auto-translate'))) {
31-
throw new Meteor.Error('error-action-not-allowed', 'Auto-Translate is not allowed', {
32-
method: 'autoTranslate.saveSettings',
33-
});
34-
}
35-
36-
return TranslationProviderRegistry.getSupportedLanguages(targetLanguage);
25+
return getSupportedLanguages(userId, targetLanguage);
3726
},
3827
});
3928

Lines changed: 2 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import type { ServerMethods } from '@rocket.chat/ddp-client';
2-
import { Subscriptions, Rooms } from '@rocket.chat/models';
3-
import { check } from 'meteor/check';
42
import { Meteor } from 'meteor/meteor';
53

6-
import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission';
7-
import { notifyOnSubscriptionChangedById } from '../../../lib/server/lib/notifyListener';
4+
import { saveAutoTranslateSettings } from '../functions/saveSettings';
85

96
declare module '@rocket.chat/ddp-client' {
107
// eslint-disable-next-line @typescript-eslint/naming-convention
@@ -22,68 +19,6 @@ Meteor.methods<ServerMethods>({
2219
});
2320
}
2421

25-
if (!(await hasPermissionAsync(userId, 'auto-translate'))) {
26-
throw new Meteor.Error('error-action-not-allowed', 'Auto-Translate is not allowed', {
27-
method: 'autoTranslate.saveSettings',
28-
});
29-
}
30-
31-
check(rid, String);
32-
check(field, String);
33-
check(value, String);
34-
35-
if (['autoTranslate', 'autoTranslateLanguage'].indexOf(field) === -1) {
36-
throw new Meteor.Error('error-invalid-settings', 'Invalid settings field', {
37-
method: 'saveAutoTranslateSettings',
38-
});
39-
}
40-
41-
const subscription = await Subscriptions.findOneByRoomIdAndUserId(rid, userId);
42-
if (!subscription) {
43-
throw new Meteor.Error('error-invalid-subscription', 'Invalid subscription', {
44-
method: 'saveAutoTranslateSettings',
45-
});
46-
}
47-
48-
let shouldNotifySubscriptionChanged = false;
49-
50-
switch (field) {
51-
case 'autoTranslate':
52-
const room = await Rooms.findE2ERoomById(rid, { projection: { _id: 1 } });
53-
if (room && value === '1') {
54-
throw new Meteor.Error('error-e2e-enabled', 'Enabling auto-translation in E2E encrypted rooms is not allowed', {
55-
method: 'saveAutoTranslateSettings',
56-
});
57-
}
58-
59-
const updateAutoTranslateResponse = await Subscriptions.updateAutoTranslateById(subscription._id, value === '1');
60-
if (updateAutoTranslateResponse.modifiedCount) {
61-
shouldNotifySubscriptionChanged = true;
62-
}
63-
64-
if (!subscription.autoTranslateLanguage && options.defaultLanguage) {
65-
const updateAutoTranslateLanguageResponse = await Subscriptions.updateAutoTranslateLanguageById(
66-
subscription._id,
67-
options.defaultLanguage,
68-
);
69-
if (updateAutoTranslateLanguageResponse.modifiedCount) {
70-
shouldNotifySubscriptionChanged = true;
71-
}
72-
}
73-
74-
break;
75-
case 'autoTranslateLanguage':
76-
const updateAutoTranslateLanguage = await Subscriptions.updateAutoTranslateLanguageById(subscription._id, value);
77-
if (updateAutoTranslateLanguage.modifiedCount) {
78-
shouldNotifySubscriptionChanged = true;
79-
}
80-
break;
81-
}
82-
83-
if (shouldNotifySubscriptionChanged) {
84-
void notifyOnSubscriptionChangedById(subscription._id);
85-
}
86-
87-
return true;
22+
return saveAutoTranslateSettings(userId, rid, field, value, options);
8823
},
8924
});
Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import type { IMessage } from '@rocket.chat/core-typings';
22
import type { ServerMethods } from '@rocket.chat/ddp-client';
3-
import { Rooms } from '@rocket.chat/models';
43
import { Meteor } from 'meteor/meteor';
54

6-
import { TranslationProviderRegistry } from '..';
5+
import { translateMessage } from '../functions/translateMessage';
76

87
declare module '@rocket.chat/ddp-client' {
98
// eslint-disable-next-line @typescript-eslint/naming-convention
@@ -14,16 +13,6 @@ declare module '@rocket.chat/ddp-client' {
1413

1514
Meteor.methods<ServerMethods>({
1615
async 'autoTranslate.translateMessage'(message, targetLanguage) {
17-
if (!TranslationProviderRegistry.enabled) {
18-
return;
19-
}
20-
if (!message?.rid) {
21-
return;
22-
}
23-
24-
const room = await Rooms.findOneById(message?.rid);
25-
if (message && room) {
26-
await TranslationProviderRegistry.translateMessage(message, room, targetLanguage);
27-
}
16+
return translateMessage(targetLanguage, message);
2817
},
2918
});

0 commit comments

Comments
 (0)