feat: add reciprocal user blocking
This commit is contained in:
+29
-2
@@ -12,9 +12,11 @@ use Filament\Models\Contracts\HasAvatar;
|
||||
use Filament\Panel;
|
||||
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Illuminate\Contracts\Translation\HasLocalePreference;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Concerns\HasUlids;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
||||
@@ -152,20 +154,45 @@ class User extends Authenticatable implements FilamentUser, HasAvatar, HasLocale
|
||||
return $this->morphMany(Report::class, 'reportable');
|
||||
}
|
||||
|
||||
public function followers(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
||||
public function followers(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(self::class, 'follows', 'following_id', 'follower_id')
|
||||
->withPivot('status')
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
public function following(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
||||
public function following(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(self::class, 'follows', 'follower_id', 'following_id')
|
||||
->withPivot('status')
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
public function blockedUsers(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(self::class, 'user_blocks', 'blocker_id', 'blocked_id')
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
public function blockedByUsers(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(self::class, 'user_blocks', 'blocked_id', 'blocker_id')
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
public function scopeWithoutBlockingRelationshipWith(Builder $query, User $user): Builder
|
||||
{
|
||||
return $query
|
||||
->whereDoesntHave('blockedByUsers', fn (Builder $blockedByQuery): Builder => $blockedByQuery->whereKey($user->getKey()))
|
||||
->whereDoesntHave('blockedUsers', fn (Builder $blockedQuery): Builder => $blockedQuery->whereKey($user->getKey()));
|
||||
}
|
||||
|
||||
public function hasBlockingRelationshipWith(User $user): bool
|
||||
{
|
||||
return $this->blockedUsers()->whereKey($user->getKey())->exists()
|
||||
|| $this->blockedByUsers()->whereKey($user->getKey())->exists();
|
||||
}
|
||||
|
||||
public function moderationCase(): MorphOne
|
||||
{
|
||||
return $this->morphOne(ModerationCase::class, 'caseable');
|
||||
|
||||
Reference in New Issue
Block a user