Skip to content

Commit

Permalink
Merge branch 'develop' into chore/composer-formatters-translation
Browse files Browse the repository at this point in the history
  • Loading branch information
kodiakhq[bot] authored Mar 1, 2023
2 parents 3949cd3 + 8119410 commit 95e5882
Show file tree
Hide file tree
Showing 15 changed files with 212 additions and 84 deletions.
31 changes: 19 additions & 12 deletions apps/meteor/app/autotranslate/client/lib/actionButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
hasTranslationLanguageInAttachments,
hasTranslationLanguageInMessage,
} from '../../../../client/views/room/MessageList/lib/autoTranslate';
import { roomCoordinator } from '../../../../client/lib/rooms/roomCoordinator';

Meteor.startup(() => {
AutoTranslate.init();
Expand All @@ -33,18 +34,20 @@ Meteor.startup(() => {
const action = 'autoTranslateShowInverse' in message ? '$unset' : '$set';
Messages.update({ _id: message._id }, { [action]: { autoTranslateShowInverse: true } });
},
condition({ message, subscription, user }) {
condition({ message, subscription, user, room }) {
if (!user) {
return false;
}
const language = subscription?.autoTranslateLanguage || AutoTranslate.getLanguage(message.rid) || '';
const isLivechatRoom = roomCoordinator.isLivechatRoom(room?.t);
const isDifferentUser = message?.u && message.u._id !== user._id;
const autoTranslateEnabled = subscription?.autoTranslate || isLivechatRoom;
const hasLanguage =
hasTranslationLanguageInMessage(message, language) || hasTranslationLanguageInAttachments(message.attachments, language);

return Boolean(
(message?.u &&
message.u._id !== user._id &&
subscription?.autoTranslate &&
(message as { autoTranslateShowInverse?: boolean }).autoTranslateShowInverse) ||
(!hasTranslationLanguageInMessage(message, language) && !hasTranslationLanguageInAttachments(message.attachments, language)),
(message as { autoTranslateShowInverse?: boolean }).autoTranslateShowInverse ||
(isDifferentUser && autoTranslateEnabled && !hasLanguage),
);
},
order: 90,
Expand All @@ -65,18 +68,22 @@ Meteor.startup(() => {
const action = 'autoTranslateShowInverse' in message ? '$unset' : '$set';
Messages.update({ _id: message._id }, { [action]: { autoTranslateShowInverse: true } });
},
condition({ message, subscription, user }) {
condition({ message, subscription, user, room }) {
const language = subscription?.autoTranslateLanguage || AutoTranslate.getLanguage(message.rid) || '';
const isLivechatRoom = roomCoordinator.isLivechatRoom(room?.t);
if (!user) {
return false;
}
const isDifferentUser = message?.u && message.u._id !== user._id;
const autoTranslateEnabled = subscription?.autoTranslate || isLivechatRoom;
const hasLanguage =
hasTranslationLanguageInMessage(message, language) || hasTranslationLanguageInAttachments(message.attachments, language);

return Boolean(
message?.u &&
message.u._id !== user._id &&
subscription?.autoTranslate &&
!(message as { autoTranslateShowInverse?: boolean }).autoTranslateShowInverse &&
(hasTranslationLanguageInMessage(message, language) || hasTranslationLanguageInAttachments(message.attachments, language)),
!(message as { autoTranslateShowInverse?: boolean }).autoTranslateShowInverse &&
isDifferentUser &&
autoTranslateEnabled &&
hasLanguage,
);
},
order: 90,
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/models/server/models/LivechatRooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class LivechatRooms extends Base {
);
this.tryEnsureIndex({ 'livechatData.$**': 1 });
this.tryEnsureIndex({ pdfTranscriptRequested: 1 }, { sparse: true });
this.tryEnsureIndex({ pdfFileId: 1 }, { sparse: true });
this.tryEnsureIndex({ pdfTranscriptFileId: 1 }, { sparse: true });
}

findOneByIdOrName(_idOrName, options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@
white-space: initial;

opacity: 1;
border: 1px solid var(--rcx-color-shadow-elevation-border, var(--rcx-color-stroke-extra-light, var(--rcx-color-neutral-250, #ebecef)));

border-radius: var(--popover-radius);

background-color: var(--rcx-color-surface-light, white);
box-shadow: 0 0 2px 0 rgba(47, 52, 61, 0.08), 0 0 12px 0 rgba(47, 52, 61, 0.12);

box-shadow: 0 0 1px 0 var(--rcx-color-shadow-elevation-2x, rgba(47, 52, 61, 0.08)), 0 0 12px 0 var(--rcx-color-shadow-elevation-2y, rgba(47, 52, 61, 0.12));

&.show {
display: flex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('RoomMessageContent', () => {
'../../MessageContentBody': () => fakeMessage.msg,
'../../content/DiscussionMetrics': () => null,
'../../content/MessageActions': () => null,
'../../hooks/useNormalizedMessage': { useNormalizedMessage: (args: any) => ({ md: fakeMessage.md, ...args }) },
});

const ProvidersMock = ({ children }: { children: ReactNode }) => {
Expand Down
10 changes: 6 additions & 4 deletions apps/meteor/client/lib/parseMessageTextToAstMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,18 @@ export const parseMessageAttachments = <T extends MessageAttachment>(
);
}

if (isFileAttachment(attachment) && attachment.description) {
attachment.descriptionMd = attachment.descriptionMd ?? textToMessageToken(attachment.description, parseOptions);
}

const text =
(isTranslatedAttachment(attachment) && autoTranslateLanguage && attachment?.translations?.[autoTranslateLanguage]) ||
attachment.text ||
attachment.description ||
'';

if (isFileAttachment(attachment) && attachment.description) {
attachment.descriptionMd = translated
? textToMessageToken(text, parseOptions)
: attachment.descriptionMd ?? textToMessageToken(text, parseOptions);
}

return {
...attachment,
md: translated ? textToMessageToken(text, parseOptions) : attachment.md ?? textToMessageToken(text, parseOptions),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const MonitorsPage = () => {
<VerticalBar.Close onClick={handleCloseVerticalBar} />
</VerticalBar.Header>
<VerticalBar.ScrollableContent>
{context === 'edit' && <EditTriggerPageContainer id={id} onSave={reload.current} />}
{context === 'edit' && <EditTriggerPageContainer key={id} id={id} onSave={reload.current} />}
{context === 'new' && <NewTriggerPage onSave={reload.current} />}
</VerticalBar.ScrollableContent>
</VerticalBar>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@ import { renderHook } from '@testing-library/react-hooks';
import { expect } from 'chai';
import proxyquire from 'proxyquire';

const COMPONENT_PATH = '../../../../../../../client/views/room/MessageList/hooks/useAutoTranslate';
const COMPONENT_PATH = './useAutoTranslate';
const defaultConfig = {
'@rocket.chat/ui-contexts': {
useSetting: () => true,
},
'../../../../../app/autotranslate/client': {
AutoTranslate: {
getLanguage: () => 'lang',
},
},
'../../../../lib/rooms/roomCoordinator': {
roomCoordinator: {
isLivechatRoom: () => false,
},
},
};

describe('room/MessageList/hooks/useAutoTranslate', () => {
it('should return enabled false and undefined language if no subscription and setting disabled', () => {
const { useAutoTranslate } = proxyquire.load(COMPONENT_PATH, {
const { useAutoTranslate } = proxyquire.noCallThru().load(COMPONENT_PATH, {
...defaultConfig,
'@rocket.chat/ui-contexts': {
useSetting: () => false,
Expand All @@ -29,7 +39,7 @@ describe('room/MessageList/hooks/useAutoTranslate', () => {
});

it('should return enabled false and undefined language if no subscription', () => {
const { useAutoTranslate } = proxyquire.load(COMPONENT_PATH, defaultConfig);
const { useAutoTranslate } = proxyquire.noCallThru().load(COMPONENT_PATH, defaultConfig);

const { result } = renderHook(() => useAutoTranslate());

Expand All @@ -42,7 +52,7 @@ describe('room/MessageList/hooks/useAutoTranslate', () => {
});

it('should return enabled true and the auto translate language if has subscription', () => {
const { useAutoTranslate } = proxyquire.load(COMPONENT_PATH, defaultConfig);
const { useAutoTranslate } = proxyquire.noCallThru().load(COMPONENT_PATH, defaultConfig);

const { result } = renderHook(() => useAutoTranslate({ autoTranslate: true, autoTranslateLanguage: 'lang', u: { _id: 1 } }));

Expand All @@ -54,8 +64,33 @@ describe('room/MessageList/hooks/useAutoTranslate', () => {
expect(result.current.showAutoTranslate({ u: { _id: 2 }, translations: { lang: 'translated' } })).to.be.equal(true);
});

it('should return enabled true and the default auto translate language if is livechat room', () => {
const { useAutoTranslate } = proxyquire.noCallThru().load(COMPONENT_PATH, {
...defaultConfig,
'../../../../../app/autotranslate/client': {
AutoTranslate: {
getLanguage: () => 'default',
},
},
'../../../../lib/rooms/roomCoordinator': {
roomCoordinator: {
isLivechatRoom: () => true,
},
},
});

const { result } = renderHook(() => useAutoTranslate({ autoTranslate: false, autoTranslateLanguage: 'default', u: { _id: 1 } }));

expect(result.current.autoTranslateEnabled).to.be.equal(true);
expect(result.current.autoTranslateLanguage).to.be.equal('default');

expect(result.current.showAutoTranslate({ u: { _id: 2 } })).to.be.equal(false);
expect(result.current.showAutoTranslate({ u: { _id: 1 }, translations: { default: 'translated' } })).to.be.equal(false);
expect(result.current.showAutoTranslate({ u: { _id: 2 }, translations: { default: 'translated' } })).to.be.equal(true);
});

it('should return enabled false if no auto translate language', () => {
const { useAutoTranslate } = proxyquire.load(COMPONENT_PATH, defaultConfig);
const { useAutoTranslate } = proxyquire.noCallThru().load(COMPONENT_PATH, defaultConfig);

const { result } = renderHook(() => useAutoTranslate({ autoTranslate: true, autoTranslateLanguage: undefined, u: { _id: 1 } }));

Expand All @@ -68,7 +103,7 @@ describe('room/MessageList/hooks/useAutoTranslate', () => {
});

it('should return enabled false and language undefined if auto translate is false and has auto translate language', () => {
const { useAutoTranslate } = proxyquire.load(COMPONENT_PATH, defaultConfig);
const { useAutoTranslate } = proxyquire.noCallThru().load(COMPONENT_PATH, defaultConfig);

const { result } = renderHook(() => useAutoTranslate({ autoTranslate: false, autoTranslateLanguage: 'lang', u: { _id: 1 } }));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { IMessage, ISubscription, ITranslatedMessage } from '@rocket.chat/c
import { useSetting } from '@rocket.chat/ui-contexts';
import { useCallback, useMemo } from 'react';

import { AutoTranslate } from '../../../../../app/autotranslate/client';
import { roomCoordinator } from '../../../../lib/rooms/roomCoordinator';
import { hasTranslationLanguageInAttachments, hasTranslationLanguageInMessage } from '../lib/autoTranslate';
import { isOwnUserMessage } from '../lib/isOwnUserMessage';

Expand All @@ -13,8 +15,10 @@ export type AutoTranslateOptions = {

export const useAutoTranslate = (subscription?: ISubscription): AutoTranslateOptions => {
const autoTranslateSettingEnabled = Boolean(useSetting('AutoTranslate_Enabled'));
const autoTranslateEnabled = Boolean(autoTranslateSettingEnabled && subscription?.autoTranslateLanguage && subscription?.autoTranslate);
const autoTranslateLanguage = autoTranslateEnabled ? subscription?.autoTranslateLanguage : undefined;
const isSubscriptionEnabled = autoTranslateSettingEnabled && subscription?.autoTranslateLanguage && subscription?.autoTranslate;
const isLivechatRoom = useMemo(() => subscription && roomCoordinator.isLivechatRoom(subscription?.t), [subscription]);
const autoTranslateEnabled = Boolean(isSubscriptionEnabled || isLivechatRoom);
const autoTranslateLanguage = autoTranslateEnabled && subscription ? AutoTranslate.getLanguage(subscription.rid) : undefined;

const showAutoTranslate = useCallback(
(message: IMessage): boolean => {
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/ee/app/license/server/getStatistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ async function getEEStatistics(): Promise<EEOnlyStats | undefined> {

// Number of PDF transcript that succeeded
statsPms.push(
LivechatRooms.find({ pdfFileId: { $exists: true } })
LivechatRooms.find({ pdfTranscriptFileId: { $exists: true } })
.count()
.then((count) => {
statistics.omnichannelPdfTranscriptSucceeded = count;
Expand Down
34 changes: 13 additions & 21 deletions apps/meteor/tests/data/livechat/rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,27 +276,8 @@ export const fetchMessages = (roomId: string, visitorToken: string): Promise<IMe
});
};

// Closes room using methodCall
export const closeRoom = (roomId: string): Promise<boolean> => {
return new Promise((resolve, reject) => {
request
.post(methodCall('livechat:closeRoom'))
.set(credentials)
.send({
message: JSON.stringify({
method: 'livechat:closeRoom',
params: [roomId, faker.lorem.sentence(), { clientAction: true }],
id: 'id',
msg: 'method',
}),
})
.end((err: Error, res: DummyResponse<boolean, 'wrapped'>) => {
if (err) {
return reject(err);
}
resolve(res.body.result);
});
});
export const closeOmnichanelRoom = async (roomId: string): Promise<void> => {
await request.post(api('livechat/room.closeByUser')).set(credentials).send({ rid: roomId }).expect(200)
};

export const bulkCreateLivechatRooms = async (
Expand All @@ -317,3 +298,14 @@ export const bulkCreateLivechatRooms = async (

return rooms;
};

export const startANewLivechatRoomAndTakeIt = async (): Promise<{ room: IOmnichannelRoom; visitor: ILivechatVisitor }> => {
const visitor = await createVisitor();
const room = await createLivechatRoom(visitor.token);
const { _id: roomId } = room;
const inq = await fetchInquiry(roomId);
await takeInquiry(inq._id);
await sendMessage(roomId, 'test message', visitor.token);

return { room, visitor };
};
Loading

0 comments on commit 95e5882

Please sign in to comment.