5107e71ab499967db88cc6851e2f76af186d0e64
[friendica.git/.git] / src / Model / Post.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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;
23
24 use Friendica\Database\DBA;
25 use Friendica\Protocol\Activity;
26
27 class Post
28 {
29         /**
30          * Fetch a single post row
31          *
32          * @param mixed $stmt statement object
33          * @return array|false current row or false
34          * @throws \Exception
35          */
36         public static function fetch($stmt)
37         {
38                 $row = DBA::fetch($stmt);
39
40                 if (!is_array($row)) {
41                         return $row;
42                 }
43
44                 if (array_key_exists('verb', $row)) {
45                         if (in_array($row['verb'], Item::ACTIVITIES)) {
46                                 if (array_key_exists('title', $row)) {
47                                         $row['title'] = '';
48                                 }
49                                 if (array_key_exists('body', $row)) {
50                                         $row['body'] = $row['verb'];
51                                 }
52                                 if (array_key_exists('object', $row)) {
53                                         $row['object'] = '';
54                                 }
55                                 if (array_key_exists('object-type', $row)) {
56                                         $row['object-type'] = Activity\ObjectType::NOTE;
57                                 }
58                         } elseif (in_array($row['verb'], ['', Activity::POST, Activity::SHARE])) {
59                                 // Posts don't have a target - but having tags or files.
60                                 if (array_key_exists('target', $row)) {
61                                         $row['target'] = '';
62                                 }
63                         }
64                 }
65
66                 if (!array_key_exists('verb', $row) || in_array($row['verb'], ['', Activity::POST, Activity::SHARE])) {
67                         // Build the file string out of the term entries
68                         if (array_key_exists('file', $row)) {
69                                 if ($row['internal-file-count'] > 0) {
70                                         $row['file'] = Post\Category::getTextByURIId($row['internal-uri-id'], $row['internal-uid']);
71                                 } else {
72                                         $row['file'] = '';
73                                 }
74                         }
75                 }
76
77                 // Remove internal fields
78                 unset($row['internal-file-count']);
79                 unset($row['internal-uri-id']);
80                 unset($row['internal-uid']);
81
82                 return $row;
83         }
84
85         /**
86          * Fills an array with data from an post query
87          *
88          * @param object $stmt statement object
89          * @param bool   $do_close
90          * @return array Data array
91          */
92         public static function toArray($stmt, $do_close = true) {
93                 if (is_bool($stmt)) {
94                         return $stmt;
95                 }
96
97                 $data = [];
98                 while ($row = self::fetch($stmt)) {
99                         $data[] = $row;
100                 }
101                 if ($do_close) {
102                         DBA::close($stmt);
103                 }
104                 return $data;
105         }
106
107         /**
108          * Check if post data exists
109          *
110          * @param array $condition array of fields for condition
111          *
112          * @return boolean Are there rows for that condition?
113          * @throws \Exception
114          */
115         public static function exists($condition) {
116                 return DBA::exists('post-view', $condition);
117         }
118
119         /**
120          * Counts the posts satisfying the provided condition
121          *
122          * @param array        $condition array of fields for condition
123          * @param array        $params    Array of several parameters
124          *
125          * @return int
126          *
127          * Example:
128          * $condition = ["uid" => 1, "network" => 'dspr'];
129          * or:
130          * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
131          *
132          * $count = Post::count($condition);
133          * @throws \Exception
134          */
135         public static function count(array $condition = [], array $params = [])
136         {
137                 return DBA::count('post-view', $condition, $params);
138         }
139
140         /**
141          * Retrieve a single record from the post table and returns it in an associative array
142          *
143          * @param array $fields
144          * @param array $condition
145          * @param array $params
146          * @return bool|array
147          * @throws \Exception
148          * @see   DBA::select
149          */
150         public static function selectFirst(array $fields = [], array $condition = [], $params = [])
151         {
152                 $params['limit'] = 1;
153
154                 $result = self::select($fields, $condition, $params);
155
156                 if (is_bool($result)) {
157                         return $result;
158                 } else {
159                         $row = self::fetch($result);
160                         DBA::close($result);
161                         return $row;
162                 }
163         }
164
165         /**
166          * Select rows from the post table and returns them as an array
167          *
168          * @param array $selected  Array of selected fields, empty for all
169          * @param array $condition Array of fields for condition
170          * @param array $params    Array of several parameters
171          *
172          * @return array
173          * @throws \Exception
174          */
175         public static function selectToArray(array $fields = [], array $condition = [], $params = [])
176         {
177                 $result = self::select($fields, $condition, $params);
178
179                 if (is_bool($result)) {
180                         return [];
181                 }
182
183                 $data = [];
184                 while ($row = self::fetch($result)) {
185                         $data[] = $row;
186                 }
187                 DBA::close($result);
188
189                 return $data;
190         }
191
192         /**
193          * Select rows from the given view
194          *
195          * @param string $view      View (post-view or post-thread-view)
196          * @param array  $selected  Array of selected fields, empty for all
197          * @param array  $condition Array of fields for condition
198          * @param array  $params    Array of several parameters
199          *
200          * @return boolean|object
201          * @throws \Exception
202          */
203         private static function selectView(string $view, array $selected = [], array $condition = [], $params = [])
204         {
205                 if (empty($selected)) {
206                         $selected = array_merge(['author-addr', 'author-nick', 'owner-addr', 'owner-nick', 'causer-addr', 'causer-nick',
207                                 'causer-network', 'photo', 'name-date', 'uri-date', 'avatar-date', 'thumb', 'dfrn-id',
208                                 'parent-guid', 'parent-network', 'parent-author-id', 'parent-author-link', 'parent-author-name',
209                                 'parent-author-network', 'signed_text'], Item::DISPLAY_FIELDLIST, Item::ITEM_FIELDLIST, Item::CONTENT_FIELDLIST);
210                         
211                         if ($view == 'post-thread-view') {
212                                 $selected = array_merge($selected, ['ignored', 'iid']);
213                         }
214                 }
215
216                 $selected = array_merge($selected, ['internal-uri-id', 'internal-uid', 'internal-file-count']);
217                 $selected = array_unique($selected);
218
219                 return DBA::select($view, $selected, $condition, $params);
220         }
221
222         /**
223          * Select rows from the post table
224          *
225          * @param array $selected  Array of selected fields, empty for all
226          * @param array $condition Array of fields for condition
227          * @param array $params    Array of several parameters
228          *
229          * @return boolean|object
230          * @throws \Exception
231          */
232         public static function select(array $selected = [], array $condition = [], $params = [])
233         {
234                 return self::selectView('post-view', $selected, $condition, $params);
235         }
236
237         /**
238          * Select rows from the post table
239          *
240          * @param array $selected  Array of selected fields, empty for all
241          * @param array $condition Array of fields for condition
242          * @param array $params    Array of several parameters
243          *
244          * @return boolean|object
245          * @throws \Exception
246          */
247         public static function selectThread(array $selected = [], array $condition = [], $params = [])
248         {
249                 return self::selectView('post-thread-view', $selected, $condition, $params);
250         }
251
252         /**
253          * Select rows from the given view for a given user
254          *
255          * @param string  $view      View (post-view or post-thread-view)
256          * @param integer $uid       User ID
257          * @param array   $selected  Array of selected fields, empty for all
258          * @param array   $condition Array of fields for condition
259          * @param array   $params    Array of several parameters
260          *
261          * @return boolean|object
262          * @throws \Exception
263          */
264         private static function selectViewForUser(string $view, $uid, array $selected = [], array $condition = [], $params = [])
265         {
266                 if (empty($selected)) {
267                         $selected = Item::DISPLAY_FIELDLIST;
268                 }
269
270                 $selected = array_unique(array_merge($selected, ['internal-uri-id', 'internal-uid', 'internal-file-count']));
271
272                 $condition = DBA::mergeConditions($condition,
273                         ["`visible` AND NOT `deleted` AND NOT `moderated`
274                         AND NOT `author-blocked` AND NOT `owner-blocked`
275                         AND (NOT `causer-blocked` OR `causer-id` = ?) AND NOT `contact-blocked`
276                         AND ((NOT `contact-readonly` AND NOT `contact-pending` AND (`contact-rel` IN (?, ?)))
277                                 OR `self` OR `gravity` != ? OR `contact-uid` = ?)
278                         AND NOT EXISTS (SELECT `iid` FROM `user-item` WHERE `hidden` AND `iid` = `id` AND `uid` = ?)
279                         AND NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `author-id` AND `blocked`)
280                         AND NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `owner-id` AND `blocked`)
281                         AND NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `author-id` AND `ignored` AND `gravity` = ?)
282                         AND NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `owner-id` AND `ignored` AND `gravity` = ?)",
283                         0, Contact::SHARING, Contact::FRIEND, GRAVITY_PARENT, 0, $uid, $uid, $uid, $uid, GRAVITY_PARENT, $uid, GRAVITY_PARENT]);
284
285                 $select_string = '';
286
287                 if (in_array('pinned', $selected)) {
288                         $selected = array_flip($selected);
289                         unset($selected['pinned']);
290                         $selected = array_flip($selected);      
291
292                         $select_string = "(SELECT `pinned` FROM `user-item` WHERE `iid` = `" . $view . "`.`id` AND uid=`" . $view . "`.`uid`) AS `pinned`, ";
293                 }
294
295                 $select_string .= implode(', ', array_map([DBA::class, 'quoteIdentifier'], $selected));
296
297                 $condition_string = DBA::buildCondition($condition);
298                 $param_string = DBA::buildParameter($params);
299
300                 $sql = "SELECT " . $select_string . " FROM `" . $view . "` " . $condition_string . $param_string;
301                 $sql = DBA::cleanQuery($sql);
302
303                 return DBA::p($sql, $condition);
304         }
305
306         /**
307          * Select rows from the post view for a given user
308          *
309          * @param integer $uid       User ID
310          * @param array   $selected  Array of selected fields, empty for all
311          * @param array   $condition Array of fields for condition
312          * @param array   $params    Array of several parameters
313          *
314          * @return boolean|object
315          * @throws \Exception
316          */
317         public static function selectForUser($uid, array $selected = [], array $condition = [], $params = [])
318         {
319                 return self::selectViewForUser('post-view', $uid, $selected, $condition, $params);
320         }
321
322                 /**
323          * Select rows from the post view for a given user
324          *
325          * @param integer $uid       User ID
326          * @param array   $selected  Array of selected fields, empty for all
327          * @param array   $condition Array of fields for condition
328          * @param array   $params    Array of several parameters
329          *
330          * @return boolean|object
331          * @throws \Exception
332          */
333         public static function selectThreadForUser($uid, array $selected = [], array $condition = [], $params = [])
334         {
335                 return self::selectViewForUser('post-thread-view', $uid, $selected, $condition, $params);
336         }
337
338         /**
339          * Retrieve a single record from the post view for a given user and returns it in an associative array
340          *
341          * @param integer $uid User ID
342          * @param array   $selected
343          * @param array   $condition
344          * @param array   $params
345          * @return bool|array
346          * @throws \Exception
347          * @see   DBA::select
348          */
349         public static function selectFirstForUser($uid, array $selected = [], array $condition = [], $params = [])
350         {
351                 $params['limit'] = 1;
352
353                 $result = self::selectForUser($uid, $selected, $condition, $params);
354
355                 if (is_bool($result)) {
356                         return $result;
357                 } else {
358                         $row = self::fetch($result);
359                         DBA::close($result);
360                         return $row;
361                 }
362         }
363
364         /**
365          * Retrieve a single record from the starting post in the item table and returns it in an associative array
366          *
367          * @param integer $uid User ID
368          * @param array   $selected
369          * @param array   $condition
370          * @param array   $params
371          * @return bool|array
372          * @throws \Exception
373          * @see   DBA::select
374          */
375         public static function selectFirstThreadForUser($uid, array $selected = [], array $condition = [], $params = [])
376         {
377                 $params['limit'] = 1;
378
379                 $result = self::selectThreadForUser($uid, $selected, $condition, $params);
380
381                 if (is_bool($result)) {
382                         return $result;
383                 } else {
384                         $row = self::fetch($result);
385                         DBA::close($result);
386                         return $row;
387                 }
388         }
389
390         /**
391          * Select pinned rows from the item table for a given user
392          *
393          * @param integer $uid       User ID
394          * @param array   $selected  Array of selected fields, empty for all
395          * @param array   $condition Array of fields for condition
396          * @param array   $params    Array of several parameters
397          *
398          * @return boolean|object
399          * @throws \Exception
400          */
401         public static function selectPinned(int $uid, array $selected = [], array $condition = [], $params = [])
402         {
403                 $useritems = DBA::select('user-item', ['iid'], ['uid' => $uid, 'pinned' => true]);
404                 if (!DBA::isResult($useritems)) {
405                         return $useritems;
406                 }
407
408                 $pinned = [];
409                 while ($useritem = DBA::fetch($useritems)) {
410                         $pinned[] = $useritem['iid'];
411                 }
412                 DBA::close($useritems);
413
414                 if (empty($pinned)) {
415                         return [];
416                 }
417
418                 $condition = DBA::mergeConditions(['iid' => $pinned], $condition);
419
420                 return self::selectThreadForUser($uid, $selected, $condition, $params);
421         }
422 }