3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Module\DFRN;
25 use Friendica\BaseModule;
26 use Friendica\Core\L10n;
27 use Friendica\Core\Logger;
28 use Friendica\Core\System;
29 use Friendica\Database\Database;
31 use Friendica\Model\Contact;
32 use Friendica\Model\Conversation;
33 use Friendica\Model\User;
34 use Friendica\Module\OStatus\Salmon;
35 use Friendica\Module\Response;
36 use Friendica\Protocol\DFRN;
37 use Friendica\Protocol\Diaspora;
38 use Friendica\Util\Network;
39 use Friendica\Network\HTTPException;
40 use Friendica\Util\Profiler;
41 use Psr\Log\LoggerInterface;
46 class Notify extends BaseModule
51 public function __construct(Database $database, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
53 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
55 $this->database = $database;
58 protected function post(array $request = [])
60 $postdata = Network::postdata();
62 if (empty($postdata)) {
63 throw new HTTPException\BadRequestException();
66 $data = json_decode($postdata);
67 if (is_object($data) && !empty($this->parameters['nickname'])) {
68 $user = User::getByNickname($this->parameters['nickname']);
70 throw new \Friendica\Network\HTTPException\InternalServerErrorException();
72 $this->dispatchPrivate($user, $postdata);
74 $this->dispatchPublic($postdata);
78 private function dispatchPublic(string $postdata): bool
80 $msg = Diaspora::decodeRaw($postdata, '', true);
81 if (!is_array($msg)) {
82 // We have to fail silently to be able to hand it over to the salmon parser
83 $this->logger->warning('Diaspora::decodeRaw() has failed for some reason.', ['post-data' => $postdata]);
87 // Fetch the corresponding public contact
88 $contact_id = Contact::getIdForURL($msg['author']);
89 if (empty($contact_id)) {
90 $this->logger->notice('Contact not found', ['address' => $msg['author']]);
91 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
94 // Fetch the importer (Mixture of sender and receiver)
95 $importer = DFRN::getImporter($contact_id);
96 if (empty($importer)) {
97 $this->logger->notice('Importer contact not found', ['address' => $msg['author']]);
98 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
101 $this->logger->debug('Importing post with the public envelope.', ['transmitter' => $msg['author']]);
103 // Now we should be able to import it
104 $ret = DFRN::import($msg['message'], $importer, Conversation::PARCEL_DIASPORA_DFRN, Conversation::RELAY);
105 System::xmlExit($ret, 'Done');
110 private function dispatchPrivate(array $user, string $postdata)
112 $msg = Diaspora::decodeRaw($postdata, $user['prvkey'] ?? '');
113 if (!is_array($msg)) {
114 System::xmlExit(4, 'Unable to parse message');
118 $contact = Contact::getByURLForUser($msg['author'], $user['uid'], null, ['id', 'blocked', 'pending']);
119 if (empty($contact['id'])) {
120 $this->logger->notice('Contact not found', ['address' => $msg['author']]);
121 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
124 if ($contact['pending'] || $contact['blocked']) {
125 $this->logger->notice('Contact is blocked or pending', ['address' => $msg['author'], 'contact' => $contact]);
126 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
129 // Fetch the importer (Mixture of sender and receiver)
130 $importer = DFRN::getImporter($contact['id'], $user['uid']);
131 if (empty($importer)) {
132 $this->logger->notice('Importer contact not found for user', ['uid' => $user['uid'], 'cid' => $contact['id'], 'address' => $msg['author']]);
133 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
136 $this->logger->debug('Importing post with the private envelope.', ['transmitter' => $msg['author'], 'receiver' => $user['nickname']]);
138 // Now we should be able to import it
139 $ret = DFRN::import($msg['message'], $importer, Conversation::PARCEL_DIASPORA_DFRN, Conversation::PUSH);
140 System::xmlExit($ret, 'Done');