Skip to content

Commit fbaed93

Browse files
committed
Update SearchApiV2Service, improve performance and include hashtag post counts when applicable
1 parent a37971d commit fbaed93

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

app/Services/SearchApiV2Service.php

+24-13
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
1313
use App\Util\ActivityPub\Helpers;
1414
use Illuminate\Support\Str;
15+
use App\Services\AccountService;
16+
use App\Services\HashtagService;
17+
use App\Services\StatusService;
1518

1619
class SearchApiV2Service
1720
{
@@ -86,19 +89,27 @@ protected function resolve()
8689

8790
protected function accounts()
8891
{
92+
$user = request()->user();
8993
$limit = $this->query->input('limit') ?? 20;
9094
$offset = $this->query->input('offset') ?? 0;
9195
$query = '%' . $this->query->input('q') . '%';
92-
$results = Profile::whereNull('status')
96+
$results = Profile::select('profiles.*', 'followers.profile_id', 'followers.created_at')
97+
->whereNull('status')
98+
->leftJoin('followers', function($join) use($user) {
99+
return $join->on('profiles.id', '=', 'followers.following_id')
100+
->where('followers.profile_id', $user->profile_id);
101+
})
93102
->where('username', 'like', $query)
103+
->orderByDesc('profiles.followers_count')
104+
->orderByDesc('followers.created_at')
94105
->offset($offset)
95106
->limit($limit)
96-
->get();
107+
->get()
108+
->map(function($res) {
109+
return AccountService::get($res['id']);
110+
});
97111

98-
$fractal = new Fractal\Manager();
99-
$fractal->setSerializer(new ArraySerializer());
100-
$resource = new Fractal\Resource\Collection($results, new AccountTransformer());
101-
return $fractal->createData($resource)->toArray();
112+
return $results;
102113
}
103114

104115
protected function hashtags()
@@ -115,6 +126,7 @@ protected function hashtags()
115126
return [
116127
'name' => $tag->name,
117128
'url' => $tag->url(),
129+
'count' => HashtagService::count($tag->id),
118130
'history' => []
119131
];
120132
});
@@ -134,12 +146,11 @@ protected function statusesById()
134146
$results = Status::where('caption', 'like', $query)
135147
->whereProfileId($accountId)
136148
->limit($limit)
137-
->get();
138-
139-
$fractal = new Fractal\Manager();
140-
$fractal->setSerializer(new ArraySerializer());
141-
$resource = new Fractal\Resource\Collection($results, new StatusTransformer());
142-
return $fractal->createData($resource)->toArray();
149+
->get()
150+
->map(function($status) {
151+
return StatusService::get($status->id);
152+
});
153+
return $results;
143154
}
144155

145156
protected function resolveQuery()
@@ -213,4 +224,4 @@ protected function resolveLocalProfile()
213224
];
214225
}
215226

216-
}
227+
}

0 commit comments

Comments
 (0)