12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427 |
- <?php
- namespace App\Util\ActivityPub;
- use App\DirectMessage;
- use App\Follower;
- use App\FollowRequest;
- use App\Instance;
- use App\Jobs\DeletePipeline\DeleteRemoteProfilePipeline;
- 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;
- use App\Jobs\PushNotificationPipeline\MentionPushNotifyPipeline;
- use App\Jobs\StatusPipeline\RemoteStatusDelete;
- use App\Jobs\StatusPipeline\StatusRemoteUpdatePipeline;
- use App\Jobs\StoryPipeline\StoryExpire;
- use App\Jobs\StoryPipeline\StoryFetch;
- 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;
- use App\Services\NotificationAppGatewayService;
- use App\Services\PollService;
- use App\Services\PushNotificationService;
- use App\Services\ReblogService;
- use App\Services\RelationshipService;
- use App\Services\UserFilterService;
- use App\Status;
- use App\Story;
- use App\StoryView;
- use App\UserFilter;
- 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;
- use App\Util\ActivityPub\Validator\MoveValidator;
- use App\Util\ActivityPub\Validator\RejectValidator;
- use App\Util\ActivityPub\Validator\UpdatePersonValidator;
- use Cache;
- use Illuminate\Support\Facades\Bus;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Str;
- use Purify;
- use Storage;
- use Throwable;
- class Inbox
- {
- protected $headers;
- protected $profile;
- protected $payload;
- protected $logger;
- public function __construct($headers, $profile, $payload)
- {
- $this->headers = $headers;
- $this->profile = $profile;
- $this->payload = $payload;
- }
- public function handle()
- {
- $this->handleVerb();
- }
- public function handleVerb()
- {
- $verb = (string) $this->payload['type'];
- switch ($verb) {
- case 'Add':
- $this->handleAddActivity();
- break;
- case 'Create':
- $this->handleCreateActivity();
- break;
- case 'Follow':
- if (FollowValidator::validate($this->payload) == false) {
- return;
- }
- $this->handleFollowActivity();
- break;
- case 'Announce':
- if (AnnounceValidator::validate($this->payload) == false) {
- return;
- }
- $this->handleAnnounceActivity();
- break;
- case 'Accept':
- if (AcceptValidator::validate($this->payload) == false) {
- return;
- }
- $this->handleAcceptActivity();
- break;
- case 'Delete':
- $this->handleDeleteActivity();
- break;
- case 'Like':
- if (LikeValidator::validate($this->payload) == false) {
- return;
- }
- $this->handleLikeActivity();
- break;
- case 'Reject':
- if (RejectValidator::validate($this->payload) == false) {
- return;
- }
- $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;
- case 'Move':
- if (MoveValidator::validate($this->payload) == false) {
- Log::info('[AP][INBOX][MOVE] VALIDATE_FAILURE '.json_encode($this->payload));
- return;
- }
- $this->handleMoveActivity();
- break;
- default:
- // TODO: decide how to handle invalid verbs.
- break;
- }
- }
- 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::profileFetch($actorUrl);
- }
- public function handleAddActivity()
- {
- // stories ;)
- if (! isset(
- $this->payload['actor'],
- $this->payload['object']
- )) {
- return;
- }
- $actor = $this->payload['actor'];
- $obj = $this->payload['object'];
- if (! Helpers::validateUrl($actor)) {
- return;
- }
- if (! isset($obj['type'])) {
- return;
- }
- switch ($obj['type']) {
- case 'Story':
- StoryFetch::dispatch($this->payload);
- break;
- }
- }
- public function handleCreateActivity()
- {
- $activity = $this->payload['object'];
- if (config('autospam.live_filters.enabled')) {
- $filters = config('autospam.live_filters.filters');
- if (! empty($filters) && isset($activity['content']) && ! empty($activity['content']) && strlen($filters) > 3) {
- $filters = array_map('trim', explode(',', $filters));
- $content = $activity['content'];
- foreach ($filters as $filter) {
- $filter = trim(strtolower($filter));
- if (! $filter || ! strlen($filter)) {
- continue;
- }
- if (str_contains(strtolower($content), $filter)) {
- return;
- }
- }
- }
- }
- $actor = $this->actorFirstOrCreate($this->payload['actor']);
- if (! $actor || $actor->domain == null) {
- return;
- }
- if (! isset($activity['to'])) {
- return;
- }
- $to = isset($activity['to']) ? $activity['to'] : [];
- $cc = isset($activity['cc']) ? $activity['cc'] : [];
- if ($activity['type'] == 'Question') {
- // $this->handlePollCreate();
- return;
- }
- if (is_array($to) &&
- is_array($cc) &&
- count($to) == 1 &&
- count($cc) == 0 &&
- parse_url($to[0], PHP_URL_HOST) == config('pixelfed.domain.app')
- ) {
- $this->handleDirectMessage();
- return;
- }
- if ($activity['type'] == 'Note' && ! empty($activity['inReplyTo'])) {
- $this->handleNoteReply();
- } elseif ($activity['type'] == 'Note' && ! empty($activity['attachment'])) {
- if (! $this->verifyNoteAttachment()) {
- return;
- }
- $this->handleNoteCreate();
- }
- }
- public function handleNoteReply()
- {
- $activity = $this->payload['object'];
- $actor = $this->actorFirstOrCreate($this->payload['actor']);
- if (! $actor || $actor->domain == null) {
- return;
- }
- $inReplyTo = $activity['inReplyTo'];
- $url = isset($activity['url']) ? $activity['url'] : $activity['id'];
- Helpers::statusFirstOrFetch($url, true);
- }
- public function handlePollCreate()
- {
- $activity = $this->payload['object'];
- $actor = $this->actorFirstOrCreate($this->payload['actor']);
- if (! $actor || $actor->domain == null) {
- return;
- }
- $url = isset($activity['url']) ? $activity['url'] : $activity['id'];
- Helpers::statusFirstOrFetch($url);
- }
- public function handleNoteCreate()
- {
- $activity = $this->payload['object'];
- $actor = $this->actorFirstOrCreate($this->payload['actor']);
- if (! $actor || $actor->domain == null) {
- return;
- }
- if (isset($activity['inReplyTo']) &&
- isset($activity['name']) &&
- ! isset($activity['content']) &&
- ! isset($activity['attachment']) &&
- Helpers::validateLocalUrl($activity['inReplyTo'])
- ) {
- $this->handlePollVote();
- return;
- }
- if ($actor->followers_count == 0) {
- if (config('federation.activitypub.ingest.store_notes_without_followers')) {
- } elseif (FollowerService::followerCount($actor->id, true) == 0) {
- return;
- }
- }
- $hasUrl = isset($activity['url']);
- $url = isset($activity['url']) ? $activity['url'] : $activity['id'];
- if ($hasUrl) {
- if (Status::whereUri($url)->exists()) {
- return;
- }
- } else {
- if (Status::whereObjectUrl($url)->exists()) {
- return;
- }
- }
- Helpers::storeStatus(
- $url,
- $actor,
- $activity
- );
- }
- public function handlePollVote()
- {
- $activity = $this->payload['object'];
- $actor = $this->actorFirstOrCreate($this->payload['actor']);
- if (! $actor) {
- return;
- }
- $status = Helpers::statusFetch($activity['inReplyTo']);
- if (! $status) {
- return;
- }
- $poll = $status->poll;
- if (! $poll) {
- return;
- }
- if (now()->gt($poll->expires_at)) {
- return;
- }
- $choices = $poll->poll_options;
- $choice = array_search($activity['name'], $choices);
- if ($choice === false) {
- return;
- }
- if (PollVote::whereStatusId($status->id)->whereProfileId($actor->id)->exists()) {
- 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();
- if (! $actor || in_array($actor->id, $profile->blockedIds()->toArray())) {
- return;
- }
- if (AccountService::blocksDomain($profile->id, $actor->domain) == true) {
- return;
- }
- $msg = Purify::clean($activity['content']);
- $msgText = strip_tags($msg);
- if (Str::startsWith($msgText, '@'.$profile->username)) {
- $len = strlen('@'.$profile->username);
- $msgText = substr($msgText, $len + 1);
- }
- 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->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();
- $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,
- 'from_id' => $actor->id,
- ],
- [
- 'type' => 'text',
- 'status_id' => $status->id,
- 'dm_id' => $dm->id,
- 'is_hidden' => $hidden,
- ]
- );
- if (count($activity['attachment'])) {
- $photos = 0;
- $videos = 0;
- $allowed = explode(',', config_cache('pixelfed.media_types'));
- $activity['attachment'] = array_slice($activity['attachment'], 0, config_cache('pixelfed.max_album_length'));
- foreach ($activity['attachment'] as $a) {
- $type = $a['mediaType'];
- $url = $a['url'];
- $valid = Helpers::validateUrl($url);
- 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();
- 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();
- }
- }
- if (filter_var($msgText, FILTER_VALIDATE_URL)) {
- if (Helpers::validateUrl($msgText)) {
- $dm->type = 'link';
- $dm->meta = [
- 'domain' => parse_url($msgText, PHP_URL_HOST),
- 'local' => parse_url($msgText, PHP_URL_HOST) ==
- 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->item_id = $dm->id;
- $notification->item_type = "App\DirectMessage";
- $notification->save();
- 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');
- }
- }
- }
- }
- }
- public function handleFollowActivity()
- {
- $actor = $this->actorFirstOrCreate($this->payload['actor']);
- $target = $this->actorFirstOrCreate($this->payload['object']);
- if (! $actor || ! $target) {
- return;
- }
- if ($actor->domain == null || $target->domain !== null) {
- return;
- }
- if (AccountService::blocksDomain($target->id, $actor->domain) == true) {
- return;
- }
- if (
- Follower::whereProfileId($actor->id)
- ->whereFollowingId($target->id)
- ->exists() ||
- FollowRequest::whereFollowerId($actor->id)
- ->whereFollowingId($target->id)
- ->exists()
- ) {
- return;
- }
- $blocks = UserFilterService::blocks($target->id);
- if ($blocks && in_array($actor->id, $blocks)) {
- return;
- }
- if ($target->is_private == true) {
- FollowRequest::updateOrCreate([
- 'follower_id' => $actor->id,
- 'following_id' => $target->id,
- ], [
- 'activity' => collect($this->payload)->only(['id', 'actor', 'object', 'type'])->toArray(),
- ]);
- } 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',
- '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(),
- ],
- ];
- 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'];
- if (! $actor || $actor->domain == null) {
- return;
- }
- $parent = Helpers::statusFetch($activity);
- if (! $parent || empty($parent)) {
- return;
- }
- if (AccountService::blocksDomain($parent->profile_id, $actor->domain) == true) {
- return;
- }
- $blocks = UserFilterService::blocks($parent->profile_id);
- if ($blocks && in_array($actor->id, $blocks)) {
- return;
- }
- $status = Status::firstOrCreate([
- 'profile_id' => $actor->id,
- 'reblog_of_id' => $parent->id,
- 'type' => 'share',
- 'local' => false,
- ]);
- 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'];
- if ($type !== 'Follow') {
- return;
- }
- $actor = Helpers::validateLocalUrl($actor);
- $target = Helpers::validateUrl($obj);
- if (! $actor || ! $target) {
- return;
- }
- $actor = Helpers::profileFetch($actor);
- $target = Helpers::profileFetch($target);
- if (! $actor || ! $target) {
- return;
- }
- if (AccountService::blocksDomain($target->id, $actor->domain) == true) {
- return;
- }
- $request = FollowRequest::whereFollowerId($actor->id)
- ->whereFollowingId($target->id)
- ->whereIsRejected(false)
- ->first();
- if (! $request) {
- return;
- }
- $follower = Follower::firstOrCreate([
- 'profile_id' => $actor->id,
- 'following_id' => $target->id,
- ]);
- FollowPipeline::dispatch($follower)->onQueue('high');
- RelationshipService::refresh($actor->id, $target->id);
- Cache::forget('profile:following:'.$target->id);
- Cache::forget('profile:followers:'.$target->id);
- Cache::forget('profile:following:'.$actor->id);
- Cache::forget('profile:followers:'.$actor->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);
- AccountService::del($actor->id);
- AccountService::del($target->id);
- RelationshipService::get($actor->id, $target->id);
- $request->delete();
- }
- public function handleDeleteActivity()
- {
- if (! isset(
- $this->payload['actor'],
- $this->payload['object']
- )) {
- return;
- }
- $actor = $this->payload['actor'];
- $obj = $this->payload['object'];
- if (is_string($obj) == true && $actor == $obj && Helpers::validateUrl($obj)) {
- $profile = Profile::whereRemoteUrl($obj)->first();
- if (! $profile || $profile->private_key != null) {
- return;
- }
- DeleteRemoteProfilePipeline::dispatch($profile)->onQueue('inbox');
- return;
- } else {
- if (! isset(
- $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']);
- if (! Helpers::validateUrl($actor) || ! Helpers::validateUrl($obj['id']) || ! $typeCheck) {
- return;
- }
- 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;
- }
- DeleteRemoteProfilePipeline::dispatch($profile)->onQueue('inbox');
- return;
- break;
- case 'Tombstone':
- $profile = Profile::whereRemoteUrl($actor)->first();
- if (! $profile || $profile->private_key != null) {
- return;
- }
- $status = Status::where('object_url', $id)->first();
- if (! $status) {
- $status = Status::where('url', $id)->first();
- if (! $status) {
- return;
- }
- }
- if ($status->profile_id != $profile->id) {
- return;
- }
- 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;
- break;
- case 'Story':
- $story = Story::whereObjectId($id)
- ->first();
- if ($story) {
- StoryExpire::dispatch($story)->onQueue('story');
- }
- return;
- break;
- default:
- return;
- break;
- }
- }
- }
- public function handleLikeActivity()
- {
- $actor = $this->payload['actor'];
- if (! Helpers::validateUrl($actor)) {
- return;
- }
- $profile = self::actorFirstOrCreate($actor);
- $obj = $this->payload['object'];
- if (! Helpers::validateUrl($obj)) {
- return;
- }
- $status = Helpers::statusFirstOrFetch($obj);
- if (! $status || ! $profile) {
- return;
- }
- if (AccountService::blocksDomain($status->profile_id, $profile->domain) == true) {
- return;
- }
- $blocks = UserFilterService::blocks($status->profile_id);
- if ($blocks && in_array($profile->id, $blocks)) {
- return;
- }
- $like = Like::firstOrCreate([
- 'profile_id' => $profile->id,
- 'status_id' => $status->id,
- ]);
- if ($like->wasRecentlyCreated == true) {
- $status->likes_count = $status->likes_count + 1;
- $status->save();
- LikePipeline::dispatch($like);
- }
- }
- public function handleRejectActivity()
- {
- $actorUrl = $this->payload['actor'];
- $obj = $this->payload['object'];
- $profileUrl = $obj['actor'];
- if (! Helpers::validateUrl($actorUrl) || ! Helpers::validateLocalUrl($profileUrl)) {
- return;
- }
- $actor = Helpers::profileFetch($actorUrl);
- $profile = Helpers::profileFetch($profileUrl);
- FollowRequest::whereFollowerId($profile->id)->whereFollowingId($actor->id)->forceDelete();
- RelationshipService::refresh($actor->id, $profile->id);
- }
- public function handleUndoActivity()
- {
- $actor = $this->payload['actor'];
- $profile = self::actorFirstOrCreate($actor);
- $obj = $this->payload['object'];
- if (! $profile) {
- return;
- }
- // TODO: Some implementations do not inline the object, skip for now
- if (! $obj || ! is_array($obj) || ! isset($obj['type'])) {
- return;
- }
- switch ($obj['type']) {
- case 'Accept':
- break;
- case 'Announce':
- if (is_array($obj) && isset($obj['object'])) {
- $obj = $obj['object'];
- }
- if (! is_string($obj)) {
- return;
- }
- if (Helpers::validateLocalUrl($obj)) {
- $parsedId = last(explode('/', $obj));
- $status = Status::find($parsedId);
- } else {
- $status = Status::whereUri($obj)->first();
- }
- if (! $status) {
- return;
- }
- if (AccountService::blocksDomain($status->profile_id, $profile->domain) == true) {
- return;
- }
- 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']);
- if (! $following) {
- return;
- }
- if (AccountService::blocksDomain($following->id, $profile->domain) == true) {
- return;
- }
- 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'];
- if (! is_string($objectUri)) {
- if (is_array($objectUri) && isset($objectUri['id']) && is_string($objectUri['id'])) {
- $objectUri = $objectUri['id'];
- } else {
- return;
- }
- }
- $status = Helpers::statusFirstOrFetch($objectUri);
- if (! $status) {
- return;
- }
- if (AccountService::blocksDomain($status->profile_id, $profile->domain) == true) {
- return;
- }
- 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;
- }
- }
- public function handleViewActivity()
- {
- if (! isset(
- $this->payload['actor'],
- $this->payload['object']
- )) {
- return;
- }
- $actor = $this->payload['actor'];
- $obj = $this->payload['object'];
- if (! Helpers::validateUrl($actor)) {
- return;
- }
- if (! $obj || ! is_array($obj)) {
- return;
- }
- if (! isset($obj['type']) || ! isset($obj['object']) || $obj['type'] != 'Story') {
- return;
- }
- if (! Helpers::validateLocalUrl($obj['object'])) {
- return;
- }
- $profile = Helpers::profileFetch($actor);
- $storyId = Str::of($obj['object'])->explode('/')->last();
- $story = Story::whereActive(true)
- ->whereLocal(true)
- ->find($storyId);
- if (! $story) {
- return;
- }
- if (AccountService::blocksDomain($story->profile_id, $profile->domain) == true) {
- return;
- }
- if (! FollowerService::follows($profile->id, $story->profile_id)) {
- return;
- }
- $view = StoryView::firstOrCreate([
- 'story_id' => $story->id,
- 'profile_id' => $profile->id,
- ]);
- if ($view->wasRecentlyCreated == true) {
- $story->view_count++;
- $story->save();
- }
- }
- public function handleStoryReactionActivity()
- {
- if (! isset(
- $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']);
- if (parse_url($id, PHP_URL_HOST) !== parse_url($actor, PHP_URL_HOST)) {
- return;
- }
- if (! Helpers::validateUrl($id) || ! Helpers::validateUrl($actor)) {
- return;
- }
- if (! Helpers::validateLocalUrl($storyUrl)) {
- return;
- }
- if (! Helpers::validateLocalUrl($to)) {
- return;
- }
- if (Status::whereObjectUrl($id)->exists()) {
- return;
- }
- $storyId = Str::of($storyUrl)->explode('/')->last();
- $targetProfile = Helpers::profileFetch($to);
- $story = Story::whereProfileId($targetProfile->id)
- ->find($storyId);
- if (! $story) {
- return;
- }
- if ($story->can_react == false) {
- return;
- }
- $actorProfile = Helpers::profileFetch($actor);
- if (AccountService::blocksDomain($targetProfile->id, $actorProfile->domain) == true) {
- return;
- }
- if (! FollowerService::follows($actorProfile->id, $targetProfile->id)) {
- return;
- }
- $url = $id;
- if (str_ends_with($url, '/activity')) {
- $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->scope = 'direct';
- $status->visibility = 'direct';
- $status->in_reply_to_profile_id = $story->profile_id;
- $status->entities = json_encode([
- 'story_id' => $story->id,
- 'reaction' => $text,
- ]);
- $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)),
- 'reaction' => $text,
- ]);
- $dm->save();
- Conversation::updateOrInsert(
- [
- 'to_id' => $story->profile_id,
- 'from_id' => $actorProfile->id,
- ],
- [
- 'type' => 'story:react',
- 'status_id' => $status->id,
- 'dm_id' => $dm->id,
- 'is_hidden' => false,
- ]
- );
- $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()
- {
- if (! isset(
- $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']);
- if (parse_url($id, PHP_URL_HOST) !== parse_url($actor, PHP_URL_HOST)) {
- return;
- }
- if (! Helpers::validateUrl($id) || ! Helpers::validateUrl($actor)) {
- return;
- }
- if (! Helpers::validateLocalUrl($storyUrl)) {
- return;
- }
- if (! Helpers::validateLocalUrl($to)) {
- return;
- }
- if (Status::whereObjectUrl($id)->exists()) {
- return;
- }
- $storyId = Str::of($storyUrl)->explode('/')->last();
- $targetProfile = Helpers::profileFetch($to);
- $story = Story::whereProfileId($targetProfile->id)
- ->find($storyId);
- if (! $story) {
- return;
- }
- if ($story->can_react == false) {
- return;
- }
- $actorProfile = Helpers::profileFetch($actor);
- if (AccountService::blocksDomain($targetProfile->id, $actorProfile->domain) == true) {
- return;
- }
- if (! FollowerService::follows($actorProfile->id, $targetProfile->id)) {
- return;
- }
- $url = $id;
- if (str_ends_with($url, '/activity')) {
- $url = substr($url, 0, -9);
- }
- $status = new Status;
- $status->profile_id = $actorProfile->id;
- $status->type = 'story:reply';
- $status->caption = $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,
- 'caption' => $text,
- ]);
- $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)),
- 'caption' => $text,
- ]);
- $dm->save();
- Conversation::updateOrInsert(
- [
- 'to_id' => $story->profile_id,
- 'from_id' => $actorProfile->id,
- ],
- [
- 'type' => 'story:comment',
- 'status_id' => $status->id,
- 'dm_id' => $dm->id,
- 'is_hidden' => false,
- ]
- );
- $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()
- {
- if (! isset(
- $this->payload['id'],
- $this->payload['type'],
- $this->payload['actor'],
- $this->payload['object']
- )) {
- return;
- }
- $id = $this->payload['id'];
- $actor = $this->payload['actor'];
- if (Helpers::validateLocalUrl($id) || parse_url($id, PHP_URL_HOST) !== parse_url($actor, PHP_URL_HOST)) {
- return;
- }
- $content = null;
- 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']);
- }
- }
- $object = $this->payload['object'];
- if (empty($object) || (! is_array($object) && ! is_string($object))) {
- return;
- }
- if (is_array($object) && count($object) > 100) {
- return;
- }
- $objects = collect([]);
- $accountId = null;
- foreach ($object as $objectUrl) {
- if (! Helpers::validateLocalUrl($objectUrl)) {
- return;
- }
- if (str_contains($objectUrl, '/users/')) {
- $username = last(explode('/', $objectUrl));
- $profileId = Profile::whereUsername($username)->first();
- if ($profileId) {
- $accountId = $profileId->id;
- }
- } elseif (str_contains($objectUrl, '/p/')) {
- $postId = last(explode('/', $objectUrl));
- $objects->push($postId);
- } else {
- continue;
- }
- }
- if (! $accountId && ! $objects->count()) {
- return;
- }
- if ($objects->count()) {
- $obc = $objects->count();
- 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();
- if ($obc !== $count) {
- return;
- }
- }
- $instanceHost = parse_url($id, PHP_URL_HOST);
- $instance = Instance::updateOrCreate([
- 'domain' => $instanceHost,
- ]);
- $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,
- 'object' => $object,
- ];
- $report->save();
- }
- public function handleUpdateActivity()
- {
- $activity = $this->payload['object'];
- if (! isset($activity['type'], $activity['id'])) {
- return;
- }
- if (! Helpers::validateUrl($activity['id'])) {
- return;
- }
- if ($activity['type'] === 'Note') {
- if (Status::whereObjectUrl($activity['id'])->exists()) {
- StatusRemoteUpdatePipeline::dispatch($activity);
- }
- } elseif ($activity['type'] === 'Person') {
- if (UpdatePersonValidator::validate($this->payload)) {
- HandleUpdateActivity::dispatch($this->payload)->onQueue('low');
- }
- }
- }
- public function handleMoveActivity()
- {
- $actor = $this->payload['actor'];
- $activity = $this->payload['object'];
- $target = $this->payload['target'];
- if (
- ! Helpers::validateUrl($actor) ||
- ! Helpers::validateUrl($activity) ||
- ! Helpers::validateUrl($target)
- ) {
- return;
- }
- Bus::chain([
- 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')
- ->delay(now()->addMinutes(random_int(1, 3)))
- ->dispatch();
- }
- }
|