Improvements inspired by the OStatus code

This commit is contained in:
Stephen Paul Weber 2015-10-28 01:45:51 +00:00
parent fe4c8a771b
commit 6ac8b845bf

View File

@ -10,8 +10,10 @@ require_once __DIR__ . '/lib/util.php';
*/ */
class MentionURLPlugin extends Plugin class MentionURLPlugin extends Plugin
{ {
public function onStartFindMentions($sender, $text, &$mentions) function onEndFindMentions(Profile $sender, $text, &$mentions)
{ {
$matches = array();
preg_match_all('/(?:^|\s+)@([A-Za-z0-9_:\-\.\/%]+)\b/', preg_match_all('/(?:^|\s+)@([A-Za-z0-9_:\-\.\/%]+)\b/',
$text, $text,
$atmatches, $atmatches,
@ -26,15 +28,27 @@ class MentionURLPlugin extends Plugin
} }
if($mentioned instanceof Profile) { if($mentioned instanceof Profile) {
$mentions[] = array('mentioned' => array($mentioned), $matches[$match[1]] = array('mentioned' => array($mentioned),
'type' => 'mention', 'type' => 'mention',
'text' => $text, 'text' => $text,
'position' => $match[1], 'position' => $match[1],
'length' => mb_strlen($match[0]), 'length' => mb_strlen($match[0]),
'url' => $mentioned->profileurl); 'url' => $mentioned->profileurl);
} }
} }
foreach ($mentions as $i => $other) {
// If we share a common prefix with a local user, override it!
$pos = $other['position'];
if (isset($matches[$pos])) {
$mentions[$i] = $matches[$pos];
unset($matches[$pos]);
}
}
foreach ($matches as $mention) {
$mentions[] = $mention;
}
return true; return true;
} }