pixelfed/app/Util/ActivityPub/Inbox.php

380 lines
11 KiB
PHP
Raw Normal View History

2018-06-01 03:17:07 +00:00
<?php
namespace App\Util\ActivityPub;
2019-01-01 06:28:34 +00:00
use Cache, DB, Log, Purify, Redis, Validator;
2018-11-17 22:33:24 +00:00
use App\{
Activity,
Follower,
FollowRequest,
Like,
Notification,
Profile,
Status
};
use Carbon\Carbon;
use App\Util\ActivityPub\Helpers;
use App\Jobs\LikePipeline\LikePipeline;
2018-06-01 03:17:07 +00:00
2019-01-04 03:04:29 +00:00
use App\Util\ActivityPub\Validator\{
2019-07-19 03:53:02 +00:00
Accept,
2019-01-04 03:04:29 +00:00
Follow
};
2018-08-28 03:07:36 +00:00
class Inbox
{
2018-11-17 22:33:24 +00:00
protected $headers;
2018-06-01 03:17:07 +00:00
protected $profile;
protected $payload;
2018-11-17 22:33:24 +00:00
protected $logger;
2018-06-01 03:17:07 +00:00
2018-11-17 22:33:24 +00:00
public function __construct($headers, $profile, $payload)
2018-06-01 03:17:07 +00:00
{
2018-11-17 22:33:24 +00:00
$this->headers = $headers;
2018-06-01 03:17:07 +00:00
$this->profile = $profile;
$this->payload = $payload;
}
public function handle()
{
2018-12-24 02:42:50 +00:00
$this->handleVerb();
2018-06-01 03:17:07 +00:00
}
public function handleVerb()
{
2019-04-07 04:27:32 +00:00
$verb = (string) $this->payload['type'];
2018-06-01 03:17:07 +00:00
switch ($verb) {
case 'Create':
$this->handleCreateActivity();
break;
case 'Follow':
$this->handleFollowActivity();
break;
2018-11-17 22:33:24 +00:00
case 'Announce':
$this->handleAnnounceActivity();
break;
case 'Accept':
2019-01-04 03:04:29 +00:00
if(Accept::validate($this->payload) == false) { return; }
2018-11-17 22:33:24 +00:00
$this->handleAcceptActivity();
break;
case 'Delete':
$this->handleDeleteActivity();
break;
case 'Like':
$this->handleLikeActivity();
break;
case 'Reject':
$this->handleRejectActivity();
break;
case 'Undo':
$this->handleUndoActivity();
break;
2018-06-01 03:17:07 +00:00
default:
// TODO: decide how to handle invalid verbs.
break;
}
}
2018-11-17 22:33:24 +00:00
public function verifyNoteAttachment()
{
$activity = $this->payload['object'];
if(isset($activity['inReplyTo']) &&
!empty($activity['inReplyTo']) &&
Helpers::validateUrl($activity['inReplyTo'])
) {
// reply detected, skip attachment check
return true;
}
$valid = Helpers::verifyAttachments($activity);
return $valid;
}
public function actorFirstOrCreate($actorUrl)
{
return Helpers::profileFirstOrNew($actorUrl);
}
2018-06-01 03:17:07 +00:00
public function handleCreateActivity()
{
2018-11-17 22:33:24 +00:00
$activity = $this->payload['object'];
if(!$this->verifyNoteAttachment()) {
return;
}
if($activity['type'] == 'Note' && !empty($activity['inReplyTo'])) {
$this->handleNoteReply();
} elseif($activity['type'] == 'Note' && !empty($activity['attachment'])) {
$this->handleNoteCreate();
}
}
public function handleNoteReply()
{
$activity = $this->payload['object'];
$actor = $this->actorFirstOrCreate($this->payload['actor']);
2019-04-05 05:19:56 +00:00
if(!$actor || $actor->domain == null) {
return;
}
2018-11-17 22:33:24 +00:00
$inReplyTo = $activity['inReplyTo'];
2018-12-24 02:42:50 +00:00
$url = $activity['id'];
2018-11-17 22:33:24 +00:00
2019-04-05 05:19:56 +00:00
Helpers::statusFirstOrFetch($url, true);
return;
2018-11-17 22:33:24 +00:00
}
public function handleNoteCreate()
{
2019-04-18 04:49:27 +00:00
return;
2019-04-18 05:29:22 +00:00
2018-11-17 22:33:24 +00:00
$activity = $this->payload['object'];
$actor = $this->actorFirstOrCreate($this->payload['actor']);
if(!$actor || $actor->domain == null) {
return;
}
if(Helpers::userInAudience($this->profile, $this->payload) == false) {
return;
}
2018-12-24 02:42:50 +00:00
$url = $activity['id'];
if(Status::whereUrl($url)->exists()) {
2018-11-17 22:33:24 +00:00
return;
}
2019-06-25 04:46:35 +00:00
Helpers::statusFetch($url);
2019-04-05 05:19:56 +00:00
return;
2018-06-01 03:17:07 +00:00
}
public function handleFollowActivity()
{
2018-11-17 22:33:24 +00:00
$actor = $this->actorFirstOrCreate($this->payload['actor']);
if(!$actor || $actor->domain == null) {
return;
}
2018-06-01 03:17:07 +00:00
$target = $this->profile;
2018-11-17 22:33:24 +00:00
if($target->is_private == true) {
// make follow request
FollowRequest::firstOrCreate([
'follower_id' => $actor->id,
'following_id' => $target->id
]);
// todo: send notification
} else {
// store new follower
$follower = Follower::firstOrCreate([
'profile_id' => $actor->id,
'following_id' => $target->id,
'local_profile' => empty($actor->domain)
]);
2019-02-25 03:17:01 +00:00
if($follower->wasRecentlyCreated == true) {
// send notification
Notification::firstOrCreate([
'profile_id' => $target->id,
'actor_id' => $actor->id,
'action' => 'follow',
'message' => $follower->toText(),
'rendered' => $follower->toHtml(),
'item_id' => $target->id,
'item_type' => 'App\Profile'
]);
2018-11-17 22:33:24 +00:00
}
2019-03-09 09:15:16 +00:00
$payload = $this->payload;
2018-11-17 22:33:24 +00:00
// send Accept to remote profile
$accept = [
'@context' => 'https://www.w3.org/ns/activitystreams',
2018-12-28 05:55:21 +00:00
'id' => $target->permalink().'#accepts/follows/' . $follower->id,
2018-11-17 22:33:24 +00:00
'type' => 'Accept',
'actor' => $target->permalink(),
2019-03-09 09:15:16 +00:00
'object' => $payload
2018-11-17 22:33:24 +00:00
];
Helpers::sendSignedObject($target, $actor->inbox_url, $accept);
}
2018-06-01 03:17:07 +00:00
}
2018-11-17 22:33:24 +00:00
public function handleAnnounceActivity()
{
2018-12-24 02:42:50 +00:00
$actor = $this->actorFirstOrCreate($this->payload['actor']);
$activity = $this->payload['object'];
2019-06-25 05:57:48 +00:00
2018-12-24 02:42:50 +00:00
if(!$actor || $actor->domain == null) {
return;
}
2019-06-25 05:57:48 +00:00
2018-12-24 02:42:50 +00:00
if(Helpers::validateLocalUrl($activity) == false) {
return;
}
2019-06-25 05:57:48 +00:00
$parent = Helpers::statusFetch($activity);
if(empty($parent)) {
2018-12-24 02:42:50 +00:00
return;
}
2019-06-25 05:57:48 +00:00
2018-12-24 02:42:50 +00:00
$status = Status::firstOrCreate([
'profile_id' => $actor->id,
2018-12-25 06:57:36 +00:00
'reblog_of_id' => $parent->id,
2019-06-25 05:57:48 +00:00
'type' => 'share'
2018-12-24 02:42:50 +00:00
]);
2019-06-25 05:57:48 +00:00
2018-12-25 06:35:04 +00:00
Notification::firstOrCreate([
'profile_id' => $parent->profile->id,
'actor_id' => $actor->id,
'action' => 'share',
'message' => $status->replyToText(),
'rendered' => $status->replyToHtml(),
'item_id' => $parent->id,
'item_type' => 'App\Status'
]);
2019-06-25 06:10:34 +00:00
2019-06-18 18:57:56 +00:00
$parent->reblogs_count = $parent->shares()->count();
$parent->save();
2018-11-17 22:33:24 +00:00
}
public function handleAcceptActivity()
{
2019-07-25 01:04:39 +00:00
return;
2019-02-25 03:07:43 +00:00
$actor = $this->payload['actor'];
$obj = $this->payload['object'];
switch ($obj['type']) {
case 'Follow':
$accept = [
'@context' => 'https://www.w3.org/ns/activitystreams',
'id' => $target->permalink().'#accepts/follows/' . $follower->id,
'type' => 'Accept',
'actor' => $target->permalink(),
'object' => [
'id' => $actor->permalink('#follows/'.$target->id),
'type' => 'Follow',
'actor' => $actor->permalink(),
'object' => $target->permalink()
]
];
break;
default:
# code...
break;
}
2018-11-17 22:33:24 +00:00
}
public function handleDeleteActivity()
{
2018-12-25 04:42:31 +00:00
$actor = $this->payload['actor'];
$obj = $this->payload['object'];
2019-04-18 05:29:22 +00:00
abort_if(!Helpers::validateUrl($obj), 400);
2018-12-25 04:42:31 +00:00
if(is_string($obj) && Helpers::validateUrl($obj)) {
// actor object detected
2019-01-01 06:28:34 +00:00
// todo delete actor
2019-06-27 06:44:09 +00:00
return;
2019-04-18 05:29:22 +00:00
} else if (Helpers::validateUrl($obj['id']) && Helpers::validateObject($obj) && $obj['type'] == 'Tombstone') {
// todo delete status or object
2019-06-27 06:44:09 +00:00
return;
2018-12-25 04:42:31 +00:00
}
2018-11-17 22:33:24 +00:00
}
public function handleLikeActivity()
2018-06-01 03:17:07 +00:00
{
2018-11-17 22:33:24 +00:00
$actor = $this->payload['actor'];
2019-04-18 05:29:22 +00:00
abort_if(!Helpers::validateUrl($actor), 400);
2018-11-17 22:33:24 +00:00
$profile = self::actorFirstOrCreate($actor);
$obj = $this->payload['object'];
2019-04-18 05:29:22 +00:00
abort_if(!Helpers::validateLocalUrl($obj), 400);
2018-11-17 22:33:24 +00:00
$status = Helpers::statusFirstOrFetch($obj);
2019-04-07 04:27:32 +00:00
if(!$status || !$profile) {
return;
}
2018-11-17 22:33:24 +00:00
$like = Like::firstOrCreate([
'profile_id' => $profile->id,
'status_id' => $status->id
]);
2019-04-18 05:29:22 +00:00
if($like->wasRecentlyCreated == true) {
2019-06-18 18:57:56 +00:00
$status->likes_count = $status->likes()->count();
$status->save();
2019-04-18 05:29:22 +00:00
LikePipeline::dispatch($like);
2018-11-17 22:33:24 +00:00
}
2019-04-18 05:29:22 +00:00
return;
2018-11-17 22:33:24 +00:00
}
public function handleRejectActivity()
{
}
public function handleUndoActivity()
{
$actor = $this->payload['actor'];
$profile = self::actorFirstOrCreate($actor);
$obj = $this->payload['object'];
switch ($obj['type']) {
2019-02-25 03:07:43 +00:00
case 'Accept':
2018-12-25 04:42:31 +00:00
break;
case 'Announce':
2019-06-25 19:17:16 +00:00
$obj = $obj['object'];
2019-06-25 06:10:34 +00:00
abort_if(!Helpers::validateLocalUrl($obj), 400);
$status = Helpers::statusFetch($obj);
if(!$status) {
return;
}
Status::whereProfileId($profile->id)
->whereReblogOfId($status->id)
->forceDelete();
Notification::whereProfileId($status->profile->id)
->whereActorId($profile->id)
->whereAction('share')
->whereItemId($status->reblog_of_id)
->whereItemType('App\Status')
->forceDelete();
2018-11-17 22:33:24 +00:00
break;
2019-02-25 03:07:43 +00:00
case 'Block':
break;
case 'Follow':
$following = self::actorFirstOrCreate($obj['object']);
2019-04-07 04:27:32 +00:00
if(!$following) {
return;
}
2019-02-25 03:07:43 +00:00
Follower::whereProfileId($profile->id)
->whereFollowingId($following->id)
->delete();
break;
case 'Like':
$status = Helpers::statusFirstOrFetch($obj['object']);
2019-04-07 04:27:32 +00:00
if(!$status) {
return;
}
2019-02-25 03:07:43 +00:00
Like::whereProfileId($profile->id)
->whereStatusId($status->id)
->forceDelete();
Notification::whereProfileId($status->profile->id)
->whereActorId($profile->id)
->whereAction('like')
->whereItemId($status->id)
->whereItemType('App\Status')
->forceDelete();
break;
2018-06-01 03:17:07 +00:00
}
2019-06-25 06:10:34 +00:00
return;
2018-06-01 03:17:07 +00:00
}
2018-08-28 03:07:36 +00:00
}