浏览代码

Added `following_since` attribute to `/api/v1/accounts/relationships` endpoint when `_pe=1` (pixelfed entity) parameter is present

Daniel Supernault 2 年之前
父节点
当前提交
992d910b9c
共有 2 个文件被更改,包括 26 次插入2 次删除
  1. 5 2
      app/Http/Controllers/Api/ApiV1Controller.php
  2. 21 0
      app/Services/RelationshipService.php

+ 5 - 2
app/Http/Controllers/Api/ApiV1Controller.php

@@ -905,13 +905,16 @@ class ApiV1Controller extends Controller
 			'id'    => 'required|array|min:1|max:20',
 			'id.*'  => 'required|integer|min:1|max:' . PHP_INT_MAX
 		]);
+		$napi = $request->has(self::PF_API_ENTITY_KEY);
 		$pid = $request->user()->profile_id ?? $request->user()->profile->id;
 		$res = collect($request->input('id'))
 			->filter(function($id) use($pid) {
 				return intval($id) !== intval($pid);
 			})
-			->map(function($id) use($pid) {
-				return RelationshipService::get($pid, $id);
+			->map(function($id) use($pid, $napi) {
+				return $napi ?
+				 RelationshipService::getWithDate($pid, $id) :
+				 RelationshipService::get($pid, $id);
 		});
 		return $this->json($res);
 	}

+ 21 - 0
app/Services/RelationshipService.php

@@ -52,6 +52,7 @@ class RelationshipService
 
 	public static function delete($aid, $tid)
 	{
+		Cache::forget(self::key("wd:a_{$aid}:t_{$tid}"));
 		return Cache::forget(self::key("a_{$aid}:t_{$tid}"));
 	}
 
@@ -85,4 +86,24 @@ class RelationshipService
 	{
 		return self::CACHE_KEY . $suffix;
 	}
+
+	public static function getWithDate($aid, $tid)
+	{
+		$res = self::get($aid, $tid);
+
+		if(!$res || !$res['following']) {
+			$res['following_since'] = null;
+			return $res;
+		}
+
+		return Cache::remember(self::key("wd:a_{$aid}:t_{$tid}"), 1209600, function() use($aid, $tid, $res) {
+			$tmp = Follower::whereProfileId($aid)->whereFollowingId($tid)->first();
+			if(!$tmp) {
+				$res['following_since'] = null;
+				return $res;
+			}
+			$res['following_since'] = str_replace('+00:00', 'Z', $tmp->created_at->format(DATE_RFC3339_EXTENDED));
+			return $res;
+		});
+	}
 }