From 6ac8b845bfbee5f4b121b67e881c663713b7d340 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Wed, 28 Oct 2015 01:45:51 +0000 Subject: [PATCH] Improvements inspired by the OStatus code --- plugins/MentionURL/MentionURLPlugin.php | 28 ++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/plugins/MentionURL/MentionURLPlugin.php b/plugins/MentionURL/MentionURLPlugin.php index 86135d8827..f76aa8b28d 100644 --- a/plugins/MentionURL/MentionURLPlugin.php +++ b/plugins/MentionURL/MentionURLPlugin.php @@ -10,8 +10,10 @@ require_once __DIR__ . '/lib/util.php'; */ 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/', $text, $atmatches, @@ -26,15 +28,27 @@ class MentionURLPlugin extends Plugin } if($mentioned instanceof Profile) { - $mentions[] = array('mentioned' => array($mentioned), - 'type' => 'mention', - 'text' => $text, - 'position' => $match[1], - 'length' => mb_strlen($match[0]), - 'url' => $mentioned->profileurl); + $matches[$match[1]] = array('mentioned' => array($mentioned), + 'type' => 'mention', + 'text' => $text, + 'position' => $match[1], + 'length' => mb_strlen($match[0]), + '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; } -- 2.39.5