diff --git a/app/Util/ActivityPub/Helpers.php b/app/Util/ActivityPub/Helpers.php index a5903b22e..bb3114817 100644 --- a/app/Util/ActivityPub/Helpers.php +++ b/app/Util/ActivityPub/Helpers.php @@ -245,129 +245,132 @@ class Helpers { if($local) { $id = (int) last(explode('/', $url)); return Status::whereNotIn('scope', ['draft','archived'])->findOrFail($id); + } + + $cached = Status::whereNotIn('scope', ['draft','archived']) + ->whereUri($url) + ->orWhere('object_url', $url) + ->first(); + + if($cached) { + return $cached; + } + + $res = self::fetchFromUrl($url); + + if(!$res || empty($res)) { + return; + } + + if(isset($res['object'])) { + $activity = $res; } else { - $cached = Status::whereNotIn('scope', ['draft','archived']) - ->whereUri($url) - ->orWhere('object_url', $url) - ->first(); + $activity = ['object' => $res]; + } - if($cached) { - return $cached; + $scope = 'private'; + + $cw = isset($res['sensitive']) ? (bool) $res['sensitive'] : false; + + if(isset($res['to']) == true) { + if(is_array($res['to']) && in_array('https://www.w3.org/ns/activitystreams#Public', $res['to'])) { + $scope = 'public'; } - - $res = self::fetchFromUrl($url); - - if(!$res || empty($res)) { - return; + if(is_string($res['to']) && 'https://www.w3.org/ns/activitystreams#Public' == $res['to']) { + $scope = 'public'; } + } - if(isset($res['object'])) { - $activity = $res; - } else { - $activity = ['object' => $res]; + if(isset($res['cc']) == true) { + if(is_array($res['cc']) && in_array('https://www.w3.org/ns/activitystreams#Public', $res['cc'])) { + $scope = 'unlisted'; } - - $scope = 'private'; - - $cw = isset($res['sensitive']) ? (bool) $res['sensitive'] : false; - - if(isset($res['to']) == true) { - if(is_array($res['to']) && in_array('https://www.w3.org/ns/activitystreams#Public', $res['to'])) { - $scope = 'public'; - } - if(is_string($res['to']) && 'https://www.w3.org/ns/activitystreams#Public' == $res['to']) { - $scope = 'public'; - } + if(is_string($res['cc']) && 'https://www.w3.org/ns/activitystreams#Public' == $res['cc']) { + $scope = 'unlisted'; } + } - if(isset($res['cc']) == true) { - if(is_array($res['cc']) && in_array('https://www.w3.org/ns/activitystreams#Public', $res['cc'])) { - $scope = 'unlisted'; - } - if(is_string($res['cc']) && 'https://www.w3.org/ns/activitystreams#Public' == $res['cc']) { - $scope = 'unlisted'; - } - } - - if(config('costar.enabled') == true) { - $blockedKeywords = config('costar.keyword.block'); - if($blockedKeywords !== null) { - $keywords = config('costar.keyword.block'); - foreach($keywords as $kw) { - if(Str::contains($res['content'], $kw) == true) { - abort(400, 'Invalid object'); - } + if(config('costar.enabled') == true) { + $blockedKeywords = config('costar.keyword.block'); + if($blockedKeywords !== null) { + $keywords = config('costar.keyword.block'); + foreach($keywords as $kw) { + if(Str::contains($res['content'], $kw) == true) { + return; } } - - $unlisted = config('costar.domain.unlisted'); - if(in_array(parse_url($url, PHP_URL_HOST), $unlisted) == true) { - $unlisted = true; - $scope = 'unlisted'; - } else { - $unlisted = false; - } - - $cwDomains = config('costar.domain.cw'); - if(in_array(parse_url($url, PHP_URL_HOST), $cwDomains) == true) { - $cw = true; - } } - $id = isset($res['id']) ? $res['id'] : $url; - - if(!self::validateUrl($id) || - !self::validateUrl($activity['object']['attributedTo']) - ) { - return; - } - - $idDomain = parse_url($id, PHP_URL_HOST); - $urlDomain = parse_url($url, PHP_URL_HOST); - $actorDomain = parse_url($activity['object']['attributedTo'], PHP_URL_HOST); - - if( - $idDomain !== $urlDomain || - $actorDomain !== $urlDomain || - $idDomain !== $actorDomain - ) { - return; - } - - $profile = self::profileFirstOrNew($activity['object']['attributedTo']); - if(isset($activity['object']['inReplyTo']) && !empty($activity['object']['inReplyTo']) && $replyTo == true) { - $reply_to = self::statusFirstOrFetch($activity['object']['inReplyTo'], false); - $reply_to = optional($reply_to)->id; + $unlisted = config('costar.domain.unlisted'); + if(in_array(parse_url($url, PHP_URL_HOST), $unlisted) == true) { + $unlisted = true; + $scope = 'unlisted'; } else { - $reply_to = null; + $unlisted = false; } - $ts = is_array($res['published']) ? $res['published'][0] : $res['published']; - $status = DB::transaction(function() use($profile, $res, $url, $ts, $reply_to, $cw, $scope, $id) { - $status = new Status; - $status->profile_id = $profile->id; - $status->url = isset($res['url']) ? $res['url'] : $url; - $status->uri = isset($res['url']) ? $res['url'] : $url; - $status->object_url = $id; - $status->caption = strip_tags($res['content']); - $status->rendered = Purify::clean($res['content']); - $status->created_at = Carbon::parse($ts); - $status->in_reply_to_id = $reply_to; - $status->local = false; - $status->is_nsfw = $cw; - $status->scope = $scope; - $status->visibility = $scope; - $status->cw_summary = $cw == true && isset($res['summary']) ? - Purify::clean(strip_tags($res['summary'])) : null; - $status->save(); - if($reply_to == null) { - self::importNoteAttachment($res, $status); - } - return $status; - }); - - return $status; + $cwDomains = config('costar.domain.cw'); + if(in_array(parse_url($url, PHP_URL_HOST), $cwDomains) == true) { + $cw = true; + } } + + $id = isset($res['id']) ? $res['id'] : $url; + $idDomain = parse_url($id, PHP_URL_HOST); + $urlDomain = parse_url($url, PHP_URL_HOST); + + if(!self::validateUrl($id)) { + return; + } + + if(isset($activity['object']['attributedTo'])) { + $actorDomain = parse_url($activity['object']['attributedTo'], PHP_URL_HOST); + if(!self::validateUrl($activity['object']['attributedTo']) || + $idDomain !== $actorDomain) + { + return; + } + } + + if( + $idDomain !== $urlDomain || + $actorDomain !== $urlDomain + ) { + return; + } + + $profile = self::profileFirstOrNew($activity['object']['attributedTo']); + if(isset($activity['object']['inReplyTo']) && !empty($activity['object']['inReplyTo']) && $replyTo == true) { + $reply_to = self::statusFirstOrFetch($activity['object']['inReplyTo'], false); + $reply_to = optional($reply_to)->id; + } else { + $reply_to = null; + } + $ts = is_array($res['published']) ? $res['published'][0] : $res['published']; + $status = DB::transaction(function() use($profile, $res, $url, $ts, $reply_to, $cw, $scope, $id) { + $status = new Status; + $status->profile_id = $profile->id; + $status->url = isset($res['url']) ? $res['url'] : $url; + $status->uri = isset($res['url']) ? $res['url'] : $url; + $status->object_url = $id; + $status->caption = strip_tags($res['content']); + $status->rendered = Purify::clean($res['content']); + $status->created_at = Carbon::parse($ts); + $status->in_reply_to_id = $reply_to; + $status->local = false; + $status->is_nsfw = $cw; + $status->scope = $scope; + $status->visibility = $scope; + $status->cw_summary = $cw == true && isset($res['summary']) ? + Purify::clean(strip_tags($res['summary'])) : null; + $status->save(); + if($reply_to == null) { + self::importNoteAttachment($res, $status); + } + return $status; + }); + + return $status; } public static function statusFetch($url)