From 7320c5e8e835e09b7ec77c8dd11303d6d6dec039 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Thu, 21 Feb 2019 21:29:22 -0500 Subject: [PATCH 01/13] Normalize Contact::magicLinkByContact method name --- include/conversation.php | 8 ++++---- src/Model/Contact.php | 4 ++-- src/Object/Post.php | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/include/conversation.php b/include/conversation.php index 23a0dcbf29..3c52e62fc4 100644 --- a/include/conversation.php +++ b/include/conversation.php @@ -365,7 +365,7 @@ function localize_item(&$item) // Only create a redirection to a magic link when logged in if (!empty($item['plink']) && (local_user() || remote_user())) { - $item['plink'] = Contact::magicLinkbyContact($author, $item['plink']); + $item['plink'] = Contact::magicLinkByContact($author, $item['plink']); } } @@ -625,7 +625,7 @@ function conversation(App $a, array $items, Pager $pager, $mode, $update, $previ $author = ['uid' => 0, 'id' => $item['author-id'], 'network' => $item['author-network'], 'url' => $item['author-link']]; - $profile_link = Contact::magicLinkbyContact($author); + $profile_link = Contact::magicLinkByContact($author); if (strpos($profile_link, 'redir/') === 0) { $sparkle = ' sparkle'; @@ -858,7 +858,7 @@ function item_photo_menu($item) { $author = ['uid' => 0, 'id' => $item['author-id'], 'network' => $item['author-network'], 'url' => $item['author-link']]; - $profile_link = Contact::magicLinkbyContact($author); + $profile_link = Contact::magicLinkByContact($author); $sparkle = (strpos($profile_link, 'redir/') === 0); $cid = 0; @@ -966,7 +966,7 @@ function builtin_activity_puller($item, &$conv_responses) { if (activity_match($item['verb'], $verb) && ($item['id'] != $item['parent'])) { $author = ['uid' => 0, 'id' => $item['author-id'], 'network' => $item['author-network'], 'url' => $item['author-link']]; - $url = Contact::magicLinkbyContact($author); + $url = Contact::magicLinkByContact($author); if (strpos($url, 'redir/') === 0) { $sparkle = ' class="sparkle" '; } diff --git a/src/Model/Contact.php b/src/Model/Contact.php index d38d1cc101..5f63d89d0f 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -2184,7 +2184,7 @@ class Contact extends BaseObject { $contact = DBA::selectFirst('contact', ['id', 'network', 'url', 'uid'], ['id' => $cid]); - return self::magicLinkbyContact($contact, $url); + return self::magicLinkByContact($contact, $url); } /** @@ -2197,7 +2197,7 @@ class Contact extends BaseObject * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function magicLinkbyContact($contact, $url = '') + public static function magicLinkByContact($contact, $url = '') { if ((!local_user() && !remote_user()) || ($contact['network'] != Protocol::DFRN)) { return $url ?: $contact['url']; // Equivalent to ($url != '') ? $url : $contact['url']; diff --git a/src/Object/Post.php b/src/Object/Post.php index e24607a787..bd48dbb3d3 100644 --- a/src/Object/Post.php +++ b/src/Object/Post.php @@ -83,7 +83,7 @@ class Post extends BaseObject $author = ['uid' => 0, 'id' => $this->getDataValue('author-id'), 'network' => $this->getDataValue('author-network'), 'url' => $this->getDataValue('author-link')]; - $this->redirect_url = Contact::magicLinkbyContact($author); + $this->redirect_url = Contact::magicLinkByContact($author); if (!$this->isToplevel()) { $this->threaded = true; } @@ -224,7 +224,7 @@ class Post extends BaseObject 'network' => $item['author-network'], 'url' => $item['author-link']]; if (local_user() || remote_user()) { - $profile_link = Contact::magicLinkbyContact($author); + $profile_link = Contact::magicLinkByContact($author); } else { $profile_link = $item['author-link']; } @@ -948,7 +948,7 @@ class Post extends BaseObject $owner = ['uid' => 0, 'id' => $this->getDataValue('owner-id'), 'network' => $this->getDataValue('owner-network'), 'url' => $this->getDataValue('owner-link')]; - $this->owner_url = Contact::magicLinkbyContact($owner); + $this->owner_url = Contact::magicLinkByContact($owner); } } } From cd535851012fc4e96b8e073f3d252ce7fa2844b1 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Thu, 21 Feb 2019 21:37:23 -0500 Subject: [PATCH 02/13] Improve /itemsource display - Add Item Id - Add Item Terms --- src/Module/Itemsource.php | 16 ++++++++++--- view/templates/debug/itemsource.tpl | 37 +++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/src/Module/Itemsource.php b/src/Module/Itemsource.php index 9e62a568c8..12ce04f95c 100644 --- a/src/Module/Itemsource.php +++ b/src/Module/Itemsource.php @@ -2,9 +2,12 @@ namespace Friendica\Module; +use Friendica\Content\Text\HTML; use Friendica\Core\L10n; use Friendica\Core\Renderer; use Friendica\Model; +use Friendica\Protocol\ActivityPub\Processor; +use Friendica\Protocol\Diaspora; /** * @author Hypolite Petovan @@ -27,20 +30,27 @@ class Itemsource extends \Friendica\BaseModule $source = ''; $item_uri = ''; + $item_id = ''; + $terms = []; if (!empty($guid)) { - $item = Model\Item::selectFirst([], ['guid' => $guid]); + $item = Model\Item::selectFirst(['id', 'guid', 'uri'], ['guid' => $guid]); $conversation = Model\Conversation::getByItemUri($item['uri']); + $guid = $item['guid']; + $item_id = $item['id']; $item_uri = $item['uri']; $source = $conversation['source']; + $terms = Model\Term::tagArrayFromItemId($item['id'], [Model\Term::HASHTAG, Model\Term::MENTION, Model\Term::IMPLICIT_MENTION]); } $tpl = Renderer::getMarkupTemplate('debug/itemsource.tpl'); $o = Renderer::replaceMacros($tpl, [ - '$guid' => ['guid', L10n::t('Item Guid'), defaults($_REQUEST, 'guid', ''), ''], + '$guid' => ['guid', L10n::t('Item Guid'), $guid, ''], '$source' => $source, - '$item_uri' => $item_uri + '$item_uri' => $item_uri, + '$item_id' => $item_id, + '$terms' => $terms, ]); return $o; diff --git a/view/templates/debug/itemsource.tpl b/view/templates/debug/itemsource.tpl index 377409ebf3..0d925b3c8a 100644 --- a/view/templates/debug/itemsource.tpl +++ b/view/templates/debug/itemsource.tpl @@ -10,6 +10,14 @@ {{if $source}}
+
+
+

Item Id

+
+
+ {{$item_id}} +
+

Item URI

@@ -18,6 +26,35 @@ {{$item_uri}}
+
+
+

Terms

+
+
+ + + + + + + {{foreach $terms as $term}} + + + + + + {{/foreach}} +
TypeTermURL
+ {{if $term.type == 1}}Tag{{/if}} + {{if $term.type == 2}}Mention{{/if}} + {{if $term.type == 8}}Implicit Mention{{/if}} + + {{$term.term}} + + {{$term.url}} +
+
+

Source

