mirror of
https://github.com/friendica/friendica
synced 2025-04-27 21:50:11 +00:00
Merge pull request #9873 from annando/post-user
New "post-user" structure, new update functionality
This commit is contained in:
commit
9a4128a5c8
10 changed files with 258 additions and 72 deletions
|
@ -196,84 +196,45 @@ class Item
|
|||
return false;
|
||||
}
|
||||
|
||||
$data_fields = $fields;
|
||||
|
||||
// To ensure the data integrity we do it in an transaction
|
||||
DBA::transaction();
|
||||
|
||||
// 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', 'uid'], $condition);
|
||||
|
||||
$content_fields = [];
|
||||
foreach (array_merge(self::CONTENT_FIELDLIST, self::MIXED_CONTENT_FIELDLIST) as $field) {
|
||||
if (isset($fields[$field])) {
|
||||
$content_fields[$field] = $fields[$field];
|
||||
unset($fields[$field]);
|
||||
}
|
||||
if (!empty($fields['verb'])) {
|
||||
$fields['vid'] = Verb::getID($fields['verb']);
|
||||
}
|
||||
|
||||
$delivery_data = Post\DeliveryData::extractFields($fields);
|
||||
|
||||
$clear_fields = ['bookmark', 'type', 'author-name', 'author-avatar', 'author-link', 'owner-name', 'owner-avatar', 'owner-link', 'postopts', 'inform'];
|
||||
foreach ($clear_fields as $field) {
|
||||
unset($fields[$field]);
|
||||
$rows = Post::update($fields, $condition);
|
||||
if (is_bool($rows)) {
|
||||
return $rows;
|
||||
}
|
||||
|
||||
if (array_key_exists('file', $fields)) {
|
||||
$files = $fields['file'];
|
||||
unset($fields['file']);
|
||||
} else {
|
||||
$files = null;
|
||||
// We only need to call the line by line update for specific fields
|
||||
if (empty($fields['body']) && empty($fields['file']) &&
|
||||
empty($fields['attach']) && empty($fields['edited'])) {
|
||||
return $rows;
|
||||
}
|
||||
|
||||
if (!empty($content_fields['verb'])) {
|
||||
$fields['vid'] = Verb::getID($content_fields['verb']);
|
||||
}
|
||||
Logger::info('Updating per single row method', ['fields' => $fields, 'condition' => $condition]);
|
||||
|
||||
if (!empty($fields)) {
|
||||
$success = DBA::update('item', $fields, $condition);
|
||||
|
||||
if (!$success) {
|
||||
DBA::close($items);
|
||||
DBA::rollback();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// When there is no content for the "old" item table, this will count the fetched items
|
||||
$rows = DBA::affectedRows();
|
||||
$items = Post::select(['id', 'origin', 'uri-id', 'uid'], $condition);
|
||||
|
||||
$notify_items = [];
|
||||
|
||||
while ($item = DBA::fetch($items)) {
|
||||
Post\User::update($item['uri-id'], $item['uid'], $data_fields);
|
||||
|
||||
if (empty($content_fields['verb']) || !in_array($content_fields['verb'], self::ACTIVITIES)) {
|
||||
if (!empty($content_fields['body'])) {
|
||||
$content_fields['raw-body'] = trim($content_fields['raw-body'] ?? $content_fields['body']);
|
||||
|
||||
// Remove all media attachments from the body and store them in the post-media table
|
||||
$content_fields['raw-body'] = Post\Media::insertFromBody($item['uri-id'], $content_fields['raw-body']);
|
||||
$content_fields['raw-body'] = self::setHashtags($content_fields['raw-body']);
|
||||
}
|
||||
|
||||
if (!empty($fields['body'])) {
|
||||
$content_fields = ['raw-body' => trim($fields['raw-body'] ?? $fields['body'])];
|
||||
|
||||
// Remove all media attachments from the body and store them in the post-media table
|
||||
$content_fields['raw-body'] = Post\Media::insertFromBody($item['uri-id'], $content_fields['raw-body']);
|
||||
$content_fields['raw-body'] = self::setHashtags($content_fields['raw-body']);
|
||||
self::updateContent($content_fields, ['uri-id' => $item['uri-id']]);
|
||||
}
|
||||
|
||||
if (!is_null($files)) {
|
||||
Post\Category::storeTextByURIId($item['uri-id'], $item['uid'], $files);
|
||||
if (!empty($fields['file'])) {
|
||||
Post\Category::storeTextByURIId($item['uri-id'], $item['uid'], $fields['file']);
|
||||
}
|
||||
|
||||
if (!empty($fields['attach'])) {
|
||||
Post\Media::insertFromAttachment($item['uri-id'], $fields['attach']);
|
||||
}
|
||||
|
||||
Post\DeliveryData::update($item['uri-id'], $delivery_data);
|
||||
|
||||
self::updateThread($item['id']);
|
||||
|
||||
// We only need to notfiy others when it is an original entry from us.
|
||||
// Only call the notifier when the item has some content relevant change.
|
||||
if ($item['origin'] && in_array('edited', array_keys($fields))) {
|
||||
|
@ -282,7 +243,6 @@ class Item
|
|||
}
|
||||
|
||||
DBA::close($items);
|
||||
DBA::commit();
|
||||
|
||||
foreach ($notify_items as $notify_item) {
|
||||
Worker::add(PRIORITY_HIGH, "Notifier", Delivery::POST, $notify_item);
|
||||
|
@ -1140,7 +1100,8 @@ class Item
|
|||
Tag::storeFromBody($item['uri-id'], $body);
|
||||
}
|
||||
|
||||
if (Post\User::insert($item['uri-id'], $item['uid'], $item)) {
|
||||
$id = Post\User::insert($item['uri-id'], $item['uid'], $item);
|
||||
if ($id) {
|
||||
// Remove all fields that aren't part of the item table
|
||||
foreach ($item as $field => $value) {
|
||||
if (!in_array($field, $structure['item'])) {
|
||||
|
@ -1148,6 +1109,9 @@ class Item
|
|||
}
|
||||
}
|
||||
|
||||
// We syncronize the id value of the of the post-user table with the item table
|
||||
$item['id'] = $id;
|
||||
|
||||
$condition = ['uri-id' => $item['uri-id'], 'uid' => $item['uid'], 'network' => $item['network']];
|
||||
if (Post::exists($condition)) {
|
||||
Logger::notice('Item is already inserted - aborting', $condition);
|
||||
|
@ -1991,7 +1955,8 @@ class Item
|
|||
if (($community_page || $prvgroup) &&
|
||||
!$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]);
|
||||
DBA::delete('item', ['uri-id' => $item['uri-id'], 'uid' => $item['uid']]);
|
||||
Post\User::delete(['uri-id' => $item['uri-id'], 'uid' => $item['uid']]);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
@ -2670,6 +2635,7 @@ class Item
|
|||
$condition = ["`uri-id` = ? AND NOT `deleted` AND NOT (`uid` IN (?, 0))", $uri_id, $item["uid"]];
|
||||
if (!Post::exists($condition)) {
|
||||
DBA::delete('item', ['uri-id' => $uri_id, 'uid' => 0]);
|
||||
Post\User::delete(['uri-id' => $uri_id, 'uid' => 0]);
|
||||
Logger::debug('Deleted shadow item', ['id' => $itemid, 'uri-id' => $uri_id]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,7 +21,9 @@
|
|||
|
||||
namespace Friendica\Model;
|
||||
|
||||
use Friendica\Core\Logger;
|
||||
use Friendica\Database\DBA;
|
||||
use Friendica\Database\DBStructure;
|
||||
use Friendica\Protocol\Activity;
|
||||
|
||||
class Post
|
||||
|
@ -400,4 +402,115 @@ class Post
|
|||
|
||||
return self::selectThreadForUser($uid, $selected, $condition, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update existing post entries
|
||||
*
|
||||
* @param array $fields The fields that are to be changed
|
||||
* @param array $condition The condition for finding the item entries
|
||||
*
|
||||
* A return value of "0" doesn't mean an error - but that 0 rows had been changed.
|
||||
*
|
||||
* @return integer|boolean number of affected rows - or "false" if there was an error
|
||||
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
||||
*/
|
||||
public static function update(array $fields, array $condition)
|
||||
{
|
||||
$affected = 0;
|
||||
|
||||
Logger::info('Start Update', ['fields' => $fields, 'condition' => $condition]);
|
||||
|
||||
// Don't allow changes to fields that are responsible for the relation between the records
|
||||
unset($fields['id']);
|
||||
unset($fields['parent']);
|
||||
unset($fields['uid']);
|
||||
unset($fields['uri']);
|
||||
unset($fields['uri-id']);
|
||||
unset($fields['thr-parent']);
|
||||
unset($fields['thr-parent-id']);
|
||||
unset($fields['parent-uri']);
|
||||
unset($fields['parent-uri-id']);
|
||||
|
||||
// To ensure the data integrity we do it in an transaction
|
||||
DBA::transaction();
|
||||
|
||||
$update_fields = DBStructure::getFieldsForTable('post-user', $fields);
|
||||
if (!empty($update_fields)) {
|
||||
$rows = DBA::selectToArray('post-view', ['post-user-id'], $condition);
|
||||
$puids = array_column($rows, 'post-user-id');
|
||||
if (!DBA::update('post-user', $update_fields, ['id' => $puids])) {
|
||||
DBA::rollback();
|
||||
Logger::notice('Updating post-user failed', ['fields' => $update_fields, 'condition' => $condition]);
|
||||
return false;
|
||||
}
|
||||
$affected = DBA::affectedRows();
|
||||
}
|
||||
|
||||
$update_fields = DBStructure::getFieldsForTable('item-content', $fields);
|
||||
if (!empty($update_fields)) {
|
||||
$rows = DBA::selectToArray('post-view', ['uri-id'], $condition, ['group_by' => ['uri-id']]);
|
||||
$uriids = array_column($rows, 'uri-id');
|
||||
if (!DBA::update('item-content', $update_fields, ['uri-id' => $uriids])) {
|
||||
DBA::rollback();
|
||||
Logger::notice('Updating item-content failed', ['fields' => $update_fields, 'condition' => $condition]);
|
||||
return false;
|
||||
}
|
||||
$affected = max($affected, DBA::affectedRows());
|
||||
}
|
||||
|
||||
$update_fields = Post\DeliveryData::extractFields($fields);
|
||||
if (!empty($update_fields)) {
|
||||
if (empty($uriids)) {
|
||||
$rows = DBA::selectToArray('post-view', ['uri-id'], $condition, ['group_by' => ['uri-id']]);
|
||||
$uriids = array_column($rows, 'uri-id');
|
||||
}
|
||||
if (!DBA::update('post-delivery-data', $update_fields, ['uri-id' => $uriids])) {
|
||||
DBA::rollback();
|
||||
Logger::notice('Updating post-delivery-data failed', ['fields' => $update_fields, 'condition' => $condition]);
|
||||
return false;
|
||||
}
|
||||
$affected = max($affected, DBA::affectedRows());
|
||||
}
|
||||
|
||||
$update_fields = DBStructure::getFieldsForTable('thread', $fields);
|
||||
if (!empty($update_fields)) {
|
||||
$rows = DBA::selectToArray('post-view', ['id'], $condition);
|
||||
$ids = array_column($rows, 'id');
|
||||
if (!DBA::update('thread', $update_fields, ['iid' => $ids])) {
|
||||
DBA::rollback();
|
||||
Logger::notice('Updating thread failed', ['fields' => $update_fields, 'condition' => $condition]);
|
||||
return false;
|
||||
}
|
||||
$affected = max($affected, DBA::affectedRows());
|
||||
}
|
||||
|
||||
$item_fields = ['guid', 'type', 'wall', 'gravity', 'extid', 'created', 'edited', 'commented', 'received', 'changed',
|
||||
'resource-id', 'post-type', 'private', 'pubmail', 'moderated', 'visible', 'starred', 'bookmark',
|
||||
'unseen', 'deleted', 'origin', 'forum_mode', 'mention', 'global', 'network', 'vid', 'psid',
|
||||
'contact-id', 'author-id', 'owner-id', 'causer-id', 'event-id'];
|
||||
|
||||
$update_fields = [];
|
||||
foreach ($item_fields as $field) {
|
||||
if (array_key_exists($field, $fields)) {
|
||||
$update_fields[$field] = $fields[$field];
|
||||
}
|
||||
}
|
||||
if (!empty($update_fields)) {
|
||||
if (empty($ids)) {
|
||||
$rows = DBA::selectToArray('post-view', ['id'], $condition, []);
|
||||
$ids = array_column($rows, 'id');
|
||||
}
|
||||
if (!DBA::update('item', $update_fields, ['id' => $ids])) {
|
||||
DBA::rollback();
|
||||
Logger::notice('Updating item failed', ['fields' => $update_fields, 'condition' => $condition]);
|
||||
return false;
|
||||
}
|
||||
$affected = max($affected, DBA::affectedRows());
|
||||
}
|
||||
|
||||
DBA::commit();
|
||||
|
||||
Logger::info('Updated posts', ['rows' => $affected]);
|
||||
return $affected;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,7 +34,7 @@ class User
|
|||
* @param integer $uri_id
|
||||
* @param integer $uid
|
||||
* @param array $fields
|
||||
* @return bool
|
||||
* @return int ID of inserted post-user
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function insert(int $uri_id, int $uid, array $data = [])
|
||||
|
@ -58,7 +58,11 @@ class User
|
|||
$fields['unseen'] = false;
|
||||
}
|
||||
|
||||
return DBA::insert('post-user', $fields, Database::INSERT_IGNORE);
|
||||
if (!DBA::insert('post-user', $fields, Database::INSERT_IGNORE)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return DBA::lastInsertId();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -89,4 +93,20 @@ class User
|
|||
|
||||
return DBA::update('post-user', $fields, ['uri-id' => $uri_id, 'uid' => $uid], $insert_if_missing ? true : []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a row from the post-user table
|
||||
*
|
||||
* @param array $conditions Field condition(s)
|
||||
* @param array $options
|
||||
* - cascade: If true we delete records in other tables that depend on the one we're deleting through
|
||||
* relations (default: true)
|
||||
*
|
||||
* @return boolean was the delete successful?
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function delete(array $conditions, array $options = [])
|
||||
{
|
||||
return DBA::delete('post-user', $conditions, $options);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue