Update AP Helpers

pull/2571/head
Daniel Supernault 5 years ago
parent 7d376c64ec
commit fa0df4004c
No known key found for this signature in database
GPG Key ID: 0DEF1C662C9033F7

@ -245,129 +245,132 @@ class Helpers {
if($local) { if($local) {
$id = (int) last(explode('/', $url)); $id = (int) last(explode('/', $url));
return Status::whereNotIn('scope', ['draft','archived'])->findOrFail($id); return Status::whereNotIn('scope', ['draft','archived'])->findOrFail($id);
} else { }
$cached = Status::whereNotIn('scope', ['draft','archived'])
->whereUri($url)
->orWhere('object_url', $url)
->first();
if($cached) { $cached = Status::whereNotIn('scope', ['draft','archived'])
return $cached; ->whereUri($url)
} ->orWhere('object_url', $url)
->first();
$res = self::fetchFromUrl($url); if($cached) {
return $cached;
if(!$res || empty($res)) { }
return;
}
if(isset($res['object'])) { $res = self::fetchFromUrl($url);
$activity = $res;
} else { if(!$res || empty($res)) {
$activity = ['object' => $res]; return;
} }
$scope = 'private'; if(isset($res['object'])) {
$activity = $res;
$cw = isset($res['sensitive']) ? (bool) $res['sensitive'] : false; } else {
$activity = ['object' => $res];
}
if(isset($res['to']) == true) { $scope = 'private';
if(is_array($res['to']) && in_array('https://www.w3.org/ns/activitystreams#Public', $res['to'])) {
$scope = 'public'; $cw = isset($res['sensitive']) ? (bool) $res['sensitive'] : false;
}
if(is_string($res['to']) && 'https://www.w3.org/ns/activitystreams#Public' == $res['to']) { if(isset($res['to']) == true) {
$scope = 'public'; 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(isset($res['cc']) == true) { if(isset($res['cc']) == true) {
if(is_array($res['cc']) && in_array('https://www.w3.org/ns/activitystreams#Public', $res['cc'])) { if(is_array($res['cc']) && in_array('https://www.w3.org/ns/activitystreams#Public', $res['cc'])) {
$scope = 'unlisted'; $scope = 'unlisted';
} }
if(is_string($res['cc']) && 'https://www.w3.org/ns/activitystreams#Public' == $res['cc']) { if(is_string($res['cc']) && 'https://www.w3.org/ns/activitystreams#Public' == $res['cc']) {
$scope = 'unlisted'; $scope = 'unlisted';
}
} }
}
if(config('costar.enabled') == true) { if(config('costar.enabled') == true) {
$blockedKeywords = config('costar.keyword.block'); $blockedKeywords = config('costar.keyword.block');
if($blockedKeywords !== null) { if($blockedKeywords !== null) {
$keywords = config('costar.keyword.block'); $keywords = config('costar.keyword.block');
foreach($keywords as $kw) { foreach($keywords as $kw) {
if(Str::contains($res['content'], $kw) == true) { if(Str::contains($res['content'], $kw) == true) {
abort(400, 'Invalid object'); return;
}
} }
} }
}
$unlisted = config('costar.domain.unlisted'); $unlisted = config('costar.domain.unlisted');
if(in_array(parse_url($url, PHP_URL_HOST), $unlisted) == true) { if(in_array(parse_url($url, PHP_URL_HOST), $unlisted) == true) {
$unlisted = true; $unlisted = true;
$scope = 'unlisted'; $scope = 'unlisted';
} else { } else {
$unlisted = false; $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; $cwDomains = config('costar.domain.cw');
if(in_array(parse_url($url, PHP_URL_HOST), $cwDomains) == true) {
$cw = true;
}
}
if(!self::validateUrl($id) || $id = isset($res['id']) ? $res['id'] : $url;
!self::validateUrl($activity['object']['attributedTo']) $idDomain = parse_url($id, PHP_URL_HOST);
) { $urlDomain = parse_url($url, PHP_URL_HOST);
return;
}
$idDomain = parse_url($id, PHP_URL_HOST); if(!self::validateUrl($id)) {
$urlDomain = parse_url($url, PHP_URL_HOST); return;
$actorDomain = parse_url($activity['object']['attributedTo'], PHP_URL_HOST); }
if( if(isset($activity['object']['attributedTo'])) {
$idDomain !== $urlDomain || $actorDomain = parse_url($activity['object']['attributedTo'], PHP_URL_HOST);
$actorDomain !== $urlDomain || if(!self::validateUrl($activity['object']['attributedTo']) ||
$idDomain !== $actorDomain $idDomain !== $actorDomain)
) { {
return; return;
} }
}
$profile = self::profileFirstOrNew($activity['object']['attributedTo']); if(
if(isset($activity['object']['inReplyTo']) && !empty($activity['object']['inReplyTo']) && $replyTo == true) { $idDomain !== $urlDomain ||
$reply_to = self::statusFirstOrFetch($activity['object']['inReplyTo'], false); $actorDomain !== $urlDomain
$reply_to = optional($reply_to)->id; ) {
} else { return;
$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; $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) public static function statusFetch($url)

Loading…
Cancel
Save