]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Post.php
API: We now support two more timeline api endpoints
[friendica.git] / src / Model / Post.php
index bb35f126b34a981539da2e9de81aeecb1d022d5c..9ce301ade0c4540dfb06470a8d0ebb8c9e9f4d0d 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2021, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -127,12 +127,13 @@ class Post
         * Check if post data exists
         *
         * @param array $condition array of fields for condition
+        * @param bool  $user_mode true = post-user-view, false = post-view
         *
         * @return boolean Are there rows for that condition?
         * @throws \Exception
         */
-       public static function exists($condition) {
-               return DBA::exists('post-user-view', $condition);
+       public static function exists($condition, bool $user_mode = true) {
+               return DBA::exists($user_mode ? 'post-user-view' : 'post-view', $condition);
        }
 
        /**
@@ -140,6 +141,7 @@ class Post
         *
         * @param array        $condition array of fields for condition
         * @param array        $params    Array of several parameters
+        * @param bool         $user_mode true = post-user-view, false = post-view
         *
         * @return int
         *
@@ -151,9 +153,9 @@ class Post
         * $count = Post::count($condition);
         * @throws \Exception
         */
-       public static function count(array $condition = [], array $params = [])
+       public static function count(array $condition = [], array $params = [], bool $user_mode = true)
        {
-               return DBA::count('post-user-view', $condition, $params);
+               return DBA::count($user_mode ? 'post-user-view' : 'post-view', $condition, $params);
        }
 
        /**
@@ -181,6 +183,31 @@ class Post
                }
        }
 
+       /**
+        * Retrieve a single record from the post-thread table and returns it in an associative array
+        *
+        * @param array $fields
+        * @param array $condition
+        * @param array $params
+        * @return bool|array
+        * @throws \Exception
+        * @see   DBA::select
+        */
+       public static function selectFirstThread(array $fields = [], array $condition = [], $params = [])
+       {
+               $params['limit'] = 1;
+
+               $result = self::selectThread($fields, $condition, $params);
+
+               if (is_bool($result)) {
+                       return $result;
+               } else {
+                       $row = self::fetch($result);
+                       DBA::close($result);
+                       return $row;
+               }
+       }
+
        /**
         * Select rows from the post table and returns them as an array
         *
@@ -295,17 +322,7 @@ class Post
                        AND NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `owner-id` AND `ignored` AND `gravity` = ?)",
                        0, Contact::SHARING, Contact::FRIEND, GRAVITY_PARENT, 0, $uid, $uid, $uid, $uid, GRAVITY_PARENT, $uid, GRAVITY_PARENT]);
 
-               $select_string = '';
-
-               if (in_array('pinned', $selected)) {
-                       $selected = array_flip($selected);
-                       unset($selected['pinned']);
-                       $selected = array_flip($selected);      
-
-                       $select_string = "(SELECT `pinned` FROM `post-thread-user` WHERE `uri-id` = `" . $view . "`.`uri-id` AND uid=`" . $view . "`.`uid`) AS `pinned`, ";
-               }
-
-               $select_string .= implode(', ', array_map([DBA::class, 'quoteIdentifier'], $selected));
+               $select_string = implode(', ', array_map([DBA::class, 'quoteIdentifier'], $selected));
 
                $condition_string = DBA::buildCondition($condition);
                $param_string = DBA::buildParameter($params);
@@ -391,7 +408,7 @@ class Post
                if (!DBA::isResult($postthreaduser)) {
                        return $postthreaduser;
                }
-       
+
                $pinned = [];
                while ($useritem = DBA::fetch($postthreaduser)) {
                        $pinned[] = $useritem['uri-id'];