Skip to content

Commit 9f20734

Browse files
committed
Update For You Feed, hide blocked creators. Fixes #346
1 parent c772506 commit 9f20734

3 files changed

Lines changed: 26 additions & 34 deletions

File tree

app/Http/Controllers/Api/AccountController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ public function accountBlock(Request $request, $id)
169169

170170
$profile = Profile::findOrFail($id);
171171

172+
$count = UserFilter::where('profile_id', $pid)->count();
173+
174+
abort_if($count > 250, 422, 'You cannot block any more accounts');
175+
172176
UserFilter::updateOrCreate([
173177
'profile_id' => $pid,
174178
'account_id' => $profile->id,

app/Jobs/UpdateUserInterestsJob.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public function handle(): void
3636
$this->decayInterests($profile);
3737

3838
app(ForYouFeedService::class)->forgetUserInterests($this->profileId);
39-
app(ForYouFeedService::class)->forgetHiddenCreators($this->profileId);
4039
}
4140

4241
private function updateFromLikes(Profile $profile): void

app/Services/ForYouFeedService.php

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ private function buildFeedQuery(Profile $profile, ?string $cursor, int $limit):
6262
$interests = $this->getUserInterests($profile->id);
6363
$isNewUser = $interests->isEmpty();
6464

65-
$hiddenCreatorIds = $this->getHiddenCreators($profile->id);
66-
6765
$cursorScore = null;
6866
$cursorId = null;
6967
if ($cursor) {
@@ -76,7 +74,6 @@ private function buildFeedQuery(Profile $profile, ?string $cursor, int $limit):
7674
$personalizedVideos = $this->getPersonalizedVideos(
7775
$profile,
7876
$interests,
79-
$hiddenCreatorIds,
8077
$cursorScore,
8178
$cursorId,
8279
$isNewUser ? $limit : $personalizedCount
@@ -85,7 +82,6 @@ private function buildFeedQuery(Profile $profile, ?string $cursor, int $limit):
8582
if ($personalizedVideos->count() < self::MIN_RESULTS || $isNewUser) {
8683
$popularVideos = $this->getPopularVideos(
8784
$profile,
88-
$hiddenCreatorIds,
8985
$cursorId,
9086
$limit - $personalizedVideos->count(),
9187
$personalizedVideos->pluck('id')->toArray()
@@ -103,7 +99,6 @@ private function buildFeedQuery(Profile $profile, ?string $cursor, int $limit):
10399
if (! $isNewUser && $discoveryCount > 0) {
104100
$discoveryVideos = $this->getDiscoveryVideos(
105101
$profile,
106-
$hiddenCreatorIds,
107102
$personalizedVideos->pluck('id')->toArray(),
108103
$discoveryCount
109104
);
@@ -114,10 +109,17 @@ private function buildFeedQuery(Profile $profile, ?string $cursor, int $limit):
114109
return $personalizedVideos->take($limit)->values();
115110
}
116111

112+
private function applyUserFilters($query, int $profileId)
113+
{
114+
return $query->leftJoin('user_filters', function ($join) use ($profileId) {
115+
$join->on('videos.profile_id', '=', 'user_filters.account_id')
116+
->where('user_filters.profile_id', '=', $profileId);
117+
})->whereNull('user_filters.id');
118+
}
119+
117120
private function getPersonalizedVideos(
118121
Profile $profile,
119122
Collection $interests,
120-
array $hiddenCreatorIds,
121123
?float $cursorScore,
122124
?int $cursorId,
123125
int $limit
@@ -130,7 +132,6 @@ private function getPersonalizedVideos(
130132

131133
while ($scoredVideos->count() < $limit && $attempts < $maxAttempts) {
132134
$attempts++;
133-
134135
$batchSize = $limit * $batchMultiplier * $attempts;
135136

136137
$query = Video::query()
@@ -141,13 +142,15 @@ private function getPersonalizedVideos(
141142
->where('videos.status', 2)
142143
->where('videos.visibility', 1)
143144
->where('videos.created_at', '>=', now()->subDays(self::MAX_AGE_DAYS))
144-
->whereNotIn('videos.profile_id', $hiddenCreatorIds)
145145
->orderByDesc('videos.id')
146146
->with([
147147
'profile:id,username',
148148
'hashtags:hashtags.id,hashtags.name',
149149
]);
150150

151+
// Apply user filters via join
152+
$query = $this->applyUserFilters($query, $profile->id);
153+
151154
if ($lastId !== null) {
152155
$query->where('videos.id', '<', $lastId);
153156
}
@@ -181,7 +184,6 @@ private function getPersonalizedVideos(
181184

182185
private function getPopularVideos(
183186
Profile $profile,
184-
array $hiddenCreatorIds,
185187
?int $cursorId,
186188
int $limit,
187189
array $alreadyIncludedIds = []
@@ -201,12 +203,14 @@ private function getPopularVideos(
201203
->where('videos.visibility', 1)
202204
->where('videos.created_at', '>=', now()->subDays(self::MAX_AGE_DAYS))
203205
->whereNotIn('videos.id', $alreadyIncludedIds)
204-
->whereNotIn('videos.profile_id', $hiddenCreatorIds)
205206
->with([
206207
'profile:id,username',
207208
'hashtags:hashtags.id,hashtags.name',
208209
]);
209210

211+
// Apply user filters via join
212+
$query = $this->applyUserFilters($query, $profile->id);
213+
210214
if ($cursorId) {
211215
$query->where('videos.id', '<', $cursorId);
212216
}
@@ -223,11 +227,10 @@ private function getPopularVideos(
223227

224228
private function getDiscoveryVideos(
225229
Profile $profile,
226-
array $hiddenCreatorIds,
227230
array $alreadyIncludedIds,
228231
int $limit
229232
): Collection {
230-
$videos = Video::query()
233+
$query = Video::query()
231234
->select([
232235
'videos.*',
233236
DB::raw('RAND() as feed_score'),
@@ -236,15 +239,20 @@ private function getDiscoveryVideos(
236239
->where('videos.visibility', 1)
237240
->where('videos.created_at', '>=', now()->subDays(self::MAX_AGE_DAYS))
238241
->whereNotIn('videos.id', $alreadyIncludedIds)
239-
->whereNotIn('videos.profile_id', $hiddenCreatorIds)
240242
->where('videos.likes', '>=', 2)
241243
->with([
242244
'profile:id,username',
243245
'hashtags:hashtags.id,hashtags.name',
244-
])
246+
]);
247+
248+
// Apply user filters via join
249+
$query = $this->applyUserFilters($query, $profile->id);
250+
251+
$videos = $query
245252
->inRandomOrder()
246253
->limit($limit)
247254
->get();
255+
248256
$videos = app(ImpressionBloomFilterService::class)->filterVideos($profile->id, $videos);
249257

250258
return $videos;
@@ -411,25 +419,6 @@ public function forgetUserInterests(int $profileId): void
411419
Cache::forget("fyf:user_interests:{$profileId}");
412420
}
413421

414-
private function getHiddenCreators(int $profileId): array
415-
{
416-
return Cache::remember(
417-
"fyf:hidden_creators:{$profileId}",
418-
now()->addMinutes(30),
419-
fn () => FeedFeedback::where('feed_feedback.profile_id', $profileId)
420-
->where('feedback_type', FeedFeedback::TYPE_HIDE_CREATOR)
421-
->join('videos', 'feed_feedback.video_id', '=', 'videos.id')
422-
->pluck('videos.profile_id')
423-
->unique()
424-
->toArray()
425-
);
426-
}
427-
428-
public function forgetHiddenCreators(int $profileId): void
429-
{
430-
Cache::forget("fyf:hidden_creators:{$profileId}");
431-
}
432-
433422
private function generateCursor(Collection $videos): ?string
434423
{
435424
if ($videos->isEmpty()) {

0 commit comments

Comments
 (0)