feat: Update autotranslate on language preference change (#31417)

This commit is contained in:
Gustavo Reis Bauer 2024-01-26 17:27:28 -03:00 committed by GitHub
parent baf5694465
commit e7d3cdeef0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 38 additions and 0 deletions

View File

@ -0,0 +1,6 @@
---
"@rocket.chat/meteor": minor
"@rocket.chat/model-typings": minor
---
Added feature to sync the user's language preference with the autotranslate setting.

View File

@ -5,6 +5,8 @@ import type { ThemePreference } from '@rocket.chat/ui-theming/src/types/themes';
import { Match, check } from 'meteor/check';
import { Meteor } from 'meteor/meteor';
import { settings as rcSettings } from '../../app/settings/server';
type UserPreferences = {
language: string;
newRoomNotification: string;
@ -102,6 +104,7 @@ export const saveUserPreferences = async (settings: Partial<UserPreferences>, us
desktopNotifications: oldDesktopNotifications,
pushNotifications: oldMobileNotifications,
emailNotificationMode: oldEmailNotifications,
language: oldLanguage,
} = user.settings?.preferences || {};
if (user.settings == null) {
@ -169,6 +172,10 @@ export const saveUserPreferences = async (settings: Partial<UserPreferences>, us
if (Array.isArray(settings.highlights)) {
await Subscriptions.updateUserHighlights(user._id, settings.highlights);
}
if (settings.language && oldLanguage !== settings.language && rcSettings.get('AutoTranslate_AutoEnableOnJoinRoom')) {
await Subscriptions.updateAllAutoTranslateLanguagesByUserId(user._id, settings.language);
}
});
};

View File

@ -61,6 +61,7 @@ export class SubscriptionsRaw extends BaseRaw<ISubscription> implements ISubscri
{ key: { prid: 1 } },
{ key: { 'u._id': 1, 'open': 1, 'department': 1 } },
{ key: { rid: 1, ls: 1 } },
{ key: { 'u._id': 1, 'autotranslate': 1 } },
];
}
@ -603,6 +604,21 @@ export class SubscriptionsRaw extends BaseRaw<ISubscription> implements ISubscri
return this.updateOne(query, update);
}
updateAllAutoTranslateLanguagesByUserId(userId: IUser['_id'], language: string): Promise<UpdateResult | Document> {
const query = {
'u._id': userId,
'autoTranslate': true,
};
const update: UpdateFilter<ISubscription> = {
$set: {
autoTranslateLanguage: language,
},
};
return this.updateMany(query, update);
}
disableAutoTranslateByRoomId(roomId: IRoom['_id']): Promise<UpdateResult | Document> {
const query = {
rid: roomId,

View File

@ -470,6 +470,14 @@ describe('AutoTranslate', function () {
expect(sub).to.have.property('autoTranslate');
expect(sub).to.have.property('autoTranslateLanguage').and.to.be.equal('en');
});
it('should change the auto translate language when the user changes his language preference', async () => {
await setLanguagePref('es', credA);
const subscription = await getSub(channel._id, credA);
expect(subscription).to.have.property('autoTranslate', true);
expect(subscription).to.have.property('autoTranslateLanguage', 'es');
});
});
});
});

View File

@ -131,6 +131,7 @@ export interface ISubscriptionsModel extends IBaseModel<ISubscription> {
findByUserId(userId: string, options?: FindOptions<ISubscription>): FindCursor<ISubscription>;
cachedFindByUserId(userId: string, options?: FindOptions<ISubscription>): FindCursor<ISubscription>;
updateAutoTranslateById(_id: string, autoTranslate: boolean): Promise<UpdateResult>;
updateAllAutoTranslateLanguagesByUserId(userId: IUser['_id'], language: string): Promise<UpdateResult | Document>;
disableAutoTranslateByRoomId(roomId: IRoom['_id']): Promise<UpdateResult | Document>;
findAlwaysNotifyDesktopUsersByRoomId(roomId: string): FindCursor<ISubscription>;