]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Item.php
Added post update to remove duplicated contacts
[friendica.git] / src / Model / Item.php
index 248e646afcd3a6ed8b545809f6205a69e56d2233..3a421e96c43508d8ad598cc1419ae4cf4707f1ea 100644 (file)
@@ -59,7 +59,7 @@ class Item extends BaseObject
                '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_count', 'delivery_queue_done', 'delivery_queue_failed'
        ];
 
        // Field list that is used to deliver items via the protocols
@@ -883,6 +883,8 @@ class Item extends BaseObject
                // When there is no content for the "old" item table, this will count the fetched items
                $rows = DBA::affectedRows();
 
+               $notify_items = [];
+
                while ($item = DBA::fetch($items)) {
                        if (!empty($item['iaid']) || (!empty($content_fields['verb']) && (self::activityToIndex($content_fields['verb']) >= 0))) {
                                self::updateActivity($content_fields, ['uri-id' => $item['uri-id']]);
@@ -954,12 +956,17 @@ class Item extends BaseObject
                        // We only need to notfiy others when it is an original entry from us.
                        // Only call the notifier when the item has some content relevant change.
                        if ($item['origin'] && in_array('edited', array_keys($fields))) {
-                               Worker::add(PRIORITY_HIGH, "Notifier", Delivery::POST, $item['id']);
+                               $notify_items[] = $item['id'];
                        }
                }
 
                DBA::close($items);
                DBA::commit();
+
+               foreach ($notify_items as $notify_item) {
+                       Worker::add(PRIORITY_HIGH, "Notifier", Delivery::POST, $notify_item);
+               }
+
                return $rows;
        }
 
@@ -1234,7 +1241,11 @@ class Item extends BaseObject
 
        private static function contactId($item)
        {
-               if (!empty($item['uid']) && !Contact::isSharing($item['author-id'], $item['uid'])) {
+               if (!empty($item['contact-id']) && DBA::exists('contact', ['self' => true, 'id' => $item['contact-id']])) {
+                       return $item['contact-id'];
+               } elseif (($item['gravity'] == GRAVITY_PARENT) && !empty($item['uid']) && !empty($item['contact-id']) && Contact::isSharing($item['contact-id'], $item['uid'])) {
+                       return $item['contact-id'];
+               } elseif (!empty($item['uid']) && !Contact::isSharing($item['author-id'], $item['uid'])) {
                        return $item['author-id'];
                } elseif (!empty($item['contact-id'])) {
                        return $item['contact-id'];
@@ -1244,7 +1255,6 @@ class Item extends BaseObject
                                return $contact_id;
                        }
                }
-
                return $item['author-id'];
        }
 
@@ -1526,7 +1536,7 @@ class Item extends BaseObject
                unset($item['causer-link']);
 
                // The contact-id should be set before "self::insert" was called - but there seems to be issues sometimes
-               $item['contact-id'] = self::contactId($item);
+               $item["contact-id"] = self::contactId($item);
 
                if ($item['network'] == Protocol::PHANTOM) {
                        $item['network'] = Protocol::DFRN;
@@ -1790,6 +1800,8 @@ class Item extends BaseObject
                unset($item['owner-name']);
                unset($item['owner-avatar']);
 
+               $like_no_comment = Config::get('system', 'like_no_comment');
+
                DBA::transaction();
                $ret = DBA::insert('item', $item);
 
@@ -1832,7 +1844,6 @@ class Item extends BaseObject
                }
 
                Logger::log('created item '.$current_post);
