Prepare for group mentions

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2021-10-08 13:23:18 +02:00
parent 1ea5983568
commit 6536ff2cec
No known key found for this signature in database
GPG key ID: 7076EA9751AACDDA

View file

@ -227,21 +227,23 @@ class Comment implements IComment {
*
*/
public function getMentions() {
$ok = preg_match_all("/\B(?<![^a-z0-9_\-@\.\'\s])@(\"guest\/[a-f0-9]+\"|\"[a-z0-9_\-@\.\' ]+\"|[a-z0-9_\-@\.\']+)/i", $this->getMessage(), $mentions);
$ok = preg_match_all("/\B(?<![^a-z0-9_\-@\.\'\s])@(\"guest\/[a-f0-9]+\"|\"group\/[a-z0-9_\-@\.\' ]+\"|\"[a-z0-9_\-@\.\' ]+\"|[a-z0-9_\-@\.\']+)/i", $this->getMessage(), $mentions);
if (!$ok || !isset($mentions[0]) || !is_array($mentions[0])) {
return [];
}
$uids = array_unique($mentions[0]);
usort($uids, static function ($uid1, $uid2) {
return mb_strlen($uid2) <=> mb_strlen($uid1);
$mentionIds = array_unique($mentions[0]);
usort($mentionIds, static function ($mentionId1, $mentionId2) {
return mb_strlen($mentionId2) <=> mb_strlen($mentionId1);
});
$result = [];
foreach ($uids as $uid) {
$cleanUid = trim(substr($uid, 1), '"');
if (strpos($cleanUid, 'guest/') === 0) {
$result[] = ['type' => 'guest', 'id' => $cleanUid];
foreach ($mentionIds as $mentionId) {
$cleanId = trim(substr($mentionId, 1), '"');
if (strpos($cleanId, 'guest/') === 0) {
$result[] = ['type' => 'guest', 'id' => $cleanId];
} elseif (strpos($cleanId, 'group/') === 0) {
$result[] = ['type' => 'group', 'id' => substr($cleanId, 6)];
} else {
$result[] = ['type' => 'user', 'id' => $cleanUid];
$result[] = ['type' => 'user', 'id' => $cleanId];
}
}
return $result;