From 0c1e5a76ee0c11c1484d621c6260ff54fa0af63e Mon Sep 17 00:00:00 2001
From: Martin Brennan <martin@discourse.org>
Date: Wed, 23 Nov 2022 14:12:54 +1000
Subject: [PATCH] FIX: Set chat_allowed_groups based on chat_enabled setting
 (#19146)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Sets the chat_allowed_groups to staff (the old default) in the database for
people who already have chat enabled if they did not already change it.

The assumption is that most people who this applies to will be
upgrading from a version that has neither of these two PRs (
the other PR being #19116) to a version that has both of these PRs.

So, for existing site with chat enabled who haven’t set groups, we
want to persist the value which is more likely to match what that are
upgrading from (staff).

People who don’t yet have chat enabled should get the new value (TL1
and staff) when they do enable it.

Follow up to 05b740036e90cb0491b8a45e48c368e8948c9136
---
 ...8_save_chat_allowed_groups_site_setting.rb | 22 +++++++++++++++++++
 1 file changed, 22 insertions(+)
 create mode 100644 plugins/chat/db/migrate/20221122070108_save_chat_allowed_groups_site_setting.rb

diff --git a/plugins/chat/db/migrate/20221122070108_save_chat_allowed_groups_site_setting.rb b/plugins/chat/db/migrate/20221122070108_save_chat_allowed_groups_site_setting.rb
new file mode 100644
index 00000000000..cfeabbc6a4d
--- /dev/null
+++ b/plugins/chat/db/migrate/20221122070108_save_chat_allowed_groups_site_setting.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+class SaveChatAllowedGroupsSiteSetting < ActiveRecord::Migration[7.0]
+  def up
+    chat_enabled = DB.query_single("SELECT value FROM site_settings WHERE name = 'chat_enabled' AND value = 't'")
+    return if chat_enabled.blank?
+
+    chat_allowed_groups = DB.query_single("SELECT value FROM site_settings WHERE name = 'chat_allowed_groups'")
+    return if chat_allowed_groups.blank?
+
+    # The original default was auto group ID 3 (staff) so we are
+    # using that here.
+    DB.exec(<<~SQL)
+          INSERT INTO site_settings(name, data_type, value, created_at, updated_at)
+          VALUES ('chat_allowed_groups', 20, '3', now(), now())
+    SQL
+  end
+
+  def down
+    raise ActiveRecord::IrreversibleMigration
+  end
+end