]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/Diaspora/Receive.php
Introduce `Response` for Modules to create a testable way for module responses
[friendica.git] / src / Module / Diaspora / Receive.php
index 01c04dfb6cfc5b6aa4c85442c0c2319a91d668f3..242b774b33a450190d9c5ae677545a0dc997675d 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2021, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
 
 namespace Friendica\Module\Diaspora;
 
+use Friendica\App;
 use Friendica\BaseModule;
-use Friendica\DI;
+use Friendica\Core\Config\Capability\IManageConfigValues;
+use Friendica\Core\L10n;
 use Friendica\Model\User;
+use Friendica\Module\Response;
 use Friendica\Network\HTTPException;
 use Friendica\Protocol\Diaspora;
 use Friendica\Util\Network;
+use Friendica\Util\Profiler;
 use Psr\Log\LoggerInterface;
 
 /**
@@ -35,37 +39,28 @@ use Psr\Log\LoggerInterface;
  */
 class Receive extends BaseModule
 {
-       /** @var LoggerInterface */
-       private static $logger;
+       /** @var IManageConfigValues */
+       protected $config;
 
-       public static function init(array $parameters = [])
+       public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IManageConfigValues $config, array $server, array $parameters = [])
        {
-               self::$logger = DI::logger();
+               parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
+
+               $this->config = $config;
        }
 
-       public static function post(array $parameters = [])
+       protected function post(array $request = [], array $post = [])
        {
-               $enabled = DI::config()->get('system', 'diaspora_enabled', false);
+               $enabled = $this->config->get('system', 'diaspora_enabled', false);
                if (!$enabled) {
-                       self::$logger->info('Diaspora disabled.');
-                       throw new HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
+                       $this->logger->info('Diaspora disabled.');
+                       throw new HTTPException\ForbiddenException($this->t('Access denied.'));
                }
 
-               $args = DI::args();
-
-               $type = $args->get(1);
-
-               switch ($type) {
-                       case 'public':
-                               self::receivePublic();
-                               break;
-                       case 'users':
-                               self::receiveUser($args->get(2));
-                               break;
-                       default:
-                               self::$logger->info('Wrong call.');
-                               throw new HTTPException\BadRequestException('wrong call.');
-                               break;
+               if ($this->parameters['type'] === 'public') {
+                       $this->receivePublic();
+               } else if ($this->parameters['type'] === 'users') {
+                       $this->receiveUser();
                }
        }
 
@@ -75,13 +70,13 @@ class Receive extends BaseModule
         * @throws HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       private static function receivePublic()
+       private  function receivePublic()
        {
-               self::$logger->info('Diaspora: Receiving post.');
+               $this->logger->info('Diaspora: Receiving post.');
 
-               $msg = self::decodePost();
+               $msg = $this->decodePost();
 
-               self::$logger->info('Diaspora: Dispatching.');
+               $this->logger->info('Diaspora: Dispatching.');
 
                Diaspora::dispatchPublic($msg);
        }
@@ -89,25 +84,25 @@ class Receive extends BaseModule
        /**
         * Receive a Diaspora posting for a user
         *
-        * @param string $guid The GUID of the importer
-        *
         * @throws HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       private static function receiveUser(string $guid)
+       private function receiveUser()
        {
-               self::$logger->info('Diaspora: Receiving post.');
+               $this->logger->info('Diaspora: Receiving post.');
 
-               $importer = User::getByGuid($guid);
+               $importer = User::getByGuid($this->parameters['guid']);
 
-               $msg = self::decodePost(false, $importer['prvkey'] ?? '');
+               $msg = $this->decodePost(false, $importer['prvkey'] ?? '');
 
-               self::$logger->info('Diaspora: Dispatching.');
+               $this->logger->info('Diaspora: Dispatching.');
 
                if (Diaspora::dispatch($importer, $msg)) {
                        throw new HTTPException\OKException();
                } else {
-                       throw new HTTPException\InternalServerErrorException();
+                       // We couldn't process the content.
+                       // To avoid the remote system trying again we send the message that we accepted the content.
+                       throw new HTTPException\AcceptedException();
                }
        }
 
@@ -121,7 +116,7 @@ class Receive extends BaseModule
         * @throws HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       private static function decodePost(bool $public = true, string $privKey = '')
+       private function decodePost(bool $public = true, string $privKey = '')
        {
                if (empty($_POST['xml'])) {
 
@@ -131,24 +126,24 @@ class Receive extends BaseModule
                                throw new HTTPException\InternalServerErrorException('Missing postdata.');
                        }
 
-                       self::$logger->info('Diaspora: Message is in the new format.');
+                       $this->logger->info('Diaspora: Message is in the new format.');
 
                        $msg = Diaspora::decodeRaw($postdata, $privKey);
                } else {
 
                        $xml = urldecode($_POST['xml']);
 
-                       self::$logger->info('Diaspora: Decode message in the old format.');
+                       $this->logger->info('Diaspora: Decode message in the old format.');
                        $msg = Diaspora::decode($xml, $privKey);
 
                        if ($public && !$msg) {
-                               self::$logger->info('Diaspora: Decode message in the new format.');
+                               $this->logger->info('Diaspora: Decode message in the new format.');
                                $msg = Diaspora::decodeRaw($xml, $privKey);
                        }
                }
 
-               self::$logger->info('Diaspora: Post decoded.');
-               self::$logger->debug('Diaspora: Decoded message.', ['msg' => print_r($msg, true)]);
+               $this->logger->info('Diaspora: Post decoded.');
+               $this->logger->debug('Diaspora: Decoded message.', ['msg' => $msg]);
 
                if (!is_array($msg)) {
                        throw new HTTPException\InternalServerErrorException('Message is not an array.');