Skip to content

Commit

Permalink
Add user caching from event (#694)
Browse files Browse the repository at this point in the history
* Add user caching from event

* add from member

* add from member

* update GUILD_MEMBER_ADD user caching

* Simplified code into common functions
  • Loading branch information
SQKo authored Jan 29, 2022
1 parent 4430d41 commit 13f03de
Show file tree
Hide file tree
Showing 18 changed files with 77 additions and 23 deletions.
15 changes: 15 additions & 0 deletions src/Discord/WebSockets/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,21 @@ public function __construct(Http $http, Factory $factory, Discord $discord)
*/
abstract public function handle(Deferred &$deferred, $data);

/**
* Cache User repository from Event data
*
* @param object $userdata
*/
protected function cacheUser($userdata)
{
// User caching
if ($user = $this->discord->users->get('id', $userdata->id)) {
$user->fill((array) $userdata);
} else {
$this->discord->users->pushItem($this->factory->part(\Discord\Parts\User\User::class, (array) $userdata, true));
}
}

public function __debugInfo(): array
{
return [];
Expand Down
2 changes: 2 additions & 0 deletions src/Discord/WebSockets/Events/GuildBanAdd.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public function handle(Deferred &$deferred, $data): void
$this->discord->guilds->push($guild);
}

$this->cacheUser($data->user);

$deferred->resolve($ban);
}
}
2 changes: 2 additions & 0 deletions src/Discord/WebSockets/Events/GuildBanRemove.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public function handle(Deferred &$deferred, $data): void
$this->discord->guilds->push($guild);
}

$this->cacheUser($data->user);

$deferred->resolve($ban);
}
}
5 changes: 4 additions & 1 deletion src/Discord/WebSockets/Events/GuildEmojisUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ public function handle(Deferred &$deferred, $data): void
}

foreach ($data->emojis as $emoji) {
if (! isset($emoji->user) && $oldPart = $oldParts->offsetGet($emoji->id)) {
if (isset($emoji->user)) {
// User caching from emoji uploader
$this->cacheUser($emoji->user);
} elseif ($oldPart = $oldParts->offsetGet($emoji->id)) {
$emoji->user = $oldPart->user;
}
$emojiPart = $this->factory->create(Emoji::class, $emoji, true);
Expand Down
3 changes: 2 additions & 1 deletion src/Discord/WebSockets/Events/GuildMemberAdd.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public function handle(Deferred &$deferred, $data): void
++$guild->member_count;
}

$this->discord->users->push($member->user);
$this->cacheUser($data->user);

$deferred->resolve($member);
}
}
2 changes: 2 additions & 0 deletions src/Discord/WebSockets/Events/GuildMemberRemove.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public function handle(Deferred &$deferred, $data): void
$this->discord->guilds->push($guild);
}

$this->cacheUser($data->user);

$deferred->resolve($member);
}
}
4 changes: 1 addition & 3 deletions src/Discord/WebSockets/Events/GuildMemberUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ public function handle(Deferred &$deferred, $data): void
$guild->members->push($memberPart);
}

if ($user = $this->discord->users->get('id', $data->user->id)) {
$user->fill((array) $data->user);
}
$this->cacheUser($data->user);

$deferred->resolve([$memberPart, $old]);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Discord/WebSockets/Events/GuildScheduledEventCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public function handle(Deferred &$deferred, $data): void
$guild->guild_scheduled_events->push($scheduled_event);
}

if (isset($data->creator)) {
$this->cacheUser($data->creator);
}

$deferred->resolve($scheduled_event);
}
}
4 changes: 4 additions & 0 deletions src/Discord/WebSockets/Events/GuildScheduledEventDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public function handle(Deferred &$deferred, $data): void
$guild->guild_scheduled_events->pull($scheduled_event->id);
}

if (isset($data->creator)) {
$this->cacheUser($data->creator);
}

$deferred->resolve($scheduled_event);
}
}
2 changes: 2 additions & 0 deletions src/Discord/WebSockets/Events/GuildScheduledEventUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public function handle(Deferred &$deferred, $data): void
$guild->guild_scheduled_events->push($scheduled_event);
}

$this->cacheUser($data->creator);

$deferred->resolve([$scheduled_event, $old]);
}
}
5 changes: 4 additions & 1 deletion src/Discord/WebSockets/Events/GuildStickersUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ public function handle(Deferred &$deferred, $data): void
}

