]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Item.php
Issue 8735: Ensure the correct data type for "hide"
[friendica.git] / src / Model / Item.php
index a6036e6bdbe8c458e25f7e1dda44a948e331b294..7a965f80b5f41649856f47abf2d0866afbf046e5 100644 (file)
@@ -72,14 +72,14 @@ class Item
                'event-id', 'event-created', 'event-edited', 'event-start', 'event-finish',
                'event-summary', 'event-desc', 'event-location', 'event-type',
                'event-nofinish', 'event-adjust', 'event-ignore', 'event-id',
-               'delivery_queue_count', 'delivery_queue_done', 'delivery_queue_failed', 'activity'
+               'delivery_queue_count', 'delivery_queue_done', 'delivery_queue_failed'
        ];
 
        // Field list that is used to deliver items via the protocols
        const DELIVER_FIELDLIST = ['uid', 'id', 'parent', 'uri-id', 'uri', 'thr-parent', 'parent-uri', 'guid',
                        'parent-guid', 'created', 'edited', 'verb', 'object-type', 'object', 'target',
                        'private', 'title', 'body', 'location', 'coord', 'app',
-                       'attach', 'deleted', 'extid', 'post-type',
+                       'attach', 'deleted', 'extid', 'post-type', 'gravity',
                        'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
                        'author-id', 'author-link', 'owner-link', 'contact-uid',
                        'signed_text', 'signature', 'signer', 'network'];
@@ -285,6 +285,10 @@ class Item
                        }
                }
 
+               if (array_key_exists('vid', $row) && is_null($row['vid']) && !empty($row['verb'])) {
+                       $row['vid'] = Verb::getID($row['verb']);
+               }
+                       
                if (!array_key_exists('verb', $row) || in_array($row['verb'], ['', Activity::POST, Activity::SHARE])) {
                        // Build the file string out of the term entries
                        if (array_key_exists('file', $row) && empty($row['file'])) {
@@ -1066,7 +1070,7 @@ class Item
 
                // clean up categories and tags so they don't end up as orphans
 
-               $matches = false;
+               $matches = [];
                $cnt = preg_match_all('/<(.*?)>/', $item['file'], $matches, PREG_SET_ORDER);
 
                if ($cnt) {
@@ -1075,7 +1079,7 @@ class Item
                        }
                }
 
-               $matches = false;
+               $matches = [];
 
                $cnt = preg_match_all('/\[(.*?)\]/', $item['file'], $matches, PREG_SET_ORDER);
 
@@ -1777,7 +1781,7 @@ class Item
 
                // Check for hashtags in the body and repair or add hashtag links
                self::setHashtags($item);
-               
+
                // Fill the cache field
                self::putInCache($item);
 
@@ -1796,12 +1800,14 @@ class Item
                }
 
                $body = $item['body'];
-               
+
                // We just remove everything that is content
                foreach (array_merge(self::CONTENT_FIELDLIST, self::MIXED_CONTENT_FIELDLIST) as $field) {
                        unset($item[$field]);
                }
 
+               unset($item['activity']);
+
                // Filling item related side tables
 
                // Diaspora signature
@@ -2946,39 +2952,6 @@ class Item
                        return false;
                }
 
-               switch ($verb) {
-                       case 'like':
-                       case 'unlike':
-                               $activity = Activity::LIKE;
-                               break;
-                       case 'dislike':
-                       case 'undislike':
-                               $activity = Activity::DISLIKE;
-                               break;
-                       case 'attendyes':
-                       case 'unattendyes':
-                               $activity = Activity::ATTEND;
-                               break;
-                       case 'attendno':
-                       case 'unattendno':
-                               $activity = Activity::ATTENDNO;
-                               break;
-                       case 'attendmaybe':
-                       case 'unattendmaybe':
-                               $activity = Activity::ATTENDMAYBE;
-                               break;
-                       case 'follow':
-                       case 'unfollow':
-                               $activity = Activity::FOLLOW;
-                               break;
-                       default:
-                               Logger::log('like: unknown verb ' . $verb . ' for item ' . $item_id);
-                               return false;
-               }
-
-               // Enable activity toggling instead of on/off
-               $event_verb_flag = $activity === Activity::ATTEND || $activity === Activity::ATTENDNO || $activity === Activity::ATTENDMAYBE;
-
                Logger::log('like: verb ' . $verb . ' item ' . $item_id);
 
                $item = self::selectFirst(self::ITEM_FIELDLIST, ['`id` = ? OR `uri` = ?', $item_id, $item_id]);
@@ -3027,9 +3000,44 @@ class Item
                        }
                }
 
