X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModel%2FItem.php;h=b6f8ffa3d1665e5950aa3b69e9dbb05a868fea50;hb=24a82110fd45a8422efee0677b7acc214aa66e9a;hp=a6036e6bdbe8c458e25f7e1dda44a948e331b294;hpb=c6aa42dd4ee431f47eef690e980e429e934400df;p=friendica.git diff --git a/src/Model/Item.php b/src/Model/Item.php index a6036e6bdb..b6f8ffa3d1 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -31,6 +31,7 @@ use Friendica\Core\Session; use Friendica\Core\System; use Friendica\Core\Worker; use Friendica\Database\DBA; +use Friendica\Database\DBStructure; use Friendica\DI; use Friendica\Model\Post\Category; use Friendica\Protocol\Activity; @@ -72,14 +73,14 @@ class Item 'event-id', 'event-created', 'event-edited', 'event-start', 'event-finish', 'event-summary', 'event-desc', 'event-location', 'event-type', 'event-nofinish', 'event-adjust', 'event-ignore', 'event-id', - 'delivery_queue_count', 'delivery_queue_done', 'delivery_queue_failed', 'activity' + 'delivery_queue_count', 'delivery_queue_done', 'delivery_queue_failed' ]; // Field list that is used to deliver items via the protocols const DELIVER_FIELDLIST = ['uid', 'id', 'parent', 'uri-id', 'uri', 'thr-parent', 'parent-uri', 'guid', 'parent-guid', 'created', 'edited', 'verb', 'object-type', 'object', 'target', 'private', 'title', 'body', 'location', 'coord', 'app', - 'attach', 'deleted', 'extid', 'post-type', + 'attach', 'deleted', 'extid', 'post-type', 'gravity', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'author-id', 'author-link', 'owner-link', 'contact-uid', 'signed_text', 'signature', 'signer', 'network']; @@ -118,8 +119,22 @@ class Item const PRIVATE = 1; const UNLISTED = 2; + const TABLES = ['item', 'user-item', 'item-content', 'post-delivery-data', 'diaspora-interaction']; + private static $legacy_mode = null; + private static function getItemFields() + { + $definition = DBStructure::definition('', false); + + $postfields = []; + foreach (self::TABLES as $table) { + $postfields[$table] = array_keys($definition[$table]['fields']); + } + + return $postfields; + } + public static function isLegacyMode() { if (is_null(self::$legacy_mode)) { @@ -285,6 +300,10 @@ class Item } } + if (array_key_exists('vid', $row) && is_null($row['vid']) && !empty($row['verb'])) { + $row['vid'] = Verb::getID($row['verb']); + } + if (!array_key_exists('verb', $row) || in_array($row['verb'], ['', Activity::POST, Activity::SHARE])) { // Build the file string out of the term entries if (array_key_exists('file', $row) && empty($row['file'])) { @@ -668,7 +687,8 @@ class Item $fields['parent-item'] = ['guid' => 'parent-guid', 'network' => 'parent-network']; - $fields['parent-item-author'] = ['url' => 'parent-author-link', 'name' => 'parent-author-name']; + $fields['parent-item-author'] = ['url' => 'parent-author-link', 'name' => 'parent-author-name', + 'network' => 'parent-author-network']; $fields['event'] = ['created' => 'event-created', 'edited' => 'event-edited', 'start' => 'event-start','finish' => 'event-finish', @@ -782,12 +802,12 @@ class Item $joins .= " LEFT JOIN `permissionset` ON `permissionset`.`id` = `item`.`psid`"; } - if ((strpos($sql_commands, "`parent-item`.") !== false) || (strpos($sql_commands, "`parent-author`.") !== false)) { + if ((strpos($sql_commands, "`parent-item`.") !== false) || (strpos($sql_commands, "`parent-item-author`.") !== false)) { $joins .= " STRAIGHT_JOIN `item` AS `parent-item` ON `parent-item`.`id` = `item`.`parent`"; - } - if (strpos($sql_commands, "`parent-item-author`.") !== false) { - $joins .= " STRAIGHT_JOIN `contact` AS `parent-item-author` ON `parent-item-author`.`id` = `parent-item`.`author-id`"; + if (strpos($sql_commands, "`parent-item-author`.") !== false) { + $joins .= " STRAIGHT_JOIN `contact` AS `parent-item-author` ON `parent-item-author`.`id` = `parent-item`.`author-id`"; + } } return $joins; @@ -1066,7 +1086,7 @@ class Item // clean up categories and tags so they don't end up as orphans - $matches = false; + $matches = []; $cnt = preg_match_all('/<(.*?)>/', $item['file'], $matches, PREG_SET_ORDER); if ($cnt) { @@ -1075,7 +1095,7 @@ class Item } } - $matches = false; + $matches = []; $cnt = preg_match_all('/\[(.*?)\]/', $item['file'], $matches, PREG_SET_ORDER); @@ -1534,9 +1554,7 @@ class Item } // Update the contact relations - if ($item['author-id'] != $parent['author-id']) { - DBA::update('contact-relation', ['last-interaction' => $item['created']], ['cid' => $parent['author-id'], 'relation-cid' => $item['author-id']], true); - } + ContactRelation::store($parent['author-id'], $item['author-id'], $item['created']); } return $item; @@ -1567,6 +1585,8 @@ class Item public static function insert($item, $notify = false, $dontcache = false) { + $structure = self::getItemFields(); + $orig_item = $item; $priority = PRIORITY_HIGH; @@ -1675,11 +1695,15 @@ class Item $default = ['url' => $item['author-link'], 'name' => $item['author-name'], 'photo' => $item['author-avatar'], 'network' => $item['network']]; - $item['author-id'] = ($item['author-id'] ?? 0) ?: Contact::getIdForURL($item['author-link'], 0, false, $default); + $item['author-id'] = ($item['author-id'] ?? 0) ?: Contact::getIdForURL($item['author-link'], 0, null, $default); $default = ['url' => $item['owner-link'], 'name' => $item['owner-name'], 'photo' => $item['owner-avatar'], 'network' => $item['network']]; - $item['owner-id'] = ($item['owner-id'] ?? 0) ?: Contact::getIdForURL($item['owner-link'], 0, false, $default); + $item['owner-id'] = ($item['owner-id'] ?? 0) ?: Contact::getIdForURL($item['owner-link'], 0, null, $default); + + // Ensure that there is an avatar cache + Contact::checkAvatarCache($item['author-id']); + Contact::checkAvatarCache($item['owner-id']); // The contact-id should be set before "self::insert" was called - but there seems to be issues sometimes $item["contact-id"] = self::contactId($item); @@ -1776,8 +1800,8 @@ class Item // Check for hashtags in the body and repair or add hashtag links - self::setHashtags($item); - + $item['body'] = self::setHashtags($item['body']); + // Fill the cache field self::putInCache($item); @@ -1796,12 +1820,14 @@ class Item } $body = $item['body']; - + // We just remove everything that is content foreach (array_merge(self::CONTENT_FIELDLIST, self::MIXED_CONTENT_FIELDLIST) as $field) { unset($item[$field]); } + unset($item['activity']); + // Filling item related side tables // Diaspora signature @@ -1831,7 +1857,14 @@ class Item if (!Tag::existsForPost($item['uri-id'])) { Tag::storeFromBody($item['uri-id'], $body); } - + + // Remove all fields that aren't part of the item table + foreach ($item as $field => $value) { + if (!in_array($field, $structure['item'])) { + unset($item[$field]); + } + } + $ret = DBA::insert('item', $item); // When the item was successfully stored we fetch the ID of the item. @@ -1933,6 +1966,9 @@ class Item check_user_notification($current_post); + // Distribute items to users who subscribed to their tags + self::distributeByTags($item); + $transmit = $notify || ($item['visible'] && ($parent_origin || $item['origin'])); if ($transmit) { @@ -1952,6 +1988,24 @@ class Item return $current_post; } + /** + * Distribute the given item to users who subscribed to their tags + * + * @param array $item Processed item + */ + private static function distributeByTags(array $item) + { + if (($item['uid'] != 0) || ($item['gravity'] != GRAVITY_PARENT) || !in_array($item['network'], Protocol::FEDERATED)) { + return; + } + + $uids = Tag::getUIDListByURIId($item['uri-id']); + foreach ($uids as $uid) { + $stored = self::storeForUserByUriId($item['uri-id'], $uid); + Logger::info('Stored item for users', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'stored' => $stored]); + } + } + /** * Insert a new item content entry * @@ -2015,9 +2069,7 @@ class Item } if (empty($fields)) { - // when there are no fields at all, just use the condition - // This is to ensure that we always store content. - $fields = $condition; + return; } DBA::update('item-content', $fields, $condition, true); @@ -2050,13 +2102,6 @@ class Item $origin = $item['origin']; - unset($item['id']); - unset($item['parent']); - unset($item['mention']); - unset($item['wall']); - unset($item['origin']); - unset($item['starred']); - $users = []; /// @todo add a field "pcid" in the contact table that referrs to the public contact id. @@ -2088,7 +2133,7 @@ class Item DBA::close($contacts); if (!empty($owner['alias'])) { - $condition = ['url' => $owner['alias'], 'rel' => [Contact::SHARING, Contact::FRIEND]]; + $condition = ['nurl' => Strings::normaliseLink($owner['alias']), 'rel' => [Contact::SHARING, Contact::FRIEND]]; $contacts = DBA::select('contact', ['uid'], $condition); while ($contact = DBA::fetch($contacts)) { if ($contact['uid'] == 0) { @@ -2116,33 +2161,76 @@ class Item if ($origin_uid == $uid) { $item['diaspora_signed_text'] = $signed_text; } - self::storeForUser($itemid, $item, $uid); + self::storeForUser($item, $uid); } } /** - * Store public items for the receivers + * Store a public item defined by their URI-ID for the given users + * + * @param integer $uri_id URI-ID of the given item + * @param integer $uid The user that will receive the item entry + * @return integer stored item id + */ + public static function storeForUserByUriId(int $uri_id, int $uid) + { + $item = self::selectFirst(self::ITEM_FIELDLIST, ['uri-id' => $uri_id, 'uid' => 0]); + if (!DBA::isResult($item)) { + return 0; + } + + if (($item['private'] == self::PRIVATE) || !in_array($item['network'], Protocol::FEDERATED)) { + Logger::notice('Item is private or not from a federated network. It will not be stored for the user.', ['uri-id' => $uri_id, 'uid' => $uid, 'private' => $item['private'], 'network' => $item['network']]); + return 0; + } + + $stored = self::storeForUser($item, $uid); + Logger::info('Public item stored for user', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'stored' => $stored]); + return $stored; + } + + /** + * Store a public item array for the given users * - * @param integer $itemid Item ID that should be added * @param array $item The item entry that will be stored * @param integer $uid The user that will receive the item entry + * @return integer stored item id * @throws \Exception */ - private static function storeForUser($itemid, $item, $uid) + private static function storeForUser(array $item, int $uid) { + if (self::exists(['uri-id' => $item['uri-id'], 'uid' => $uid])) { + Logger::info('Item already exists', ['uri-id' => $item['uri-id'], 'uid' => $uid]); + return 0; + } + + unset($item['id']); + unset($item['parent']); + unset($item['mention']); + unset($item['starred']); + unset($item['unseen']); + unset($item['psid']); + $item['uid'] = $uid; $item['origin'] = 0; $item['wall'] = 0; - if ($item['uri'] == $item['parent-uri']) { - $item['contact-id'] = Contact::getIdForURL($item['owner-link'], $uid); + + if ($item['gravity'] == GRAVITY_PARENT) { + $contact = Contact::getByURLForUser($item['owner-link'], $uid, false, ['id']); } else { - $item['contact-id'] = Contact::getIdForURL($item['author-link'], $uid); + $contact = Contact::getByURLForUser($item['author-link'], $uid, false, ['id']); } - if (empty($item['contact-id'])) { + if (!empty($contact['id'])) { + $item['contact-id'] = $contact['id']; + } else { + // Shouldn't happen at all + Logger::warning('contact-id could not be fetched', ['uid' => $uid, 'item' => $item]); $self = DBA::selectFirst('contact', ['id'], ['self' => true, 'uid' => $uid]); if (!DBA::isResult($self)) { - return; + // Shouldn't happen even less + Logger::warning('self contact could not be fetched', ['uid' => $uid, 'item' => $item]); + return 0; } $item['contact-id'] = $self['id']; } @@ -2150,7 +2238,7 @@ class Item /// @todo Handling of "event-id" $notify = false; - if ($item['uri'] == $item['parent-uri']) { + if ($item['gravity'] == GRAVITY_PARENT) { $contact = DBA::selectFirst('contact', [], ['id' => $item['contact-id'], 'self' => false]); if (DBA::isResult($contact)) { $notify = self::isRemoteSelf($contact, $item); @@ -2160,10 +2248,11 @@ class Item $distributed = self::insert($item, $notify, true); if (!$distributed) { - Logger::log("Distributed public item " . $itemid . " for user " . $uid . " wasn't stored", Logger::DEBUG); + Logger::info("Distributed public item wasn't stored", ['uri-id' => $item['uri-id'], 'user' => $uid]); } else { - Logger::log("Distributed public item " . $itemid . " for user " . $uid . " with id " . $distributed, Logger::DEBUG); + Logger::info('Distributed public item was stored', ['uri-id' => $item['uri-id'], 'user' => $uid, 'stored' => $distributed]); } + return $distributed; } /** @@ -2226,7 +2315,7 @@ class Item $public_shadow = self::insert($item, false, true); - Logger::log("Stored public shadow for thread ".$itemid." under id ".$public_shadow, Logger::DEBUG); + Logger::info('Stored public shadow', ['thread' => $itemid, 'id' => $public_shadow]); } } @@ -2284,7 +2373,7 @@ class Item $public_shadow = self::insert($item, false, true); - Logger::log("Stored public shadow for comment ".$item['uri']." under id ".$public_shadow, Logger::DEBUG); + Logger::info('Stored public shadow', ['uri' => $item['uri'], 'id' => $public_shadow]); // If this was a comment to a Diaspora post we don't get our comment back. // This means that we have to distribute the comment by ourselves. @@ -2386,7 +2475,7 @@ class Item } /// @todo On private posts we could obfuscate the date - $update = ($arr['private'] != self::PRIVATE); + $update = ($arr['private'] != self::PRIVATE) || in_array($arr['network'], Protocol::FEDERATED); // Is it a forum? Then we don't care about the rules from above if (!$update && in_array($arr["network"], [Protocol::ACTIVITYPUB, Protocol::DFRN]) && ($arr["parent-uri"] === $arr["uri"])) { @@ -2404,98 +2493,83 @@ class Item } else { $condition = ['id' => $arr['contact-id'], 'self' => false]; } - DBA::update('contact', ['success_update' => $arr['received'], 'last-item' => $arr['received']], $condition); + DBA::update('contact', ['failed' => false, 'success_update' => $arr['received'], 'last-item' => $arr['received']], $condition); } // Now do the same for the system wide contacts with uid=0 if ($arr['private'] != self::PRIVATE) { - DBA::update('contact', ['success_update' => $arr['received'], 'last-item' => $arr['received']], + DBA::update('contact', ['failed' => false, 'success_update' => $arr['received'], 'last-item' => $arr['received']], ['id' => $arr['owner-id']]); if ($arr['owner-id'] != $arr['author-id']) { - DBA::update('contact', ['success_update' => $arr['received'], 'last-item' => $arr['received']], + DBA::update('contact', ['failed' => false, 'success_update' => $arr['received'], 'last-item' => $arr['received']], ['id' => $arr['author-id']]); } } } - public static function setHashtags(&$item) + public static function setHashtags($body) { - $tags = BBCode::getTags($item["body"]); + $body = BBCode::performWithEscapedTags($body, ['noparse', 'pre', 'code'], function ($body) { + $tags = BBCode::getTags($body); - // No hashtags? - if (!count($tags)) { - return false; - } + // No hashtags? + if (!count($tags)) { + return $body; + } - // What happens in [code], stays in [code]! - // escape the # and the [ - // hint: we will also get in trouble with #tags, when we want markdown in posts -> ### Headline 3 - $item["body"] = preg_replace_callback("/\[code(.*?)\](.*?)\[\/code\]/ism", - function ($match) { - // we truly ESCape all # and [ to prevent gettin weird tags in [code] blocks - $find = ['#', '[']; - $replace = [chr(27).'sharp', chr(27).'leftsquarebracket']; - return ("[code" . $match[1] . "]" . str_replace($find, $replace, $match[2]) . "[/code]"); - }, $item["body"]); - - // This sorting is important when there are hashtags that are part of other hashtags - // Otherwise there could be problems with hashtags like #test and #test2 - // Because of this we are sorting from the longest to the shortest tag. - usort($tags, function($a, $b) { - return strlen($b) <=> strlen($a); - }); + // This sorting is important when there are hashtags that are part of other hashtags + // Otherwise there could be problems with hashtags like #test and #test2 + // Because of this we are sorting from the longest to the shortest tag. + usort($tags, function ($a, $b) { + return strlen($b) <=> strlen($a); + }); - $URLSearchString = "^\[\]"; + $URLSearchString = "^\[\]"; - // All hashtags should point to the home server if "local_tags" is activated - if (DI::config()->get('system', 'local_tags')) { - $item["body"] = preg_replace("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", - "#[url=".DI::baseUrl()."/search?tag=$2]$2[/url]", $item["body"]); - } + // All hashtags should point to the home server if "local_tags" is activated + if (DI::config()->get('system', 'local_tags')) { + $body = preg_replace("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", + "#[url=" . DI::baseUrl() . "/search?tag=$2]$2[/url]", $body); + } - // mask hashtags inside of url, bookmarks and attachments to avoid urls in urls - $item["body"] = preg_replace_callback("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", - function ($match) { - return ("[url=" . str_replace("#", "#", $match[1]) . "]" . str_replace("#", "#", $match[2]) . "[/url]"); - }, $item["body"]); + // mask hashtags inside of url, bookmarks and attachments to avoid urls in urls + $body = preg_replace_callback("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", + function ($match) { + return ("[url=" . str_replace("#", "#", $match[1]) . "]" . str_replace("#", "#", $match[2]) . "[/url]"); + }, $body); - $item["body"] = preg_replace_callback("/\[bookmark\=([$URLSearchString]*)\](.*?)\[\/bookmark\]/ism", - function ($match) { - return ("[bookmark=" . str_replace("#", "#", $match[1]) . "]" . str_replace("#", "#", $match[2]) . "[/bookmark]"); - }, $item["body"]); + $body = preg_replace_callback("/\[bookmark\=([$URLSearchString]*)\](.*?)\[\/bookmark\]/ism", + function ($match) { + return ("[bookmark=" . str_replace("#", "#", $match[1]) . "]" . str_replace("#", "#", $match[2]) . "[/bookmark]"); + }, $body); - $item["body"] = preg_replace_callback("/\[attachment (.*)\](.*?)\[\/attachment\]/ism", - function ($match) { - return ("[attachment " . str_replace("#", "#", $match[1]) . "]" . $match[2] . "[/attachment]"); - }, $item["body"]); + $body = preg_replace_callback("/\[attachment (.*)\](.*?)\[\/attachment\]/ism", + function ($match) { + return ("[attachment " . str_replace("#", "#", $match[1]) . "]" . $match[2] . "[/attachment]"); + }, $body); - // Repair recursive urls - $item["body"] = preg_replace("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", - "#$2", $item["body"]); + // Repair recursive urls + $body = preg_replace("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", + "#$2", $body); - foreach ($tags as $tag) { - if ((strpos($tag, '#') !== 0) || strpos($tag, '[url=') || strlen($tag) < 2 || $tag[1] == '#') { - continue; - } + foreach ($tags as $tag) { + if ((strpos($tag, '#') !== 0) || strpos($tag, '[url=') || strlen($tag) < 2 || $tag[1] == '#') { + continue; + } - $basetag = str_replace('_',' ',substr($tag,1)); - $newtag = '#[url=' . DI::baseUrl() . '/search?tag=' . $basetag . ']' . $basetag . '[/url]'; + $basetag = str_replace('_', ' ', substr($tag, 1)); + $newtag = '#[url=' . DI::baseUrl() . '/search?tag=' . $basetag . ']' . $basetag . '[/url]'; - $item["body"] = str_replace($tag, $newtag, $item["body"]); - } + $body = str_replace($tag, $newtag, $body); + } - // Convert back the masked hashtags - $item["body"] = str_replace("#", "#", $item["body"]); + // Convert back the masked hashtags + $body = str_replace("#", "#", $body); + + return $body; + }); - // Remember! What happens in [code], stays in [code] - // roleback the # and [ - $item["body"] = preg_replace_callback("/\[code(.*?)\](.*?)\[\/code\]/ism", - function ($match) { - // we truly unESCape all sharp and leftsquarebracket - $find = [chr(27).'sharp', chr(27).'leftsquarebracket']; - $replace = ['#', '[']; - return ("[code" . $match[1] . "]" . str_replace($find, $replace, $match[2]) . "[/code]"); - }, $item["body"]); + return $body; } /** @@ -2610,29 +2684,29 @@ class Item // Prevent the forwarding of posts that are forwarded if (!empty($datarray["extid"]) && ($datarray["extid"] == Protocol::DFRN)) { - Logger::log('Already forwarded', Logger::DEBUG); + Logger::info('Already forwarded'); return false; } // Prevent to forward already forwarded posts if ($datarray["app"] == DI::baseUrl()->getHostname()) { - Logger::log('Already forwarded (second test)', Logger::DEBUG); + Logger::info('Already forwarded (second test)'); return false; } // Only forward posts if ($datarray["verb"] != Activity::POST) { - Logger::log('No post', Logger::DEBUG); + Logger::info('No post'); return false; } if (($contact['network'] != Protocol::FEED) && ($datarray['private'] == self::PRIVATE)) { - Logger::log('Not public', Logger::DEBUG); + Logger::info('Not public'); return false; } $datarray2 = $datarray; - Logger::log('remote-self start - Contact '.$contact['url'].' - '.$contact['remote_self'].' Item '.print_r($datarray, true), Logger::DEBUG); + Logger::info('remote-self start', ['contact' => $contact['url'], 'remote_self'=> $contact['remote_self'], 'item' => $datarray]); if ($contact['remote_self'] == 2) { $self = DBA::selectFirst('contact', ['id', 'name', 'url', 'thumb'], ['uid' => $contact['uid'], 'self' => true]); @@ -2671,7 +2745,7 @@ class Item if ($contact['network'] != Protocol::FEED) { // Store the original post $result = self::insert($datarray2); - Logger::log('remote-self post original item - Contact '.$contact['url'].' return '.$result.' Item '.print_r($datarray2, true), Logger::DEBUG); + Logger::info('remote-self post original item', ['contact' => $contact['url'], 'result'=> $result, 'item' => $datarray2]); } else { $datarray["app"] = "Feed"; $result = true; @@ -2703,7 +2777,7 @@ class Item return $s; } - Logger::log('check for photos', Logger::DEBUG); + Logger::info('check for photos'); $site = substr(DI::baseUrl(), strpos(DI::baseUrl(), '://')); $orig_body = $s; @@ -2717,7 +2791,7 @@ class Item $img_st_close++; // make it point to AFTER the closing bracket $image = substr($orig_body, $img_start + $img_st_close, $img_len); - Logger::log('found photo ' . $image, Logger::DEBUG); + Logger::info('found photo', ['image' => $image]); if (stristr($image, $site . '/photo/')) { // Only embed locally hosted photos @@ -2756,7 +2830,7 @@ class Item $photo_img = Photo::getImageForPhoto($photo); // If a custom width and height were specified, apply before embedding if (preg_match("/\[img\=([0-9]*)x([0-9]*)\]/is", substr($orig_body, $img_start, $img_st_close), $match)) { - Logger::log('scaling photo', Logger::DEBUG); + Logger::info('scaling photo'); $width = intval($match[1]); $height = intval($match[2]); @@ -2767,9 +2841,9 @@ class Item $data = $photo_img->asString(); $type = $photo_img->getType(); - Logger::log('replacing photo', Logger::DEBUG); + Logger::info('replacing photo'); $image = 'data:' . $type . ';base64,' . base64_encode($data); - Logger::log('replaced: ' . $image, Logger::DATA); + Logger::debug('replaced', ['image' => $image]); } } } @@ -2946,39 +3020,6 @@ class Item return false; } - switch ($verb) { - case 'like': - case 'unlike': - $activity = Activity::LIKE; - break; - case 'dislike': - case 'undislike': - $activity = Activity::DISLIKE; - break; - case 'attendyes': - case 'unattendyes': - $activity = Activity::ATTEND; - break; - case 'attendno': - case 'unattendno': - $activity = Activity::ATTENDNO; - break; - case 'attendmaybe': - case 'unattendmaybe': - $activity = Activity::ATTENDMAYBE; - break; - case 'follow': - case 'unfollow': - $activity = Activity::FOLLOW; - break; - default: - Logger::log('like: unknown verb ' . $verb . ' for item ' . $item_id); - return false; - } - - // Enable activity toggling instead of on/off - $event_verb_flag = $activity === Activity::ATTEND || $activity === Activity::ATTENDNO || $activity === Activity::ATTENDMAYBE; - Logger::log('like: verb ' . $verb . ' item ' . $item_id); $item = self::selectFirst(self::ITEM_FIELDLIST, ['`id` = ? OR `uri` = ?', $item_id, $item_id]); @@ -2999,6 +3040,10 @@ class Item return false; } + if (!Item::exists(['uri-id' => $item['parent-uri-id'], 'uid' => $uid])) { + $stored = self::storeForUserByUriId($item['parent-uri-id'], $uid); + } + // Retrieves the local post owner $owner_self_contact = DBA::selectFirst('contact', [], ['uid' => $uid, 'self' => true]); if (!DBA::isResult($owner_self_contact)) { @@ -3019,7 +3064,7 @@ class Item if (local_user() == $uid) { $item_contact_id = $owner_self_contact['id']; } else { - $item_contact_id = Contact::getIdForURL($author_contact['url'], $uid, true); + $item_contact_id = Contact::getIdForURL($author_contact['url'], $uid, false); $item_contact = DBA::selectFirst('contact', [], ['id' => $item_contact_id]); if (!DBA::isResult($item_contact)) { Logger::log('like: unknown item contact ' . $item_contact_id); @@ -3027,9 +3072,44 @@ class Item } } + $activity = null; + switch ($verb) { + case 'like': + case 'unlike': + $activity = Activity::LIKE; + break; + case 'dislike': + case 'undislike': + $activity = Activity::DISLIKE; + break; + case 'attendyes': + case 'unattendyes': + $activity = Activity::ATTEND; + break; + case 'attendno': + case 'unattendno': + $activity = Activity::ATTENDNO; + break; + case 'attendmaybe': + case 'unattendmaybe': + $activity = Activity::ATTENDMAYBE; + break; + case 'follow': + case 'unfollow': + $activity = Activity::FOLLOW; + break; + default: + Logger::log('like: unknown verb ' . $verb . ' for item ' . $item_id); + return false; + } + + $mode = Strings::startsWith($verb, 'un') ? 'delete' : 'create'; + + // Enable activity toggling instead of on/off + $event_verb_flag = $activity === Activity::ATTEND || $activity === Activity::ATTENDNO || $activity === Activity::ATTENDMAYBE; + // Look for an existing verb row - // event participation are essentially radio toggles. If you make a subsequent choice, - // we need to eradicate your first choice. + // Event participation activities are mutually exclusive, only one of them can exist at all times. if ($event_verb_flag) { $verbs = [Activity::ATTEND, Activity::ATTENDNO, Activity::ATTENDMAYBE]; @@ -3044,20 +3124,43 @@ class Item $condition = ['vid' => $vids, 'deleted' => false, 'gravity' => GRAVITY_ACTIVITY, 'author-id' => $author_id, 'uid' => $item['uid'], 'thr-parent' => $item_uri]; - $like_item = self::selectFirst(['id', 'guid', 'verb'], $condition); - // If it exists, mark it as deleted if (DBA::isResult($like_item)) { - self::markForDeletionById($like_item['id']); + /** + * Truth table for existing activities + * + * | Inputs || Outputs | + * |----------------------------||-------------------| + * | Mode | Event | Same verb || Delete? | Return? | + * |--------|-------|-----------||---------|---------| + * | create | Yes | Yes || No | Yes | + * | create | Yes | No || Yes | No | + * | create | No | Yes || No | Yes | + * | create | No | No || N/A† | + * | delete | Yes | Yes || Yes | N/A‡ | + * | delete | Yes | No || No | N/A‡ | + * | delete | No | Yes || Yes | N/A‡ | + * | delete | No | No || N/A† | + * |--------|-------|-----------||---------|---------| + * | A | B | C || A xor C | !B or C | + * + * † Can't happen: It's impossible to find an existing non-event activity without + * the same verb because we are only looking for this single verb. + * + * ‡ The "mode = delete" is returning early whether an existing activity was found or not. + */ + if ($mode == 'create' xor $like_item['verb'] == $activity) { + self::markForDeletionById($like_item['id']); + } if (!$event_verb_flag || $like_item['verb'] == $activity) { return true; } } - // Verb is "un-something", just trying to delete existing entries - if (strpos($verb, 'un') === 0) { + // No need to go further if we aren't creating anything + if ($mode == 'delete') { return true; } @@ -3110,7 +3213,7 @@ class Item private static function addThread($itemid, $onlyshadow = false) { $fields = ['uid', 'created', 'edited', 'commented', 'received', 'changed', 'wall', 'private', 'pubmail', - 'moderated', 'visible', 'starred', 'contact-id', 'post-type', + 'moderated', 'visible', 'starred', 'contact-id', 'post-type', 'uri-id', 'deleted', 'origin', 'forum_mode', 'mention', 'network', 'author-id', 'owner-id']; $condition = ["`id` = ? AND (`parent` = ? OR `parent` = 0)", $itemid, $itemid]; $item = self::selectFirst($fields, $condition); @@ -3124,14 +3227,14 @@ class Item if (!$onlyshadow) { $result = DBA::insert('thread', $item); - Logger::log("Add thread for item ".$itemid." - ".print_r($result, true), Logger::DEBUG); + Logger::info('Add thread', ['item' => $itemid, 'result' => $result]); } } private static function updateThread($itemid, $setmention = false) { $fields = ['uid', 'guid', 'created', 'edited', 'commented', 'received', 'changed', 'post-type', - 'wall', 'private', 'pubmail', 'moderated', 'visible', 'starred', 'contact-id', + 'wall', 'private', 'pubmail', 'moderated', 'visible', 'starred', 'contact-id', 'uri-id', 'deleted', 'origin', 'forum_mode', 'network', 'author-id', 'owner-id']; $condition = ["`id` = ? AND (`parent` = ? OR `parent` = 0)", $itemid, $itemid]; @@ -3154,20 +3257,20 @@ class Item $result = DBA::update('thread', $fields, ['iid' => $itemid]); - Logger::log("Update thread for item ".$itemid." - guid ".$item["guid"]." - ".(int)$result, Logger::DEBUG); + Logger::info('Update thread', ['item' => $itemid, 'guid' => $item["guid"], 'result' => $result]); } private static function deleteThread($itemid, $itemuri = "") { $item = DBA::selectFirst('thread', ['uid'], ['iid' => $itemid]); if (!DBA::isResult($item)) { - Logger::log('No thread found for id '.$itemid, Logger::DEBUG); + Logger::info('No thread found', ['id' => $itemid]); return; } $result = DBA::delete('thread', ['iid' => $itemid], ['cascade' => false]); - Logger::log("deleteThread: Deleted thread for item ".$itemid." - ".print_r($result, true), Logger::DEBUG); + Logger::info('Deleted thread', ['item' => $itemid, 'result' => $result]); if ($itemuri != "") { $condition = ["`uri` = ? AND NOT `deleted` AND NOT (`uid` IN (?, 0))", $itemuri, $item["uid"]]; @@ -3474,9 +3577,7 @@ class Item */ public static function getPlink($item) { - $a = DI::app(); - - if ($a->user['nickname'] != "") { + if (local_user()) { $ret = [ 'href' => "display/" . $item['guid'], 'orig' => "display/" . $item['guid'], @@ -3488,7 +3589,6 @@ class Item $ret["href"] = DI::baseUrl()->remove($item['plink']); $ret["title"] = DI::l10n()->t('link to source'); } - } elseif (!empty($item['plink']) && ($item['private'] != self::PRIVATE)) { $ret = [ 'href' => $item['plink'], @@ -3603,10 +3703,12 @@ class Item * * @return integer item id */ - public static function fetchByLink($uri, $uid = 0) + public static function fetchByLink(string $uri, int $uid = 0) { + Logger::info('Trying to fetch link', ['uid' => $uid, 'uri' => $uri]); $item_id = self::searchByLink($uri, $uid); if (!empty($item_id)) { + Logger::info('Link found', ['uid' => $uid, 'uri' => $uri, 'id' => $item_id]); return $item_id; } @@ -3617,9 +3719,11 @@ class Item } if (!empty($item_id)) { + Logger::info('Link fetched', ['uid' => $uid, 'uri' => $uri, 'id' => $item_id]); return $item_id; } + Logger::info('Link not found', ['uid' => $uid, 'uri' => $uri]); return 0; } @@ -3656,7 +3760,7 @@ class Item * * @return array item array with data from the original item */ - public static function addShareDataFromOriginal($item) + public static function addShareDataFromOriginal(array $item) { $shared = self::getShareArray($item); if (empty($shared)) { @@ -3678,9 +3782,9 @@ class Item } // Otherwhise try to find (and possibly fetch) the item via the link. This should work for Diaspora and ActivityPub posts - $id = self::fetchByLink($shared['link'], $uid); + $id = self::fetchByLink($shared['link'] ?? '', $uid); if (empty($id)) { - Logger::info('Original item not found', ['url' => $shared['link'], 'callstack' => System::callstack()]); + Logger::info('Original item not found', ['url' => $shared['link'] ?? '', 'callstack' => System::callstack()]); return $item; }