fix(Comments): sort by id (not type+id) in getMentions

Updated the getMentions method to inherit documentation and change sorting logic.

Signed-off-by: Josh <josh.t.richards@gmail.com>
This commit is contained in:
Josh 2025-11-03 10:52:07 -05:00 committed by GitHub
parent 2d6ac6ad74
commit 2f2abb09a1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -182,15 +182,7 @@ class Comment implements IComment {
}
/**
* 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
*/
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);
@ -198,9 +190,6 @@ class Comment implements IComment {
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
@ -237,6 +226,10 @@ class Comment implements IComment {
$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']);
});
return $result;
}