Skip to content

Commit

Permalink
fix(textParse): use mention id from message parameters if available
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Sukharev <[email protected]>
  • Loading branch information
Antreesy committed Feb 7, 2025
1 parent 3ddf020 commit 3ec8793
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export type importEmailsParams = Required<operations['room-import-emails-as-part
export type importEmailsResponse = ApiResponse<operations['room-import-emails-as-participants']['responses'][200]['content']['application/json']>

// 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,
Expand Down
33 changes: 33 additions & 0 deletions src/utils/__tests__/textParse.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]"'
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'
Expand Down
5 changes: 4 additions & 1 deletion src/utils/textParse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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://', '')}"`
Expand Down

0 comments on commit 3ec8793

Please sign in to comment.