foreach ($data->stickers as $sticker) {
if (! isset($sticker->user) && $oldPart = $oldParts->offsetGet($sticker->id)) {
if (isset($sticker->user)) {
// User caching from sticker uploader
$this->cacheUser($sticker->user);
} elseif($oldPart = $oldParts->offsetGet($sticker->id)) {
$sticker->user = $oldPart->user;
}
$stickerPart = $this->factory->create(Sticker::class, $sticker, true);
Expand Down
9 changes: 2 additions & 7 deletions src/Discord/WebSockets/Events/IntegrationCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,8 @@ public function handle(Deferred &$deferred, $data): void
$guild->integrations->pushItem($integration);
}

// User caching
if (! isset($data->user)) {
if ($user = $this->discord->users->get('id', $data->user->id)) {
$user->fill((array) $data->user);
} else {
$this->discord->users->pushItem($this->factory->part(User::class, (array) $data->user, true));
}
if (isset($data->user)) {
$this->cacheUser($data->user);
}

$deferred->resolve($integration);
Expand Down
9 changes: 2 additions & 7 deletions src/Discord/WebSockets/Events/IntegrationUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,8 @@ public function handle(Deferred &$deferred, $data): void
$guild->integrations->pushItem($integration);
}

// User caching
if (! isset($data->user)) {
if ($user = $this->discord->users->get('id', $data->user->id)) {
$user->fill((array) $data->user);
} else {
$this->discord->users->pushItem($this->factory->part(User::class, (array) $data->user, true));
}
if (isset($data->user)) {
$this->cacheUser($data->user);
}

$deferred->resolve($integration);
Expand Down
13 changes: 11 additions & 2 deletions src/Discord/WebSockets/Events/InteractionCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public function handle(Deferred &$deferred, $data): void
if ($userPart = $this->discord->users->get('id', $snowflake)) {
$userPart->fill((array) $user);
} else {
$userPart = $this->factory->create(User::class, $user, true);
$this->discord->users->pushItem($userPart);
$this->discord->users->pushItem($this->factory->part(User::class, (array) $user, true));
}
}

Expand Down Expand Up @@ -59,6 +58,16 @@ public function handle(Deferred &$deferred, $data): void
}
}

if (isset($data->member->user)) {
// User caching from member
$this->cacheUser($data->member->user);
}

if (isset($data->user)) {
// User caching from user dm
$this->cacheUser($data->user);
}

$deferred->resolve($interaction);
}
}
10 changes: 10 additions & 0 deletions src/Discord/WebSockets/Events/InviteCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ public function handle(Deferred &$deferred, $data): void
{
$invite = $this->factory->create(Invite::class, $data, true);

if (isset($data->inviter)) {
// User caching from inviter
$this->cacheUser($data->inviter);
}

if (isset($data->target_user)) {
// User caching from target user
$this->cacheUser($data->target_user);
}

$deferred->resolve($invite);
}
}
5 changes: 4 additions & 1 deletion src/Discord/WebSockets/Events/MessageReactionAdd.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Discord\Parts\WebSockets\MessageReaction;
use Discord\WebSockets\Event;
use Discord\Helpers\Deferred;
use Discord\Parts\Channel\Reaction;

class MessageReactionAdd extends Event
{
Expand Down Expand Up @@ -53,6 +52,10 @@ public function handle(Deferred &$deferred, $data): void
}
}

if (isset($data->member->user)) {
$this->cacheUser($data->member->user);
}

$deferred->resolve($reaction);
}
}
4 changes: 4 additions & 0 deletions src/Discord/WebSockets/Events/TypingStart.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public function handle(Deferred &$deferred, $data): void
{
$typing = $this->factory->create(TypingStartPart::class, $data, true);

if (isset($data->member->user)) {
$this->cacheUser($data->member->user);
}

$deferred->resolve($typing);
}
}
2 changes: 2 additions & 0 deletions src/Discord/WebSockets/Events/VoiceStateUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public function handle(Deferred &$deferred, $data): void
$this->discord->guilds->offsetSet($state->guild->id, $state->guild);
}

$this->cacheUser($data->member->user);

$deferred->resolve([$state, $old_state]);
}
}

0 comments on commit 13f03de

Please sign in to comment.