]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Contact.php
Fix method name typos in Model\Post\UserNotification
[friendica.git] / src / Model / Contact.php
index 8c84625931b195450cd5a98845022d2cca248ac4..18e041ab9849c1f18611f925b81e0e534deef8f1 100644 (file)
@@ -133,6 +133,7 @@ class Contact
        const FOLLOWER = 1;
        const SHARING  = 2;
        const FRIEND   = 3;
+       const SELF     = 4;
        /**
         * @}
         */
@@ -221,7 +222,10 @@ class Contact
        public static function update(array $fields, array $condition, $old_fields = [])
        {
                $ret = DBA::update('contact', $fields, $condition, $old_fields);
-               // @todo Apply changes to the "user-contact" table on dedicated fields
+
+               // Apply changes to the "user-contact" table on dedicated fields
+               Contact\User::updateByContactUpdate($fields, $condition);
+
                return $ret;
        }
 
@@ -649,9 +653,9 @@ class Contact
                        'nick'        => $user['nickname'],
                        'pubkey'      => $user['pubkey'],
                        'prvkey'      => $user['prvkey'],
-                       'photo'       => DI::baseUrl() . '/photo/profile/' . $user['uid'] . '.jpg',
-                       'thumb'       => DI::baseUrl() . '/photo/avatar/'  . $user['uid'] . '.jpg',
-                       'micro'       => DI::baseUrl() . '/photo/micro/'   . $user['uid'] . '.jpg',
+                       'photo'       => User::getAvatarUrlForId($user['uid']),
+                       'thumb'       => User::getAvatarUrlForId($user['uid'], Proxy::SIZE_THUMB),
+                       'micro'       => User::getAvatarUrlForId($user['uid'], Proxy::SIZE_MICRO),
                        'blocked'     => 0,
                        'pending'     => 0,
                        'url'         => DI::baseUrl() . '/profile/' . $user['nickname'],
@@ -764,7 +768,7 @@ class Contact
                        $fields['micro'] = self::getDefaultAvatar($fields, Proxy::SIZE_MICRO);
                }
 
-               $fields['avatar'] = DI::baseUrl() . '/photo/profile/' .$uid . '.' . $file_suffix;
+               $fields['avatar'] = User::getAvatarUrlForId($uid);
                $fields['forum'] = $user['page-flags'] == User::PAGE_FLAGS_COMMUNITY;
                $fields['prv'] = $user['page-flags'] == User::PAGE_FLAGS_PRVGROUP;
                $fields['unsearchable'] = !$profile['net-publish'];
@@ -790,8 +794,11 @@ class Contact
                        self::update($fields, ['uid' => 0, 'nurl' => $self['nurl']]);
 
                        // Update the profile
-                       $fields = ['photo' => DI::baseUrl() . '/photo/profile/' .$uid . '.' . $file_suffix,
-                               'thumb' => DI::baseUrl() . '/photo/avatar/' . $uid .'.' . $file_suffix];
+                       $fields = [
+                               'photo' => User::getAvatarUrlForId($uid),
+                               'thumb' => User::getAvatarUrlForId($uid, Proxy::SIZE_THUMB)
+                       ];
+
                        DBA::update('profile', $fields, ['uid' => $uid]);
                }
 
@@ -802,7 +809,6 @@ class Contact
         * Marks a contact for removal
         *
         * @param int $id contact id
-        * @return null
         * @throws HTTPException\InternalServerErrorException
         */
        public static function remove($id)
