3 * @copyright Copyright (C) 2020, Friendica
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;
24 use Friendica\Database\DBA;
25 use Friendica\Protocol\Activity;
30 * Fetch a single post row
32 * @param mixed $stmt statement object
33 * @return array|false current row or false
36 public static function fetch($stmt)
38 $row = DBA::fetch($stmt);
40 if (!is_array($row)) {
44 if (array_key_exists('verb', $row)) {
45 if (in_array($row['verb'], Item::ACTIVITIES)) {
46 if (array_key_exists('title', $row)) {
49 if (array_key_exists('body', $row)) {
50 $row['body'] = $row['verb'];
52 if (array_key_exists('object', $row)) {
55 if (array_key_exists('object-type', $row)) {
56 $row['object-type'] = Activity\ObjectType::NOTE;
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)) {
70 * Fills an array with data from an post query
72 * @param object $stmt statement object
73 * @param bool $do_close
74 * @return array Data array
76 public static function toArray($stmt, $do_close = true) {
82 while ($row = self::fetch($stmt)) {
92 * Check if post data exists
94 * @param array $condition array of fields for condition
96 * @return boolean Are there rows for that condition?
99 public static function exists($condition) {
100 return DBA::exists('post-view', $condition);
104 * Counts the posts satisfying the provided condition
106 * @param array $condition array of fields for condition
107 * @param array $params Array of several parameters
112 * $condition = ["uid" => 1, "network" => 'dspr'];
114 * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
116 * $count = Post::count($condition);
119 public static function count(array $condition = [], array $params = [])
121 return DBA::count('post-view', $condition, $params);
125 * Retrieve a single record from the post table and returns it in an associative array
127 * @param array $fields
128 * @param array $condition
129 * @param array $params
134 public static function selectFirst(array $fields = [], array $condition = [], $params = [])
136 $params['limit'] = 1;
138 $result = self::select($fields, $condition, $params);
140 if (is_bool($result)) {
143 $row = self::fetch($result);
150 * Select rows from the post table and returns them as an array
152 * @param array $selected Array of selected fields, empty for all
153 * @param array $condition Array of fields for condition
154 * @param array $params Array of several parameters
159 public static function selectToArray(array $fields = [], array $condition = [], $params = [])
161 $result = self::select($fields, $condition, $params);
163 if (is_bool($result)) {
168 while ($row = self::fetch($result)) {
177 * Select rows from the given view
179 * @param string $view View (post-view or post-thread-view)
180 * @param array $selected Array of selected fields, empty for all
181 * @param array $condition Array of fields for condition
182 * @param array $params Array of several parameters
184 * @return boolean|object
187 private static function selectView(string $view, array $selected = [], array $condition = [], $params = [])
189 if (empty($selected)) {
190 $selected = array_merge(['author-addr', 'author-nick', 'owner-addr', 'owner-nick', 'causer-addr', 'causer-nick',
191 'causer-network', 'photo', 'name-date', 'uri-date', 'avatar-date', 'thumb', 'dfrn-id',
192 'parent-guid', 'parent-network', 'parent-author-id', 'parent-author-link', 'parent-author-name',
193 'parent-author-network', 'signed_text'], Item::DISPLAY_FIELDLIST, Item::ITEM_FIELDLIST, Item::CONTENT_FIELDLIST);
195 if ($view == 'post-thread-view') {
196 $selected = array_merge($selected, ['ignored', 'iid']);
200 $selected = array_unique($selected);
202 return DBA::select($view, $selected, $condition, $params);
206 * Select rows from the post table
208 * @param array $selected Array of selected fields, empty for all
209 * @param array $condition Array of fields for condition
210 * @param array $params Array of several parameters
212 * @return boolean|object
215 public static function select(array $selected = [], array $condition = [], $params = [])
217 return self::selectView('post-view', $selected, $condition, $params);
221 * Select rows from the post table
223 * @param array $selected Array of selected fields, empty for all
224 * @param array $condition Array of fields for condition
225 * @param array $params Array of several parameters
227 * @return boolean|object
230 public static function selectThread(array $selected = [], array $condition = [], $params = [])
232 return self::selectView('post-thread-view', $selected, $condition, $params);
236 * Select rows from the given view for a given user
238 * @param string $view View (post-view or post-thread-view)
239 * @param integer $uid User ID
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
244 * @return boolean|object
247 private static function selectViewForUser(string $view, $uid, array $selected = [], array $condition = [], $params = [])
249 if (empty($selected)) {
250 $selected = Item::DISPLAY_FIELDLIST;
253 $condition = DBA::mergeConditions($condition,
254 ["`visible` AND NOT `deleted` AND NOT `moderated`
255 AND NOT `author-blocked` AND NOT `owner-blocked`
256 AND (NOT `causer-blocked` OR `causer-id` = ?) AND NOT `contact-blocked`
257 AND ((NOT `contact-readonly` AND NOT `contact-pending` AND (`contact-rel` IN (?, ?)))
258 OR `self` OR `gravity` != ? OR `contact-uid` = ?)
259 AND NOT EXISTS (SELECT `iid` FROM `user-item` WHERE `hidden` AND `iid` = `id` AND `uid` = ?)
260 AND NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `author-id` AND `blocked`)
261 AND NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `owner-id` AND `blocked`)
262 AND NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `author-id` AND `ignored` AND `gravity` = ?)
263 AND NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `owner-id` AND `ignored` AND `gravity` = ?)",
264 0, Contact::SHARING, Contact::FRIEND, GRAVITY_PARENT, 0, $uid, $uid, $uid, $uid, GRAVITY_PARENT, $uid, GRAVITY_PARENT]);
268 if (in_array('pinned', $selected)) {
269 $selected = array_flip($selected);
270 unset($selected['pinned']);
271 $selected = array_flip($selected);
273 $select_string = "(SELECT `pinned` FROM `user-item` WHERE `iid` = `" . $view . "`.`id` AND uid=`" . $view . "`.`uid`) AS `pinned`, ";
276 $select_string .= implode(', ', array_map([DBA::class, 'quoteIdentifier'], $selected));
278 $condition_string = DBA::buildCondition($condition);
279 $param_string = DBA::buildParameter($params);
281 $sql = "SELECT " . $select_string . " FROM `" . $view . "` " . $condition_string . $param_string;
282 $sql = DBA::cleanQuery($sql);
284 return DBA::p($sql, $condition);
288 * Select rows from the post view for a given user
290 * @param integer $uid User ID
291 * @param array $selected Array of selected fields, empty for all
292 * @param array $condition Array of fields for condition
293 * @param array $params Array of several parameters
295 * @return boolean|object
298 public static function selectForUser($uid, array $selected = [], array $condition = [], $params = [])
300 return self::selectViewForUser('post-view', $uid, $selected, $condition, $params);
304 * Select rows from the post view for a given user
306 * @param integer $uid User ID
307 * @param array $selected Array of selected fields, empty for all
308 * @param array $condition Array of fields for condition
309 * @param array $params Array of several parameters
311 * @return boolean|object
314 public static function selectThreadForUser($uid, array $selected = [], array $condition = [], $params = [])
316 return self::selectViewForUser('post-thread-view', $uid, $selected, $condition, $params);
320 * Retrieve a single record from the post view for a given user and returns it in an associative array
322 * @param integer $uid User ID
323 * @param array $selected
324 * @param array $condition
325 * @param array $params
330 public static function selectFirstForUser($uid, array $selected = [], array $condition = [], $params = [])
332 $params['limit'] = 1;
334 $result = self::selectForUser($uid, $selected, $condition, $params);
336 if (is_bool($result)) {
339 $row = self::fetch($result);
346 * Retrieve a single record from the starting post in the item table and returns it in an associative array
348 * @param integer $uid User ID
349 * @param array $selected
350 * @param array $condition
351 * @param array $params
356 public static function selectFirstThreadForUser($uid, array $selected = [], array $condition = [], $params = [])
358 $params['limit'] = 1;
360 $result = self::selectThreadForUser($uid, $selected, $condition, $params);
362 if (is_bool($result)) {
365 $row = self::fetch($result);
372 * Select pinned rows from the item table for a given user
374 * @param integer $uid User ID
375 * @param array $selected Array of selected fields, empty for all
376 * @param array $condition Array of fields for condition
377 * @param array $params Array of several parameters
379 * @return boolean|object
382 public static function selectPinned(int $uid, array $selected = [], array $condition = [], $params = [])
384 $useritems = DBA::select('user-item', ['iid'], ['uid' => $uid, 'pinned' => true]);
385 if (!DBA::isResult($useritems)) {
390 while ($useritem = DBA::fetch($useritems)) {
391 $pinned[] = $useritem['iid'];
393 DBA::close($useritems);
395 if (empty($pinned)) {
399 $condition = DBA::mergeConditions(['iid' => $pinned], $condition);
401 return self::selectThreadForUser($uid, $selected, $condition, $params);