1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\ActiveRecord; |
4
|
|
|
|
5
|
|
|
use Ffcms\Core\App as MainApp; |
6
|
|
|
use Ffcms\Core\Arch\ActiveModel; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Blacklist. Active record for user blacklist table |
10
|
|
|
* @package Apps\ActiveRecord |
11
|
|
|
* @property int $id |
12
|
|
|
* @property int $user_id |
13
|
|
|
* @property int $target_id |
14
|
|
|
* @property string $comment |
15
|
|
|
* @property string $created_at |
16
|
|
|
* @property string $updated_at |
17
|
|
|
* @property User $targetUser |
18
|
|
|
*/ |
19
|
|
|
class Blacklist extends ActiveModel |
20
|
|
|
{ |
21
|
|
|
protected $casts = [ |
22
|
|
|
'id' => 'integer', |
23
|
|
|
'user_id' => 'integer', |
24
|
|
|
'target_id' => 'integer', |
25
|
|
|
'comment' => 'string' |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Check if current user have in blacklist target_id user |
30
|
|
|
* @param int $currentId |
31
|
|
|
* @param int $targetId |
32
|
|
|
* @return bool |
33
|
|
|
*/ |
34
|
|
|
public static function have($currentId, $targetId): bool |
35
|
|
|
{ |
36
|
|
|
return self::where('user_id', $currentId) |
37
|
|
|
->where('target_id', $targetId) |
38
|
|
|
->count() > 0; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Check if user1 or user2 have in themself blacklists |
43
|
|
|
* @param int $user1 |
44
|
|
|
* @param int $user2 |
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
|
|
public static function check($user1, $user2): bool |
48
|
|
|
{ |
49
|
|
|
$query = self::where(function ($query) use ($user1, $user2) { |
50
|
|
|
$query->where('user_id', $user1) |
51
|
|
|
->where('target_id', $user2); |
52
|
|
|
})->orWhere(function ($query) use ($user1, $user2) { |
53
|
|
|
$query->where('user_id', $user2) |
54
|
|
|
->where('target_id', $user1); |
55
|
|
|
}); |
56
|
|
|
|
57
|
|
|
return $query->count() < 1; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get target user object relation |
62
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
63
|
|
|
*/ |
64
|
|
|
public function targetUser() |
65
|
|
|
{ |
66
|
|
|
return $this->belongsTo(User::class, 'target_id'); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|