Update UserObserver, add default domain blocks logic

This commit is contained in:
Daniel Supernault 2023-12-21 04:47:09 -07:00
parent 519c7a3735
commit fa0380ac3b
No known key found for this signature in database
GPG key ID: 23740873EE6F76A1

View file

@ -7,6 +7,9 @@ use App\Follower;
use App\Profile;
use App\User;
use App\UserSetting;
use App\Services\UserFilterService;
use App\Models\DefaultDomainBlock;
use App\Models\UserDomainBlock;
use App\Jobs\FollowPipeline\FollowPipeline;
use DB;
use App\Services\FollowerService;
@ -14,7 +17,18 @@ use App\Services\FollowerService;
class UserObserver
{
/**
* Listen to the User created event.
* Handle the notification "created" event.
*
* @param \App\User $user
* @return void
*/
public function created(User $user): void
{
$this->handleUser($user);
}
/**
* Listen to the User saved event.
*
* @param \App\User $user
*
@ -22,7 +36,38 @@ class UserObserver
*/
public function saved(User $user)
{
if($user->status == 'deleted') {
$this->handleUser($user);
}
/**
* Listen to the User updated event.
*
* @param \App\User $user
*
* @return void
*/
public function updated(User $user): void
{
$this->handleUser($user);
if($user->profile) {
$this->applyDefaultDomainBlocks($user);
}
}
/**
* Handle the user "deleted" event.
*
* @param \App\User $user
* @return void
*/
public function deleted(User $user)
{
FollowerService::delCache($user->profile_id);
}
protected function handleUser($user)
{
if(in_array($user->status, ['deleted', 'delete'])) {
return;
}
@ -49,9 +94,11 @@ class UserObserver
$profile->private_key = $pki_private;
$profile->public_key = $pki_public;
$profile->save();
$this->applyDefaultDomainBlocks($user);
return $profile;
});
DB::transaction(function() use($user, $profile) {
$user = User::findOrFail($user->id);
$user->profile_id = $profile->id;
@ -92,14 +139,22 @@ class UserObserver
}
}
/**
* Handle the user "deleted" event.
*
* @param \App\User $user
* @return void
*/
public function deleted(User $user)
protected function applyDefaultDomainBlocks($user)
{
FollowerService::delCache($user->profile_id);
if($user->profile_id == null) {
return;
}
$defaultDomainBlocks = DefaultDomainBlock::pluck('domain')->toArray();
if(!$defaultDomainBlocks || !count($defaultDomainBlocks)) {
return;
}
foreach($defaultDomainBlocks as $domain) {
UserDomainBlock::updateOrCreate([
'profile_id' => $user->profile_id,
'domain' => strtolower(trim($domain))
]);
}
}
}