1
0
Fork 0
forked from mirror/pixelfed

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

This commit is contained in:
Daniel Supernault 2023-05-29 00:08:31 -06:00
parent d4f92da0e5
commit 992d910b9c
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
2 changed files with 26 additions and 2 deletions

View file

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

View file

@ -52,6 +52,7 @@ class RelationshipService
public static function delete($aid, $tid) public static function delete($aid, $tid)
{ {
Cache::forget(self::key("wd:a_{$aid}:t_{$tid}"));
return Cache::forget(self::key("a_{$aid}:t_{$tid}")); return Cache::forget(self::key("a_{$aid}:t_{$tid}"));
} }
@ -85,4 +86,24 @@ class RelationshipService
{ {
return self::CACHE_KEY . $suffix; 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;
});
}
} }