]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Contact.php
Merge pull request #9947 from annando/database
[friendica.git] / src / Model / Contact.php
index 9f7834f6fbf53885dfe364c833d71b25d76ea6c4..f96c556d241784ce4e86279b7c6517efa0f2c4cc 100644 (file)
@@ -34,7 +34,6 @@ use Friendica\Core\Worker;
 use Friendica\Database\Database;
 use Friendica\Database\DBA;
 use Friendica\DI;
-use Friendica\Model\Notify\Type;
 use Friendica\Network\HTTPException;
 use Friendica\Network\Probe;
 use Friendica\Protocol\Activity;
@@ -724,7 +723,7 @@ class Contact
        {
                // We want just to make sure that we don't delete our "self" contact
                $contact = DBA::selectFirst('contact', ['uid'], ['id' => $id, 'self' => false]);
-               if (!DBA::isResult($contact) || !intval($contact['uid'])) {
+               if (!DBA::isResult($contact)) {
                        return;
                }
 
@@ -809,7 +808,7 @@ class Contact
                        Logger::info('Empty contact', ['contact' => $contact, 'callstack' => System::callstack(20)]);
                }
 
-               Logger::info('Contact is marked for archival', ['id' => $contact['id']]);
+               Logger::info('Contact is marked for archival', ['id' => $contact['id'], 'term-date' => $contact['term-date']]);
 
                // Contact already archived or "self" contact? => nothing to do
                if ($contact['archive'] || $contact['self']) {
@@ -867,7 +866,7 @@ class Contact
                        return;
                }
 
-               Logger::info('Contact is marked as vital again', ['id' => $contact['id']]);
+               Logger::info('Contact is marked as vital again', ['id' => $contact['id'], 'term-date' => $contact['term-date']]);
 
                if (!isset($contact['url']) && !empty($contact['id'])) {
                        $fields = ['id', 'url', 'batch'];
@@ -906,7 +905,7 @@ class Contact
 
                if (empty($contact['uid']) || ($contact['uid'] != $uid)) {
                        if ($uid == 0) {
-                               $profile_link = self::magicLink($contact['url']);
+                               $profile_link = self::magicLinkByContact($contact);
                                $menu = ['profile' => [DI::l10n()->t('View Profile'), $profile_link, true]];
 
                                return $menu;
@@ -1296,9 +1295,9 @@ class Contact
                }
 
                if (empty($contact["network"]) || in_array($contact["network"], Protocol::FEDERATED)) {
-                       $sql = "(`item`.`uid` = 0 OR (`item`.`uid` = ? AND NOT `item`.`global`))";
+                       $sql = "(`uid` = 0 OR (`uid` = ? AND NOT `global`))";
                } else {
-                       $sql = "`item`.`uid` = ?";
+                       $sql = "`uid` = ?";
                }
 
                $contact_field = ((($contact["contact-type"] == self::TYPE_COMMUNITY) || ($contact['network'] == Protocol::MAIL)) ? 'owner-id' : 'author-id');
@@ -1340,18 +1339,11 @@ class Contact
                }
 
                if ($thread_mode) {             
-                       $r = Item::selectForUser(local_user(), ['uri', 'gravity', 'parent-uri', 'thr-parent-id', 'author-id'], $condition, $params);
-                       $items = [];
-                       while ($item = DBA::fetch($r)) {
-                               $items[] = $item;
-                       }
-                       DBA::close($r);
+                       $items = Post::toArray(Post::selectForUser(local_user(), ['uri-id', 'gravity', 'parent-uri-id', 'thr-parent-id', 'author-id'], $condition, $params));
 
                        $o .= conversation($a, $items, 'contacts', $update, false, 'commented', local_user());
                } else {
-                       $r = Item::selectForUser(local_user(), [], $condition, $params);
-
-                       $items = Item::inArray($r);
+                       $items = Post::toArray(Post::selectForUser(local_user(), Item::DISPLAY_FIELDLIST, $condition, $params));
 
                        $o .= conversation($a, $items, 'contact-posts', $update);
                }
@@ -1678,7 +1670,7 @@ class Contact
                        }
                }
 
