Skip to content

Commit e65f310

Browse files
Merge pull request #48760 from nextcloud/feat/noid/support-email-mentions
feat(comments): Support mentioning emails
2 parents 91614c2 + 882f916 commit e65f310

File tree

3 files changed

+37
-34
lines changed

3 files changed

+37
-34
lines changed

lib/private/Comments/Comment.php

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -185,24 +185,15 @@ public function setMessage($message, $maxLength = self::MAX_MESSAGE_LENGTH): ICo
185185
* returns an array containing mentions that are included in the comment
186186
*
187187
* @return array each mention provides a 'type' and an 'id', see example below
188+
* @psalm-return list<array{type: 'guest'|'email'|'federated_group'|'group'|'federated_team'|'team'|'federated_user'|'user', id: non-empty-lowercase-string}>
189+
* @since 30.0.2 Type 'email' is supported
190+
* @since 29.0.0 Types 'federated_group', 'federated_team', 'team' and 'federated_user' are supported
191+
* @since 23.0.0 Type 'group' is supported
192+
* @since 17.0.0 Type 'guest' is supported
188193
* @since 11.0.0
189-
*
190-
* The return array looks like:
191-
* [
192-
* [
193-
* 'type' => 'user',
194-
* 'id' => 'citizen4'
195-
* ],
196-
* [
197-
* 'type' => 'group',
198-
* 'id' => 'media'
199-
* ],
200-
* …
201-
* ]
202-
*
203194
*/
204195
public function getMentions(): array {
205-
$ok = preg_match_all("/\B(?<![^a-z0-9_\-@\.\'\s])@(\"guest\/[a-f0-9]+\"|\"(?:federated_)?(?:group|team|user){1}\/[a-z0-9_\-@\.\' \/:]+\"|\"[a-z0-9_\-@\.\' ]+\"|[a-z0-9_\-@\.\']+)/i", $this->getMessage(), $mentions);
196+
$ok = preg_match_all("/\B(?<![^a-z0-9_\-@\.\'\s])@(\"(guest|email)\/[a-f0-9]+\"|\"(?:federated_)?(?:group|team|user){1}\/[a-z0-9_\-@\.\' \/:]+\"|\"[a-z0-9_\-@\.\' ]+\"|[a-z0-9_\-@\.\']+)/i", $this->getMessage(), $mentions);
206197
if (!$ok || !isset($mentions[0])) {
207198
return [];
208199
}
@@ -213,20 +204,35 @@ public function getMentions(): array {
213204
$result = [];
214205
foreach ($mentionIds as $mentionId) {
215206
// Cut-off the @ and remove wrapping double-quotes
207+
/** @var non-empty-lowercase-string $cleanId */
216208
$cleanId = trim(substr($mentionId, 1), '"');
217209

218210
if (str_starts_with($cleanId, 'guest/')) {
219211
$result[] = ['type' => 'guest', 'id' => $cleanId];
212+
} elseif (str_starts_with($cleanId, 'email/')) {
213+
/** @var non-empty-lowercase-string $cleanId */
214+
$cleanId = substr($cleanId, 6);
215+
$result[] = ['type' => 'email', 'id' => $cleanId];
220216
} elseif (str_starts_with($cleanId, 'federated_group/')) {
221-
$result[] = ['type' => 'federated_group', 'id' => substr($cleanId, 16)];
217+
/** @var non-empty-lowercase-string $cleanId */
218+
$cleanId = substr($cleanId, 16);
219+
$result[] = ['type' => 'federated_group', 'id' => $cleanId];
222220
} elseif (str_starts_with($cleanId, 'group/')) {
223-
$result[] = ['type' => 'group', 'id' => substr($cleanId, 6)];
221+
/** @var non-empty-lowercase-string $cleanId */
222+
$cleanId = substr($cleanId, 6);
223+
$result[] = ['type' => 'group', 'id' => $cleanId];
224224
} elseif (str_starts_with($cleanId, 'federated_team/')) {
225-
$result[] = ['type' => 'federated_team', 'id' => substr($cleanId, 15)];
225+
/** @var non-empty-lowercase-string $cleanId */
226+
$cleanId = substr($cleanId, 15);
227+
$result[] = ['type' => 'federated_team', 'id' => $cleanId];
226228
} elseif (str_starts_with($cleanId, 'team/')) {
227-
$result[] = ['type' => 'team', 'id' => substr($cleanId, 5)];
229+
/** @var non-empty-lowercase-string $cleanId */
230+
$cleanId = substr($cleanId, 5);
231+
$result[] = ['type' => 'team', 'id' => $cleanId];
228232
} elseif (str_starts_with($cleanId, 'federated_user/')) {
229-
$result[] = ['type' => 'federated_user', 'id' => substr($cleanId, 15)];
233+
/** @var non-empty-lowercase-string $cleanId */
234+
$cleanId = substr($cleanId, 15);
235+
$result[] = ['type' => 'federated_user', 'id' => $cleanId];
230236
} else {
231237
$result[] = ['type' => 'user', 'id' => $cleanId];
232238
}

lib/public/Comments/IComment.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -124,21 +124,12 @@ public function setMessage($message, $maxLength = self::MAX_MESSAGE_LENGTH);
124124
* returns an array containing mentions that are included in the comment
125125
*
126126
* @return array each mention provides a 'type' and an 'id', see example below
127+
* @psalm-return list<array{type: 'guest'|'email'|'federated_group'|'group'|'federated_team'|'team'|'federated_user'|'user', id: non-empty-lowercase-string}>
128+
* @since 30.0.2 Type 'email' is supported
129+
* @since 29.0.0 Types 'federated_group', 'federated_team', 'team' and 'federated_user' are supported
130+
* @since 23.0.0 Type 'group' is supported
131+
* @since 17.0.0 Type 'guest' is supported
127132
* @since 11.0.0
128-
*
129-
* The return array looks like:
130-
* [
131-
* [
132-
* 'type' => 'user',
133-
* 'id' => 'citizen4'
134-
* ],
135-
* [
136-
* 'type' => 'group',
137-
* 'id' => 'media'
138-
* ],
139-
* …
140-
* ]
141-
*
142133
*/
143134
public function getMentions();
144135

tests/lib/Comments/CommentTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ public function mentionsProvider(): array {
196196
['type' => 'federated_team', 'id' => 'Former Cirle'],
197197
],
198198
],
199+
[
200+
'Emails are supported since 30.0.2 right? @"email/aa23d315de327cfc330f0401ea061005b2b0cdd45ec8346f12664dd1f34cb886"',
201+
[
202+
['type' => 'email', 'id' => 'aa23d315de327cfc330f0401ea061005b2b0cdd45ec8346f12664dd1f34cb886'],
203+
],
204+
],
199205
];
200206
}
201207

0 commit comments

Comments
 (0)