From 15162b4027c7726a325cc1907aa269bc7b45aeea Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 31 Jan 2021 18:32:22 +0000 Subject: [PATCH] New table "post-thread-user" --- include/api.php | 10 ++-- src/Model/Item.php | 4 ++ src/Model/Post.php | 33 +++++++++--- src/Model/Post/ThreadUser.php | 99 +++++++++++++++++++++++++++++++++++ static/dbstructure.config.php | 18 +++++++ update.php | 23 ++++++-- 6 files changed, 170 insertions(+), 17 deletions(-) create mode 100644 src/Model/Post/ThreadUser.php diff --git a/include/api.php b/include/api.php index bcf14d6a82..8543eef9e7 100644 --- a/include/api.php +++ b/include/api.php @@ -2170,10 +2170,10 @@ function api_statuses_mentions($type) $start = max(0, ($page - 1) * $count); - $query = "`gravity` IN (?, ?) AND `id` IN (SELECT `iid` FROM `user-item` + $query = "`gravity` IN (?, ?) AND `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE (`hidden` IS NULL OR NOT `hidden`) AND - `uid` = ? AND `notification-type` & ? != 0 - AND `iid` > ?"; + `uid` = ? AND `notification-type` & ? != 0) + AND `id` > ?"; $condition = [GRAVITY_PARENT, GRAVITY_COMMENT, api_user(), UserItem::NOTIF_EXPLICIT_TAGGED | UserItem::NOTIF_IMPLICIT_TAGGED | @@ -2182,12 +2182,10 @@ function api_statuses_mentions($type) $since_id]; if ($max_id > 0) { - $query .= " AND `iid` <= ?"; + $query .= " AND `id` <= ?"; $condition[] = $max_id; } - $query .= ")"; - array_unshift($condition, $query); $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; diff --git a/src/Model/Item.php b/src/Model/Item.php index 6d882463fb..bb371e5324 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -1099,6 +1099,10 @@ class Item $id = Post\User::insert($item['uri-id'], $item['uid'], $item); if ($id) { + if ($item['gravity'] == GRAVITY_PARENT) { + Post\ThreadUser::insert($item['uri-id'], $item['uid'], $item); + } + // Remove all fields that aren't part of the item table foreach ($item as $field => $value) { if (!in_array($field, $structure['item'])) { diff --git a/src/Model/Post.php b/src/Model/Post.php index 59e2f419c5..5bb41d16c2 100644 --- a/src/Model/Post.php +++ b/src/Model/Post.php @@ -258,7 +258,7 @@ class Post AND (NOT `causer-blocked` OR `causer-id` = ?) AND NOT `contact-blocked` AND ((NOT `contact-readonly` AND NOT `contact-pending` AND (`contact-rel` IN (?, ?))) OR `self` OR `gravity` != ? OR `contact-uid` = ?) - AND NOT EXISTS (SELECT `iid` FROM `user-item` WHERE `hidden` AND `iid` = `id` AND `uid` = ?) + AND NOT EXISTS (SELECT `uri-id` FROM `post-user` WHERE `hidden` AND `uri-id` = `" . $view . "`.`uri-id` AND `uid` = ?) AND NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `author-id` AND `blocked`) AND NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `owner-id` AND `blocked`) AND NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `author-id` AND `ignored` AND `gravity` = ?) @@ -431,6 +431,8 @@ class Post unset($fields['parent-uri']); unset($fields['parent-uri-id']); + $thread_condition = DBA::mergeConditions($condition, ['gravity' => GRAVITY_PARENT]); + // To ensure the data integrity we do it in an transaction DBA::transaction(); @@ -472,13 +474,32 @@ class Post $affected = max($affected, DBA::affectedRows()); } + $update_fields = DBStructure::getFieldsForTable('post-thread-user', $fields); + if (!empty($update_fields)) { + $rows = DBA::selectToArray('post-view', ['post-user-id'], $thread_condition); + $thread_puids = array_column($rows, 'post-user-id'); + + $post_thread_condition = DBA::collapseCondition(['id' => $thread_puids]); + + $post_thread_condition[0] = "EXISTS(SELECT `id` FROM `post-user` WHERE " . + $post_thread_condition[0] . " AND `uri-id` = `post-thread-user`.`uri-id` AND `uid` = `post-thread-user`.`uid`)"; + Logger::info('Test2-start', ['condition' => $post_thread_condition]); + if (!DBA::update('post-thread-user', $update_fields, $post_thread_condition)) { + DBA::rollback(); + Logger::notice('Updating post-thread-user failed', ['fields' => $update_fields, 'condition' => $condition]); + return false; + } + Logger::info('Test2-end'); + $affected = max($affected, DBA::affectedRows()); + } + $update_fields = DBStructure::getFieldsForTable('thread', $fields); if (!empty($update_fields)) { - $rows = DBA::selectToArray('post-view', ['id'], $condition); + $rows = DBA::selectToArray('post-view', ['id'], $thread_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]); + Logger::notice('Updating thread failed', ['fields' => $update_fields, 'condition' => $thread_condition]); return false; } $affected = max($affected, DBA::affectedRows()); @@ -496,10 +517,8 @@ class Post } } if (!empty($update_fields)) { - if (empty($ids)) { - $rows = DBA::selectToArray('post-view', ['id'], $condition, []); - $ids = array_column($rows, 'id'); - } + $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]); diff --git a/src/Model/Post/ThreadUser.php b/src/Model/Post/ThreadUser.php new file mode 100644 index 0000000000..c824b44c89 --- /dev/null +++ b/src/Model/Post/ThreadUser.php @@ -0,0 +1,99 @@ +. + * + */ + +namespace Friendica\Model\Post; + +use \BadMethodCallException; +use Friendica\Database\Database; +use Friendica\Database\DBA; +use Friendica\Database\DBStructure; + +class ThreadUser +{ + /** + * Insert a new URI user entry + * + * @param integer $uri_id + * @param integer $uid + * @param array $fields + * @return bool success + * @throws \Exception + */ + public static function insert(int $uri_id, int $uid, array $data = []) + { + if (empty($uri_id)) { + throw new BadMethodCallException('Empty URI_id'); + } + + $fields = DBStructure::getFieldsForTable('post-thread-user', $data); + + // Additionally assign the key fields + $fields['uri-id'] = $uri_id; + $fields['uid'] = $uid; + + return DBA::insert('post-thread-user', $fields, Database::INSERT_IGNORE); + } + + /** + * Update a URI user entry + * + * @param integer $uri_id + * @param integer $uid + * @param array $data + * @param bool $insert_if_missing + * @return bool + * @throws \Exception + */ + public static function update(int $uri_id, int $uid, array $data = [], bool $insert_if_missing = false) + { + if (empty($uri_id)) { + throw new BadMethodCallException('Empty URI_id'); + } + + $fields = DBStructure::getFieldsForTable('post-thread-user', $data); + + // Remove the key fields + unset($fields['uri-id']); + unset($fields['uid']); + + if (empty($fields)) { + return true; + } + + return DBA::update('post-thread-user', $fields, ['uri-id' => $uri_id, 'uid' => $uid], $insert_if_missing ? true : []); + } + + /** + * Delete a row from the post-thread-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-thread-user', $conditions, $options); + } +} diff --git a/static/dbstructure.config.php b/static/dbstructure.config.php index d0c35e4f8d..e38e234696 100644 --- a/static/dbstructure.config.php +++ b/static/dbstructure.config.php @@ -1193,6 +1193,24 @@ return [ "cid" => ["cid"] ] ], + "post-thread-user" => [ + "comment" => "Thread related data per user", + "fields" => [ + "uri-id" => ["type" => "int unsigned", "not null" => "1", "primary" => "1", "foreign" => ["item-uri" => "id"], "comment" => "Id of the item-uri table entry that contains the item uri"], + "uid" => ["type" => "mediumint unsigned", "not null" => "1", "default" => "0", "primary" => "1", "foreign" => ["user" => "uid"], "comment" => "Owner id which owns this copy of the item"], + "pinned" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => "The thread is pinned on the profile page"], + "starred" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""], + "ignored" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => "Ignore updates for this thread"], + "wall" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => "This item was posted to the wall of uid"], + "pubmail" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""], + "forum_mode" => ["type" => "tinyint unsigned", "not null" => "1", "default" => "0", "comment" => ""] + ], + "indexes" => [ + "PRIMARY" => ["uid", "uri-id"], + "uid_wall" => ["uid", "wall"], + "uri-id" => ["uri-id"], + ] + ], "post-user" => [ "comment" => "User specific post data", "fields" => [ diff --git a/update.php b/update.php index 3e9292f2e4..f2dd665779 100644 --- a/update.php +++ b/update.php @@ -466,7 +466,7 @@ function pre_update_1364() return Update::FAILED; } - if (!DBA::e("DELETE FROM `user-item` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) { + if (DBStructure::existsTable('user-item') && !DBA::e("DELETE FROM `user-item` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) { return Update::FAILED; } @@ -518,7 +518,7 @@ function pre_update_1364() return Update::FAILED; } - if (!DBA::e("DELETE FROM `user-item` WHERE NOT `iid` IN (SELECT `id` FROM `item`)")) { + if (DBStructure::existsTable('user-item') && !DBA::e("DELETE FROM `user-item` WHERE NOT `iid` IN (SELECT `id` FROM `item`)")) { return Update::FAILED; } @@ -686,7 +686,7 @@ function update_1395() return Update::FAILED; } - if (!DBA::e("INSERT INTO `post-user`(`uri-id`, `uid`, `hidden`, `notification-type`) + if (DBStructure::existsTable('user-item') && !DBA::e("INSERT INTO `post-user`(`uri-id`, `uid`, `hidden`, `notification-type`) SELECT `uri-id`, `user-item`.`uid`, `hidden`,`notification-type` FROM `user-item` INNER JOIN `item` ON `item`.`id` = `user-item`.`iid` ON DUPLICATE KEY UPDATE `hidden` = `user-item`.`hidden`, `notification-type` = `user-item`.`notification-type`")) { @@ -713,4 +713,19 @@ function update_1396() return Update::FAILED; } return Update::SUCCESS; -} \ No newline at end of file +} + +function update_139x() +{ + if (!DBStructure::existsTable('thread') || DBStructure::existsTable('user-item')) { + return Update::SUCCESS; + } + + if (!DBA::e("INSERT IGNORE INTO `post-thread-user`(`uri-id`, `uid`, `pinned`, `starred`, `ignored`, `wall`, `pubmail`, `forum_mode`) + SELECT `thread`.`uri-id`, `thread`.`uid`, `user-item`.`pinned`, `thread`.`starred`, + `thread`.`ignored`, `thread`.`wall`, `thread`.`pubmail`, `thread`.`forum_mode` + FROM `thread` LEFT JOIN `user-item` ON `user-item`.`iid` = `thread`.`iid`")) { + return Update::FAILED; + } + return Update::SUCCESS; +}