Преглед изворни кода

Update ApiV1Controller, fix search v2 entities

Daniel Supernault пре 3 година
родитељ
комит
9dac861ebe

+ 1 - 1
app/Http/Controllers/Api/ApiV1Controller.php

@@ -2513,7 +2513,7 @@ class ApiV1Controller extends Controller
 			'following' => 'nullable'
 		]);
 
-		return $this->json(SearchApiV2Service::query($request));
+		return $this->json(SearchApiV2Service::query($request, true));
 	}
 
 	/**

+ 38 - 22
app/Services/SearchApiV2Service.php

@@ -19,9 +19,11 @@ use App\Services\StatusService;
 class SearchApiV2Service
 {
 	private $query;
+	static $mastodonMode = false;
 
-	public static function query($query)
+	public static function query($query, $mastodonMode = false)
 	{
+		self::$mastodonMode = $mastodonMode;
 		return (new self)->run($query);
 	}
 
@@ -81,6 +83,7 @@ class SearchApiV2Service
 
 	protected function accounts()
 	{
+		$mastodonMode = self::$mastodonMode;
 		$user = request()->user();
 		$limit = $this->query->input('limit') ?? 20;
 		$offset = $this->query->input('offset') ?? 0;
@@ -93,6 +96,7 @@ class SearchApiV2Service
 					->where('followers.profile_id', $user->profile_id);
 			})
 			->where('username', 'like', $query)
+			->orderBy('domain')
 			->orderByDesc('profiles.followers_count')
 			->orderByDesc('followers.created_at')
 			->offset($offset)
@@ -101,8 +105,10 @@ class SearchApiV2Service
 			->filter(function($profile) use ($banned) {
 				return in_array($profile->domain, $banned) == false;
 			})
-			->map(function($res) {
-				return AccountService::get($res['id']);
+			->map(function($res) use($mastodonMode) {
+				return $mastodonMode ?
+				AccountService::getMastodon($res['id']) :
+				AccountService::get($res['id']);
 			})
 			->filter(function($account) {
 				return $account && isset($account['id']);
@@ -114,6 +120,7 @@ class SearchApiV2Service
 
 	protected function hashtags()
 	{
+		$mastodonMode = self::$mastodonMode;
 		$limit = $this->query->input('limit') ?? 20;
 		$offset = $this->query->input('offset') ?? 0;
 		$query = '%' . $this->query->input('q') . '%';
@@ -122,13 +129,18 @@ class SearchApiV2Service
 			->offset($offset)
 			->limit($limit)
 			->get()
-			->map(function($tag) {
-				return [
+			->map(function($tag) use($mastodonMode) {
+				$res = [
 					'name' => $tag->name,
-					'url'  => $tag->url(),
-					'count' => HashtagService::count($tag->id),
-					'history' => []
+					'url'  => $tag->url()
 				];
+
+				if(!$mastodonMode) {
+					$res['history'] = [];
+					$res['count'] = HashtagService::count($tag->id);
+				}
+
+				return $res;
 			});
 	}
 
@@ -140,6 +152,7 @@ class SearchApiV2Service
 
 	protected function statusesById()
 	{
+		$mastodonMode = self::$mastodonMode;
 		$accountId = $this->query->input('account_id');
 		$limit = $this->query->input('limit', 20);
 		$query = '%' . $this->query->input('q') . '%';
@@ -147,8 +160,10 @@ class SearchApiV2Service
 			->whereProfileId($accountId)
 			->limit($limit)
 			->get()
-			->map(function($status) {
-				return StatusService::get($status->id);
+			->map(function($status) use($mastodonMode) {
+				return $mastodonMode ?
+					StatusService::getMastodon($status->id) :
+					StatusService::get($status->id);
 			})
 			->filter(function($status) {
 				return $status && isset($status['account']);
@@ -158,6 +173,7 @@ class SearchApiV2Service
 
 	protected function resolveQuery()
 	{
+		$mastodonMode = self::$mastodonMode;
 		$query = urldecode($this->query->input('q'));
 		if(Helpers::validateLocalUrl($query)) {
 			if(Str::contains($query, '/p/')) {
@@ -177,7 +193,7 @@ class SearchApiV2Service
 
 			if(Str::substrCount($query, '@') == 2) {
 				try {
-					$res = WebfingerService::lookup($query);
+					$res = WebfingerService::lookup($query, $mastodonMode);
 				} catch (\Exception $e) {
 					return $default;
 				}
@@ -209,7 +225,9 @@ class SearchApiV2Service
 							if(!$obj || !isset($obj['id'])) {
 								return $default;
 							}
-							$note = StatusService::get($obj['id']);
+							$note = $mastodonMode ?
+								StatusService::getMastodon($obj['id']) :
+								StatusService::get($obj['id']);
 							if(!$note) {
 								return $default;
 							}
@@ -225,7 +243,9 @@ class SearchApiV2Service
 							if(in_array($obj['domain'], $banned)) {
 								return $default;
 							}
-							$default['accounts'][] = AccountService::get($obj['id']);
+							$default['accounts'][] = $mastodonMode ?
+								AccountService::getMastodon($obj['id']) :
+								AccountService::get($obj['id']);
 							return $default;
 						break;
 
@@ -254,10 +274,7 @@ class SearchApiV2Service
 	{
 		$query = urldecode($this->query->input('q'));
 		$query = last(explode('/', $query));
-		$status = Status::whereNull('uri')
-			->whereScope('public')
-			->find($query);
-
+		$status = StatusService::getMastodon($query);
 		if(!$status) {
 			return [
 				'accounts' => [],
@@ -266,14 +283,13 @@ class SearchApiV2Service
 			];
 		}
 
-		$fractal = new Fractal\Manager();
-		$fractal->setSerializer(new ArraySerializer());
-		$resource = new Fractal\Resource\Item($status, new StatusTransformer());
-		return [
+		$res = [
 			'accounts' => [],
 			'hashtags' => [],
-			'statuses' => $fractal->createData($resource)->toArray()
+			'statuses' => [$status]
 		];
+
+		return $res;
 	}
 
 	protected function resolveLocalProfile()

+ 9 - 5
app/Services/WebfingerService.php

@@ -11,15 +11,17 @@ use App\Services\AccountService;
 
 class WebfingerService
 {
-	public static function lookup($query)
+	public static function lookup($query, $mastodonMode = false)
 	{
-		return (new self)->run($query);
+		return (new self)->run($query, $mastodonMode);
 	}
 
-	protected function run($query)
+	protected function run($query, $mastodonMode)
 	{
 		if($profile = Profile::whereUsername($query)->first()) {
-			return AccountService::get($profile->id);
+			return $mastodonMode ?
+				AccountService::getMastodon($profile->id, true) :
+				AccountService::get($profile->id);
 		}
 		$url = WebfingerUrl::generateWebfingerUrl($query);
 		if(!Helpers::validateUrl($url)) {
@@ -54,6 +56,8 @@ class WebfingerService
 			->first();
 
 		$profile = Helpers::profileFetch($link);
-		return AccountService::get($profile->id);
+		return $mastodonMode ?
+			AccountService::getMastodon($profile->id, true) :
+			AccountService::get($profile->id);
 	}
 }