-               self::updateContact($item);
 
                if (!$parent_id || ($item['parent-uri'] === $item['uri'])) {
                        $parent_id = $current_post;
@@ -1846,7 +1857,7 @@ class Item extends BaseObject
 
                // update the commented timestamp on the parent
                // Only update "commented" if it is really a comment
-               if (($item['gravity'] != GRAVITY_ACTIVITY) || !Config::get("system", "like_no_comment")) {
+               if (($item['gravity'] != GRAVITY_ACTIVITY) || !$like_no_comment) {
                        DBA::update('item', ['commented' => DateTimeFormat::utcNow(), 'changed' => DateTimeFormat::utcNow()], ['id' => $parent_id]);
                } else {
                        DBA::update('item', ['changed' => DateTimeFormat::utcNow()], ['id' => $parent_id]);
@@ -1875,32 +1886,6 @@ class Item extends BaseObject
                        DBA::insert('diaspora-interaction', ['uri-id' => $item['uri-id'], 'interaction' => $diaspora_signed_text], true);
                }
 
-               // In that function we check if this is a forum post. Additionally we delete the item under certain circumstances
-               if (self::tagDeliver($item['uid'], $current_post)) {
-                       // Get the user information for the logging
-                       $user = User::getById($uid);
-
-                       Logger::notice('Item had been deleted', ['id' => $current_post, 'user' => $uid, 'account-type' => $user['account-type']]);
-                       return 0;
-               }
-
-               /*
-                * current post can be deleted if is for a community page and no mention are
-                * in it.
-                */
-               if (!$dontcache) {
-                       $posted_item = self::selectFirst(self::ITEM_FIELDLIST, ['id' => $current_post]);
-                       if (DBA::isResult($posted_item)) {
-                               if ($notify) {
-                                       Hook::callAll('post_local_end', $posted_item);
-                               } else {
-                                       Hook::callAll('post_remote_end', $posted_item);
-                               }
-                       } else {
-                               Logger::log('new item not found in DB, id ' . $current_post);
-                       }
-               }
-
                if ($item['parent-uri'] === $item['uri']) {
                        self::addThread($current_post);
                } else {
@@ -1925,12 +1910,36 @@ class Item extends BaseObject
                        Term::insertFromFileFieldByItemId($current_post, $files);
                }
 
+               // In that function we check if this is a forum post. Additionally we delete the item under certain circumstances
+               if (self::tagDeliver($item['uid'], $current_post)) {
+                       // Get the user information for the logging
+                       $user = User::getById($uid);
+
+                       Logger::notice('Item had been deleted', ['id' => $current_post, 'user' => $uid, 'account-type' => $user['account-type']]);
+                       return 0;
+               }
+
+               if (!$dontcache) {
+                       $posted_item = self::selectFirst(self::ITEM_FIELDLIST, ['id' => $current_post]);
+                       if (DBA::isResult($posted_item)) {
+                               if ($notify) {
+                                       Hook::callAll('post_local_end', $posted_item);
+                               } else {
+                                       Hook::callAll('post_remote_end', $posted_item);
+                               }
+                       } else {
+                               Logger::log('new item not found in DB, id ' . $current_post);
+                       }
+               }
+
                if ($item['parent-uri'] === $item['uri']) {
                        self::addShadow($current_post);
                } else {
                        self::addShadowPost($current_post);
                }
 
+               self::updateContact($item);
+
                check_user_notification($current_post);
 
                if ($notify || ($item['visible'] && ((!empty($parent) && $parent['origin']) || $item['origin']))) {
@@ -2802,7 +2811,7 @@ class Item extends BaseObject
                                                                        $replace = true;
                                                                }
                                                        } elseif ($item) {
-                                                               if (self::samePermissions($item, $photo)) {
+                                                               if (self::samePermissions($uid, $item, $photo)) {
                                                                        $replace = true;
                                                                }
                                                        }
@@ -2852,7 +2861,7 @@ class Item extends BaseObject
                        !empty($obj['deny_cid']) || !empty($obj['deny_gid']);
        }
 
-       private static function samePermissions($obj1, $obj2)
+       private static function samePermissions($uid, $obj1, $obj2)
        {
                // first part is easy. Check that these are exactly the same.
                if (($obj1['allow_cid'] == $obj2['allow_cid'])
@@ -2872,13 +2881,20 @@ class Item extends BaseObject
                return ($recipients1 == $recipients2);
        }
 
-       // returns an array of contact-ids that are allowed to see this object
-       public static function enumeratePermissions($obj)
+       /**
+        * Returns an array of contact-ids that are allowed to see this object
+        *
+        * @param array $obj        Item array with at least uid, allow_cid, allow_gid, deny_cid and deny_gid
+        * @param bool  $check_dead Prunes unavailable contacts from the result
+        * @return array
+        * @throws \Exception
+        */
+       public static function enumeratePermissions(array $obj, bool $check_dead = false)
        {
                $allow_people = expand_acl($obj['allow_cid']);
-               $allow_groups = Group::expand(expand_acl($obj['allow_gid']));
+               $allow_groups = Group::expand($obj['uid'], expand_acl($obj['allow_gid']), $check_dead);
                $deny_people  = expand_acl($obj['deny_cid']);
-               $deny_groups  = Group::expand(expand_acl($obj['deny_gid']));
+               $deny_groups  = Group::expand($obj['uid'], expand_acl($obj['deny_gid']), $check_dead);
                $recipients   = array_unique(array_merge($allow_people, $allow_groups));
                $deny         = array_unique(array_merge($deny_people, $deny_groups));
                $recipients   = array_diff($recipients, $deny);