Pārlūkot izejas kodu

Update ApiV1Controller, add markers endpoint

Daniel Supernault 3 gadi atpakaļ
vecāks
revīzija
93a9769e47

+ 42 - 0
app/Http/Controllers/Api/ApiV1Controller.php

@@ -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([]);
+	}
 }

+ 28 - 0
app/Services/MarkerService.php

@@ -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;
+	}
+}

+ 2 - 0
routes/api.php

@@ -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) {