forked from hootlex/laravel-friendships
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFriendable.php
379 lines (330 loc) · 9.82 KB
/
Friendable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
namespace Hootlex\Friendships\Traits;
use Hootlex\Friendships\Models\Friendship;
use Hootlex\Friendships\Status;
use Illuminate\Database\Eloquent\Model;
trait Friendable
{
/**
* @param Model $recipient
*
* @return \Hootlex\Friendships\Models\Friendship|false
*/
public function befriend(Model $recipient)
{
if (!$this->canBefriend($recipient)) {
return false;
}
$friendship = (new Friendship)->fillRecipient($recipient)->fill([
'status' => Status::PENDING,
]);
$this->friendshipRequests()->save($friendship);
return $friendship;
}
/**
* @param Model $recipient
*
* @return bool
*/
public function unfriend(Model $recipient)
{
return $this->findFriendship($recipient)->delete();
}
/**
* @param Model $recipient
*
* @return bool
*/
public function hasFriendRequestFrom(Model $recipient)
{
return Friendship::whereRecipient($this)->whereSender($recipient)->whereStatus(Status::PENDING)->exists();
}
/**
* @param Model $recipient
*
* @return bool
*/
public function isFriendWith(Model $recipient)
{
return $this->findFriendship($recipient)->where('status', Status::ACCEPTED)->exists();
}
/**
* @param Model $recipient
*
* @return bool|int
*/
public function acceptFriendRequest(Model $recipient)
{
return $this->findFriendship($recipient)->whereRecipient($this)->update([
'status' => Status::ACCEPTED,
]);
}
/**
* @param Model $recipient
*
* @return bool|int
*/
public function denyFriendRequest(Model $recipient)
{
return $this->findFriendship($recipient)->whereRecipient($this)->update([
'status' => Status::DENIED,
]);
}
/**
* @param Model $recipient
*
* @return \Hootlex\Friendships\Models\Friendship
*/
public function blockFriend(Model $recipient)
{
//if there is a friendship between two users delete it
$this->findFriendship($recipient)->delete();
$friendship = (new Friendship)->fillRecipient($recipient)->fill([
'status' => Status::BLOCKED,
]);
return $this->friendshipRequests()->save($friendship);
}
/**
* @param Model $recipient
*
* @return mixed
*/
public function unblockFriend(Model $recipient)
{
return $this->findFriendship($recipient)->delete();
}
/**
* @param Model $recipient
*
* @return \Hootlex\Friendships\Models\Friendship
*/
public function getFriendship(Model $recipient)
{
return $this->findFriendship($recipient)->first();
}
/**
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getAllFriendships()
{
return $this->findRequests()->get();
}
/**
* @return \Illuminate\Database\Eloquent\Collection
*
*/
public function getPendingFriendships()
{
return $this->findRequests(Status::PENDING)->get();
}
/**
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getAcceptedFriendships()
{
return $this->findRequests(Status::ACCEPTED)->get();
}
/**
* @return \Illuminate\Database\Eloquent\Collection
*
*/
public function getDeniedFriendships()
{
return $this->findRequests(Status::DENIED)->get();
}
/**
* @return \Illuminate\Database\Eloquent\Collection
*
*/
public function getBlockedFriendships()
{
return $this->findRequests(Status::BLOCKED)->get();
}
/**
* @param Model $recipient
*
* @return bool
*/
public function hasBlocked(Model $recipient)
{
return $this->friendshipRequests()->whereRecipient($recipient)->whereStatus(Status::BLOCKED)->exists();
}
/**
* @param Model $recipient
*
* @return bool
*/
public function isBlockedBy(Model $recipient)
{
return $recipient->hasBlocked($this);
}
/**
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getFriendRequests()
{
return Friendship::whereRecipient($this)->whereStatus(Status::PENDING)->get();
}
/**
* This method will not return Friendship models
* It will return the 'friends' models. ex: App\User
*
* @param int $perPage Number
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getFriends($perPage = 0)
{
if ($perPage == 0) {
return $this->friendships()->get();
} else {
return $this->friendships()->paginate($perPage);
}
}
/**
* This method will not return Friendship models
* It will return the 'friends' models. ex: App\User
*
* @param int $perPage Number
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getFriendsOfFriends($perPage = 0)
{
if ($perPage == 0) {
return $this->friendsOfFriendsQueryBuilder()->get();
} else {
return $this->friendsOfFriendsQueryBuilder()->paginate($perPage);
}
}
/**
* Get the number of friends
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getFriendsCount()
{
$friendsCount = $this->findRequests(Status::ACCEPTED)->count();
return $friendsCount;
}
/**
* @param Model $recipient
*
* @return bool
*/
public function canBefriend($recipient)
{
// if user has Blocked the recipient and changed his mind
// he can send a friend request after unblocking
if($this->hasBlocked($recipient)){
$this->unblockFriend($recipient);
return true;
}
// if sender has a friendship with the recipient return false
if ($friendship = $this->getFriendship($recipient)) {
// if previous friendship was Denied then let the user send fr
if($friendship->status != Status::DENIED){
return false;
}
}
return true;
}
/**
* @param Model $recipient
*
* @return \Illuminate\Database\Eloquent\Builder
*/
private function findFriendship(Model $recipient)
{
return Friendship::betweenModels($this, $recipient);
}
/**
* @param $status
*
* @return \Illuminate\Database\Eloquent\Collection
*/
private function findRequests($status = null)
{
$query = Friendship::where(function ($query) {
$query->where(function ($q) {
$q->whereSender($this);
})->orWhere(function ($q) {
$q->whereRecipient($this);
});
});
//if $status is passed, add where clause
if(!is_null($status)){
$query->where('status', $status);
}
return $query;
}
/**
* Get the query builder for friendsOfFriends ('friend' model)
*
* @return \Illuminate\Database\Eloquent\Builder
*/
private function friendsOfFriendsQueryBuilder()
{
$friendships = $this->findRequests(Status::ACCEPTED)->get(['sender_id', 'recipient_id']);
$recipients = $friendships->lists('recipient_id')->all();
$senders = $friendships->lists('sender_id')->all();
$friendIds = array_unique(array_merge($recipients, $senders));
$fofs = Friendship::where('status', Status::ACCEPTED)
->where(function ($query) use ($friendIds) {
$query->where(function ($q) use ($friendIds) {
$q->whereIn('sender_id', $friendIds);
})->orWhere(function ($q) use ($friendIds) {
$q->whereIn('recipient_id', $friendIds);
});
})->get(['sender_id', 'recipient_id']);
$fofIds = array_unique(
array_merge($fofs->pluck('sender_id')->all(), $fofs->lists('recipient_id')->all())
);
// Alternative way using collection helpers
// $fofIds = array_unique(
// $fofs->map(function ($item) {
// return [$item->sender_id, $item->recipient_id];
// })->flatten()->all()
// );
return $this->whereIn('id', $fofIds)->whereNotIn('id', $friendIds);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
protected function friendshipRequests()
{
return $this->morphMany(Friendship::class, 'sender');
}
/**
* Get friendships of this user.
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function friendships()
{
$friendships = $this->findRequests(Status::ACCEPTED)->get(['sender_id', 'recipient_id']);
$recipients = $friendships->lists('recipient_id')->all();
$senders = $friendships->lists('sender_id')->all();
return $this->where('id', '!=', $this->getKey())->whereIn('id', array_merge($recipients, $senders));
}
/**
* Get requests of this user.
*
* @return \Illuminate\Database\Eloquent\Builder|Friendship
*/
public function requests()
{
$friendships = $this->findRequests();
return $friendships;
}
/**
* @deprecated Will be removed in version 2
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function friends()
{
@trigger_error(sprintf('The '.__METHOD__.' method was deprecated in version 1.1 and will be removed in version 2.0. You should implement this method yourself in %s.', get_class($this)), E_USER_DEPRECATED);
return $this->morphMany(Friendship::class, 'sender');
}
}