Don't require a notice object to common_linkify_mentions

This commit is contained in:
Mikael Nordfeldth 2016-01-01 18:20:42 +01:00
parent 86106b890a
commit 10973dcf69
2 changed files with 36 additions and 36 deletions

View File

@ -1635,14 +1635,16 @@ class Notice extends Managed_DataObject
self::blow('reply:stream:%d', $parentauthor->id); self::blow('reply:stream:%d', $parentauthor->id);
} catch (NoParentNoticeException $e) { } catch (NoParentNoticeException $e) {
// Not a reply, since it has no parent! // Not a reply, since it has no parent!
$parent = null;
} catch (NoResultException $e) { } catch (NoResultException $e) {
// Parent notice was probably deleted // Parent notice was probably deleted
$parent = null;
} }
// @todo ideally this parser information would only // @todo ideally this parser information would only
// be calculated once. // 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, // store replied only for first @ (what user/notice what the reply directed,
// we assume first @ is it) // we assume first @ is it)
@ -2049,6 +2051,7 @@ class Notice extends Managed_DataObject
if (Event::handle('StartActivityObjectFromNotice', array($this, &$object))) { if (Event::handle('StartActivityObjectFromNotice', array($this, &$object))) {
$object->type = $this->object_type ?: ActivityObject::NOTE; $object->type = $this->object_type ?: ActivityObject::NOTE;
$object->id = $this->getUri(); $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->title = sprintf('New %1$s by %2$s', ActivityObject::canonicalType($object->type), $this->getProfile()->getNickname());
$object->content = $this->rendered; $object->content = $this->rendered;
$object->link = $this->getUrl(); $object->link = $this->getUrl();
@ -2769,6 +2772,16 @@ class Notice extends Managed_DataObject
return false; return false;
} }
public function hasParent()
{
try {
$this->getParent();
} catch (NoParentNoticeException $e) {
return false;
}
return true;
}
public function getParent() public function getParent()
{ {
$reply_to_id = null; $reply_to_id = null;

View File

@ -613,7 +613,7 @@ function common_remove_unicode_formatting($text)
function common_render_content($text, Notice $notice) function common_render_content($text, Notice $notice)
{ {
$text = common_render_text($text); $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; return $text;
} }
@ -623,13 +623,14 @@ function common_render_content($text, Notice $notice)
* *
* Should generally not be called except from common_render_content(). * Should generally not be called except from common_render_content().
* *
* @param string $text partially-rendered HTML * @param string $text partially-rendered HTML
* @param Notice $notice in-progress or complete Notice object for context * @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 * @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, // We need to go through in reverse order by position,
// so our positions stay valid despite our fudging with the // 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 * 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(). * such. Should not be used directly; rather, call common_linkify_mentions().
* *
* @param string $text * @param string $text
* @param Notice $notice notice in whose context we're building links * @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 * @return array
* *
* @access private * @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(); $mentions = array();
if (Event::handle('StartFindMentions', array($sender, $text, &$mentions))) { if (Event::handle('StartFindMentions', array($sender, $text, &$mentions))) {
// Get the context of the original notice, if any // Get the context of the original notice, if any
$origAuthor = null;
$origNotice = null;
$origMentions = array(); $origMentions = array();
// Is it a reply? // Does it have a parent notice for context?
if ($parent instanceof Notice) {
try { $ids = $parent->getReplies(); // replied-to _profile ids_
$origNotice = $notice->getParent();
$origAuthor = $origNotice->getProfile();
$ids = $origNotice->getReplies();
foreach ($ids as $id) { foreach ($ids as $id) {
try { try {
@ -723,10 +716,6 @@ function common_find_mentions($text, Notice $notice)
// continue foreach // 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); $matches = common_find_mentions_raw($text);
@ -743,22 +732,23 @@ function common_find_mentions($text, Notice $notice)
// Start with conversation context, then go to // Start with conversation context, then go to
// sender context. // sender context.
if ($origAuthor instanceof Profile && $origAuthor->nickname == $nickname) { if ($parent instanceof Notice && $parent->getProfile()->getNickname() === $nickname) {
$mentioned = $origAuthor; $mentioned = $parent->getProfile();
} else if (!empty($origMentions) && } else if (!empty($origMentions) &&
array_key_exists($nickname, $origMentions)) { array_key_exists($nickname, $origMentions)) {
$mentioned = $origMentions[$nickname]; $mentioned = $origMentions[$nickname];
} else { } else {
// sets to null if no match
$mentioned = common_relative_profile($sender, $nickname); $mentioned = common_relative_profile($sender, $nickname);
} }
if ($mentioned instanceof Profile) { if ($mentioned instanceof Profile) {
$user = User::getKV('id', $mentioned->id); $user = User::getKV('id', $mentioned->id);
if ($user instanceof User) { try {
$url = common_local_url('userbyid', array('id' => $user->id)); $url = $mentioned->getUrl();
} else { } catch (InvalidUrlException $e) {
$url = $mentioned->profileurl; $url = common_local_url('userbyid', array('id' => $mentioned->getID()));
} }
$mention = array('mentioned' => array($mentioned), $mention = array('mentioned' => array($mentioned),
@ -766,12 +756,9 @@ function common_find_mentions($text, Notice $notice)
'text' => $match[0], 'text' => $match[0],
'position' => $match[1], 'position' => $match[1],
'length' => mb_strlen($match[0]), 'length' => mb_strlen($match[0]),
'title' => $mentioned->getFullname(),
'url' => $url); 'url' => $url);
if (!empty($mentioned->fullname)) {
$mention['title'] = $mentioned->fullname;
}
$mentions[] = $mention; $mentions[] = $mention;
} }
} }
@ -782,7 +769,7 @@ function common_find_mentions($text, Notice $notice)
$text, $hmatches, PREG_OFFSET_CAPTURE); $text, $hmatches, PREG_OFFSET_CAPTURE);
foreach ($hmatches[1] as $hmatch) { foreach ($hmatches[1] as $hmatch) {
$tag = common_canonical_tag($hmatch[0]); $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) { if (!$plist instanceof Profile_list || $plist->private) {
continue; continue;
} }