Update ApiV1Controller, fix relationship fields. Fixes #5900

pull/5925/head
Daniel Supernault 3 months ago
parent 8a86808a06
commit 245ab3bc4f
No known key found for this signature in database
GPG Key ID: 23740873EE6F76A1

@ -2,116 +2,120 @@
namespace App\Services; namespace App\Services;
use Illuminate\Support\Facades\Cache;
use App\Follower; use App\Follower;
use App\FollowRequest; use App\FollowRequest;
use App\Profile;
use App\UserFilter; use App\UserFilter;
use Illuminate\Support\Facades\Cache;
class RelationshipService class RelationshipService
{ {
const CACHE_KEY = 'pf:services:urel:'; const CACHE_KEY = 'pf:services:urel:';
public static function get($aid, $tid) public static function get($aid, $tid)
{ {
$actor = AccountService::get($aid, true); $actor = AccountService::get($aid, true);
$target = AccountService::get($tid, true); $target = AccountService::get($tid, true);
if(!$actor || !$target) { if (! $actor || ! $target) {
return self::defaultRelation($tid); return self::defaultRelation($tid);
} }
if($actor['id'] === $target['id']) { if ($actor['id'] === $target['id']) {
return self::defaultRelation($tid); return self::defaultRelation($tid);
} }
return Cache::remember(self::key("a_{$aid}:t_{$tid}"), 1209600, function() use($aid, $tid) { return Cache::remember(self::key("a_{$aid}:t_{$tid}"), 1209600, function () use ($aid, $tid) {
return [ return [
'id' => (string) $tid, 'id' => (string) $tid,
'following' => Follower::whereProfileId($aid)->whereFollowingId($tid)->exists(), 'following' => Follower::whereProfileId($aid)->whereFollowingId($tid)->exists(),
'followed_by' => Follower::whereProfileId($tid)->whereFollowingId($aid)->exists(), 'followed_by' => Follower::whereProfileId($tid)->whereFollowingId($aid)->exists(),
'blocking' => UserFilter::whereUserId($aid) 'blocking' => UserFilter::whereUserId($aid)
->whereFilterableType('App\Profile') ->whereFilterableType('App\Profile')
->whereFilterableId($tid) ->whereFilterableId($tid)
->whereFilterType('block') ->whereFilterType('block')
->exists(), ->exists(),
'muting' => UserFilter::whereUserId($aid) 'muting' => UserFilter::whereUserId($aid)
->whereFilterableType('App\Profile') ->whereFilterableType('App\Profile')
->whereFilterableId($tid) ->whereFilterableId($tid)
->whereFilterType('mute') ->whereFilterType('mute')
->exists(), ->exists(),
'muting_notifications' => null, 'muting_notifications' => false,
'requested' => FollowRequest::whereFollowerId($aid) 'requested' => FollowRequest::whereFollowerId($aid)
->whereFollowingId($tid) ->whereFollowingId($tid)
->exists(), ->exists(),
'domain_blocking' => null, 'domain_blocking' => false,
'showing_reblogs' => null, 'showing_reblogs' => false,
'endorsed' => false 'endorsed' => false,
]; ];
}); });
} }
public static function delete($aid, $tid) public static function delete($aid, $tid)
{ {
Cache::forget(self::key("wd:a_{$aid}:t_{$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}"));
}
public static function refresh($aid, $tid)
{ public static function refresh($aid, $tid)
Cache::forget('pf:services:follower:audience:' . $aid); {
Cache::forget('pf:services:follower:audience:' . $tid); Cache::forget('pf:services:follower:audience:'.$aid);
self::delete($tid, $aid); Cache::forget('pf:services:follower:audience:'.$tid);
self::delete($aid, $tid); self::delete($tid, $aid);
self::get($tid, $aid); self::delete($aid, $tid);
return self::get($aid, $tid); self::get($tid, $aid);
}
return self::get($aid, $tid);
public static function forget($aid, $tid) }
{
Cache::forget('pf:services:follower:audience:' . $aid); public static function forget($aid, $tid)
Cache::forget('pf:services:follower:audience:' . $tid); {
self::delete($tid, $aid); Cache::forget('pf:services:follower:audience:'.$aid);
self::delete($aid, $tid); Cache::forget('pf:services:follower:audience:'.$tid);
} self::delete($tid, $aid);
self::delete($aid, $tid);
public static function defaultRelation($tid) }
{
return [ public static function defaultRelation($tid)
{
return [
'id' => (string) $tid, 'id' => (string) $tid,
'following' => false, 'following' => false,
'followed_by' => false, 'followed_by' => false,
'blocking' => false, 'blocking' => false,
'muting' => false, 'muting' => false,
'muting_notifications' => null, 'muting_notifications' => false,
'requested' => false, 'requested' => false,
'domain_blocking' => null, 'domain_blocking' => false,
'showing_reblogs' => null, 'showing_reblogs' => false,
'endorsed' => false 'endorsed' => false,
]; ];
} }
protected static function key($suffix) protected static function key($suffix)
{ {
return self::CACHE_KEY . $suffix; return self::CACHE_KEY.$suffix;
} }
public static function getWithDate($aid, $tid) public static function getWithDate($aid, $tid)
{ {
$res = self::get($aid, $tid); $res = self::get($aid, $tid);
if(!$res || !$res['following']) { if (! $res || ! $res['following']) {
$res['following_since'] = null; $res['following_since'] = null;
return $res;
} 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(); return Cache::remember(self::key("wd:a_{$aid}:t_{$tid}"), 1209600, function () use ($aid, $tid, $res) {
if(!$tmp) { $tmp = Follower::whereProfileId($aid)->whereFollowingId($tid)->first();
$res['following_since'] = null; if (! $tmp) {
return $res; $res['following_since'] = null;
}
$res['following_since'] = str_replace('+00:00', 'Z', $tmp->created_at->format(DATE_RFC3339_EXTENDED)); return $res;
return $res; }
}); $res['following_since'] = str_replace('+00:00', 'Z', $tmp->created_at->format(DATE_RFC3339_EXTENDED));
}
return $res;
});
}
} }

