X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModel%2FItem.php;h=7a965f80b5f41649856f47abf2d0866afbf046e5;hb=7ac4a08e1b6981be6178f85381ad1827d986d6b1;hp=78dd6aff23221165605ffbe42807c376bc79ce61;hpb=06da3084f51261e9c5402c0a630ad9941f32189d;p=friendica.git diff --git a/src/Model/Item.php b/src/Model/Item.php index 78dd6aff23..7a965f80b5 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -36,13 +36,11 @@ use Friendica\Model\Post\Category; use Friendica\Protocol\Activity; use Friendica\Protocol\ActivityPub; use Friendica\Protocol\Diaspora; -use Friendica\Protocol\OStatus; use Friendica\Util\DateTimeFormat; use Friendica\Util\Map; use Friendica\Util\Network; use Friendica\Util\Security; use Friendica\Util\Strings; -use Friendica\Util\XML; use Friendica\Worker\Delivery; use Text_LanguageDetect; use Friendica\Repository\PermissionSet as RepPermissionSet; @@ -74,14 +72,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']; @@ -96,8 +94,8 @@ class Item // All fields in the item table const ITEM_FIELDLIST = ['id', 'uid', 'parent', 'uri', 'parent-uri', 'thr-parent', - 'guid', 'uri-id', 'parent-uri-id', 'thr-parent-id', - 'contact-id', 'type', 'wall', 'gravity', 'extid', 'icid', 'iaid', 'psid', + 'guid', 'uri-id', 'parent-uri-id', 'thr-parent-id', 'vid', + 'contact-id', 'type', 'wall', 'gravity', 'extid', 'icid', 'psid', 'created', 'edited', 'commented', 'received', 'changed', 'verb', 'postopts', 'plink', 'resource-id', 'event-id', 'attach', 'inform', 'file', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'post-type', @@ -108,8 +106,8 @@ class Item 'author-id', 'author-link', 'author-name', 'author-avatar', 'author-network', 'owner-id', 'owner-link', 'owner-name', 'owner-avatar']; + // List of all verbs that don't need additional content data. // Never reorder or remove entries from this list. Just add new ones at the end, if needed. - // The item-activity table only stores the index and needs this array to know the matching activity. const ACTIVITIES = [ Activity::LIKE, Activity::DISLIKE, Activity::ATTEND, Activity::ATTENDNO, Activity::ATTENDMAYBE, @@ -205,38 +203,6 @@ class Item return self::selectThreadForUser($uid, $selected, $condition, $params); } - /** - * returns an activity index from an activity string - * - * @param string $activity activity string - * @return integer Activity index - */ - public static function activityToIndex($activity) - { - $index = array_search($activity, self::ACTIVITIES); - - if (is_bool($index)) { - $index = -1; - } - - return $index; - } - - /** - * returns an activity string from an activity index - * - * @param integer $index activity index - * @return string Activity string - */ - private static function indexToActivity($index) - { - if (is_null($index) || !array_key_exists($index, self::ACTIVITIES)) { - return ''; - } - - return self::ACTIVITIES[$index]; - } - /** * Fetch a single item row * @@ -293,30 +259,36 @@ class Item } } - if (!empty($row['internal-iaid']) && array_key_exists('verb', $row)) { - $row['verb'] = self::indexToActivity($row['internal-activity']); - if (array_key_exists('title', $row)) { - $row['title'] = ''; + if (array_key_exists('verb', $row)) { + if (!is_null($row['internal-verb'])) { + $row['verb'] = $row['internal-verb']; } - if (array_key_exists('body', $row)) { - $row['body'] = $row['verb']; - } - if (array_key_exists('object', $row)) { - $row['object'] = ''; - } - if (array_key_exists('object-type', $row)) { - $row['object-type'] = Activity\ObjectType::NOTE; - } - } elseif (array_key_exists('verb', $row) && in_array($row['verb'], ['', Activity::POST, Activity::SHARE])) { - // Posts don't have a target - but having tags or files. - // We safe some performance by building tag and file strings only here. - // We remove the target since they aren't used for this type. - // In mail posts we do store some mail header data in the object. - if (array_key_exists('target', $row)) { - $row['target'] = ''; + + if (in_array($row['verb'], self::ACTIVITIES)) { + if (array_key_exists('title', $row)) { + $row['title'] = ''; + } + if (array_key_exists('body', $row)) { + $row['body'] = $row['verb']; + } + if (array_key_exists('object', $row)) { + $row['object'] = ''; + } + if (array_key_exists('object-type', $row)) { + $row['object-type'] = Activity\ObjectType::NOTE; + } + } elseif (in_array($row['verb'], ['', Activity::POST, Activity::SHARE])) { + // Posts don't have a target - but having tags or files. + if (array_key_exists('target', $row)) { + $row['target'] = ''; + } } } + 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'])) { @@ -344,12 +316,11 @@ class Item } // Remove internal fields - unset($row['internal-activity']); unset($row['internal-network']); unset($row['internal-uri-id']); unset($row['internal-uid']); unset($row['internal-psid']); - unset($row['internal-iaid']); + unset($row['internal-verb']); unset($row['internal-user-ignored']); unset($row['interaction']); @@ -667,26 +638,26 @@ class Item $fields = []; $fields['item'] = ['id', 'uid', 'parent', 'uri', 'parent-uri', 'thr-parent', - 'guid', 'uri-id', 'parent-uri-id', 'thr-parent-id', + 'guid', 'uri-id', 'parent-uri-id', 'thr-parent-id', 'vid', 'contact-id', 'owner-id', 'author-id', 'type', 'wall', 'gravity', 'extid', 'created', 'edited', 'commented', 'received', 'changed', 'psid', 'resource-id', 'event-id', 'attach', 'post-type', 'file', 'private', 'pubmail', 'moderated', 'visible', 'starred', 'bookmark', 'unseen', 'deleted', 'origin', 'forum_mode', 'mention', 'global', - 'id' => 'item_id', 'network', 'icid', 'iaid', + 'id' => 'item_id', 'network', 'icid', 'uri-id' => 'internal-uri-id', 'uid' => 'internal-uid', - 'network' => 'internal-network', 'iaid' => 'internal-iaid', 'psid' => 'internal-psid']; + 'network' => 'internal-network', 'psid' => 'internal-psid']; if ($usermode) { $fields['user-item'] = ['pinned', 'notification-type', 'ignored' => 'internal-user-ignored']; } - $fields['item-activity'] = ['activity', 'activity' => 'internal-activity']; - $fields['item-content'] = array_merge(self::CONTENT_FIELDLIST, self::MIXED_CONTENT_FIELDLIST); $fields['post-delivery-data'] = array_merge(Post\DeliveryData::LEGACY_FIELD_LIST, Post\DeliveryData::FIELD_LIST); + $fields['verb'] = ['name' => 'internal-verb']; + $fields['permissionset'] = ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid']; $fields['author'] = ['url' => 'author-link', 'name' => 'author-name', 'addr' => 'author-addr', @@ -799,10 +770,6 @@ class Item $joins .= " LEFT JOIN `diaspora-interaction` ON `diaspora-interaction`.`uri-id` = `item`.`uri-id`"; } - if (strpos($sql_commands, "`item-activity`.") !== false) { - $joins .= " LEFT JOIN `item-activity` ON `item-activity`.`uri-id` = `item`.`uri-id`"; - } - if (strpos($sql_commands, "`item-content`.") !== false) { $joins .= " LEFT JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`"; } @@ -811,6 +778,10 @@ class Item $joins .= " LEFT JOIN `post-delivery-data` ON `post-delivery-data`.`uri-id` = `item`.`uri-id` AND `item`.`origin`"; } + if (strpos($sql_commands, "`verb`.") !== false) { + $joins .= " LEFT JOIN `verb` ON `verb`.`id` = `item`.`vid`"; + } + if (strpos($sql_commands, "`permissionset`.") !== false) { $joins .= " LEFT JOIN `permissionset` ON `permissionset`.`id` = `item`.`psid`"; } @@ -837,11 +808,11 @@ class Item private static function constructSelectFields(array $fields, array $selected) { if (!empty($selected)) { - $selected = array_merge($selected, ['internal-uri-id', 'internal-uid', 'internal-psid', 'internal-iaid', 'internal-network']); + $selected = array_merge($selected, ['internal-uri-id', 'internal-uid', 'internal-psid', 'internal-network']); } if (in_array('verb', $selected)) { - $selected[] = 'internal-activity'; + $selected = array_merge($selected, ['internal-verb']); } if (in_array('ignored', $selected)) { @@ -920,7 +891,7 @@ class Item // We cannot simply expand the condition to check for origin entries // The condition needn't to be a simple array but could be a complex condition. // And we have to execute this query before the update to ensure to fetch the same data. - $items = DBA::select('item', ['id', 'origin', 'uri', 'uri-id', 'iaid', 'icid', 'uid', 'file'], $condition); + $items = DBA::select('item', ['id', 'origin', 'uri', 'uri-id', 'icid', 'uid', 'file'], $condition); $content_fields = []; foreach (array_merge(self::CONTENT_FIELDLIST, self::MIXED_CONTENT_FIELDLIST) as $field) { @@ -950,6 +921,10 @@ class Item $files = null; } + if (!empty($content_fields['verb'])) { + $fields['vid'] = Verb::getID($content_fields['verb']); + } + if (!empty($fields)) { $success = DBA::update('item', $fields, $condition); @@ -966,34 +941,7 @@ class Item $notify_items = []; while ($item = DBA::fetch($items)) { - if (!empty($item['iaid']) || (!empty($content_fields['verb']) && (self::activityToIndex($content_fields['verb']) >= 0))) { - self::updateActivity($content_fields, ['uri-id' => $item['uri-id']]); - - if (empty($item['iaid'])) { - $item_activity = DBA::selectFirst('item-activity', ['id'], ['uri-id' => $item['uri-id']]); - if (DBA::isResult($item_activity)) { - $item_fields = ['iaid' => $item_activity['id'], 'icid' => null]; - foreach (self::MIXED_CONTENT_FIELDLIST as $field) { - if (self::isLegacyMode()) { - $item_fields[$field] = null; - } else { - unset($item_fields[$field]); - } - } - DBA::update('item', $item_fields, ['id' => $item['id']]); - - if (!empty($item['icid']) && !DBA::exists('item', ['icid' => $item['icid']])) { - DBA::delete('item-content', ['id' => $item['icid']]); - } - } - } elseif (!empty($item['icid'])) { - DBA::update('item', ['icid' => null], ['id' => $item['id']]); - - if (!DBA::exists('item', ['icid' => $item['icid']])) { - DBA::delete('item-content', ['id' => $item['icid']]); - } - } - } else { + if (empty($content_fields['verb']) || !in_array($content_fields['verb'], self::ACTIVITIES)) { self::updateContent($content_fields, ['uri-id' => $item['uri-id']]); if (empty($item['icid'])) { @@ -1001,12 +949,10 @@ class Item if (DBA::isResult($item_content)) { $item_fields = ['icid' => $item_content['id']]; // Clear all fields in the item table that have a content in the item-content table - foreach ($item_content as $field => $content) { - if (in_array($field, self::MIXED_CONTENT_FIELDLIST) && !empty($item_content[$field])) { - if (self::isLegacyMode()) { + if (self::isLegacyMode()) { + foreach ($item_content as $field => $content) { + if (in_array($field, self::MIXED_CONTENT_FIELDLIST) && !empty($content)) { $item_fields[$field] = null; - } else { - unset($item_fields[$field]); } } } @@ -1105,7 +1051,7 @@ class Item $fields = ['id', 'uri', 'uri-id', 'uid', 'parent', 'parent-uri', 'origin', 'deleted', 'file', 'resource-id', 'event-id', 'attach', 'verb', 'object-type', 'object', 'target', 'contact-id', - 'icid', 'iaid', 'psid']; + 'icid', 'psid', 'gravity']; $item = self::selectFirst($fields, ['id' => $item_id]); if (!DBA::isResult($item)) { Logger::info('Item not found.', ['id' => $item_id]); @@ -1124,7 +1070,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) { @@ -1133,7 +1079,7 @@ class Item } } - $matches = false; + $matches = []; $cnt = preg_match_all('/\[(.*?)\]/', $item['file'], $matches, PREG_SET_ORDER); @@ -1184,8 +1130,6 @@ class Item Post\DeliveryData::delete($item['uri-id']); - // We don't delete the item-activity here, since we need some of the data for ActivityPub - if (!empty($item['icid']) && !self::exists(['icid' => $item['icid'], 'deleted' => false])) { DBA::delete('item-content', ['id' => $item['icid']], ['cascade' => false]); } @@ -1198,7 +1142,7 @@ class Item //} // If it's the parent of a comment thread, kill all the kids - if ($item['id'] == $item['parent']) { + if ($item['gravity'] == GRAVITY_PARENT) { self::markForDeletion(['parent' => $item['parent'], 'deleted' => false], $priority); } @@ -1333,7 +1277,13 @@ class Item } } - private static function isDuplicate($item) + /** + * Check if the item array is a duplicate + * + * @param array $item + * @return boolean is it a duplicate? + */ + private static function isDuplicate(array $item) { // Checking if there is already an item with the same guid $condition = ['guid' => $item['guid'], 'network' => $item['network'], 'uid' => $item['uid']]; @@ -1375,7 +1325,7 @@ class Item * There is a timing issue here that sometimes creates double postings. * An unique index would help - but the limitations of MySQL (maximum size of index values) prevent this. */ - if (($item["uid"] == 0) && self::exists(['uri' => trim($item['uri']), 'uid' => 0])) { + if (($item['uid'] == 0) && self::exists(['uri' => trim($item['uri']), 'uid' => 0])) { Logger::notice('Global item already stored.', ['uri' => $item['uri'], 'network' => $item['network']]); return true; } @@ -1383,7 +1333,13 @@ class Item return false; } - private static function validItem($item) + /** + * Check if the item array is valid + * + * @param array $item + * @return boolean item is valid + */ + private static function isValid(array $item) { // When there is no content then we don't post it if ($item['body'].$item['title'] == '') { @@ -1456,7 +1412,7 @@ class Item if ($item['verb'] == Activity::FOLLOW) { if (!$item['origin'] && ($item['author-id'] == Contact::getPublicIdByUserId($item['uid']))) { // Our own follow request can be relayed to us. We don't store it to avoid notification chaos. - Logger::log("Follow: Don't store not origin follow request from us for " . $item['parent-uri'], Logger::DEBUG); + Logger::info("Follow: Don't store not origin follow request", ['parent-uri' => $item['parent-uri']]); return false; } @@ -1464,7 +1420,7 @@ class Item 'parent-uri' => $item['parent-uri'], 'author-id' => $item['author-id']]; if (self::exists($condition)) { // It happens that we receive multiple follow requests by the same author - we only store one. - Logger::log('Follow: Found existing follow request from author ' . $item['author-id'] . ' for ' . $item['parent-uri'], Logger::DEBUG); + Logger::info('Follow: Found existing follow request from author', ['author-id' => $item['author-id'], 'parent-uri' => $item['parent-uri']]); return false; } } @@ -1472,7 +1428,13 @@ class Item return true; } - private static function getDuplicateID($item) + /** + * Return the id of the given item array if it has been stored before + * + * @param array $item + * @return integer item id + */ + private static function getDuplicateID(array $item) { if (empty($item['network']) || in_array($item['network'], Protocol::FEDERATED)) { $condition = ["`uri` = ? AND `uid` = ? AND `network` IN (?, ?, ?, ?)", @@ -1497,7 +1459,13 @@ class Item return 0; } - private static function getParentData($item) + /** + * Fetch parent data for the given item array + * + * @param array $item + * @return array item array with parent data + */ + private static function getParentData(array $item) { // find the parent and snarf the item id and ACLs // and anything else we need to inherit @@ -1530,7 +1498,7 @@ class Item } } - $item["parent"] = $parent['id']; + $item['parent'] = $parent['id']; $item["deleted"] = $parent['deleted']; $item["allow_cid"] = $parent['allow_cid']; $item['allow_gid'] = $parent['allow_gid']; @@ -1565,8 +1533,8 @@ class Item // If its a post that originated here then tag the thread as "mention" if ($item['origin'] && $item['uid']) { - DBA::update('thread', ['mention' => true], ['iid' => $item["parent"]]); - Logger::info('tagged thread as mention', ['parent' => $item["parent"], 'uid' => $item['uid']]); + DBA::update('thread', ['mention' => true], ['iid' => $item['parent']]); + Logger::info('tagged thread as mention', ['parent' => $item['parent'], 'uid' => $item['uid']]); } // Update the contact relations @@ -1578,7 +1546,13 @@ class Item return $item; } - private static function getGravity($item) + /** + * Get the gravity for the given item array + * + * @param array $item + * @return integer gravity + */ + private static function getGravity(array $item) { $activity = DI::activity(); @@ -1595,7 +1569,7 @@ class Item return GRAVITY_UNKNOWN; // Should not happen } - public static function insert($item, $dummy = false, $notify = false, $dontcache = false) + public static function insert($item, $notify = false, $dontcache = false) { $orig_item = $item; @@ -1687,8 +1661,6 @@ class Item $item['inform'] = trim($item['inform'] ?? ''); $item['file'] = trim($item['file'] ?? ''); - self::addLanguageToItemArray($item); - // Items cannot be stored before they happen ... if ($item['created'] > DateTimeFormat::utcNow()) { $item['created'] = DateTimeFormat::utcNow(); @@ -1701,47 +1673,39 @@ class Item $item['plink'] = ($item['plink'] ?? '') ?: DI::baseUrl() . '/display/' . urlencode($item['guid']); + $item['language'] = self::getLanguage($item); + $item['gravity'] = self::getGravity($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); - unset($item['author-link']); - unset($item['author-name']); - unset($item['author-avatar']); - unset($item['author-network']); - $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); - unset($item['owner-link']); - unset($item['owner-name']); - unset($item['owner-avatar']); - // The contact-id should be set before "self::insert" was called - but there seems to be issues sometimes $item["contact-id"] = self::contactId($item); - if ($item['network'] == Protocol::PHANTOM) { - $item['network'] = Protocol::DFRN; - Logger::notice('Missing network, setting to {network}.', [ - 'uri' => $item["uri"], - 'network' => $item['network'], - 'callstack' => System::callstack() - ]); - } - - if (!self::validItem($item)) { + if (!self::isValid($item)) { return 0; } // We don't store the causer, we only have it here for the checks in the function above unset($item['causer-id']); unset($item['causer-link']); - + + // We don't store these fields anymore in the item table + unset($item['author-link']); + unset($item['author-name']); + unset($item['author-avatar']); + unset($item['author-network']); + + unset($item['owner-link']); + unset($item['owner-name']); + unset($item['owner-avatar']); + $item['thr-parent'] = $item['parent-uri']; if ($item['parent-uri'] != $item['uri']) { @@ -1814,67 +1778,66 @@ class Item // It is mainly used in the "post_local" hook. unset($item['api_source']); - // Filling item related side tables - if (!empty($item['dsprsig'])) { - $dsprsig = json_decode(base64_decode($item['dsprsig'])); - /* - * Friendica servers lower than 3.4.3-2 had double encoded the signature ... - * We can check for this condition when we decode and encode the stuff again. - */ - if (base64_encode(base64_decode(base64_decode($dsprsig->signature))) == base64_decode($dsprsig->signature)) { - $dsprsig->signature = base64_decode($dsprsig->signature); - Logger::log("Repaired double encoded signature from handle ".$dsprsig->signer, Logger::DEBUG); - } + // Check for hashtags in the body and repair or add hashtag links + self::setHashtags($item); - if (!empty($dsprsig->signed_text) && empty($dsprsig->signature) && empty($dsprsig->signer)) { - DBA::insert('diaspora-interaction', ['uri-id' => $item['uri-id'], 'interaction' => $dsprsig->signed_text], true); - } + // Fill the cache field + self::putInCache($item); + + if (stristr($item['verb'], Activity::POKE)) { + $notify_type = Delivery::POKE; + } else { + $notify_type = Delivery::POST; + } + + $like_no_comment = DI::config()->get('system', 'like_no_comment'); + + DBA::transaction(); + + if (!in_array($item['verb'], self::ACTIVITIES)) { + $item['icid'] = self::insertContent($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['dsprsig']); + unset($item['activity']); + + // Filling item related side tables + // Diaspora signature if (!empty($item['diaspora_signed_text'])) { DBA::insert('diaspora-interaction', ['uri-id' => $item['uri-id'], 'interaction' => $item['diaspora_signed_text']], true); } unset($item['diaspora_signed_text']); - if (array_key_exists('file', $item) && !empty($item['file'])) { + // Attached file links + if (!empty($item['file'])) { Category::storeTextByURIId($item['uri-id'], $item['uid'], $item['file']); } unset($item['file']); + // Delivery relevant data $delivery_data = Post\DeliveryData::extractFields($item); unset($item['postopts']); unset($item['inform']); - // Check for hashtags in the body and repair or add hashtag links - self::setHashtags($item); + if (!empty($item['origin']) || !empty($item['wall']) || !empty($delivery_data['postopts']) || !empty($delivery_data['inform'])) { + Post\DeliveryData::insert($item['uri-id'], $delivery_data); + } // Store tags from the body if this hadn't been handled previously in the protocol classes if (!Tag::existsForPost($item['uri-id'])) { - Tag::storeFromBody($item['uri-id'], $item['body']); + Tag::storeFromBody($item['uri-id'], $body); } - // Fill the cache field - self::putInCache($item); - - if (stristr($item['verb'], Activity::POKE)) { - $notify_type = Delivery::POKE; - } else { - $notify_type = Delivery::POST; - } - - // We are doing this outside of the transaction to avoid timing problems - if (!self::insertActivity($item)) { - self::insertContent($item); - } - - $like_no_comment = DI::config()->get('system', 'like_no_comment'); - - DBA::transaction(); $ret = DBA::insert('item', $item); // When the item was successfully stored we fetch the ID of the item. @@ -1940,11 +1903,6 @@ class Item } else { self::updateThread($parent_id); } - - if (!empty($item['origin']) || !empty($item['wall']) || !empty($delivery_data['postopts']) || !empty($delivery_data['inform'])) { - Post\DeliveryData::insert($item['uri-id'], $delivery_data); - } - DBA::commit(); // In that function we check if this is a forum post. Additionally we delete the item under certain circumstances @@ -2004,116 +1962,45 @@ class Item * Insert a new item content entry * * @param array $item The item fields that are to be inserted - * @return bool * @throws \Exception */ - private static function insertActivity(&$item) - { - $activity_index = self::activityToIndex($item['verb']); - - if ($activity_index < 0) { - return false; - } - - $fields = ['activity' => $activity_index, 'uri-hash' => (string)$item['uri-id'], 'uri-id' => $item['uri-id']]; - - // We just remove everything that is content - foreach (array_merge(self::CONTENT_FIELDLIST, self::MIXED_CONTENT_FIELDLIST) as $field) { - unset($item[$field]); - } - - // To avoid timing problems, we are using locks. - $locked = DI::lock()->acquire('item_insert_activity'); - if (!$locked) { - Logger::log("Couldn't acquire lock for URI " . $item['uri'] . " - proceeding anyway."); - } - - // Do we already have this content? - $item_activity = DBA::selectFirst('item-activity', ['id'], ['uri-id' => $item['uri-id']]); - if (DBA::isResult($item_activity)) { - $item['iaid'] = $item_activity['id']; - Logger::log('Fetched activity for URI ' . $item['uri'] . ' (' . $item['iaid'] . ')'); - } elseif (DBA::insert('item-activity', $fields)) { - $item['iaid'] = DBA::lastInsertId(); - Logger::log('Inserted activity for URI ' . $item['uri'] . ' (' . $item['iaid'] . ')'); - } else { - // This shouldn't happen. - Logger::log('Could not insert activity for URI ' . $item['uri'] . ' - should not happen'); - DI::lock()->release('item_insert_activity'); - return false; - } - if ($locked) { - DI::lock()->release('item_insert_activity'); - } - return true; - } - - /** - * Insert a new item content entry - * - * @param array $item The item fields that are to be inserted - * @throws \Exception - */ - private static function insertContent(&$item) + private static function insertContent(array $item) { $fields = ['uri-plink-hash' => (string)$item['uri-id'], 'uri-id' => $item['uri-id']]; foreach (array_merge(self::CONTENT_FIELDLIST, self::MIXED_CONTENT_FIELDLIST) as $field) { if (isset($item[$field])) { $fields[$field] = $item[$field]; - unset($item[$field]); } } - // To avoid timing problems, we are using locks. - $locked = DI::lock()->acquire('item_insert_content'); - if (!$locked) { - Logger::log("Couldn't acquire lock for URI " . $item['uri'] . " - proceeding anyway."); - } - - // Do we already have this content? $item_content = DBA::selectFirst('item-content', ['id'], ['uri-id' => $item['uri-id']]); if (DBA::isResult($item_content)) { - $item['icid'] = $item_content['id']; - Logger::log('Fetched content for URI ' . $item['uri'] . ' (' . $item['icid'] . ')'); - } elseif (DBA::insert('item-content', $fields)) { - $item['icid'] = DBA::lastInsertId(); - Logger::log('Inserted content for URI ' . $item['uri'] . ' (' . $item['icid'] . ')'); - } else { - // This shouldn't happen. - Logger::log('Could not insert content for URI ' . $item['uri'] . ' - should not happen'); + $icid = $item_content['id']; + Logger::info('Content found', ['icid' => $icid, 'uri' => $item['uri']]); + return $icid; } - if ($locked) { - DI::lock()->release('item_insert_content'); - } - } - /** - * Update existing item content entries - * - * @param array $item The item fields that are to be changed - * @param array $condition The condition for finding the item content entries - * @return bool - * @throws \Exception - */ - private static function updateActivity($item, $condition) - { - if (empty($item['verb'])) { - return false; + DBA::insert('item-content', $fields, true); + $icid = DBA::lastInsertId(); + if ($icid != 0) { + Logger::info('Content inserted', ['icid' => $icid, 'uri' => $item['uri']]); + return $icid; } - $activity_index = self::activityToIndex($item['verb']); - if ($activity_index < 0) { - return false; + // Possibly there can be timing issues. Then the same content could be inserted multiple times. + // Due to the indexes this doesn't happen, but "lastInsertId" will be empty in these situations. + // So we have to fetch the id manually. This is no bug and there is no data loss. + $item_content = DBA::selectFirst('item-content', ['id'], ['uri-id' => $item['uri-id']]); + if (DBA::isResult($item_content)) { + $icid = $item_content['id']; + Logger::notice('Content inserted with empty lastInsertId', ['icid' => $icid, 'uri' => $item['uri']]); + return $icid; } - $fields = ['activity' => $activity_index]; - - Logger::log('Update activity for ' . json_encode($condition)); - - DBA::update('item-activity', $fields, $condition, true); - - return true; + // This shouldn't happen. + Logger::error("Content wasn't inserted", $item); + return null; } /** @@ -2139,9 +2026,8 @@ class Item $fields = $condition; } - Logger::log('Update content for ' . json_encode($condition)); - DBA::update('item-content', $fields, $condition, true); + Logger::info('Updated content', ['condition' => $condition]); } /** @@ -2277,7 +2163,7 @@ class Item } } - $distributed = self::insert($item, false, $notify, true); + $distributed = self::insert($item, $notify, true); if (!$distributed) { Logger::log("Distributed public item " . $itemid . " for user " . $uid . " wasn't stored", Logger::DEBUG); @@ -2344,7 +2230,7 @@ class Item $item['contact-id'] = $item['author-id']; } - $public_shadow = self::insert($item, false, false, true); + $public_shadow = self::insert($item, false, true); Logger::log("Stored public shadow for thread ".$itemid." under id ".$public_shadow, Logger::DEBUG); } @@ -2366,7 +2252,7 @@ class Item } // Is it a toplevel post? - if ($item['id'] == $item['parent']) { + if ($item['gravity'] == GRAVITY_PARENT) { self::addShadow($itemid); return; } @@ -2402,7 +2288,7 @@ class Item unset($item['inform']); $item['contact-id'] = Contact::getIdForURL($item['author-link']); - $public_shadow = self::insert($item, false, false, true); + $public_shadow = self::insert($item, false, true); Logger::log("Stored public shadow for comment ".$item['uri']." under id ".$public_shadow, Logger::DEBUG); @@ -2417,20 +2303,22 @@ class Item * Adds a language specification in a "language" element of given $arr. * Expects "body" element to exist in $arr. * - * @param $item + * @param array $item + * @return string detected language * @throws \Text_LanguageDetect_Exception */ - private static function addLanguageToItemArray(&$item) + private static function getLanguage(array $item) { $naked_body = BBCode::toPlaintext($item['body'], false); $ld = new Text_LanguageDetect(); $ld->setNameMode(2); $languages = $ld->detect($naked_body, 3); - if (is_array($languages)) { - $item['language'] = json_encode($languages); + return json_encode($languages); } + + return ''; } /** @@ -2662,7 +2550,7 @@ class Item if (!$mention) { if (($community_page || $prvgroup) && - !$item['wall'] && !$item['origin'] && ($item['id'] == $item['parent'])) { + !$item['wall'] && !$item['origin'] && ($item['gravity'] == GRAVITY_PARENT)) { Logger::info('Delete private group/communiy top-level item without mention', ['id' => $item_id, 'guid'=> $item['guid']]); DBA::delete('item', ['id' => $item_id]); return true; @@ -2788,7 +2676,7 @@ class Item if ($contact['network'] != Protocol::FEED) { // Store the original post - $result = self::insert($datarray2, false, false); + $result = self::insert($datarray2); Logger::log('remote-self post original item - Contact '.$contact['url'].' return '.$result.' Item '.print_r($datarray2, true), Logger::DEBUG); } else { $datarray["app"] = "Feed"; @@ -2963,7 +2851,7 @@ class Item return; } - $condition = ["`uid` = ? AND NOT `deleted` AND `id` = `parent` AND `gravity` = ?", + $condition = ["`uid` = ? AND NOT `deleted` AND `gravity` = ?", $uid, GRAVITY_PARENT]; /* @@ -3064,39 +2952,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]); @@ -3145,37 +3000,95 @@ 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]; // Translate to the index based activity index - $activities = []; + $vids = []; foreach ($verbs as $verb) { - $activities[] = self::activityToIndex($verb); + $vids[] = Verb::getID($verb); } } else { - $activities = self::activityToIndex($activity); + $vids = Verb::getID($activity); } - $condition = ['activity' => $activities, 'deleted' => false, 'gravity' => GRAVITY_ACTIVITY, + $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; } @@ -3228,7 +3141,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); @@ -3249,7 +3162,7 @@ class Item 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]; @@ -3345,9 +3258,9 @@ class Item return DI::l10n()->t('event'); } elseif (!empty($item['resource-id'])) { return DI::l10n()->t('photo'); - } elseif (!empty($item['verb']) && $item['verb'] !== Activity::POST) { + } elseif ($item['gravity'] == GRAVITY_ACTIVITY) { return DI::l10n()->t('activity'); - } elseif ($item['id'] != $item['parent']) { + } elseif ($item['gravity'] == GRAVITY_COMMENT) { return DI::l10n()->t('comment'); }