2021-04-02 17:20:02 +00:00
|
|
|
<?php
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-04-02 17:20:02 +00:00
|
|
|
/**
|
|
|
|
* @copyright 2021 Podlibre
|
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace ActivityPub\Controllers;
|
|
|
|
|
|
|
|
use ActivityPub\Config\ActivityPub;
|
2021-06-21 10:04:18 +00:00
|
|
|
use ActivityPub\Entities\Status;
|
2021-04-02 17:20:02 +00:00
|
|
|
use ActivityPub\Objects\OrderedCollectionObject;
|
|
|
|
use ActivityPub\Objects\OrderedCollectionPage;
|
|
|
|
use CodeIgniter\Controller;
|
2021-05-19 16:35:13 +00:00
|
|
|
use CodeIgniter\Exceptions\PageNotFoundException;
|
|
|
|
use CodeIgniter\HTTP\RedirectResponse;
|
2021-06-08 09:52:11 +00:00
|
|
|
use CodeIgniter\HTTP\Response;
|
2021-05-19 16:35:13 +00:00
|
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
2021-04-02 17:20:02 +00:00
|
|
|
use CodeIgniter\I18n\Time;
|
|
|
|
|
2021-06-21 10:04:18 +00:00
|
|
|
class StatusController extends Controller
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2021-04-02 17:20:02 +00:00
|
|
|
protected $helpers = ['activitypub'];
|
|
|
|
|
2021-06-21 10:04:18 +00:00
|
|
|
protected Status $status;
|
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-06-21 10:04:18 +00:00
|
|
|
if (($status = model('StatusModel')->getStatusById($params[0])) === null) {
|
2021-06-09 12:40:22 +00:00
|
|
|
throw PageNotFoundException::forPageNotFound();
|
|
|
|
}
|
|
|
|
|
2021-06-21 10:04:18 +00:00
|
|
|
$this->status = $status;
|
2021-06-09 12:40:22 +00:00
|
|
|
|
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-06-08 09:52:11 +00:00
|
|
|
/**
|
|
|
|
* @noRector ReturnTypeDeclarationRector
|
|
|
|
*/
|
|
|
|
public function index(): Response
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
|
|
|
$noteObjectClass = $this->config->noteObject;
|
2021-06-21 10:04:18 +00:00
|
|
|
$noteObject = new $noteObjectClass($this->status);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
return $this->response
|
|
|
|
->setContentType('application/activity+json')
|
|
|
|
->setBody($noteObject->toJSON());
|
|
|
|
}
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
/**
|
|
|
|
* @noRector ReturnTypeDeclarationRector
|
|
|
|
*/
|
|
|
|
public function replies(): Response
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-05-19 16:35:13 +00:00
|
|
|
/**
|
2021-06-21 10:04:18 +00:00
|
|
|
* get status replies
|
2021-05-19 16:35:13 +00:00
|
|
|
*/
|
2021-06-21 10:04:18 +00:00
|
|
|
$statusReplies = model('StatusModel')
|
|
|
|
->where('in_reply_to_id', service('uuid') ->fromString($this->status->id) ->getBytes())
|
2021-04-02 17:20:02 +00:00
|
|
|
->where('`published_at` <= NOW()', null, false)
|
|
|
|
->orderBy('published_at', 'ASC');
|
|
|
|
|
2021-06-10 13:18:58 +00:00
|
|
|
$pageNumber = (int) $this->request->getGet('page');
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-06-10 13:18:58 +00:00
|
|
|
if ($pageNumber < 1) {
|
2021-06-21 10:04:18 +00:00
|
|
|
$statusReplies->paginate(12);
|
|
|
|
$pager = $statusReplies->pager;
|
2021-04-02 17:20:02 +00:00
|
|
|
$collection = new OrderedCollectionObject(null, $pager);
|
|
|
|
} else {
|
2021-06-21 10:04:18 +00:00
|
|
|
$paginatedReplies = $statusReplies->paginate(12, 'default', $pageNumber);
|
|
|
|
$pager = $statusReplies->pager;
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
$orderedItems = [];
|
|
|
|
$noteObjectClass = $this->config->noteObject;
|
2021-05-12 14:00:25 +00:00
|
|
|
|
|
|
|
if ($paginatedReplies !== null) {
|
|
|
|
foreach ($paginatedReplies as $reply) {
|
|
|
|
$replyObject = new $noteObjectClass($reply);
|
2021-07-12 18:40:22 +00:00
|
|
|
$orderedItems[] = $replyObject->toArray();
|
2021-05-12 14:00:25 +00:00
|
|
|
}
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
2021-05-12 14:00:25 +00:00
|
|
|
|
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-12 14:00:25 +00:00
|
|
|
public function attemptCreate(): RedirectResponse
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
|
|
|
$rules = [
|
|
|
|
'actor_id' => 'required|is_natural_no_zero',
|
|
|
|
'message' => 'required|max_length[500]',
|
|
|
|
];
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2021-06-21 10:04:18 +00:00
|
|
|
$newStatus = new Status([
|
2021-04-02 17:20:02 +00:00
|
|
|
'actor_id' => $this->request->getPost('actor_id'),
|
|
|
|
'message' => $this->request->getPost('message'),
|
|
|
|
'published_at' => Time::now(),
|
|
|
|
]);
|
|
|
|
|
2021-06-21 10:04:18 +00:00
|
|
|
if (! model('StatusModel')->addStatus($newStatus)) {
|
2021-04-02 17:20:02 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->withInput()
|
|
|
|
// TODO: translate
|
2021-06-21 10:04:18 +00:00
|
|
|
->with('error', "Couldn't create Status");
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 10:04:18 +00:00
|
|
|
// Status without preview card has been successfully created
|
2021-04-02 17:20:02 +00:00
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
public function attemptFavourite(): RedirectResponse
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
|
|
|
$rules = [
|
|
|
|
'actor_id' => 'required|is_natural_no_zero',
|
|
|
|
];
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
$actor = model('ActorModel')
|
2021-06-08 09:52:11 +00:00
|
|
|
->getActorById($this->request->getPost('actor_id'));
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
model('FavouriteModel')
|
2021-06-21 10:04:18 +00:00
|
|
|
->toggleFavourite($actor, $this->status->id);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
public function attemptReblog(): RedirectResponse
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
|
|
|
$rules = [
|
|
|
|
'actor_id' => 'required|is_natural_no_zero',
|
|
|
|
];
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
$actor = model('ActorModel')
|
2021-06-08 09:52:11 +00:00
|
|
|
->getActorById($this->request->getPost('actor_id'));
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-06-21 10:04:18 +00:00
|
|
|
model('StatusModel')
|
|
|
|
->toggleReblog($actor, $this->status);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
public function attemptReply(): RedirectResponse
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
|
|
|
$rules = [
|
|
|
|
'actor_id' => 'required|is_natural_no_zero',
|
|
|
|
'message' => 'required|max_length[500]',
|
|
|
|
];
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2021-06-21 10:04:18 +00:00
|
|
|
$newReplyStatus = new Status([
|
2021-04-02 17:20:02 +00:00
|
|
|
'actor_id' => $this->request->getPost('actor_id'),
|
2021-06-21 10:04:18 +00:00
|
|
|
'in_reply_to_id' => $this->status->id,
|
2021-04-02 17:20:02 +00:00
|
|
|
'message' => $this->request->getPost('message'),
|
|
|
|
'published_at' => Time::now(),
|
|
|
|
]);
|
|
|
|
|
2021-06-21 10:04:18 +00:00
|
|
|
if (! model('StatusModel')->addReply($newReplyStatus)) {
|
2021-04-02 17:20:02 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->withInput()
|
|
|
|
// TODO: translate
|
2021-05-06 14:00:48 +00:00
|
|
|
->with('error', "Couldn't create Reply");
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 10:04:18 +00:00
|
|
|
// Reply status without preview card has been successfully created
|
2021-04-02 17:20:02 +00:00
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
public function attemptRemoteAction(string $action): 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-06-21 10:04:18 +00:00
|
|
|
// TODO: error, couldn't remote favourite/share/reply to status
|
|
|
|
// The instance doesn't allow its users remote actions on statuses
|
2021-04-02 17:20:02 +00:00
|
|
|
return $this->response->setJSON([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->to(
|
2021-06-21 10:04:18 +00:00
|
|
|
str_replace('{uri}', urlencode($this->status->uri), $data->links[$ostatusKey]->template),
|
2021-04-02 17:20:02 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
public function attemptBlockActor(): RedirectResponse
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-06-21 10:04:18 +00:00
|
|
|
model('ActorModel')->blockActor($this->status->actor->id);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
public function attemptBlockDomain(): RedirectResponse
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-06-21 10:04:18 +00:00
|
|
|
model('BlockedDomainModel')->blockDomain($this->status->actor->domain);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
public function attemptDelete(): RedirectResponse
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-06-21 10:04:18 +00:00
|
|
|
model('StatusModel', false)->removeStatus($this->status);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
}
|