@ -2,11 +2,10 @@
namespace App\Transformer\Api; namespace App\Transformer\Api;
use App\FollowRequest;
use App\Models\UserDomainBlock;
use App\Profile;
use Auth; use Auth;
use App\{
FollowRequest,
Profile
};
use League\Fractal; use League\Fractal;
class RelationshipTransformer extends Fractal\TransformerAbstract class RelationshipTransformer extends Fractal\TransformerAbstract
@ -14,27 +13,35 @@ class RelationshipTransformer extends Fractal\TransformerAbstract
public function transform(Profile $profile) public function transform(Profile $profile)
{ {
$auth = Auth::check(); $auth = Auth::check();
if(!$auth) { if (! $auth) {
return []; return [];
} }
$user = $auth ? Auth::user()->profile : false; $user = $auth ? Auth::user()->profile : false;
$requested = false; $requested = false;
if($user) { $domainBlocking = false;
if ($user) {
$requested = FollowRequest::whereFollowerId($user->id) $requested = FollowRequest::whereFollowerId($user->id)
->whereFollowingId($profile->id) ->whereFollowingId($profile->id)
->exists(); ->exists();
if ($profile->domain) {
$domainBlocking = UserDomainBlock::whereProfileId($user->id)
->whereDomain($profile->domain)
->exists();
}
} }
return [ return [
'id' => (string) $profile->id, 'id' => (string) $profile->id,
'following' => $auth ? $user->follows($profile) : false, 'following' => $auth ? $user->follows($profile) : false,
'followed_by' => $auth ? $user->followedBy($profile) : false, 'followed_by' => $auth ? $user->followedBy($profile) : false,
'blocking' => $auth ? $user->blockedIds()->contains($profile->id) : false, 'blocking' => $auth ? $user->blockedIds()->contains($profile->id) : false,
'muting' => $auth ? $user->mutedIds()->contains($profile->id) : false, 'muting' => $auth ? $user->mutedIds()->contains($profile->id) : false,
'muting_notifications' => null, 'muting_notifications' => false,
'requested' => $requested, 'requested' => $requested,
'domain_blocking' => null, 'domain_blocking' => $domainBlocking,
'showing_reblogs' => null, 'showing_reblogs' => false,
'endorsed' => false 'endorsed' => false,
]; ];
} }
} }

Loading…
Cancel
Save