Skip to content

Commit 145302c

Browse files
committed
fix: Groups not using dirty relationship
1 parent fff512b commit 145302c

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

src/Addons/Groups.php

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,42 +23,45 @@ public function setGroups(): void
2323
{
2424
$user = $this->flarum->user();
2525
if ($user->id !== null && $user->id !== 0) {
26-
$groups = [];
26+
$user_groups = $user->getDirtyRelationships()['groups'] ?? null;
27+
if ($user_groups !== null) {
28+
$groups = [];
2729

28-
/** Search flarum groups - @noinspection NullPointerExceptionInspection */
29-
$flarum_groups = Arr::pluck(
30-
$this->flarum->api->groups()->request()->collect()->all(),
31-
'attributes.nameSingular',
32-
'id'
33-
);
30+
/** Search flarum groups - @noinspection NullPointerExceptionInspection */
31+
$flarum_groups = Arr::pluck(
32+
$this->flarum->api->groups()->request()->collect()->all(),
33+
'attributes.nameSingular',
34+
'id'
35+
);
3436

35-
foreach ($user->relationships->groups as $group) {
36-
if (empty($group) || !is_string($group)) {
37-
continue;
38-
}
37+
foreach ($user_groups as $group) {
38+
if (empty($group) || !is_string($group)) {
39+
continue;
40+
}
3941

40-
// Find ID of the group
41-
$id = array_key_first(Arr::where($flarum_groups, static fn ($name): bool => $name === $group));
42-
// If it doesn't exists, create it
43-
if ($id === 0 || $id === '' || $id === null) {
44-
$id = $this->createGroup($group);
45-
}
42+
// Find ID of the group
43+
$id = array_key_first(Arr::where($flarum_groups, static fn($name): bool => $name === $group));
44+
// If it doesn't exists, create it
45+
if ($id === 0 || $id === '' || $id === null) {
46+
$id = $this->createGroup($group);
47+
}
4648

47-
$groups[] = [
48-
'type' => 'groups',
49-
'id' => $id
50-
];
51-
}
49+
$groups[] = [
50+
'type' => 'groups',
51+
'id' => $id
52+
];
53+
}
5254

53-
$this->flarum->api->users($user->id)->patch([
54-
'relationships' => [
55-
'groups' => [
56-
'data' => $groups
55+
$this->flarum->api->users($user->id)->patch([
56+
'relationships' => [
57+
'groups' => [
58+
'data' => $groups
59+
],
5760
],
58-
],
59-
])->request();
61+
])->request();
6062

61-
$user->relationships->clearDirty();
63+
$user->relationships->clearDirty();
64+
}
6265
}
6366
}
6467

0 commit comments

Comments
 (0)