]> git.mxchange.org Git - friendica.git/commitdiff
Use an insert to avoid duplicates and for analyzing
authorMichael <heluecht@pirati.ca>
Sun, 14 Jul 2019 10:22:19 +0000 (10:22 +0000)
committerMichael <heluecht@pirati.ca>
Sun, 14 Jul 2019 10:22:19 +0000 (10:22 +0000)
src/Model/Contact.php
src/Protocol/Diaspora.php

index e892c99f2a3b341a5766630958cae35793fb0bc4..9f6fa6264b3736272b421455aa3346f671643e4a 100644 (file)
@@ -1418,13 +1418,22 @@ class Contact extends BaseObject
 
                        $condition = ['nurl' => Strings::normaliseLink($data["url"]), 'uid' => $uid, 'deleted' => false];
 
-                       // This does an insert but should most likely prevent duplicates
-                       DBA::update('contact', $fields, $condition, true);
-
+                       // Before inserting we do check if the entry does exist now.
                        $contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]);
                        if (!DBA::isResult($contact)) {
-                               // Shouldn't happen
-                               return 0;
+                               Logger::info('Create new contact', $fields);
+
+                               DBA::insert('contact', $fields);
+
+                               // We intentionally aren't using lastInsertId here. There is a chance for duplicates.
+                               $contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]);
+                               if (!DBA::isResult($contact)) {
+                                       Logger::info('Contact creation failed', $fields);
+                                       // Shouldn't happen
+                                       return 0;
+                               }
+                       } else {
+                               Logger::info('Contact had been created before', ['id' => $contact["id"], 'url' => $url, 'contact' => $fields]);
                        }
 
                        $contact_id = $contact["id"];
index 5f37684ad640a4a02d69e264580818941880be1d..8e2e487796a300c373edddebc880cd9833e3af7f 100644 (file)
@@ -191,15 +191,17 @@ class Diaspora
                $fields = array_merge($fields, $network_fields);
 
                $condition = ['uid' => 0, 'nurl' => Strings::normaliseLink($server_url)];
-               $relay = DBA::selectFirst('contact', ['id'], $condition);
-               if (DBA::isResult($relay)) {
+               $old = DBA::selectFirst('contact', [], $condition);
+               if (DBA::isResult($old)) {
                        unset($fields['created']);
-                       $condition = ['id' => $relay['id']];
-               }
-
-               Logger::info('Set relay contact', ['fields' => $fields, 'condition' => $condition]);
+                       $condition = ['id' => $old['id']];
 
-               DBA::update('contact', $fields, $condition, true);
+                       Logger::info('Update relay contact', ['fields' => $fields, 'condition' => $condition]);
+                       DBA::update('contact', $fields, $condition, $old);
+               } else {
+                       Logger::info('Create relay contact', ['fields' => $fields]);
+                       DBA::insert('contact', $fields);
+               }
        }
 
        /**