3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Model\Post;
24 use Friendica\Database\DBA;
25 use \BadMethodCallException;
26 use Friendica\Database\Database;
28 use Friendica\Model\Item;
29 use Friendica\Protocol\Activity;
34 * Insert a new post user entry
36 * @param integer $uri_id
38 * @param array $fields
39 * @return int ID of inserted post-user
42 public static function insert(int $uri_id, int $uid, array $data = [])
45 throw new BadMethodCallException('Empty URI_id');
48 $fields = DI::dbaDefinition()->truncateFieldsForTable('post-user', $data);
50 // Additionally assign the key fields
51 $fields['uri-id'] = $uri_id;
52 $fields['uid'] = $uid;
54 // Public posts and activities (like, dislike, ...) are always seen
55 if ($uid == 0 || (($data['gravity'] == Item::GRAVITY_ACTIVITY) && ($data['verb'] != Activity::ANNOUNCE))) {
56 $fields['unseen'] = false;
59 // Does the entry already exist?
60 if (DBA::exists('post-user', ['uri-id' => $uri_id, 'uid' => $uid])) {
61 $postuser = DBA::selectFirst('post-user', [], ['uri-id' => $uri_id, 'uid' => $uid]);
63 // We quit here, when there are obvious differences
64 foreach (['created', 'owner-id', 'author-id', 'vid', 'network', 'private', 'wall', 'origin'] as $key) {
65 if ($fields[$key] != $postuser[$key]) {
71 foreach (['gravity', 'parent-uri-id', 'thr-parent-id'] as $key) {
72 if ($fields[$key] != $postuser[$key]) {
73 $update[$key] = $fields[$key];
77 // When the parents changed, we apply these changes to the existing entry
78 if (!empty($update)) {
79 DBA::update('post-user', $update, ['id' => $postuser['id']]);
80 return $postuser['id'];
86 if (!DBA::insert('post-user', $fields, Database::INSERT_IGNORE)) {
90 return DBA::lastInsertId();
94 * Update a post user entry
96 * @param integer $uri_id
99 * @param bool $insert_if_missing
103 public static function update(int $uri_id, int $uid, array $data = [], bool $insert_if_missing = false)
105 if (empty($uri_id)) {
106 throw new BadMethodCallException('Empty URI_id');
109 $fields = DI::dbaDefinition()->truncateFieldsForTable('post-user', $data);
111 // Remove the key fields
112 unset($fields['uri-id']);
113 unset($fields['uid']);
115 if (empty($fields)) {
119 return DBA::update('post-user', $fields, ['uri-id' => $uri_id, 'uid' => $uid], $insert_if_missing ? true : []);
123 * Delete a row from the post-user table
125 * @param array $conditions Field condition(s)
126 * @param array $options
127 * - cascade: If true we delete records in other tables that depend on the one we're deleting through
128 * relations (default: true)
130 * @return boolean was the delete successful?
133 public static function delete(array $conditions, array $options = [])
135 return DBA::delete('post-user', $conditions, $options);