X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModel%2FItem.php;h=778b872bbb751452c2894c6ba2d2c2c94aebeba8;hb=26df7595ac5366bd0f14f94ef961cf8af6b72ebb;hp=1f339120ee0487ba6b9ad54c5b1fd84fd389a6ce;hpb=dc5838ecf9ae0b0f6c7ac84af7985a863d495a46;p=friendica.git diff --git a/src/Model/Item.php b/src/Model/Item.php index 1f339120ee..778b872bbb 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; @@ -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)) { @@ -787,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; @@ -1379,7 +1394,7 @@ class Item return false; } - if (!empty($item['uid']) && Contact::isBlockedByUser($item['author-id'], $item['uid'])) { + if (!empty($item['uid']) && Contact\User::isBlocked($item['author-id'], $item['uid'])) { Logger::notice('Author is blocked by user', ['author-link' => $item['author-link'], 'uid' => $item['uid'], 'item-uri' => $item['uri']]); return false; } @@ -1394,18 +1409,18 @@ class Item return false; } - if (!empty($item['uid']) && Contact::isBlockedByUser($item['owner-id'], $item['uid'])) { + if (!empty($item['uid']) && Contact\User::isBlocked($item['owner-id'], $item['uid'])) { Logger::notice('Owner is blocked by user', ['owner-link' => $item['owner-link'], 'uid' => $item['uid'], 'item-uri' => $item['uri']]); return false; } // The causer is set during a thread completion, for example because of a reshare. It countains the responsible actor. - if (!empty($item['uid']) && !empty($item['causer-id']) && Contact::isBlockedByUser($item['causer-id'], $item['uid'])) { + if (!empty($item['uid']) && !empty($item['causer-id']) && Contact\User::isBlocked($item['causer-id'], $item['uid'])) { Logger::notice('Causer is blocked by user', ['causer-link' => $item['causer-link'], 'uid' => $item['uid'], 'item-uri' => $item['uri']]); return false; } - if (!empty($item['uid']) && !empty($item['causer-id']) && ($item['parent-uri'] == $item['uri']) && Contact::isIgnoredByUser($item['causer-id'], $item['uid'])) { + if (!empty($item['uid']) && !empty($item['causer-id']) && ($item['parent-uri'] == $item['uri']) && Contact\User::isIgnored($item['causer-id'], $item['uid'])) { Logger::notice('Causer is ignored by user', ['causer-link' => $item['causer-link'], 'uid' => $item['uid'], 'item-uri' => $item['uri']]); return false; } @@ -1539,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); - } + Contact\Relation::store($parent['author-id'], $item['author-id'], $item['created']); } return $item; @@ -1565,6 +1578,8 @@ class Item return GRAVITY_COMMENT; } elseif ($activity->match($item['verb'], Activity::FOLLOW)) { return GRAVITY_ACTIVITY; + } elseif ($activity->match($item['verb'], Activity::ANNOUNCE)) { + return GRAVITY_ACTIVITY; } Logger::info('Unknown gravity for verb', ['verb' => $item['verb']]); return GRAVITY_UNKNOWN; // Should not happen @@ -1572,6 +1587,8 @@ class Item public static function insert($item, $notify = false, $dontcache = false) { + $structure = self::getItemFields(); + $orig_item = $item; $priority = PRIORITY_HIGH; @@ -1680,11 +1697,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); @@ -1779,6 +1800,7 @@ class Item // It is mainly used in the "post_local" hook. unset($item['api_source']); + self::transformToForumPost($item); // Check for hashtags in the body and repair or add hashtag links $item['body'] = self::setHashtags($item['body']); @@ -1838,7 +1860,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. @@ -1940,6 +1969,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) { @@ -1959,6 +1991,55 @@ class Item return $current_post; } + /** + * Convert items to forum posts + * + * (public) forum posts in the new format consist of the regular post by the author + * followed by an announce message sent from the forum account. + * This means we have to look out for an announce message send by a forum account. + * + * @param array $item + * @return void + */ + private static function transformToForumPost(array $item) + { + if ($item["verb"] != Activity::ANNOUNCE) { + // No announce message, so don't do anything + return; + } + + $pcontact = Contact::selectFirst(['nurl'], ['id' => $item['author-id'], 'contact-type' => Contact::TYPE_COMMUNITY]); + if (empty($pcontact['nurl'])) { + // The announce message wasn't created by a forum account, so we don't need to continue + return; + } + + $contact = Contact::selectFirst(['id'], ['nurl' => $pcontact['nurl'], 'uid' => $item['uid']]); + if (!empty($contact['id'])) { + $condition = ['uri-id' => $item['parent-uri-id'], 'uid' => $item['uid']]; + Item::update(['owner-id' => $item['author-id'], 'contact-id' => $contact['id']], $condition); + Logger::info('Convert message into a forum message', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id'], 'uid' => $item['uid'], 'owner-id' => $item['author-id'], 'contact-id' => $contact['id']]); + } + } + + /** + * 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 * @@ -2022,9 +2103,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); @@ -2057,13 +2136,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. @@ -2095,7 +2167,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) { @@ -2123,33 +2195,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']; } @@ -2157,7 +2272,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); @@ -2167,10 +2282,11 @@ class Item $distributed = self::insert($item, $notify, true); if (!$distributed) { - Logger::info("Distributed public item wasn't stored", ['id' => $itemid, 'user' => $uid]); + Logger::info("Distributed public item wasn't stored", ['uri-id' => $item['uri-id'], 'user' => $uid]); } else { - Logger::info('Distributed public item was stored', ['id' => $itemid, 'user' => $uid, 'stored' => $distributed]); + Logger::info('Distributed public item was stored', ['uri-id' => $item['uri-id'], 'user' => $uid, 'stored' => $distributed]); } + return $distributed; } /** @@ -2393,7 +2509,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"])) { @@ -2411,15 +2527,15 @@ 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']]); } } @@ -2591,6 +2707,10 @@ class Item Worker::add(['priority' => PRIORITY_HIGH, 'dont_fork' => true], 'Notifier', Delivery::POST, $item_id); + /// @todo This code should be activated by the end of the year 2020 + // See also "createActivityFromItem" + //Item::performActivity($item_id, 'announce', $uid); + return false; } @@ -2920,11 +3040,12 @@ class Item * * Toggle activities as like,dislike,attend of an item * - * @param string $item_id + * @param int $item_id * @param string $verb * Activity verb. One of * like, unlike, dislike, undislike, attendyes, unattendyes, - * attendno, unattendno, attendmaybe, unattendmaybe + * attendno, unattendno, attendmaybe, unattendmaybe, + * announce, unannouce * @return bool * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException @@ -2932,15 +3053,15 @@ class Item * array $arr * 'post_id' => ID of posted item */ - public static function performActivity($item_id, $verb) + public static function performActivity(int $item_id, string $verb, int $uid) { - if (!Session::isAuthenticated()) { + if (empty($uid)) { return false; } - Logger::log('like: verb ' . $verb . ' item ' . $item_id); + Logger::notice('Start create activity', ['verb' => $verb, 'item' => $item_id, 'user' => $uid]); - $item = self::selectFirst(self::ITEM_FIELDLIST, ['`id` = ? OR `uri` = ?', $item_id, $item_id]); + $item = self::selectFirst(self::ITEM_FIELDLIST, ['id' => $item_id]); if (!DBA::isResult($item)) { Logger::log('like: unknown item ' . $item_id); return false; @@ -2948,44 +3069,35 @@ class Item $item_uri = $item['uri']; - $uid = $item['uid']; - if (($uid == 0) && local_user()) { - $uid = local_user(); + if (!in_array($item['uid'], [0, $uid])) { + return false; } - if (!Security::canWriteToUserWall($uid)) { - Logger::log('like: unable to write on wall ' . $uid); - return false; + if (!Item::exists(['uri-id' => $item['parent-uri-id'], 'uid' => $uid])) { + $stored = self::storeForUserByUriId($item['parent-uri-id'], $uid); + if (($item['parent-uri-id'] == $item['uri-id']) && !empty($stored)) { + $item = self::selectFirst(self::ITEM_FIELDLIST, ['id' => $stored]); + if (!DBA::isResult($item)) { + Logger::info('Could not fetch just created item - should not happen', ['stored' => $stored, 'uid' => $uid, 'item-uri' => $item_uri]); + return false; + } + } } // Retrieves the local post owner - $owner_self_contact = DBA::selectFirst('contact', [], ['uid' => $uid, 'self' => true]); - if (!DBA::isResult($owner_self_contact)) { - Logger::log('like: unknown owner ' . $uid); + $owner = User::getOwnerDataById($uid); + if (empty($owner)) { + Logger::info('Empty owner for user', ['uid' => $uid]); return false; } // Retrieve the current logged in user's public contact - $author_id = public_contact(); - - $author_contact = DBA::selectFirst('contact', ['url'], ['id' => $author_id]); - if (!DBA::isResult($author_contact)) { - Logger::log('like: unknown author ' . $author_id); + $author_id = Contact::getIdForURL($owner['url']); + if (empty($author_id)) { + Logger::info('Empty public contact'); return false; } - // Contact-id is the uid-dependant author contact - if (local_user() == $uid) { - $item_contact_id = $owner_self_contact['id']; - } else { - $item_contact_id = Contact::getIdForURL($author_contact['url'], $uid, true); - $item_contact = DBA::selectFirst('contact', [], ['id' => $item_contact_id]); - if (!DBA::isResult($item_contact)) { - Logger::log('like: unknown item contact ' . $item_contact_id); - return false; - } - } - $activity = null; switch ($verb) { case 'like': @@ -3012,8 +3124,12 @@ class Item case 'unfollow': $activity = Activity::FOLLOW; break; + case 'announce': + case 'unannounce': + $activity = Activity::ANNOUNCE; + break; default: - Logger::log('like: unknown verb ' . $verb . ' for item ' . $item_id); + Logger::notice('unknown verb', ['verb' => $verb, 'item' => $item_id]); return false; } @@ -3084,7 +3200,7 @@ class Item 'guid' => System::createUUID(), 'uri' => self::newURI($item['uid']), 'uid' => $item['uid'], - 'contact-id' => $item_contact_id, + 'contact-id' => $owner['id'], 'wall' => $item['wall'], 'origin' => 1, 'network' => Protocol::DFRN, @@ -3617,10 +3733,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; } @@ -3631,9 +3749,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; } @@ -3670,7 +3790,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)) { @@ -3692,9 +3812,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; }