+               $activity = null;
+               switch ($verb) {
+                       case 'like':
+                       case 'unlike':
+                               $activity = Activity::LIKE;
+                               break;
+                       case 'dislike':
+                       case 'undislike':
+                               $activity = Activity::DISLIKE;
+                               break;
+                       case 'attendyes':
+                       case 'unattendyes':
+                               $activity = Activity::ATTEND;
+                               break;
+                       case 'attendno':
+                       case 'unattendno':
+                               $activity = Activity::ATTENDNO;
+                               break;
+                       case 'attendmaybe':
+                       case 'unattendmaybe':
+                               $activity = Activity::ATTENDMAYBE;
+                               break;
+                       case 'follow':
+                       case 'unfollow':
+                               $activity = Activity::FOLLOW;
+                               break;
+                       default:
+                               Logger::log('like: unknown verb ' . $verb . ' for item ' . $item_id);
+                               return false;
+               }
+
+               $mode = Strings::startsWith($verb, 'un') ? 'delete' : 'create';
+
+               // Enable activity toggling instead of on/off
+               $event_verb_flag = $activity === Activity::ATTEND || $activity === Activity::ATTENDNO || $activity === Activity::ATTENDMAYBE;
+
                // Look for an existing verb row
-               // event participation are essentially radio toggles. If you make a subsequent choice,
-               // we need to eradicate your first choice.
+               // Event participation activities are mutually exclusive, only one of them can exist at all times.
                if ($event_verb_flag) {
                        $verbs = [Activity::ATTEND, Activity::ATTENDNO, Activity::ATTENDMAYBE];
 
@@ -3044,20 +3052,43 @@ class Item
 
                $condition = ['vid' => $vids, 'deleted' => false, 'gravity' => GRAVITY_ACTIVITY,
                        'author-id' => $author_id, 'uid' => $item['uid'], 'thr-parent' => $item_uri];
-
                $like_item = self::selectFirst(['id', 'guid', 'verb'], $condition);
 
-               // If it exists, mark it as deleted
                if (DBA::isResult($like_item)) {
-                       self::markForDeletionById($like_item['id']);
+                       /**
+                        * Truth table for existing activities
+                        *
+                        * |          Inputs            ||      Outputs      |
+                        * |----------------------------||-------------------|
+                        * |  Mode  | Event | Same verb || Delete? | Return? |
+                        * |--------|-------|-----------||---------|---------|
+                        * | create |  Yes  |    Yes    ||   No    |   Yes   |
+                        * | create |  Yes  |    No     ||   Yes   |   No    |
+                        * | create |  No   |    Yes    ||   No    |   Yes   |
+                        * | create |  No   |    No     ||        N/A†       |
+                        * | delete |  Yes  |    Yes    ||   Yes   |   N/A‡  |
+                        * | delete |  Yes  |    No     ||   No    |   N/A‡  |
+                        * | delete |  No   |    Yes    ||   Yes   |   N/A‡  |
+                        * | delete |  No   |    No     ||        N/A†       |
+                        * |--------|-------|-----------||---------|---------|
+                        * |   A    |   B   |     C     || A xor C | !B or C |
+                        *
+                        * † Can't happen: It's impossible to find an existing non-event activity without
+                        *                 the same verb because we are only looking for this single verb.
+                        *
+                        * ‡ The "mode = delete" is returning early whether an existing activity was found or not.
+                        */
+                       if ($mode == 'create' xor $like_item['verb'] == $activity) {
+                               self::markForDeletionById($like_item['id']);
+                       }
 
                        if (!$event_verb_flag || $like_item['verb'] == $activity) {
                                return true;
                        }
                }
 
-               // Verb is "un-something", just trying to delete existing entries
-               if (strpos($verb, 'un') === 0) {
+               // No need to go further if we aren't creating anything
+               if ($mode == 'delete') {
                        return true;
                }
 
@@ -3110,7 +3141,7 @@ class Item
        private static function addThread($itemid, $onlyshadow = false)
        {
                $fields = ['uid', 'created', 'edited', 'commented', 'received', 'changed', 'wall', 'private', 'pubmail',
-                       'moderated', 'visible', 'starred', 'contact-id', 'post-type',
+                       'moderated', 'visible', 'starred', 'contact-id', 'post-type', 'uri-id',
                        'deleted', 'origin', 'forum_mode', 'mention', 'network', 'author-id', 'owner-id'];
                $condition = ["`id` = ? AND (`parent` = ? OR `parent` = 0)", $itemid, $itemid];
                $item = self::selectFirst($fields, $condition);
@@ -3131,7 +3162,7 @@ class Item
        private static function updateThread($itemid, $setmention = false)
        {
                $fields = ['uid', 'guid', 'created', 'edited', 'commented', 'received', 'changed', 'post-type',
-                       'wall', 'private', 'pubmail', 'moderated', 'visible', 'starred', 'contact-id',
+                       'wall', 'private', 'pubmail', 'moderated', 'visible', 'starred', 'contact-id', 'uri-id',
                        'deleted', 'origin', 'forum_mode', 'network', 'author-id', 'owner-id'];
                $condition = ["`id` = ? AND (`parent` = ? OR `parent` = 0)", $itemid, $itemid];