pixelfed/app/Util/ActivityPub/Inbox.php

1401 lines
41 KiB
PHP
Raw Normal View History

2018-06-01 03:17:07 +00:00
<?php
namespace App\Util\ActivityPub;
2024-09-07 09:01:50 +00:00
use App\DirectMessage;
use App\Follower;
use App\FollowRequest;
use App\Instance;
2020-12-18 09:58:00 +00:00
use App\Jobs\DeletePipeline\DeleteRemoteProfilePipeline;
2024-09-07 09:01:50 +00:00
use App\Jobs\FollowPipeline\FollowPipeline;
use App\Jobs\HomeFeedPipeline\FeedRemoveRemotePipeline;
use App\Jobs\LikePipeline\LikePipeline;
use App\Jobs\MovePipeline\CleanupLegacyAccountMovePipeline;
use App\Jobs\MovePipeline\MoveMigrateFollowersPipeline;
use App\Jobs\MovePipeline\ProcessMovePipeline;
use App\Jobs\MovePipeline\UnfollowLegacyAccountMovePipeline;
use App\Jobs\ProfilePipeline\HandleUpdateActivity;
2024-09-30 11:12:52 +00:00
use App\Jobs\PushNotificationPipeline\MentionPushNotifyPipeline;
2023-08-01 10:30:50 +00:00
use App\Jobs\StatusPipeline\RemoteStatusDelete;
2024-09-07 09:01:50 +00:00
use App\Jobs\StatusPipeline\StatusRemoteUpdatePipeline;
2021-09-04 03:21:17 +00:00
use App\Jobs\StoryPipeline\StoryExpire;
use App\Jobs\StoryPipeline\StoryFetch;
2024-09-07 09:01:50 +00:00
use App\Like;
use App\Media;
use App\Models\Conversation;
use App\Models\RemoteReport;
use App\Notification;
use App\Profile;
use App\Services\AccountService;
use App\Services\FollowerService;
2024-09-30 11:12:52 +00:00
use App\Services\NotificationAppGatewayService;
2024-09-07 09:01:50 +00:00
use App\Services\PollService;
2024-09-30 11:12:52 +00:00
use App\Services\PushNotificationService;
2024-09-07 09:01:50 +00:00
use App\Services\ReblogService;
use App\Services\UserFilterService;
use App\Status;
use App\Story;
use App\StoryView;
use App\UserFilter;
2020-04-30 02:52:48 +00:00
use App\Util\ActivityPub\Validator\Accept as AcceptValidator;
use App\Util\ActivityPub\Validator\Announce as AnnounceValidator;
use App\Util\ActivityPub\Validator\Follow as FollowValidator;
use App\Util\ActivityPub\Validator\Like as LikeValidator;
2024-09-07 08:49:33 +00:00
use App\Util\ActivityPub\Validator\MoveValidator;
2024-09-07 09:01:50 +00:00
use App\Util\ActivityPub\Validator\UpdatePersonValidator;
use Cache;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Log;
2024-09-07 09:01:50 +00:00
use Illuminate\Support\Str;
use Purify;
use Storage;
use Throwable;
2021-08-05 02:29:21 +00:00
2018-08-28 03:07:36 +00:00
class Inbox
{
2023-12-08 10:26:32 +00:00
protected $headers;
2024-09-07 09:01:50 +00:00
2023-12-08 10:26:32 +00:00
protected $profile;
2024-09-07 09:01:50 +00:00
2023-12-08 10:26:32 +00:00
protected $payload;
2024-09-07 09:01:50 +00:00
2023-12-08 10:26:32 +00:00
protected $logger;
public function __construct($headers, $profile, $payload)
{
$this->headers = $headers;
$this->profile = $profile;
$this->payload = $payload;
}
public function handle()
{
$this->handleVerb();
2024-09-07 09:01:50 +00:00
2023-12-08 10:26:32 +00:00
}
public function handleVerb()
{
$verb = (string) $this->payload['type'];
switch ($verb) {
case 'Add':
$this->handleAddActivity();
break;
case 'Create':
$this->handleCreateActivity();
break;
case 'Follow':
2024-09-07 09:01:50 +00:00
if (FollowValidator::validate($this->payload) == false) {
return;
}
2023-12-08 10:26:32 +00:00
$this->handleFollowActivity();
break;
case 'Announce':
2024-09-07 09:01:50 +00:00
if (AnnounceValidator::validate($this->payload) == false) {
return;
}
2023-12-08 10:26:32 +00:00
$this->handleAnnounceActivity();
break;
case 'Accept':
2024-09-07 09:01:50 +00:00
if (AcceptValidator::validate($this->payload) == false) {
return;
}
2023-12-08 10:26:32 +00:00
$this->handleAcceptActivity();
break;
case 'Delete':
$this->handleDeleteActivity();
break;
case 'Like':
2024-09-07 09:01:50 +00:00
if (LikeValidator::validate($this->payload) == false) {
return;
}
2023-12-08 10:26:32 +00:00
$this->handleLikeActivity();
break;
case 'Reject':
$this->handleRejectActivity();
break;
case 'Undo':
$this->handleUndoActivity();
break;
case 'View':
$this->handleViewActivity();
break;
case 'Story:Reaction':
$this->handleStoryReactionActivity();
break;
case 'Story:Reply':
$this->handleStoryReplyActivity();
break;
case 'Flag':
$this->handleFlagActivity();
break;
case 'Update':
$this->handleUpdateActivity();
break;
2024-09-07 08:49:33 +00:00
case 'Move':
2024-09-07 09:01:50 +00:00
if (MoveValidator::validate($this->payload) == false) {
Log::info('[AP][INBOX][MOVE] VALIDATE_FAILURE '.json_encode($this->payload));
2024-09-07 08:49:33 +00:00
return;
}
$this->handleMoveActivity();
break;
2023-12-08 10:26:32 +00:00
default:
// TODO: decide how to handle invalid verbs.
break;
}
}
public function verifyNoteAttachment()
{
$activity = $this->payload['object'];
2024-09-07 09:01:50 +00:00
if (isset($activity['inReplyTo']) &&
! empty($activity['inReplyTo']) &&
2023-12-08 10:26:32 +00:00
Helpers::validateUrl($activity['inReplyTo'])
) {
// reply detected, skip attachment check
return true;
}
$valid = Helpers::verifyAttachments($activity);
return $valid;
}
public function actorFirstOrCreate($actorUrl)
{
return Helpers::profileFetch($actorUrl);
}
public function handleAddActivity()
{
// stories ;)
2024-09-07 09:01:50 +00:00
if (! isset(
2023-12-08 10:26:32 +00:00
$this->payload['actor'],
$this->payload['object']
)) {
return;
}
$actor = $this->payload['actor'];
$obj = $this->payload['object'];
2024-09-07 09:01:50 +00:00
if (! Helpers::validateUrl($actor)) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if (! isset($obj['type'])) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
switch ($obj['type']) {
2023-12-08 10:26:32 +00:00
case 'Story':
StoryFetch::dispatch($this->payload);
2024-09-07 09:01:50 +00:00
break;
2023-12-08 10:26:32 +00:00
}
}
public function handleCreateActivity()
{
$activity = $this->payload['object'];
2024-09-07 09:01:50 +00:00
if (config('autospam.live_filters.enabled')) {
$filters = config('autospam.live_filters.filters');
2024-09-07 09:01:50 +00:00
if (! empty($filters) && isset($activity['content']) && ! empty($activity['content']) && strlen($filters) > 3) {
$filters = array_map('trim', explode(',', $filters));
$content = $activity['content'];
2024-09-07 09:01:50 +00:00
foreach ($filters as $filter) {
$filter = trim(strtolower($filter));
2024-09-07 09:01:50 +00:00
if (! $filter || ! strlen($filter)) {
continue;
}
2024-09-07 09:01:50 +00:00
if (str_contains(strtolower($content), $filter)) {
return;
}
}
}
}
2023-12-08 10:26:32 +00:00
$actor = $this->actorFirstOrCreate($this->payload['actor']);
2024-09-07 09:01:50 +00:00
if (! $actor || $actor->domain == null) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if (! isset($activity['to'])) {
2023-12-08 10:26:32 +00:00
return;
}
$to = isset($activity['to']) ? $activity['to'] : [];
$cc = isset($activity['cc']) ? $activity['cc'] : [];
2024-09-07 09:01:50 +00:00
if ($activity['type'] == 'Question') {
2024-09-30 11:12:52 +00:00
//$this->handlePollCreate();
2024-09-07 09:01:50 +00:00
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if (is_array($to) &&
2023-12-08 10:26:32 +00:00
is_array($cc) &&
count($to) == 1 &&
count($cc) == 0 &&
parse_url($to[0], PHP_URL_HOST) == config('pixelfed.domain.app')
) {
$this->handleDirectMessage();
2024-09-07 09:01:50 +00:00
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if ($activity['type'] == 'Note' && ! empty($activity['inReplyTo'])) {
2023-12-08 10:26:32 +00:00
$this->handleNoteReply();
2024-09-07 09:01:50 +00:00
} elseif ($activity['type'] == 'Note' && ! empty($activity['attachment'])) {
if (! $this->verifyNoteAttachment()) {
2023-12-08 10:26:32 +00:00
return;
}
$this->handleNoteCreate();
}
2024-09-07 09:01:50 +00:00
2023-12-08 10:26:32 +00:00
}
public function handleNoteReply()
{
$activity = $this->payload['object'];
$actor = $this->actorFirstOrCreate($this->payload['actor']);
2024-09-07 09:01:50 +00:00
if (! $actor || $actor->domain == null) {
2023-12-08 10:26:32 +00:00
return;
}
$inReplyTo = $activity['inReplyTo'];
$url = isset($activity['url']) ? $activity['url'] : $activity['id'];
Helpers::statusFirstOrFetch($url, true);
2024-09-07 09:01:50 +00:00
2023-12-08 10:26:32 +00:00
}
public function handlePollCreate()
{
$activity = $this->payload['object'];
$actor = $this->actorFirstOrCreate($this->payload['actor']);
2024-09-07 09:01:50 +00:00
if (! $actor || $actor->domain == null) {
2023-12-08 10:26:32 +00:00
return;
}
$url = isset($activity['url']) ? $activity['url'] : $activity['id'];
Helpers::statusFirstOrFetch($url);
2024-09-07 09:01:50 +00:00
2023-12-08 10:26:32 +00:00
}
public function handleNoteCreate()
{
$activity = $this->payload['object'];
$actor = $this->actorFirstOrCreate($this->payload['actor']);
2024-09-07 09:01:50 +00:00
if (! $actor || $actor->domain == null) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if (isset($activity['inReplyTo']) &&
2023-12-08 10:26:32 +00:00
isset($activity['name']) &&
2024-09-07 09:01:50 +00:00
! isset($activity['content']) &&
! isset($activity['attachment']) &&
2023-12-08 10:26:32 +00:00
Helpers::validateLocalUrl($activity['inReplyTo'])
) {
$this->handlePollVote();
2024-09-07 09:01:50 +00:00
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if ($actor->followers_count == 0) {
if (config('federation.activitypub.ingest.store_notes_without_followers')) {
} elseif (FollowerService::followerCount($actor->id, true) == 0) {
2023-12-08 10:26:32 +00:00
return;
}
}
$hasUrl = isset($activity['url']);
$url = isset($activity['url']) ? $activity['url'] : $activity['id'];
2024-09-07 09:01:50 +00:00
if ($hasUrl) {
if (Status::whereUri($url)->exists()) {
2023-12-08 10:26:32 +00:00
return;
}
} else {
2024-09-07 09:01:50 +00:00
if (Status::whereObjectUrl($url)->exists()) {
2023-12-08 10:26:32 +00:00
return;
}
}
Helpers::storeStatus(
$url,
$actor,
$activity
);
2024-09-07 09:01:50 +00:00
2023-12-08 10:26:32 +00:00
}
public function handlePollVote()
{
$activity = $this->payload['object'];
$actor = $this->actorFirstOrCreate($this->payload['actor']);
2024-09-07 09:01:50 +00:00
if (! $actor) {
2023-12-08 10:26:32 +00:00
return;
}
$status = Helpers::statusFetch($activity['inReplyTo']);
2024-09-07 09:01:50 +00:00
if (! $status) {
2023-12-08 10:26:32 +00:00
return;
}
$poll = $status->poll;
2024-09-07 09:01:50 +00:00
if (! $poll) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if (now()->gt($poll->expires_at)) {
2023-12-08 10:26:32 +00:00
return;
}
$choices = $poll->poll_options;
$choice = array_search($activity['name'], $choices);
2024-09-07 09:01:50 +00:00
if ($choice === false) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if (PollVote::whereStatusId($status->id)->whereProfileId($actor->id)->exists()) {
2023-12-08 10:26:32 +00:00
return;
}
$vote = new PollVote;
$vote->status_id = $status->id;
$vote->profile_id = $actor->id;
$vote->poll_id = $poll->id;
$vote->choice = $choice;
$vote->uri = isset($activity['id']) ? $activity['id'] : null;
$vote->save();
$tallies = $poll->cached_tallies;
$tallies[$choice] = $tallies[$choice] + 1;
$poll->cached_tallies = $tallies;
$poll->votes_count = array_sum($tallies);
$poll->save();
PollService::del($status->id);
}
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();
2024-09-07 09:01:50 +00:00
if (! $actor || in_array($actor->id, $profile->blockedIds()->toArray())) {
return;
}
2024-09-07 09:01:50 +00:00
if (AccountService::blocksDomain($profile->id, $actor->domain) == true) {
2023-12-08 10:26:32 +00:00
return;
}
2024-11-19 09:24:00 +00:00
$msg = Purify::clean($activity['content']);
$msgText = strip_tags($msg);
2023-12-08 10:26:32 +00:00
2024-09-07 09:01:50 +00:00
if (Str::startsWith($msgText, '@'.$profile->username)) {
$len = strlen('@'.$profile->username);
2023-12-08 10:26:32 +00:00
$msgText = substr($msgText, $len + 1);
}
2024-09-07 09:01:50 +00:00
if ($profile->user->settings->public_dm == false || $profile->is_private) {
if ($profile->follows($actor) == true) {
2023-12-08 10:26:32 +00:00
$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';
$status->url = $activity['id'];
$status->uri = $activity['id'];
$status->object_url = $activity['id'];
$status->in_reply_to_profile_id = $profile->id;
$status->save();
2023-12-08 10:26:32 +00:00
$dm = new DirectMessage;
$dm->to_id = $profile->id;
$dm->from_id = $actor->id;
$dm->status_id = $status->id;
$dm->is_hidden = $hidden;
$dm->type = 'text';
$dm->save();
Conversation::updateOrInsert(
[
'to_id' => $profile->id,
2024-09-07 09:01:50 +00:00
'from_id' => $actor->id,
2023-12-08 10:26:32 +00:00
],
[
'type' => 'text',
'status_id' => $status->id,
'dm_id' => $dm->id,
2024-09-07 09:01:50 +00:00
'is_hidden' => $hidden,
2023-12-08 10:26:32 +00:00
]
);
2024-09-07 09:01:50 +00:00
if (count($activity['attachment'])) {
2023-12-08 10:26:32 +00:00
$photos = 0;
$videos = 0;
$allowed = explode(',', config_cache('pixelfed.media_types'));
$activity['attachment'] = array_slice($activity['attachment'], 0, config_cache('pixelfed.max_album_length'));
2024-09-07 09:01:50 +00:00
foreach ($activity['attachment'] as $a) {
2023-12-08 10:26:32 +00:00
$type = $a['mediaType'];
$url = $a['url'];
$valid = Helpers::validateUrl($url);
2024-09-07 09:01:50 +00:00
if (in_array($type, $allowed) == false || $valid == false) {
2023-12-08 10:26:32 +00:00
continue;
}
2024-09-07 09:01:50 +00:00
$media = new Media;
2023-12-08 10:26:32 +00:00
$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();
2024-09-07 09:01:50 +00:00
if (explode('/', $type)[0] == 'image') {
2023-12-08 10:26:32 +00:00
$photos = $photos + 1;
}
2024-09-07 09:01:50 +00:00
if (explode('/', $type)[0] == 'video') {
2023-12-08 10:26:32 +00:00
$videos = $videos + 1;
}
}
2024-09-07 09:01:50 +00:00
if ($photos && $videos == 0) {
2023-12-08 10:26:32 +00:00
$dm->type = $photos == 1 ? 'photo' : 'photos';
$dm->save();
}
2024-09-07 09:01:50 +00:00
if ($videos && $photos == 0) {
2023-12-08 10:26:32 +00:00
$dm->type = $videos == 1 ? 'video' : 'videos';
$dm->save();
}
}
2024-09-07 09:01:50 +00:00
if (filter_var($msgText, FILTER_VALIDATE_URL)) {
if (Helpers::validateUrl($msgText)) {
2023-12-08 10:26:32 +00:00
$dm->type = 'link';
$dm->meta = [
'domain' => parse_url($msgText, PHP_URL_HOST),
'local' => parse_url($msgText, PHP_URL_HOST) ==
2024-09-07 09:01:50 +00:00
parse_url(config('app.url'), PHP_URL_HOST),
2023-12-08 10:26:32 +00:00
];
$dm->save();
}
}
$nf = UserFilter::whereUserId($profile->id)
->whereFilterableId($actor->id)
->whereFilterableType('App\Profile')
->whereFilterType('dm.mute')
->exists();
2024-09-07 09:01:50 +00:00
if ($profile->domain == null && $hidden == false && ! $nf) {
$notification = new Notification;
2023-12-08 10:26:32 +00:00
$notification->profile_id = $profile->id;
$notification->actor_id = $actor->id;
$notification->action = 'dm';
$notification->item_id = $dm->id;
$notification->item_type = "App\DirectMessage";
$notification->save();
2024-09-30 11:12:52 +00:00
if (NotificationAppGatewayService::enabled()) {
if (PushNotificationService::check('mention', $profile->id)) {
$user = User::whereProfileId($profile->id)->first();
if ($user && $user->expo_token && $user->notify_enabled) {
MentionPushNotifyPipeline::dispatch($user->expo_token, $actor->username)->onQueue('pushnotify');
}
}
}
2023-12-08 10:26:32 +00:00
}
}
public function handleFollowActivity()
{
$actor = $this->actorFirstOrCreate($this->payload['actor']);
$target = $this->actorFirstOrCreate($this->payload['object']);
2024-09-07 09:01:50 +00:00
if (! $actor || ! $target) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if ($actor->domain == null || $target->domain !== null) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if (AccountService::blocksDomain($target->id, $actor->domain) == true) {
return;
}
2024-09-07 09:01:50 +00:00
if (
2023-12-08 10:26:32 +00:00
Follower::whereProfileId($actor->id)
->whereFollowingId($target->id)
->exists() ||
FollowRequest::whereFollowerId($actor->id)
->whereFollowingId($target->id)
->exists()
) {
return;
}
$blocks = UserFilterService::blocks($target->id);
2024-09-07 09:01:50 +00:00
if ($blocks && in_array($actor->id, $blocks)) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if ($target->is_private == true) {
2023-12-08 10:26:32 +00:00
FollowRequest::updateOrCreate([
'follower_id' => $actor->id,
'following_id' => $target->id,
2024-09-07 09:01:50 +00:00
], [
'activity' => collect($this->payload)->only(['id', 'actor', 'object', 'type'])->toArray(),
2023-12-08 10:26:32 +00:00
]);
} else {
$follower = new Follower;
$follower->profile_id = $actor->id;
$follower->following_id = $target->id;
$follower->local_profile = empty($actor->domain);
$follower->save();
FollowPipeline::dispatch($follower);
FollowerService::add($actor->id, $target->id);
// send Accept to remote profile
$accept = [
'@context' => 'https://www.w3.org/ns/activitystreams',
2024-09-07 09:01:50 +00:00
'id' => $target->permalink().'#accepts/follows/'.$follower->id,
'type' => 'Accept',
'actor' => $target->permalink(),
'object' => [
'id' => $this->payload['id'],
'actor' => $actor->permalink(),
'type' => 'Follow',
'object' => $target->permalink(),
],
2023-12-08 10:26:32 +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);
}
}
public function handleAnnounceActivity()
{
$actor = $this->actorFirstOrCreate($this->payload['actor']);
$activity = $this->payload['object'];
2024-09-07 09:01:50 +00:00
if (! $actor || $actor->domain == null) {
2023-12-08 10:26:32 +00:00
return;
}
$parent = Helpers::statusFetch($activity);
2024-09-07 09:01:50 +00:00
if (! $parent || empty($parent)) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if (AccountService::blocksDomain($parent->profile_id, $actor->domain) == true) {
return;
}
2023-12-08 10:26:32 +00:00
$blocks = UserFilterService::blocks($parent->profile_id);
2024-09-07 09:01:50 +00:00
if ($blocks && in_array($actor->id, $blocks)) {
2023-12-08 10:26:32 +00:00
return;
}
$status = Status::firstOrCreate([
'profile_id' => $actor->id,
'reblog_of_id' => $parent->id,
2024-09-07 09:01:50 +00:00
'type' => 'share',
2023-12-08 10:26:32 +00:00
]);
Notification::firstOrCreate(
[
'profile_id' => $parent->profile_id,
'actor_id' => $actor->id,
'action' => 'share',
'item_id' => $parent->id,
'item_type' => 'App\Status',
]
);
$parent->reblogs_count = $parent->reblogs_count + 1;
$parent->save();
ReblogService::addPostReblog($parent->profile_id, $status->id);
}
public function handleAcceptActivity()
{
$actor = $this->payload['object']['actor'];
$obj = $this->payload['object']['object'];
$type = $this->payload['object']['type'];
2024-09-07 09:01:50 +00:00
if ($type !== 'Follow') {
2023-12-08 10:26:32 +00:00
return;
}
$actor = Helpers::validateLocalUrl($actor);
$target = Helpers::validateUrl($obj);
2024-09-07 09:01:50 +00:00
if (! $actor || ! $target) {
2023-12-08 10:26:32 +00:00
return;
}
$actor = Helpers::profileFetch($actor);
$target = Helpers::profileFetch($target);
2024-09-07 09:01:50 +00:00
if (! $actor || ! $target) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if (AccountService::blocksDomain($target->id, $actor->domain) == true) {
return;
}
2023-12-08 10:26:32 +00:00
$request = FollowRequest::whereFollowerId($actor->id)
->whereFollowingId($target->id)
->whereIsRejected(false)
->first();
2024-09-07 09:01:50 +00:00
if (! $request) {
2023-12-08 10:26:32 +00:00
return;
}
$follower = Follower::firstOrCreate([
'profile_id' => $actor->id,
'following_id' => $target->id,
]);
FollowPipeline::dispatch($follower);
$request->delete();
}
public function handleDeleteActivity()
{
2024-09-07 09:01:50 +00:00
if (! isset(
2023-12-08 10:26:32 +00:00
$this->payload['actor'],
$this->payload['object']
)) {
return;
}
$actor = $this->payload['actor'];
$obj = $this->payload['object'];
2024-09-07 09:01:50 +00:00
if (is_string($obj) == true && $actor == $obj && Helpers::validateUrl($obj)) {
2023-12-08 10:26:32 +00:00
$profile = Profile::whereRemoteUrl($obj)->first();
2024-09-07 09:01:50 +00:00
if (! $profile || $profile->private_key != null) {
2023-12-08 10:26:32 +00:00
return;
}
DeleteRemoteProfilePipeline::dispatch($profile)->onQueue('inbox');
2024-09-07 09:01:50 +00:00
2023-12-08 10:26:32 +00:00
return;
} else {
2024-09-07 09:01:50 +00:00
if (! isset(
2023-12-08 10:26:32 +00:00
$obj['id'],
$this->payload['object'],
$this->payload['object']['id'],
$this->payload['object']['type']
)) {
return;
}
$type = $this->payload['object']['type'];
$typeCheck = in_array($type, ['Person', 'Tombstone', 'Story']);
2024-09-07 09:01:50 +00:00
if (! Helpers::validateUrl($actor) || ! Helpers::validateUrl($obj['id']) || ! $typeCheck) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if (parse_url($obj['id'], PHP_URL_HOST) !== parse_url($actor, PHP_URL_HOST)) {
2023-12-08 10:26:32 +00:00
return;
}
$id = $this->payload['object']['id'];
switch ($type) {
case 'Person':
2024-09-07 09:01:50 +00:00
$profile = Profile::whereRemoteUrl($actor)->first();
if (! $profile || $profile->private_key != null) {
2023-12-08 10:26:32 +00:00
return;
2024-09-07 09:01:50 +00:00
}
DeleteRemoteProfilePipeline::dispatch($profile)->onQueue('inbox');
return;
2023-12-08 10:26:32 +00:00
break;
case 'Tombstone':
2024-09-07 09:01:50 +00:00
$profile = Profile::whereRemoteUrl($actor)->first();
if (! $profile || $profile->private_key != null) {
return;
}
2024-09-07 09:01:50 +00:00
$status = Status::where('object_url', $id)->first();
if (! $status) {
$status = Status::where('url', $id)->first();
if (! $status) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
}
if ($status->profile_id != $profile->id) {
2023-12-08 10:26:32 +00:00
return;
2024-09-07 09:01:50 +00:00
}
if ($status->scope && in_array($status->scope, ['public', 'unlisted', 'private'])) {
if ($status->type && ! in_array($status->type, ['story:reaction', 'story:reply', 'reply'])) {
FeedRemoveRemotePipeline::dispatch($status->id, $status->profile_id)->onQueue('feed');
}
}
RemoteStatusDelete::dispatch($status)->onQueue('high');
return;
2023-12-08 10:26:32 +00:00
break;
case 'Story':
$story = Story::whereObjectId($id)
->first();
2024-09-07 09:01:50 +00:00
if ($story) {
2023-12-08 10:26:32 +00:00
StoryExpire::dispatch($story)->onQueue('story');
}
2024-09-07 09:01:50 +00:00
2023-12-08 10:26:32 +00:00
return;
break;
default:
return;
break;
}
}
2024-09-07 09:01:50 +00:00
2023-12-08 10:26:32 +00:00
}
public function handleLikeActivity()
{
$actor = $this->payload['actor'];
2024-09-07 09:01:50 +00:00
if (! Helpers::validateUrl($actor)) {
2023-12-08 10:26:32 +00:00
return;
}
$profile = self::actorFirstOrCreate($actor);
$obj = $this->payload['object'];
2024-09-07 09:01:50 +00:00
if (! Helpers::validateUrl($obj)) {
2023-12-08 10:26:32 +00:00
return;
}
$status = Helpers::statusFirstOrFetch($obj);
2024-09-07 09:01:50 +00:00
if (! $status || ! $profile) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if (AccountService::blocksDomain($status->profile_id, $profile->domain) == true) {
return;
}
2023-12-08 10:26:32 +00:00
$blocks = UserFilterService::blocks($status->profile_id);
2024-09-07 09:01:50 +00:00
if ($blocks && in_array($profile->id, $blocks)) {
2023-12-08 10:26:32 +00:00
return;
}
$like = Like::firstOrCreate([
'profile_id' => $profile->id,
2024-09-07 09:01:50 +00:00
'status_id' => $status->id,
2023-12-08 10:26:32 +00:00
]);
2024-09-07 09:01:50 +00:00
if ($like->wasRecentlyCreated == true) {
2023-12-08 10:26:32 +00:00
$status->likes_count = $status->likes_count + 1;
$status->save();
LikePipeline::dispatch($like);
}
}
2024-09-07 09:01:50 +00:00
public function handleRejectActivity() {}
2023-12-08 10:26:32 +00:00
public function handleUndoActivity()
{
$actor = $this->payload['actor'];
$profile = self::actorFirstOrCreate($actor);
$obj = $this->payload['object'];
2024-09-07 09:01:50 +00:00
if (! $profile) {
2023-12-08 10:26:32 +00:00
return;
}
// TODO: Some implementations do not inline the object, skip for now
2024-09-07 09:01:50 +00:00
if (! $obj || ! is_array($obj) || ! isset($obj['type'])) {
2023-12-08 10:26:32 +00:00
return;
}
switch ($obj['type']) {
case 'Accept':
break;
case 'Announce':
2024-09-07 09:01:50 +00:00
if (is_array($obj) && isset($obj['object'])) {
2023-12-08 10:26:32 +00:00
$obj = $obj['object'];
}
2024-09-07 09:01:50 +00:00
if (! is_string($obj)) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if (Helpers::validateLocalUrl($obj)) {
2023-12-08 10:26:32 +00:00
$parsedId = last(explode('/', $obj));
$status = Status::find($parsedId);
} else {
$status = Status::whereUri($obj)->first();
}
2024-09-07 09:01:50 +00:00
if (! $status) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if (AccountService::blocksDomain($status->profile_id, $profile->domain) == true) {
return;
}
2023-12-08 10:26:32 +00:00
FeedRemoveRemotePipeline::dispatch($status->id, $status->profile_id)->onQueue('feed');
Status::whereProfileId($profile->id)
->whereReblogOfId($status->id)
->delete();
ReblogService::removePostReblog($profile->id, $status->id);
Notification::whereProfileId($status->profile_id)
->whereActorId($profile->id)
->whereAction('share')
->whereItemId($status->reblog_of_id)
->whereItemType('App\Status')
->forceDelete();
break;
case 'Block':
break;
case 'Follow':
$following = self::actorFirstOrCreate($obj['object']);
2024-09-07 09:01:50 +00:00
if (! $following) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if (AccountService::blocksDomain($following->id, $profile->domain) == true) {
return;
}
2023-12-08 10:26:32 +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();
FollowerService::remove($profile->id, $following->id);
break;
case 'Like':
$objectUri = $obj['object'];
2024-09-07 09:01:50 +00:00
if (! is_string($objectUri)) {
if (is_array($objectUri) && isset($objectUri['id']) && is_string($objectUri['id'])) {
2023-12-08 10:26:32 +00:00
$objectUri = $objectUri['id'];
} else {
return;
}
}
$status = Helpers::statusFirstOrFetch($objectUri);
2024-09-07 09:01:50 +00:00
if (! $status) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if (AccountService::blocksDomain($status->profile_id, $profile->domain) == true) {
return;
}
2023-12-08 10:26:32 +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;
}
2024-09-07 09:01:50 +00:00
2023-12-08 10:26:32 +00:00
}
public function handleViewActivity()
{
2024-09-07 09:01:50 +00:00
if (! isset(
2023-12-08 10:26:32 +00:00
$this->payload['actor'],
$this->payload['object']
)) {
return;
}
$actor = $this->payload['actor'];
$obj = $this->payload['object'];
2024-09-07 09:01:50 +00:00
if (! Helpers::validateUrl($actor)) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if (! $obj || ! is_array($obj)) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if (! isset($obj['type']) || ! isset($obj['object']) || $obj['type'] != 'Story') {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if (! Helpers::validateLocalUrl($obj['object'])) {
2023-12-08 10:26:32 +00:00
return;
}
$profile = Helpers::profileFetch($actor);
$storyId = Str::of($obj['object'])->explode('/')->last();
$story = Story::whereActive(true)
->whereLocal(true)
->find($storyId);
2024-09-07 09:01:50 +00:00
if (! $story) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if (AccountService::blocksDomain($story->profile_id, $profile->domain) == true) {
return;
}
2024-09-07 09:01:50 +00:00
if (! FollowerService::follows($profile->id, $story->profile_id)) {
2023-12-08 10:26:32 +00:00
return;
}
$view = StoryView::firstOrCreate([
'story_id' => $story->id,
2024-09-07 09:01:50 +00:00
'profile_id' => $profile->id,
2023-12-08 10:26:32 +00:00
]);
2024-09-07 09:01:50 +00:00
if ($view->wasRecentlyCreated == true) {
2023-12-08 10:26:32 +00:00
$story->view_count++;
$story->save();
}
}
public function handleStoryReactionActivity()
{
2024-09-07 09:01:50 +00:00
if (! isset(
2023-12-08 10:26:32 +00:00
$this->payload['actor'],
$this->payload['id'],
$this->payload['inReplyTo'],
$this->payload['content']
)) {
return;
}
$id = $this->payload['id'];
$actor = $this->payload['actor'];
$storyUrl = $this->payload['inReplyTo'];
$to = $this->payload['to'];
$text = Purify::clean($this->payload['content']);
2024-09-07 09:01:50 +00:00
if (parse_url($id, PHP_URL_HOST) !== parse_url($actor, PHP_URL_HOST)) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if (! Helpers::validateUrl($id) || ! Helpers::validateUrl($actor)) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if (! Helpers::validateLocalUrl($storyUrl)) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if (! Helpers::validateLocalUrl($to)) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if (Status::whereObjectUrl($id)->exists()) {
2023-12-08 10:26:32 +00:00
return;
}
$storyId = Str::of($storyUrl)->explode('/')->last();
$targetProfile = Helpers::profileFetch($to);
$story = Story::whereProfileId($targetProfile->id)
->find($storyId);
2024-09-07 09:01:50 +00:00
if (! $story) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if ($story->can_react == false) {
2023-12-08 10:26:32 +00:00
return;
}
$actorProfile = Helpers::profileFetch($actor);
2024-09-07 09:01:50 +00:00
if (AccountService::blocksDomain($targetProfile->id, $actorProfile->domain) == true) {
return;
}
2024-09-07 09:01:50 +00:00
if (! FollowerService::follows($actorProfile->id, $targetProfile->id)) {
2023-12-08 10:26:32 +00:00
return;
}
$url = $id;
2024-09-07 09:01:50 +00:00
if (str_ends_with($url, '/activity')) {
2023-12-08 10:26:32 +00:00
$url = substr($url, 0, -9);
}
$status = new Status;
$status->profile_id = $actorProfile->id;
$status->type = 'story:reaction';
$status->url = $url;
$status->uri = $url;
$status->object_url = $url;
$status->caption = $text;
$status->rendered = $text;
$status->scope = 'direct';
$status->visibility = 'direct';
$status->in_reply_to_profile_id = $story->profile_id;
$status->entities = json_encode([
'story_id' => $story->id,
2024-09-07 09:01:50 +00:00
'reaction' => $text,
2023-12-08 10:26:32 +00:00
]);
$status->save();
$dm = new DirectMessage;
$dm->to_id = $story->profile_id;
$dm->from_id = $actorProfile->id;
$dm->type = 'story:react';
$dm->status_id = $status->id;
$dm->meta = json_encode([
'story_username' => $targetProfile->username,
'story_actor_username' => $actorProfile->username,
'story_id' => $story->id,
'story_media_url' => url(Storage::url($story->path)),
2024-09-07 09:01:50 +00:00
'reaction' => $text,
2023-12-08 10:26:32 +00:00
]);
$dm->save();
Conversation::updateOrInsert(
[
'to_id' => $story->profile_id,
2024-09-07 09:01:50 +00:00
'from_id' => $actorProfile->id,
2023-12-08 10:26:32 +00:00
],
[
'type' => 'story:react',
'status_id' => $status->id,
'dm_id' => $dm->id,
2024-09-07 09:01:50 +00:00
'is_hidden' => false,
2023-12-08 10:26:32 +00:00
]
);
$n = new Notification;
$n->profile_id = $dm->to_id;
$n->actor_id = $dm->from_id;
$n->item_id = $dm->id;
$n->item_type = 'App\DirectMessage';
$n->action = 'story:react';
$n->save();
}
public function handleStoryReplyActivity()
{
2024-09-07 09:01:50 +00:00
if (! isset(
2023-12-08 10:26:32 +00:00
$this->payload['actor'],
$this->payload['id'],
$this->payload['inReplyTo'],
$this->payload['content']
)) {
return;
}
$id = $this->payload['id'];
$actor = $this->payload['actor'];
$storyUrl = $this->payload['inReplyTo'];
$to = $this->payload['to'];
$text = Purify::clean($this->payload['content']);
2024-09-07 09:01:50 +00:00
if (parse_url($id, PHP_URL_HOST) !== parse_url($actor, PHP_URL_HOST)) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if (! Helpers::validateUrl($id) || ! Helpers::validateUrl($actor)) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if (! Helpers::validateLocalUrl($storyUrl)) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if (! Helpers::validateLocalUrl($to)) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if (Status::whereObjectUrl($id)->exists()) {
2023-12-08 10:26:32 +00:00
return;
}
$storyId = Str::of($storyUrl)->explode('/')->last();
$targetProfile = Helpers::profileFetch($to);
$story = Story::whereProfileId($targetProfile->id)
->find($storyId);
2024-09-07 09:01:50 +00:00
if (! $story) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if ($story->can_react == false) {
2023-12-08 10:26:32 +00:00
return;
}
$actorProfile = Helpers::profileFetch($actor);
2024-09-07 09:01:50 +00:00
if (AccountService::blocksDomain($targetProfile->id, $actorProfile->domain) == true) {
return;
}
2024-09-07 09:01:50 +00:00
if (! FollowerService::follows($actorProfile->id, $targetProfile->id)) {
2023-12-08 10:26:32 +00:00
return;
}
$url = $id;
2024-09-07 09:01:50 +00:00
if (str_ends_with($url, '/activity')) {
2023-12-08 10:26:32 +00:00
$url = substr($url, 0, -9);
}
$status = new Status;
$status->profile_id = $actorProfile->id;
$status->type = 'story:reply';
$status->caption = $text;
$status->rendered = $text;
$status->url = $url;
$status->uri = $url;
$status->object_url = $url;
$status->scope = 'direct';
$status->visibility = 'direct';
$status->in_reply_to_profile_id = $story->profile_id;
$status->entities = json_encode([
'story_id' => $story->id,
2024-09-07 09:01:50 +00:00
'caption' => $text,
2023-12-08 10:26:32 +00:00
]);
$status->save();
$dm = new DirectMessage;
$dm->to_id = $story->profile_id;
$dm->from_id = $actorProfile->id;
$dm->type = 'story:comment';
$dm->status_id = $status->id;
$dm->meta = json_encode([
'story_username' => $targetProfile->username,
'story_actor_username' => $actorProfile->username,
'story_id' => $story->id,
'story_media_url' => url(Storage::url($story->path)),
2024-09-07 09:01:50 +00:00
'caption' => $text,
2023-12-08 10:26:32 +00:00
]);
$dm->save();
Conversation::updateOrInsert(
[
'to_id' => $story->profile_id,
2024-09-07 09:01:50 +00:00
'from_id' => $actorProfile->id,
2023-12-08 10:26:32 +00:00
],
[
'type' => 'story:comment',
'status_id' => $status->id,
'dm_id' => $dm->id,
2024-09-07 09:01:50 +00:00
'is_hidden' => false,
2023-12-08 10:26:32 +00:00
]
);
$n = new Notification;
$n->profile_id = $dm->to_id;
$n->actor_id = $dm->from_id;
$n->item_id = $dm->id;
$n->item_type = 'App\DirectMessage';
$n->action = 'story:comment';
$n->save();
}
public function handleFlagActivity()
{
2024-09-07 09:01:50 +00:00
if (! isset(
2023-12-08 10:26:32 +00:00
$this->payload['id'],
$this->payload['type'],
$this->payload['actor'],
$this->payload['object']
)) {
return;
}
$id = $this->payload['id'];
$actor = $this->payload['actor'];
2024-09-07 09:01:50 +00:00
if (Helpers::validateLocalUrl($id) || parse_url($id, PHP_URL_HOST) !== parse_url($actor, PHP_URL_HOST)) {
2023-12-08 10:26:32 +00:00
return;
}
$content = null;
2024-09-07 09:01:50 +00:00
if (isset($this->payload['content'])) {
if (strlen($this->payload['content']) > 5000) {
$content = Purify::clean(substr($this->payload['content'], 0, 5000).' ... (truncated message due to exceeding max length)');
} else {
$content = Purify::clean($this->payload['content']);
}
}
2023-12-08 10:26:32 +00:00
$object = $this->payload['object'];
2024-09-07 09:01:50 +00:00
if (empty($object) || (! is_array($object) && ! is_string($object))) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if (is_array($object) && count($object) > 100) {
2023-12-08 10:26:32 +00:00
return;
}
$objects = collect([]);
$accountId = null;
2024-09-07 09:01:50 +00:00
foreach ($object as $objectUrl) {
if (! Helpers::validateLocalUrl($objectUrl)) {
return;
2023-12-08 10:26:32 +00:00
}
2024-09-07 09:01:50 +00:00
if (str_contains($objectUrl, '/users/')) {
2023-12-08 10:26:32 +00:00
$username = last(explode('/', $objectUrl));
$profileId = Profile::whereUsername($username)->first();
2024-09-07 09:01:50 +00:00
if ($profileId) {
2023-12-08 10:26:32 +00:00
$accountId = $profileId->id;
}
2024-09-07 09:01:50 +00:00
} elseif (str_contains($objectUrl, '/p/')) {
2023-12-08 10:26:32 +00:00
$postId = last(explode('/', $objectUrl));
$objects->push($postId);
} else {
continue;
}
}
2024-09-07 09:01:50 +00:00
if (! $accountId && ! $objects->count()) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if ($objects->count()) {
$obc = $objects->count();
2024-09-07 09:01:50 +00:00
if ($obc > 25) {
if ($obc > 30) {
return;
} else {
$objLimit = $objects->take(20);
$objects = collect($objLimit->all());
$obc = $objects->count();
}
}
$count = Status::whereProfileId($accountId)->find($objects)->count();
2024-09-07 09:01:50 +00:00
if ($obc !== $count) {
return;
}
}
2023-12-08 10:26:32 +00:00
$instanceHost = parse_url($id, PHP_URL_HOST);
$instance = Instance::updateOrCreate([
2024-09-07 09:01:50 +00:00
'domain' => $instanceHost,
2023-12-08 10:26:32 +00:00
]);
$report = new RemoteReport;
$report->status_ids = $objects->toArray();
$report->comment = $content;
$report->account_id = $accountId;
$report->uri = $id;
$report->instance_id = $instance->id;
$report->report_meta = [
'actor' => $actor,
2024-09-07 09:01:50 +00:00
'object' => $object,
2023-12-08 10:26:32 +00:00
];
$report->save();
}
public function handleUpdateActivity()
{
$activity = $this->payload['object'];
2024-09-07 09:01:50 +00:00
if (! isset($activity['type'], $activity['id'])) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if (! Helpers::validateUrl($activity['id'])) {
2023-12-08 10:26:32 +00:00
return;
}
2024-09-07 09:01:50 +00:00
if ($activity['type'] === 'Note') {
if (Status::whereObjectUrl($activity['id'])->exists()) {
2023-12-08 10:26:32 +00:00
StatusRemoteUpdatePipeline::dispatch($activity);
}
2024-09-07 09:01:50 +00:00
} elseif ($activity['type'] === 'Person') {
if (UpdatePersonValidator::validate($this->payload)) {
2023-12-08 10:26:32 +00:00
HandleUpdateActivity::dispatch($this->payload)->onQueue('low');
}
}
}
2024-09-07 08:49:33 +00:00
public function handleMoveActivity()
{
$actor = $this->payload['actor'];
$activity = $this->payload['object'];
$target = $this->payload['target'];
2024-09-07 09:01:50 +00:00
if (
! Helpers::validateUrl($actor) ||
! Helpers::validateUrl($activity) ||
! Helpers::validateUrl($target)
2024-09-07 08:49:33 +00:00
) {
return;
}
Bus::chain([
2024-09-07 09:14:56 +00:00
new ProcessMovePipeline($target, $activity),
new MoveMigrateFollowersPipeline($target, $activity),
new UnfollowLegacyAccountMovePipeline($target, $activity),
new CleanupLegacyAccountMovePipeline($target, $activity),
])
->catch(function (Throwable $e) {
Log::error($e);
})
->onQueue('move')
2024-09-11 08:57:18 +00:00
->delay(now()->addMinutes(random_int(1, 3)))
->dispatch();
2024-09-07 08:49:33 +00:00
}
2018-08-28 03:07:36 +00:00
}