]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Item.php
We now store the uri data in a separate table (#5560)
[friendica.git] / src / Model / Item.php
index bb53896efc0cc57eb543b669a3f3e9e688b6122e..84c51a2bf0d4e8bdce3f9f2e68b892181703617b 100644 (file)
@@ -15,6 +15,9 @@ use Friendica\Core\PConfig;
 use Friendica\Core\System;
 use Friendica\Core\Worker;
 use Friendica\Database\DBA;
+use Friendica\Model\Contact;
+use Friendica\Model\PermissionSet;
+use Friendica\Model\ItemURI;
 use Friendica\Object\Image;
 use Friendica\Protocol\Diaspora;
 use Friendica\Protocol\OStatus;
@@ -513,9 +516,8 @@ class Item extends BaseObject
 
                $fields['item'] = ['id', 'uid', 'parent', 'uri', 'parent-uri', 'thr-parent', 'guid',
                        'contact-id', 'owner-id', 'author-id', 'type', 'wall', 'gravity', 'extid',
-                       'created', 'edited', 'commented', 'received', 'changed',
-                       'resource-id', 'event-id', 'tag', 'attach', 'post-type',
-                       'file', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'psid',
+                       'created', 'edited', 'commented', 'received', 'changed', 'psid',
+                       'resource-id', 'event-id', 'tag', 'attach', 'post-type', 'file',
                        'private', 'pubmail', 'moderated', 'visible', 'starred', 'bookmark',
                        'unseen', 'deleted', 'origin', 'forum_mode', 'mention', 'global',
                        'id' => 'item_id', 'network', 'icid', 'iaid', 'id' => 'internal-iid',
@@ -528,6 +530,8 @@ class Item extends BaseObject
 
                $fields['item-delivery-data'] = self::DELIVERY_DATA_FIELDLIST;
 
+               $fields['permissionset'] = ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid'];
+
                $fields['author'] = ['url' => 'author-link', 'name' => 'author-name',
                        'thumb' => 'author-avatar', 'nick' => 'author-nick', 'network' => 'author-network'];
 
@@ -600,7 +604,7 @@ class Item extends BaseObject
                                STRAIGHT_JOIN `contact` AS `author` ON `author`.`id` = $master_table.`author-id` AND NOT `author`.`blocked`
                                STRAIGHT_JOIN `contact` AS `owner` ON `owner`.`id` = $master_table.`owner-id` AND NOT `owner`.`blocked`
                                LEFT JOIN `user-item` ON `user-item`.`iid` = $master_table_key AND `user-item`.`uid` = %d",
-                               CONTACT_IS_SHARING, CONTACT_IS_FRIEND, GRAVITY_PARENT, intval($uid));
+                               Contact::SHARING, Contact::FRIEND, GRAVITY_PARENT, intval($uid));
                } else {
                        if (strpos($sql_commands, "`contact`.") !== false) {
                                $joins .= "LEFT JOIN `contact` ON `contact`.`id` = $master_table.`contact-id`";
@@ -641,6 +645,10 @@ class Item extends BaseObject
                        $joins .= " LEFT JOIN `item-delivery-data` ON `item-delivery-data`.`iid` = `item`.`id`";
                }
 
+               if (strpos($sql_commands, "`permissionset`.") !== false) {
+                       $joins .= " LEFT JOIN `permissionset` ON `permissionset`.`id` = `item`.`psid`";
+               }
+
                if ((strpos($sql_commands, "`parent-item`.") !== false) || (strpos($sql_commands, "`parent-author`.") !== false)) {
                        $joins .= " STRAIGHT_JOIN `item` AS `parent-item` ON `parent-item`.`id` = `item`.`parent`";
                }
@@ -972,7 +980,8 @@ class Item extends BaseObject
                // locate item to be deleted
                $fields = ['id', 'uri', 'uid', 'parent', 'parent-uri', 'origin',
                        'deleted', 'file', 'resource-id', 'event-id', 'attach',
-                       'verb', 'object-type', 'object', 'target', 'contact-id'];
+                       'verb', 'object-type', 'object', 'target', 'contact-id',
+                       'icid', 'iaid', 'psid'];
                $item = self::selectFirst($fields, ['id' => $item_id]);
                if (!DBA::isResult($item)) {
                        logger('Item with ID ' . $item_id . " hasn't been found.", LOGGER_DEBUG);
@@ -1035,11 +1044,7 @@ class Item extends BaseObject
                self::deleteTagsFromItem($item);
 
                // Set the item to "deleted"
-               // This erasing of item content is superfluous for items with a matching item-content.
-               // But for the next time we will still have old content in the item table.
-               $item_fields = ['deleted' => true, 'edited' => DateTimeFormat::utcNow(), 'changed' => DateTimeFormat::utcNow(),
-                       'body' => '', 'title' => '', 'content-warning' => '', 'rendered-hash' => '', 'rendered-html' => '',
-                       'object' => '', 'target' => '', 'tag' => '', 'postopts' => '', 'attach' => '', 'file' => ''];
+               $item_fields = ['deleted' => true, 'edited' => DateTimeFormat::utcNow(), 'changed' => DateTimeFormat::utcNow()];
                DBA::update('item', $item_fields, ['id' => $item['id']]);
 
                Term::insertFromTagFieldByItemId($item['id'], '');
@@ -1052,6 +1057,18 @@ class Item extends BaseObject
 
                DBA::delete('item-delivery-data', ['iid' => $item['id']]);
 
+               if (!empty($item['iaid']) && !DBA::exists('item', ['iaid' => $item['iaid'], 'deleted' => false])) {
+                       DBA::delete('item-activity', ['id' => $item['iaid']], ['cascade' => false]);
+               }
+               if (!empty($item['icid']) && !DBA::exists('item', ['icid' => $item['icid'], 'deleted' => false])) {
+                       DBA::delete('item-content', ['id' => $item['icid']], ['cascade' => false]);
+               }
+               // When the permission set will be used in photo and events as well,
+               // this query here needs to be extended.
+               if (!empty($item['psid']) && !DBA::exists('item', ['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['id'] == $item['parent']) {
                        self::delete(['parent' => $item['parent'], 'deleted' => false], $priority);
@@ -1237,6 +1254,9 @@ class Item extends BaseObject
                $item['guid'] = self::guid($item, $notify);
                $item['uri'] = notags(trim(defaults($item, 'uri', self::newURI($item['uid'], $item['guid']))));
 
+               // Store URI data
+               $item['uri-id'] = ItemURI::insert(['uri' => $item['uri'], 'guid' => $item['guid']]);
+
                // Store conversation data
                $item = Conversation::insert($item);
 
@@ -1330,8 +1350,6 @@ class Item extends BaseObject
                        $item['uri-hash'] = $existing['uri-hash'];
                }
 
-               self::addLanguageToItemArray($item);
-
                $item['wall']          = intval(defaults($item, 'wall', 0));
                $item['extid']         = trim(defaults($item, 'extid', ''));
                $item['author-name']   = trim(defaults($item, 'author-name', ''));
@@ -1383,6 +1401,8 @@ class Item extends BaseObject
                        return 0;
                }
 
+               self::addLanguageToItemArray($item);
+
                // Items cannot be stored before they happen ...
                if ($item['created'] > DateTimeFormat::utcNow()) {
                        $item['created'] = DateTimeFormat::utcNow();
@@ -1549,6 +1569,9 @@ class Item extends BaseObject
                        }
                }
 
+               $item['parent-uri-id'] = ItemURI::getIdByURI($item['parent-uri']);
+               $item['thr-parent-id'] = ItemURI::getIdByURI($item['thr-parent']);
+
                $condition = ["`uri` = ? AND `network` IN (?, ?) AND `uid` = ?",
                        $item['uri'], $item['network'], NETWORK_DFRN, $item['uid']];
                if (self::exists($condition)) {
@@ -1647,8 +1670,7 @@ class Item extends BaseObject
                        $files = '';
                }
 
-               // Creates the permission set
-               // Currently we only store the data but don't using it
+               // Creates or assigns the permission set
                $item['psid'] = PermissionSet::fetchIDForPost($item);
 
                // We are doing this outside of the transaction to avoid timing problems
@@ -2025,8 +2047,10 @@ class Item extends BaseObject
                $users = [];
 
                $condition = ["`nurl` IN (SELECT `nurl` FROM `contact` WHERE `id` = ?) AND `uid` != 0 AND NOT `blocked` AND `rel` IN (?, ?)",
-                       $parent['owner-id'], CONTACT_IS_SHARING,  CONTACT_IS_FRIEND];
+                       $parent['owner-id'], Contact::SHARING,  Contact::FRIEND];
+
                $contacts = DBA::select('contact', ['uid'], $condition);
