]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Contact.php
Merge pull request #12674 from nupplaphil/bug/config_typesafe
[friendica.git] / src / Model / Contact.php
index 3b85eae8e7d0c5c7cdf2799a64b123c5f51501a6..e847ce034666cf458ce7121363127af7f5d8eac1 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -23,6 +23,7 @@ namespace Friendica\Model;
 
 use Friendica\Contact\Avatar;
 use Friendica\Contact\Introduction\Exception\IntroductionNotFoundException;
+use Friendica\Content\Conversation As ConversationContent;
 use Friendica\Content\Pager;
 use Friendica\Content\Text\HTML;
 use Friendica\Core\Hook;
@@ -47,6 +48,7 @@ use Friendica\Util\Images;
 use Friendica\Util\Network;
 use Friendica\Util\Proxy;
 use Friendica\Util\Strings;
+use Friendica\Worker\UpdateContact;
 
 /**
  * functions for interacting with a contact
@@ -329,7 +331,7 @@ class Contact
                // Add internal fields
                $removal = [];
                if (!empty($fields)) {
-                       foreach (['id', 'next-update', 'network'] as $internal) {
+                       foreach (['id', 'next-update', 'network', 'local-data'] as $internal) {
                                if (!in_array($internal, $fields)) {
                                        $fields[] = $internal;
                                        $removal[] = $internal;
@@ -358,9 +360,15 @@ class Contact
                        return [];
                }
 
+               $background_update = DI::config()->get('system', 'update_active_contacts') ? $contact['local-data'] : true;
+
                // Update the contact in the background if needed
-               if (Probe::isProbable($contact['network']) && ($contact['next-update'] < DateTimeFormat::utcNow())) {
-                       Worker::add(['priority' => Worker::PRIORITY_LOW, 'dont_fork' => true], 'UpdateContact', $contact['id']);
+               if ($background_update && !self::isLocal($url) && Probe::isProbable($contact['network']) && ($contact['next-update'] < DateTimeFormat::utcNow())) {
+                       try {
+                               UpdateContact::add(['priority' => Worker::PRIORITY_LOW, 'dont_fork' => true], $contact['id']);
+                       } catch (\InvalidArgumentException $e) {
+                               Logger::notice($e->getMessage(), ['contact' => $contact]);
+                       }
                }
 
                // Remove the internal fields
@@ -622,7 +630,7 @@ class Contact
        public static function getPublicAndUserContactID(int $cid, int $uid): array
        {
                // We have to use the legacy function as long as the post update hasn't finished
-               if (DI::config()->get('system', 'post_update_version') < 1427) {
+               if (DI::keyValue()->get('post_update_version') < 1427) {
                        return self::legacyGetPublicAndUserContactID($cid, $uid);
                }
 
@@ -1130,39 +1138,25 @@ class Contact
         * Returns the data array for the photo menu of a given contact
         *
         * @param array $contact contact
-        * @param int   $uid     optional, default 0
+        * @param int   $uid     Visitor user id
         * @return array
         * @throws HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function photoMenu(array $contact, int $uid = 0): array
+       public static function photoMenu(array $contact, int $uid): array
        {
-               $pm_url = '';
-               $status_link = '';
-               $photos_link = '';
-
-               if ($uid == 0) {
-                       $uid = DI::userSession()->getLocalUserId();
+               // Anonymous visitor
+               if (!$uid) {
+                       return ['profile' => [DI::l10n()->t('View Profile'), self::magicLinkByContact($contact), true]];
                }
 
-               if (empty($contact['uid']) || ($contact['uid'] != $uid)) {
-                       if ($uid == 0) {
-                               $profile_link = self::magicLinkByContact($contact);
-                               $menu = ['profile' => [DI::l10n()->t('View Profile'), $profile_link, true]];
-
-                               return $menu;
-                       }
-
-                       // Look for our own contact if the uid doesn't match and isn't public
-                       $contact_own = DBA::selectFirst('contact', [], ['nurl' => $contact['nurl'], 'network' => $contact['network'], 'uid' => $uid]);
-                       if (DBA::isResult($contact_own)) {
-                               return self::photoMenu($contact_own, $uid);
-                       }
-               }
+               $pm_url      = '';
+               $status_link = '';
+               $photos_link = '';
 
                $sparkle = false;
                if (($contact['network'] === Protocol::DFRN) && !$contact['self'] && empty($contact['pending'])) {
-                       $sparkle = true;
+                       $sparkle      = true;
                        $profile_link = 'contact/redir/' . $contact['id'];
                } else {
                        $profile_link = $contact['url'];
@@ -1173,8 +1167,8 @@ class Contact
                }
 
                if ($sparkle) {
-                       $status_link = $profile_link . '/status';
-                       $photos_link = $profile_link . '/photos';
+                       $status_link  = $profile_link . '/status';
+                       $photos_link  = $profile_link . '/photos';
                        $profile_link = $profile_link . '/profile';
                }
 
@@ -1183,15 +1177,14 @@ class Contact
                }
 
                $contact_url = 'contact/' . $contact['id'];
-
                $posts_link = 'contact/' . $contact['id'] . '/conversations';
 
-               $follow_link = '';
+               $follow_link   = '';
                $unfollow_link = '';
                if (!$contact['self'] && Protocol::supportsFollow($contact['network'])) {
                        if ($contact['uid'] && in_array($contact['rel'], [self::SHARING, self::FRIEND])) {
                                $unfollow_link = 'contact/unfollow?url=' . urlencode($contact['url']) . '&auto=1';
-                       } elseif(!$contact['pending']) {
+                       } elseif (!$contact['pending']) {
                                $follow_link = 'contact/follow?url=' . urlencode($contact['url']) . '&auto=1';
                        }
                }
@@ -1202,27 +1195,27 @@ class Contact
                 */
                if (empty($contact['uid'])) {
                        $menu = [
-                               'profile' => [DI::l10n()->t('View Profile')  , $profile_link , true],
-                               'network' => [DI::l10n()->t('Network Posts') , $posts_link   , false],
-                               'edit'    => [DI::l10n()->t('View Contact')  , $contact_url  , false],
-                               'follow'  => [DI::l10n()->t('Connect/Follow'), $follow_link  , true],
-                               'unfollow'=> [DI::l10n()->t('Unfollow')      , $unfollow_link, true],
+                               'profile'  => [DI::l10n()->t('View Profile')  , $profile_link , true ],
+                               'network'  => [DI::l10n()->t('Network Posts') , $posts_link   , false],
+                               'edit'     => [DI::l10n()->t('View Contact')  , $contact_url  , false],
+                               'follow'   => [DI::l10n()->t('Connect/Follow'), $follow_link  , true ],
+                               'unfollow' => [DI::l10n()->t('Unfollow')      , $unfollow_link, true ],
                        ];
                } else {
                        $menu = [
-                               'status'  => [DI::l10n()->t('View Status')   , $status_link      , true],
-                               'profile' => [DI::l10n()->t('View Profile')  , $profile_link     , true],
-                               'photos'  => [DI::l10n()->t('View Photos')   , $photos_link      , true],
-                               'network' => [DI::l10n()->t('Network Posts') , $posts_link       , false],
-                               'edit'    => [DI::l10n()->t('View Contact')  , $contact_url      , false],
-                               'pm'      => [DI::l10n()->t('Send PM')       , $pm_url           , false],
-                               'follow'  => [DI::l10n()->t('Connect/Follow'), $follow_link      , true],
-                               'unfollow'=> [DI::l10n()->t('Unfollow')      , $unfollow_link    , true],
+                               'status'   => [DI::l10n()->t('View Status')   , $status_link  , true ],
+                               'profile'  => [DI::l10n()->t('View Profile')  , $profile_link , true ],
+                               'photos'   => [DI::l10n()->t('View Photos')   , $photos_link  , true ],
+                               'network'  => [DI::l10n()->t('Network Posts') , $posts_link   , false],
+                               'edit'     => [DI::l10n()->t('View Contact')  , $contact_url  , false],
+                               'pm'       => [DI::l10n()->t('Send PM')       , $pm_url       , false],
+                               'follow'   => [DI::l10n()->t('Connect/Follow'), $follow_link  , true ],
+                               'unfollow' => [DI::l10n()->t('Unfollow')      , $unfollow_link, true ],
                        ];
 
                        if (!empty($contact['pending'])) {
                                try {
-                                       $intro = DI::intro()->selectForContact($contact['id']);
+                                       $intro          = DI::intro()->selectForContact($contact['id']);
                                        $menu['follow'] = [DI::l10n()->t('Approve'), 'notifications/intros/' . $intro->id, true];
                                } catch (IntroductionNotFoundException $exception) {
                                        DI::logger()->error('Pending contact doesn\'t have an introduction.', ['exception' => $exception]);
@@ -1281,13 +1274,19 @@ class Contact
                        return 0;
                }
 
-               $contact = self::getByURL($url, false, ['id', 'network', 'uri-id', 'next-update'], $uid);
+               $contact = self::getByURL($url, false, ['id', 'network', 'uri-id', 'next-update', 'local-data'], $uid);
 
                if (!empty($contact)) {
                        $contact_id = $contact['id'];
 
-                       if (Probe::isProbable($contact['network']) && ($contact['next-update'] < DateTimeFormat::utcNow())) {
-                               Worker::add(['priority' => Worker::PRIORITY_LOW, 'dont_fork' => true], 'UpdateContact', $contact['id']);
+                       $background_update = DI::config()->get('system', 'update_active_contacts') ? $contact['local-data'] : true;
+
+                       if ($background_update && !self::isLocal($url) && Probe::isProbable($contact['network']) && ($contact['next-update'] < DateTimeFormat::utcNow())) {
+                               try {
+                                       UpdateContact::add(['priority' => Worker::PRIORITY_LOW, 'dont_fork' => true], $contact['id']);
+                               } catch (\InvalidArgumentException $e) {
+                                       Logger::notice($e->getMessage(), ['contact' => $contact]);
+                               }
                        }
 
                        if (empty($update) && (!empty($contact['uri-id']) || is_bool($update))) {
@@ -1401,14 +1400,18 @@ class Contact
                if ($data['network'] == Protocol::DIASPORA) {
                        try {
                                DI::dsprContact()->updateFromProbeArray($data);
+                       } catch (HTTPException\NotFoundException $e) {
+                               Logger::notice($e->getMessage(), ['url' => $url, 'data' => $data]);
                        } catch (\InvalidArgumentException $e) {
-                               Logger::error($e->getMessage(), ['url' => $url, 'data' => $data]);
+                               Logger::notice($e->getMessage(), ['url' => $url, 'data' => $data]);
                        }
                } elseif (!empty($data['networks'][Protocol::DIASPORA])) {
                        try {
                                DI::dsprContact()->updateFromProbeArray($data['networks'][Protocol::DIASPORA]);
+                       } catch (HTTPException\NotFoundException $e) {
+                               Logger::notice($e->getMessage(), ['url' => $url, 'data' => $data['networks'][Protocol::DIASPORA]]);
                        } catch (\InvalidArgumentException $e) {
-                               Logger::error($e->getMessage(), ['url' => $url, 'data' => $data['networks'][Protocol::DIASPORA]]);
+                               Logger::notice($e->getMessage(), ['url' => $url, 'data' => $data['networks'][Protocol::DIASPORA]]);
                        }
                }
 
@@ -1555,8 +1558,8 @@ class Contact
                $contact_field = ((($contact["contact-type"] == self::TYPE_COMMUNITY) || ($contact['network'] == Protocol::MAIL)) ? 'owner-id' : 'author-id');
 
                if ($thread_mode) {
-                       $condition = ["((`$contact_field` = ? AND `gravity` = ?) OR (`author-id` = ? AND `gravity` = ? AND `vid` = ? AND `thr-parent-id` = `parent-uri-id`)) AND " . $sql,
-                               $cid, Item::GRAVITY_PARENT, $cid, Item::GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), DI::userSession()->getLocalUserId()];
+                       $condition = ["((`$contact_field` = ? AND `gravity` = ?) OR (`author-id` = ? AND `gravity` = ? AND `vid` = ? AND `protocol` != ? AND `thr-parent-id` = `parent-uri-id`)) AND " . $sql,
+                               $cid, Item::GRAVITY_PARENT, $cid, Item::GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), Conversation::PARCEL_DIASPORA, DI::userSession()->getLocalUserId()];
                } else {
                        $condition = ["`$contact_field` = ? AND `gravity` IN (?, ?) AND " . $sql,
                                $cid, Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT, DI::userSession()->getLocalUserId()];
@@ -1607,7 +1610,7 @@ class Contact
                                }
                        }
 
-                       $o .= DI::conversation()->create($items, 'contacts', $update, false, 'pinned_commented', DI::userSession()->getLocalUserId());
+                       $o .= DI::conversation()->create($items, ConversationContent::MODE_CONTACTS, $update, false, 'pinned_commented', DI::userSession()->getLocalUserId());
                } else {
                        $fields = array_merge(Item::DISPLAY_FIELDLIST, ['featured']);
                        $items = Post::toArray(Post::selectForUser(DI::userSession()->getLocalUserId(), $fields, $condition, $params));
@@ -1622,7 +1625,7 @@ class Contact
                                }
                        }
 