From 1917f04153ffa37880a9900c76e54d6d73e90c85 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 22 Feb 2019 19:15:35 -0500 Subject: [PATCH 03/13] Rewrite Term class - Move term type constants from TERM_* to Term::* - Move term object type constants from TERM_OBJ_* to Term::OBJECT_TYPE_* - Add Term::isType() method - Add Strings::startsWith() method --- boot.php | 37 +++--- src/Model/Term.php | 265 ++++++++++++++++++++++++++++--------------- src/Util/Strings.php | 15 +++ 3 files changed, 209 insertions(+), 108 deletions(-) diff --git a/boot.php b/boot.php index 14c0a88ede..830a636aca 100644 --- a/boot.php +++ b/boot.php @@ -25,6 +25,7 @@ use Friendica\Core\Protocol; use Friendica\Core\System; use Friendica\Database\DBA; use Friendica\Model\Contact; +use Friendica\Model\Term; use Friendica\Util\BasePath; use Friendica\Util\DateTimeFormat; @@ -171,23 +172,27 @@ define('NOTIFY_SYSTEM', 32768); /* @}*/ -/** - * @name Term - * - * Tag/term types - * @{ - */ -define('TERM_UNKNOWN', 0); -define('TERM_HASHTAG', 1); -define('TERM_MENTION', 2); -define('TERM_CATEGORY', 3); -define('TERM_PCATEGORY', 4); -define('TERM_FILE', 5); -define('TERM_SAVEDSEARCH', 6); -define('TERM_CONVERSATION', 7); +/** @deprecated since 2019.03, use Term::UNKNOWN instead */ +define('TERM_UNKNOWN', Term::UNKNOWN); +/** @deprecated since 2019.03, use Term::HASHTAG instead */ +define('TERM_HASHTAG', Term::HASHTAG); +/** @deprecated since 2019.03, use Term::MENTION instead */ +define('TERM_MENTION', Term::MENTION); +/** @deprecated since 2019.03, use Term::CATEGORY instead */ +define('TERM_CATEGORY', Term::CATEGORY); +/** @deprecated since 2019.03, use Term::PCATEGORY instead */ +define('TERM_PCATEGORY', Term::PCATEGORY); +/** @deprecated since 2019.03, use Term::FILE instead */ +define('TERM_FILE', Term::FILE); +/** @deprecated since 2019.03, use Term::SAVEDSEARCH instead */ +define('TERM_SAVEDSEARCH', Term::SAVEDSEARCH); +/** @deprecated since 2019.03, use Term::CONVERSATION instead */ +define('TERM_CONVERSATION', Term::CONVERSATION); -define('TERM_OBJ_POST', 1); -define('TERM_OBJ_PHOTO', 2); +/** @deprecated since 2019.03, use Term::OBJECT_TYPE_POST instead */ +define('TERM_OBJ_POST', Term::OBJECT_TYPE_POST); +/** @deprecated since 2019.03, use Term::OBJECT_TYPE_PHOTO instead */ +define('TERM_OBJ_PHOTO', Term::OBJECT_TYPE_PHOTO); /** * @name Namespaces diff --git a/src/Model/Term.php b/src/Model/Term.php index 6e3425524a..5266627aca 100644 --- a/src/Model/Term.php +++ b/src/Model/Term.php @@ -6,32 +6,61 @@ namespace Friendica\Model; use Friendica\Core\System; use Friendica\Database\DBA; +use Friendica\Util\Strings; class Term { - public static function tagTextFromItemId($itemid) - { - $tag_text = ''; - $condition = ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_HASHTAG, TERM_MENTION]]; - $tags = DBA::select('term', [], $condition); - while ($tag = DBA::fetch($tags)) { - if ($tag_text != '') { - $tag_text .= ','; - } + const UNKNOWN = 0; + const HASHTAG = 1; + const MENTION = 2; + const CATEGORY = 3; + const PCATEGORY = 4; + const FILE = 5; + const SAVEDSEARCH = 6; + const CONVERSATION = 7; + const IMPLICIT_MENTION = 8; + const EXCLUSIVE_MENTION = 9; - if ($tag['type'] == 1) { - $tag_text .= '#'; - } else { - $tag_text .= '@'; - } - $tag_text .= '[url=' . $tag['url'] . ']' . $tag['term'] . '[/url]'; + const TAG_CHARACTER = [ + self::HASHTAG => '#', + self::MENTION => '@', + self::IMPLICIT_MENTION => '%', + self::EXCLUSIVE_MENTION => '!', + ]; + + const OBJECT_TYPE_POST = 1; + const OBJECT_TYPE_PHOTO = 2; + + /** + * Generates the legacy item.tag field comma-separated BBCode string from an item ID. + * Includes only hashtags, implicit and explicit mentions. + * + * @param int $item_id + * @return string + * @throws \Exception + */ + public static function tagTextFromItemId($item_id) + { + $tag_list = []; + $tags = self::tagArrayFromItemId($item_id, [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION]); + foreach ($tags as $tag) { + $tag_list[] = self::TAG_CHARACTER[$tag['type']] . '[url=' . $tag['url'] . ']' . $tag['term'] . '[/url]'; } - return $tag_text; + + return implode(',', $tag_list); } - public static function tagArrayFromItemId($itemid, $type = [TERM_HASHTAG, TERM_MENTION]) + /** + * Retrieves the terms from the provided type(s) associated with the provided item ID. + * + * @param int $item_id + * @param int|array $type + * @return array + * @throws \Exception + */ + public static function tagArrayFromItemId($item_id, $type = [self::HASHTAG, self::MENTION]) { - $condition = ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => $type]; + $condition = ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => $type]; $tags = DBA::select('term', ['type', 'term', 'url'], $condition); if (!DBA::isResult($tags)) { return []; @@ -40,22 +69,39 @@ class Term return DBA::toArray($tags); } - public static function fileTextFromItemId($itemid) + /** + * Generates the legacy item.file field string from an item ID. + * Includes only file and category terms. + * + * @param int $item_id + * @return string + * @throws \Exception + */ + public static function fileTextFromItemId($item_id) { $file_text = ''; - $condition = ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_FILE, TERM_CATEGORY]]; - $tags = DBA::select('term', [], $condition); - while ($tag = DBA::fetch($tags)) { - if ($tag['type'] == TERM_CATEGORY) { + $tags = self::tagArrayFromItemId($item_id, [self::FILE, self::CATEGORY]); + foreach ($tags as $tag) { + if ($tag['type'] == self::CATEGORY) { $file_text .= '<' . $tag['term'] . '>'; } else { $file_text .= '[' . $tag['term'] . ']'; } } + return $file_text; } - public static function insertFromTagFieldByItemId($itemid, $tags) + /** + * Inserts new terms for the provided item ID based on the legacy item.tag field BBCode content. + * Deletes all previous tag terms for the same item ID. + * Sets both the item.mention and thread.mentions field flags if a mention concerning the item UID is found. + * + * @param int $item_id + * @param string $tag_str + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + */ + public static function insertFromTagFieldByItemId($item_id, $tag_str) { $profile_base = System::baseUrl(); $profile_data = parse_url($profile_base); @@ -64,32 +110,32 @@ class Term $profile_base_diaspora = $profile_data['host'] . $profile_path . '/u/'; $fields = ['guid', 'uid', 'id', 'edited', 'deleted', 'created', 'received', 'title', 'body', 'parent']; - $message = Item::selectFirst($fields, ['id' => $itemid]); - if (!DBA::isResult($message)) { + $item = Item::selectFirst($fields, ['id' => $item_id]); + if (!DBA::isResult($item)) { return; } - $message['tag'] = $tags; + $item['tag'] = $tag_str; // Clean up all tags - self::deleteByItemId($itemid); + self::deleteByItemId($item_id); - if ($message['deleted']) { + if ($item['deleted']) { return; } - $taglist = explode(',', $message['tag']); + $taglist = explode(',', $item['tag']); $tags_string = ''; foreach ($taglist as $tag) { - if ((substr(trim($tag), 0, 1) == '#') || (substr(trim($tag), 0, 1) == '@') || (substr(trim($tag), 0, 1) == '!')) { + if (Strings::startsWith($tag, self::TAG_CHARACTER)) { $tags_string .= ' ' . trim($tag); } else { $tags_string .= ' #' . trim($tag); } } - $data = ' ' . $message['title'] . ' ' . $message['body'] . ' ' . $tags_string . ' '; + $data = ' ' . $item['title'] . ' ' . $item['body'] . ' ' . $tags_string . ' '; // ignore anything in a code block $data = preg_replace('/\[code\](.*?)\[\/code\]/sm', '', $data); @@ -103,11 +149,15 @@ class Term } } - $pattern = '/\W([\#@!])\[url\=(.*?)\](.*?)\[\/url\]/ism'; + $pattern = '/\W([\#@!%])\[url\=(.*?)\](.*?)\[\/url\]/ism'; if (preg_match_all($pattern, $data, $matches, PREG_SET_ORDER)) { foreach ($matches as $match) { - if (($match[1] == '@') || ($match[1] == '!')) { + if (in_array($match[1], [ + self::TAG_CHARACTER[self::MENTION], + self::TAG_CHARACTER[self::IMPLICIT_MENTION], + self::TAG_CHARACTER[self::EXCLUSIVE_MENTION] + ])) { $contact = Contact::getDetailsByURL($match[2], 0); if (!empty($contact['addr'])) { $match[3] = $contact['addr']; @@ -118,12 +168,12 @@ class Term } } - $tags[$match[1] . trim($match[3], ',.:;[]/\"?!')] = $match[2]; + $tags[$match[2]] = $match[1] . trim($match[3], ',.:;[]/\"?!'); } } - foreach ($tags as $tag => $link) { - if (substr(trim($tag), 0, 1) == '#') { + foreach ($tags as $link => $tag) { + if (self::isType($tag, self::HASHTAG)) { // try to ignore #039 or #1 or anything like that if (ctype_digit(substr(trim($tag), 1))) { continue; @@ -134,11 +184,15 @@ class Term continue; } - $type = TERM_HASHTAG; + $type = self::HASHTAG; $term = substr($tag, 1); $link = ''; - } elseif ((substr(trim($tag), 0, 1) == '@') || (substr(trim($tag), 0, 1) == '!')) { - $type = TERM_MENTION; + } elseif (self::isType($tag, self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION)) { + if (self::isType($tag, self::MENTION, self::EXCLUSIVE_MENTION)) { + $type = self::MENTION; + } else { + $type = self::IMPLICIT_MENTION; + } $contact = Contact::getDetailsByURL($link, 0); if (!empty($contact['name'])) { @@ -147,43 +201,49 @@ class Term $term = substr($tag, 1); } } else { // This shouldn't happen - $type = TERM_HASHTAG; + $type = self::HASHTAG; $term = $tag; $link = ''; } - if (DBA::exists('term', ['uid' => $message['uid'], 'otype' => TERM_OBJ_POST, 'oid' => $itemid, 'term' => $term])) { + if (DBA::exists('term', ['uid' => $item['uid'], 'otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'term' => $term, 'type' => $type])) { continue; } - if ($message['uid'] == 0) { + if ($item['uid'] == 0) { $global = true; - DBA::update('term', ['global' => true], ['otype' => TERM_OBJ_POST, 'guid' => $message['guid']]); + DBA::update('term', ['global' => true], ['otype' => self::OBJECT_TYPE_POST, 'guid' => $item['guid']]); } else { - $global = DBA::exists('term', ['uid' => 0, 'otype' => TERM_OBJ_POST, 'guid' => $message['guid']]); + $global = DBA::exists('term', ['uid' => 0, 'otype' => self::OBJECT_TYPE_POST, 'guid' => $item['guid']]); } DBA::insert('term', [ - 'uid' => $message['uid'], - 'oid' => $itemid, - 'otype' => TERM_OBJ_POST, + 'uid' => $item['uid'], + 'oid' => $item_id, + 'otype' => self::OBJECT_TYPE_POST, 'type' => $type, 'term' => $term, 'url' => $link, - 'guid' => $message['guid'], - 'created' => $message['created'], - 'received' => $message['received'], + 'guid' => $item['guid'], + 'created' => $item['created'], + 'received' => $item['received'], 'global' => $global ]); // Search for mentions - if (((substr($tag, 0, 1) == '@') || (substr($tag, 0, 1) == '!')) && (strpos($link, $profile_base_friendica) || strpos($link, $profile_base_diaspora))) { - $users = q("SELECT `uid` FROM `contact` WHERE self AND (`url` = '%s' OR `nurl` = '%s')", $link, $link); + if (self::isType($tag, self::MENTION, self::EXCLUSIVE_MENTION) + && ( + strpos($link, $profile_base_friendica) !== false + || strpos($link, $profile_base_diaspora) !== false + ) + ) { + $users_stmt = DBA::p("SELECT `uid` FROM `contact` WHERE self AND (`url` = ? OR `nurl` = ?)", $link, $link); + $users = DBA::toArray($users_stmt); foreach ($users AS $user) { - if ($user['uid'] == $message['uid']) { - /// @todo This function is called frim Item::update - so we mustn't call that function here - DBA::update('item', ['mention' => true], ['id' => $itemid]); - DBA::update('thread', ['mention' => true], ['iid' => $message['parent']]); + if ($user['uid'] == $item['uid']) { + /// @todo This function is called from Item::update - so we mustn't call that function here + DBA::update('item', ['mention' => true], ['id' => $item_id]); + DBA::update('thread', ['mention' => true], ['iid' => $item['parent']]); } } } @@ -191,20 +251,23 @@ class Term } /** - * @param integer $itemid item id + * Inserts new terms for the provided item ID based on the legacy item.file field BBCode content. + * Deletes all previous file terms for the same item ID. + * + * @param integer $item_id item id * @param $files * @return void * @throws \Exception */ - public static function insertFromFileFieldByItemId($itemid, $files) + public static function insertFromFileFieldByItemId($item_id, $files) { - $message = Item::selectFirst(['uid', 'deleted'], ['id' => $itemid]); + $message = Item::selectFirst(['uid', 'deleted'], ['id' => $item_id]); if (!DBA::isResult($message)) { return; } // Clean up all tags - DBA::delete('term', ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_FILE, TERM_CATEGORY]]); + DBA::delete('term', ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => [self::FILE, self::CATEGORY]]); if ($message["deleted"]) { return; @@ -216,9 +279,9 @@ class Term foreach ($files[1] as $file) { DBA::insert('term', [ 'uid' => $message["uid"], - 'oid' => $itemid, - 'otype' => TERM_OBJ_POST, - 'type' => TERM_FILE, + 'oid' => $item_id, + 'otype' => self::OBJECT_TYPE_POST, + 'type' => self::FILE, 'term' => $file ]); } @@ -228,9 +291,9 @@ class Term foreach ($files[1] as $file) { DBA::insert('term', [ 'uid' => $message["uid"], - 'oid' => $itemid, - 'otype' => TERM_OBJ_POST, - 'type' => TERM_CATEGORY, + 'oid' => $item_id, + 'otype' => self::OBJECT_TYPE_POST, + 'type' => self::CATEGORY, 'term' => $file ]); } @@ -252,6 +315,7 @@ class Term 'tags' => [], 'hashtags' => [], 'mentions' => [], + 'implicit_mentions' => [], ]; $searchpath = System::baseUrl() . "/search?tag="; @@ -259,10 +323,9 @@ class Term $taglist = DBA::select( 'term', ['type', 'term', 'url'], - ["`otype` = ? AND `oid` = ? AND `type` IN (?, ?)", TERM_OBJ_POST, $item['id'], TERM_HASHTAG, TERM_MENTION], + ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item['id'], 'type' => [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION]], ['order' => ['tid']] ); - while ($tag = DBA::fetch($taglist)) { if ($tag['url'] == '') { $tag['url'] = $searchpath . rawurlencode($tag['term']); @@ -270,25 +333,25 @@ class Term $orig_tag = $tag['url']; - $author = ['uid' => 0, 'id' => $item['author-id'], - 'network' => $item['author-network'], 'url' => $item['author-link']]; + $prefix = self::TAG_CHARACTER[$tag['type']]; + switch($tag['type']) { + case self::HASHTAG: + if ($orig_tag != $tag['url']) { + $item['body'] = str_replace($orig_tag, $tag['url'], $item['body']); + } - $prefix = ''; - if ($tag['type'] == TERM_HASHTAG) { - $tag['url'] = Contact::magicLinkByContact($author, $tag['url']); - if ($orig_tag != $tag['url']) { - $item['body'] = str_replace($orig_tag, $tag['url'], $item['body']); - } - - $return['hashtags'][] = '#' . $tag['term'] . ''; - $prefix = '#'; - } elseif ($tag['type'] == TERM_MENTION) { - $tag['url'] = Contact::magicLink($tag['url']); - $return['mentions'][] = '@' . $tag['term'] . ''; - $prefix = '@'; + $return['hashtags'][] = $prefix . '' . $tag['term'] . ''; + $return['tags'][] = $prefix . '' . $tag['term'] . ''; + break; + case self::MENTION: + $tag['url'] = Contact::magicLink($tag['url']); + $return['mentions'][] = $prefix . '' . $tag['term'] . ''; + $return['tags'][] = $prefix . '' . $tag['term'] . ''; + break; + case self::IMPLICIT_MENTION: + $return['implicit_mentions'][] = $prefix . $tag['term']; + break; } - - $return['tags'][] = $prefix . '' . $tag['term'] . ''; } DBA::close($taglist); @@ -296,20 +359,38 @@ class Term } /** - * Delete all tags from an item + * Delete tags of the specific type(s) from an item * - * @param int itemid - choose from which item the tags will be removed - * @param array $type + * @param int $item_id + * @param int|array $type * @throws \Exception */ - public static function deleteByItemId($itemid, $type = [TERM_HASHTAG, TERM_MENTION]) + public static function deleteByItemId($item_id, $type = [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION]) { - if (empty($itemid)) { + if (empty($item_id)) { return; } // Clean up all tags - DBA::delete('term', ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => $type]); + DBA::delete('term', ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => $type]); + } + /** + * Check if the provided tag is of one of the provided term types. + * + * @param string $tag + * @param int ...$types + * @return bool + */ + public static function isType($tag, ...$types) + { + $tag_chars = []; + foreach ($types as $type) { + if (isset(self::TAG_CHARACTER[$type])) { + $tag_chars[] = self::TAG_CHARACTER[$type]; + } + } + + return Strings::startsWith($tag, $tag_chars); } } diff --git a/src/Util/Strings.php b/src/Util/Strings.php index 0c63749c85..55751d8d82 100644 --- a/src/Util/Strings.php +++ b/src/Util/Strings.php @@ -331,4 +331,19 @@ class Strings return $uri; } + + + /** + * Check if the trimmed provided string is starting with one of the provided characters + * + * @param string $string + * @param array $chars + * @return bool + */ + public static function startsWith($string, array $chars) + { + $return = in_array(substr(trim($string), 0, 1), $chars); + + return $return; + } } From 8c1db51a760dd60745c55592fd08dee03c2d8a52 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 22 Feb 2019 23:00:16 -0500 Subject: [PATCH 04/13] Improve Logger calls - Replace various deprecated Logger::log calls - Reassign log level for verbose log calls --- include/enotify.php | 6 +++--- mod/item.php | 2 +- src/Database/DBA.php | 2 +- src/Model/Item.php | 17 ++++++++--------- src/Protocol/ActivityPub/Processor.php | 8 ++++++-- src/Protocol/ActivityPub/Receiver.php | 8 ++++---- src/Protocol/DFRN.php | 2 +- src/Protocol/Diaspora.php | 2 +- src/Util/XML.php | 6 ++++-- 9 files changed, 29 insertions(+), 24 deletions(-) diff --git a/include/enotify.php b/include/enotify.php index 58e1a75f00..db44b464de 100644 --- a/include/enotify.php +++ b/include/enotify.php @@ -33,8 +33,8 @@ function notification($params) $a = \get_app(); // Temporary logging for finding the origin - if (!isset($params['language']) || !isset($params['uid'])) { - Logger::log('Missing parameters.' . System::callstack()); + if (!isset($params['uid'])) { + Logger::notice('Missing parameters "uid".', ['params' => $params, 'callstack' => System::callstack()]); } // Ensure that the important fields are set at any time @@ -42,7 +42,7 @@ function notification($params) $user = DBA::selectFirst('user', $fields, ['uid' => $params['uid']]); if (!DBA::isResult($user)) { - Logger::log('Unknown user ' . $params['uid']); + Logger::error('Unknown user', ['uid' => $params['uid']]); return false; } diff --git a/mod/item.php b/mod/item.php index 40c01da20f..100205f1c4 100644 --- a/mod/item.php +++ b/mod/item.php @@ -131,7 +131,7 @@ function item_post(App $a) { } if ($parent) { - Logger::log('mod_item: item_post parent=' . $parent); + Logger::info('mod_item: item_post parent=' . $toplevel_item_id); } $post_id = intval(defaults($_REQUEST, 'post_id', 0)); diff --git a/src/Database/DBA.php b/src/Database/DBA.php index 897ab2e6ed..326fb0771d 100644 --- a/src/Database/DBA.php +++ b/src/Database/DBA.php @@ -425,7 +425,7 @@ class DBA if ((substr_count($sql, '?') != count($args)) && (count($args) > 0)) { // Question: Should we continue or stop the query here? - Logger::log('Parameter mismatch. Query "'.$sql.'" - Parameters '.print_r($args, true), Logger::DEBUG); + Logger::error('Parameter mismatch. Query "'.$sql.'" - Parameters '.print_r($args, true)); } $sql = self::cleanQuery($sql); diff --git a/src/Model/Item.php b/src/Model/Item.php index 475f9131fd..e4d18c9a74 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -1336,7 +1336,7 @@ class Item extends BaseObject $expire_date = time() - ($expire_interval * 86400); $created_date = strtotime($item['created']); if ($created_date < $expire_date) { - Logger::log('item-store: item created ('.date('c', $created_date).') before expiration time ('.date('c', $expire_date).'). ignored. ' . print_r($item,true), Logger::DEBUG); + Logger::notice('item-store: item created ('.date('c', $created_date).') before expiration time ('.date('c', $expire_date).'). ignored. ' . print_r($item,true)); return 0; } } @@ -1354,7 +1354,7 @@ class Item extends BaseObject if (DBA::isResult($existing)) { // We only log the entries with a different user id than 0. Otherwise we would have too many false positives if ($uid != 0) { - Logger::log("Item with uri ".$item['uri']." already existed for user ".$uid." with id ".$existing["id"]." target network ".$existing["network"]." - new network: ".$item['network']); + Logger::notice("Item with uri ".$item['uri']." already existed for user ".$uid." with id ".$existing["id"]." target network ".$existing["network"]." - new network: ".$item['network']); } return $existing["id"]; @@ -1405,7 +1405,7 @@ class Item extends BaseObject // When there is no content then we don't post it if ($item['body'].$item['title'] == '') { - Logger::log('No body, no title.'); + Logger::notice('No body, no title.'); return 0; } @@ -1432,7 +1432,7 @@ class Item extends BaseObject $item['author-id'] = defaults($item, 'author-id', Contact::getIdForURL($item["author-link"], 0, false, $default)); if (Contact::isBlocked($item["author-id"])) { - Logger::log('Contact '.$item["author-id"].' is blocked, item '.$item["uri"].' will not be stored'); + Logger::notice('Contact '.$item["author-id"].' is blocked, item '.$item["uri"].' will not be stored'); return 0; } @@ -1442,22 +1442,21 @@ class Item extends BaseObject $item['owner-id'] = defaults($item, 'owner-id', Contact::getIdForURL($item["owner-link"], 0, false, $default)); if (Contact::isBlocked($item["owner-id"])) { - Logger::log('Contact '.$item["owner-id"].' is blocked, item '.$item["uri"].' will not be stored'); + Logger::notice('Contact '.$item["owner-id"].' is blocked, item '.$item["uri"].' will not be stored'); return 0; } if ($item['network'] == Protocol::PHANTOM) { - Logger::log('Missing network. Called by: '.System::callstack(), Logger::DEBUG); + Logger::notice('Missing network. Called by: '.System::callstack(), Logger::DEBUG); $item['network'] = Protocol::DFRN; - Logger::log("Set network to " . $item["network"] . " for " . $item["uri"], Logger::DEBUG); + Logger::notice("Set network to " . $item["network"] . " for " . $item["uri"], Logger::DEBUG); } // Checking if there is already an item with the same guid - Logger::log('Checking for an item for user '.$item['uid'].' on network '.$item['network'].' with the guid '.$item['guid'], Logger::DEBUG); $condition = ['guid' => $item['guid'], 'network' => $item['network'], 'uid' => $item['uid']]; if (self::exists($condition)) { - Logger::log('found item with guid '.$item['guid'].' for user '.$item['uid'].' on network '.$item['network'], Logger::DEBUG); + Logger::notice('Found already existing item with guid '.$item['guid'].' for user '.$item['uid'].' on network '.$item['network']); return 0; } diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index f4befcd6f1..5899d7f25f 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -289,7 +289,7 @@ class Processor $item['owner-link'] = $activity['actor']; $item['owner-id'] = Contact::getIdForURL($activity['actor'], 0, true); } else { - Logger::log('Ignoring actor because of thread completion.', Logger::DEBUG); + Logger::info('Ignoring actor because of thread completion.'); $item['owner-link'] = $item['author-link']; $item['owner-id'] = $item['author-id']; } @@ -358,7 +358,11 @@ class Processor } $item_id = Item::insert($item); - Logger::log('Storing for user ' . $item['uid'] . ': ' . $item_id); + if ($item_id) { + Logger::info('Item insertion successful', ['user' => $item['uid'], 'item_id' => $item_id]); + } else { + Logger::notice('Item insertion aborted', ['user' => $item['uid']]); + } if ($item['uid'] == 0) { $stored = $item_id; diff --git a/src/Protocol/ActivityPub/Receiver.php b/src/Protocol/ActivityPub/Receiver.php index e0ee1f0f90..5ee81302f2 100644 --- a/src/Protocol/ActivityPub/Receiver.php +++ b/src/Protocol/ActivityPub/Receiver.php @@ -62,16 +62,16 @@ class Receiver { $http_signer = HTTPSignature::getSigner($body, $header); if (empty($http_signer)) { - Logger::log('Invalid HTTP signature, message will be discarded.', Logger::DEBUG); + Logger::warning('Invalid HTTP signature, message will be discarded.'); return; } else { - Logger::log('HTTP signature is signed by ' . $http_signer, Logger::DEBUG); + Logger::info('Valid HTTP signature', ['signer' => $http_signer]); } $activity = json_decode($body, true); if (empty($activity)) { - Logger::log('Invalid body.', Logger::DEBUG); + Logger::warning('Invalid body.'); return; } @@ -79,7 +79,7 @@ class Receiver $actor = JsonLD::fetchElement($ldactivity, 'as:actor'); - Logger::log('Message for user ' . $uid . ' is from actor ' . $actor, Logger::DEBUG); + Logger::info('Message for user ' . $uid . ' is from actor ' . $actor); if (LDSignature::isSigned($activity)) { $ld_signer = LDSignature::getSigner($activity); diff --git a/src/Protocol/DFRN.php b/src/Protocol/DFRN.php index e6524de965..39d982bb92 100644 --- a/src/Protocol/DFRN.php +++ b/src/Protocol/DFRN.php @@ -1546,7 +1546,7 @@ class DFRN $author["network"] = $contact_old["network"]; } else { if (!$onlyfetch) { - Logger::log("Contact ".$author["link"]." wasn't found for user ".$importer["importer_uid"]." XML: ".$xml, Logger::DEBUG); + Logger::debug("Contact ".$author["link"]." wasn't found for user ".$importer["importer_uid"]." XML: ".$xml); } $author["contact-unknown"] = true; diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 77ad017044..3687fb9d4b 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -942,7 +942,7 @@ class Diaspora $person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'addr' => $handle]); if (DBA::isResult($person)) { - Logger::log("In cache " . print_r($person, true), Logger::DEBUG); + Logger::debug("In cache " . print_r($person, true)); // update record occasionally so it doesn't get stale $d = strtotime($person["updated"]." +00:00"); diff --git a/src/Util/XML.php b/src/Util/XML.php index ba146ec7b8..4dd6d84ecb 100644 --- a/src/Util/XML.php +++ b/src/Util/XML.php @@ -6,6 +6,7 @@ namespace Friendica\Util; use Friendica\Core\Logger; use DOMXPath; +use Friendica\Core\System; use SimpleXMLElement; /** @@ -422,10 +423,11 @@ class XML $x = @simplexml_load_string($s); if (!$x) { - Logger::log('libxml: parse: error: ' . $s, Logger::DATA); + Logger::error('Error(s) while parsing XML string.', ['callstack' => System::callstack()]); foreach (libxml_get_errors() as $err) { - Logger::log('libxml: parse: ' . $err->code." at ".$err->line.":".$err->column." : ".$err->message, Logger::DATA); + Logger::info('libxml error', ['code' => $err->code, 'position' => $err->line . ":" . $err->column, 'message' => $err->message]); } + Logger::debug('Erroring XML string', ['xml' => $s]); libxml_clear_errors(); } return $x; From 34bc0b0c97b7ba4a29d217c33227906e2ced6a73 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 22 Feb 2019 23:03:32 -0500 Subject: [PATCH 05/13] Add potential debug display of implicit mentions in frio - Refactored conversation() by removing extraneous intermediary variables --- include/conversation.php | 32 +++++++++-------------- src/Object/Post.php | 1 + view/theme/frio/templates/wall_thread.tpl | 5 +++- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/include/conversation.php b/include/conversation.php index 3c52e62fc4..77b96574a0 100644 --- a/include/conversation.php +++ b/include/conversation.php @@ -660,21 +660,12 @@ function conversation(App $a, array $items, Pager $pager, $mode, $update, $previ list($categories, $folders) = get_cats_and_terms($item); - $profile_name_e = $profile_name; - if (!empty($item['content-warning']) && PConfig::get(local_user(), 'system', 'disable_cw', false)) { - $title_e = ucfirst($item['content-warning']); + $title = ucfirst($item['content-warning']); } else { - $title_e = $item['title']; + $title = $item['title']; } - $body_e = $body; - $tags_e = $tags['tags']; - $hashtags_e = $tags['hashtags']; - $mentions_e = $tags['mentions']; - $location_e = $location; - $owner_name_e = $owner_name; - $tmp_item = [ 'template' => $tpl, 'id' => ($preview ? 'P0' : $item['id']), @@ -684,27 +675,28 @@ function conversation(App $a, array $items, Pager $pager, $mode, $update, $previ 'linktitle' => L10n::t('View %s\'s profile @ %s', $profile_name, $item['author-link']), 'profile_url' => $profile_link, 'item_photo_menu' => item_photo_menu($item), - 'name' => $profile_name_e, + 'name' => $profile_name, 'sparkle' => $sparkle, 'lock' => $lock, 'thumb' => System::removedBaseUrl(ProxyUtils::proxifyUrl($item['author-avatar'], false, ProxyUtils::SIZE_THUMB)), - 'title' => $title_e, - 'body' => $body_e, - 'tags' => $tags_e, - 'hashtags' => $hashtags_e, - 'mentions' => $mentions_e, + 'title' => $title, + 'body' => $body, + 'tags' => $tags['tags'], + 'hashtags' => $tags['hashtags'], + 'mentions' => $tags['mentions'], + 'implicit_mentions' => $tags['implicit_mentions'], 'txt_cats' => L10n::t('Categories:'), 'txt_folders' => L10n::t('Filed under:'), 'has_cats' => ((count($categories)) ? 'true' : ''), 'has_folders' => ((count($folders)) ? 'true' : ''), 'categories' => $categories, 'folders' => $folders, - 'text' => strip_tags($body_e), + 'text' => strip_tags($body), 'localtime' => DateTimeFormat::local($item['created'], 'r'), 'ago' => (($item['app']) ? L10n::t('%s from %s', Temporal::getRelativeDate($item['created']),$item['app']) : Temporal::getRelativeDate($item['created'])), - 'location' => $location_e, + 'location' => $location, 'indent' => '', - 'owner_name' => $owner_name_e, + 'owner_name' => $owner_name, 'owner_url' => $owner_url, 'owner_photo' => System::removedBaseUrl(ProxyUtils::proxifyUrl($item['owner-avatar'], false, ProxyUtils::SIZE_THUMB)), 'plink' => Item::getPlink($item), diff --git a/src/Object/Post.php b/src/Object/Post.php index bd48dbb3d3..85fd7876cb 100644 --- a/src/Object/Post.php +++ b/src/Object/Post.php @@ -366,6 +366,7 @@ class Post extends BaseObject 'tags' => $tags['tags'], 'hashtags' => $tags['hashtags'], 'mentions' => $tags['mentions'], + 'implicit_mentions' => $tags['implicit_mentions'], 'txt_cats' => L10n::t('Categories:'), 'txt_folders' => L10n::t('Filed under:'), 'has_cats' => ((count($categories)) ? 'true' : ''), diff --git a/view/theme/frio/templates/wall_thread.tpl b/view/theme/frio/templates/wall_thread.tpl index 1fb1221253..bd069f5cd7 100644 --- a/view/theme/frio/templates/wall_thread.tpl +++ b/view/theme/frio/templates/wall_thread.tpl @@ -293,8 +293,11 @@ as the value of $top_child_total (this is done at the end of this file) {{foreach $item.mentions as $tag}} {{$tag nofilter}} {{/foreach}} - {{/if}} + {{*foreach $item.implicit_mentions as $tag}} + {{$tag nofilter}} + {{/foreach*}} + {{/if}} {{foreach $item.folders as $cat}} {{$cat.name}}{{if $cat.removeurl}} (x) {{/if}} {{/foreach}} From 0cc0df9e3e7696c79642411bc7efa9e7d6008124 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 22 Feb 2019 23:23:05 -0500 Subject: [PATCH 06/13] Rename system.disable_mentions_removal config key to system.disable_implicit_mentions - Update configuration key description --- config/defaults.config.php | 11 ++++++++--- src/Protocol/ActivityPub/Processor.php | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/config/defaults.config.php b/config/defaults.config.php index 694f75c220..a6f90f319e 100644 --- a/config/defaults.config.php +++ b/config/defaults.config.php @@ -135,9 +135,14 @@ return [ // Disables the check if a mail address is in a valid format and can be resolved via DNS. 'disable_email_validation' => false, - // disable_mentions_removal (Boolean) - // Disables the automatic removal of implicit mentions in ActivityPub postings. - 'disable_mentions_removal' => false, + // disable_implicit_mentions (Boolean) since 2019.03 + // Implicit mentions are mentions in the body of replies that are redundant in a thread-enabled system like Friendica. + // This config key disables the gathering of implicit mentions in incoming and outgoing posts. + // Also disables the default automatic removal of implicit mentions from the body of incoming posts. + // Also disables the default automatic addition of implicit mentions in the body of outgoing posts. + // Disabling implicit mentions also affects the "explicit_mentions" additional feature by limiting it + // to the replied-to post author mention in the comment boxes. + 'disable_implicit_mentions' => false, // disable_url_validation (Boolean) // Disables the DNS lookup of an URL. diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index 5899d7f25f..68697381b2 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -693,7 +693,7 @@ class Processor */ private static function removeImplicitMentionsFromBody($body, array $implicit_mentions) { - if (Config::get('system', 'disable_mentions_removal')) { + if (Config::get('system', 'disable_implicit_mentions')) { return $body; } From c7dfc88c6c31750a07e36dc1ba91ecfcfce3c7bd Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 22 Feb 2019 23:28:39 -0500 Subject: [PATCH 07/13] Add implicit mention gathering in local posts - Rename $parent_item to $toplevel_item in item_post() - Add $thread_parent_id variable to distinguish from $parent_item - Add item_add_implicit_mentions() function --- mod/item.php | 125 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 77 insertions(+), 48 deletions(-) diff --git a/mod/item.php b/mod/item.php index 100205f1c4..122f62c4e5 100644 --- a/mod/item.php +++ b/mod/item.php @@ -33,6 +33,7 @@ use Friendica\Model\FileTag; use Friendica\Model\Item; use Friendica\Model\Photo; use Friendica\Model\Attach; +use Friendica\Model\Term; use Friendica\Protocol\Diaspora; use Friendica\Protocol\Email; use Friendica\Util\DateTimeFormat; @@ -83,13 +84,13 @@ function item_post(App $a) { } // Is this a reply to something? - $thr_parent = intval(defaults($_REQUEST, 'parent', 0)); + $toplevel_item_id = intval(defaults($_REQUEST, 'parent', 0)); $thr_parent_uri = trim(defaults($_REQUEST, 'parent_uri', '')); - $thr_parent_contact = null; + $thread_parent_id = 0; + $thread_parent_contact = null; - $parent = 0; - $parent_item = null; + $toplevel_item = null; $parent_user = null; $parent_contact = null; @@ -98,25 +99,26 @@ function item_post(App $a) { $profile_uid = defaults($_REQUEST, 'profile_uid', local_user()); $posttype = defaults($_REQUEST, 'post_type', Item::PT_ARTICLE); - if ($thr_parent || $thr_parent_uri) { - if ($thr_parent) { - $parent_item = Item::selectFirst([], ['id' => $thr_parent]); + if ($toplevel_item_id || $thr_parent_uri) { + if ($toplevel_item_id) { + $toplevel_item = Item::selectFirst([], ['id' => $toplevel_item_id]); } elseif ($thr_parent_uri) { - $parent_item = Item::selectFirst([], ['uri' => $thr_parent_uri, 'uid' => $profile_uid]); + $toplevel_item = Item::selectFirst([], ['uri' => $thr_parent_uri, 'uid' => $profile_uid]); } - // if this isn't the real parent of the conversation, find it - if (DBA::isResult($parent_item)) { + // if this isn't the top-level parent of the conversation, find it + if (DBA::isResult($toplevel_item)) { // The URI and the contact is taken from the direct parent which needn't to be the top parent - $thr_parent_uri = $parent_item['uri']; - $thr_parent_contact = Contact::getDetailsByURL($parent_item["author-link"]); + $thread_parent_id = $toplevel_item['id']; + $thr_parent_uri = $toplevel_item['uri']; + $thread_parent_contact = Contact::getDetailsByURL($toplevel_item["author-link"]); - if ($parent_item['id'] != $parent_item['parent']) { - $parent_item = Item::selectFirst(Item::ITEM_FIELDLIST, ['id' => $parent_item['parent']]); + if ($toplevel_item['id'] != $toplevel_item['parent']) { + $toplevel_item = Item::selectFirst(Item::ITEM_FIELDLIST, ['id' => $toplevel_item['parent']]); } } - if (!DBA::isResult($parent_item)) { + if (!DBA::isResult($toplevel_item)) { notice(L10n::t('Unable to locate original post.') . EOL); if (!empty($_REQUEST['return'])) { $a->internalRedirect($return_path); @@ -124,13 +126,13 @@ function item_post(App $a) { exit(); } - $parent = $parent_item['id']; - $parent_user = $parent_item['uid']; + $toplevel_item_id = $toplevel_item['id']; + $parent_user = $toplevel_item['uid']; $objecttype = ACTIVITY_OBJ_COMMENT; } - if ($parent) { + if ($toplevel_item_id) { Logger::info('mod_item: item_post parent=' . $toplevel_item_id); } @@ -160,7 +162,7 @@ function item_post(App $a) { } // Allow commenting if it is an answer to a public post - $allow_comment = local_user() && ($profile_uid == 0) && $parent && in_array($parent_item['network'], [Protocol::ACTIVITYPUB, Protocol::OSTATUS, Protocol::DIASPORA, Protocol::DFRN]); + $allow_comment = local_user() && ($profile_uid == 0) && $toplevel_item_id && in_array($toplevel_item['network'], [Protocol::ACTIVITYPUB, Protocol::OSTATUS, Protocol::DIASPORA, Protocol::DFRN]); // Now check that valid personal details have been provided if (!Security::canWriteToUserWall($profile_uid) && !$allow_comment) { @@ -183,7 +185,7 @@ function item_post(App $a) { $user = DBA::selectFirst('user', [], ['uid' => $profile_uid]); - if (!DBA::isResult($user) && !$parent) { + if (!DBA::isResult($user) && !$toplevel_item_id) { return 0; } @@ -287,21 +289,21 @@ function item_post(App $a) { // If this is a comment, set the permissions from the parent. - if ($parent_item) { + if ($toplevel_item) { // for non native networks use the network of the original post as network of the item - if (($parent_item['network'] != Protocol::DIASPORA) - && ($parent_item['network'] != Protocol::OSTATUS) + if (($toplevel_item['network'] != Protocol::DIASPORA) + && ($toplevel_item['network'] != Protocol::OSTATUS) && ($network == "")) { - $network = $parent_item['network']; + $network = $toplevel_item['network']; } - $str_contact_allow = $parent_item['allow_cid']; - $str_group_allow = $parent_item['allow_gid']; - $str_contact_deny = $parent_item['deny_cid']; - $str_group_deny = $parent_item['deny_gid']; - $private = $parent_item['private']; + $str_contact_allow = $toplevel_item['allow_cid']; + $str_group_allow = $toplevel_item['allow_gid']; + $str_contact_deny = $toplevel_item['deny_cid']; + $str_group_deny = $toplevel_item['deny_gid']; + $private = $toplevel_item['private']; - $wall = $parent_item['wall']; + $wall = $toplevel_item['wall']; } $pubmail_enabled = defaults($_REQUEST, 'pubmail_enable', false) && !$private; @@ -382,12 +384,8 @@ function item_post(App $a) { $tags = BBCode::getTags($body); - // Add a tag if the parent contact is from ActivityPub or OStatus (This will notify them) - if ($parent && in_array($thr_parent_contact['network'], [Protocol::OSTATUS, Protocol::ACTIVITYPUB])) { - $contact = '@[url=' . $thr_parent_contact['url'] . ']' . $thr_parent_contact['nick'] . '[/url]'; - if (!stripos(implode($tags), '[url=' . $thr_parent_contact['url'] . ']')) { - $tags[] = $contact; - } + if ($thread_parent_id && !\Friendica\Content\Feature::isEnabled($uid, 'explicit_mentions')) { + $tags = item_add_implicit_mentions($tags, $thread_parent_contact, $thread_parent_id); } $tagged = []; @@ -400,7 +398,7 @@ function item_post(App $a) { foreach ($tags as $tag) { $tag_type = substr($tag, 0, 1); - if ($tag_type == '#') { + if ($tag_type == Term::TAG_CHARACTER[Term::HASHTAG]) { continue; } @@ -425,9 +423,9 @@ function item_post(App $a) { $tagged[] = $tag; } // When the forum is private or the forum is addressed with a "!" make the post private - if (is_array($success['contact']) && (!empty($success['contact']['prv']) || ($tag_type == '!'))) { + if (is_array($success['contact']) && (!empty($success['contact']['prv']) || ($tag_type == Term::TAG_CHARACTER[Term::EXCLUSIVE_MENTION]))) { $private_forum = $success['contact']['prv']; - $only_to_forum = ($tag_type == '!'); + $only_to_forum = ($tag_type == Term::TAG_CHARACTER[Term::EXCLUSIVE_MENTION]); $private_id = $success['contact']['id']; $forum_contact = $success['contact']; } elseif (is_array($success['contact']) && !empty($success['contact']['forum']) && @@ -442,7 +440,7 @@ function item_post(App $a) { $original_contact_id = $contact_id; - if (!$parent && count($forum_contact) && ($private_forum || $only_to_forum)) { + if (!$toplevel_item_id && count($forum_contact) && ($private_forum || $only_to_forum)) { // we tagged a forum in a top level post. Now we change the post $private = $private_forum; @@ -595,7 +593,7 @@ function item_post(App $a) { $network = Protocol::DFRN; } - $gravity = ($parent ? GRAVITY_COMMENT : GRAVITY_PARENT); + $gravity = ($toplevel_item_id ? GRAVITY_COMMENT : GRAVITY_PARENT); // even if the post arrived via API we are considering that it // originated on this site by default for determining relayability. @@ -607,12 +605,12 @@ function item_post(App $a) { $origin = $_REQUEST['origin']; } - $notify_type = ($parent ? 'comment-new' : 'wall-new'); + $notify_type = ($toplevel_item_id ? 'comment-new' : 'wall-new'); $uri = ($message_id ? $message_id : Item::newURI($api_source ? $profile_uid : $uid, $guid)); // Fallback so that we alway have a parent uri - if (!$thr_parent_uri || !$parent) { + if (!$thr_parent_uri || !$toplevel_item_id) { $thr_parent_uri = $uri; } @@ -670,7 +668,7 @@ function item_post(App $a) { * 'self' if true indicates the owner is posting on their own wall * If parent is 0 it is a top-level post. */ - $datarray['parent'] = $parent; + $datarray['parent'] = $toplevel_item_id; $datarray['self'] = $self; // This triggers posts via API and the mirror functions @@ -788,7 +786,7 @@ function item_post(App $a) { FileTag::updatePconfig($uid, $categories_old, $categories_new, 'category'); // These notifications are sent if someone else is commenting other your wall - if ($parent) { + if ($toplevel_item_id) { if ($contact_record != $author) { notification([ 'type' => NOTIFY_COMMENT, @@ -804,8 +802,8 @@ function item_post(App $a) { 'source_photo' => $datarray['author-avatar'], 'verb' => ACTIVITY_POST, 'otype' => 'item', - 'parent' => $parent, - 'parent_uri' => $parent_item['uri'] + 'parent' => $toplevel_item_id, + 'parent_uri' => $toplevel_item['uri'] ]); } } else { @@ -962,7 +960,7 @@ function handle_tag(&$body, &$inform, &$str_tags, $profile_uid, $tag, $network = $r = null; //is it a person tag? - if ((strpos($tag, '@') === 0) || (strpos($tag, '!') === 0)) { + if (Term::isType($tag, Term::MENTION, Term::IMPLICIT_MENTION, Term::EXCLUSIVE_MENTION)) { $tag_type = substr($tag, 0, 1); //is it already replaced? if (strpos($tag, '[url=')) { @@ -1099,3 +1097,34 @@ function handle_tag(&$body, &$inform, &$str_tags, $profile_uid, $tag, $network = return ['replaced' => $replaced, 'contact' => $contact]; } + +function item_add_implicit_mentions(array $tags, array $thread_parent_contact, $thread_parent_id) +{ + if (Config::get('system', 'disable_implicit_mentions')) { + // Add a tag if the parent contact is from ActivityPub or OStatus (This will notify them) + if (in_array($thread_parent_contact['network'], [Protocol::OSTATUS, Protocol::ACTIVITYPUB])) { + $contact = Term::TAG_CHARACTER[Term::MENTION] . '[url=' . $thread_parent_contact['url'] . ']' . $thread_parent_contact['nick'] . '[/url]'; + if (!stripos(implode($tags), '[url=' . $thread_parent_contact['url'] . ']')) { + $tags[] = $contact; + } + } + } else { + $implicit_mentions = [ + $thread_parent_contact['url'] => $thread_parent_contact['nick'] + ]; + + $parent_terms = Term::tagArrayFromItemId($thread_parent_id, [Term::MENTION, Term::IMPLICIT_MENTION]); + + foreach ($parent_terms as $parent_term) { + $implicit_mentions[$parent_term['url']] = $parent_term['term']; + } + + foreach ($implicit_mentions as $url => $label) { + if ($url != \Friendica\Model\Profile::getMyURL() && !stripos(implode($tags), '[url=' . $url . ']')) { + $tags[] = Term::TAG_CHARACTER[Term::IMPLICIT_MENTION] . '[url=' . $url . ']' . $label . '[/url]'; + } + } + } + + return $tags; +} \ No newline at end of file From e51f2cea0d821890c70ad8783c489635f7ab8daa Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 22 Feb 2019 23:36:06 -0500 Subject: [PATCH 08/13] Add implicit mentions to explicit mentions default text --- src/Object/Post.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Object/Post.php b/src/Object/Post.php index 85fd7876cb..807c2d2227 100644 --- a/src/Object/Post.php +++ b/src/Object/Post.php @@ -805,7 +805,7 @@ class Post extends BaseObject $text = ''; } - $terms = Term::tagArrayFromItemId($this->getId(), TERM_MENTION); + $terms = Term::tagArrayFromItemId($this->getId(), [Term::MENTION, Term::IMPLICIT_MENTION]); foreach ($terms as $term) { $profile = Contact::getDetailsByURL($term['url']); From fc47a0780168fe4da046b990c1d1b461e0211940 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 22 Feb 2019 23:38:59 -0500 Subject: [PATCH 09/13] Fix Diaspora outgoing implicit mention - Use name instead of nick for implicit mention - Rename $parent to $toplevel_item in Diaspora::constructComment - Use thread parent to retrieve the expected mention instead of the top level item --- src/Protocol/Diaspora.php | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 3687fb9d4b..ca66aa0a67 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -3675,7 +3675,7 @@ class Diaspora && !strstr($body, $profile['addr']) && !strstr($body, $profile_url) ) { - $body = '@[url=' . $profile_url . ']' . $profile['nick'] . '[/url] ' . $body; + $body = '@[url=' . $profile_url . ']' . $profile['name'] . '[/url] ' . $body; } return $body; @@ -3776,7 +3776,7 @@ class Diaspora * @param array $item The item that will be exported * @param array $owner the array of the item owner * - * @return array The data for a comment + * @return array|false The data for a comment * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ private static function constructComment(array $item, array $owner) @@ -3788,30 +3788,40 @@ class Diaspora return $result; } - $parent = Item::selectFirst(['guid', 'author-link'], ['id' => $item["parent"], 'parent' => $item["parent"]]); - if (!DBA::isResult($parent)) { + $toplevel_item = Item::selectFirst(['guid', 'author-link'], ['id' => $item["parent"], 'parent' => $item["parent"]]); + if (!DBA::isResult($toplevel_item)) { + Logger::error('Missing parent conversation item', ['parent' => $item["parent"]]); return false; } + $thread_parent_item = $toplevel_item; + if ($item['thr-parent'] != $item['parent-uri']) { + $thread_parent_item = Item::selectFirst(['guid', 'author-link'], ['uri' => $item['thr-parent'], 'uid' => $item['uid']]); + } + $body = $item["body"]; - if (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions')) { - $body = self::prependParentAuthorMention($body, $parent['author-link']); + if ((empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions')) + && !Config::get('system', 'disable_implicit_mentions') + ) { + $body = self::prependParentAuthorMention($body, $thread_parent_item['author-link']); } $text = html_entity_decode(BBCode::toMarkdown($body)); $created = DateTimeFormat::utc($item["created"], DateTimeFormat::ATOM); - $comment = ["author" => self::myHandle($owner), - "guid" => $item["guid"], - "created_at" => $created, - "parent_guid" => $parent["guid"], - "text" => $text, - "author_signature" => ""]; + $comment = [ + "author" => self::myHandle($owner), + "guid" => $item["guid"], + "created_at" => $created, + "parent_guid" => $toplevel_item["guid"], + "text" => $text, + "author_signature" => "" + ]; // Send the thread parent guid only if it is a threaded comment if ($item['thr-parent'] != $item['parent-uri']) { - $comment['thread_parent_guid'] = self::getGuidFromUri($item['thr-parent'], $item['uid']); + $comment['thread_parent_guid'] = $thread_parent_item['guid']; } Cache::set($cachekey, $comment, Cache::QUARTER_HOUR); From 3ac8576c8e464f989ec8baf4dff4fa9ad99d524a Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 22 Feb 2019 23:42:04 -0500 Subject: [PATCH 10/13] Fix implicit mentions in incoming ActivityPub posts - Use thr-parent instead of parent-uri to retrieve implicit mentions to remove from body - Add potential implicit mentions to 'tags' array for storage in Processor::convertImplicitMentionsInTags() - Add usage of system.disable_implicit_mentions to disable implicit mention behavior --- src/Protocol/ActivityPub/Processor.php | 107 ++++++++++++++++++------- 1 file changed, 80 insertions(+), 27 deletions(-) diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index 68697381b2..b77df1fae8 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -65,7 +65,7 @@ class Processor * @param array $implicit_mentions List of profile URLs to skip * @return string with tags */ - private static function constructTagString($tags, $sensitive, array $implicit_mentions) + private static function constructTagString(array $tags, $sensitive) { if (empty($tags)) { return ''; @@ -73,7 +73,7 @@ class Processor $tag_text = ''; foreach ($tags as $tag) { - if (in_array(defaults($tag, 'type', ''), ['Mention', 'Hashtag']) && !in_array($tag['href'], $implicit_mentions)) { + if (in_array(defaults($tag, 'type', ''), ['Mention', 'Hashtag'])) { if (!empty($tag_text)) { $tag_text .= ','; } @@ -129,7 +129,7 @@ class Processor */ public static function updateItem($activity) { - $item = Item::selectFirst(['uri', 'parent-uri', 'gravity'], ['uri' => $activity['id']]); + $item = Item::selectFirst(['uri', 'thr-parent', 'gravity'], ['uri' => $activity['id']]); if (!DBA::isResult($item)) { Logger::warning('Unknown item', ['uri' => $activity['id']]); return; @@ -144,20 +144,20 @@ class Processor $content = self::replaceEmojis($content, $activity['emojis']); $content = self::convertMentions($content); - $implicit_mentions = []; - if (($item['parent-uri'] != $item['uri']) && ($item['gravity'] == GRAVITY_COMMENT)) { - $parent = Item::selectFirst(['id', 'author-link', 'alias'], ['uri' => $item['parent-uri']]); + if (($item['thr-parent'] != $item['uri']) && ($item['gravity'] == GRAVITY_COMMENT)) { + $parent = Item::selectFirst(['id', 'author-link', 'alias'], ['uri' => $item['thr-parent']]); if (!DBA::isResult($parent)) { - Logger::warning('Unknown parent item.', ['uri' => $item['parent-uri']]); + Logger::warning('Unknown parent item.', ['uri' => $item['thr-parent']]); return; } - $implicit_mentions = self::getImplicitMentionList($parent); - $content = self::removeImplicitMentionsFromBody($content, $implicit_mentions); + $potential_implicit_mentions = self::getImplicitMentionList($parent); + $content = self::removeImplicitMentionsFromBody($content, $potential_implicit_mentions); + $activity['tags'] = self::convertImplicitMentionsInTags($activity['tags'], $potential_implicit_mentions); } $item['body'] = $content; - $item['tag'] = self::constructTagString($activity['tags'], $activity['sensitive'], $implicit_mentions); + $item['tag'] = self::constructTagString($activity['tags'], $activity['sensitive']); Item::update($item, ['uri' => $activity['id']]); } @@ -173,7 +173,7 @@ class Processor { $item = []; $item['verb'] = ACTIVITY_POST; - $item['parent-uri'] = $activity['reply-to-id']; + $item['thr-parent'] = $activity['reply-to-id']; if ($activity['reply-to-id'] == $activity['id']) { $item['gravity'] = GRAVITY_PARENT; @@ -220,7 +220,7 @@ class Processor { $item = []; $item['verb'] = $verb; - $item['parent-uri'] = $activity['object_id']; + $item['thr-parent'] = $activity['object_id']; $item['gravity'] = GRAVITY_ACTIVITY; $item['object-type'] = ACTIVITY_OBJ_NOTE; @@ -275,8 +275,8 @@ class Processor { /// @todo What to do with $activity['context']? - if (($item['gravity'] != GRAVITY_PARENT) && !Item::exists(['uri' => $item['parent-uri']])) { - Logger::log('Parent ' . $item['parent-uri'] . ' not found, message will be discarded.', Logger::DEBUG); + if (($item['gravity'] != GRAVITY_PARENT) && !Item::exists(['uri' => $item['thr-parent']])) { + Logger::log('Parent ' . $item['thr-parent'] . ' not found, message will be discarded.', Logger::DEBUG); return; } @@ -299,21 +299,20 @@ class Processor $content = self::replaceEmojis($content, $activity['emojis']); $content = self::convertMentions($content); - $implicit_mentions = []; - - if (($item['parent-uri'] != $item['uri']) && ($item['gravity'] == GRAVITY_COMMENT)) { + if (($item['thr-parent'] != $item['uri']) && ($item['gravity'] == GRAVITY_COMMENT)) { $item_private = !in_array(0, $activity['item_receiver']); - $parent = Item::selectFirst(['id', 'private', 'author-link', 'alias'], ['uri' => $item['parent-uri']]); + $parent = Item::selectFirst(['id', 'private', 'author-link', 'alias'], ['uri' => $item['thr-parent']]); if (!DBA::isResult($parent)) { return; } if ($item_private && !$parent['private']) { - Logger::log('Item ' . $item['uri'] . ' is private but the parent ' . $item['parent-uri'] . ' is not. So we drop it.'); + Logger::warning('Item is private but the parent is not. Dropping.', ['item-uri' => $item['uri'], 'thr-parent' => $item['thr-parent']]); return; } - $implicit_mentions = self::getImplicitMentionList($parent); - $content = self::removeImplicitMentionsFromBody($content, $implicit_mentions); + $potential_implicit_mentions = self::getImplicitMentionList($parent); + $content = self::removeImplicitMentionsFromBody($content, $potential_implicit_mentions); + $activity['tags'] = self::convertImplicitMentionsInTags($activity['tags'], $potential_implicit_mentions); } $item['created'] = $activity['published']; @@ -333,7 +332,7 @@ class Processor $item['coord'] = $item['latitude'] . ' ' . $item['longitude']; } - $item['tag'] = self::constructTagString($activity['tags'], $activity['sensitive'], $implicit_mentions); + $item['tag'] = self::constructTagString($activity['tags'], $activity['sensitive']); $item['app'] = $activity['generator']; $item['plink'] = defaults($activity, 'alternate-url', $item['uri']); @@ -662,10 +661,18 @@ class Processor */ private static function getImplicitMentionList(array $parent) { - $parent_terms = Term::tagArrayFromItemId($parent['id'], [TERM_MENTION]); + if (Config::get('system', 'disable_implicit_mentions')) { + return []; + } + + $parent_terms = Term::tagArrayFromItemId($parent['id'], [Term::MENTION, Term::IMPLICIT_MENTION]); + + $parent_author = Contact::getDetailsByURL($parent['author-link'], 0); $implicit_mentions = [ - $parent['author-link'] + $parent_author['url'], + $parent_author['nurl'], + $parent_author['alias'], ]; if ($parent['alias']) { @@ -688,10 +695,10 @@ class Processor * Strips from the body prepended implicit mentions * * @param string $body - * @param array $implicit_mentions List of profile URLs + * @param array $potential_mentions * @return string */ - private static function removeImplicitMentionsFromBody($body, array $implicit_mentions) + private static function removeImplicitMentionsFromBody($body, array $potential_mentions) { if (Config::get('system', 'disable_implicit_mentions')) { return $body; @@ -701,7 +708,7 @@ class Processor // Extract one prepended mention at a time from the body while(preg_match('#^(@\[url=([^\]]+)].*?\[\/url]\s)(.*)#mis', $body, $matches)) { - if (!in_array($matches[2], $implicit_mentions) ) { + if (!in_array($matches[2], $potential_mentions) ) { $kept_mentions[] = $matches[1]; } @@ -713,4 +720,50 @@ class Processor return implode('', $kept_mentions); } + + private static function convertImplicitMentionsInTags($activity_tags, array $potential_mentions) + { + if (Config::get('system', 'disable_implicit_mentions')) { + return $activity_tags; + } + + foreach ($activity_tags as $index => $tag) { + if (in_array($tag['href'], $potential_mentions)) { + $activity_tags[$index]['name'] = preg_replace( + '/' . preg_quote(Term::TAG_CHARACTER[Term::MENTION], '/') . '/', + Term::TAG_CHARACTER[Term::IMPLICIT_MENTION], + $activity_tags[$index]['name'], + 1 + ); + } + } + + return $activity_tags; + } + + public static function testImplicitMentions($item, $source) + { + $parent = Item::selectFirst(['id', 'guid', 'author-link', 'alias'], ['uri' => $item['thr-parent']]); + + $implicit_mentions = self::getImplicitMentionList($parent); + var_dump($implicit_mentions); + + $object = json_decode($source, true)['object']; + var_dump($object); + + $content = HTML::toBBCode($object['content']); + $content = self::convertMentions($content); + + $activity = [ + 'tags' => $object['tag'], + 'content' => $content + ]; + + var_dump($activity); + + $activity['content'] = Processor::removeImplicitMentionsFromBody($activity['content'], $implicit_mentions); + $activity['tags'] = Processor::convertImplicitMentionsInTags($activity['tags'], $implicit_mentions); + + return $activity; + } } From cb78e7785079726b31ed8620043bfaf11a78462d Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 22 Feb 2019 23:43:32 -0500 Subject: [PATCH 11/13] Fix implicit mentions in outgoing ActivityPub posts - Add usage of system.disable_implicit_mentions to disable implicit mention behavior - Add usage of item own implicit mentions to be prepended to the outgoing body --- src/Protocol/ActivityPub/Transmitter.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index ebd32786a8..7d33fb2dc5 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -343,7 +343,7 @@ class Transmitter $actor_profile = APContact::getByURL($item['author-link']); } - $terms = Term::tagArrayFromItemId($item['id'], TERM_MENTION); + $terms = Term::tagArrayFromItemId($item['id'], [Term::MENTION, Term::IMPLICIT_MENTION]); if (!$item['private']) { $data = array_merge($data, self::fetchPermissionBlockFromConversation($item)); @@ -807,12 +807,12 @@ class Transmitter { $tags = []; - $terms = Term::tagArrayFromItemId($item['id']); + $terms = Term::tagArrayFromItemId($item['id'], [Term::HASHTAG, Term::MENTION, Term::IMPLICIT_MENTION]); foreach ($terms as $term) { - if ($term['type'] == TERM_HASHTAG) { + if ($term['type'] == Term::HASHTAG) { $url = System::baseUrl() . '/search?tag=' . urlencode($term['term']); $tags[] = ['type' => 'Hashtag', 'href' => $url, 'name' => '#' . $term['term']]; - } elseif ($term['type'] == TERM_MENTION) { + } elseif ($term['type'] == Term::MENTION || $term['type'] == Term::IMPLICIT_MENTION) { $contact = Contact::getDetailsByURL($term['url']); if (!empty($contact['addr'])) { $mention = '@' . $contact['addr']; @@ -1439,6 +1439,10 @@ class Transmitter private static function prependMentions($body, array $permission_block) { + if (Config::get('system', 'disable_implicit_mentions')) { + return $body; + } + $mentions = []; foreach ($permission_block['to'] as $profile_url) { From 67aa1888309e83648be62988b10e757c14a2e2a9 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 23 Feb 2019 09:25:21 -0500 Subject: [PATCH 12/13] Improve Logger calls - Add context in various calls - Remove deprecated Logger::log call in Processor --- mod/item.php | 2 +- src/Database/DBA.php | 2 +- src/Model/Item.php | 32 +++++++++++++++++++------- src/Model/Term.php | 2 ++ src/Protocol/ActivityPub/Processor.php | 2 +- 5 files changed, 29 insertions(+), 11 deletions(-) diff --git a/mod/item.php b/mod/item.php index 122f62c4e5..b126c4825b 100644 --- a/mod/item.php +++ b/mod/item.php @@ -1127,4 +1127,4 @@ function item_add_implicit_mentions(array $tags, array $thread_parent_contact, $ } return $tags; -} \ No newline at end of file +} diff --git a/src/Database/DBA.php b/src/Database/DBA.php index 326fb0771d..832f0a444f 100644 --- a/src/Database/DBA.php +++ b/src/Database/DBA.php @@ -425,7 +425,7 @@ class DBA if ((substr_count($sql, '?') != count($args)) && (count($args) > 0)) { // Question: Should we continue or stop the query here? - Logger::error('Parameter mismatch. Query "'.$sql.'" - Parameters '.print_r($args, true)); + Logger::warning('Query parameters mismatch.', ['query' => $sql, 'args' => $args, 'callstack' => System::callstack()]); } $sql = self::cleanQuery($sql); diff --git a/src/Model/Item.php b/src/Model/Item.php index e4d18c9a74..16a3b07b53 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -1336,7 +1336,11 @@ class Item extends BaseObject $expire_date = time() - ($expire_interval * 86400); $created_date = strtotime($item['created']); if ($created_date < $expire_date) { - Logger::notice('item-store: item created ('.date('c', $created_date).') before expiration time ('.date('c', $expire_date).'). ignored. ' . print_r($item,true)); + Logger::notice('Item created before expiration interval.', [ + 'created' => date('c', $created_date), + 'expired' => date('c', $expire_date), + '$item' => $item + ]); return 0; } } @@ -1354,7 +1358,13 @@ class Item extends BaseObject if (DBA::isResult($existing)) { // We only log the entries with a different user id than 0. Otherwise we would have too many false positives if ($uid != 0) { - Logger::notice("Item with uri ".$item['uri']." already existed for user ".$uid." with id ".$existing["id"]." target network ".$existing["network"]." - new network: ".$item['network']); + Logger::notice('Item already existed for user', [ + 'uri' => $item['uri'], + 'uid' => $uid, + 'network' => $item['network'], + 'existing_id' => $existing["id"], + 'existing_network' => $existing["network"] + ]); } return $existing["id"]; @@ -1432,7 +1442,7 @@ class Item extends BaseObject $item['author-id'] = defaults($item, 'author-id', Contact::getIdForURL($item["author-link"], 0, false, $default)); if (Contact::isBlocked($item["author-id"])) { - Logger::notice('Contact '.$item["author-id"].' is blocked, item '.$item["uri"].' will not be stored'); + Logger::notice('Author is blocked node-wide', ['author-link' => $item["author-link"], 'item-uri' => $item["uri"]]); return 0; } @@ -1442,21 +1452,27 @@ class Item extends BaseObject $item['owner-id'] = defaults($item, 'owner-id', Contact::getIdForURL($item["owner-link"], 0, false, $default)); if (Contact::isBlocked($item["owner-id"])) { - Logger::notice('Contact '.$item["owner-id"].' is blocked, item '.$item["uri"].' will not be stored'); + Logger::notice('Owner is blocked node-wide', ['owner-link' => $item["owner-link"], 'item-uri' => $item["uri"]]); return 0; } if ($item['network'] == Protocol::PHANTOM) { - Logger::notice('Missing network. Called by: '.System::callstack(), Logger::DEBUG); - $item['network'] = Protocol::DFRN; - Logger::notice("Set network to " . $item["network"] . " for " . $item["uri"], Logger::DEBUG); + Logger::notice('Missing network, setting to {network}.', [ + 'uri' => $item["uri"], + 'network' => $item['network'], + 'callstack' => System::callstack() + ]); } // Checking if there is already an item with the same guid $condition = ['guid' => $item['guid'], 'network' => $item['network'], 'uid' => $item['uid']]; if (self::exists($condition)) { - Logger::notice('Found already existing item with guid '.$item['guid'].' for user '.$item['uid'].' on network '.$item['network']); + Logger::notice('Found already existing item', [ + 'guid' => $item['guid'], + 'uid' => $item['uid'], + 'network' => $item['network'] + ]); return 0; } diff --git a/src/Model/Term.php b/src/Model/Term.php index 5266627aca..36876368df 100644 --- a/src/Model/Term.php +++ b/src/Model/Term.php @@ -204,6 +204,8 @@ class Term $type = self::HASHTAG; $term = $tag; $link = ''; + + Logger::notice('Unknown term type', ['tag' => $tag]); } if (DBA::exists('term', ['uid' => $item['uid'], 'otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'term' => $term, 'type' => $type])) { diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index b77df1fae8..2661444e0c 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -276,7 +276,7 @@ class Processor /// @todo What to do with $activity['context']? if (($item['gravity'] != GRAVITY_PARENT) && !Item::exists(['uri' => $item['thr-parent']])) { - Logger::log('Parent ' . $item['thr-parent'] . ' not found, message will be discarded.', Logger::DEBUG); + Logger::info('Parent not found, message will be discarded.', ['thr-parent' => $item['thr-parent']]); return; } From f4745c5936947197935e0c57ee4b80058626042c Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 23 Feb 2019 09:25:37 -0500 Subject: [PATCH 13/13] Add doc to Model\Term --- src/Model/Term.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Model/Term.php b/src/Model/Term.php index 36876368df..58c3223659 100644 --- a/src/Model/Term.php +++ b/src/Model/Term.php @@ -1,6 +1,6 @@