Skip to content

Commit

Permalink
fix(textParse): refactor method and tests
Browse files Browse the repository at this point in the history
- split logic for federated users from host and from own server
- wrap user mentions in double quotes

Signed-off-by: Maksim Sukharev <[email protected]>
  • Loading branch information
Antreesy committed Feb 7, 2025
1 parent 3630322 commit 3ddf020
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 13 deletions.
62 changes: 58 additions & 4 deletions src/utils/__tests__/textParse.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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', () => {
Expand Down Expand Up @@ -38,7 +38,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 +72,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
18 changes: 9 additions & 9 deletions src/utils/textParse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ function parseMentions(text: string, parameters: ChatMessage['messageParameters'

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}"`
} else if (key.startsWith('mention-group')
&& [MENTION.TYPE.USERGROUP, MENTION.TYPE.GROUP].includes(value.type)) {
} else if (key.startsWith('mention-federated-user') && value.type === MENTION.TYPE.FEDERATED_USER) {
mention = `@"federated_user/${value.id}@${value!.server!.replace('https://', '')}"`
} else if (key.startsWith('mention-federated-user') && value.type === MENTION.TYPE.USER) {
// Current user and mention user are both from the same federated server, so parameter is translated.
mention = `@"federated_user/${value.id}@${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)) {
} 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 3ddf020

Please sign in to comment.