From c69e57a3c935fc5b4e2f80b2a92684975787683f Mon Sep 17 00:00:00 2001 From: Maksim Sukharev Date: Fri, 7 Feb 2025 19:49:03 +0100 Subject: [PATCH] fix(textParse): use mention id from message parameters if available Signed-off-by: Maksim Sukharev --- src/stores/__tests__/chatExtras.spec.js | 6 ++--- src/types/index.ts | 2 +- src/utils/__tests__/textParse.spec.js | 33 +++++++++++++++++++++++++ src/utils/textParse.ts | 5 +++- 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/stores/__tests__/chatExtras.spec.js b/src/stores/__tests__/chatExtras.spec.js index 41af7af03fa..c61a6338b15 100644 --- a/src/stores/__tests__/chatExtras.spec.js +++ b/src/stores/__tests__/chatExtras.spec.js @@ -128,8 +128,8 @@ describe('chatExtrasStore', () => { it('should render mentions properly when editing message', () => { // Arrange const parameters = { - 'mention-call1': { type: 'call', name: 'Conversation101' }, - 'mention-user1': { type: 'user', name: 'Alice Joel', id: 'alice' }, + 'mention-call1': { type: 'call', name: 'Conversation101', 'mention-id': 'all' }, + 'mention-user1': { type: 'user', name: 'Alice Joel', id: 'alice', 'mention-id': 'alice' }, } // Act chatExtrasStore.setChatEditInput({ @@ -138,7 +138,7 @@ describe('chatExtrasStore', () => { parameters }) // Assert - expect(chatExtrasStore.getChatEditInput('token-1')).toBe('Hello @all and @alice') + expect(chatExtrasStore.getChatEditInput('token-1')).toBe('Hello @"all" and @"alice"') }) it('should store chat input without escaping special symbols', () => { diff --git a/src/types/index.ts b/src/types/index.ts index ccd704e721c..d9c38e05be8 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -127,7 +127,7 @@ export type importEmailsParams = Required // Chats -export type Mention = RichObject<'server'|'call-type'|'icon-url'> +export type Mention = RichObject<'server'|'call-type'|'icon-url'> & { 'mention-id'?: string } export type File = RichObject<'size'|'path'|'link'|'mimetype'|'preview-available'> & { 'etag': string, 'permissions': string, diff --git a/src/utils/__tests__/textParse.spec.js b/src/utils/__tests__/textParse.spec.js index 5ff87f5c3c3..3e5b967c3c3 100644 --- a/src/utils/__tests__/textParse.spec.js +++ b/src/utils/__tests__/textParse.spec.js @@ -10,6 +10,39 @@ jest.mock('@nextcloud/router', () => ({ describe('textParse', () => { describe('parseMentions', () => { + it('replaces mentions correctly if mention-id is available', () => { + const input = 'test {mention-call1} test {mention-user1} test {mention-group1} test {mention-federated-user1}' + const output = 'test @"all" test @"alice" test @"group/talk" test @"federated_user/alice@server2.com"' + const parameters = { + 'mention-call1': { + id: 'room-id', + name: 'Room Display Name', + type: 'call', + 'mention-id': 'all', + }, + 'mention-user1': { + id: 'alice', + name: 'Just Alice', + type: 'user', + 'mention-id': 'alice', + }, + 'mention-group1': { + id: 'talk', + name: 'Talk Group', + type: 'user-group', + 'mention-id': 'group/talk', + }, + 'mention-federated-user1': { + id: 'alice', + name: 'Feder Alice', + type: 'user', + server: 'https://server2.com', + 'mention-id': 'federated_user/alice@https://server2.com', + } + } + expect(parseMentions(input, parameters)).toBe(output) + }) + it('replaces {mention-call} correctly', () => { const input = 'test {mention-call1}' const output = 'test @all' diff --git a/src/utils/textParse.ts b/src/utils/textParse.ts index aeac102d6c8..1e7d216777c 100644 --- a/src/utils/textParse.ts +++ b/src/utils/textParse.ts @@ -19,7 +19,10 @@ function parseMentions(text: string, parameters: ChatMessage['messageParameters' const value: Mention = parameters[key] as Mention let mention = '' - if (key.startsWith('mention-call') && value.type === MENTION.TYPE.CALL) { + if (value['mention-id']) { + // It is safer to always wrap id in double quotes. HTTPS protocol is default and therefore omitted. + mention = `@"${value['mention-id'].replace('https://', '')}"` + } else if (key.startsWith('mention-call') && value.type === MENTION.TYPE.CALL) { mention = '@all' } else if (key.startsWith('mention-federated-user') && value.type === MENTION.TYPE.FEDERATED_USER) { mention = `@"federated_user/${value.id}@${value!.server!.replace('https://', '')}"`