]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Item.php
Merge pull request #10116 from mexon/mat/addon-console-command
[friendica.git] / src / Model / Item.php
index 8d2eefe0aa37752ea1680345c281de99c2e3ecad..48652a0e12fbf691f6ceea1bdb42b6bbed7c2838 100644 (file)
@@ -32,7 +32,6 @@ use Friendica\Core\System;
 use Friendica\Model\Tag;
 use Friendica\Core\Worker;
 use Friendica\Database\DBA;
-use Friendica\Database\DBStructure;
 use Friendica\DI;
 use Friendica\Model\Post;
 use Friendica\Protocol\Activity;
@@ -75,12 +74,12 @@ class Item
                'uid', 'id', 'parent', 'guid', 'network', 'gravity',
                'uri-id', 'uri', 'thr-parent-id', 'thr-parent', 'parent-uri-id', 'parent-uri',
                'commented', 'created', 'edited', 'received', 'verb', 'object-type', 'postopts', 'plink',
-               'wall', 'private', 'starred', 'origin', 'title', 'body', 'language',
+               'wall', 'private', 'starred', 'origin', 'parent-origin', 'title', 'body', 'language',
                'content-warning', 'location', 'coord', 'app', 'rendered-hash', 'rendered-html', 'object',
-               'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
+               'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'mention',
                'author-id', 'author-link', 'author-name', 'author-avatar', 'author-network',
-               'owner-id', 'owner-link', 'owner-name', 'owner-avatar', 'owner-network',
-               'causer-id', 'causer-link', 'causer-name', 'causer-avatar', 'causer-contact-type',
+               'owner-id', 'owner-link', 'owner-name', 'owner-avatar', 'owner-network', 'owner-contact-type',
+               'causer-id', 'causer-link', 'causer-name', 'causer-avatar', 'causer-contact-type', 'causer-network',
                'contact-id', 'contact-uid', 'contact-link', 'contact-name', 'contact-avatar',
                'writable', 'self', 'cid', 'alias',
                'event-created', 'event-edited', 'event-start', 'event-finish',
@@ -116,22 +115,6 @@ class Item
                        'author-id', 'author-link', 'author-name', 'author-avatar', 'author-network',
                        'owner-id', 'owner-link', 'owner-name', 'owner-avatar', 'causer-id'];
 
