Skip to content

Commit

Permalink
Merge pull request #1101 from discord-php/feat-thread-member-guild-me…
Browse files Browse the repository at this point in the history
…mber

Add Thread Member::$member
  • Loading branch information
SQKo authored Aug 13, 2023
2 parents 58356d2 + c4fd2fb commit da686d2
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 8 deletions.
51 changes: 46 additions & 5 deletions src/Discord/Parts/Thread/Member.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

use Carbon\Carbon;
use Discord\Http\Endpoint;
use Discord\Parts\Guild\Guild;
use Discord\Parts\Part;
use Discord\Parts\User\Member as GuildMember;
use Discord\Parts\User\User;
use React\Promise\ExtendedPromiseInterface;

Expand All @@ -25,11 +27,12 @@
*
* @since 7.0.0
*
* @property string|null $id ID of the thread.
* @property string|null $user_id ID of the user that the member object represents.
* @property-read User|null $user The user that the member object represents.
* @property Carbon $join_timestamp The time that the member joined the thread.
* @property int $flags Flags relating to the member. Only used for client notifications.
* @property string|null $id ID of the thread.
* @property string|null $user_id ID of the user that the member object represents.
* @property-read User|null $user The user that the member object represents.
* @property Carbon $join_timestamp The time that the member joined the thread.
* @property int $flags Flags relating to the member. Only used for client notifications.
* @property-read GuildMember|null $member Additional information about the user.
*/
class Member extends Part
{
Expand All @@ -41,6 +44,9 @@ class Member extends Part
'user_id',
'join_timestamp',
'flags',
'member',
// @internal and events only
'guild_id',
];

/**
Expand All @@ -65,6 +71,41 @@ protected function getJoinTimestampAttribute(): Carbon
return new Carbon($this->attributes['join_timestamp']);
}

/**
* Returns the guild member that the thread member represents.
*
* @return GuildMember|null
*
* @since 10.0.0
*/
protected function getMemberAttribute(): ?GuildMember
{
if (! isset($this->attributes['member'])) {
if ($guild = $this->getGuildAttribute()) {
return $guild->members->get('id', $this->user_id);
}

return null;
}

$memberData = (array) $this->attributes['member'];
if (isset($this->guild_id)) {
$memberData['guild_id'] = $this->guild_id;
}

return $this->factory->part(Member::class, $memberData, true);
}

/**
* Returns the guild attribute based on internal `guild_id`.
*
* @return Guild|null The guild attribute.
*/
private function getGuildAttribute(): ?Guild
{
return $this->discord->guilds->get('id', $this->guild_id);
}

/**
* Attempts to remove the member from the thread.
*
Expand Down
1 change: 1 addition & 0 deletions src/Discord/Parts/Thread/Thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ protected function afterConstruct(): void
$memberPart = $this->members->create((array) $this->attributes['member'] + [
'id' => $this->id,
'user_id' => $this->discord->id,
'guild_id' => $this->guild_id,
], $this->created);
$memberPart->created = &$this->created;
$this->members->pushItem($memberPart);
Expand Down
2 changes: 1 addition & 1 deletion src/Discord/Repository/Channel/ThreadRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected function cacheFreshen($response): ExtendedPromiseInterface
foreach ($items as $thread) {
foreach ($members as $member) {
if ($member->id == $thread->id) {
$thread->members->cache->set($member->id, $thread->members->create($member, true));
$thread->members->cache->set($member->id, $thread->members->create((array) $member + ['guild_id' => $thread->guild_id], true));
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Discord/WebSockets/Events/ThreadListSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function handle($data)
foreach ($data->members as $member) {
/** @var ?Thread */
if ($threadPart = $threadParts[$member->id] ?? null) {
$threadPart->members->set($member->user_id, $threadPart->members->create($member, true));
$threadPart->members->set($member->user_id, $threadPart->members->create((array) $member + ['guild_id' => $data->guild_id], true));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Discord/WebSockets/Events/ThreadMembersUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function handle($data)
}

foreach ($data->added_members ?? [] as $member) {
$thread->members->set($member->user_id, $thread->members->create($member, true));
$thread->members->set($member->user_id, $thread->members->create((array) $member + ['guild_id' => $data->guild_id], true));

if (isset($member->member)) {
$this->cacheMember($guild->members, (array) $member->member);
Expand Down

0 comments on commit da686d2

Please sign in to comment.