Forráskód Böngészése

Update AP Helpers

Daniel Supernault 4 éve
szülő
commit
fa0df4004c
1 módosított fájl, 104 hozzáadás és 101 törlés
  1. 104 101
      app/Util/ActivityPub/Helpers.php

+ 104 - 101
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);
-		} else {
-			$cached = Status::whereNotIn('scope', ['draft','archived'])
-				->whereUri($url)
-				->orWhere('object_url', $url)
-				->first();
+		}
 
-			if($cached) {
-				return $cached;
-			}
+		$cached = Status::whereNotIn('scope', ['draft','archived'])
+			->whereUri($url)
+			->orWhere('object_url', $url)
+			->first();
 
-			$res = self::fetchFromUrl($url);
-			
-			if(!$res || empty($res)) {
-				return;
-			}
+		if($cached) {
+			return $cached;
+		}
 
-			if(isset($res['object'])) {
-				$activity = $res;
-			} else {
-				$activity = ['object' => $res];
-			}
+		$res = self::fetchFromUrl($url);
+		
+		if(!$res || empty($res)) {
+			return;
+		}
 
-			$scope = 'private';
-			
-			$cw = isset($res['sensitive']) ? (bool) $res['sensitive'] : false;
+		if(isset($res['object'])) {
+			$activity = $res;
+		} else {
+			$activity = ['object' => $res];
+		}
 
-			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';
-				}
+		$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(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(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;
-				} 
+			$unlisted = config('costar.domain.unlisted');
+			if(in_array(parse_url($url, PHP_URL_HOST), $unlisted) == true) {
+				$unlisted = true;
+				$scope = 'unlisted';
+			} else {
+				$unlisted = false;
 			}
 
-			$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) ||
-			   !self::validateUrl($activity['object']['attributedTo'])
-			) {
-				return;
-			}
+		$id = isset($res['id']) ? $res['id'] : $url;
+		$idDomain = parse_url($id, PHP_URL_HOST);
+		$urlDomain = parse_url($url, PHP_URL_HOST);
 
-			$idDomain = parse_url($id, PHP_URL_HOST);
-			$urlDomain = parse_url($url, PHP_URL_HOST);
-			$actorDomain = parse_url($activity['object']['attributedTo'], PHP_URL_HOST);
+		if(!self::validateUrl($id)) {
+			return;
+		}
 
-			if(
-				$idDomain !== $urlDomain || 
-				$actorDomain !== $urlDomain || 
-				$idDomain !== $actorDomain
-			) {
+		if(isset($activity['object']['attributedTo'])) {
+			$actorDomain = parse_url($activity['object']['attributedTo'], PHP_URL_HOST);
+			if(!self::validateUrl($activity['object']['attributedTo']) ||
+				$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;
-			} 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;
-			});
-
+		if(
+			$idDomain !== $urlDomain || 
+			$actorDomain !== $urlDomain
+		) {
+			return;
+		}
 
-			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)