Update ApiV1Controller, add markers endpoint

This commit is contained in:
Daniel Supernault 2022-03-24 23:37:19 -06:00
parent fbe07c5161
commit 93a9769e47
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
3 changed files with 72 additions and 0 deletions

View file

@ -80,6 +80,7 @@ use App\Util\Media\License;
use App\Jobs\MediaPipeline\MediaSyncLicensePipeline;
use App\Services\DiscoverService;
use App\Services\CustomEmojiService;
use App\Services\MarkerService;
class ApiV1Controller extends Controller
{
@ -2742,4 +2743,45 @@ class ApiV1Controller extends Controller
return $this->json([]);
}
/**
* GET /api/v1/markers
*
*
* @return array
*/
public function getMarkers(Request $request)
{
abort_if(!$request->user(), 403);
$type = $request->input('timeline');
if(!$type || !in_array($type, ['home', 'notifications'])) {
return $this->json([]);
}
$pid = $request->user()->profile_id;
return $this->json(MarkerService::get($pid, $type));
}
/**
* POST /api/v1/markers
*
*
* @return array
*/
public function setMarkers(Request $request)
{
abort_if(!$request->user(), 403);
$pid = $request->user()->profile_id;
$home = $request->input('home.last_read_id');
$notifications = $request->input('notifications.last_read_id');
if($home) {
return $this->json(MarkerService::set($pid, 'home', $home));
}
if($notifications) {
return $this->json(MarkerService::set($pid, 'notifications', $notifications));
}
return $this->json([]);
}
}

View file

@ -0,0 +1,28 @@
<?php
namespace App\Services;
use Cache;
class MarkerService
{
const CACHE_KEY = 'pf:services:markers:timeline:';
public static function get($profileId, $timeline = 'home')
{
return Cache::get(self::CACHE_KEY . $timeline . ':' . $profileId);
}
public static function set($profileId, $timeline = 'home', $entityId)
{
$existing = self::get($profileId, $timeline);
$key = self::CACHE_KEY . $timeline . ':' . $profileId;
$val = [
'last_read_id' => (string) $entityId,
'version' => $existing ? ($existing['version'] + 1) : 1,
'updated_at' => now()->format('c')
];
Cache::put($key, $val, 2592000);
return $val;
}
}

View file

@ -87,6 +87,8 @@ Route::group(['prefix' => 'api'], function() use($middleware) {
Route::get('preferences', 'Api\ApiV1Controller@getPreferences')->middleware($middleware);
Route::get('trends', 'Api\ApiV1Controller@getTrends')->middleware($middleware);
Route::get('announcements', 'Api\ApiV1Controller@getAnnouncements')->middleware($middleware);
Route::get('markers', 'Api\ApiV1Controller@getMarkers')->middleware($middleware);
Route::post('markers', 'Api\ApiV1Controller@setMarkers')->middleware($middleware);
});
Route::group(['prefix' => 'v2'], function() use($middleware) {