mirror of https://github.com/pixelfed/pixelfed
Refactor following & relationship logic. Replace FollowerObserver with FollowerService and added RelationshipService to cache results. Removed NotificationTransformer includes and replaced with cached services to improve performance and reduce database queries.
parent
0a8eb81bf0
commit
80d9b9399a
@ -1,64 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Follower;
|
||||
use App\Services\FollowerService;
|
||||
|
||||
class FollowerObserver
|
||||
{
|
||||
/**
|
||||
* Handle the Follower "created" event.
|
||||
*
|
||||
* @param \App\Models\Follower $follower
|
||||
* @return void
|
||||
*/
|
||||
public function created(Follower $follower)
|
||||
{
|
||||
FollowerService::add($follower->profile_id, $follower->following_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Follower "updated" event.
|
||||
*
|
||||
* @param \App\Models\Follower $follower
|
||||
* @return void
|
||||
*/
|
||||
public function updated(Follower $follower)
|
||||
{
|
||||
FollowerService::add($follower->profile_id, $follower->following_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Follower "deleted" event.
|
||||
*
|
||||
* @param \App\Models\Follower $follower
|
||||
* @return void
|
||||
*/
|
||||
public function deleted(Follower $follower)
|
||||
{
|
||||
FollowerService::remove($follower->profile_id, $follower->following_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Follower "restored" event.
|
||||
*
|
||||
* @param \App\Models\Follower $follower
|
||||
* @return void
|
||||
*/
|
||||
public function restored(Follower $follower)
|
||||
{
|
||||
FollowerService::add($follower->profile_id, $follower->following_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Follower "force deleted" event.
|
||||
*
|
||||
* @param \App\Models\Follower $follower
|
||||
* @return void
|
||||
*/
|
||||
public function forceDeleted(Follower $follower)
|
||||
{
|
||||
FollowerService::remove($follower->profile_id, $follower->following_id);
|
||||
}
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use App\Follower;
|
||||
use App\FollowRequest;
|
||||
use App\Profile;
|
||||
use App\UserFilter;
|
||||
|
||||
class RelationshipService
|
||||
{
|
||||
const CACHE_KEY = 'pf:services:urel:';
|
||||
|
||||
public static function get($aid, $tid)
|
||||
{
|
||||
$actor = AccountService::get($aid);
|
||||
$target = AccountService::get($tid);
|
||||
if(!$actor || !$target) {
|
||||
return self::defaultRelation($tid);
|
||||
}
|
||||
|
||||
if($actor['id'] === $target['id']) {
|
||||
return self::defaultRelation($tid);
|
||||
}
|
||||
|
||||
return Cache::remember(self::key("a_{$aid}:t_{$tid}"), 1209600, function() use($aid, $tid) {
|
||||
return [
|
||||
'id' => (string) $tid,
|
||||
'following' => Follower::whereProfileId($aid)->whereFollowingId($tid)->exists(),
|
||||
'followed_by' => Follower::whereProfileId($tid)->whereFollowingId($aid)->exists(),
|
||||
'blocking' => UserFilter::whereUserId($aid)
|
||||
->whereFilterableType('App\Profile')
|
||||
->whereFilterableId($tid)
|
||||
->whereFilterType('block')
|
||||
->exists(),
|
||||
'muting' => UserFilter::whereUserId($aid)
|
||||
->whereFilterableType('App\Profile')
|
||||
->whereFilterableId($tid)
|
||||
->whereFilterType('mute')
|
||||
->exists(),
|
||||
'muting_notifications' => null,
|
||||
'requested' => FollowRequest::whereFollowerId($aid)
|
||||
->whereFollowingId($tid)
|
||||
->exists(),
|
||||
'domain_blocking' => null,
|
||||
'showing_reblogs' => null,
|
||||
'endorsed' => false
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
public static function delete($aid, $tid)
|
||||
{
|
||||
return Cache::forget(self::key("a_{$aid}:t_{$tid}"));
|
||||
}
|
||||
|
||||
public static function refresh($aid, $tid)
|
||||
{
|
||||
self::delete($tid, $aid);
|
||||
self::delete($aid, $tid);
|
||||
self::get($tid, $aid);
|
||||
return self::get($aid, $tid);
|
||||
}
|
||||
|
||||
public static function defaultRelation($tid)
|
||||
{
|
||||
return [
|
||||
'id' => (string) $tid,
|
||||
'following' => false,
|
||||
'followed_by' => false,
|
||||
'blocking' => false,
|
||||
'muting' => false,
|
||||
'muting_notifications' => null,
|
||||
'requested' => false,
|
||||
'domain_blocking' => null,
|
||||
'showing_reblogs' => null,
|
||||
'endorsed' => false
|
||||
];
|
||||
}
|
||||
|
||||
protected static function key($suffix)
|
||||
{
|
||||
return self::CACHE_KEY . $suffix;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue