]> git.mxchange.org Git - friendica.git/commitdiff
Move follow revoke protocol to worker task
authorHypolite Petovan <hypolite@mrpetovan.com>
Sun, 17 Oct 2021 01:09:49 +0000 (21:09 -0400)
committerHypolite Petovan <hypolite@mrpetovan.com>
Thu, 24 Feb 2022 14:35:55 +0000 (09:35 -0500)
- Switch to public contact for revoke_follow hook

doc/Addons.md
src/Core/Protocol.php
src/Model/Contact.php
src/Module/Contact/Revoke.php
src/Worker/Contact/RevokeFollow.php [new file with mode: 0644]

index 504ab43984b5231317db48b0e64901a3b1c600af..7c9b89d3424efe4e4495aa743bcdcb4d5acbe767 100644 (file)
@@ -635,7 +635,8 @@ Hook data:
 Called when making a remote contact on a non-native network (like Twitter) unfollow you.
 
 Hook data:
-- **contact** (input): the remote contact (uid = local revoking user id) array.
+- **contact** (input): the target public contact (uid = 0) array.
+- **uid** (input): the id of the source local user.
 - **result** (output): a boolean value indicating wether the operation was successful or not.
 
 ### block
index 47d4b4539f2311cbbddded8ea4de73db9660b278..c141bbc54e6240f2c79372050468e458ab34964c 100644 (file)
@@ -22,7 +22,6 @@
 namespace Friendica\Core;
 
 use Friendica\Database\DBA;
-use Friendica\DI;
 use Friendica\Model\User;
 use Friendica\Network\HTTPException;
 use Friendica\Protocol\Activity;
@@ -227,12 +226,13 @@ class Protocol
        /**
         * Revoke an incoming follow from the provided contact
         *
-        * @param array $contact Private contact (uid != 0) array
+        * @param array $contact Target public contact (uid == 0) array
+        * @param int   $uid     Source local user id
         * @return bool|null true if successful, false if not, null if no action was performed
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function revokeFollow(array $contact): ?bool
+       public static function revokeFollow(array $contact, int $uid): ?bool
        {
                if (empty($contact['network'])) {
                        throw new \InvalidArgumentException('Missing network key in contact array');
@@ -244,13 +244,14 @@ class Protocol
                }
 
                if ($protocol == Protocol::ACTIVITYPUB) {
-                       return ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $contact['uid']);
+                       return ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $uid);
                }
 
                // Catch-all hook for connector addons
                $hook_data = [
                        'contact' => $contact,
-                       'result' => null,
+                       'uid'     => $uid,
+                       'result'  => null,
                ];
                Hook::callAll('revoke_follow', $hook_data);
 
index 99b1c0971f67dd31e3a282d5726a59cc4620a160..f0be45772246457eaa122e204d735fc2e0c70d70 100644 (file)
@@ -840,12 +840,13 @@ class Contact
        /**
         * Revoke follow privileges of the remote user contact
         *
-        * @param array   $contact  Contact unfriended
-        * @return bool|null Whether the remote operation is successful or null if no remote operation was performed
+        * The local relationship is updated immediately, the eventual remote server is messaged in the background.
+        *
+        * @param array $contact User-specific contact array (uid != 0) to revoke the follow from
         * @throws HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function revokeFollow(array $contact): ?bool
+       public static function revokeFollow(array $contact): void
        {
                if (empty($contact['network'])) {
                        throw new \InvalidArgumentException('Empty network in contact array');
@@ -855,19 +856,12 @@ class Contact
                        throw new \InvalidArgumentException('Unexpected public contact record');
                }
 
-               $result = Protocol::revokeFollow($contact);
-
-               // 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']);
-                       }
+               if (in_array($contact['rel'], [self::FOLLOWER, self::FRIEND])) {
+                       $cdata = Contact::getPublicAndUserContactID($contact['id'], $contact['uid']);
+                       Worker::add(PRIORITY_HIGH, 'Contact\RevokeFollow', $cdata['public'], $contact['uid']);
                }
 
-               return $result;
+               self::removeFollower($contact);
        }
 
        /**
@@ -2754,6 +2748,13 @@ class Contact
                return null;
        }
 
+       /**
+        * Update the local relationship when a local user loses a follower
+        *
+        * @param array $contact User-specific contact (uid != 0) array
+        * @throws HTTPException\InternalServerErrorException
+        * @throws \ImagickException
+        */
        public static function removeFollower(array $contact)
        {
                if (in_array($contact['rel'] ?? [], [self::FRIEND, self::SHARING])) {
index 59b3bdafae803af66ca07bf072022dc5cf90a373..35cb48149543686a6d1a046ee2926009fea18d66 100644 (file)
@@ -38,7 +38,10 @@ use Psr\Log\LoggerInterface;
 
 class Revoke extends BaseModule
 {
-       /** @var array */
+       /**
+        * User-specific contact (uid != 0) array
+        * @var array
+        */
        protected $contact;
        
        /** @var Database */
@@ -82,14 +85,9 @@ class Revoke extends BaseModule
 
                self::checkFormSecurityTokenRedirectOnError('contact/' . $this->parameters['id'], 'contact_revoke');
 
-               $result = Model\Contact::revokeFollow($this->contact);
-               if ($result === true) {
-                       notice($this->t('Follow was successfully revoked.'));
-               } elseif ($result === null) {
-                       notice($this->t('Follow was successfully revoked, however the remote contact won\'t be aware of this revokation.'));
-               } else {
-                       notice($this->t('Unable to revoke follow, please try again later or contact the administrator.'));
-               }
+               Model\Contact::revokeFollow($this->contact);
+
+               notice($this->t('Follow was successfully revoked.'));
 
                $this->baseUrl->redirect('contact/' . $this->parameters['id']);
        }
diff --git a/src/Worker/Contact/RevokeFollow.php b/src/Worker/Contact/RevokeFollow.php
new file mode 100644 (file)
index 0000000..726a69b
--- /dev/null
@@ -0,0 +1,51 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2022, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Worker\Contact;
+
+use Friendica\Core\Protocol;
+use Friendica\Core\Worker;
+use Friendica\Model\Contact;
+
+class RevokeFollow
+{
+       /**
+        * Issue asynchronous follow revokation message to remote servers.
+        * The local relationship has already been updated, so we can't use the user-specific contact
+        *
+        * @param int $cid Target public contact id
+        * @param int $uid Source local user id
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
+        */
+       public static function execute(int $cid, int $uid)
+       {
+               $contact = Contact::getById($cid);
+               if (empty($contact)) {
+                       return;
+               }
+
+               $result = Protocol::revokeFollow($contact, $uid);
+               if ($result === false) {
+                       Worker::defer();
+               }
+       }
+}