@@ -821,52 +827,62 @@ class Contact
        }
 
        /**
-        * Sends an unfriend message. Does not remove the contact
+        * Sends an unfriend message. Removes the contact for two-way unfriending or sharing only protocols (feed an mail)
+        *
+        * @param array   $user    User unfriending
+        * @param array   $contact Contact (uid != 0) unfriended
+        * @param boolean $two_way Revoke eventual inbound follow as well
+        * @return bool|null true if successful, false if not, null if no action was performed
+        * @throws HTTPException\InternalServerErrorException
+        * @throws \ImagickException
+        */
+       public static function terminateFriendship(array $user, array $contact): bool
+       {
+               $result = Protocol::terminateFriendship($user, $contact);
+
+               if ($contact['rel'] == Contact::SHARING || in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) {
+                       self::remove($contact['id']);
+               } else {
+                       self::update(['rel' => Contact::FOLLOWER], ['id' => $contact['id']]);
+               }
+
+               return $result;
+       }
+
+       /**
+        * Revoke follow privileges of the remote user contact
         *
-        * @param array   $user     User unfriending
         * @param array   $contact  Contact unfriended
-        * @param boolean $dissolve Remove the contact on the remote side
-        * @return void
+        * @return bool|null Whether the remote operation is successful or null if no remote operation was performed
         * @throws HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function terminateFriendship(array $user, array $contact, $dissolve = false)
+       public static function revokeFollow(array $contact): bool
        {
                if (empty($contact['network'])) {
-                       return;
+                       throw new \InvalidArgumentException('Empty network in contact array');
                }
 
-               $protocol = $contact['network'];
-               if (($protocol == Protocol::DFRN) && !empty($contact['protocol'])) {
-                       $protocol = $contact['protocol'];
+               if (empty($contact['uid'])) {
+                       throw new \InvalidArgumentException('Unexpected public contact record');
                }
 
-               if (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
-                       // create an unfollow slap
-                       $item = [];
-                       $item['verb'] = Activity::O_UNFOLLOW;
-                       $item['gravity'] = GRAVITY_ACTIVITY;
-                       $item['follow'] = $contact["url"];
-                       $item['body'] = '';
-                       $item['title'] = '';
-                       $item['guid'] = '';
-                       $item['uri-id'] = 0;
-                       $slap = OStatus::salmon($item, $user);
-
-                       if (!empty($contact['notify'])) {
-                               Salmon::slapper($user, $contact['notify'], $slap);
-                       }
-               } elseif ($protocol == Protocol::DIASPORA) {
-                       Diaspora::sendUnshare($user, $contact);
-               } elseif ($protocol == Protocol::ACTIVITYPUB) {
-                       ActivityPub\Transmitter::sendContactUndo($contact['url'], $contact['id'], $user['uid']);
+               $result = Protocol::revokeFollow($contact);
 
-                       if ($dissolve) {
-                               ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $user['uid']);
+               // A null value here means the remote network doesn't support explicit follow revocation, we can still
+               // break the locally recorded relationship
+               if ($result !== false) {
+                       if ($contact['rel'] == self::FRIEND) {
+                               self::update(['rel' => self::SHARING], ['id' => $contact['id']]);
+                       } else {
+                               self::remove($contact['id']);
                        }
                }
+
+               return $result;
        }
 
+
        /**
         * Marks a contact for archival after a communication issue delay
         *
@@ -982,7 +998,6 @@ class Contact
                $pm_url = '';
                $status_link = '';
                $photos_link = '';
-               $contact_drop_link = '';
                $poke_link = '';
 
                if ($uid == 0) {
@@ -1034,13 +1049,9 @@ class Contact
 
                $posts_link = DI::baseUrl() . '/contact/' . $contact['id'] . '/conversations';
 
-               if (!$contact['self']) {
-                       $contact_drop_link = DI::baseUrl() . '/contact/' . $contact['id'] . '/drop?confirm=1';
-               }
-
                $follow_link = '';
                $unfollow_link = '';
-               if (!$contact['self'] && in_array($contact['network'], Protocol::NATIVE_SUPPORT)) {
+               if (!$contact['self'] && Protocol::supportsFollow($contact['network'])) {
                        if ($contact['uid'] && in_array($contact['rel'], [self::SHARING, self::FRIEND])) {
                                $unfollow_link = 'unfollow?url=' . urlencode($contact['url']) . '&auto=1';
                        } elseif(!$contact['pending']) {
@@ -1048,10 +1059,6 @@ class Contact
                        }
                }
 
-               if (!empty($follow_link) || !empty($unfollow_link)) {
-                       $contact_drop_link = '';
-               }
-
                /**
                 * Menu array:
                 * "name" => [ "Label", "link", (bool)Should the link opened in a new tab? ]
@@ -1071,7 +1078,6 @@ class Contact
                                '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],
-                               'drop'    => [DI::l10n()->t('Drop Contact')  , $contact_drop_link, false],
                                'pm'      => [DI::l10n()->t('Send PM')       , $pm_url           , false],
                                'poke'    => [DI::l10n()->t('Poke')          , $poke_link        , false],
                                'follow'  => [DI::l10n()->t('Connect/Follow'), $follow_link      , true],
@@ -1358,12 +1364,13 @@ class Contact
         * @param bool   $thread_mode
         * @param int    $update      Update mode
         * @param int    $parent      Item parent ID for the update mode
+        * @param bool   $only_media  Only display media content
         * @return string posts in HTML
         * @throws \Exception
         */
