]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Post.php
Improved profile summary for notifications
[friendica.git] / src / Model / Post.php
index c70237e8f5fea4fcd697e18a91c260156beee6a1..770ab083178f41b3ee8d3a14021bbb433581ca33 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -26,7 +26,6 @@ use Friendica\Core\Logger;
 use Friendica\Core\System;
 use Friendica\Database\Database;
 use Friendica\Database\DBA;
-use Friendica\Database\DBStructure;
 use Friendica\DI;
 use Friendica\Protocol\Activity;
 
@@ -103,26 +102,25 @@ class Post
        }
 
        /**
-        * Fills an array with data from an post query
+        * Fills an array with data from a post query
         *
-        * @param object $stmt statement object
-        * @param bool   $do_close
+        * @param object|bool $stmt Return value from Database->select
         * @return array Data array
-        * @todo Find proper type-hint for $stmt and maybe avoid boolean
+        * @throws \Exception
         */
-       public static function toArray($stmt, bool $do_close = true)
+       public static function toArray($stmt): array
        {
                if (is_bool($stmt)) {
-                       return $stmt;
+                       return [];
                }
 
                $data = [];
                while ($row = self::fetch($stmt)) {
                        $data[] = $row;
                }
-               if ($do_close) {
-                       DBA::close($stmt);
-               }
+
+               DBA::close($stmt);
+
                return $data;
        }
 
@@ -228,6 +226,46 @@ class Post
                }
        }
 
