# アクセスできなくなったチャンネルからユーザーを自動的に削除する必要があります

**URL:** <https://meta.discourse.org/t/users-should-be-automatically-removed-from-channels-they-can-no-longer-access/233446>\
**Category:** Bug\
**Tags:** chat\
**Created:** [2022 年 7 月 15 日午後 4:36 UTC](https://meta.discourse.org/t/users-should-be-automatically-removed-from-channels-they-can-no-longer-access/233446 "2022-07-15T16:36:43Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![mcwumbly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mcwumbly/32/103861_2.png) [@mcwumbly](https://meta.discourse.org/u/mcwumbly)\
**Post date:** [2025 年 10 月 30 日午前 1:58 UTC](https://meta.discourse.org/t/users-should-be-automatically-removed-from-channels-they-can-no-longer-access/233446/8 "2025-10-30T01:58:37Z")

</div>

待てよ…ここに戻ってきていなかったけど、これに関連する変更がいくつかマージされたよ：

> <https://github.com/discourse/discourse/pull/20344>
>
> There are many situations that may cause users to lose permission to
> send messa…ges in a chat channel. Until now we have relied on security
> checks in \`Chat::ChatChannelFetcher\` to remove channels which the
> user may have a \`UserChatChannelMembership\` record for but which
> they do not have access to.
> 
> This commit takes a more proactive approach. Now any of these following
> \`DiscourseEvent\` triggers may cause \`UserChatChannelMembership\`
> records to be deleted:
> 
> \* \`category\_updated\` - Permissions of the category changed
> (i.e. CategoryGroup records changed)
> \* \`user\_removed\_from\_group\` - Means the user may not be able to access the
> channel based on \`GroupUser\` or also \`chat\_allowed\_groups\`
> \* \`site\_setting\_changed\` - The \`chat\_allowed\_groups\` was updated, some
> users may no longer be in groups that can access chat.
> \* \`group\_destroyed\` - Means the user may not be able to access the
> channel based on \`GroupUser\` or also \`chat\_allowed\_groups\`
> 
> All of these are handled in a distinct service run in a background
> job. Users removed are logged via \`StaffActionLog\` and then we
> publish messages on a per-channel basis to users who had their
> memberships deleted.
> 
> When the user has a channel they are kicked from open, we show
> this dialog.
> 
> \<img src="https://user-images.githubusercontent.com/920448/221748981-248e6d89-1b81-454d-9744-b0d0e68a6c2e.png" width="400"\>
> 
> When they click OK we redirect them either:
> 
> \* To their first other public channel, if they have any followed
> \* The chat browse page if they don't
> 
> This is to save on tons of requests from kicked out users getting messages
> from other channels.
> 
> When the user does not have the kicked channel open, we can just
> silently yoink it out of their sidebar and turn off subscriptions.

> <https://github.com/discourse/discourse/pull/29193>
>
> Chat channels that are linked to a category can be set to automatically join use…rs.
> 
> This is handled by subscribing to the following events
> 
> \- group\_destroyed
> \- user\_seen
> \- user\_confirmed\_email
> \- user\_added\_to\_group
> \- user\_removed\_from\_group
> \- category\_updated
> \- site\_setting\_changed (for \`chat\_allowed\_groups\`)
> 
> As well as a
> 
> \- hourly background job (\`AutoJoinUsers\`)
> \- \`CreateCategoryChannel\` service
> \- \`UpdateChannel\` service
> 
> There was however two issues with the current implementation
> 
> 1. We were triggering a lot of background jobs, mostly because it was decided to batch to auto join/leave into groups of 1000 users, adding a lot of stress to the system
> 2. We had one "class" (a service or a background job) per "event" and all of them had slightly different ways to select users to join/leave, making it hard to keep everything in sync
> 
> This PR "simply" adds two new services. \`AutoJoinChannels\` and \`AutoLeaveChannels\` that takes care, in an efficient way, of all the cases when users might automatically join and leave a chat channel.
> 
> Every other changes come from the fact that we're now always calling either one of those services, depending on the event that happened.
> 
> In the making of these classes, a few bugs were encountered and fixed, notably
> 
> \- The \`chat\_allowed\_group\` site setting was missing the (dynamic) \`@staff\` group
> \- A user is only ever able to access chat channels if and only if they're part of a group listed in the \`chat\_allowed\_group\` site setting
> \- A category that has no associated "category groups" is only accessible to staff members (and not "Everyone")
> \- A silenced user should not be able to automatically join channels
> \- We should not attempt to automatically join users to deleted chat channels
> \- There is no need to automatically join users to chat channels that have already more than \`max\_chat\_auto\_joined\_users\` users
> 
> Internal - t/135259 & t/70607

---

_[View the full topic](https://meta.discourse.org/t/users-should-be-automatically-removed-from-channels-they-can-no-longer-access/233446)._
