From: Mikael Nordfeldth Date: Fri, 1 Jan 2016 17:20:42 +0000 (+0100) Subject: Don't require a notice object to common_linkify_mentions X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=10973dcf697214184aae5496673484a99fdf5f42;p=quix0rs-gnu-social.git Don't require a notice object to common_linkify_mentions --- diff --git a/classes/Notice.php b/classes/Notice.php index 43ffa0833f..e7c305b2d9 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -1635,14 +1635,16 @@ class Notice extends Managed_DataObject self::blow('reply:stream:%d', $parentauthor->id); } catch (NoParentNoticeException $e) { // Not a reply, since it has no parent! + $parent = null; } catch (NoResultException $e) { // Parent notice was probably deleted + $parent = null; } // @todo ideally this parser information would only // be calculated once. - $mentions = common_find_mentions($this->content, $this); + $mentions = common_find_mentions($this->content, $sender, $parent); // store replied only for first @ (what user/notice what the reply directed, // we assume first @ is it) @@ -2049,6 +2051,7 @@ class Notice extends Managed_DataObject if (Event::handle('StartActivityObjectFromNotice', array($this, &$object))) { $object->type = $this->object_type ?: ActivityObject::NOTE; $object->id = $this->getUri(); + //FIXME: = $object->title ?: sprintf(... because we might get a title from StartActivityObjectFromNotice $object->title = sprintf('New %1$s by %2$s', ActivityObject::canonicalType($object->type), $this->getProfile()->getNickname()); $object->content = $this->rendered; $object->link = $this->getUrl(); @@ -2769,6 +2772,16 @@ class Notice extends Managed_DataObject return false; } + public function hasParent() + { + try { + $this->getParent(); + } catch (NoParentNoticeException $e) { + return false; + } + return true; + } + public function getParent() { $reply_to_id = null; diff --git a/lib/util.php b/lib/util.php index 7f788fc131..6e01860ee2 100644 --- a/lib/util.php +++ b/lib/util.php @@ -613,7 +613,7 @@ function common_remove_unicode_formatting($text) function common_render_content($text, Notice $notice) { $text = common_render_text($text); - $text = common_linkify_mentions($text, $notice); + $text = common_linkify_mentions($text, $notice->getProfile(), $notice->hasParent() ? $notice->getParent() : null); return $text; } @@ -623,13 +623,14 @@ function common_render_content($text, Notice $notice) * * Should generally not be called except from common_render_content(). * - * @param string $text partially-rendered HTML - * @param Notice $notice in-progress or complete Notice object for context + * @param string $text partially-rendered HTML + * @param Profile $author the Profile that is composing the current notice + * @param Notice $parent the Notice this is sent in reply to, if any * @return string partially-rendered HTML */ -function common_linkify_mentions($text, Notice $notice) +function common_linkify_mentions($text, Profile $author, Notice $parent=null) { - $mentions = common_find_mentions($text, $notice); + $mentions = common_find_mentions($text, $author, $parent); // We need to go through in reverse order by position, // so our positions stay valid despite our fudging with the @@ -687,33 +688,25 @@ function common_linkify_mention(array $mention) * Note the return data format is internal, to be used for building links and * such. Should not be used directly; rather, call common_linkify_mentions(). * - * @param string $text - * @param Notice $notice notice in whose context we're building links + * @param string $text + * @param Profile $sender the Profile that is sending the current text + * @param Notice $parent the Notice this text is in reply to, if any * * @return array * * @access private */ -function common_find_mentions($text, Notice $notice) +function common_find_mentions($text, Profile $sender, Notice $parent=null) { - // The getProfile call throws NoProfileException on failure - $sender = $notice->getProfile(); - $mentions = array(); if (Event::handle('StartFindMentions', array($sender, $text, &$mentions))) { // Get the context of the original notice, if any - $origAuthor = null; - $origNotice = null; $origMentions = array(); - // Is it a reply? - - try { - $origNotice = $notice->getParent(); - $origAuthor = $origNotice->getProfile(); - - $ids = $origNotice->getReplies(); + // Does it have a parent notice for context? + if ($parent instanceof Notice) { + $ids = $parent->getReplies(); // replied-to _profile ids_ foreach ($ids as $id) { try { @@ -723,10 +716,6 @@ function common_find_mentions($text, Notice $notice) // continue foreach } } - } catch (NoParentNoticeException $e) { - // It wasn't a reply to anything, so we can't harvest nickname-relations. - } catch (NoResultException $e) { - // The parent notice was deleted. } $matches = common_find_mentions_raw($text); @@ -743,22 +732,23 @@ function common_find_mentions($text, Notice $notice) // Start with conversation context, then go to // sender context. - if ($origAuthor instanceof Profile && $origAuthor->nickname == $nickname) { - $mentioned = $origAuthor; + if ($parent instanceof Notice && $parent->getProfile()->getNickname() === $nickname) { + $mentioned = $parent->getProfile(); } else if (!empty($origMentions) && array_key_exists($nickname, $origMentions)) { $mentioned = $origMentions[$nickname]; } else { + // sets to null if no match $mentioned = common_relative_profile($sender, $nickname); } if ($mentioned instanceof Profile) { $user = User::getKV('id', $mentioned->id); - if ($user instanceof User) { - $url = common_local_url('userbyid', array('id' => $user->id)); - } else { - $url = $mentioned->profileurl; + try { + $url = $mentioned->getUrl(); + } catch (InvalidUrlException $e) { + $url = common_local_url('userbyid', array('id' => $mentioned->getID())); } $mention = array('mentioned' => array($mentioned), @@ -766,12 +756,9 @@ function common_find_mentions($text, Notice $notice) 'text' => $match[0], 'position' => $match[1], 'length' => mb_strlen($match[0]), + 'title' => $mentioned->getFullname(), 'url' => $url); - if (!empty($mentioned->fullname)) { - $mention['title'] = $mentioned->fullname; - } - $mentions[] = $mention; } } @@ -782,7 +769,7 @@ function common_find_mentions($text, Notice $notice) $text, $hmatches, PREG_OFFSET_CAPTURE); foreach ($hmatches[1] as $hmatch) { $tag = common_canonical_tag($hmatch[0]); - $plist = Profile_list::getByTaggerAndTag($sender->id, $tag); + $plist = Profile_list::getByTaggerAndTag($sender->getID(), $tag); if (!$plist instanceof Profile_list || $plist->private) { continue; }