New table "post-user" and more foreign keys

This commit is contained in:
Michael 2020-11-17 22:33:44 +00:00
parent ba0d3b2435
commit eaa58da25b
7 changed files with 274 additions and 46 deletions

View file

@ -34,7 +34,7 @@ use Friendica\Core\Worker;
use Friendica\Database\DBA;
use Friendica\Database\DBStructure;
use Friendica\DI;
use Friendica\Model\Post\Category;
use Friendica\Model\Post;
use Friendica\Protocol\Activity;
use Friendica\Protocol\ActivityPub;
use Friendica\Protocol\Diaspora;
@ -71,8 +71,6 @@ class Item
const PT_FETCHED = 75;
const PT_PERSONAL_NOTE = 128;
const LOCK_INSERT = 'item-insert';
// Field list that is used to display the items
const DISPLAY_FIELDLIST = [
'uid', 'id', 'parent', 'uri-id', 'uri', 'thr-parent', 'parent-uri', 'guid', 'network', 'gravity',
@ -310,7 +308,7 @@ class Item
if (!array_key_exists('verb', $row) || in_array($row['verb'], ['', Activity::POST, Activity::SHARE])) {
// Build the file string out of the term entries
if (array_key_exists('file', $row) && empty($row['file'])) {
$row['file'] = Category::getTextByURIId($row['internal-uri-id'], $row['internal-uid']);
$row['file'] = Post\Category::getTextByURIId($row['internal-uri-id'], $row['internal-uid']);
}
}
@ -911,6 +909,8 @@ class Item
return false;
}
$data_fields = $fields;
// To ensure the data integrity we do it in an transaction
DBA::transaction();
@ -967,6 +967,8 @@ class Item
$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']);
@ -996,7 +998,7 @@ class Item
}
if (!is_null($files)) {
Category::storeTextByURIId($item['uri-id'], $item['uid'], $files);
Post\Category::storeTextByURIId($item['uri-id'], $item['uid'], $files);
if (!empty($item['file'])) {
DBA::update('item', ['file' => ''], ['id' => $item['id']]);
}
@ -1056,8 +1058,10 @@ class Item
return;
}
$items = self::select(['id', 'uid'], $condition);
$items = self::select(['id', 'uid', 'uri-id'], $condition);
while ($item = self::fetch($items)) {
Post\User::update($item['uri-id'], $item['uid'], ['hidden' => true]);
// "Deleting" global items just means hiding them
if ($item['uid'] == 0) {
DBA::update('user-item', ['hidden' => true], ['iid' => $item['id'], 'uid' => $uid], true);
@ -1158,7 +1162,7 @@ class Item
$item_fields = ['deleted' => true, 'edited' => DateTimeFormat::utcNow(), 'changed' => DateTimeFormat::utcNow()];
DBA::update('item', $item_fields, ['id' => $item['id']]);
Category::storeTextByURIId($item['uri-id'], $item['uid'], '');
Post\Category::storeTextByURIId($item['uri-id'], $item['uid'], '');
self::deleteThread($item['id'], $item['parent-uri']);
if (!self::exists(["`uri` = ? AND `uid` != 0 AND NOT `deleted`", $item['uri']])) {
@ -1191,6 +1195,7 @@ class Item
// send the notification upstream/downstream
Worker::add(['priority' => $priority, 'dont_fork' => true], "Notifier", Delivery::DELETION, intval($item['id']));
} elseif ($item['uid'] != 0) {
Post\User::update($item['uri-id'], $item['uid'], ['hidden' => true]);
// When we delete just our local user copy of an item, we have to set a marker to hide it
$global_item = self::selectFirst(['id'], ['uri' => $item['uri'], 'uid' => 0, 'deleted' => false]);
@ -1862,7 +1867,7 @@ class Item
// Attached file links
if (!empty($item['file'])) {
Category::storeTextByURIId($item['uri-id'], $item['uid'], $item['file']);
Post\Category::storeTextByURIId($item['uri-id'], $item['uid'], $item['file']);
}
unset($item['file']);
@ -1888,16 +1893,9 @@ class Item
}
}
$locked = DI::lock()->acquire(self::LOCK_INSERT, 0);
if ($locked || DBA::lock('item')) {
if (Post\User::insert($item['uri-id'], $item['uid'], $item)) {
$condition = ['uri-id' => $item['uri-id'], 'uid' => $item['uid'], 'network' => $item['network']];
if (DBA::exists('item', $condition)) {
if ($locked) {
DI::lock()->release(self::LOCK_INSERT);
} else {
DBA::unlock();
}
Logger::notice('Item is already inserted - aborting', $condition);
return 0;
}
@ -1906,15 +1904,9 @@ class Item
// When the item was successfully stored we fetch the ID of the item.
$current_post = DBA::lastInsertId();
if ($locked) {
DI::lock()->release(self::LOCK_INSERT);
} else {
DBA::unlock();
}
} else {
Logger::warning('Item lock had not been acquired');
$result = false;
$current_post = 0;
Logger::notice('Post-User is already inserted - aborting');
return 0;
}
if (empty($current_post) || !DBA::isResult($result)) {

92
src/Model/Post/User.php Normal file
View file

@ -0,0 +1,92 @@
<?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 Friendica\Database\DBA;
use \BadMethodCallException;
use Friendica\Core\Logger;
use Friendica\Database\DBStructure;
class User
{
/**
* Insert a new URI user entry
*
* @param integer $uri_id
* @param integer $uid
* @param array $fields
* @return bool
* @throws \Exception
*/
public static function insert(int $uri_id, int $uid, array $data = [])
{
if (empty($uri_id)) {
throw new BadMethodCallException('Empty URI_id');
}
if (DBA::exists('post-user', ['uri-id' => $uri_id, 'uid' => $uid])) {
return false;
}
$fields = DBStructure::getFieldsForTable('post-user', $data);
// Additionally assign the key fields
$fields['uri-id'] = $uri_id;
$fields['uid'] = $uid;
// Public posts are always seen
if ($uid == 0) {
$fields['unseen'] = false;
}
return DBA::insert('post-user', $fields);
}
/**
* Update a URI user entry
*
* @param integer $uri_id
* @param integer $uid
* @param array $fields
* @return bool
* @throws \Exception
*/
public static function update(int $uri_id, int $uid, array $data = [])
{
if (empty($uri_id)) {
throw new BadMethodCallException('Empty URI_id');
}
$fields = DBStructure::getFieldsForTable('post-user', $data);
// Remove the key fields
unset($fields['uri-id']);
unset($fields['uid']);
if (empty($fields)) {
return true;
}
Logger::info('Blubb-Update', ['uri-id' => $uri_id, 'uid' => $uid, 'fields' => $fields]);
return DBA::update('post-user', $fields, ['uri-id' => $uri_id, 'uid' => $uid], true);
}
}

View file

@ -156,7 +156,9 @@ class UserItem
Logger::info('Set notification', ['iid' => $item['id'], 'uid' => $uid, 'notification-type' => $notification_type]);
DBA::update('user-item', ['notification-type' => $notification_type], ['iid' => $item['id'], 'uid' => $uid], true);
$fields = ['notification-type' => $notification_type];
Post\User::update($item['uri-id'], $uid, $fields);
DBA::update('user-item', $fields, ['iid' => $item['id'], 'uid' => $uid], true);
}
/**