1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Merodiro\Friendships; |
4
|
|
|
|
5
|
|
|
trait Friendable |
6
|
|
|
{ |
7
|
34 |
|
public function checkFriendship($user) |
8
|
|
|
{ |
9
|
34 |
|
if ($this->id == $user->id) { |
|
|
|
|
10
|
2 |
|
return 'same_user'; |
11
|
|
|
} |
12
|
|
|
|
13
|
32 |
|
$friendship = Friendship::betweenUsers($this, $user) |
14
|
32 |
|
->first(); |
15
|
|
|
|
16
|
32 |
|
if (!$friendship) { |
17
|
32 |
|
return 'not_friends'; |
18
|
|
|
} |
19
|
|
|
|
20
|
30 |
|
if ($friendship->status == 1) { |
21
|
8 |
|
return 'friends'; |
22
|
|
|
} |
23
|
30 |
|
if ($friendship->requester == $this->id) { |
24
|
10 |
|
return 'waiting'; |
25
|
|
|
} |
26
|
28 |
|
if ($friendship->user_requested == $this->id) { |
27
|
28 |
|
return 'pending'; |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
34 |
|
public function addFriend($recipient) |
32
|
|
|
{ |
33
|
34 |
|
$friendshipStatus = $this->checkFriendship($recipient); |
34
|
|
|
|
35
|
34 |
|
if ($friendshipStatus == 'not_friends') { |
36
|
32 |
|
Friendship::create([ |
37
|
32 |
|
'requester' => $this->id, |
38
|
32 |
|
'user_requested' => $recipient->id, |
39
|
|
|
]); |
40
|
32 |
|
event('friendrequest.sent', [$this, $recipient]); |
41
|
|
|
} |
42
|
|
|
|
43
|
34 |
|
return $friendshipStatus == 'not_friends'; |
44
|
|
|
} |
45
|
|
|
|
46
|
22 |
|
public function acceptFriend($sender) |
47
|
|
|
{ |
48
|
22 |
|
$friendshipStatus = $this->checkFriendship($sender); |
49
|
|
|
|
50
|
22 |
|
if ($friendshipStatus == 'pending') { |
51
|
20 |
|
Friendship::betweenUsers($this, $sender) |
52
|
20 |
|
->update(['status' => 1]); |
53
|
20 |
|
event('friendrequest.accepted', [$this, $sender]); |
54
|
|
|
} |
55
|
|
|
|
56
|
22 |
|
return $friendshipStatus == 'pending'; |
57
|
|
|
} |
58
|
|
|
|
59
|
10 |
|
public function deleteFriend($user) |
60
|
|
|
{ |
61
|
10 |
|
$friendshipStatus = $this->checkFriendship($user); |
62
|
|
|
|
63
|
10 |
|
if ($friendshipStatus != 'not_friends') { |
64
|
10 |
|
Friendship::betweenUsers($this, $user) |
65
|
10 |
|
->delete(); |
66
|
10 |
|
event('friendship.deleted', [$this, $user]); |
67
|
|
|
} |
68
|
|
|
|
69
|
10 |
|
return $friendshipStatus != 'not_friends'; |
70
|
|
|
} |
71
|
|
|
|
72
|
6 |
|
public function friends_ids() |
73
|
|
|
{ |
74
|
|
|
$friendsIds = Friendship::where(function ($query) { |
75
|
6 |
|
$query->whereSender($this); |
76
|
|
|
})->orWhere(function ($query) { |
77
|
6 |
|
$query->whereRecipient($this); |
78
|
6 |
|
})->accepted(1)->get(['user_requested', 'requester'])->toArray(); |
79
|
|
|
|
80
|
|
|
$friendsIds = collect($friendsIds)->flatten()->unique()->reject(function ($id) { |
81
|
6 |
|
return $id == $this->id; |
82
|
6 |
|
}); |
83
|
6 |
|
return $friendsIds; |
84
|
|
|
} |
85
|
|
|
|
86
|
4 |
|
public function friends() |
87
|
|
|
{ |
88
|
4 |
|
$friendsIds = $this->friends_ids(); |
89
|
|
|
|
90
|
4 |
|
return static::whereIn('id', $friendsIds) |
91
|
4 |
|
->distinct() |
92
|
4 |
|
->get(); |
93
|
|
|
} |
94
|
|
|
|
95
|
14 |
View Code Duplication |
public function friendRequestsReceived() |
|
|
|
|
96
|
|
|
{ |
97
|
14 |
|
$senders = Friendship::whereRecipient($this) |
98
|
14 |
|
->accepted(0) |
99
|
14 |
|
->get(['requester']) |
100
|
14 |
|
->toArray(); |
101
|
|
|
|
102
|
14 |
|
return static::whereIn('id', $senders) |
103
|
14 |
|
->get(); |
104
|
|
|
} |
105
|
|
|
|
106
|
8 |
View Code Duplication |
public function friendRequestsSent() |
|
|
|
|
107
|
|
|
{ |
108
|
8 |
|
$recipients = Friendship::whereSender($this) |
109
|
8 |
|
->accepted(0) |
110
|
8 |
|
->get(['user_requested']) |
111
|
8 |
|
->toArray(); |
112
|
|
|
|
113
|
8 |
|
return static::whereIn('id', $recipients) |
114
|
8 |
|
->get(); |
115
|
|
|
} |
116
|
|
|
|
117
|
4 |
|
public function isFriendsWith($user) |
118
|
|
|
{ |
119
|
4 |
|
$friendshipStatus = $this->checkFriendship($user); |
120
|
|
|
|
121
|
4 |
|
return $friendshipStatus === 'friends'; |
122
|
|
|
} |
123
|
|
|
|
124
|
2 |
|
public function mutualFriends($user) |
125
|
|
|
{ |
126
|
2 |
|
$userFriends = $user->friends_ids(); |
127
|
2 |
|
$friends = $this->friends_ids(); |
128
|
|
|
|
129
|
2 |
|
$mutualIds = $userFriends->intersect($friends); |
130
|
|
|
|
131
|
2 |
|
return static::whereIn('id', $mutualIds)->get(); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: