1
|
|
|
<?php |
2
|
|
|
namespace Domains; |
3
|
|
|
|
4
|
|
|
use Carbon\Carbon; |
5
|
|
|
use Illuminate\Auth\Authenticatable; |
6
|
|
|
use Illuminate\Auth\Passwords\CanResetPassword; |
7
|
|
|
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; |
8
|
|
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; |
9
|
|
|
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; |
10
|
|
|
use Illuminate\Foundation\Auth\Access\Authorizable; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class User |
14
|
|
|
* @deprecated |
15
|
|
|
* |
16
|
|
|
* @property int $id |
17
|
|
|
* @property string $gitter_id |
18
|
|
|
* @property string $name |
19
|
|
|
* @property string $avatar |
20
|
|
|
* @property string $url |
21
|
|
|
* @property string $login |
22
|
|
|
* @property string $email |
23
|
|
|
* @property string $password |
24
|
|
|
* @property string $remember_token |
25
|
|
|
* @property Carbon $created_at |
26
|
|
|
* @property Carbon $updated_at |
27
|
|
|
* |
28
|
|
|
* === Accessors === |
29
|
|
|
* |
30
|
|
|
* @property-read string $karma_text |
31
|
|
|
* @property-read string $thanks_text |
32
|
|
|
* |
33
|
|
|
* === Relations === |
34
|
|
|
* |
35
|
|
|
* @property-read Achieve[] $achievements |
36
|
|
|
* @property-read Karma[] $karma |
37
|
|
|
* @property-read Karma[] $thanks |
38
|
|
|
* |
39
|
|
|
*/ |
40
|
|
|
class User extends \Eloquent implements |
41
|
|
|
AuthenticatableContract, |
42
|
|
|
AuthorizableContract, |
43
|
|
|
CanResetPasswordContract |
44
|
|
|
{ |
45
|
|
|
use Authenticatable, |
46
|
|
|
Authorizable, |
47
|
|
|
CanResetPassword; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
protected $table = 'users'; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
protected $hidden = ['password', 'remember_token']; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var array |
61
|
|
|
*/ |
62
|
|
|
protected $fillable = ['gitter_id', 'url', 'login', 'name', 'avatar']; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* |
66
|
|
|
*/ |
67
|
|
|
protected static function boot() |
68
|
|
|
{ |
69
|
|
|
parent::boot(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
74
|
|
|
*/ |
75
|
|
|
public function achievements() |
76
|
|
|
{ |
77
|
|
|
return $this->hasMany(Achieve::class, 'user_id', 'id'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
82
|
|
|
*/ |
83
|
|
|
public function karma() |
84
|
|
|
{ |
85
|
|
|
return $this->hasMany(Karma::class, 'user_target_id', 'id'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function getKarmaTextAttribute() |
92
|
|
|
{ |
93
|
|
|
$karma = $this->karma->count(); |
|
|
|
|
94
|
|
|
|
95
|
|
|
return ($karma > 0 ? '+' : '') . $karma; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
100
|
|
|
*/ |
101
|
|
|
public function thanks() |
102
|
|
|
{ |
103
|
|
|
return $this->hasMany(Karma::class, 'user_id', 'id'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public function getThanksTextAttribute() |
110
|
|
|
{ |
111
|
|
|
return $this->thanks->count(); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param $roomId |
116
|
|
|
* @return Carbon |
117
|
|
|
*/ |
118
|
|
|
public function getLastKarmaTimeForRoom($roomId) |
119
|
|
|
{ |
120
|
|
|
$result = Karma::query() |
121
|
|
|
->where('user_target_id', $this->id) |
122
|
|
|
->where('room_id', $roomId) |
123
|
|
|
->orderBy('created_at', 'desc') |
124
|
|
|
->first(); |
125
|
|
|
|
126
|
|
|
if ($result) { |
127
|
|
|
return $result->created_at; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return Carbon::createFromTimestamp(0); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param User $user |
135
|
|
|
* @param Message $message |
136
|
|
|
* @return static |
137
|
|
|
*/ |
138
|
|
|
public function addKarmaTo(User $user, Message $message) |
139
|
|
|
{ |
140
|
|
|
return Karma::create([ |
141
|
|
|
'room_id' => $message->room_id, |
142
|
|
|
'message_id' => $message->gitter_id, |
143
|
|
|
'user_id' => $this->id, |
144
|
|
|
'user_target_id' => $user->id, |
145
|
|
|
'created_at' => $message->created_at, |
146
|
|
|
]); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param User $user |
151
|
|
|
* |
152
|
|
|
* @return bool |
153
|
|
|
*/ |
154
|
|
|
public function is(User $user) |
155
|
|
|
{ |
156
|
|
|
return $this->login === $user->login; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
|
|
public function isBot() |
163
|
|
|
{ |
164
|
|
|
return $this->is(\Auth::user()); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.