1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the overtrue/laravel-follow |
5
|
|
|
* |
6
|
|
|
* (c) overtrue <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overtrue\LaravelFollow; |
13
|
|
|
|
14
|
|
|
use Illuminate\Database\Eloquent\Model; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Trait Followable. |
18
|
|
|
* |
19
|
|
|
* @property \Illuminate\Database\Eloquent\Collection $followings |
20
|
|
|
* @property \Illuminate\Database\Eloquent\Collection $followers |
21
|
|
|
*/ |
22
|
|
|
trait Followable |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @return bool |
26
|
|
|
*/ |
27
|
|
|
public function needsToApproveFollowRequests() |
28
|
|
|
{ |
29
|
|
|
return false; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param \Illuminate\Database\Eloquent\Model|int $user |
34
|
|
|
* |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
|
|
public function follow($user) |
38
|
|
|
{ |
39
|
|
|
$isPending = $user->needsToApproveFollowRequests() ?: false; |
40
|
|
|
|
41
|
|
|
$this->followings()->attach($user, [ |
42
|
|
|
'accepted_at' => $isPending ? null : now() |
43
|
|
|
]); |
44
|
|
|
|
45
|
|
|
return ['pending' => $isPending]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param \Illuminate\Database\Eloquent\Model|int $user |
50
|
|
|
*/ |
51
|
|
|
public function unfollow($user) |
52
|
|
|
{ |
53
|
|
|
$this->followings()->detach($user); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param \Illuminate\Database\Eloquent\Model|int $user |
58
|
|
|
* |
59
|
|
|
*/ |
60
|
|
|
public function toggleFollow($user) |
61
|
|
|
{ |
62
|
|
|
$this->isFollowing($user) ? $this->unfollow($user) : $this->follow($user); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param \Illuminate\Database\Eloquent\Model|int $user |
67
|
|
|
*/ |
68
|
|
|
public function rejectFollowRequestFrom($user) |
69
|
|
|
{ |
70
|
|
|
$this->followers()->detach($user); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param \Illuminate\Database\Eloquent\Model|int $user |
75
|
|
|
*/ |
76
|
|
|
public function acceptFollowRequestFrom($user) |
77
|
|
|
{ |
78
|
|
|
$this->followers()->updateExistingPivot($user, ['accepted_at' => now()]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param \Illuminate\Database\Eloquent\Model|int $user |
83
|
|
|
*/ |
84
|
|
|
public function hasRequestedToFollow(Model $user): bool |
85
|
|
|
{ |
86
|
|
|
if ($user instanceof Model) { |
|
|
|
|
87
|
|
|
$user = $user->getKey(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/* @var \Illuminate\Database\Eloquent\Model $this */ |
91
|
|
|
if ($this->relationLoaded('followings')) { |
92
|
|
|
return $this->followings |
93
|
|
|
->whereNull('pivot.accepted_at') |
|
|
|
|
94
|
|
|
->contains($user); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->followings() |
|
|
|
|
98
|
|
|
->wherePivot('accepted_at', null) |
99
|
|
|
->where($this->getQualifiedKeyName(), $user) |
100
|
|
|
->exists(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param \Illuminate\Database\Eloquent\Model|int $user |
105
|
|
|
* |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
|
|
public function isFollowing($user) |
109
|
|
|
{ |
110
|
|
|
if ($user instanceof Model) { |
111
|
|
|
$user = $user->getKey(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/* @var \Illuminate\Database\Eloquent\Model $this */ |
115
|
|
|
if ($this->relationLoaded('followings')) { |
116
|
|
|
return $this->followings |
117
|
|
|
->whereNotNull('pivot.accepted_at') |
118
|
|
|
->contains($user); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $this->followings() |
|
|
|
|
122
|
|
|
->wherePivot('accepted_at', '!=', null) |
123
|
|
|
->where($this->getQualifiedKeyName(), $user) |
124
|
|
|
->exists(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param \Illuminate\Database\Eloquent\Model|int $user |
129
|
|
|
* |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
|
|
public function isFollowedBy($user) |
133
|
|
|
{ |
134
|
|
|
if ($user instanceof Model) { |
135
|
|
|
$user = $user->getKey(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/* @var \Illuminate\Database\Eloquent\Model $this */ |
139
|
|
|
if ($this->relationLoaded('followers')) { |
140
|
|
|
return $this->followers |
141
|
|
|
->whereNotNull('pivot.accepted_at') |
|
|
|
|
142
|
|
|
->contains($user); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $this->followers() |
|
|
|
|
146
|
|
|
->wherePivot('accepted_at', '!=', null) |
147
|
|
|
->where($this->getQualifiedKeyName(), $user) |
148
|
|
|
->exists(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param \Illuminate\Database\Eloquent\Model|int $user |
153
|
|
|
* |
154
|
|
|
* @return bool |
155
|
|
|
*/ |
156
|
|
|
public function areFollowingEachOther($user) |
157
|
|
|
{ |
158
|
|
|
/* @var \Illuminate\Database\Eloquent\Model $user*/ |
159
|
|
|
return $this->isFollowing($user) && $this->isFollowedBy($user); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
164
|
|
|
*/ |
165
|
|
|
public function followers() |
166
|
|
|
{ |
167
|
|
|
/* @var \Illuminate\Database\Eloquent\Model $this */ |
168
|
|
|
return $this->belongsToMany( |
169
|
|
|
__CLASS__, |
170
|
|
|
\config('follow.relation_table', 'user_follower'), |
171
|
|
|
'following_id', |
172
|
|
|
'follower_id' |
173
|
|
|
)->withPivot('accepted_at')->withTimestamps()->using(UserFollower::class); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
178
|
|
|
*/ |
179
|
|
|
public function followings() |
180
|
|
|
{ |
181
|
|
|
/* @var \Illuminate\Database\Eloquent\Model $this */ |
182
|
|
|
return $this->belongsToMany( |
183
|
|
|
__CLASS__, |
184
|
|
|
\config('follow.relation_table', 'user_follower'), |
185
|
|
|
'follower_id', |
186
|
|
|
'following_id' |
187
|
|
|
)->withPivot('accepted_at')->withTimestamps()->using(UserFollower::class); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|