Skip to content

Commit

Permalink
Changing event subscribers to always register, but still conditionall…
Browse files Browse the repository at this point in the history
…y check whether the methods should be handled.
  • Loading branch information
RTippin committed Aug 4, 2021
1 parent 4060678 commit 692256b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 29 deletions.
14 changes: 11 additions & 3 deletions src/Listeners/CallSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function subscribe(Dispatcher $events): void
*/
public function teardownCall(CallEndedEvent $event): void
{
if (Messenger::getCallSubscriber('enabled')) {
if ($this->isEnabled()) {
Messenger::getCallSubscriber('queued')
? TeardownCall::dispatch($event)->onQueue(Messenger::getCallSubscriber('channel'))
: TeardownCall::dispatchSync($event);
Expand All @@ -43,7 +43,7 @@ public function teardownCall(CallEndedEvent $event): void
*/
public function endCallIfEmpty(CallLeftEvent $event): void
{
if (Messenger::getCallSubscriber('enabled')) {
if ($this->isEnabled()) {
Messenger::getCallSubscriber('queued')
? EndCallIfEmpty::dispatch($event)->onQueue(Messenger::getCallSubscriber('channel'))
: EndCallIfEmpty::dispatchSync($event);
Expand All @@ -55,10 +55,18 @@ public function endCallIfEmpty(CallLeftEvent $event): void
*/
public function setupCall(CallStartedEvent $event): void
{
if (Messenger::getCallSubscriber('enabled') && ! $event->call->setup_complete) {
if ($this->isEnabled() && ! $event->call->setup_complete) {
Messenger::getCallSubscriber('queued')
? SetupCall::dispatch($event)->onQueue(Messenger::getCallSubscriber('channel'))
: SetupCall::dispatchSync($event);
}
}

/**
* @return bool
*/
private function isEnabled(): bool
{
return Messenger::getCallSubscriber('enabled');
}
}
37 changes: 22 additions & 15 deletions src/Listeners/SystemMessageSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function subscribe(Dispatcher $events): void
*/
public function callEndedMessage(CallEndedEvent $event): void
{
if (Messenger::getSystemMessageSubscriber('enabled')) {
if ($this->isEnabled()) {
Messenger::getSystemMessageSubscriber('queued')
? CallEndedMessage::dispatch($event)->onQueue(Messenger::getSystemMessageSubscriber('channel'))
: CallEndedMessage::dispatchSync($event);
Expand All @@ -76,7 +76,7 @@ public function callEndedMessage(CallEndedEvent $event): void
*/
public function demotedAdminMessage(DemotedAdminEvent $event): void
{
if (Messenger::getSystemMessageSubscriber('enabled')) {
if ($this->isEnabled()) {
Messenger::getSystemMessageSubscriber('queued')
? DemotedAdminMessage::dispatch($event)->onQueue(Messenger::getSystemMessageSubscriber('channel'))
: DemotedAdminMessage::dispatchSync($event);
Expand All @@ -88,7 +88,7 @@ public function demotedAdminMessage(DemotedAdminEvent $event): void
*/
public function joinedWithInviteMessage(InviteUsedEvent $event): void
{
if (Messenger::getSystemMessageSubscriber('enabled')) {
if ($this->isEnabled()) {
Messenger::getSystemMessageSubscriber('queued')
? JoinedWithInviteMessage::dispatch($event)->onQueue(Messenger::getSystemMessageSubscriber('channel'))
: JoinedWithInviteMessage::dispatchSync($event);
Expand All @@ -100,7 +100,7 @@ public function joinedWithInviteMessage(InviteUsedEvent $event): void
*/
public function participantsAddedMessage(ParticipantsAddedEvent $event): void
{
if (Messenger::getSystemMessageSubscriber('enabled')) {
if ($this->isEnabled()) {
Messenger::getSystemMessageSubscriber('queued')
? ParticipantsAddedMessage::dispatch($event)->onQueue(Messenger::getSystemMessageSubscriber('channel'))
: ParticipantsAddedMessage::dispatchSync($event);
Expand All @@ -112,7 +112,7 @@ public function participantsAddedMessage(ParticipantsAddedEvent $event): void
*/
public function promotedAdminMessage(PromotedAdminEvent $event): void
{
if (Messenger::getSystemMessageSubscriber('enabled')) {
if ($this->isEnabled()) {
Messenger::getSystemMessageSubscriber('queued')
? PromotedAdminMessage::dispatch($event)->onQueue(Messenger::getSystemMessageSubscriber('channel'))
: PromotedAdminMessage::dispatchSync($event);
Expand All @@ -124,7 +124,7 @@ public function promotedAdminMessage(PromotedAdminEvent $event): void
*/
public function removedFromThreadMessage(RemovedFromThreadEvent $event): void
{
if (Messenger::getSystemMessageSubscriber('enabled')) {
if ($this->isEnabled()) {
Messenger::getSystemMessageSubscriber('queued')
? RemovedFromThreadMessage::dispatch($event)->onQueue(Messenger::getSystemMessageSubscriber('channel'))
: RemovedFromThreadMessage::dispatchSync($event);
Expand All @@ -136,7 +136,7 @@ public function removedFromThreadMessage(RemovedFromThreadEvent $event): void
*/
public function threadArchivedMessage(ThreadArchivedEvent $event): void
{
if (Messenger::getSystemMessageSubscriber('enabled')) {
if ($this->isEnabled()) {
Messenger::getSystemMessageSubscriber('queued')
? ThreadArchivedMessage::dispatch($event)->onQueue(Messenger::getSystemMessageSubscriber('channel'))
: ThreadArchivedMessage::dispatchSync($event);
Expand All @@ -148,7 +148,7 @@ public function threadArchivedMessage(ThreadArchivedEvent $event): void
*/
public function threadAvatarMessage(ThreadAvatarEvent $event): void
{
if (Messenger::getSystemMessageSubscriber('enabled')) {
if ($this->isEnabled()) {
Messenger::getSystemMessageSubscriber('queued')
? ThreadAvatarMessage::dispatch($event)->onQueue(Messenger::getSystemMessageSubscriber('channel'))
: ThreadAvatarMessage::dispatchSync($event);
Expand All @@ -160,7 +160,7 @@ public function threadAvatarMessage(ThreadAvatarEvent $event): void
*/
public function threadLeftMessage(ThreadLeftEvent $event): void
{
if (Messenger::getSystemMessageSubscriber('enabled')) {
if ($this->isEnabled()) {
Messenger::getSystemMessageSubscriber('queued')
? ThreadLeftMessage::dispatch($event)->onQueue(Messenger::getSystemMessageSubscriber('channel'))
: ThreadLeftMessage::dispatchSync($event);
Expand All @@ -172,7 +172,7 @@ public function threadLeftMessage(ThreadLeftEvent $event): void
*/
public function threadNameMessage(ThreadSettingsEvent $event): void
{
if (Messenger::getSystemMessageSubscriber('enabled') && $event->nameChanged) {
if ($this->isEnabled() && $event->nameChanged) {
Messenger::getSystemMessageSubscriber('queued')
? ThreadNameMessage::dispatch($event)->onQueue(Messenger::getSystemMessageSubscriber('channel'))
: ThreadNameMessage::dispatchSync($event);
Expand All @@ -184,7 +184,7 @@ public function threadNameMessage(ThreadSettingsEvent $event): void
*/
public function botAddedMessage(NewBotEvent $event): void
{
if (Messenger::getSystemMessageSubscriber('enabled')) {
if ($this->isEnabled()) {
Messenger::getSystemMessageSubscriber('queued')
? BotAddedMessage::dispatch($event)->onQueue(Messenger::getSystemMessageSubscriber('channel'))
: BotAddedMessage::dispatchSync($event);
Expand All @@ -196,8 +196,7 @@ public function botAddedMessage(NewBotEvent $event): void
*/
public function botNameMessage(BotUpdatedEvent $event): void
{
if (Messenger::getSystemMessageSubscriber('enabled')
&& $event->originalName !== $event->bot->name) {
if ($this->isEnabled() && $event->originalName !== $event->bot->name) {
Messenger::getSystemMessageSubscriber('queued')
? BotNameMessage::dispatch($event)->onQueue(Messenger::getSystemMessageSubscriber('channel'))
: BotNameMessage::dispatchSync($event);
Expand All @@ -209,7 +208,7 @@ public function botNameMessage(BotUpdatedEvent $event): void
*/
public function botAvatarMessage(BotAvatarEvent $event): void
{
if (Messenger::getSystemMessageSubscriber('enabled')) {
if ($this->isEnabled()) {
Messenger::getSystemMessageSubscriber('queued')
? BotAvatarMessage::dispatch($event)->onQueue(Messenger::getSystemMessageSubscriber('channel'))
: BotAvatarMessage::dispatchSync($event);
Expand All @@ -221,10 +220,18 @@ public function botAvatarMessage(BotAvatarEvent $event): void
*/
public function botRemovedMessage(BotArchivedEvent $event): void
{
if (Messenger::getSystemMessageSubscriber('enabled')) {
if ($this->isEnabled()) {
Messenger::getSystemMessageSubscriber('queued')
? BotRemovedMessage::dispatch($event)->onQueue(Messenger::getSystemMessageSubscriber('channel'))
: BotRemovedMessage::dispatchSync($event);
}
}

/**
* @return bool
*/
private function isEnabled(): bool
{
return Messenger::getSystemMessageSubscriber('enabled');
}
}
14 changes: 3 additions & 11 deletions src/MessengerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,9 @@ private function registerSubscribers()
{
$events = $this->app->make(Dispatcher::class);

if (config('messenger.calling.subscriber.enabled')) {
$events->subscribe(CallSubscriber::class);
}

if (config('messenger.system_messages.subscriber.enabled')) {
$events->subscribe(SystemMessageSubscriber::class);
}

if (config('messenger.bots.subscriber.enabled')) {
$events->subscribe(BotSubscriber::class);
}
$events->subscribe(CallSubscriber::class);
$events->subscribe(SystemMessageSubscriber::class);
$events->subscribe(BotSubscriber::class);
}

/**
Expand Down

0 comments on commit 692256b

Please sign in to comment.