mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-22 06:21:27 +00:00
Update StoryApiV1Controller, add self-carousel endpoint. Fixes #4352
This commit is contained in:
parent
e5e3be0598
commit
bcb88d5b0a
2 changed files with 424 additions and 299 deletions
|
@ -24,24 +24,39 @@ use App\Http\Resources\StoryView as StoryViewResource;
|
|||
|
||||
class StoryApiV1Controller extends Controller
|
||||
{
|
||||
const RECENT_KEY = 'pf:stories:recent-by-id:';
|
||||
const RECENT_TTL = 300;
|
||||
|
||||
public function carousel(Request $request)
|
||||
{
|
||||
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
|
||||
$pid = $request->user()->profile_id;
|
||||
|
||||
if(config('database.default') == 'pgsql') {
|
||||
$s = Story::select('stories.*', 'followers.following_id')
|
||||
$s = Cache::remember(self::RECENT_KEY . $pid, self::RECENT_TTL, function() use($pid) {
|
||||
return Story::select('stories.*', 'followers.following_id')
|
||||
->leftJoin('followers', 'followers.following_id', 'stories.profile_id')
|
||||
->where('followers.profile_id', $pid)
|
||||
->where('stories.active', true)
|
||||
->get();
|
||||
->map(function($s) {
|
||||
$r = new \StdClass;
|
||||
$r->id = $s->id;
|
||||
$r->profile_id = $s->profile_id;
|
||||
$r->type = $s->type;
|
||||
$r->path = $s->path;
|
||||
return $r;
|
||||
})
|
||||
->unique('profile_id');
|
||||
});
|
||||
} else {
|
||||
$s = Story::select('stories.*', 'followers.following_id')
|
||||
$s = Cache::remember(self::RECENT_KEY . $pid, self::RECENT_TTL, function() use($pid) {
|
||||
return Story::select('stories.*', 'followers.following_id')
|
||||
->leftJoin('followers', 'followers.following_id', 'stories.profile_id')
|
||||
->where('followers.profile_id', $pid)
|
||||
->where('stories.active', true)
|
||||
->orderBy('id')
|
||||
->get();
|
||||
});
|
||||
}
|
||||
|
||||
$nodes = $s->map(function($s) use($pid) {
|
||||
|
@ -121,6 +136,115 @@ class StoryApiV1Controller extends Controller
|
|||
return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
|
||||
}
|
||||
|
||||
public function selfCarousel(Request $request)
|
||||
{
|
||||
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
|
||||
$pid = $request->user()->profile_id;
|
||||
|
||||
if(config('database.default') == 'pgsql') {
|
||||
$s = Cache::remember(self::RECENT_KEY . $pid, self::RECENT_TTL, function() use($pid) {
|
||||
return Story::select('stories.*', 'followers.following_id')
|
||||
->leftJoin('followers', 'followers.following_id', 'stories.profile_id')
|
||||
->where('followers.profile_id', $pid)
|
||||
->where('stories.active', true)
|
||||
->map(function($s) {
|
||||
$r = new \StdClass;
|
||||
$r->id = $s->id;
|
||||
$r->profile_id = $s->profile_id;
|
||||
$r->type = $s->type;
|
||||
$r->path = $s->path;
|
||||
return $r;
|
||||
})
|
||||
->unique('profile_id');
|
||||
});
|
||||
} else {
|
||||
$s = Cache::remember(self::RECENT_KEY . $pid, self::RECENT_TTL, function() use($pid) {
|
||||
return Story::select('stories.*', 'followers.following_id')
|
||||
->leftJoin('followers', 'followers.following_id', 'stories.profile_id')
|
||||
->where('followers.profile_id', $pid)
|
||||
->where('stories.active', true)
|
||||
->orderBy('id')
|
||||
->get();
|
||||
});
|
||||
}
|
||||
|
||||
$nodes = $s->map(function($s) use($pid) {
|
||||
$profile = AccountService::get($s->profile_id, true);
|
||||
if(!$profile || !isset($profile['id'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return [
|
||||
'id' => (string) $s->id,
|
||||
'pid' => (string) $s->profile_id,
|
||||
'type' => $s->type,
|
||||
'src' => url(Storage::url($s->path)),
|
||||
'duration' => $s->duration ?? 3,
|
||||
'seen' => StoryService::hasSeen($pid, $s->id),
|
||||
'created_at' => $s->created_at->format('c')
|
||||
];
|
||||
})
|
||||
->filter()
|
||||
->groupBy('pid')
|
||||
->map(function($item) use($pid) {
|
||||
$profile = AccountService::get($item[0]['pid'], true);
|
||||
$url = $profile['local'] ? url("/stories/{$profile['username']}") :
|
||||
url("/i/rs/{$profile['id']}");
|
||||
return [
|
||||
'id' => 'pfs:' . $profile['id'],
|
||||
'user' => [
|
||||
'id' => (string) $profile['id'],
|
||||
'username' => $profile['username'],
|
||||
'username_acct' => $profile['acct'],
|
||||
'avatar' => $profile['avatar'],
|
||||
'local' => $profile['local'],
|
||||
'is_author' => $profile['id'] == $pid
|
||||
],
|
||||
'nodes' => $item,
|
||||
'url' => $url,
|
||||
'seen' => StoryService::hasSeen($pid, StoryService::latest($profile['id'])),
|
||||
];
|
||||
})
|
||||
->sortBy('seen')
|
||||
->values();
|
||||
|
||||
$selfProfile = AccountService::get($pid, true);
|
||||
$res = [
|
||||
'self' => [
|
||||
'user' => [
|
||||
'id' => (string) $selfProfile['id'],
|
||||
'username' => $selfProfile['acct'],
|
||||
'avatar' => $selfProfile['avatar'],
|
||||
'local' => $selfProfile['local'],
|
||||
'is_author' => true
|
||||
],
|
||||
|
||||
'nodes' => [],
|
||||
],
|
||||
'nodes' => $nodes,
|
||||
];
|
||||
|
||||
if(Story::whereProfileId($pid)->whereActive(true)->exists()) {
|
||||
$selfStories = Story::whereProfileId($pid)
|
||||
->whereActive(true)
|
||||
->get()
|
||||
->map(function($s) use($pid) {
|
||||
return [
|
||||
'id' => (string) $s->id,
|
||||
'type' => $s->type,
|
||||
'src' => url(Storage::url($s->path)),
|
||||
'duration' => $s->duration,
|
||||
'seen' => true,
|
||||
'created_at' => $s->created_at->format('c')
|
||||
];
|
||||
})
|
||||
->sortBy('id')
|
||||
->values();
|
||||
$res['self']['nodes'] = $selfStories;
|
||||
}
|
||||
return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
|
||||
}
|
||||
|
||||
public function add(Request $request)
|
||||
{
|
||||
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
|
||||
|
|
|
@ -313,6 +313,7 @@ Route::group(['prefix' => 'api'], function() use($middleware) {
|
|||
|
||||
Route::group(['prefix' => 'stories'], function () use($middleware) {
|
||||
Route::get('carousel', 'Stories\StoryApiV1Controller@carousel')->middleware($middleware);
|
||||
Route::get('self-carousel', 'Stories\StoryApiV1Controller@selfCarousel')->middleware($middleware);
|
||||
Route::post('add', 'Stories\StoryApiV1Controller@add')->middleware($middleware);
|
||||
Route::post('publish', 'Stories\StoryApiV1Controller@publish')->middleware($middleware);
|
||||
Route::post('seen', 'Stories\StoryApiV1Controller@viewed')->middleware($middleware);
|
||||
|
|
Loading…
Reference in a new issue