-               $default_avatar = empty($avatar) || ($avatar == DI::baseUrl() . self::DEFAULT_AVATAR_PHOTO);
+               $default_avatar = empty($avatar) || strpos($avatar, self::DEFAULT_AVATAR_PHOTO);
 
                if ($default_avatar) {
                        $avatar = self::getDefaultAvatar($contact, Proxy::SIZE_SMALL);
@@ -1762,6 +1754,17 @@ class Contact
                DBA::update('contact', $fields, ['id' => $cids]);
        }
 
+       public static function deleteContactByUrl(string $url)
+       {
+               // Update contact data for all users
+               $condition = ['self' => false, 'nurl' => Strings::normaliseLink($url)];
+               $contacts = DBA::select('contact', ['id', 'uid'], $condition);
+               while ($contact = DBA::fetch($contacts)) {
+                       Logger::info('Deleting contact', ['id' => $contact['id'], 'uid' => $contact['uid'], 'url' => $url]);
+                       self::remove($contact['id']);
+               }
+       }
+
        /**
         * Helper function for "updateFromProbe". Updates personal and public contact
         *
@@ -1914,6 +1917,15 @@ class Contact
                        return false;
                }
 
+               if (!empty($ret['account-type']) && $ret['account-type'] == User::ACCOUNT_TYPE_DELETED) {
+                       Logger::info('Deleted account', ['id' => $id, 'url' => $ret['url'], 'ret' => $ret]);
+                       self::remove($id);
+
+                       // Delete all contacts with the same URL
+                       self::deleteContactByUrl($ret['url']);
+                       return true;
+               }
+
                $uid = $contact['uid'];
                unset($contact['uid']);
 
@@ -2486,12 +2498,12 @@ class Contact
 
                                Group::addMember(User::getDefaultGroup($importer['uid'], $contact_record["network"]), $contact_record['id']);
 
-                               if (($user['notify-flags'] & Type::INTRO) &&
+                               if (($user['notify-flags'] & Notification\Type::INTRO) &&
                                        in_array($user['page-flags'], [User::PAGE_FLAGS_NORMAL])) {
 
                                        notification([
-                                               'type'  => Type::INTRO,
-                                               'otype' => Notify\ObjectType::INTRO,
+                                               'type'  => Notification\Type::INTRO,
+                                               'otype' => Notification\ObjectType::INTRO,
                                                'verb'  => ($sharing ? Activity::FRIEND : Activity::FOLLOW),
                                                'uid'   => $user['uid'],
                                                'cid'   => $contact_record['id'],
@@ -2638,7 +2650,7 @@ class Contact
         * @throws HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function magicLinkbyId($cid, $url = '')
+       public static function magicLinkById($cid, $url = '')
        {
                $contact = DBA::selectFirst('contact', ['id', 'network', 'url', 'uid'], ['id' => $cid]);
 
@@ -2672,14 +2684,10 @@ class Contact
                        return 'contact/' . $contact['id'] . '/conversations';
                }
 
-               if ($contact['network'] != Protocol::DFRN) {
+               if (!empty($contact['network']) && $contact['network'] != Protocol::DFRN) {
                        return $destination;
                }
 
-               if (!empty($contact['uid'])) {
-                       return self::magicLink($contact['url'], $url);
-               }
-
                if (empty($contact['id'])) {
                        return $destination;
                }
@@ -2807,22 +2815,22 @@ class Contact
        }
 
        /**
-        * Returns a random, global contact of the current node
+        * Returns a random, global contact array of the current node
         *
-        * @return string The profile URL
+        * @return array The profile array
         * @throws Exception
         */
-       public static function getRandomUrl()
+       public static function getRandomContact()
        {
-               $r = DBA::selectFirst('contact', ['url'], [
+               $contact = DBA::selectFirst('contact', ['id', 'network', 'url', 'uid'], [
                        "`uid` = ? AND `network` = ? AND NOT `failed` AND `last-item` > ?",
                        0, Protocol::DFRN, DateTimeFormat::utc('now - 1 month'),
                ], ['order' => ['RAND()']]);
 
-               if (DBA::isResult($r)) {
-                       return $r['url'];
+               if (DBA::isResult($contact)) {
+                       return $contact;
                }
 
-               return '';
+               return [];
        }
 }