Skip to content

Commit

Permalink
Sync user to discourse on edit of synced fields
Browse files Browse the repository at this point in the history
  • Loading branch information
rjackson committed Sep 5, 2024
1 parent 33b37da commit 79471d1
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 2 deletions.
26 changes: 26 additions & 0 deletions app/Events/MemberDiscourseParamsChanged.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace BB\Events;

use BB\Entities\User;
use Illuminate\Queue\SerializesModels;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;

class MemberDiscourseParamsChanged
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/** @var User */
public $user;

/**
* Create a new event instance.
*
* @return void
*/
public function __construct(User $user)
{
$this->user = $user;
}
}
4 changes: 2 additions & 2 deletions app/Jobs/DiscourseSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ public function handle(Client $client)

protected function sso_params()
{
$this->user->isActive();

return [
'external_id' => $this->user->id,

// Make sure UserObserver@saved triggers MemberDiscourseParamsChanged for any field that are used here
// TODO: Flip dependency here to pull these from one centralised list on user? Or some intermediary class?
'email' => $this->user->email,
'username' => $this->user->name,
'name' => $this->user->suppress_real_name ? $this->user->name : ($this->user->given_name . " " . $this->user->family_name),
Expand Down
9 changes: 9 additions & 0 deletions app/Listeners/DiscourseSyncSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use BB\Events\MemberBecameActive;
use BB\Events\MemberBecameInactive;
use BB\Events\MemberDiscourseParamsChanged;
use BB\Jobs\DiscourseSync;
use Illuminate\Auth\Events\Login as Login;

Expand All @@ -23,6 +24,10 @@ public function subscribe($events)
MemberBecameInactive::class,
[DiscourseSyncSubscriber::class, 'handleMembershipBecameInactive']
);
$events->listen(
MemberDiscourseParamsChanged::class,
[DiscourseSyncSubscriber::class, 'handleMemberDiscourseParamsChanged']
);
}

public static function handleLogin(Login $event)
Expand All @@ -39,4 +44,8 @@ public static function handleMembershipBecameInactive(MemberBecameInactive $even
{
DiscourseSync::dispatch($event->user);
}
public static function handleMemberDiscourseParamsChanged(MemberDiscourseParamsChanged $event)
{
DiscourseSync::dispatch($event->user);
}
}
15 changes: 15 additions & 0 deletions app/Observer/UserObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use BB\Events\MemberBecameActive;
use BB\Events\MemberBecameInactive;
use BB\Events\MemberDiscourseParamsChanged;
use BB\Mailer\UserMailer;
use BB\Helpers\TelegramHelper;
use Illuminate\Support\Facades\Log;
Expand Down Expand Up @@ -62,6 +63,20 @@ public function saved($user)
if ($original['active'] === true && $user->active === false) {
event(new MemberBecameInactive($user));
}

// Make sure this list is in sync with the fields DiscourseSyncSubscriber uses
// TODO: Flip dependency here to pull these from one centralised list on user? Or some intermediary class?
$discourseFields = [
'email',
'name',
'suppress_real_name',
'given_name',
'family_name',
'banned',
];
if (!empty(array_intersect($discourseFields, array_keys($user->getDirty())))) {
event(new MemberDiscourseParamsChanged($user));
};
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/Listeners/DiscourseSyncSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use BB\Entities\User;
use BB\Events\MemberBecameInactive;
use BB\Events\MemberBecameActive;
use BB\Events\MemberDiscourseParamsChanged;
use BB\Jobs\DiscourseSync;
use Illuminate\Auth\Events\Login;
use Illuminate\Support\Facades\Bus;
Expand Down Expand Up @@ -60,4 +61,21 @@ public function testsMemberBecameInactive()
return $job->user->display_name === 'Inactive Ivan';
});
}

public function testsMemberDiscourseParamsChanged()
{
Bus::fake();

$user = factory(User::class)->create([
'display_name' => 'Updated Ulysses'
]);

Event::dispatch(
(new MemberDiscourseParamsChanged($user))
);

Bus::assertDispatched(DiscourseSync::class, function ($job) {
return $job->user->display_name === 'Updated Ulysses';
});
}
}
18 changes: 18 additions & 0 deletions tests/unit/Observer/UserObserverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use BB\Entities\User;
use BB\Events\MemberBecameActive;
use BB\Events\MemberBecameInactive;
use BB\Events\MemberDiscourseParamsChanged;
use Illuminate\Support\Facades\Event;
use Tests\TestCase;

Expand Down Expand Up @@ -45,4 +46,21 @@ public function testBroadcastsMemberBecameInactive()
return $event->user->id === $user->id;
});
}

public function testBroadcastsMemberDiscourseParamsChanged()
{
Event::fake([MemberDiscourseParamsChanged::class]);

$user = factory(User::class)->create([
'given_name' => 'John',
]);

$user->update([
'given_name' => 'Johnny',
]);

Event::assertDispatched(MemberDiscourseParamsChanged::class, function ($event) use ($user) {
return $event->user->id === $user->id;
});
}
}

0 comments on commit 79471d1

Please sign in to comment.