1
0
Fork 0
pixelfed/app/Util/ActivityPub/Inbox.php

626 lines
19 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,
2020-11-18 21:52:44 +00:00
DirectMessage,
2018-11-17 22:33:24 +00:00
Follower,
FollowRequest,
Like,
Notification,
2020-11-18 21:52:44 +00:00
Media,
2018-11-17 22:33:24 +00:00
Profile,
2019-09-06 02:40:40 +00:00
Status,
StatusHashtag,
2020-11-18 21:52:44 +00:00
UserFilter
2018-11-17 22:33:24 +00:00
};
use Carbon\Carbon;
use App\Util\ActivityPub\Helpers;
2020-11-19 02:13:34 +00:00
use Illuminate\Support\Str;
2018-11-17 22:33:24 +00:00
use App\Jobs\LikePipeline\LikePipeline;
2019-07-25 01:25:51 +00:00
use App\Jobs\FollowPipeline\FollowPipeline;
2020-12-18 09:58:00 +00:00
use App\Jobs\DeletePipeline\DeleteRemoteProfilePipeline;
2018-06-01 03:17:07 +00:00
2020-04-30 02:52:48 +00:00
use App\Util\ActivityPub\Validator\Accept as AcceptValidator;
2020-11-18 21:19:02 +00:00
use App\Util\ActivityPub\Validator\Add as AddValidator;
2020-04-30 02:52:48 +00:00
use App\Util\ActivityPub\Validator\Announce as AnnounceValidator;
use App\Util\ActivityPub\Validator\Follow as FollowValidator;
use App\Util\ActivityPub\Validator\Like as LikeValidator;
use App\Util\ActivityPub\Validator\UndoFollow as UndoFollowValidator;
2019-01-04 03:04:29 +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();
2020-04-29 19:27:05 +00:00
2020-05-21 22:45:51 +00:00
// if(!Activity::where('data->id', $this->payload['id'])->exists()) {
// (new Activity())->create([
// 'to_id' => $this->profile->id,
// 'data' => json_encode($this->payload)
// ]);
// }
2020-04-30 02:28:30 +00:00
return;
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) {
2020-11-18 21:19:02 +00:00
case 'Add':
if(AddValidator::validate($this->payload) == false) { return; }
$this->handleAddActivity();
break;
2018-06-01 03:17:07 +00:00
case 'Create':
$this->handleCreateActivity();
break;
case 'Follow':
2020-05-21 22:45:51 +00:00
if(FollowValidator::validate($this->payload) == false) { return; }
2018-06-01 03:17:07 +00:00
$this->handleFollowActivity();
break;
2018-11-17 22:33:24 +00:00
case 'Announce':
2020-04-30 02:52:48 +00:00
if(AnnounceValidator::validate($this->payload) == false) { return; }
2018-11-17 22:33:24 +00:00
$this->handleAnnounceActivity();
break;
case 'Accept':
2020-04-30 02:52:48 +00:00
if(AcceptValidator::validate($this->payload) == false) { return; }
2018-11-17 22:33:24 +00:00
$this->handleAcceptActivity();
break;
case 'Delete':
$this->handleDeleteActivity();
break;
case 'Like':
2020-05-21 22:45:51 +00:00
if(LikeValidator::validate($this->payload) == false) { return; }
2018-11-17 22:33:24 +00:00
$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)
{
2019-08-17 07:45:55 +00:00
return Helpers::profileFetch($actorUrl);
2018-11-17 22:33:24 +00:00
}
2020-11-18 21:19:02 +00:00
public function handleAddActivity()
{
// stories ;)
}
2018-06-01 03:17:07 +00:00
public function handleCreateActivity()
{
2018-11-17 22:33:24 +00:00
$activity = $this->payload['object'];
2020-11-18 22:23:11 +00:00
$actor = $this->actorFirstOrCreate($this->payload['actor']);
if(!$actor || $actor->domain == null) {
return;
}
$to = $activity['to'];
$cc = isset($activity['cc']) ? $activity['cc'] : [];
2020-11-18 22:23:11 +00:00
if(count($to) == 1 &&
count($cc) == 0 &&
parse_url($to[0], PHP_URL_HOST) == config('pixelfed.domain.app')
) {
$this->handleDirectMessage();
return;
}
2018-11-17 22:33:24 +00:00
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'];
2019-09-07 04:00:24 +00:00
$url = isset($activity['url']) ? $activity['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()
{
$activity = $this->payload['object'];
$actor = $this->actorFirstOrCreate($this->payload['actor']);
if(!$actor || $actor->domain == null) {
return;
}
2019-07-27 05:13:29 +00:00
if($actor->followers()->count() == 0) {
2019-07-27 05:02:13 +00:00
return;
}
2018-11-17 22:33:24 +00:00
2019-09-07 04:00:24 +00:00
$url = isset($activity['url']) ? $activity['url'] : $activity['id'];
2018-12-24 02:42:50 +00:00
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
}
2020-11-18 21:19:02 +00:00
public function handleDirectMessage()
{
$activity = $this->payload['object'];
$actor = $this->actorFirstOrCreate($this->payload['actor']);
$profile = Profile::whereNull('domain')
->whereUsername(array_last(explode('/', $activity['to'][0])))
->firstOrFail();
if(in_array($actor->id, $profile->blockedIds()->toArray())) {
return;
}
$msg = $activity['content'];
$msgText = strip_tags($activity['content']);
2020-11-19 02:08:21 +00:00
if(Str::startsWith($msgText, '@' . $profile->username)) {
$len = strlen('@' . $profile->username);
$msgText = substr($msgText, $len + 1);
}
2020-11-18 21:19:02 +00:00
if($profile->user->settings->public_dm == false || $profile->is_private) {
if($profile->follows($actor) == true) {
$hidden = false;
} else {
$hidden = true;
}
} else {
$hidden = false;
}
$status = new Status;
$status->profile_id = $actor->id;
$status->caption = $msgText;
$status->rendered = $msg;
$status->visibility = 'direct';
$status->scope = 'direct';
2020-11-18 22:29:54 +00:00
$status->url = $activity['id'];
2020-11-18 21:19:02 +00:00
$status->in_reply_to_profile_id = $profile->id;
$status->save();
$dm = new DirectMessage;
$dm->to_id = $profile->id;
$dm->from_id = $actor->id;
$dm->status_id = $status->id;
$dm->is_hidden = $hidden;
2020-11-18 22:05:34 +00:00
$dm->type = 'text';
2020-11-18 21:19:02 +00:00
$dm->save();
if(count($activity['attachment'])) {
2020-11-19 02:08:21 +00:00
$photos = 0;
$videos = 0;
2020-11-18 21:19:02 +00:00
$allowed = explode(',', config('pixelfed.media_types'));
2020-11-19 02:08:21 +00:00
$activity['attachment'] = array_slice($activity['attachment'], 0, config('pixelfed.max_album_length'));
2020-11-18 21:19:02 +00:00
foreach($activity['attachment'] as $a) {
$type = $a['mediaType'];
$url = $a['url'];
2020-11-18 22:29:54 +00:00
$valid = Helpers::validateUrl($url);
2020-11-18 21:19:02 +00:00
if(in_array($type, $allowed) == false || $valid == false) {
continue;
}
$media = new Media();
$media->remote_media = true;
$media->status_id = $status->id;
$media->profile_id = $status->profile_id;
$media->user_id = null;
$media->media_path = $url;
$media->remote_url = $url;
$media->mime = $type;
$media->save();
2020-11-19 02:08:21 +00:00
if(explode('/', $type)[0] == 'image') {
$photos = $photos + 1;
}
if(explode('/', $type)[0] == 'video') {
$videos = $videos + 1;
}
}
if($photos && $videos == 0) {
$dm->type = $photos == 1 ? 'photo' : 'photos';
$dm->save();
}
if($videos && $photos == 0) {
$dm->type = $videos == 1 ? 'video' : 'videos';
$dm->save();
2020-11-18 21:19:02 +00:00
}
}
2020-11-19 02:54:09 +00:00
if(filter_var($msgText, FILTER_VALIDATE_URL)) {
if(Helpers::validateUrl($msgText)) {
2020-11-18 21:19:02 +00:00
$dm->type = 'link';
$dm->meta = [
2020-11-19 02:54:09 +00:00
'domain' => parse_url($msgText, PHP_URL_HOST),
'local' => parse_url($msgText, PHP_URL_HOST) ==
2020-11-18 21:19:02 +00:00
parse_url(config('app.url'), PHP_URL_HOST)
];
$dm->save();
}
}
$nf = UserFilter::whereUserId($profile->id)
->whereFilterableId($actor->id)
->whereFilterableType('App\Profile')
->whereFilterType('dm.mute')
->exists();
if($profile->domain == null && $hidden == false && !$nf) {
$notification = new Notification();
$notification->profile_id = $profile->id;
$notification->actor_id = $actor->id;
$notification->action = 'dm';
$notification->message = $dm->toText();
$notification->rendered = $dm->toHtml();
$notification->item_id = $dm->id;
$notification->item_type = "App\DirectMessage";
$notification->save();
}
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']);
2020-11-26 07:39:01 +00:00
$target = $this->actorFirstOrCreate($this->payload['object']);
2020-05-21 22:45:51 +00:00
if(!$actor || $actor->domain == null || $target->domain !== null) {
return;
}
if(
Follower::whereProfileId($actor->id)
->whereFollowingId($target->id)
->exists() ||
FollowRequest::whereFollowerId($actor->id)
->whereFollowingId($target->id)
2020-05-21 23:45:31 +00:00
->exists()
2020-05-21 22:45:51 +00:00
) {
2018-11-17 22:33:24 +00:00
return;
}
if($target->is_private == true) {
FollowRequest::firstOrCreate([
'follower_id' => $actor->id,
'following_id' => $target->id
]);
Cache::forget('profile:follower_count:'.$target->id);
Cache::forget('profile:follower_count:'.$actor->id);
Cache::forget('profile:following_count:'.$target->id);
Cache::forget('profile:following_count:'.$actor->id);
2018-11-17 22:33:24 +00:00
} else {
2020-05-21 22:45:51 +00:00
$follower = new Follower;
2020-05-21 23:48:05 +00:00
$follower->profile_id = $actor->id;
$follower->following_id = $target->id;
$follower->local_profile = empty($actor->domain);
$follower->save();
2020-05-21 22:45:51 +00:00
FollowPipeline::dispatch($follower);
2019-08-12 06:23:46 +00:00
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-08-12 06:23:46 +00:00
'object' => [
'id' => $this->payload['id'],
2019-08-12 06:23:46 +00:00
'actor' => $actor->permalink(),
'type' => 'Follow',
'object' => $target->permalink()
]
2018-11-17 22:33:24 +00:00
];
Helpers::sendSignedObject($target, $actor->inbox_url, $accept);
Cache::forget('profile:follower_count:'.$target->id);
Cache::forget('profile:follower_count:'.$actor->id);
Cache::forget('profile:following_count:'.$target->id);
Cache::forget('profile:following_count:'.$actor->id);
2018-11-17 22:33:24 +00:00
}
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:25:51 +00:00
2019-08-12 02:27:17 +00:00
$actor = $this->payload['object']['actor'];
2019-07-25 01:32:07 +00:00
$obj = $this->payload['object']['object'];
2019-07-25 01:25:51 +00:00
$type = $this->payload['object']['type'];
if($type !== 'Follow') {
return;
2019-02-25 03:07:43 +00:00
}
2019-07-25 01:25:51 +00:00
2019-08-12 02:27:17 +00:00
$actor = Helpers::validateLocalUrl($actor);
$target = Helpers::validateUrl($obj);
2019-07-25 01:25:51 +00:00
if(!$actor || !$target) {
return;
}
$actor = Helpers::profileFetch($actor);
$target = Helpers::profileFetch($target);
$request = FollowRequest::whereFollowerId($actor->id)
->whereFollowingId($target->id)
->whereIsRejected(false)
->first();
if(!$request) {
return;
}
2019-08-13 01:26:57 +00:00
$follower = Follower::firstOrCreate([
'profile_id' => $actor->id,
'following_id' => $target->id,
]);
2019-07-25 01:25:51 +00:00
FollowPipeline::dispatch($follower);
$request->delete();
2018-11-17 22:33:24 +00:00
}
public function handleDeleteActivity()
{
2019-09-06 02:40:40 +00:00
if(!isset(
$this->payload['actor'],
2020-06-13 06:18:52 +00:00
$this->payload['object']
2019-09-06 02:40:40 +00:00
)) {
return;
}
2018-12-25 04:42:31 +00:00
$actor = $this->payload['actor'];
$obj = $this->payload['object'];
2020-11-18 21:19:02 +00:00
if(is_string($obj) == true && $actor == $obj && Helpers::validateUrl($obj)) {
$profile = Profile::whereRemoteUrl($obj)->first();
if(!$profile || $profile->private_key != null) {
return;
}
2020-12-18 09:58:00 +00:00
DeleteRemoteProfilePipeline::dispatchNow($profile);
2020-04-29 21:56:21 +00:00
return;
2020-11-26 07:39:01 +00:00
} else {
$type = $this->payload['object']['type'];
$typeCheck = in_array($type, ['Person', 'Tombstone']);
if(!Helpers::validateUrl($actor) || !Helpers::validateUrl($obj['id']) || !$typeCheck) {
2019-09-06 02:40:40 +00:00
return;
2020-11-26 07:39:01 +00:00
}
if(parse_url($obj['id'], PHP_URL_HOST) !== parse_url($actor, PHP_URL_HOST)) {
return;
}
$id = $this->payload['object']['id'];
switch ($type) {
case 'Person':
$profile = Profile::whereRemoteUrl($actor)->first();
if(!$profile || $profile->private_key != null) {
return;
}
2020-12-18 09:58:00 +00:00
DeleteRemoteProfilePipeline::dispatchNow($profile);
return;
2020-11-26 07:39:01 +00:00
break;
case 'Tombstone':
$profile = Helpers::profileFetch($actor);
$status = Status::whereProfileId($profile->id)
->whereUri($id)
->orWhere('url', $id)
->orWhere('object_url', $id)
->first();
if(!$status) {
return;
}
$status->directMessage()->delete();
$status->media()->delete();
$status->likes()->delete();
$status->shares()->delete();
$status->delete();
2019-09-06 02:40:40 +00:00
return;
2020-11-26 07:39:01 +00:00
break;
default:
2019-09-06 02:40:40 +00:00
return;
2020-11-26 07:39:01 +00:00
break;
}
2019-09-06 02:40:40 +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
2019-09-06 02:40:40 +00:00
if(!Helpers::validateUrl($actor)) {
return;
}
2019-04-18 05:29:22 +00:00
2018-11-17 22:33:24 +00:00
$profile = self::actorFirstOrCreate($actor);
$obj = $this->payload['object'];
2019-09-06 02:40:40 +00:00
if(!Helpers::validateUrl($obj)) {
return;
}
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-09-06 02:40:40 +00:00
if(!Helpers::validateLocalUrl($obj)) {
return;
}
2019-06-25 06:10:34 +00:00
$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();
Notification::whereProfileId($following->id)
->whereActorId($profile->id)
->whereAction('follow')
->whereItemId($following->id)
->whereItemType('App\Profile')
->forceDelete();
2019-02-25 03:07:43 +00:00
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
}