Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions lib/private/Comments/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,25 +182,14 @@ 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
* @inheritDoc
*/
Comment on lines 184 to 186
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would completely remove that block and add a #[Override] instead

public function getMentions(): array {
$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 [];
}
$mentionIds = array_unique($mentions[0]);
usort($mentionIds, static function ($mentionId1, $mentionId2) {
return mb_strlen($mentionId2) <=> mb_strlen($mentionId1);
});
$result = [];
foreach ($mentionIds as $mentionId) {
// Cut-off the @ and remove wrapping double-quotes
Expand Down Expand Up @@ -237,6 +226,10 @@ public function getMentions(): array {
$result[] = ['type' => 'user', 'id' => $cleanId];
}
}
// Return sorted by id (descending length)
usort($result, static function ($mention1, $mention2) {
return mb_strlen($mention2['id']) <=> mb_strlen($mention1['id']);
});
Comment on lines +230 to +232
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
usort($result, static function ($mention1, $mention2) {
return mb_strlen($mention2['id']) <=> mb_strlen($mention1['id']);
});
usort($result, static fn (array $mention1, array $mention2): int => mb_strlen($mention2['id']) <=> mb_strlen($mention1['id']));

While at it ;)

return $result;
}

Expand Down
10 changes: 8 additions & 2 deletions lib/public/Comments/IComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,19 @@ public function getMessage();
public function setMessage($message, $maxLength = self::MAX_MESSAGE_LENGTH);

/**
* returns an array containing mentions that are included in the comment
* Returns all mentions from the comment message.
*
* @return array each mention provides a 'type' and an 'id', see example below
* Parses the message, returning an array of mentions.
* Mentions are sorted by descending length of their id before being returned.
*
* Supported mention types: 'user', 'group', 'team', 'guest', 'email', 'federated_group', 'federated_team', 'federated_user'.
*
* @return array Each mention is an associative array with 'type' and 'id' keys, sorted by descending length.
* @psalm-return list<array{type: 'guest'|'email'|'federated_group'|'group'|'federated_team'|'team'|'federated_user'|'user', id: non-empty-lowercase-string}>
Comment on lines +131 to 132
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return array Each mention is an associative array with 'type' and 'id' keys, sorted by descending length.
* @psalm-return list<array{type: 'guest'|'email'|'federated_group'|'group'|'federated_team'|'team'|'federated_user'|'user', id: non-empty-lowercase-string}>
* @return list<array{type: 'guest'|'email'|'federated_group'|'group'|'federated_team'|'team'|'federated_user'|'user', id: non-empty-lowercase-string}> Each mention is an associative array with 'type' and 'id' keys, sorted by descending length.

Merge the two @return annotation

* @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 21.0.1 Sort returned results by
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by what?

* @since 17.0.0 Type 'guest' is supported
* @since 11.0.0
*/
Expand Down
Loading