Newer
Older

Yassine Doghri
committed
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
<?php
declare(strict_types=1);
/**
* @copyright 2020 Ad Aures
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
use App\Entities\Podcast;
use App\Models\ActorModel;
use App\Models\PodcastModel;
use CodeIgniter\Shield\Entities\User;
use Modules\Auth\Auth;
use Modules\Fediverse\Entities\Actor;
use Modules\Fediverse\Models\NotificationModel;
if (! function_exists('auth')) {
/**
* Provides convenient access to the main Auth class for CodeIgniter Shield.
*
* @param string|null $alias Authenticator alias
*/
function auth(?string $alias = null): Auth
{
/** @var Auth $auth */
$auth = service('auth');
return $auth->setAuthenticator($alias);
}
}
if (! function_exists('set_interact_as_actor')) {
/**
* Sets the actor id of which the user is acting as
*/
function set_interact_as_actor(int $actorId): void
{
if (auth()->loggedIn()) {
session()
->set('interact_as_actor_id', $actorId);
}
}
}
if (! function_exists('remove_interact_as_actor')) {
/**
* Removes the actor id of which the user is acting as
*/
function remove_interact_as_actor(): void
{
session()->remove('interact_as_actor_id');
}
}
if (! function_exists('interact_as_actor_id')) {
/**
* Sets the podcast id of which the user is acting as
*/
function interact_as_actor_id(): ?int
{
return session()->get('interact_as_actor_id');
}
}
if (! function_exists('interact_as_actor')) {
/**
* Get the actor the user is currently interacting as
*/
function interact_as_actor(): Actor | false
{
if (! auth()->loggedIn()) {
return false;
}
$session = session();
if (! $session->has('interact_as_actor_id')) {
return false;
}
return model(ActorModel::class, false)->getActorById($session->get('interact_as_actor_id'));
}
}
if (! function_exists('can_user_interact')) {
function can_user_interact(): bool
{
return (bool) interact_as_actor();
}
}
if (! function_exists('add_podcast_group')) {
function add_podcast_group(User $user, int $podcastId, string $group): User
{
$podcastGroup = 'podcast#' . $podcastId . '-' . $group;
return $user->addGroup($podcastGroup);
}
}
if (! function_exists('get_instance_group')) {
function get_instance_group(User $user): ?string
{
$instanceGroups = array_filter($user->getGroups() ?? [], static function ($group): bool {
return ! str_starts_with($group, 'podcast#');
});
if ($instanceGroups === []) {
return null;
}
$instanceGroup = array_shift($instanceGroups);
// Verify that a user belongs to one group only!
if ($instanceGroups !== []) {
// remove any other group the user belongs to
$user->removeGroup(...$instanceGroups);
}
return $instanceGroup;
}
}
if (! function_exists('set_instance_group')) {
function set_instance_group(User $user, string $group): User
{
// remove old instance group
if (get_instance_group($user)) {
$user->removeGroup(get_instance_group($user));
}
// set new group
return $user->addGroup($group);
}
}
if (! function_exists('get_podcast_group')) {
function get_podcast_group(User $user, int $podcastId): ?string
{
$podcastGroups = array_filter($user->getGroups() ?? [], static function ($group) use ($podcastId): bool {
return str_starts_with($group, "podcast#{$podcastId}");
});
if ($podcastGroups === []) {
return null;
}
$podcastGroup = array_shift($podcastGroups);
// Verify that a user belongs to one group only!
if ($podcastGroups !== []) {
// remove any other group the user belongs to
$user->removeGroup(...$podcastGroups);
}
// strip the `podcast#{id}.` prefix when returning group
return substr((string) $podcastGroup, strlen('podcast#' . $podcastId . '-'));

Yassine Doghri
committed
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
}
}
if (! function_exists('set_podcast_group')) {
function set_podcast_group(User $user, int $podcastId, string $group): User
{
// remove old instance group
$user->removeGroup("podcast#{$podcastId}-" . get_podcast_group($user, $podcastId));
// set new group
return add_podcast_group($user, $podcastId, $group);
}
}
if (! function_exists('get_podcast_groups')) {
/**
* @return string[]
*/
function get_user_podcast_ids(User $user): array
{
$podcastGroups = array_filter($user->getGroups() ?? [], static function ($group): bool {
return str_starts_with($group, 'podcast#');
});
$userPodcastIds = [];
// extract all podcast ids from groups
foreach ($podcastGroups as $podcastGroup) {
// extract podcast id from group and add it to the list of ids
preg_match('~podcast#([0-9]+)-[a-z]+~', $podcastGroup, $matches);
$userPodcastIds[] = $matches[1];

Yassine Doghri
committed
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
}
return $userPodcastIds;
}
}
if (! function_exists('can_podcast')) {
function can_podcast(User $user, int $podcastId, string $permission): bool
{
return $user->can('podcast#' . $podcastId . '.' . $permission);
}
}
if (! function_exists('get_user_podcasts')) {
/**
* Returns the podcasts the user is contributing to
*
* @return Podcast[]
*/
function get_user_podcasts(User $user): array
{
return (new PodcastModel())->getUserPodcasts($user->id, get_user_podcast_ids($user));
}
}
if (! function_exists('get_podcasts_user_can_interact_with')) {
/**
* @return Podcast[]
*/
function get_podcasts_user_can_interact_with(User $user): array
{
$userPodcasts = (new PodcastModel())->getUserPodcasts($user->id, get_user_podcast_ids($user));
$hasInteractAsPrivilege = interact_as_actor_id() === null;
if ($userPodcasts === []) {
if ($hasInteractAsPrivilege) {
remove_interact_as_actor();
}
return [];
}
$isInteractAsPrivilegeLost = true;
$podcastsUserCanInteractWith = [];
foreach ($userPodcasts as $userPodcast) {
if (can_podcast($user, $userPodcast->id, 'interact-as')) {
if (interact_as_actor_id() === $userPodcast->actor_id) {
$isInteractAsPrivilegeLost = false;
}
$podcastsUserCanInteractWith[] = $userPodcast;
}
}
if ($podcastsUserCanInteractWith === []) {
if (interact_as_actor_id() !== null) {
remove_interact_as_actor();
}
return [];
}
// check if user has lost the interact as privilege for current podcast actor.
// --> Remove interact as if there's no podcast actor to interact as
// or set the first podcast actor the user can interact as
if ($isInteractAsPrivilegeLost) {
set_interact_as_actor($podcastsUserCanInteractWith[0]->actor_id);
}
return $podcastsUserCanInteractWith;
}
}
if (! function_exists('get_actor_ids_with_unread_notifications')) {
/**
* Returns the ids of the user's actors that have unread notifications
*
* @return int[]
*/
function get_actor_ids_with_unread_notifications(User $user): array
{
if (($userPodcasts = get_user_podcasts($user)) === []) {
return [];
}
$unreadNotifications = (new NotificationModel())->whereIn(
'target_actor_id',
array_column($userPodcasts, 'actor_id')
)
->where('read_at', null)
->findAll();
return array_column($unreadNotifications, 'target_actor_id');
}
}
if (! function_exists('get_group_title')) {
/**
* @return array<'title'|'description', string>
*/
function get_group_info(string $group, ?int $podcastId = null): array
{
if ($podcastId === null) {
return setting('AuthGroups.instanceGroups')[$group];
}
return setting('AuthGroups.podcastGroups')[$group];
}
}