]> git.mxchange.org Git - friendica.git/blob - src/Model/Post/User.php
Merge pull request #13227 from BirdboyBolu/develop
[friendica.git] / src / Model / Post / User.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Model\Post;
23
24 use Friendica\Database\DBA;
25 use \BadMethodCallException;
26 use Friendica\Database\Database;
27 use Friendica\DI;
28 use Friendica\Model\Item;
29 use Friendica\Protocol\Activity;
30
31 class User
32 {
33         /**
34          * Insert a new post user entry
35          *
36          * @param integer $uri_id
37          * @param integer $uid
38          * @param array   $fields
39          * @return int    ID of inserted post-user
40          * @throws \Exception
41          */
42         public static function insert(int $uri_id, int $uid, array $data = [])
43         {
44                 if (empty($uri_id)) {
45                         throw new BadMethodCallException('Empty URI_id');
46                 }
47
48                 $fields = DI::dbaDefinition()->truncateFieldsForTable('post-user', $data);
49
50                 // Additionally assign the key fields
51                 $fields['uri-id'] = $uri_id;
52                 $fields['uid'] = $uid;
53
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;
57                 }
58
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]);
62
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]) {
66                                         return 0;
67                                 }
68                         }
69
70                         $update = [];
71                         foreach (['gravity', 'parent-uri-id', 'thr-parent-id'] as $key) {
72                                 if ($fields[$key] != $postuser[$key]) {
73                                         $update[$key] = $fields[$key];
74                                 }
75                         }
76
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'];
81                         } else {
82                                 return 0;
83                         }
84                 }
85
86                 if (!DBA::insert('post-user', $fields, Database::INSERT_IGNORE)) {
87                         return 0;
88                 }
89
90                 return DBA::lastInsertId();
91         }
92
93         /**
94          * Update a post user entry
95          *
96          * @param integer $uri_id
97          * @param integer $uid
98          * @param array   $data
99          * @param bool    $insert_if_missing
100          * @return bool
101          * @throws \Exception
102          */
103         public static function update(int $uri_id, int $uid, array $data = [], bool $insert_if_missing = false)
104         {
105                 if (empty($uri_id)) {
106                         throw new BadMethodCallException('Empty URI_id');
107                 }
108
109                 $fields = DI::dbaDefinition()->truncateFieldsForTable('post-user', $data);
110
111                 // Remove the key fields
112                 unset($fields['uri-id']);
113                 unset($fields['uid']);
114
115                 if (empty($fields)) {
116                         return true;
117                 }
118
119                 return DBA::update('post-user', $fields, ['uri-id' => $uri_id, 'uid' => $uid], $insert_if_missing ? true : []);
120         }
121
122         /**
123          * Delete a row from the post-user table
124          *
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)
129          *
130          * @return boolean was the delete successful?
131          * @throws \Exception
132          */
133         public static function delete(array $conditions, array $options = [])
134         {
135                 return DBA::delete('post-user', $conditions, $options);
136         }
137 }