-       // Item fields that still are in use
-       const USED_FIELDLIST = ['id', 'parent', 'guid', 'uri', 'uri-id', 'parent-uri', 'parent-uri-id',
-               'thr-parent', 'thr-parent-id', 'created', 'edited', 'commented', 'received', 'changed',
-               'gravity', 'network', 'owner-id', 'author-id', 'causer-id', 'vid', 'extid', 'post-type',
-               'global', 'private', 'visible', 'deleted', 'uid', 'contact-id',
-               'wall', 'origin', 'pubmail', 'starred', 'unseen', 'mention', 'forum_mode', 'psid',
-               'event-id'];
-
-       // Legacy item fields that aren't stored any more in the item table
-       const LEGACY_FIELDLIST = ['uri-hash', 'iaid', 'icid', 'attach',
-               'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'postopts', 
-               'resource-id', 'inform', 'file', 'location', 'coord', 'tag', 'plink', 
-               'title', 'content-warning', 'body', 'app', 'verb', 'object-type', 'object', 
-               'target-type', 'target', 'author-name', 'author-link', 'author-avatar', 
-               'owner-name', 'owner-link', 'owner-avatar', 'rendered-hash', 'rendered-html'];
-
        // List of all verbs that don't need additional content data.
        // Never reorder or remove entries from this list. Just add new ones at the end, if needed.
        const ACTIVITIES = [
@@ -335,14 +318,6 @@ class Item
 
                Post\DeliveryData::delete($item['uri-id']);
 
-               // When the permission set will be used in photo and events as well,
-               // this query here needs to be extended.
-               // @todo Currently deactivated. We need the permission set in the deletion process.
-               // This is a reminder to add the removal somewhere else.
-               //if (!empty($item['psid']) && !self::exists(['psid' => $item['psid'], 'deleted' => false])) {
-               //      DBA::delete('permissionset', ['id' => $item['psid']], ['cascade' => false]);
-               //}
-
                // If it's the parent of a comment thread, kill all the kids
                if ($item['gravity'] == GRAVITY_PARENT) {
                        self::markForDeletion(['parent' => $item['parent'], 'deleted' => false], $priority);
@@ -883,6 +858,8 @@ class Item
                                $item['wall'] = $toplevel_parent['wall'];
                        } else {
                                $item['wall'] = false;
+                               // Participations are technical messages, so they are set to "seen" automatically
+                               $item['unseen'] = false;
                        }
 
                        /*
@@ -1042,38 +1019,13 @@ class Item
                        Post\ThreadUser::insert($item['uri-id'], $item['uid'], $item);
                }
 
-               // Remove all fields that aren't part of the item table
-               $table_fields = DBStructure::getFieldsForTable('item', $item);
-
-               // We remove all legacy fields that now are stored in other tables
-               foreach (self::LEGACY_FIELDLIST as $field) {
-                       unset($table_fields[$field]);
-               }
-
-               $result = DBA::insert('item', $table_fields);
-
-               // When the item was successfully stored we fetch the ID of the item.
-               $current_post = DBA::lastInsertId();
-
-               if (empty($current_post) || !DBA::isResult($result)) {
-                       // On failure store the data into a spool file so that the "SpoolPost" worker can try again later.
-                       Logger::warning('Could not store item. it will be spooled', ['result' => $result, 'id' => $current_post]);
-                       self::spool($orig_item);
-                       return 0;
-               }
-
-               Logger::notice('created item', ['id' => $current_post, 'uid' => $item['uid'], 'network' => $item['network'], 'uri-id' => $item['uri-id'], 'guid' => $item['guid']]);
-
-               if (!$parent_id || ($item['gravity'] === GRAVITY_PARENT)) {
-                       $parent_id = $current_post;
-               }
-
-               // Set parent id
-               DBA::update('item', ['parent' => $parent_id], ['id' => $current_post]);
+               Logger::notice('created item', ['post-id' => $post_user_id, 'uid' => $item['uid'], 'network' => $item['network'], 'uri-id' => $item['uri-id'], 'guid' => $item['guid']]);
 
                $posted_item = Post::selectFirst(self::ITEM_FIELDLIST, ['post-user-id' => $post_user_id]);
                if (!DBA::isResult($posted_item)) {
-                       Logger::warning('new item not found in DB', ['id' => $post_user_id]);
+                       // On failure store the data into a spool file so that the "SpoolPost" worker can try again later.
+                       Logger::warning('Could not store item. it will be spooled', ['id' => $post_user_id]);
+                       self::spool($orig_item);
                        return 0;
                }
 
@@ -1087,11 +1039,13 @@ class Item
                }
 
                if ($update_commented) {
-                       Post::update(['commented' => DateTimeFormat::utcNow(), 'changed' => DateTimeFormat::utcNow()], ['id' => $post_user_id]);
+                       $fields = ['commented' => DateTimeFormat::utcNow(), 'changed' => DateTimeFormat::utcNow()];
                } else {
-                       Post::update(['changed' => DateTimeFormat::utcNow()], ['uri-id' => $posted_item['parent-uri-id'], 'uid' => $posted_item['uid']]);
+                       $fields = ['changed' => DateTimeFormat::utcNow()];
                }
 
+               Post::update($fields, ['uri-id' => $posted_item['parent-uri-id'], 'uid' => $posted_item['uid']]);
+
                // In that function we check if this is a forum post. Additionally we delete the item under certain circumstances
                if (self::tagDeliver($posted_item['uid'], $post_user_id)) {
                        // Get the user information for the logging
@@ -1164,7 +1118,7 @@ class Item
                        return;
                }
 
-               $author = Contact::selectFirst(['url', 'contact-type'], ['id' => $item['author-id']]);
+               $author = Contact::selectFirst(['url', 'contact-type', 'network'], ['id' => $item['author-id']]);
                if (!DBA::isResult($author)) {
                        Logger::error('Author not found', ['id' => $item['author-id']]);
                        return;
@@ -1172,7 +1126,7 @@ class Item
 
                $cid = Contact::getIdForURL($author['url'], $item['uid']);
                if (empty($cid) || !Contact::isSharing($cid, $item['uid'])) {
-                       Logger::info('The resharer is not a following contact: quit', ['resharer' => $author['url'], 'uid' => $item['uid']]);
+                       Logger::info('The resharer is not a following contact: quit', ['resharer' => $author['url'], 'uid' => $item['uid'], 'cid' => $cid]);
                        return;
                }
 
@@ -1228,7 +1182,7 @@ class Item
         */
        public static function distribute($itemid, $signed_text = '')
        {
-               $condition = ["`id` IN (SELECT `parent` FROM `post-view` WHERE `id` = ?)", $itemid];
+               $condition = ["`id` IN (SELECT `parent` FROM `post-user-view` WHERE `id` = ?)", $itemid];
                $parent = Post::selectFirst(['owner-id'], $condition);
                if (!DBA::isResult($parent)) {
                        Logger::warning('Item not found', ['condition' => $condition]);
@@ -1815,7 +1769,6 @@ class Item
                        if (($community_page || $prvgroup) &&
                                  !$item['wall'] && !$item['origin'] && ($item['gravity'] == GRAVITY_PARENT)) {
                                Logger::info('Delete private group/communiy top-level item without mention', ['id' => $item['id'], 'guid'=> $item['guid']]);
-                               DBA::delete('item', ['uri-id' => $item['uri-id'], 'uid' => $item['uid']]);
                                Post\User::delete(['uri-id' => $item['uri-id'], 'uid' => $item['uid']]);
                                return true;
                        }
@@ -2220,7 +2173,8 @@ class Item
                $params = ['order' => ['received' => false]];
                $thread = Post::selectFirst(['received'], $condition, $params);
                if (DBA::isResult($thread)) {
-                       return substr(DateTimeFormat::local($thread['received']), 0, 10);
+                       $postdate = substr(DateTimeFormat::local($thread['received']), 0, 10);
+                       return $postdate;
                }
                return false;
        }
@@ -2537,12 +2491,11 @@ class Item
         * Body is preserved to avoid side-effects as we modify it just-in-time for spoilers and private image links
         *
         * @param array $item
-        * @param bool  $update
         *
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @todo Remove reference, simply return "rendered-html" and "rendered-hash"
         */
-       public static function putInCache(&$item, $update = false)
+       public static function putInCache(&$item)
        {
                // Save original body to prevent addons to modify it
                $body = $item['body'];
@@ -2566,17 +2519,8 @@ class Item
                        $item['rendered-hash'] = $hook_data['rendered-hash'];
                        unset($hook_data);
 
-                       // Force an update if the generated values differ from the existing ones
-                       if ($rendered_hash != $item['rendered-hash']) {
-                               $update = true;
-                       }
-
-                       // Only compare the HTML when we forcefully ignore the cache
-                       if (DI::config()->get('system', 'ignore_cache') && ($rendered_html != $item['rendered-html'])) {
-                               $update = true;
-                       }
-
-                       if ($update && !empty($item['id'])) {
+                       // Update if the generated values differ from the existing ones
+                       if ((($rendered_hash != $item['rendered-hash']) || ($rendered_html != $item['rendered-html'])) && !empty($item['id'])) {
                                self::update(
                                        [
                                                'rendered-html' => $item['rendered-html'],
@@ -2664,15 +2608,7 @@ class Item
                        unset($hook_data);
                }
 
-               // Update the cached values if there is no "zrl=..." on the links.
-               $update = (!Session::isAuthenticated() && ($item["uid"] == 0));
-
-               // Or update it if the current viewer is the intented viewer.
-               if (($item["uid"] == local_user()) && ($item["uid"] != 0)) {
-                       $update = true;
-               }
-
-               self::putInCache($item, $update);
+               self::putInCache($item);
                $s = $item["rendered-html"];
 
                $hook_data = [
@@ -2697,7 +2633,9 @@ class Item
                foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT, Post\Media::UNKNOWN]) as $attachment) {
                        $mime = $attachment['mimetype'];
 
-                       $the_url = Contact::magicLinkById($item['author-id'], $attachment['url']);
+                       $author = ['uid' => 0, 'id' => $item['author-id'],
+                               'network' => $item['author-network'], 'url' => $item['author-link']];
+                       $the_url = Contact::magicLinkByContact($author, $attachment['url']);
 
                        if (strpos($mime, 'video') !== false) {
                                if (!$vhead) {