1
1
Fork 0

Improved item insert functionality

This commit is contained in:
Michael 2020-05-18 21:34:57 +00:00
parent efb1c630fd
commit 2f8c6f00b5

View file

@ -291,8 +291,14 @@ class Item
} }
} }
if (!empty($row['internal-iaid']) && array_key_exists('verb', $row)) { if (array_key_exists('verb', $row)) {
if (!is_null($row['internal-verb'])) {
$row['verb'] = $row['internal-verb'];
} elseif (!is_null($row['internal-activity'])) {
$row['verb'] = self::indexToActivity($row['internal-activity']); $row['verb'] = self::indexToActivity($row['internal-activity']);
}
if (in_array($row['verb'], self::ACTIVITIES)) {
if (array_key_exists('title', $row)) { if (array_key_exists('title', $row)) {
$row['title'] = ''; $row['title'] = '';
} }
@ -305,15 +311,13 @@ class Item
if (array_key_exists('object-type', $row)) { if (array_key_exists('object-type', $row)) {
$row['object-type'] = Activity\ObjectType::NOTE; $row['object-type'] = Activity\ObjectType::NOTE;
} }
} elseif (array_key_exists('verb', $row) && in_array($row['verb'], ['', Activity::POST, Activity::SHARE])) { } elseif (in_array($row['verb'], ['', Activity::POST, Activity::SHARE])) {
// Posts don't have a target - but having tags or files. // 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)) { if (array_key_exists('target', $row)) {
$row['target'] = ''; $row['target'] = '';
} }
} }
}
if (!array_key_exists('verb', $row) || in_array($row['verb'], ['', Activity::POST, Activity::SHARE])) { if (!array_key_exists('verb', $row) || in_array($row['verb'], ['', Activity::POST, Activity::SHARE])) {
// Build the file string out of the term entries // Build the file string out of the term entries
@ -347,7 +351,7 @@ class Item
unset($row['internal-uri-id']); unset($row['internal-uri-id']);
unset($row['internal-uid']); unset($row['internal-uid']);
unset($row['internal-psid']); unset($row['internal-psid']);
unset($row['internal-iaid']); unset($row['internal-verb']);
unset($row['internal-user-ignored']); unset($row['internal-user-ignored']);
unset($row['interaction']); unset($row['interaction']);
@ -673,7 +677,7 @@ class Item
'unseen', 'deleted', 'origin', 'forum_mode', 'mention', 'global', 'unseen', 'deleted', 'origin', 'forum_mode', 'mention', 'global',
'id' => 'item_id', 'network', 'icid', 'iaid', 'id' => 'item_id', 'network', 'icid', 'iaid',
'uri-id' => 'internal-uri-id', 'uid' => 'internal-uid', '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) { if ($usermode) {
$fields['user-item'] = ['pinned', 'notification-type', 'ignored' => 'internal-user-ignored']; $fields['user-item'] = ['pinned', 'notification-type', 'ignored' => 'internal-user-ignored'];
@ -685,6 +689,8 @@ class Item
$fields['post-delivery-data'] = array_merge(Post\DeliveryData::LEGACY_FIELD_LIST, Post\DeliveryData::FIELD_LIST); $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['permissionset'] = ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid'];
$fields['author'] = ['url' => 'author-link', 'name' => 'author-name', 'addr' => 'author-addr', $fields['author'] = ['url' => 'author-link', 'name' => 'author-name', 'addr' => 'author-addr',
@ -809,6 +815,10 @@ class Item
$joins .= " LEFT JOIN `post-delivery-data` ON `post-delivery-data`.`uri-id` = `item`.`uri-id` AND `item`.`origin`"; $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) { if (strpos($sql_commands, "`permissionset`.") !== false) {
$joins .= " LEFT JOIN `permissionset` ON `permissionset`.`id` = `item`.`psid`"; $joins .= " LEFT JOIN `permissionset` ON `permissionset`.`id` = `item`.`psid`";
} }
@ -835,11 +845,11 @@ class Item
private static function constructSelectFields(array $fields, array $selected) private static function constructSelectFields(array $fields, array $selected)
{ {
if (!empty($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)) { if (in_array('verb', $selected)) {
$selected[] = 'internal-activity'; $selected = array_merge($selected, ['internal-activity', 'internal-verb']);
} }
if (in_array('ignored', $selected)) { if (in_array('ignored', $selected)) {
@ -1845,11 +1855,18 @@ class Item
$notify_type = Delivery::POST; $notify_type = Delivery::POST;
} }
// We are doing this outside of the transaction to avoid timing problems
if (in_array($item['verb'], self::ACTIVITIES)) {
$item['iaid'] = self::insertActivity($item);
} else {
$item['icid'] = self::insertContent($item);
}
$body = $item['body']; $body = $item['body'];
// We are doing this outside of the transaction to avoid timing problems // We just remove everything that is content
if (!self::insertActivity($item)) { foreach (array_merge(self::CONTENT_FIELDLIST, self::MIXED_CONTENT_FIELDLIST) as $field) {
self::insertContent($item); unset($item[$field]);
} }
$like_no_comment = DI::config()->get('system', 'like_no_comment'); $like_no_comment = DI::config()->get('system', 'like_no_comment');
@ -2013,21 +2030,16 @@ class Item
* @return bool * @return bool
* @throws \Exception * @throws \Exception
*/ */
private static function insertActivity(&$item) private static function insertActivity(array $item)
{ {
$activity_index = self::activityToIndex($item['verb']); $activity_index = self::activityToIndex($item['verb']);
if ($activity_index < 0) { if ($activity_index < 0) {
return false; // SHouldn't happen at all
return null;
} }
$fields = ['activity' => $activity_index, 'uri-hash' => (string)$item['uri-id'], 'uri-id' => $item['uri-id']]; $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. // To avoid timing problems, we are using locks.
$locked = DI::lock()->acquire('item_insert_activity'); $locked = DI::lock()->acquire('item_insert_activity');
if (!$locked) { if (!$locked) {
@ -2037,21 +2049,20 @@ class Item
// Do we already have this content? // Do we already have this content?
$item_activity = DBA::selectFirst('item-activity', ['id'], ['uri-id' => $item['uri-id']]); $item_activity = DBA::selectFirst('item-activity', ['id'], ['uri-id' => $item['uri-id']]);
if (DBA::isResult($item_activity)) { if (DBA::isResult($item_activity)) {
$item['iaid'] = $item_activity['id']; $iaid = $item_activity['id'];
Logger::log('Fetched activity for URI ' . $item['uri'] . ' (' . $item['iaid'] . ')'); Logger::log('Fetched activity for URI ' . $item['uri'] . ' (' . $iaid . ')');
} elseif (DBA::insert('item-activity', $fields)) { } elseif (DBA::insert('item-activity', $fields)) {
$item['iaid'] = DBA::lastInsertId(); $iaid = DBA::lastInsertId();
Logger::log('Inserted activity for URI ' . $item['uri'] . ' (' . $item['iaid'] . ')'); Logger::log('Inserted activity for URI ' . $item['uri'] . ' (' . $iaid . ')');
} else { } else {
// This shouldn't happen. // This shouldn't happen.
$iaid = null;
Logger::log('Could not insert activity for URI ' . $item['uri'] . ' - should not happen'); Logger::log('Could not insert activity for URI ' . $item['uri'] . ' - should not happen');
DI::lock()->release('item_insert_activity');
return false;
} }
if ($locked) { if ($locked) {
DI::lock()->release('item_insert_activity'); DI::lock()->release('item_insert_activity');
} }
return true; return $iaid;
} }
/** /**
@ -2060,14 +2071,13 @@ class Item
* @param array $item The item fields that are to be inserted * @param array $item The item fields that are to be inserted
* @throws \Exception * @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']]; $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) { foreach (array_merge(self::CONTENT_FIELDLIST, self::MIXED_CONTENT_FIELDLIST) as $field) {
if (isset($item[$field])) { if (isset($item[$field])) {
$fields[$field] = $item[$field]; $fields[$field] = $item[$field];
unset($item[$field]);
} }
} }
@ -2080,18 +2090,20 @@ class Item
// Do we already have this content? // Do we already have this content?
$item_content = DBA::selectFirst('item-content', ['id'], ['uri-id' => $item['uri-id']]); $item_content = DBA::selectFirst('item-content', ['id'], ['uri-id' => $item['uri-id']]);
if (DBA::isResult($item_content)) { if (DBA::isResult($item_content)) {
$item['icid'] = $item_content['id']; $icid = $item_content['id'];
Logger::log('Fetched content for URI ' . $item['uri'] . ' (' . $item['icid'] . ')'); Logger::log('Fetched content for URI ' . $item['uri'] . ' (' . $icid . ')');
} elseif (DBA::insert('item-content', $fields)) { } elseif (DBA::insert('item-content', $fields)) {
$item['icid'] = DBA::lastInsertId(); $icid = DBA::lastInsertId();
Logger::log('Inserted content for URI ' . $item['uri'] . ' (' . $item['icid'] . ')'); Logger::log('Inserted content for URI ' . $item['uri'] . ' (' . $icid . ')');
} else { } else {
// This shouldn't happen. // This shouldn't happen.
$icid = null;
Logger::log('Could not insert content for URI ' . $item['uri'] . ' - should not happen'); Logger::log('Could not insert content for URI ' . $item['uri'] . ' - should not happen');
} }
if ($locked) { if ($locked) {
DI::lock()->release('item_insert_content'); DI::lock()->release('item_insert_content');
} }
return $icid;
} }
/** /**