-                       $o .= DI::conversation()->create($items, 'contact-posts', $update);
+                       $o .= DI::conversation()->create($items, ConversationContent::MODE_CONTACT_POSTS, $update);
                }
 
                if (!$update) {
@@ -2249,7 +2252,9 @@ class Contact
                }
 
                if (in_array($contact['network'], [Protocol::FEED, Protocol::MAIL]) || $cache_avatar) {
-                       Avatar::deleteCache($contact);
+                       if (Avatar::deleteCache($contact)) {
+                               $force = true;
+                       }
 
                        if ($default_avatar && Proxy::isLocalImage($avatar)) {
                                $fields = ['avatar' => $avatar, 'avatar-date' => DateTimeFormat::utcNow(),
@@ -2492,6 +2497,44 @@ class Contact
                return true;
        }
 
+       /**
+        * Perform a contact update if the contact is outdated
+        *
+        * @param integer $id contact id
+        * @return bool
+        */
+       public static function updateByIdIfNeeded(int $id): bool
+       {
+               $contact = self::selectFirst(['url'], ["`id` = ? AND `next-update` < ?", $id, DateTimeFormat::utcNow()]);
+               if (empty($contact['url'])) {
+                       return false;
+               }
+
+               if (self::isLocal($contact['url'])) {
+                       return true;
+               }
+
+               $stamp = (float)microtime(true);
+               self::updateFromProbe($id);
+               Logger::debug('Contact data is updated.', ['duration' => round((float)microtime(true) - $stamp, 3), 'id' => $id, 'url' => $contact['url'], 'callstack' => System::callstack(20)]);
+               return true;
+       }
+
+       /**
+        * Perform a contact update if the contact is outdated
+        *
+        * @param string $url contact url
+        * @return bool
+        */
+       public static function updateByUrlIfNeeded(string $url): bool
+       {
+               $id = self::getIdForURL($url, 0, false);
+               if (!empty($id)) {
+                       return self::updateByIdIfNeeded($id);
+               }
+               return (bool)self::getIdForURL($url);
+       }
+
        /**
         * Updates contact record by provided id and optional network
         *
@@ -2513,14 +2556,18 @@ class Contact
                if ($data['network'] == Protocol::DIASPORA) {
                        try {
                                DI::dsprContact()->updateFromProbeArray($data);
+                       } catch (HTTPException\NotFoundException $e) {
+                               Logger::notice($e->getMessage(), ['id' => $id, 'network' => $network, 'contact' => $contact, 'data' => $data]);
                        } catch (\InvalidArgumentException $e) {
-                               Logger::error($e->getMessage(), ['id' => $id, 'network' => $network, 'contact' => $contact, 'data' => $data]);
+                               Logger::notice($e->getMessage(), ['id' => $id, 'network' => $network, 'contact' => $contact, 'data' => $data]);
                        }
                } elseif (!empty($data['networks'][Protocol::DIASPORA])) {
                        try {
                                DI::dsprContact()->updateFromProbeArray($data['networks'][Protocol::DIASPORA]);
+                       } catch (HTTPException\NotFoundException $e) {
+                               Logger::notice($e->getMessage(), ['id' => $id, 'network' => $network, 'contact' => $contact, 'data' => $data]);
                        } catch (\InvalidArgumentException $e) {
-                               Logger::error($e->getMessage(), ['id' => $id, 'network' => $network, 'contact' => $contact, 'data' => $data]);
+                               Logger::notice($e->getMessage(), ['id' => $id, 'network' => $network, 'contact' => $contact, 'data' => $data]);
                        }
                }
 
@@ -2918,7 +2965,7 @@ class Contact
                }
 
                if (($network != '') && ($ret['network'] != $network)) {
-                       Logger::notice('Expected network ' . $network . ' does not match actual network ' . $ret['network']);
+                       $result['message'] = DI::l10n()->t('Expected network %s does not match actual network %s', $network, $ret['network']);
                        return $result;
                }
 
@@ -3043,7 +3090,11 @@ class Contact
                if ($probed) {
                        self::updateFromProbeArray($contact_id, $ret);
                } else {
-                       Worker::add(Worker::PRIORITY_HIGH, 'UpdateContact', $contact_id);
+                       try {
+                               UpdateContact::add(Worker::PRIORITY_HIGH, $contact['id']);
+                       } catch (\InvalidArgumentException $e) {
+                               Logger::notice($e->getMessage(), ['contact' => $contact]);
+                       }
                }
 
                $result['success'] = Protocol::follow($uid, $contact, $protocol);
@@ -3514,8 +3565,12 @@ class Contact
                                Worker::add(Worker::PRIORITY_LOW, 'AddContact', 0, $url);
                                ++$added;
                        } elseif (!empty($contact['network']) && Probe::isProbable($contact['network']) && ($contact['next-update'] < DateTimeFormat::utcNow())) {
-                               Worker::add(['priority' => Worker::PRIORITY_LOW, 'dont_fork' => true], 'UpdateContact', $contact['id']);
-                               ++$updated;
+                               try {
+                                       UpdateContact::add(['priority' => Worker::PRIORITY_LOW, 'dont_fork' => true], $contact['id']);
+                                       ++$updated;
+                               } catch (\InvalidArgumentException $e) {
+                                       Logger::notice($e->getMessage(), ['contact' => $contact]);
+                               }
                        } else {
                                ++$unchanged;
                        }