Skip to content

Commit

Permalink
Merge pull request #48760 from nextcloud/feat/noid/support-email-ment…
Browse files Browse the repository at this point in the history
…ions

feat(comments): Support mentioning emails
  • Loading branch information
nickvergessen authored Oct 18, 2024
2 parents 91614c2 + 882f916 commit e65f310
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 34 deletions.
46 changes: 26 additions & 20 deletions lib/private/Comments/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,24 +185,15 @@ public function setMessage($message, $maxLength = self::MAX_MESSAGE_LENGTH): ICo
* returns an array containing mentions that are included in the comment
*
* @return array each mention provides a 'type' and an 'id', see example below
* @psalm-return list<array{type: 'guest'|'email'|'federated_group'|'group'|'federated_team'|'team'|'federated_user'|'user', id: non-empty-lowercase-string}>
* @since 30.0.2 Type 'email' is supported
* @since 29.0.0 Types 'federated_group', 'federated_team', 'team' and 'federated_user' are supported
* @since 23.0.0 Type 'group' is supported
* @since 17.0.0 Type 'guest' is supported
* @since 11.0.0
*
* The return array looks like:
* [
* [
* 'type' => 'user',
* 'id' => 'citizen4'
* ],
* [
* 'type' => 'group',
* 'id' => 'media'
* ],
* …
* ]
*
*/
public function getMentions(): array {
$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);
$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);
if (!$ok || !isset($mentions[0])) {
return [];
}
Expand All @@ -213,20 +204,35 @@ public function getMentions(): array {
$result = [];
foreach ($mentionIds as $mentionId) {
// Cut-off the @ and remove wrapping double-quotes
/** @var non-empty-lowercase-string $cleanId */
$cleanId = trim(substr($mentionId, 1), '"');

if (str_starts_with($cleanId, 'guest/')) {
$result[] = ['type' => 'guest', 'id' => $cleanId];
} elseif (str_starts_with($cleanId, 'email/')) {
/** @var non-empty-lowercase-string $cleanId */
$cleanId = substr($cleanId, 6);
$result[] = ['type' => 'email', 'id' => $cleanId];
} elseif (str_starts_with($cleanId, 'federated_group/')) {
$result[] = ['type' => 'federated_group', 'id' => substr($cleanId, 16)];
/** @var non-empty-lowercase-string $cleanId */
$cleanId = substr($cleanId, 16);
$result[] = ['type' => 'federated_group', 'id' => $cleanId];
} elseif (str_starts_with($cleanId, 'group/')) {
$result[] = ['type' => 'group', 'id' => substr($cleanId, 6)];
/** @var non-empty-lowercase-string $cleanId */
$cleanId = substr($cleanId, 6);
$result[] = ['type' => 'group', 'id' => $cleanId];
} elseif (str_starts_with($cleanId, 'federated_team/')) {
$result[] = ['type' => 'federated_team', 'id' => substr($cleanId, 15)];
/** @var non-empty-lowercase-string $cleanId */
$cleanId = substr($cleanId, 15);
$result[] = ['type' => 'federated_team', 'id' => $cleanId];
} elseif (str_starts_with($cleanId, 'team/')) {
$result[] = ['type' => 'team', 'id' => substr($cleanId, 5)];
/** @var non-empty-lowercase-string $cleanId */
$cleanId = substr($cleanId, 5);
$result[] = ['type' => 'team', 'id' => $cleanId];
} elseif (str_starts_with($cleanId, 'federated_user/')) {
$result[] = ['type' => 'federated_user', 'id' => substr($cleanId, 15)];
/** @var non-empty-lowercase-string $cleanId */
$cleanId = substr($cleanId, 15);
$result[] = ['type' => 'federated_user', 'id' => $cleanId];
} else {
$result[] = ['type' => 'user', 'id' => $cleanId];
}
Expand Down
19 changes: 5 additions & 14 deletions lib/public/Comments/IComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,21 +124,12 @@ public function setMessage($message, $maxLength = self::MAX_MESSAGE_LENGTH);
* returns an array containing mentions that are included in the comment
*
* @return array each mention provides a 'type' and an 'id', see example below
* @psalm-return list<array{type: 'guest'|'email'|'federated_group'|'group'|'federated_team'|'team'|'federated_user'|'user', id: non-empty-lowercase-string}>
* @since 30.0.2 Type 'email' is supported
* @since 29.0.0 Types 'federated_group', 'federated_team', 'team' and 'federated_user' are supported
* @since 23.0.0 Type 'group' is supported
* @since 17.0.0 Type 'guest' is supported
* @since 11.0.0
*
* The return array looks like:
* [
* [
* 'type' => 'user',
* 'id' => 'citizen4'
* ],
* [
* 'type' => 'group',
* 'id' => 'media'
* ],
* …
* ]
*
*/
public function getMentions();

Expand Down
6 changes: 6 additions & 0 deletions tests/lib/Comments/CommentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ public function mentionsProvider(): array {
['type' => 'federated_team', 'id' => 'Former Cirle'],
],
],
[
'Emails are supported since 30.0.2 right? @"email/aa23d315de327cfc330f0401ea061005b2b0cdd45ec8346f12664dd1f34cb886"',
[
['type' => 'email', 'id' => 'aa23d315de327cfc330f0401ea061005b2b0cdd45ec8346f12664dd1f34cb886'],
],
],
];
}

Expand Down

0 comments on commit e65f310

Please sign in to comment.