Skip to content

Commit

Permalink
Merge pull request #14469 from nextcloud/backport/14271/stable31
Browse files Browse the repository at this point in the history
[stable31] fix(textParse): use mention id to parse message parameters
  • Loading branch information
nickvergessen authored Feb 21, 2025
2 parents b2f5f85 + 6c08aa0 commit c2a94e6
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/stores/__tests__/chatExtras.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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', () => {
Expand Down
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
95 changes: 91 additions & 4 deletions src/utils/__tests__/textParse.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,44 @@
import { parseMentions, parseSpecialSymbols } from '../textParse.ts'

jest.mock('@nextcloud/router', () => ({
getBaseUrl: jest.fn().mockReturnValue('server2.com')
getBaseUrl: jest.fn().mockReturnValue('https://server2.com')
}))

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/[email protected]',
}
}
expect(parseMentions(input, parameters)).toBe(output)
})

it('replaces {mention-call} correctly', () => {
const input = 'test {mention-call1}'
const output = 'test @all'
Expand Down Expand Up @@ -38,7 +71,7 @@ describe('textParse', () => {

it('replaces {mention-user} correctly', () => {
const input = 'test {mention-user1} test {mention-user2}'
const output = 'test @alice test @"alice [email protected]"'
const output = 'test @"alice" test @"alice [email protected]"'
const parameters = {
'mention-user1': {
id: 'alice',
Expand Down Expand Up @@ -72,15 +105,69 @@ describe('textParse', () => {
expect(parseMentions(input, parameters)).toBe(output)
})

it('replaces {mention-team} correctly', () => {
const input = 'test {mention-team1} test {mention-team2}'
const output = 'test @"team/talk" test @"team/space talk"'
const parameters = {
'mention-team1': {
id: 'talk',
name: 'Talk Group',
type: 'circle',
},
'mention-team2': {
id: 'space talk',
name: 'Out of space Talk Group',
type: 'team',
}
}
expect(parseMentions(input, parameters)).toBe(output)
})

it('replaces {mention-guest} correctly', () => {
const input = 'test {mention-guest1} test {mention-guest2}'
const output = 'test @"guest/abcd" test @"guest/efgh"'
const parameters = {
'mention-guest1': {
id: 'guest/abcd',
name: 'Guest A',
type: 'guest',
},
'mention-guest2': {
id: 'guest/efgh',
name: 'Guest E',
type: 'guest',
}
}
expect(parseMentions(input, parameters)).toBe(output)
})

it('replaces {mention-email} correctly', () => {
const input = 'test {mention-email1} test {mention-email2}'
const output = 'test @"email/abcd" test @"email/efgh"'
const parameters = {
'mention-email1': {
id: 'abcd',
name: 'Email Guest A',
type: 'email',
},
'mention-email2': {
id: 'efgh',
name: 'Email Guest E',
type: 'email',
}
}
expect(parseMentions(input, parameters)).toBe(output)
})

it('replaces {mention-federated-user} correctly (for host and other federations)', () => {
const input = 'test {mention-federated-user1}'
const output = 'test @"federated_user/[email protected]"'
const parameters = {
'mention-federated-user1': {
id: 'alice',
name: 'Feder Alice',
type: 'user',
server: 'server3.com'
type: 'federated_user',
server: 'https://server3.com'
}
}
expect(parseMentions(input, parameters)).toBe(output)
Expand Down
10 changes: 6 additions & 4 deletions src/utils/textParse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,26 @@ 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']) {
mention = `@"${value['mention-id']}"`
} else if (key.startsWith('mention-call') && value.type === MENTION.TYPE.CALL) {
mention = '@all'
} else if (key.startsWith('mention-federated-user')
&& [MENTION.TYPE.USER, MENTION.TYPE.FEDERATED_USER].includes(value.type)) {
const server = (value?.server ?? getBaseUrl()).replace('https://', '')
mention = `@"federated_user/${value.id}@${server}"`
mention = `@"federated_user/${value.id}@${(value?.server ?? getBaseUrl()).replace('https://', '')}"`
} else if (key.startsWith('mention-group')
&& [MENTION.TYPE.USERGROUP, MENTION.TYPE.GROUP].includes(value.type)) {
mention = `@"group/${value.id}"`
} else if (key.startsWith('mention-team')
&& [MENTION.TYPE.CIRCLE, MENTION.TYPE.TEAM].includes(value.type)) {
mention = `@"team/${value.id}"`
} else if (key.startsWith('mention-guest') && value.type === MENTION.TYPE.GUEST) {
// id and mention-id are both prefixed with "guest/"
mention = `@"${value.id}"`
} else if (key.startsWith('mention-email') && value.type === MENTION.TYPE.EMAIL) {
mention = `@"email/${value.id}"`
} else if (key.startsWith('mention-user') && value.type === MENTION.TYPE.USER) {
mention = value.id.includes(' ') ? `@"${value.id}"` : `@${value.id}`
mention = `@"${value.id}"`
}

if (mention) {
Expand Down

0 comments on commit c2a94e6

Please sign in to comment.