]> git.mxchange.org Git - friendica.git/commitdiff
Change the owner of a reshared item
authorMichael <heluecht@pirati.ca>
Thu, 13 Aug 2020 06:09:26 +0000 (06:09 +0000)
committerMichael <heluecht@pirati.ca>
Thu, 13 Aug 2020 06:09:26 +0000 (06:09 +0000)
include/conversation.php
src/Model/Item.php

index 8969bfd4b6d215c9c04130b34952fd596dd6c7f6..1c9c37d24a7c872d9a0ca2f06c477c55d505f05e 100644 (file)
@@ -708,48 +708,17 @@ function conversation(App $a, array $items, $mode, $update, $preview = false, $o
  */
 function conversation_fetch_comments($thread_items, $pinned) {
        $comments = [];
-       $parentlines = [];
-       $lineno = 0;
-       $actor = [];
-       $received = '';
-       $owner = '';
 
        while ($row = Item::fetch($thread_items)) {
-               if (($row['verb'] == Activity::ANNOUNCE) && ($row['author-link'] == $owner)) {
-                       continue;
-               }
-
-               if (($row['verb'] == Activity::ANNOUNCE) && !empty($row['contact-uid']) && ($row['received'] > $received) && ($row['thr-parent'] == $row['parent-uri'])) {
-                       $actor = ['link' => $row['author-link'], 'avatar' => $row['author-avatar'], 'name' => $row['author-name']];
-                       $received = $row['received'];
-               }
-
-               if ((($row['gravity'] == GRAVITY_PARENT) && !$row['origin'] && !in_array($row['network'], [Protocol::DIASPORA])) &&
-                       (empty($row['contact-uid']) || !in_array($row['network'], Protocol::NATIVE_SUPPORT))) {
-                       $parentlines[] = $lineno;
-               }
-
-               if (($row['gravity'] == GRAVITY_PARENT) && ($row['author-link'] != $row['owner-link'])) {
-                       $owner = $row['owner-link'];
-               }
-
                if ($row['gravity'] == GRAVITY_PARENT) {
                        $row['pinned'] = $pinned;
                }
 
                $comments[] = $row;
-               $lineno++;
        }
 
        DBA::close($thread_items);
 
-       if (!empty($actor)) {
-               foreach ($parentlines as $line) {
-                       $comments[$line]['owner-link'] = $actor['link'];
-                       $comments[$line]['owner-avatar'] = $actor['avatar'];
-                       $comments[$line]['owner-name'] = $actor['name'];
-               }
-       }
        return $comments;
 }
 
index 778b872bbb751452c2894c6ba2d2c2c94aebeba8..1fc4be2f553dda1c6ada20a14eaf2fb5047895e3 100644 (file)
@@ -1800,7 +1800,9 @@ class Item
                // It is mainly used in the "post_local" hook.
                unset($item['api_source']);
 
-               self::transformToForumPost($item);
+               if ($item['verb'] == Activity::ANNOUNCE) {
+                       self::setOwnerforResharedItem($item);
+               }
 
                // Check for hashtags in the body and repair or add hashtag links
                $item['body'] = self::setHashtags($item['body']);
@@ -1992,34 +1994,51 @@ class Item
        }
 
        /**
-        * Convert items to forum posts
+        * Change the owner of a parent item if it had been shared by a forum or a user contact
         *
         * (public) forum posts in the new format consist of the regular post by the author
         * followed by an announce message sent from the forum account.
-        * This means we have to look out for an announce message send by a forum account.
+        * Changing the owner helps in grouping forum posts.
+        *
+        * For other reshared posts this function changes the owner of that post
+        * if the author is not a user contact but the post had been shared by one.
+        * This helps in detecting why this post appeared in the timeline.
         *
         * @param array $item
         * @return void
         */
-       private static function transformToForumPost(array $item)
+       private static function setOwnerforResharedItem(array $item)
        {
-               if ($item["verb"] != Activity::ANNOUNCE) {
-                       // No announce message, so don't do anything
+               $parent = self::selectFirst(['id', 'owner-id', 'author-id', 'author-link'], ['uri-id' => $item['parent-uri-id'], 'uid' => $item['uid']]);
+               if (!DBA::isResult($parent)) {
+                       Logger::error('Parent not found', ['uri-id' => $item['parent-uri-id'], 'uid' => $item['uid']]);
                        return;
                }
 
-               $pcontact = Contact::selectFirst(['nurl'], ['id' => $item['author-id'], 'contact-type' => Contact::TYPE_COMMUNITY]);
-               if (empty($pcontact['nurl'])) {
-                       // The announce message wasn't created by a forum account, so we don't need to continue
+               $author = Contact::selectFirst(['url', 'contact-type'], ['id' => $item['author-id']]);
+               if (!DBA::isResult($author)) {
+                       Logger::error('Author not found', ['id' => $item['author-id']]);
                        return;
                }
 
-               $contact = Contact::selectFirst(['id'], ['nurl' => $pcontact['nurl'], 'uid' => $item['uid']]);
-               if (!empty($contact['id'])) {
-                       $condition = ['uri-id' => $item['parent-uri-id'], 'uid' => $item['uid']];
-                       Item::update(['owner-id' => $item['author-id'], 'contact-id' => $contact['id']], $condition);
-                       Logger::info('Convert message into a forum message', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id'], 'uid' => $item['uid'], 'owner-id' => $item['author-id'], 'contact-id' => $contact['id']]);
+               if (($author['contact-type'] != Contact::TYPE_COMMUNITY) && (($parent['owner-id'] != $parent['author-id']) || ($item['uid'] == 0))) {
+                       logger::info('The resharer is no forum and the parent is reshared or not a personal post: quit', ['resharer' => $item['author-id'], 'owner' => $parent['owner-id'], 'author' => $parent['author-id'], 'uid' => $item['uid']]);
+                       return;
+               }
+
+               if (Contact::getIdForURL($parent['author-link'], $item['uid'])) {
+                       logger::info('The parent author is a user contact: quit', ['author' => $parent['author-link'], 'uid' => $item['uid']]);
+                       return;
+               }
+
+               $cid = Contact::getIdForURL($author['url'], $item['uid']);
+               if (empty($cid)) {
+                       logger::info('The resharer is not a user contact: quit', ['resharer' => $author['url'], 'uid' => $item['uid']]);
+                       return;
                }
+
+               Item::update(['owner-id' => $item['author-id'], 'contact-id' => $cid], ['id' => $parent['id']]);
+               Logger::info('Change owner of the parent', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id'], 'uid' => $item['uid'], 'owner-id' => $item['author-id'], 'contact-id' => $cid]);
        }
 
        /**