2020-11-17 17:33:44 -05:00
|
|
|
<?php
|
|
|
|
/**
|
2022-01-02 02:27:47 -05:00
|
|
|
* @copyright Copyright (C) 2010-2022, the Friendica project
|
2020-11-17 17:33:44 -05:00
|
|
|
*
|
|
|
|
* @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;
|
2020-11-19 14:34:48 -05:00
|
|
|
use Friendica\Database\Database;
|
2022-07-12 17:21:16 -04:00
|
|
|
use Friendica\DI;
|
2020-11-17 17:33:44 -05:00
|
|
|
|
|
|
|
class User
|
|
|
|
{
|
|
|
|
/**
|
2021-02-01 05:31:38 -05:00
|
|
|
* Insert a new post user entry
|
2020-11-17 17:33:44 -05:00
|
|
|
*
|
|
|
|
* @param integer $uri_id
|
|
|
|
* @param integer $uid
|
|
|
|
* @param array $fields
|
2021-01-28 17:45:54 -05:00
|
|
|
* @return int ID of inserted post-user
|
2020-11-17 17:33:44 -05:00
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public static function insert(int $uri_id, int $uid, array $data = [])
|
|
|
|
{
|
|
|
|
if (empty($uri_id)) {
|
|
|
|
throw new BadMethodCallException('Empty URI_id');
|
|
|
|
}
|
|
|
|
|
2022-07-12 18:23:12 -04:00
|
|
|
$fields = DI::dbaDefinition()->truncateFieldsForTable('post-user', $data);
|
2020-11-17 17:33:44 -05:00
|
|
|
|
|
|
|
// Additionally assign the key fields
|
|
|
|
$fields['uri-id'] = $uri_id;
|
|
|
|
$fields['uid'] = $uid;
|
|
|
|
|
|
|
|
// Public posts are always seen
|
|
|
|
if ($uid == 0) {
|
|
|
|
$fields['unseen'] = false;
|
|
|
|
}
|
|
|
|
|
2022-07-24 09:09:35 -04:00
|
|
|
// Does the entry already exist?
|
|
|
|
if (DBA::exists('post-user', ['uri-id' => $uri_id, 'uid' => $uid])) {
|
|
|
|
$postuser = DBA::selectFirst('post-user', [], ['uri-id' => $uri_id, 'uid' => $uid]);
|
|
|
|
|
|
|
|
// We quit here, when there are obvious differences
|
|
|
|
foreach (['created', 'owner-id', 'author-id', 'vid', 'network', 'private', 'wall', 'origin'] as $key) {
|
|
|
|
if ($fields[$key] != $postuser[$key]) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$update = [];
|
|
|
|
foreach (['gravity', 'parent-uri-id', 'thr-parent-id'] as $key) {
|
|
|
|
if ($fields[$key] != $postuser[$key]) {
|
|
|
|
$update[$key] = $fields[$key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// When the parents changed, we apply these changes to the existing entry
|
|
|
|
if (!empty($update)) {
|
|
|
|
DBA::update('post-user', $update, ['id' => $postuser['id']]);
|
|
|
|
return $postuser['id'];
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-28 17:45:54 -05:00
|
|
|
if (!DBA::insert('post-user', $fields, Database::INSERT_IGNORE)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DBA::lastInsertId();
|
2020-11-17 17:33:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-01 05:31:38 -05:00
|
|
|
* Update a post user entry
|
2020-11-17 17:33:44 -05:00
|
|
|
*
|
|
|
|
* @param integer $uri_id
|
|
|
|
* @param integer $uid
|
2021-01-22 03:16:41 -05:00
|
|
|
* @param array $data
|
|
|
|
* @param bool $insert_if_missing
|
2020-11-17 17:33:44 -05:00
|
|
|
* @return bool
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2021-01-22 03:16:41 -05:00
|
|
|
public static function update(int $uri_id, int $uid, array $data = [], bool $insert_if_missing = false)
|
2020-11-17 17:33:44 -05:00
|
|
|
{
|
|
|
|
if (empty($uri_id)) {
|
|
|
|
throw new BadMethodCallException('Empty URI_id');
|
|
|
|
}
|
|
|
|
|
2022-07-12 18:23:12 -04:00
|
|
|
$fields = DI::dbaDefinition()->truncateFieldsForTable('post-user', $data);
|
2020-11-17 17:33:44 -05:00
|
|
|
|
|
|
|
// Remove the key fields
|
|
|
|
unset($fields['uri-id']);
|
|
|
|
unset($fields['uid']);
|
|
|
|
|
|
|
|
if (empty($fields)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-22 03:16:41 -05:00
|
|
|
return DBA::update('post-user', $fields, ['uri-id' => $uri_id, 'uid' => $uid], $insert_if_missing ? true : []);
|
2020-11-17 17:33:44 -05:00
|
|
|
}
|
2021-01-28 17:45:54 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
2020-11-17 17:33:44 -05:00
|
|
|
}
|