pixelfed/app/Util/ActivityPub/Inbox.php

320 lines
8.8 KiB
PHP
Raw Normal View History

2018-06-01 03:17:07 +00:00
<?php
namespace App\Util\ActivityPub;
2018-11-17 22:33:24 +00:00
use Cache, DB, Log, Redis, Validator;
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
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 authenticatePayload()
{
2018-11-17 22:33:24 +00:00
try {
$signature = Helpers::validateSignature($this->headers, $this->payload);
$payload = Helpers::validateObject($this->payload);
if($signature == false) {
return;
}
} catch (Exception $e) {
return;
}
$this->payloadLogger();
}
2018-06-01 03:17:07 +00:00
2018-11-17 22:33:24 +00:00
public function payloadLogger()
{
$logger = new Activity;
$logger->data = json_encode($this->payload);
$logger->save();
$this->logger = $logger;
Log::info('AP:inbox:activity:new:'.$this->logger->id);
2018-06-01 03:17:07 +00:00
$this->handleVerb();
}
public function handleVerb()
{
$verb = $this->payload['type'];
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':
$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']);
$inReplyTo = $activity['inReplyTo'];
2018-12-24 02:42:50 +00:00
$url = $activity['id'];
2018-11-17 22:33:24 +00:00
2018-12-24 02:42:50 +00:00
if(!Helpers::statusFirstOrFetch($url, true)) {
2018-11-17 22:33:24 +00:00
return;
}
}
public function handleNoteCreate()
{
$activity = $this->payload['object'];
$actor = $this->actorFirstOrCreate($this->payload['actor']);
if(!$actor || $actor->domain == null) {
return;
}
if(Helpers::userInAudience($this->profile, $this->payload) == false) {
//Log::error('AP:inbox:userInAudience:false - Activity#'.$this->logger->id);
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;
}
$status = DB::transaction(function() use($activity, $actor) {
$status = new Status;
$status->profile_id = $actor->id;
$status->caption = strip_tags($activity['content']);
$status->visibility = $status->scope = 'public';
2018-12-24 02:42:50 +00:00
$status->url = $url;
2018-11-17 22:33:24 +00:00
$status->save();
return $status;
});
Helpers::importNoteAttachment($activity, $status);
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)
]);
if($follower->wasRecentlyCreated == false) {
return;
}
// send notification
$notification = new Notification();
$notification->profile_id = $target->id;
$notification->actor_id = $actor->id;
$notification->action = 'follow';
$notification->message = $follower->toText();
$notification->rendered = $follower->toHtml();
$notification->item_id = $target->id;
$notification->item_type = "App\Profile";
$notification->save();
// send Accept to remote profile
$accept = [
'@context' => 'https://www.w3.org/ns/activitystreams',
2018-12-24 02:42:50 +00:00
'id' => $target->permalink().'#accepts/follows/',
2018-11-17 22:33:24 +00:00
'type' => 'Accept',
'actor' => $target->permalink(),
'object' => [
2018-12-24 02:42:50 +00:00
'id' => $actor->permalink('#follows/'.$target->id),
2018-11-17 22:33:24 +00:00
'type' => 'Follow',
2018-12-24 02:42:50 +00:00
'actor' => $actor->permalink(),
'object' => $target->permalink()
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'];
if(!$actor || $actor->domain == null) {
return;
}
if(Helpers::validateLocalUrl($activity) == false) {
return;
}
$parent = Helpers::statusFirstOrFetch($activity, true);
if(!$parent) {
return;
}
$status = Status::firstOrCreate([
'profile_id' => $actor->id,
'in_reply_to_id' => $parent->id,
'type' => 'reply'
]);
if($status->wasRecentlyCreated) {
$notification = new Notification();
$notification->profile_id = $parent->profile->id;
$notification->actor_id = $actor->id;
$notification->action = 'comment';
$notification->message = $status->toText();
$notification->rendered = $status->toHtml();
$notification->item_id = $parent->id;
$notification->item_type = "App\Status";
$notification->save();
}
2018-11-17 22:33:24 +00:00
}
public function handleAcceptActivity()
{
}
public function handleDeleteActivity()
{
}
public function handleLikeActivity()
2018-06-01 03:17:07 +00:00
{
2018-11-17 22:33:24 +00:00
$actor = $this->payload['actor'];
$profile = self::actorFirstOrCreate($actor);
$obj = $this->payload['object'];
if(Helpers::validateLocalUrl($obj) == false) {
return;
}
$status = Helpers::statusFirstOrFetch($obj);
$like = Like::firstOrCreate([
'profile_id' => $profile->id,
'status_id' => $status->id
]);
if($like->wasRecentlyCreated == false) {
return;
}
LikePipeline::dispatch($like);
}
public function handleRejectActivity()
{
}
public function handleUndoActivity()
{
$actor = $this->payload['actor'];
$profile = self::actorFirstOrCreate($actor);
$obj = $this->payload['object'];
switch ($obj['type']) {
case 'Like':
2018-12-24 02:42:50 +00:00
$status = Helpers::statusFirstOrFetch($obj['object']);
2018-11-17 22:33:24 +00:00
Like::whereProfileId($profile->id)
->whereStatusId($status->id)
->delete();
break;
2018-06-01 03:17:07 +00:00
}
}
2018-08-28 03:07:36 +00:00
}