]> git.mxchange.org Git - friendica.git/blob - src/Model/Post/ThreadUser.php
Merge pull request #12298 from annando/api-suggestions
[friendica.git] / src / Model / Post / ThreadUser.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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 \BadMethodCallException;
25 use Friendica\Database\Database;
26 use Friendica\Database\DBA;
27 use Friendica\Database\DBStructure;
28 use Friendica\DI;
29
30 class ThreadUser
31 {
32         /**
33          * Insert a new URI user entry
34          *
35          * @param integer $uri_id
36          * @param integer $uid
37          * @param array   $fields
38          * @return bool   success
39          * @throws \Exception
40          */
41         public static function insert(int $uri_id, int $uid, array $data = [])
42         {
43                 if (empty($uri_id)) {
44                         throw new BadMethodCallException('Empty URI_id');
45                 }
46
47                 $fields = DI::dbaDefinition()->truncateFieldsForTable('post-thread-user', $data);
48
49                 // Additionally assign the key fields
50                 $fields['uri-id'] = $uri_id;
51                 $fields['uid'] = $uid;
52
53                 return DBA::insert('post-thread-user', $fields, Database::INSERT_IGNORE);
54         }
55
56         /**
57          * Update a URI user entry
58          *
59          * @param integer $uri_id
60          * @param integer $uid
61          * @param array   $data
62          * @param bool    $insert_if_missing
63          * @return bool
64          * @throws \Exception
65          */
66         public static function update(int $uri_id, int $uid, array $data = [], bool $insert_if_missing = false)
67         {
68                 if (empty($uri_id)) {
69                         throw new BadMethodCallException('Empty URI_id');
70                 }
71
72                 $fields = DI::dbaDefinition()->truncateFieldsForTable('post-thread-user', $data);
73
74                 // Remove the key fields
75                 unset($fields['uri-id']);
76                 unset($fields['uid']);
77
78                 if (empty($fields)) {
79                         return true;
80                 }
81
82                 return DBA::update('post-thread-user', $fields, ['uri-id' => $uri_id, 'uid' => $uid], $insert_if_missing ? true : []);
83         }
84
85         /**
86          * Delete a row from the post-thread-user table
87          *
88          * @param array        $conditions Field condition(s)
89          * @param array        $options
90          *                           - cascade: If true we delete records in other tables that depend on the one we're deleting through
91          *                           relations (default: true)
92          *
93          * @return boolean was the delete successful?
94          * @throws \Exception
95          */
96         public static function delete(array $conditions, array $options = [])
97         {
98                 return DBA::delete('post-thread-user', $conditions, $options);
99         }
100
101         /**
102          * @param int $uri_id 
103          * @param int $uid 
104          * @return bool 
105          * @throws Exception 
106          */
107         public static function getIgnored(int $uri_id, int $uid)
108         {
109                 $threaduser = DBA::selectFirst('post-thread-user', ['ignored'], ['uri-id' => $uri_id, 'uid' => $uid]);
110                 if (empty($threaduser)) {
111                         return false;
112                 }
113                 return (bool)$threaduser['ignored'];
114         }
115
116         /**
117          * @param int $uri_id 
118          * @param int $uid 
119          * @param int $ignored 
120          * @return void 
121          * @throws Exception 
122          */
123         public static function setIgnored(int $uri_id, int $uid, int $ignored)
124         {
125                 DBA::update('post-thread-user', ['ignored' => $ignored], ['uri-id' => $uri_id, 'uid' => $uid], true);
126         }
127 }