Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Enable TL0 and anonymous chat access on new sites (#44543)

Penar Musaraj00f21b36e142
diff --git a/db/structure.sql b/db/structure.sql
index b0b4842a410..9ce21713936 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -25853,6 +25853,7 @@ ALTER TABLE ONLY public.ad_plugin_house_ads_groups
SET search_path TO "$user", public;
INSERT INTO "schema_migrations" (version) VALUES
+('20261013183208'),
('20261013010026'),
('20261013010022'),
('20261012021959'),
diff --git a/frontend/discourse/tests/fixtures/chat.js b/frontend/discourse/tests/fixtures/chat.js
new file mode 100644
index 00000000000..958e3d2351a
--- /dev/null
+++ b/frontend/discourse/tests/fixtures/chat.js
@@ -0,0 +1,8 @@
+export default {
+ "/chat/api/me/channels": {
+ public_channels: [],
+ direct_message_channels: [],
+ meta: { message_bus_last_ids: {} },
+ tracking: {},
+ },
+};
diff --git a/plugins/chat/config/settings.yml b/plugins/chat/config/settings.yml
index 37442909d56..31c50d1663a 100644
--- a/plugins/chat/config/settings.yml
+++ b/plugins/chat/config/settings.yml
@@ -16,7 +16,7 @@ chat:
chat_allowed_groups:
type: group_list
list_type: compact
- default: "1|2|11" # @admins, @moderators, @trust_level_1
+ default: "1|2|4|10" # @admins, @moderators, @anonymous_users, @trust_level_0
mandatory_values: "1|2" # @admins, @moderators
allow_any: false
client: true
diff --git a/plugins/chat/db/migrate/20261013183208_preserve_chat_allowed_groups_for_existing_sites.rb b/plugins/chat/db/migrate/20261013183208_preserve_chat_allowed_groups_for_existing_sites.rb
new file mode 100644
index 00000000000..0513182fa46
--- /dev/null
+++ b/plugins/chat/db/migrate/20261013183208_preserve_chat_allowed_groups_for_existing_sites.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+class PreserveChatAllowedGroupsForExistingSites < ActiveRecord::Migration[8.1]
+ def up
+ return if !Migration::Helpers.existing_site?
+
+ execute <<~SQL
+ INSERT INTO site_settings(name, data_type, value, created_at, updated_at)
+ VALUES('chat_allowed_groups', 20, '1|2|11', NOW(), NOW())
+ ON CONFLICT (name) DO NOTHING
+ SQL
+ end
+
+ def down
+ raise ActiveRecord::IrreversibleMigration
+ end
+end
diff --git a/plugins/chat/spec/components/chat/seeder_spec.rb b/plugins/chat/spec/components/chat/seeder_spec.rb
index 1df9f00f50e..a2276792d4c 100644
--- a/plugins/chat/spec/components/chat/seeder_spec.rb
+++ b/plugins/chat/spec/components/chat/seeder_spec.rb
@@ -10,35 +10,41 @@ describe Chat::Seeder do
Jobs.run_immediately!
end
- def assert_channel_was_correctly_seeded(channel, group, category)
+ def assert_channel_was_correctly_seeded(channel, users, category)
expect(channel).to be_present
expect(channel.auto_join_users).to eq(true)
expect(channel.name).to eq(category.name)
expect(category.custom_fields[Chat::HAS_CHAT_ENABLED]).to eq(true)
- expected_members_count = GroupUser.where(group: group).count
- memberships_count =
- Chat::UserChatChannelMembership.automatic.where(chat_channel: channel, following: true).count
+ member_ids =
+ Chat::UserChatChannelMembership
+ .automatic
+ .where(chat_channel: channel, following: true)
+ .pluck(:user_id)
- expect(memberships_count).to eq(expected_members_count)
+ expect(member_ids).to contain_exactly(*users.map(&:id))
end
it "seeds default channels" do
last_seen_at = 1.minute.ago
- # By default, `chat_allowed_groups` is set to admins, moderators, and TL1
- Fabricate(:user, last_seen_at:, groups: [Group[:everyone], Group[:admins]])
- Fabricate(:user, last_seen_at:, groups: [Group[:everyone], Group[:moderators]])
- Fabricate(:user, last_seen_at:, groups: [Group[:everyone], Group[:trust_level_1]])
+ admin = Fabricate(:admin, last_seen_at:, refresh_auto_groups: true)
+ moderator = Fabricate(:moderator, last_seen_at:, refresh_auto_groups: true)
+ tl0_user = Fabricate(:user, last_seen_at:, trust_level: 0)
+ tl1_user = Fabricate(:user, last_seen_at:, trust_level: 1)
Chat::Seeder.new.execute
staff_channel = Chat::Channel.find_by(chatable_id: staff_category)
general_channel = Chat::Channel.find_by(chatable_id: general_category)
- assert_channel_was_correctly_seeded(staff_channel, Group[:staff], staff_category)
- assert_channel_was_correctly_seeded(general_channel, Group[:everyone], general_category)
+ assert_channel_was_correctly_seeded(staff_channel, [admin, moderator], staff_category)
+ assert_channel_was_correctly_seeded(
+ general_channel,
+ [admin, moderator, tl0_user, tl1_user],
+ general_category,
+ )
expect(SiteSetting.needs_chat_seeded).to eq(false)
end
diff --git a/plugins/chat/spec/lib/chat/guardian_extensions_spec.rb b/plugins/chat/spec/lib/chat/guardian_extensions_spec.rb
index 32a9bb00905..d28460fca86 100644
--- a/plugins/chat/spec/lib/chat/guardian_extensions_spec.rb
+++ b/plugins/chat/spec/lib/chat/guardian_extensions_spec.rb
@@ -90,12 +90,23 @@ RSpec.describe Chat::GuardianExtensions do
end
end
- it "allows TL1 to chat by default and by extension higher trust levels" do
+ it "allows TL0 to chat by default and by extension higher trust levels" do
+ SiteSetting.chat_allowed_groups = SiteSetting.defaults[:chat_allowed_groups]
+ user.change_trust_level!(TrustLevel[0])
expect(guardian.can_chat?).to eq(true)
user.change_trust_level!(TrustLevel[3])
expect(guardian.can_chat?).to eq(true)
end
+ it "allows anonymous visitors to preview public channels but not send messages by default" do
+ SiteSetting.chat_allowed_groups = SiteSetting.defaults[:chat_allowed_groups]
+ anonymous_guardian = Guardian.new
+
+ expect(anonymous_guardian.can_preview_anonymous_public_chat_channel?(channel)).to eq(true)
+ expect(anonymous_guardian.can_chat?).to eq(false)
+ expect(anonymous_guardian.can_create_chat_message?).to eq(false)
+ end
+
it "allows user in specific group to chat" do
SiteSetting.chat_allowed_groups = chat_group.id
expect(guardian.can_chat?).to eq(false)
diff --git a/plugins/chat/spec/migrations/preserve_chat_allowed_groups_for_existing_sites_spec.rb b/plugins/chat/spec/migrations/preserve_chat_allowed_groups_for_existing_sites_spec.rb
new file mode 100644
index 00000000000..fa3890448e6
--- /dev/null
+++ b/plugins/chat/spec/migrations/preserve_chat_allowed_groups_for_existing_sites_spec.rb
@@ -0,0 +1,52 @@
+# frozen_string_literal: true
+
+require Rails.root.join(
+ "plugins/chat/db/migrate/20261013183208_preserve_chat_allowed_groups_for_existing_sites.rb",
+ )
+
+RSpec.describe PreserveChatAllowedGroupsForExistingSites do
+ before do
+ Migration::Helpers.stubs(:site_created_at).returns(2.days.ago)
+ DB.exec("DELETE FROM site_settings WHERE name = 'chat_allowed_groups'")
+ end
+
+ def migrate
+ migration = described_class.new
+ migration.suppress_messages { migration.up }
+ end
+
+ it "preserves the old default on existing sites and can run again" do
+ 2.times { migrate }
+
+ expect(
+ DB.query_single("SELECT value FROM site_settings WHERE name = 'chat_allowed_groups'"),
+ ).to eq(["1|2|11"])
+ expect(
+ DB.query_single("SELECT data_type FROM site_settings WHERE name = 'chat_allowed_groups'"),
+ ).to eq([SiteSettings::TypeSupervisor.types[:group_list]])
+ end
+
+ it "preserves custom groups on existing sites" do
+ DB.exec <<~SQL
+ INSERT INTO site_settings(name, data_type, value, created_at, updated_at)
+ VALUES('chat_allowed_groups', 20, '1|2|4|10', NOW(), NOW())
+ SQL
+
+ migrate
+
+ expect(
+ DB.query_single("SELECT value FROM site_settings WHERE name = 'chat_allowed_groups'"),
+ ).to eq(["1|2|4|10"])
+ end
+
+ it "leaves new sites using the new default without inserting a setting" do
+ Migration::Helpers.stubs(:site_created_at).returns(Time.current)
+
+ migrate
+
+ expect(
+ DB.query_single("SELECT value FROM site_settings WHERE name = 'chat_allowed_groups'"),
+ ).to be_empty
+ expect(SiteSetting.defaults[:chat_allowed_groups]).to eq("1|2|4|10")
+ end
+end
diff --git a/plugins/chat/spec/services/chat/publisher_spec.rb b/plugins/chat/spec/services/chat/publisher_spec.rb
index 7df48203c23..38e8badd5c0 100644
--- a/plugins/chat/spec/services/chat/publisher_spec.rb
+++ b/plugins/chat/spec/services/chat/publisher_spec.rb
@@ -238,7 +238,9 @@ describe Chat::Publisher do
)
end
- it "calls MessageBus with the correct permissions" do
+ it "restricts MessageBus delivery when anonymous access is disabled" do
+ SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:trust_level_1]
+
messages =
MessageBus.track_publish("/chat/#{channel.id}") do
described_class.publish_new!(channel, message_1, staged_id)
@@ -306,10 +308,7 @@ describe Chat::Publisher do
).to eq(false)
end
- it "allows anonymous clients when anonymous public chat access is enabled" do
- SiteSetting.chat_allowed_groups =
- "#{Group::AUTO_GROUPS[:everyone]}|#{Group::AUTO_GROUPS[:anonymous_users]}"
-
+ it "allows anonymous clients by default" do
messages =
MessageBus.track_publish("/chat/#{channel.id}") do
described_class.publish_new!(channel, message_1, staged_id)
@@ -380,7 +379,9 @@ describe Chat::Publisher do
expect(messages).not_to be_empty
end
- it "calls MessageBus with the correct permissions" do
+ it "restricts MessageBus delivery when anonymous access is disabled" do
+ SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:trust_level_1]
+
messages =
MessageBus.track_publish("/chat/#{channel.id}") do
described_class.publish_new!(channel, message_1, staged_id)
@@ -414,7 +415,9 @@ describe Chat::Publisher do
)
end
- it "calls MessageBus with the correct permissions" do
+ it "restricts MessageBus delivery when anonymous access is disabled" do
+ SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:trust_level_1]
+
messages =
MessageBus.track_publish("/chat/#{channel.id}") do
described_class.publish_new!(channel, message_1, staged_id)