Skip to content

Commit

Permalink
Update ApiV1Controller, fix home timeline bug
Browse files Browse the repository at this point in the history
  • Loading branch information
dansup committed Mar 5, 2023
1 parent 158596b commit a8ec844
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 193 deletions.
130 changes: 31 additions & 99 deletions app/Http/Controllers/Api/ApiV1Controller.php
Expand Up @@ -2073,10 +2073,10 @@ public function accountNotifications(Request $request)
public function timelineHome(Request $request)
{
$this->validate($request,[
'page' => 'nullable|integer|max:40',
'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
'limit' => 'nullable|integer|max:100'
'page' => 'sometimes|integer|max:40',
'min_id' => 'sometimes|integer|min:0|max:' . PHP_INT_MAX,
'max_id' => 'sometimes|integer|min:0|max:' . PHP_INT_MAX,
'limit' => 'sometimes|integer|min:1|max:80'
]);

$napi = $request->has(self::PF_API_ENTITY_KEY);
Expand All @@ -2091,84 +2091,6 @@ public function timelineHome(Request $request)
return $following->push($pid)->toArray();
});

$includeReplies = false;
if(config('exp.top')) {
$includeReplies = (bool) Redis::zscore('pf:tl:replies', $pid);
}

if(config('instance.timeline.home.cached') && (!$min && !$max)) {
$ttl = config('instance.timeline.home.cache_ttl');
$res = Cache::remember(
'pf:timelines:home:' . $pid,
$ttl,
function() use(
$following,
$limit,
$pid,
$includeReplies
) {
return Status::select(
'id',
'uri',
'caption',
'rendered',
'profile_id',
'type',
'in_reply_to_id',
'reblog_of_id',
'is_nsfw',
'scope',
'local',
'reply_count',
'comments_disabled',
'place_id',
'likes_count',
'reblogs_count',
'created_at',
'updated_at'
)
->when(!$includeReplies, function($q, $includeReplies) {
return $q->whereNull('in_reply_to_id');
})
->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
->whereIn('profile_id', $following)
->whereIn('visibility',['public', 'unlisted', 'private'])
->orderBy('created_at', 'desc')
->limit($limit)
->get()
->map(function($s) {
$status = StatusService::get($s->id, false);
if(!$status) {
return false;
}
return $status;
})
->filter(function($s) {
return $s && isset($s['account']['id']);
})
->values()
->toArray();
});

$res = collect($res)
->map(function($s) use ($pid) {
$status = StatusService::get($s['id'], false);
if(!$status) {
return false;
}
$status['favourited'] = (bool) LikeService::liked($pid, $s['id']);
$status['bookmarked'] = (bool) BookmarkService::get($pid, $s['id']);
$status['reblogged'] = (bool) ReblogService::get($pid, $s['id']);
return $status;
})
->filter(function($s) {
return $s && isset($s['account']['id']);
})
->values()
->take($limit)
->toArray();
}