-       public static function getPostsFromUrl($contact_url, $thread_mode = false, $update = 0, $parent = 0)
+       public static function getPostsFromUrl($contact_url, $thread_mode = false, $update = 0, $parent = 0, bool $only_media = false)
        {
-               return self::getPostsFromId(self::getIdForURL($contact_url), $thread_mode, $update, $parent);
+               return self::getPostsFromId(self::getIdForURL($contact_url), $thread_mode, $update, $parent, $only_media);
        }
 
        /**
@@ -1372,14 +1379,13 @@ class Contact
         * @param int  $cid         Contact ID
         * @param bool $thread_mode
         * @param int  $update      Update mode
-        * @param int  $parent     Item parent ID for the update mode
+        * @param int  $parent      Item parent ID for the update mode
+        * @param bool $only_media  Only display media content
         * @return string posts in HTML
         * @throws \Exception
         */
-       public static function getPostsFromId($cid, $thread_mode = false, $update = 0, $parent = 0)
+       public static function getPostsFromId($cid, $thread_mode = false, $update = 0, $parent = 0, bool $only_media = false)
        {
-               $a = DI::app();
-
                $contact = DBA::selectFirst('contact', ['contact-type', 'network'], ['id' => $cid]);
                if (!DBA::isResult($contact)) {
                        return '';
@@ -1410,6 +1416,11 @@ class Contact
                        }
                }
 
+               if ($only_media) {
+                       $condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `post-media` WHERE `type` IN (?, ?, ?))",
+                               Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO]);
+               }
+
                if (DI::mode()->isMobile()) {
                        $itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
                                DI::config()->get('system', 'itemspage_network_mobile'));
@@ -1432,11 +1443,11 @@ class Contact
                if ($thread_mode) {
                        $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());
+                       $o .= DI::conversation()->create($items, 'contacts', $update, false, 'commented', local_user());
                } else {
                        $items = Post::toArray(Post::selectForUser(local_user(), Item::DISPLAY_FIELDLIST, $condition, $params));
 
-                       $o .= conversation($a, $items, 'contact-posts', $update);
+                       $o .= DI::conversation()->create($items, 'contact-posts', $update);
                }
 
                if (!$update) {
@@ -2694,7 +2705,7 @@ class Contact
                        // Ensure to always have the correct network type, independent from the connection request method
                        self::updateFromProbe($contact['id']);
 
-                       Post\UserNotification::insertNotication($contact['id'], Verb::getID(Activity::FOLLOW), $importer['uid']);
+                       Post\UserNotification::insertNotification($contact['id'], Verb::getID(Activity::FOLLOW), $importer['uid']);
 
                        return true;
                } else {
@@ -2725,7 +2736,7 @@ class Contact
 
                        self::updateAvatar($contact_id, $photo, true);
 
-                       Post\UserNotification::insertNotication($contact_id, Verb::getID(Activity::FOLLOW), $importer['uid']);
+                       Post\UserNotification::insertNotification($contact_id, Verb::getID(Activity::FOLLOW), $importer['uid']);
 
                        $contact_record = DBA::selectFirst('contact', ['id', 'network', 'name', 'url', 'photo'], ['id' => $contact_id]);
 
@@ -2776,12 +2787,14 @@ class Contact
                return null;
        }
 
-       public static function removeFollower($importer, $contact)
+       public static function removeFollower(array $contact)
        {
-               if (($contact['rel'] == self::FRIEND) || ($contact['rel'] == self::SHARING)) {
-                       self::update(['rel' => self::SHARING], ['id' => $contact['id']]);
-               } else {
+               if (in_array($contact['rel'] ?? [], [self::FRIEND, self::SHARING])) {
+                       DBA::update('contact', ['rel' => self::SHARING], ['id' => $contact['id']]);
+               } elseif (!empty($contact['id'])) {
                        self::remove($contact['id']);
+               } else {
+                       DI::logger()->info('Couldn\'t remove follower because of invalid contact array', ['contact' => $contact, 'callstack' => System::callstack()]);
                }
        }