2019-05-11 20:27:21 +09:00
|
|
|
<?php
|
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social 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.
|
|
|
|
//
|
|
|
|
// GNU social 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 GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ActivityPub implementation for GNU social
|
|
|
|
*
|
|
|
|
* @package GNUsocial
|
2020-10-16 09:07:01 +09:00
|
|
|
*
|
2019-05-11 20:27:21 +09:00
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
* @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org
|
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
2020-10-16 09:07:01 +09:00
|
|
|
*
|
|
|
|
* @see http://www.gnu.org/software/social/
|
2019-05-11 20:27:21 +09:00
|
|
|
*/
|
|
|
|
defined('GNUSOCIAL') || die();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ActivityPub's own Postman
|
|
|
|
*
|
|
|
|
* Standard workflow expects that we send an Explorer to find out destinataries'
|
|
|
|
* inbox address. Then we send our postman to deliver whatever we want to send them.
|
|
|
|
*
|
|
|
|
* @category Plugin
|
|
|
|
* @package GNUsocial
|
2020-10-16 09:07:01 +09:00
|
|
|
*
|
2019-05-11 20:27:21 +09:00
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
|
|
|
*/
|
|
|
|
class Activitypub_postman
|
|
|
|
{
|
|
|
|
private $actor;
|
|
|
|
private $actor_uri;
|
|
|
|
private $to = [];
|
2020-08-30 00:09:10 +09:00
|
|
|
private $failed_to = [];
|
2019-05-11 20:27:21 +09:00
|
|
|
private $client;
|
|
|
|
private $headers;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a postman to deliver something to someone
|
|
|
|
*
|
2019-08-20 07:33:18 +09:00
|
|
|
* @param Profile $from sender Profile
|
2020-10-16 09:07:01 +09:00
|
|
|
* @param array $to receiver Profiles
|
|
|
|
*
|
2019-05-11 20:27:21 +09:00
|
|
|
* @throws Exception
|
2020-10-16 09:07:01 +09:00
|
|
|
*
|
2019-05-11 20:27:21 +09:00
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
*/
|
2019-08-27 23:37:01 +09:00
|
|
|
public function __construct(Profile $from, array $to = [])
|
2019-05-11 20:27:21 +09:00
|
|
|
{
|
|
|
|
$this->actor = $from;
|
2020-10-16 09:07:01 +09:00
|
|
|
$this->to = $to;
|
2019-05-11 20:27:21 +09:00
|
|
|
|
2019-12-11 07:27:32 +09:00
|
|
|
$this->actor_uri = $this->actor->getUri();
|
2019-05-11 20:27:21 +09:00
|
|
|
|
|
|
|
$this->client = new HTTPClient();
|
|
|
|
}
|
|
|
|
|
2020-08-30 00:09:10 +09:00
|
|
|
/**
|
|
|
|
* When dear postman dies, resurrect him until he finishes what he couldn't in life
|
|
|
|
*
|
|
|
|
* @throws ServerException
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
*/
|
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
$qm = QueueManager::get();
|
|
|
|
foreach($this->failed_to as $to => $activity) {
|
|
|
|
$qm->enqueue([$to, $activity], 'activitypub_failed');
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-11 20:27:21 +09:00
|
|
|
/**
|
|
|
|
* Send something to remote instance
|
|
|
|
*
|
2020-10-16 09:07:01 +09:00
|
|
|
* @param string $data request body
|
|
|
|
* @param string $inbox url of remote inbox
|
2019-05-11 20:27:21 +09:00
|
|
|
* @param string $method request method
|
2020-10-16 09:07:01 +09:00
|
|
|
*
|
2019-05-11 20:27:21 +09:00
|
|
|
* @throws HTTP_Request2_Exception
|
2020-10-16 09:07:01 +09:00
|
|
|
* @throws Exception
|
|
|
|
*
|
|
|
|
* @return GNUsocial_HTTPResponse
|
|
|
|
*
|
2019-05-11 20:27:21 +09:00
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
*/
|
|
|
|
public function send($data, $inbox, $method = 'POST')
|
|
|
|
{
|
2020-10-16 09:07:01 +09:00
|
|
|
common_debug('ActivityPub Postman: Delivering ' . $data . ' to ' . $inbox);
|
2019-05-11 20:27:21 +09:00
|
|
|
|
|
|
|
$headers = HttpSignature::sign($this->actor, $inbox, $data);
|
|
|
|
|
2020-10-16 09:07:01 +09:00
|
|
|
common_debug('ActivityPub Postman: Delivery headers were: ' . print_r($headers, true));
|
2019-05-11 20:27:21 +09:00
|
|
|
|
|
|
|
$this->client->setBody($data);
|
|
|
|
|
|
|
|
switch ($method) {
|
|
|
|
case 'POST':
|
|
|
|
$response = $this->client->post($inbox, $headers);
|
|
|
|
break;
|
|
|
|
case 'GET':
|
|
|
|
$response = $this->client->get($inbox, $headers);
|
|
|
|
break;
|
|
|
|
default:
|
2020-10-16 09:07:01 +09:00
|
|
|
throw new Exception('Unsupported request method for postman.');
|
2019-05-11 20:27:21 +09:00
|
|
|
}
|
|
|
|
|
2020-10-16 09:07:01 +09:00
|
|
|
common_debug('ActivityPub Postman: Delivery result with status code ' . $response->getStatus() . ': ' . $response->getBody());
|
2019-05-11 20:27:21 +09:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a follow notification to remote instance
|
|
|
|
*
|
|
|
|
* @throws HTTP_Request2_Exception
|
|
|
|
* @throws Exception
|
2020-10-16 09:07:01 +09:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*
|
2019-05-11 20:27:21 +09:00
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
*/
|
|
|
|
public function follow()
|
|
|
|
{
|
2020-10-16 09:07:01 +09:00
|
|
|
$data = Activitypub_follow::follow_to_array($this->actor_uri, $this->to[0]->getUrl());
|
|
|
|
$res = $this->send(json_encode($data, JSON_UNESCAPED_SLASHES), $this->to[0]->get_inbox());
|
2019-11-01 09:12:45 +09:00
|
|
|
$res_body = json_decode($res->getBody(), true);
|
2019-05-11 20:27:21 +09:00
|
|
|
|
|
|
|
if ($res->getStatus() == 200 || $res->getStatus() == 202 || $res->getStatus() == 409) {
|
|
|
|
$pending_list = new Activitypub_pending_follow_requests($this->actor->getID(), $this->to[0]->getID());
|
|
|
|
$pending_list->add();
|
|
|
|
return true;
|
2019-11-01 09:12:45 +09:00
|
|
|
} elseif (isset($res_body['error'])) {
|
|
|
|
throw new Exception($res_body['error']);
|
2019-05-11 20:27:21 +09:00
|
|
|
}
|
|
|
|
|
2020-10-16 09:07:01 +09:00
|
|
|
throw new Exception('An unknown error occurred.');
|
2019-05-11 20:27:21 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a Undo Follow notification to remote instance
|
|
|
|
*
|
|
|
|
* @throws HTTP_Request2_Exception
|
|
|
|
* @throws Exception
|
|
|
|
* @throws Exception
|
|
|
|
* @throws Exception
|
|
|
|
* @throws Exception
|
2020-10-16 09:07:01 +09:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*
|
2019-05-11 20:27:21 +09:00
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
*/
|
|
|
|
public function undo_follow()
|
|
|
|
{
|
|
|
|
$data = Activitypub_undo::undo_to_array(
|
|
|
|
Activitypub_follow::follow_to_array(
|
2019-08-06 07:22:32 +09:00
|
|
|
$this->actor_uri,
|
2019-05-11 20:27:21 +09:00
|
|
|
$this->to[0]->getUrl()
|
2020-10-16 09:07:01 +09:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$res = $this->send(json_encode($data, JSON_UNESCAPED_SLASHES), $this->to[0]->get_inbox());
|
2019-11-01 09:12:45 +09:00
|
|
|
$res_body = json_decode($res->getBody(), true);
|
2019-05-11 20:27:21 +09:00
|
|
|
|
|
|
|
if ($res->getStatus() == 200 || $res->getStatus() == 202 || $res->getStatus() == 409) {
|
2019-07-09 03:23:48 +09:00
|
|
|
Activitypub_profile::unsubscribeCacheUpdate($this->actor, $this->to[0]->local_profile());
|
2019-05-11 20:27:21 +09:00
|
|
|
$pending_list = new Activitypub_pending_follow_requests($this->actor->getID(), $this->to[0]->getID());
|
|
|
|
$pending_list->remove();
|
|
|
|
return true;
|
|
|
|
}
|
2019-11-01 09:12:45 +09:00
|
|
|
if (isset($res_body['error'])) {
|
|
|
|
throw new Exception($res_body['error']);
|
2019-05-11 20:27:21 +09:00
|
|
|
}
|
2020-10-16 09:07:01 +09:00
|
|
|
throw new Exception('An unknown error occurred.');
|
2019-05-11 20:27:21 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a Accept Follow notification to remote instance
|
|
|
|
*
|
2019-08-06 07:22:32 +09:00
|
|
|
* @param string $id Follow activity id
|
2020-10-16 09:07:01 +09:00
|
|
|
*
|
2019-05-11 20:27:21 +09:00
|
|
|
* @throws HTTP_Request2_Exception
|
2020-10-16 09:07:01 +09:00
|
|
|
* @throws Exception Description of HTTP Response error or generic error message.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*
|
2019-05-11 20:27:21 +09:00
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
*/
|
2019-08-06 07:22:32 +09:00
|
|
|
public function accept_follow(string $id): bool
|
2019-05-11 20:27:21 +09:00
|
|
|
{
|
|
|
|
$data = Activitypub_accept::accept_to_array(
|
|
|
|
Activitypub_follow::follow_to_array(
|
|
|
|
$this->to[0]->getUrl(),
|
2019-08-06 07:22:32 +09:00
|
|
|
$this->actor_uri,
|
|
|
|
$id
|
2020-10-16 09:07:01 +09:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$res = $this->send(json_encode($data, JSON_UNESCAPED_SLASHES), $this->to[0]->get_inbox());
|
2019-11-01 09:12:45 +09:00
|
|
|
$res_body = json_decode($res->getBody(), true);
|
2019-05-11 20:27:21 +09:00
|
|
|
|
|
|
|
if ($res->getStatus() == 200 || $res->getStatus() == 202 || $res->getStatus() == 409) {
|
2019-07-09 03:23:48 +09:00
|
|
|
$pending_list = new Activitypub_pending_follow_requests($this->to[0]->getID(), $this->actor->getID());
|
2019-05-11 20:27:21 +09:00
|
|
|
$pending_list->remove();
|
|
|
|
return true;
|
|
|
|
}
|
2019-11-01 09:12:45 +09:00
|
|
|
if (isset($res_body['error'])) {
|
|
|
|
throw new Exception($res_body['error']);
|
2019-05-11 20:27:21 +09:00
|
|
|
}
|
2020-10-16 09:07:01 +09:00
|
|
|
throw new Exception('An unknown error occurred.');
|
2019-05-11 20:27:21 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a Like notification to remote instances holding the notice
|
|
|
|
*
|
|
|
|
* @param Notice $notice
|
2020-10-16 09:07:01 +09:00
|
|
|
*
|
2019-05-11 20:27:21 +09:00
|
|
|
* @throws HTTP_Request2_Exception
|
|
|
|
* @throws InvalidUrlException
|
|
|
|
* @throws Exception
|
2020-10-16 09:07:01 +09:00
|
|
|
*
|
2019-05-11 20:27:21 +09:00
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
*/
|
2020-08-28 09:12:40 +09:00
|
|
|
public function like(Notice $notice): void
|
2019-05-11 20:27:21 +09:00
|
|
|
{
|
|
|
|
$data = Activitypub_like::like_to_array(
|
2019-08-06 07:22:32 +09:00
|
|
|
$this->actor_uri,
|
2020-08-28 09:12:40 +09:00
|
|
|
$notice
|
|
|
|
);
|
2019-05-11 20:27:21 +09:00
|
|
|
$data = json_encode($data, JSON_UNESCAPED_SLASHES);
|
|
|
|
|
|
|
|
foreach ($this->to_inbox() as $inbox) {
|
2021-07-17 02:03:12 +09:00
|
|
|
try {
|
|
|
|
$res = $this->send($data, $inbox);
|
|
|
|
// accumulate errors for later use, if needed
|
|
|
|
if (!($res->getStatus() == 200 || $res->getStatus() == 202 || $res->getStatus() == 409)) {
|
|
|
|
$res_body = json_decode($res->getBody(), true);
|
|
|
|
$errors[] = isset($res_body['error']) ?
|
|
|
|
$res_body['error'] : "An unknown error occurred.";
|
|
|
|
$to_failed[$inbox] = $notice;
|
|
|
|
}
|
|
|
|
} catch (Exception $e){
|
2020-08-30 00:09:10 +09:00
|
|
|
$to_failed[$inbox] = $notice;
|
2019-07-19 03:18:23 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($errors)) {
|
2020-10-16 09:07:01 +09:00
|
|
|
common_log(LOG_ERR, sizeof($errors) . ' instance/s failed to handle the like activity!');
|
2019-05-11 20:27:21 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a Undo Like notification to remote instances holding the notice
|
|
|
|
*
|
|
|
|
* @param Notice $notice
|
2020-10-16 09:07:01 +09:00
|
|
|
*
|
2019-05-11 20:27:21 +09:00
|
|
|
* @throws HTTP_Request2_Exception
|
|
|
|
* @throws InvalidUrlException
|
|
|
|
* @throws Exception
|
2020-10-16 09:07:01 +09:00
|
|
|
*
|
2019-05-11 20:27:21 +09:00
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
*/
|
|
|
|
public function undo_like($notice)
|
|
|
|
{
|
|
|
|
$data = Activitypub_undo::undo_to_array(
|
|
|
|
Activitypub_like::like_to_array(
|
2019-08-06 07:22:32 +09:00
|
|
|
$this->actor_uri,
|
2020-08-28 09:12:40 +09:00
|
|
|
$notice
|
|
|
|
)
|
|
|
|
);
|
2019-05-11 20:27:21 +09:00
|
|
|
$data = json_encode($data, JSON_UNESCAPED_SLASHES);
|
|
|
|
|
|
|
|
foreach ($this->to_inbox() as $inbox) {
|
2021-07-17 02:03:12 +09:00
|
|
|
try {
|
|
|
|
$res = $this->send($data, $inbox);
|
|
|
|
|
|
|
|
// accummulate errors for later use, if needed
|
|
|
|
if (!($res->getStatus() == 200 || $res->getStatus() == 202 || $res->getStatus() == 409)) {
|
|
|
|
$res_body = json_decode($res->getBody(), true);
|
|
|
|
$errors[] = isset($res_body['error']) ?
|
|
|
|
$res_body['error'] : "An unknown error occurred.";
|
|
|
|
$to_failed[$inbox] = $notice;
|
|
|
|
}
|
|
|
|
} catch (Exception $e) {
|
2020-08-30 00:09:10 +09:00
|
|
|
$to_failed[$inbox] = $notice;
|
2019-07-19 03:18:23 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($errors)) {
|
2020-10-16 09:07:01 +09:00
|
|
|
common_log(LOG_ERR, sizeof($errors) . ' instance/s failed to handle the undo-like activity!');
|
2019-05-11 20:27:21 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a Create notification to remote instances
|
|
|
|
*
|
|
|
|
* @param Notice $notice
|
2020-10-16 09:07:01 +09:00
|
|
|
*
|
2019-05-11 20:27:21 +09:00
|
|
|
* @throws EmptyPkeyValueException
|
|
|
|
* @throws HTTP_Request2_Exception
|
|
|
|
* @throws InvalidUrlException
|
|
|
|
* @throws ServerException
|
2020-10-16 09:07:01 +09:00
|
|
|
*
|
2019-05-11 20:27:21 +09:00
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
*/
|
|
|
|
public function create_note($notice)
|
|
|
|
{
|
|
|
|
$data = Activitypub_create::create_to_array(
|
|
|
|
$this->actor_uri,
|
2020-08-30 07:51:46 +09:00
|
|
|
common_local_url('apNotice', ['id' => $notice->getID()]),
|
2019-05-11 20:27:21 +09:00
|
|
|
Activitypub_notice::notice_to_array($notice)
|
2020-08-28 09:12:40 +09:00
|
|
|
);
|
2019-05-11 20:27:21 +09:00
|
|
|
$data = json_encode($data, JSON_UNESCAPED_SLASHES);
|
|
|
|
|
|
|
|
foreach ($this->to_inbox() as $inbox) {
|
2021-07-17 02:03:12 +09:00
|
|
|
try {
|
|
|
|
$res = $this->send($data, $inbox);
|
|
|
|
|
|
|
|
// accummulate errors for later use, if needed
|
|
|
|
if (!($res->getStatus() == 200 || $res->getStatus() == 202 || $res->getStatus() == 409)) {
|
|
|
|
$res_body = json_decode($res->getBody(), true);
|
|
|
|
$errors[] = isset($res_body['error']) ?
|
|
|
|
$res_body['error'] : "An unknown error occurred.";
|
|
|
|
$to_failed[$inbox] = $notice;
|
|
|
|
}
|
|
|
|
} catch (Exception $e) {
|
2020-08-30 00:09:10 +09:00
|
|
|
$to_failed[$inbox] = $notice;
|
2019-07-19 03:18:23 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($errors)) {
|
2020-10-16 09:07:01 +09:00
|
|
|
common_log(LOG_ERR, sizeof($errors) . ' instance/s failed to handle the create-note activity!');
|
2019-05-11 20:27:21 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 07:33:18 +09:00
|
|
|
/**
|
|
|
|
* Send a Create direct-notification to remote instances
|
|
|
|
*
|
|
|
|
* @param Notice $message
|
2020-10-16 09:07:01 +09:00
|
|
|
*
|
2019-08-20 07:33:18 +09:00
|
|
|
* @author Bruno Casteleiro <brunoccast@fc.up.pt>
|
|
|
|
*/
|
|
|
|
public function create_direct_note(Notice $message)
|
|
|
|
{
|
|
|
|
$data = Activitypub_create::create_to_array(
|
|
|
|
$this->actor_uri,
|
2020-08-30 07:51:46 +09:00
|
|
|
common_local_url('apNotice', ['id' => $message->getID()]),
|
2019-08-20 07:33:18 +09:00
|
|
|
Activitypub_message::message_to_array($message),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
$data = json_encode($data, JSON_UNESCAPED_SLASHES);
|
|
|
|
|
2019-08-20 09:08:02 +09:00
|
|
|
foreach ($this->to_inbox(false) as $inbox) {
|
2019-08-20 07:33:18 +09:00
|
|
|
$res = $this->send($data, $inbox);
|
|
|
|
|
|
|
|
// accummulate errors for later use, if needed
|
|
|
|
if (!($res->getStatus() == 200 || $res->getStatus() == 202 || $res->getStatus() == 409)) {
|
|
|
|
$res_body = json_decode($res->getBody(), true);
|
2019-11-01 09:12:45 +09:00
|
|
|
$errors[] = isset($res_body['error']) ?
|
2020-10-16 09:07:01 +09:00
|
|
|
$res_body['error'] : 'An unknown error occurred.';
|
2019-08-20 07:33:18 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($errors)) {
|
2020-10-16 09:07:01 +09:00
|
|
|
common_log(LOG_ERR, sizeof($errors) . ' instance/s failed to handle the create-note activity!');
|
2019-08-20 07:33:18 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-11 20:27:21 +09:00
|
|
|
/**
|
|
|
|
* Send a Announce notification to remote instances
|
|
|
|
*
|
|
|
|
* @param Notice $notice
|
2020-10-16 09:07:01 +09:00
|
|
|
*
|
2019-05-11 20:27:21 +09:00
|
|
|
* @throws HTTP_Request2_Exception
|
2020-10-16 09:07:01 +09:00
|
|
|
* @throws Exception
|
|
|
|
*
|
2019-05-11 20:27:21 +09:00
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
*/
|
2020-08-28 09:12:40 +09:00
|
|
|
public function announce(Notice $notice, Notice $repeat_of): void
|
2019-05-11 20:27:21 +09:00
|
|
|
{
|
2020-08-28 09:12:40 +09:00
|
|
|
$data = json_encode(
|
|
|
|
Activitypub_announce::announce_to_array($this->actor, $notice, $repeat_of),
|
|
|
|
JSON_UNESCAPED_SLASHES
|
|
|
|
);
|
2019-05-11 20:27:21 +09:00
|
|
|
|
|
|
|
foreach ($this->to_inbox() as $inbox) {
|
2021-07-17 02:03:12 +09:00
|
|
|
try {
|
|
|
|
$res = $this->send($data, $inbox);
|
|
|
|
|
|
|
|
// accummulate errors for later use, if needed
|
|
|
|
if (!($res->getStatus() == 200 || $res->getStatus() == 202 || $res->getStatus() == 409)) {
|
|
|
|
$res_body = json_decode($res->getBody(), true);
|
|
|
|
$errors[] = isset($res_body['error']) ?
|
|
|
|
$res_body['error'] : "An unknown error occurred.";
|
|
|
|
$to_failed[$inbox] = $notice;
|
|
|
|
}
|
|
|
|
} catch (Exception $e) {
|
2020-08-30 00:09:10 +09:00
|
|
|
$to_failed[$inbox] = $notice;
|
2019-07-19 03:18:23 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($errors)) {
|
2020-10-16 09:07:01 +09:00
|
|
|
common_log(LOG_ERR, sizeof($errors) . ' instance/s failed to handle the announce activity!');
|
2019-05-11 20:27:21 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a Delete notification to remote instances holding the notice
|
|
|
|
*
|
|
|
|
* @param Notice $notice
|
2020-10-16 09:07:01 +09:00
|
|
|
*
|
2019-05-11 20:27:21 +09:00
|
|
|
* @throws HTTP_Request2_Exception
|
|
|
|
* @throws InvalidUrlException
|
|
|
|
* @throws Exception
|
2020-10-16 09:07:01 +09:00
|
|
|
*
|
2019-05-11 20:27:21 +09:00
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
*/
|
2019-08-27 23:37:01 +09:00
|
|
|
public function delete_note($notice)
|
2019-05-11 20:27:21 +09:00
|
|
|
{
|
2020-08-29 19:12:02 +09:00
|
|
|
$data = Activitypub_delete::delete_to_array($notice);
|
2019-05-11 20:27:21 +09:00
|
|
|
$errors = [];
|
2020-10-16 09:07:01 +09:00
|
|
|
$data = json_encode($data, JSON_UNESCAPED_SLASHES);
|
2019-05-11 20:27:21 +09:00
|
|
|
foreach ($this->to_inbox() as $inbox) {
|
2021-07-17 02:03:12 +09:00
|
|
|
try {
|
|
|
|
$res = $this->send($data, $inbox);
|
|
|
|
if (!($res->getStatus() == 200 || $res->getStatus() == 202 || $res->getStatus() == 409)) {
|
|
|
|
$res_body = json_decode($res->getBody(), true);
|
|
|
|
$errors[] = isset($res_body['error']) ?
|
|
|
|
$res_body['error'] : "An unknown error occurred.";
|
|
|
|
$to_failed[$inbox] = $notice;
|
|
|
|
}
|
|
|
|
} catch (Exception $e) {
|
2020-08-30 00:09:10 +09:00
|
|
|
$to_failed[$inbox] = $notice;
|
2019-05-11 20:27:21 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!empty($errors)) {
|
|
|
|
throw new Exception(json_encode($errors));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 23:37:01 +09:00
|
|
|
/**
|
|
|
|
* Send a Delete notification to remote followers of some deleted profile
|
|
|
|
*
|
2020-10-16 09:07:01 +09:00
|
|
|
* @param Notice $notice
|
|
|
|
*
|
2019-08-27 23:37:01 +09:00
|
|
|
* @throws HTTP_Request2_Exception
|
2020-10-16 09:07:01 +09:00
|
|
|
* @throws InvalidUrlException
|
|
|
|
* @throws Exception
|
|
|
|
*
|
2019-08-27 23:37:01 +09:00
|
|
|
* @author Bruno Casteleiro <brunoccast@fc.up.pt>
|
|
|
|
*/
|
2021-02-21 23:59:36 +09:00
|
|
|
public function delete_profile(Profile $deleted_profile)
|
2019-08-27 23:37:01 +09:00
|
|
|
{
|
2021-02-21 23:59:36 +09:00
|
|
|
$data = Activitypub_delete::delete_to_array($deleted_profile);
|
2019-08-27 23:37:01 +09:00
|
|
|
$data = json_encode($data, JSON_UNESCAPED_SLASHES);
|
|
|
|
|
|
|
|
$errors = [];
|
|
|
|
foreach ($this->to_inbox() as $inbox) {
|
2021-07-17 02:03:12 +09:00
|
|
|
try {
|
|
|
|
$res = $this->send($data, $inbox);
|
|
|
|
|
|
|
|
// accumulate errors for later use, if needed
|
|
|
|
if (!($res->getStatus() == 200 || $res->getStatus() == 202 || $res->getStatus() == 409)) {
|
|
|
|
$res_body = json_decode($res->getBody(), true);
|
|
|
|
$errors[] = isset($res_body['error']) ?
|
|
|
|
$res_body['error'] : "An unknown error occurred.";
|
|
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$to_failed[$inbox] = $notice;
|
2019-08-27 23:37:01 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($errors)) {
|
2020-10-16 09:07:01 +09:00
|
|
|
common_log(LOG_ERR, sizeof($errors) . ' instance/s failed to handle the delete_profile activity!');
|
2019-08-27 23:37:01 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-11 20:27:21 +09:00
|
|
|
/**
|
|
|
|
* Clean list of inboxes to deliver messages
|
|
|
|
*
|
2019-08-20 09:08:02 +09:00
|
|
|
* @param bool $actorFollowers whether to include the actor's follower collection
|
2020-10-16 09:07:01 +09:00
|
|
|
*
|
2019-12-11 09:15:27 +09:00
|
|
|
* @throws Exception
|
2020-10-16 09:07:01 +09:00
|
|
|
*
|
|
|
|
* @return array To Inbox URLs
|
|
|
|
*
|
2019-12-11 09:15:27 +09:00
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
2019-05-11 20:27:21 +09:00
|
|
|
*/
|
2019-08-20 09:08:02 +09:00
|
|
|
private function to_inbox(bool $actorFollowers = true): array
|
2019-05-11 20:27:21 +09:00
|
|
|
{
|
2019-08-20 09:08:02 +09:00
|
|
|
if ($actorFollowers) {
|
|
|
|
$discovery = new Activitypub_explorer();
|
|
|
|
$followers = apActorFollowersAction::generate_followers($this->actor, 0, null);
|
|
|
|
foreach ($followers as $sub) {
|
|
|
|
try {
|
2020-10-16 09:07:01 +09:00
|
|
|
$this->to[] = Activitypub_profile::from_profile($discovery->lookup($sub)[0]);
|
2019-08-20 09:08:02 +09:00
|
|
|
} catch (Exception $e) {
|
|
|
|
// Not an ActivityPub Remote Follower, let it go
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unset($discovery);
|
|
|
|
}
|
|
|
|
|
2019-05-11 20:27:21 +09:00
|
|
|
$to_inboxes = [];
|
|
|
|
foreach ($this->to as $to_profile) {
|
|
|
|
$i = $to_profile->get_inbox();
|
|
|
|
// Prevent delivering to self
|
2019-12-11 09:15:27 +09:00
|
|
|
if ($i == common_local_url('apInbox')) {
|
2019-05-11 20:27:21 +09:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$to_inboxes[] = $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_unique($to_inboxes);
|
|
|
|
}
|
|
|
|
}
|