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

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