-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathUser.php
475 lines (401 loc) · 12.2 KB
/
User.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<?php
declare(strict_types=1);
namespace App\Models;
use App\Enums\TransactionStatus;
use App\Traits\HasProfilePhoto;
use App\Traits\HasSettings;
use App\Traits\HasUsername;
use App\Traits\Reacts;
use Filament\Models\Contracts\FilamentUser;
use Filament\Models\Contracts\HasAvatar;
use Filament\Models\Contracts\HasName;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Auth;
use Laravel\Sanctum\HasApiTokens;
use Laravel\Socialite\Contracts\User as SocialUser;
use LaravelFeature\Featurable\Featurable;
use LaravelFeature\Featurable\FeaturableInterface;
use QCod\Gamify\Gamify;
use Rinvex\Subscriptions\Traits\HasPlanSubscriptions;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\Permission\Traits\HasRoles;
/**
* @mixin IdeHelperUser
*/
class User extends Authenticatable implements MustVerifyEmail, HasMedia, FeaturableInterface, FilamentUser, HasName, HasAvatar
{
use Gamify;
use HasFactory;
use HasPlanSubscriptions;
use HasProfilePhoto;
use HasApiTokens;
use HasRoles;
use HasUsername;
use HasSettings;
use InteractsWithMedia;
use Notifiable;
use Reacts;
use Featurable;
protected $fillable = [
'name',
'email',
'username',
'password',
'bio',
'location',
'avatar',
'avatar_type',
'reputation',
'phone_number',
'github_profile',
'twitter_profile',
'linkedin_profile',
'website',
'last_login_at',
'last_login_ip',
'email_verified_at',
'published_at',
'opt_in',
];
protected $hidden = [
'password',
'remember_token',
'two_factor_recovery_codes',
'two_factor_secret',
];
protected $casts = [
'email_verified_at' => 'datetime',
'last_login_at' => 'datetime',
'settings' => 'array',
];
protected $appends = [
'profile_photo_url',
'roles_label',
'is_sponsor',
];
protected $withCount = [
'transactions'
];
public function hasProvider(string $provider): bool
{
foreach ($this->providers as $p) {
if ($p->provider == $provider) {
return true;
}
}
return false;
}
public function enterprise(): HasOne
{
return $this->hasOne(Enterprise::class);
}
public function hasEnterprise(): bool
{
return $this->enterprise !== null;
}
public function getRolesLabelAttribute(): string
{
$roles = $this->getRoleNames()->toArray();
if (count($roles)) {
return implode(', ', array_map(function ($item) {
return ucwords($item);
}, $roles));
}
return 'N/A';
}
public function getIsSponsorAttribute(): bool
{
if ($this->transactions_count > 0) {
$transaction = $this->transactions()
->where('status', TransactionStatus::COMPLETE->value)
->first();
return (bool) $transaction;
}
return false;
}
public function isAdmin(): bool
{
return $this->hasRole('admin');
}
public function isModerator(): bool
{
return $this->hasRole('moderator');
}
public function isEnterprise(): bool
{
return $this->hasRole('company');
}
public function isLoggedInUser(): bool
{
return $this->id === Auth::id();
}
public function canAccessFilament(): bool
{
return str_ends_with($this->email, '@laravel.cm') || $this->isModerator() || $this->isAdmin();
}
public function getFilamentName(): string
{
return $this->name;
}
public function getFilamentAvatarUrl(): ?string
{
return $this->profile_photo_url;
}
/**
* @return array{name: string, username: string, picture: string}
*/
public function profile(): array
{
return [
'name' => $this->name,
'username' => $this->username,
'picture' => (string) $this->profile_photo_url,
];
}
public function registerMediaCollections(): void
{
$this->addMediaCollection('avatar')
->singleFile()
->acceptsMimeTypes(['image/jpg', 'image/jpeg', 'image/png', 'image/gif']);
}
public static function findByEmailAddress(string $emailAddress): self
{
return static::where('email', $emailAddress)->firstOrFail();
}
public static function findOrCreateSocialUserProvider(SocialUser $socialUser, string $provider, string $role = 'user'): self
{
$socialEmail = $socialUser->getEmail() ?? "{$socialUser->getId()}@{$provider}.com";
$user = static::where('email', $socialEmail)->first();
if (! $user) {
$user = self::create([
'name' => $socialUser->getName() ?? $socialUser->getNickName() ?? $socialUser->getId(),
'email' => $socialEmail,
'username' => $socialUser->getNickName() ?? $socialUser->getId(),
'github_profile' => $provider === 'github' ? $socialUser->getNickName() : null,
'twitter_profile' => $provider === 'twitter' ? $socialUser->getNickName() : null,
'email_verified_at' => now(),
'avatar_type' => $provider,
]);
$user->assignRole($role);
}
return $user;
}
public function providers(): HasMany
{
return $this->hasMany(SocialAccount::class);
}
public function articles(): HasMany
{
return $this->hasMany(Article::class);
}
public function activities(): HasMany
{
return $this->hasMany(Activity::class);
}
public function threads(): HasMany
{
return $this->hasMany(Thread::class);
}
public function replyAble(): HasMany
{
return $this->hasMany(Reply::class);
}
public function discussions(): HasMany
{
return $this->hasMany(Discussion::class);
}
public function subscriptions(): HasMany
{
return $this->hasMany(Subscribe::class);
}
public function transactions(): HasMany
{
return $this->hasMany(Transaction::class);
}
public function deleteThreads(): void
{
// We need to explicitly iterate over the threads and delete them
// separately because all related models need to be deleted.
foreach ($this->threads as $thread) {
$thread->delete();
}
}
public function deleteReplies(): void
{
// We need to explicitly iterate over the replies and delete them
// separately because all related models need to be deleted.
foreach ($this->replyAble->all() as $reply) {
$reply->delete();
}
}
public function latestArticles(int $amount = 10): Collection
{
return $this->articles()->latest()->limit($amount)->get();
}
public function githubUsername(): ?string
{
return $this->github_profile;
}
public function twitter(): ?string
{
return $this->twitter_profile;
}
public function hasTwitterAccount(): bool
{
return ! empty($this->twitter());
}
public function linkedin(): ?string
{
return $this->linkedin_profile;
}
public function scopeModerators(Builder $query): Builder
{
return $query->whereHas('roles', function ($query) {
$query->where('name', '<>', 'user');
});
}
public function scopeWithoutRole(Builder $query): Builder
{
return $query->whereDoesntHave('roles');
}
public function scopeVerifiedUsers(Builder $query): Builder
{
return $query->whereNotNull('email_verified_at');
}
public function scopeUnVerifiedUsers(Builder $query): Builder
{
return $query->whereNull('email_verified_at');
}
public function hasPassword(): bool
{
$password = $this->getAuthPassword();
return $password !== '' && $password !== null;
}
public function delete()
{
$this->deleteThreads();
$this->deleteReplies();
parent::delete();
}
public function scopeHasActivity(Builder $query): Builder
{
return $query->where(function ($query) {
$query->has('threads')
->orHas('replyAble');
});
}
/**
* Route notifications for the Slack channel.
*
* @param \Illuminate\Notifications\Notification $notification
* @return string
*/
public function routeNotificationForSlack(Notification $notification): string
{
return config('lcm.slack.web_hook');
}
public function replies(): Collection
{
return $this->replyAble;
}
public function countReplies(): int
{
return $this->replyAble()->count();
}
public function countSolutions(): int
{
return $this->replyAble()->isSolution()->count();
}
public function countArticles(): int
{
return $this->articles()->approved()->count();
}
public function countDiscussions(): int
{
return $this->discussions()->count();
}
public function countThreads(): int
{
return $this->threads()->count();
}
public function scopeMostSolutions(Builder $query, int $inLastDays = null): Builder
{
return $query->withCount(['replyAble as solutions_count' => function ($query) use ($inLastDays) {
$query->where('replyable_type', 'threads')
->join('threads', 'threads.solution_reply_id', '=', 'replies.id');
if ($inLastDays) {
$query->where('replies.created_at', '>', now()->subDays($inLastDays));
}
return $query;
}])->orderBy('solutions_count', 'desc');
}
public function scopeMostSubmissions(Builder $query, int $inLastDays = null): Builder
{
return $query->withCount(['articles as articles_count' => function ($query) use ($inLastDays) {
if ($inLastDays) {
$query->where('articles.approved_at', '>', now()->subDays($inLastDays));
}
return $query;
}])->orderByDesc('articles_count');
}
/**
* Scope of most solutions in last days
*
* @param Builder<User> $query
* @param int $days
* @return Builder<User>
*/
public function scopeMostSolutionsInLastDays(Builder $query, int $days): Builder
{
return $query->mostSolutions($days);
}
/**
* Scope for most submissions in the last days.
*
* @param Builder<User> $query
* @param int $days
* @return Builder<User>
*/
public function scopeMostSubmissionsInLastDays(Builder $query, int $days): Builder
{
return $query->mostSubmissions($days);
}
/**
* Scope for all count values associate with a user.
*
* @param Builder<User> $query
* @return Builder<User>
*/
public function scopeWithCounts(Builder $query): Builder
{
return $query->withCount([
'discussions as discussions_count',
'articles as articles_count',
'threads as threads_count',
'replyAble as replies_count',
'replyAble as solutions_count' => function (Builder $query) {
return $query->join('threads', 'threads.solution_reply_id', '=', 'replies.id')
->where('replyable_type', 'thread');
},
]);
}
/**
* Scope to get the top contributors on discussions.
*
* @param Builder<User> $query
* @return Builder<User>
*/
public function scopeTopContributors(Builder $query): Builder
{
return $query->withCount(['discussions'])->orderByDesc('discussions_count');
}
}