New table "post-thread-user"

This commit is contained in:
Michael 2021-01-31 18:32:22 +00:00
parent 998f127cb8
commit 15162b4027
6 changed files with 170 additions and 17 deletions

View File

@ -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]];

View File

@ -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'])) {

View File

@ -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]);

View File

@ -0,0 +1,99 @@
<?php
/**
* @copyright Copyright (C) 2020, Friendica
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
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);
}
}

View File

@ -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" => [

View File

@ -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;
}
}
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;
}