-
-
Notifications
You must be signed in to change notification settings - Fork 345
/
Index.php
201 lines (161 loc) · 4.91 KB
/
Index.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
<?php
declare(strict_types=1);
namespace App\Livewire\Links;
use App\Jobs\UpdateUserAvatar;
use App\Models\Link;
use App\Models\User;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Facades\Cache;
use Illuminate\View\View;
use Livewire\Attributes\Locked;
use Livewire\Attributes\On;
use Livewire\Attributes\Renderless;
use Livewire\Component;
use Symfony\Component\HttpFoundation\IpUtils;
final class Index extends Component
{
/**
* The component's user ID.
*/
#[Locked]
public int $userId;
/**
* Increment the clicks counter.
*/
#[Renderless]
public function click(int $linkId): void
{
$ipAddress = type(request()->ip())->asString();
$cacheKey = IpUtils::anonymize($ipAddress).'-clicked-'.$linkId;
if (auth()->id() === $this->userId || Cache::has($cacheKey)) {
return;
}
Link::query()
->whereKey($linkId)
->increment('click_count');
Cache::put($cacheKey, true, now()->addDay());
}
/**
* Store the new order of the links.
*
* @param array<int, string> $sort
*/
public function storeSort(array $sort): void
{
$user = type(auth()->user())->as(User::class);
$sort = collect($sort)
->map(fn (string $linkId): ?int => $user->links->contains($linkId) ? ((int) $linkId) : null)
->filter()
->values()
->toArray();
$user->update([
'links_sort' => count($sort) === 0 ? null : $sort,
]);
}
/**
* Destroy the given link.
*
* @throws AuthorizationException
*/
public function destroy(int $linkId): void
{
$user = type(auth()->user())->as(User::class);
$link = Link::findOrFail($linkId);
$this->authorize('delete', $link);
$link->delete();
if (! $user->is_uploaded_avatar) {
UpdateUserAvatar::dispatch($user);
}
$this->dispatch('notification.created', message: 'Link deleted.');
}
/**
* Set visibility the given link.
*/
public function setVisibility(int $linkId): void
{
$link = Link::findOrFail($linkId);
$this->authorize('update', $link);
$link->update([
'is_visible' => ! $link->is_visible,
]);
}
/**
* Follow the given user.
*/
public function follow(int $targetId): void
{
if (! auth()->check()) {
$this->redirectRoute('login', navigate: true);
return;
}
$user = type(auth()->user())->as(User::class);
$target = User::findOrFail($targetId);
$this->authorize('follow', $target);
if ($target->followers()->where('follower_id', $user->id)->exists()) {
return;
}
$user->following()->attach($targetId);
$this->dispatch('user.followed');
}
/**
* Unfollow the given user.
*/
public function unfollow(int $targetId): void
{
if (! auth()->check()) {
$this->redirectRoute('login', navigate: true);
return;
}
$user = type(auth()->user())->as(User::class);
$target = User::findOrFail($targetId);
$this->authorize('unfollow', $target);
if ($target->followers()->where('follower_id', $user->id)->doesntExist()) {
return;
}
$user->following()->detach($targetId);
$this->dispatch('user.unfollowed');
}
/**
* Refresh the component.
*/
#[On('link.created')]
#[On('link.updated')]
#[On('link-settings.updated')]
#[On('following.updated')]
public function refresh(): void
{
//
}
/**
* Render the component.
*/
public function render(): View
{
$user = User::query()
->with(['links' => function (Relation $relation): void {
$relation->getQuery()
->when(auth()->id() !== $this->userId, function (Builder $query): void {
$query->where('is_visible', true);
});
}])
->withCount('followers')
->withCount('following')
->findOrFail($this->userId);
$sort = $user->links_sort;
return view('livewire.links.index', [
'user' => $user,
'questionsReceivedCount' => $user->questionsReceived()
->where('is_reported', false)
->where('is_ignored', false)
->where('answer', '!=', null)->count(),
'links' => $user->links->sortBy(function (Link $link) use ($sort): int {
if (($index = array_search($link->id, $sort)) === false) {
return 1_000_000 + $link->id;
}
return $index;
})->values(),
]);
}
}