Skip to content

Commit 28a5364

Browse files
authored
Merge pull request #14271 from nextcloud/feat/noid/expose-mention-id-frontend
2 parents 8818675 + 6cff311 commit 28a5364

File tree

4 files changed

+101
-12
lines changed

4 files changed

+101
-12
lines changed

src/stores/__tests__/chatExtras.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ describe('chatExtrasStore', () => {
128128
it('should render mentions properly when editing message', () => {
129129
// Arrange
130130
const parameters = {
131-
'mention-call1': { type: 'call', name: 'Conversation101' },
132-
'mention-user1': { type: 'user', name: 'Alice Joel', id: 'alice' },
131+
'mention-call1': { type: 'call', name: 'Conversation101', 'mention-id': 'all' },
132+
'mention-user1': { type: 'user', name: 'Alice Joel', id: 'alice', 'mention-id': 'alice' },
133133
}
134134
// Act
135135
chatExtrasStore.setChatEditInput({
@@ -138,7 +138,7 @@ describe('chatExtrasStore', () => {
138138
parameters
139139
})
140140
// Assert
141-
expect(chatExtrasStore.getChatEditInput('token-1')).toBe('Hello @all and @alice')
141+
expect(chatExtrasStore.getChatEditInput('token-1')).toBe('Hello @"all" and @"alice"')
142142
})
143143

144144
it('should store chat input without escaping special symbols', () => {

src/types/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export type importEmailsParams = Required<operations['room-import-emails-as-part
121121
export type importEmailsResponse = ApiResponse<operations['room-import-emails-as-participants']['responses'][200]['content']['application/json']>
122122

123123
// Chats
124-
export type Mention = RichObject<'server'|'call-type'|'icon-url'>
124+
export type Mention = RichObject<'server'|'call-type'|'icon-url'> & { 'mention-id'?: string }
125125
export type File = RichObject<'size'|'path'|'link'|'mimetype'|'preview-available'> & {
126126
'etag': string,
127127
'permissions': string,

src/utils/__tests__/textParse.spec.js

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,44 @@
55
import { parseMentions, parseSpecialSymbols } from '../textParse.ts'
66

77
jest.mock('@nextcloud/router', () => ({
8-
getBaseUrl: jest.fn().mockReturnValue('server2.com')
8+
getBaseUrl: jest.fn().mockReturnValue('https://server2.com')
99
}))
1010

1111
describe('textParse', () => {
1212
describe('parseMentions', () => {
13+
it('replaces mentions correctly if mention-id is available', () => {
14+
const input = 'test {mention-call1} test {mention-user1} test {mention-group1} test {mention-federated-user1}'
15+
const output = 'test @"all" test @"alice" test @"group/talk" test @"federated_user/alice@server2.com"'
16+
const parameters = {
17+
'mention-call1': {
18+
id: 'room-id',
19+
name: 'Room Display Name',
20+
type: 'call',
21+
'mention-id': 'all',
22+
},
23+
'mention-user1': {
24+
id: 'alice',
25+
name: 'Just Alice',
26+
type: 'user',
27+
'mention-id': 'alice',
28+
},
29+
'mention-group1': {
30+
id: 'talk',
31+
name: 'Talk Group',
32+
type: 'user-group',
33+
'mention-id': 'group/talk',
34+
},
35+
'mention-federated-user1': {
36+
id: 'alice',
37+
name: 'Feder Alice',
38+
type: 'user',
39+
server: 'https://server2.com',
40+
'mention-id': 'federated_user/alice@server2.com',
41+
}
42+
}
43+
expect(parseMentions(input, parameters)).toBe(output)
44+
})
45+
1346
it('replaces {mention-call} correctly', () => {
1447
const input = 'test {mention-call1}'
1548
const output = 'test @all'
@@ -38,7 +71,7 @@ describe('textParse', () => {
3871

3972
it('replaces {mention-user} correctly', () => {
4073
const input = 'test {mention-user1} test {mention-user2}'
41-
const output = 'test @alice test @"alice space@mail.com"'
74+
const output = 'test @"alice" test @"alice space@mail.com"'
4275
const parameters = {
4376
'mention-user1': {
4477
id: 'alice',
@@ -72,15 +105,69 @@ describe('textParse', () => {
72105
expect(parseMentions(input, parameters)).toBe(output)
73106
})
74107

108+
it('replaces {mention-team} correctly', () => {
109+
const input = 'test {mention-team1} test {mention-team2}'
110+
const output = 'test @"team/talk" test @"team/space talk"'
111+
const parameters = {
112+
'mention-team1': {
113+
id: 'talk',
114+
name: 'Talk Group',
115+
type: 'circle',
116+
},
117+
'mention-team2': {
118+
id: 'space talk',
119+
name: 'Out of space Talk Group',
120+
type: 'team',
121+
}
122+
}
123+
expect(parseMentions(input, parameters)).toBe(output)
124+
})
125+
126+
it('replaces {mention-guest} correctly', () => {
127+
const input = 'test {mention-guest1} test {mention-guest2}'
128+
const output = 'test @"guest/abcd" test @"guest/efgh"'
129+
const parameters = {
130+
'mention-guest1': {
131+
id: 'guest/abcd',
132+
name: 'Guest A',
133+
type: 'guest',
134+
},
135+
'mention-guest2': {
136+
id: 'guest/efgh',
137+
name: 'Guest E',
138+
type: 'guest',
139+
}
140+
}
141+
expect(parseMentions(input, parameters)).toBe(output)
142+
})
143+
144+
it('replaces {mention-email} correctly', () => {
145+
const input = 'test {mention-email1} test {mention-email2}'
146+
const output = 'test @"email/abcd" test @"email/efgh"'
147+
const parameters = {
148+
'mention-email1': {
149+
id: 'abcd',
150+
name: 'Email Guest A',
151+
type: 'email',
152+
},
153+
'mention-email2': {
154+
id: 'efgh',
155+
name: 'Email Guest E',
156+
type: 'email',
157+
}
158+
}
159+
expect(parseMentions(input, parameters)).toBe(output)
160+
})
161+
75162
it('replaces {mention-federated-user} correctly (for host and other federations)', () => {
76163
const input = 'test {mention-federated-user1}'
77164
const output = 'test @"federated_user/alice@server3.com"'
78165
const parameters = {
79166
'mention-federated-user1': {
80167
id: 'alice',
81168
name: 'Feder Alice',
82-
type: 'user',
83-
server: 'server3.com'
169+
type: 'federated_user',
170+
server: 'https://server3.com'
84171
}
85172
}
86173
expect(parseMentions(input, parameters)).toBe(output)

src/utils/textParse.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,26 @@ function parseMentions(text: string, parameters: ChatMessage['messageParameters'
1919
const value: Mention = parameters[key] as Mention
2020
let mention = ''
2121

22-
if (key.startsWith('mention-call') && value.type === MENTION.TYPE.CALL) {
22+
if (value['mention-id']) {
23+
mention = `@"${value['mention-id']}"`
24+
} else if (key.startsWith('mention-call') && value.type === MENTION.TYPE.CALL) {
2325
mention = '@all'
2426
} else if (key.startsWith('mention-federated-user')
2527
&& [MENTION.TYPE.USER, MENTION.TYPE.FEDERATED_USER].includes(value.type)) {
26-
const server = (value?.server ?? getBaseUrl()).replace('https://', '')
27-
mention = `@"federated_user/${value.id}@${server}"`
28+
mention = `@"federated_user/${value.id}@${(value?.server ?? getBaseUrl()).replace('https://', '')}"`
2829
} else if (key.startsWith('mention-group')
2930
&& [MENTION.TYPE.USERGROUP, MENTION.TYPE.GROUP].includes(value.type)) {
3031
mention = `@"group/${value.id}"`
3132
} else if (key.startsWith('mention-team')
3233
&& [MENTION.TYPE.CIRCLE, MENTION.TYPE.TEAM].includes(value.type)) {
3334
mention = `@"team/${value.id}"`
3435
} else if (key.startsWith('mention-guest') && value.type === MENTION.TYPE.GUEST) {
36+
// id and mention-id are both prefixed with "guest/"
3537
mention = `@"${value.id}"`
3638
} else if (key.startsWith('mention-email') && value.type === MENTION.TYPE.EMAIL) {
3739
mention = `@"email/${value.id}"`
3840
} else if (key.startsWith('mention-user') && value.type === MENTION.TYPE.USER) {
39-
mention = value.id.includes(' ') ? `@"${value.id}"` : `@${value.id}`
41+
mention = `@"${value.id}"`
4042
}
4143

4244
if (mention) {

0 commit comments

Comments
 (0)