]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Post/User.php
Handle changed parents
[friendica.git] / src / Model / Post / User.php
index 6a6837c63bb54a09112585665d0096804a4e5882..7bd570699731ab1bea3267a46dfe5194bdbe306c 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2022, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -24,7 +24,7 @@ namespace Friendica\Model\Post;
 use Friendica\Database\DBA;
 use \BadMethodCallException;
 use Friendica\Database\Database;
-use Friendica\Database\DBStructure;
+use Friendica\DI;
 
 class User
 {
@@ -43,11 +43,7 @@ class User
                        throw new BadMethodCallException('Empty URI_id');
                }
 
-               if (DBA::exists('post-user', ['uri-id' => $uri_id, 'uid' => $uid])) {
-                       return false;
-               }
-
-               $fields = DBStructure::getFieldsForTable('post-user', $data);
+               $fields = DI::dbaDefinition()->truncateFieldsForTable('post-user', $data);
 
                // Additionally assign the key fields
                $fields['uri-id'] = $uri_id;
@@ -58,6 +54,33 @@ class User
                        $fields['unseen'] = false;
                }
 
+               // Does the entry already exist?
+               if (DBA::exists('post-user', ['uri-id' => $uri_id, 'uid' => $uid])) {
+                       $postuser = DBA::selectFirst('post-user', [], ['uri-id' => $uri_id, 'uid' => $uid]);
+
+                       // We quit here, when there are obvious differences
+                       foreach (['created', 'owner-id', 'author-id', 'vid', 'network', 'private', 'wall', 'origin'] as $key) {
+                               if ($fields[$key] != $postuser[$key]) {
+                                       return 0;
+                               }
+                       }
+                       
+                       $update = [];
+                       foreach (['gravity', 'parent-uri-id', 'thr-parent-id'] as $key) {
+                               if ($fields[$key] != $postuser[$key]) {
+                                       $update[$key] = $fields[$key];
+                               }
+                       }
+
+                       // When the parents changed, we apply these changes to the existing entry
+                       if (!empty($update)) {
+                               DBA::update('post-user', $update, ['id' => $postuser['id']]);
+                               return $postuser['id'];
+                       } else {
+                               return 0;
+                       }
+               }
+
                if (!DBA::insert('post-user', $fields, Database::INSERT_IGNORE)) {
                        return 0;
                }
@@ -81,7 +104,7 @@ class User
                        throw new BadMethodCallException('Empty URI_id');
                }
 
-               $fields = DBStructure::getFieldsForTable('post-user', $data);
+               $fields = DI::dbaDefinition()->truncateFieldsForTable('post-user', $data);
 
                // Remove the key fields
                unset($fields['uri-id']);