Skip to content

Commit

Permalink
feat: Update autotranslate on language preference change (#31417)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustrb authored Jan 26, 2024
1 parent baf5694 commit e7d3cde
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/sixty-bananas-sit.md
Original file line number Diff line number Diff line change
@@ -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.
7 changes: 7 additions & 0 deletions apps/meteor/server/methods/saveUserPreferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
});
};

Expand Down
16 changes: 16 additions & 0 deletions apps/meteor/server/models/raw/Subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } },
];
}

Expand Down Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions apps/meteor/tests/end-to-end/api/00-autotranslate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
});
1 change: 1 addition & 0 deletions packages/model-typings/src/models/ISubscriptionsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>;

Expand Down

0 comments on commit e7d3cde

Please sign in to comment.