if($min || $max) {
$dir = $min ? '>' : '<';
$id = $min ?? $max;
Expand All @@ -2177,21 +2099,24 @@ function() use(
'profile_id',
'type',
'visibility',
'created_at'
'in_reply_to_id',
'reblog_of_id'
)
->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
->where('id', $dir, $id)
->when(!$includeReplies, function($q, $includeReplies) {
return $q->whereNull('in_reply_to_id');
})
->whereIn('profile_id', $following)
->whereNull(['in_reply_to_id', 'reblog_of_id'])
->whereIntegerInRaw('profile_id', $following)
->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
->whereIn('visibility',['public', 'unlisted', 'private'])
->latest()
->orderByDesc('id')
->take(($limit * 2))
->get()
->map(function($s) use($pid, $napi) {
$status = $napi ? StatusService::get($s['id'], false) : StatusService::getMastodon($s['id'], false);
if(!$status || !isset($status['account']) || !isset($status['account']['id'])) {
try {
$status = $napi ? StatusService::get($s['id'], false) : StatusService::getMastodon($s['id'], false);
if(!$status || !isset($status['account']) || !isset($status['account']['id'])) {
return false;
}
} catch(\Exception $e) {
return false;
}

Expand All @@ -2212,20 +2137,27 @@ function() use(
'profile_id',
'type',
'visibility',
'created_at'
'in_reply_to_id',
'reblog_of_id',
)
->when(!$includeReplies, function($q, $includeReplies) {
return $q->whereNull('in_reply_to_id');
})
->whereNull(['in_reply_to_id', 'reblog_of_id'])
->whereIntegerInRaw('profile_id', $following)
->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
->whereIn('profile_id', $following)
->whereIn('visibility',['public', 'unlisted', 'private'])
->latest()
->orderByDesc('id')
->take(($limit * 2))
->get()
->map(function($s) use($pid, $napi) {
$status = $napi ? StatusService::get($s['id'], false) : StatusService::getMastodon($s['id'], false);
if(!$status || !isset($status['account']) || !isset($status['account']['id'])) {
try {
$account = AccountService::get($s['profile_id'], true);
if(!$account) {
return false;
}
$status = $napi ? StatusService::get($s['id'], false) : StatusService::getMastodon($s['id'], false);
if(!$status || !isset($status['account']) || !isset($status['account']['id'])) {
return false;
}
} catch(\Exception $e) {
return false;
}

Expand Down
106 changes: 12 additions & 94 deletions app/Http/Controllers/PublicApiController.php
Expand Up @@ -408,96 +408,6 @@ public function homeTimelineApi(Request $request)

$textOnlyReplies = false;

if(config('exp.top')) {
$textOnlyReplies = (bool) Redis::zscore('pf:tl:replies', $pid);
$textOnlyPosts = (bool) Redis::zscore('pf:tl:top', $pid);

if($textOnlyPosts) {
array_push($types, 'text');
}
}

if(config('exp.polls') == true) {
array_push($types, 'poll');
}

if(config('instance.timeline.home.cached') && $limit == 6 && (!$min && !$max)) {
$ttl = config('instance.timeline.home.cache_ttl');
$res = Cache::remember(
'pf:timelines:home:' . $pid,
$ttl,
function() use(
$types,
$textOnlyReplies,
$following,
$limit,
$filtered,
$user
) {
return Status::select(
'id',
'uri',
'caption',
'rendered',
'profile_id',
'type',
'in_reply_to_id',
'reblog_of_id',
'is_nsfw',
'scope',
'local',
'reply_count',
'comments_disabled',
'place_id',
'likes_count',
'reblogs_count',
'created_at',
'updated_at'
)
->whereIn('type', $types)
->when(!$textOnlyReplies, function($q, $textOnlyReplies) {
return $q->whereNull('in_reply_to_id');
})
->whereIn('profile_id', $following)
->whereIn('visibility',['public', 'unlisted', 'private'])
->orderBy('created_at', 'desc')
->limit($limit)
->get()
->map(function($s) use ($user) {
$status = StatusService::get($s->id, false);
if(!$status) {
return false;
}
return $status;
})
->filter(function($s) use($filtered) {
return $s && in_array($s['account']['id'], $filtered) == false;
})
->values()
->toArray();
});

$res = collect($res)
->map(function($s) use ($user) {
$status = StatusService::get($s['id'], false);
if(!$status) {
return false;
}
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s['id']);
$status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s['id']);
$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s['id']);
return $status;
})
->filter(function($s) use($filtered) {
return $s && in_array($s['account']['id'], $filtered) == false;
})
->values()
->take($limit)
->toArray();

return $res;
}

if($min || $max) {
$dir = $min ? '>' : '<';
$id = $min ?? $max;
Expand Down Expand Up @@ -532,10 +442,14 @@ function() use(
->limit($limit)
->get()
->map(function($s) use ($user) {
try {
$status = StatusService::get($s->id, false);
if(!$status) {
return false;
}
} catch(\Exception $e) {
return false;
}
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
$status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
Expand Down Expand Up @@ -577,10 +491,14 @@ function() use(
->limit($limit)
->get()
->map(function($s) use ($user) {
$status = StatusService::get($s->id, false);
if(!$status) {
return false;
}
try {
$status = StatusService::get($s->id, false);
if(!$status) {
return false;
}
} catch(\Exception $e) {
return false;
}
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
$status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
Expand Down

0 comments on commit a8ec844

Please sign in to comment.