+       /**
+        * Retrieve a single record from the post-user-view view and returns it in an associative array
+        * When the requested record is a reshare activity, the system fetches the reshared original post.
+        * Otherwise the function reacts similar to selectFirst
+        *
+        * @param array $fields
+        * @param array $condition
+        * @param array $params
+        * @param bool  $user_mode true = post-user-view, false = post-view
+        * @return bool|array
+        * @throws \Exception
+        * @see   DBA::select
+        */
+       public static function selectOriginal(array $fields = [], array $condition = [], array $params = [])
+       {
+               $original_fields = $fields;
+               $remove = [];
+               if (!empty($fields)) {
+                       foreach (['gravity', 'verb', 'thr-parent-id', 'uid'] as $field) {
+                               if (!in_array($field, $fields)) {
+                                       $fields[] = $field;
+                                       $remove[] = $field;
+                               }
+                       }
+               }
+               $result = self::selectFirst($fields, $condition, $params);
+               if (empty($result)) {
+                       return $result;
+               }
+
+               if (($result['gravity'] != Item::GRAVITY_ACTIVITY) || ($result['verb'] != Activity::ANNOUNCE)) {
+                       foreach ($remove as $field) {
+                               unset($result[$field]);
+                       }
+                       return $result;
+               }
+
+               return self::selectFirst($original_fields, ['uri-id' => $result['thr-parent-id'], 'uid' => [0, $result['uid']]], $params);
+       }
+
        /**
         * Retrieve a single record from the post-view view and returns it in an associative array
         *
@@ -376,6 +414,21 @@ class Post
                return self::selectView('post-thread-user-view', $selected, $condition, $params);
        }
 
+       /**
+        * Select rows from the post-thread-view view
+        *
+        * @param array $selected  Array of selected fields, empty for all
+        * @param array $condition Array of fields for condition
+        * @param array $params    Array of several parameters
+        *
+        * @return boolean|object
+        * @throws \Exception
+        */
+       public static function selectPostThread(array $selected = [], array $condition = [], array $params = [])
+       {
+               return self::selectView('post-thread-view', $selected, $condition, $params);
+       }
+
        /**
         * Select rows from the given view for a given user
         *
@@ -399,18 +452,18 @@ class Post
                        AND NOT `author-blocked` AND NOT `owner-blocked`
                        AND (NOT `causer-blocked` OR `causer-id` = ? OR `causer-id` IS NULL) AND NOT `contact-blocked`
                        AND ((NOT `contact-readonly` AND NOT `contact-pending` AND (`contact-rel` IN (?, ?)))
-                               OR `self` OR `gravity` != ? OR `contact-uid` = ?)
-                       AND NOT EXISTS (SELECT `uri-id` FROM `post-user` WHERE `uid` = ? AND `uri-id` = `" . $view . "`.`uri-id` AND `hidden`)
-                       AND NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `author-id` AND `blocked`)
-                       AND NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `owner-id` AND `blocked`)
-                       AND NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `author-id` AND `ignored` AND `gravity` = ?)
-                       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]);
+                               OR `self` OR `contact-uid` = ?)
+                       AND NOT `" . $view . "`.`uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `uid` = ? AND `hidden`)
+                       AND NOT `author-id` IN (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `blocked` AND `cid` = `author-id`)
+                       AND NOT `owner-id` IN (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `blocked` AND `cid` = `owner-id`)
+                       AND NOT `author-id` IN (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `ignored` AND `cid` = `author-id`)
+                       AND NOT `owner-id` IN (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `ignored` AND `cid` = `owner-id`)",
+                               0, Contact::SHARING, Contact::FRIEND, 0, $uid, $uid, $uid, $uid, $uid]);
 
                $select_string = implode(', ', array_map([DBA::class, 'quoteIdentifier'], $selected));
 
                $condition_string = DBA::buildCondition($condition);
-               $param_string = DBA::buildParameter($params);
+               $param_string     = DBA::buildParameter($params);
 
                $sql = "SELECT " . $select_string . " FROM `" . $view . "` " . $condition_string . $param_string;
                $sql = DBA::cleanQuery($sql);
@@ -492,6 +545,46 @@ class Post
                }
        }
 
+       /**
+        * Retrieve a single record from the post-user-view view for a given user and returns it in an associative array
+        * When the requested record is a reshare activity, the system fetches the reshared original post.
+        * Otherwise the function reacts similar to selectFirstForUser
+        *
+        * @param integer $uid User ID
+        * @param array   $selected
+        * @param array   $condition
+        * @param array   $params
+        * @return bool|array
+        * @throws \Exception
+        * @see   DBA::select
+        */
+       public static function selectOriginalForUser(int $uid, array $selected = [], array $condition = [], array $params = [])
+       {
+               $original_selected = $selected;
+               $remove = [];
+               if (!empty($selected)) {
+                       foreach (['gravity', 'verb', 'thr-parent-id'] as $field) {
+                               if (!in_array($field, $selected)) {
+                                       $selected[] = $field;
+                                       $remove[]   = $field;
+                               }
+                       }
+               }
+               $result = self::selectFirstForUser($uid, $selected, $condition, $params);
+               if (empty($result)) {
+                       return $result;
+               }
+
+               if (($result['gravity'] != Item::GRAVITY_ACTIVITY) || ($result['verb'] != Activity::ANNOUNCE)) {
+                       foreach ($remove as $field) {
+                               unset($result[$field]);
+                       }
+                       return $result;
+               }
+
+               return self::selectFirstForUser($uid, $original_selected, ['uri-id' => $result['thr-parent-id'], 'uid' => [0, $uid]], $params);
+       }
+
        /**
         * Update existing post entries
         *
@@ -507,7 +600,7 @@ class Post
        {
                $affected = 0;
 
-               Logger::info('Start Update', ['fields' => $fields, 'condition' => $condition, 'uid' => local_user(),'callstack' => System::callstack(10)]);
+               Logger::info('Start Update', ['fields' => $fields, 'condition' => $condition, 'uid' => DI::userSession()->getLocalUserId(),'callstack' => System::callstack(10)]);
 
                // Don't allow changes to fields that are responsible for the relation between the records
                unset($fields['id']);
@@ -520,7 +613,7 @@ class Post
                unset($fields['parent-uri']);
                unset($fields['parent-uri-id']);
 
-               $thread_condition = DBA::mergeConditions($condition, ['gravity' => GRAVITY_PARENT]);
+               $thread_condition = DBA::mergeConditions($condition, ['gravity' => Item::GRAVITY_PARENT]);
 
                // To ensure the data integrity we do it in an transaction
                DBA::transaction();
@@ -528,12 +621,12 @@ class Post
                $update_fields = DI::dbaDefinition()->truncateFieldsForTable('post-user', $fields);
                if (!empty($update_fields)) {
                        $affected_count = 0;
-                       $posts = DBA::select('post-user-view', ['post-user-id'], $condition);
+                       $posts          = DBA::select('post-user-view', ['post-user-id'], $condition);
                        while ($rows = DBA::toArray($posts, false, 100)) {
                                $puids = array_column($rows, 'post-user-id');
                                if (!DBA::update('post-user', $update_fields, ['id' => $puids])) {
                                        DBA::rollback();
-                                       Logger::notice('Updating post-user failed', ['fields' => $update_fields, 'condition' => $condition]);
+                                       Logger::warning('Updating post-user failed', ['fields' => $update_fields, 'condition' => $condition]);
                                        return false;
                                }
                                $affected_count += DBA::affectedRows();
@@ -545,12 +638,12 @@ class Post
                $update_fields = DI::dbaDefinition()->truncateFieldsForTable('post-content', $fields);
                if (!empty($update_fields)) {
                        $affected_count = 0;
-                       $posts = DBA::select('post-user-view', ['uri-id'], $condition, ['group_by' => ['uri-id']]);
+                       $posts          = DBA::select('post-user-view', ['uri-id'], $condition, ['group_by' => ['uri-id']]);
                        while ($rows = DBA::toArray($posts, false, 100)) {
                                $uriids = array_column($rows, 'uri-id');
                                if (!DBA::update('post-content', $update_fields, ['uri-id' => $uriids])) {
                                        DBA::rollback();
-                                       Logger::notice('Updating post-content failed', ['fields' => $update_fields, 'condition' => $condition]);
+                                       Logger::warning('Updating post-content failed', ['fields' => $update_fields, 'condition' => $condition]);
                                        return false;
                                }
                                $affected_count += DBA::affectedRows();
@@ -562,7 +655,7 @@ class Post
                $update_fields = DI::dbaDefinition()->truncateFieldsForTable('post', $fields);
                if (!empty($update_fields)) {
                        $affected_count = 0;
-                       $posts = DBA::select('post-user-view', ['uri-id'], $condition, ['group_by' => ['uri-id']]);
+                       $posts          = DBA::select('post-user-view', ['uri-id'], $condition, ['group_by' => ['uri-id']]);
                        while ($rows = DBA::toArray($posts, false, 100)) {
                                $uriids = array_column($rows, 'uri-id');
 
@@ -573,7 +666,7 @@ class Post
 
                                if (!DBA::update('post', $update_fields, ['uri-id' => $uriids])) {
                                        DBA::rollback();
-                                       Logger::notice('Updating post failed', ['fields' => $update_fields, 'condition' => $condition]);
+                                       Logger::warning('Updating post failed', ['fields' => $update_fields, 'condition' => $condition]);
                                        return false;
                                }
                                $affected_count += DBA::affectedRows();
@@ -585,12 +678,12 @@ class Post
                $update_fields = Post\DeliveryData::extractFields($fields);
                if (!empty($update_fields)) {
                        $affected_count = 0;
-                       $posts = DBA::select('post-user-view', ['uri-id'], $condition, ['group_by' => ['uri-id']]);
+                       $posts          = DBA::select('post-user-view', ['uri-id'], $condition, ['group_by' => ['uri-id']]);
                        while ($rows = DBA::toArray($posts, false, 100)) {
                                $uriids = array_column($rows, 'uri-id');
                                if (!DBA::update('post-delivery-data', $update_fields, ['uri-id' => $uriids])) {
                                        DBA::rollback();
-                                       Logger::notice('Updating post-delivery-data failed', ['fields' => $update_fields, 'condition' => $condition]);
+                                       Logger::warning('Updating post-delivery-data failed', ['fields' => $update_fields, 'condition' => $condition]);
                                        return false;
                                }
                                $affected_count += DBA::affectedRows();
@@ -602,12 +695,12 @@ class Post
                $update_fields = DI::dbaDefinition()->truncateFieldsForTable('post-thread', $fields);
                if (!empty($update_fields)) {
                        $affected_count = 0;
-                       $posts = DBA::select('post-user-view', ['uri-id'], $thread_condition, ['group_by' => ['uri-id']]);
+                       $posts          = DBA::select('post-user-view', ['uri-id'], $thread_condition, ['group_by' => ['uri-id']]);
                        while ($rows = DBA::toArray($posts, false, 100)) {
                                $uriids = array_column($rows, 'uri-id');
                                if (!DBA::update('post-thread', $update_fields, ['uri-id' => $uriids])) {
                                        DBA::rollback();
-                                       Logger::notice('Updating post-thread failed', ['fields' => $update_fields, 'condition' => $condition]);
+                                       Logger::warning('Updating post-thread failed', ['fields' => $update_fields, 'condition' => $condition]);
                                        return false;
                                }
                                $affected_count += DBA::affectedRows();
@@ -619,12 +712,12 @@ class Post
                $update_fields = DI::dbaDefinition()->truncateFieldsForTable('post-thread-user', $fields);
                if (!empty($update_fields)) {
                        $affected_count = 0;
-                       $posts = DBA::select('post-user-view', ['post-user-id'], $thread_condition);
+                       $posts          = DBA::select('post-user-view', ['post-user-id'], $thread_condition);
                        while ($rows = DBA::toArray($posts, false, 100)) {
                                $thread_puids = array_column($rows, 'post-user-id');
                                if (!DBA::update('post-thread-user', $update_fields, ['post-user-id' => $thread_puids])) {
                                        DBA::rollback();
-                                       Logger::notice('Updating post-thread-user failed', ['fields' => $update_fields, 'condition' => $condition]);
+                                       Logger::warning('Updating post-thread-user failed', ['fields' => $update_fields, 'condition' => $condition]);
                                        return false;
                                }
                                $affected_count += DBA::affectedRows();