3 * @copyright Copyright (C) 2010-2022, 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;
32 * Insert a new post user entry
34 * @param integer $uri_id
36 * @param array $fields
37 * @return int ID of inserted post-user
40 public static function insert(int $uri_id, int $uid, array $data = [])
43 throw new BadMethodCallException('Empty URI_id');
46 $fields = DI::dbaDefinition()->truncateFieldsForTable('post-user', $data);
48 // Additionally assign the key fields
49 $fields['uri-id'] = $uri_id;
50 $fields['uid'] = $uid;
52 // Public posts are always seen
54 $fields['unseen'] = false;
57 // Does the entry already exist?
58 if (DBA::exists('post-user', ['uri-id' => $uri_id, 'uid' => $uid])) {
59 $postuser = DBA::selectFirst('post-user', [], ['uri-id' => $uri_id, 'uid' => $uid]);
61 // We quit here, when there are obvious differences
62 foreach (['created', 'owner-id', 'author-id', 'vid', 'network', 'private', 'wall', 'origin'] as $key) {
63 if ($fields[$key] != $postuser[$key]) {
69 foreach (['gravity', 'parent-uri-id', 'thr-parent-id'] as $key) {
70 if ($fields[$key] != $postuser[$key]) {
71 $update[$key] = $fields[$key];
75 // When the parents changed, we apply these changes to the existing entry
76 if (!empty($update)) {
77 DBA::update('post-user', $update, ['id' => $postuser['id']]);
78 return $postuser['id'];
84 if (!DBA::insert('post-user', $fields, Database::INSERT_IGNORE)) {
88 return DBA::lastInsertId();
92 * Update a post user entry
94 * @param integer $uri_id
97 * @param bool $insert_if_missing
101 public static function update(int $uri_id, int $uid, array $data = [], bool $insert_if_missing = false)
103 if (empty($uri_id)) {
104 throw new BadMethodCallException('Empty URI_id');
107 $fields = DI::dbaDefinition()->truncateFieldsForTable('post-user', $data);
109 // Remove the key fields
110 unset($fields['uri-id']);
111 unset($fields['uid']);
113 if (empty($fields)) {
117 return DBA::update('post-user', $fields, ['uri-id' => $uri_id, 'uid' => $uid], $insert_if_missing ? true : []);
121 * Delete a row from the post-user table
123 * @param array $conditions Field condition(s)
124 * @param array $options
125 * - cascade: If true we delete records in other tables that depend on the one we're deleting through
126 * relations (default: true)
128 * @return boolean was the delete successful?
131 public static function delete(array $conditions, array $options = [])
133 return DBA::delete('post-user', $conditions, $options);