Skip to content

Commit

Permalink
Don't send user ids for announcement channels
Browse files Browse the repository at this point in the history
  • Loading branch information
nanaya committed Sep 26, 2024
1 parent 9368487 commit 4de1a0d
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 16 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/Chat/ChannelsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function show($channelId)
'channel' => json_item($channel, ChannelTransformer::forUser($user), ChannelTransformer::LISTING_INCLUDES),
// TODO: probably going to need a better way to list/fetch/update users on larger channels without sending user on every message.
'users' => json_collection(
$channel->visibleUsers($user)->loadMissing(UserCompactTransformer::CARD_INCLUDES_PRELOAD),
$channel->visibleUsers()->loadMissing(UserCompactTransformer::CARD_INCLUDES_PRELOAD),
new UserCompactTransformer(),
UserCompactTransformer::CARD_INCLUDES
),
Expand Down
8 changes: 2 additions & 6 deletions app/Models/Chat/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,9 @@ public function users(): Collection
});
}

public function visibleUsers(?User $user)
public function visibleUsers(): Collection
{
if ($this->isPM() || $this->isAnnouncement() && priv_check_user($user, 'ChatAnnounce', $this)->can()) {
return $this->users();
}

return new Collection();
return $this->isPM() ? $this->users() : new Collection();
}

public function scopePublic($query)
Expand Down
9 changes: 1 addition & 8 deletions app/Transformers/Chat/ChannelTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,7 @@ public function includeRecentMessages(Channel $channel)

public function includeUsers(Channel $channel)
{
if (
$channel->isPM()
|| $channel->isAnnouncement() && priv_check_user($this->user, 'ChatAnnounce', $channel)->can()
) {
return $this->primitive($channel->userIds());
}

return $this->primitive([]);
return $this->primitive($channel->isPM() ? $channel->userIds() : []);
}

public function includeUuid(Channel $channel)
Expand Down
2 changes: 1 addition & 1 deletion tests/Models/Chat/ChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public function testPublicChannelDoesNotShowUsers()
$channel = Channel::factory()->type('public', [$user])->create();

$this->assertSame(1, $channel->users()->count());
$this->assertEmpty($channel->visibleUsers($user));
$this->assertEmpty($channel->visibleUsers());
}

// test add/removeUser resets any memoized values
Expand Down

0 comments on commit 4de1a0d

Please sign in to comment.