+
                while ($contact = DBA::fetch($contacts)) {
                        $users[$contact['uid']] = $contact['uid'];
                }
@@ -2466,8 +2490,8 @@ class Item extends BaseObject
                        return;
                }
 
-               $community_page = (($user['page-flags'] == PAGE_COMMUNITY) ? true : false);
-               $prvgroup = (($user['page-flags'] == PAGE_PRVGROUP) ? true : false);
+               $community_page = (($user['page-flags'] == Contact::PAGE_COMMUNITY) ? true : false);
+               $prvgroup = (($user['page-flags'] == Contact::PAGE_PRVGROUP) ? true : false);
 
                $item = self::selectFirst(self::ITEM_FIELDLIST, ['id' => $item_id]);
                if (!DBA::isResult($item)) {
@@ -2838,7 +2862,7 @@ class Item extends BaseObject
                        $condition[] = $days;
                }
 
-               $items = self::select(['file', 'resource-id', 'starred', 'type', 'id'], $condition);
+               $items = self::select(['file', 'resource-id', 'starred', 'type', 'id', 'post-type'], $condition);
 
                if (!DBA::isResult($items)) {
                        return;
@@ -2870,9 +2894,9 @@ class Item extends BaseObject
                                continue;
                        } elseif (!$expire_starred && intval($item['starred'])) {
                                continue;
-                       } elseif (!$expire_notes && $item['type'] == 'note') {
+                       } elseif (!$expire_notes && (($item['type'] == 'note') || ($item['post-type'] == Item::PT_PERSONAL_NOTE))) {
                                continue;
-                       } elseif (!$expire_items && $item['type'] != 'note') {
+                       } elseif (!$expire_items && ($item['type'] != 'note') && ($item['post-type'] != Item::PT_PERSONAL_NOTE)) {
                                continue;
                        }
 
@@ -3015,15 +3039,7 @@ class Item extends BaseObject
 
                // If it exists, mark it as deleted
                if (DBA::isResult($like_item)) {
-                       // Already voted, undo it
-                       $fields = ['deleted' => true, 'unseen' => true, 'changed' => DateTimeFormat::utcNow()];
-                       /// @todo Consider using self::update - but before doing so, check the side effects
-                       DBA::update('item', $fields, ['id' => $like_item['id']]);
-
-                       // Clean up the Diaspora signatures for this like
-                       DBA::delete('sign', ['iid' => $like_item['id']]);
-
-                       Worker::add(PRIORITY_HIGH, "Notifier", "like", $like_item['id']);
+                       self::deleteById($like_item['id']);
 
                        if (!$event_verb_flag || $like_item['verb'] == $activity) {
                                return true;