2021-04-02 17:20:02 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @copyright 2021 Podlibre
|
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace ActivityPub\Controllers;
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
use ActivityPub\Config\ActivityPub;
|
|
|
|
use ActivityPub\Entities\Note;
|
2021-04-02 17:20:02 +00:00
|
|
|
use ActivityPub\Objects\OrderedCollectionObject;
|
|
|
|
use ActivityPub\Objects\OrderedCollectionPage;
|
2021-05-12 14:00:25 +00:00
|
|
|
use App\Entities\Actor;
|
2021-04-02 17:20:02 +00:00
|
|
|
use CodeIgniter\Controller;
|
2021-05-19 16:35:13 +00:00
|
|
|
use CodeIgniter\Exceptions\PageNotFoundException;
|
|
|
|
use CodeIgniter\HTTP\RedirectResponse;
|
|
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
2021-04-02 17:20:02 +00:00
|
|
|
use CodeIgniter\I18n\Time;
|
|
|
|
|
|
|
|
class ActorController extends Controller
|
|
|
|
{
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2021-04-02 17:20:02 +00:00
|
|
|
protected $helpers = ['activitypub'];
|
|
|
|
|
2021-05-18 17:16:36 +00:00
|
|
|
protected Actor $actor;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
2021-05-18 17:16:36 +00:00
|
|
|
protected ActivityPub $config;
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->config = config('ActivityPub');
|
|
|
|
}
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
public function _remap(string $method, string ...$params): mixed
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-05-06 14:00:48 +00:00
|
|
|
if (
|
|
|
|
count($params) > 0 &&
|
2021-05-19 16:35:13 +00:00
|
|
|
! ($this->actor = model('ActorModel')->getActorByUsername($params[0],))
|
2021-05-06 14:00:48 +00:00
|
|
|
) {
|
|
|
|
throw PageNotFoundException::forPageNotFound();
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
unset($params[0]);
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
return $this->{$method}(...$params);
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
public function index(): RedirectResponse
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
|
|
|
$actorObjectClass = $this->config->actorObject;
|
|
|
|
$actorObject = new $actorObjectClass($this->actor);
|
|
|
|
|
|
|
|
return $this->response
|
|
|
|
->setContentType('application/activity+json')
|
|
|
|
->setBody($actorObject->toJSON());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles incoming requests from fediverse servers
|
|
|
|
*/
|
2021-05-06 14:00:48 +00:00
|
|
|
public function inbox(): ResponseInterface
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
|
|
|
// get json body and parse it
|
|
|
|
$payload = $this->request->getJSON();
|
|
|
|
|
|
|
|
// retrieve payload actor from database or create it if it doesn't exist
|
|
|
|
$payloadActor = get_or_create_actor_from_uri($payload->actor);
|
|
|
|
|
|
|
|
// store activity to database
|
2021-05-19 16:35:13 +00:00
|
|
|
$activityId = model('ActivityModel')
|
|
|
|
->newActivity(
|
|
|
|
$payload->type,
|
|
|
|
$payloadActor->id,
|
|
|
|
$this->actor->id,
|
|
|
|
null,
|
|
|
|
json_encode($payload, JSON_THROW_ON_ERROR),
|
|
|
|
);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
// switch/case on activity type
|
2021-05-18 17:16:36 +00:00
|
|
|
/** @phpstan-ignore-next-line */
|
2021-04-02 17:20:02 +00:00
|
|
|
switch ($payload->type) {
|
|
|
|
case 'Create':
|
2021-05-19 16:35:13 +00:00
|
|
|
if ($payload->object->type === 'Note') {
|
|
|
|
if (! $payload->object->inReplyTo) {
|
|
|
|
return $this->response->setStatusCode(501)
|
|
|
|
->setJSON([]);
|
2021-05-06 14:00:48 +00:00
|
|
|
}
|
2021-05-19 16:35:13 +00:00
|
|
|
$replyToNote = model('NoteModel')
|
|
|
|
->getNoteByUri($payload->object->inReplyTo,);
|
2021-05-06 14:00:48 +00:00
|
|
|
// TODO: strip content from html to retrieve message
|
|
|
|
// remove all html tags and reconstruct message with mentions?
|
|
|
|
extract_text_from_html($payload->object->content);
|
|
|
|
$reply = new Note([
|
|
|
|
'uri' => $payload->object->id,
|
|
|
|
'actor_id' => $payloadActor->id,
|
|
|
|
'in_reply_to_id' => $replyToNote->id,
|
|
|
|
'message' => $payload->object->content,
|
2021-05-19 16:35:13 +00:00
|
|
|
'published_at' => Time::parse($payload->object->published,),
|
2021-05-06 14:00:48 +00:00
|
|
|
]);
|
2021-05-19 16:35:13 +00:00
|
|
|
$noteId = model('NoteModel')
|
|
|
|
->addReply($reply, true, false);
|
|
|
|
model('ActivityModel')
|
|
|
|
->update($activityId, [
|
|
|
|
'note_id' => service('uuid')
|
|
|
|
->fromBytes($noteId)
|
|
|
|
->getString(),
|
|
|
|
]);
|
|
|
|
return $this->response->setStatusCode(200)
|
|
|
|
->setJSON([]);
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
2021-05-06 14:00:48 +00:00
|
|
|
// return not handled undo error (501 = not implemented)
|
2021-05-19 16:35:13 +00:00
|
|
|
return $this->response->setStatusCode(501)
|
|
|
|
->setJSON([]);
|
2021-04-02 17:20:02 +00:00
|
|
|
case 'Delete':
|
2021-05-19 16:35:13 +00:00
|
|
|
$noteToDelete = model('NoteModel')
|
|
|
|
->getNoteByUri($payload->object->id,);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
model('NoteModel')
|
|
|
|
->removeNote($noteToDelete, false);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
return $this->response->setStatusCode(200)
|
|
|
|
->setJSON([]);
|
2021-04-02 17:20:02 +00:00
|
|
|
case 'Follow':
|
|
|
|
// add to followers table
|
2021-05-19 16:35:13 +00:00
|
|
|
model('FollowModel')
|
|
|
|
->addFollower($payloadActor, $this->actor, false,);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
// Automatically accept follow by returning accept activity
|
|
|
|
accept_follow($this->actor, $payloadActor, $payload->id);
|
|
|
|
|
|
|
|
// TODO: return 202 (Accepted) followed!
|
2021-05-19 16:35:13 +00:00
|
|
|
return $this->response->setStatusCode(202)
|
|
|
|
->setJSON([]);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
case 'Like':
|
|
|
|
// get favourited note
|
2021-05-19 16:35:13 +00:00
|
|
|
$note = model('NoteModel')
|
|
|
|
->getNoteByUri($payload->object);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
// Like side-effect
|
2021-05-19 16:35:13 +00:00
|
|
|
model('FavouriteModel')
|
|
|
|
->addFavourite($payloadActor, $note, false,);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
model('ActivityModel')
|
|
|
|
->update($activityId, [
|
|
|
|
'note_id' => $note->id,
|
|
|
|
]);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
return $this->response->setStatusCode(200)
|
|
|
|
->setJSON([]);
|
2021-04-02 17:20:02 +00:00
|
|
|
case 'Announce':
|
2021-05-19 16:35:13 +00:00
|
|
|
$note = model('NoteModel')
|
|
|
|
->getNoteByUri($payload->object);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
model('ActivityModel')
|
|
|
|
->update($activityId, [
|
|
|
|
'note_id' => $note->id,
|
|
|
|
]);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
model('NoteModel')
|
|
|
|
->reblog($payloadActor, $note, false);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
return $this->response->setStatusCode(200)
|
|
|
|
->setJSON([]);
|
2021-04-02 17:20:02 +00:00
|
|
|
case 'Undo':
|
|
|
|
// switch/case on the type of activity to undo
|
2021-05-18 17:16:36 +00:00
|
|
|
/** @phpstan-ignore-next-line */
|
2021-04-02 17:20:02 +00:00
|
|
|
switch ($payload->object->type) {
|
|
|
|
case 'Follow':
|
|
|
|
// revert side-effect by removing follow from database
|
2021-05-19 16:35:13 +00:00
|
|
|
model('FollowModel')
|
|
|
|
->removeFollower($payloadActor, $this->actor, false,);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
// TODO: undo has been accepted! (202 - Accepted)
|
2021-05-19 16:35:13 +00:00
|
|
|
return $this->response->setStatusCode(202)
|
|
|
|
->setJSON([]);
|
2021-04-02 17:20:02 +00:00
|
|
|
case 'Like':
|
2021-05-19 16:35:13 +00:00
|
|
|
$note = model('NoteModel')
|
|
|
|
->getNoteByUri($payload->object->object,);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
// revert side-effect by removing favourite from database
|
2021-05-19 16:35:13 +00:00
|
|
|
model('FavouriteModel')
|
|
|
|
->removeFavourite($payloadActor, $note, false,);
|
|
|
|
|
|
|
|
model('ActivityModel')
|
|
|
|
->update($activityId, [
|
|
|
|
'note_id' => $note->id,
|
|
|
|
]);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
return $this->response->setStatusCode(200)
|
|
|
|
->setJSON([]);
|
2021-04-02 17:20:02 +00:00
|
|
|
case 'Announce':
|
2021-05-19 16:35:13 +00:00
|
|
|
$note = model('NoteModel')
|
|
|
|
->getNoteByUri($payload->object->object,);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
$reblogNote = model('NoteModel')
|
|
|
|
->where([
|
|
|
|
'actor_id' => $payloadActor->id,
|
|
|
|
'reblog_of_id' => service('uuid')
|
|
|
|
->fromString($note->id)
|
|
|
|
->getBytes(),
|
|
|
|
])
|
|
|
|
->first();
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
model('NoteModel')
|
|
|
|
->undoReblog($reblogNote, false);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
model('ActivityModel')
|
|
|
|
->update($activityId, [
|
|
|
|
'note_id' => $note->id,
|
|
|
|
]);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
return $this->response->setStatusCode(200)
|
|
|
|
->setJSON([]);
|
2021-04-02 17:20:02 +00:00
|
|
|
default:
|
|
|
|
// return not handled undo error (501 = not implemented)
|
2021-05-19 16:35:13 +00:00
|
|
|
return $this->response->setStatusCode(501)
|
|
|
|
->setJSON([]);
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
2021-05-19 16:35:13 +00:00
|
|
|
// no break
|
2021-04-02 17:20:02 +00:00
|
|
|
default:
|
|
|
|
// return not handled activity error (501 = not implemented)
|
2021-05-19 16:35:13 +00:00
|
|
|
return $this->response->setStatusCode(501)
|
|
|
|
->setJSON([]);
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
public function outbox(): RedirectResponse
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
|
|
|
// get published activities by publication date
|
|
|
|
$actorActivity = model('ActivityModel')
|
|
|
|
->where('actor_id', $this->actor->id)
|
|
|
|
->where('`created_at` <= NOW()', null, false)
|
|
|
|
->orderBy('created_at', 'DESC');
|
|
|
|
|
|
|
|
$pageNumber = $this->request->getGet('page');
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! isset($pageNumber)) {
|
2021-04-02 17:20:02 +00:00
|
|
|
$actorActivity->paginate(12);
|
|
|
|
$pager = $actorActivity->pager;
|
|
|
|
$collection = new OrderedCollectionObject(null, $pager);
|
|
|
|
} else {
|
2021-05-19 16:35:13 +00:00
|
|
|
$paginatedActivity = $actorActivity->paginate(12, 'default', $pageNumber,);
|
2021-04-02 17:20:02 +00:00
|
|
|
$pager = $actorActivity->pager;
|
|
|
|
$orderedItems = [];
|
|
|
|
foreach ($paginatedActivity as $activity) {
|
2021-05-06 14:00:48 +00:00
|
|
|
$orderedItems[] = $activity->payload;
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
$collection = new OrderedCollectionPage($pager, $orderedItems);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->response
|
|
|
|
->setContentType('application/activity+json')
|
|
|
|
->setBody($collection->toJSON());
|
|
|
|
}
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
public function followers(): RedirectResponse
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
|
|
|
// get followers for a specific actor
|
|
|
|
$followers = model('ActorModel')
|
2021-05-19 16:35:13 +00:00
|
|
|
->join('activitypub_follows', 'activitypub_follows.actor_id = id', 'inner',)
|
2021-04-02 17:20:02 +00:00
|
|
|
->where('activitypub_follows.target_actor_id', $this->actor->id)
|
|
|
|
->orderBy('activitypub_follows.created_at', 'DESC');
|
|
|
|
|
|
|
|
$pageNumber = $this->request->getGet('page');
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! isset($pageNumber)) {
|
2021-04-02 17:20:02 +00:00
|
|
|
$followers->paginate(12);
|
|
|
|
$pager = $followers->pager;
|
|
|
|
$followersCollection = new OrderedCollectionObject(null, $pager);
|
|
|
|
} else {
|
2021-05-19 16:35:13 +00:00
|
|
|
$paginatedFollowers = $followers->paginate(12, 'default', $pageNumber,);
|
2021-04-02 17:20:02 +00:00
|
|
|
$pager = $followers->pager;
|
|
|
|
|
|
|
|
$orderedItems = [];
|
|
|
|
foreach ($paginatedFollowers as $follower) {
|
2021-05-06 14:00:48 +00:00
|
|
|
$orderedItems[] = $follower->uri;
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
2021-05-19 16:35:13 +00:00
|
|
|
$followersCollection = new OrderedCollectionPage($pager, $orderedItems,);
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->response
|
|
|
|
->setContentType('application/activity+json')
|
|
|
|
->setBody($followersCollection->toJSON());
|
|
|
|
}
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
public function attemptFollow(): RedirectResponse | ResponseInterface
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
|
|
|
$rules = [
|
|
|
|
'handle' =>
|
|
|
|
'regex_match[/^@?(?P<username>[\w\.\-]+)@(?P<host>[\w\.\-]+)(?P<port>:[\d]+)?$/]',
|
|
|
|
];
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! $this->validate($rules)) {
|
2021-04-02 17:20:02 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->withInput()
|
|
|
|
->with('errors', $this->validator->getErrors());
|
|
|
|
}
|
|
|
|
|
|
|
|
helper('text');
|
|
|
|
|
|
|
|
// get webfinger data from actor
|
|
|
|
// parse activityPub id to get actor and domain
|
|
|
|
// check if actor and domain exist
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
if (
|
2021-05-19 16:35:13 +00:00
|
|
|
! ($parts = split_handle($this->request->getPost('handle'))) ||
|
|
|
|
! ($data = get_webfinger_data($parts['username'], $parts['domain']))
|
2021-05-12 14:00:25 +00:00
|
|
|
) {
|
2021-04-02 17:20:02 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->withInput()
|
|
|
|
->with('error', lang('ActivityPub.follow.accountNotFound'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$ostatusKey = array_search(
|
|
|
|
'http://ostatus.org/schema/1.0/subscribe',
|
|
|
|
array_column($data->links, 'rel'),
|
2021-05-19 16:35:13 +00:00
|
|
|
true,
|
2021-04-02 17:20:02 +00:00
|
|
|
);
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! $ostatusKey) {
|
2021-04-02 17:20:02 +00:00
|
|
|
// TODO: error, couldn't subscribe to activitypub account
|
|
|
|
// The instance doesn't allow its users to follow others
|
|
|
|
return $this->response->setJSON([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->to(
|
2021-05-19 16:35:13 +00:00
|
|
|
str_replace('{uri}', urlencode($this->actor->uri), $data->links[$ostatusKey]->template,),
|
2021-04-02 17:20:02 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
public function activity(string $activityId): RedirectResponse
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
|
|
|
if (
|
2021-05-19 16:35:13 +00:00
|
|
|
! ($activity = model('ActivityModel')->getActivityById($activityId))
|
2021-04-02 17:20:02 +00:00
|
|
|
) {
|
2021-05-06 14:00:48 +00:00
|
|
|
throw PageNotFoundException::forPageNotFound();
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->response
|
|
|
|
->setContentType('application/activity+json')
|
2021-05-06 14:00:48 +00:00
|
|
|
->setBody(json_encode($activity->payload, JSON_THROW_ON_ERROR));
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
}
|