Skip to content

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

DEV: Rename anonymous group to anonymous_users (#40435)

Martin Brennan5ff45e59b788
diff --git a/app/controllers/admin/search_controller.rb b/app/controllers/admin/search_controller.rb
index a7134c60ead..838b8df1518 100644
--- a/app/controllers/admin/search_controller.rb
+++ b/app/controllers/admin/search_controller.rb
@@ -1,7 +1,7 @@
# frozen_string_literal: true
class Admin::SearchController < Admin::AdminController
- RESULT_TYPES = %w[page setting theme component report].freeze
+ RESULT_TYPES = %w[page setting theme component report upcomingChange].freeze
def index
respond_to do |format|
diff --git a/app/models/group.rb b/app/models/group.rb
index f828e08a420..ff3808b58a0 100644
--- a/app/models/group.rb
+++ b/app/models/group.rb
@@ -104,7 +104,7 @@ class Group < ActiveRecord::Base
admins: 1,
moderators: 2,
staff: 3,
- anonymous: 4,
+ anonymous_users: 4,
logged_in_users: 5,
trust_level_0: 10,
trust_level_1: 11,
@@ -157,7 +157,7 @@ class Group < ActiveRecord::Base
(
AUTO_GROUPS.values -
[
- Group::AUTO_GROUPS[:anonymous],
+ Group::AUTO_GROUPS[:anonymous_users],
Group::AUTO_GROUPS[:logged_in_users],
Group::AUTO_GROUPS[:everyone],
]
@@ -182,7 +182,7 @@ class Group < ActiveRecord::Base
groups =
groups.where(
"groups.id NOT IN (:ids)",
- ids: [Group::AUTO_GROUPS[:anonymous], Group::AUTO_GROUPS[:logged_in_users]],
+ ids: [Group::AUTO_GROUPS[:anonymous_users], Group::AUTO_GROUPS[:logged_in_users]],
)
end
@@ -246,7 +246,7 @@ class Group < ActiveRecord::Base
groups =
groups.where(
"groups.id NOT IN (:ids)",
- ids: [Group::AUTO_GROUPS[:anonymous], Group::AUTO_GROUPS[:logged_in_users]],
+ ids: [Group::AUTO_GROUPS[:anonymous_users], Group::AUTO_GROUPS[:logged_in_users]],
)
end
@@ -549,11 +549,11 @@ class Group < ActiveRecord::Base
group.name = default_name
end
- # the everyone, anonymous, and logged_in_users groups are special — they
+ # the everyone, anonymous_users, and logged_in_users groups are special — they
# represent implicit populations (unauthenticated visitors, or all logged-in
# users) that cannot be enumerated via group_users rows.
case name
- when :everyone, :anonymous, :logged_in_users
+ when :everyone, :anonymous_users, :logged_in_users
group.visibility_level = Group.visibility_levels[:staff]
group.save!
return group
diff --git a/app/models/site.rb b/app/models/site.rb
index 939de47c2f7..026785ce1dc 100644
--- a/app/models/site.rb
+++ b/app/models/site.rb
@@ -176,9 +176,12 @@ class Site
def groups
query =
- Group.visible_groups(@guardian.user, "groups.name ASC", include_everyone: true).includes(
- :flair_upload,
- )
+ Group.visible_groups(
+ @guardian.user,
+ "groups.name ASC",
+ include_everyone: !SiteSetting.granular_anonymous_and_logged_in_groups_permissions,
+ include_pseudogroups: SiteSetting.granular_anonymous_and_logged_in_groups_permissions,
+ ).includes(:flair_upload)
query = DiscoursePluginRegistry.apply_modifier(:site_groups_query, query, self)
query
diff --git a/app/models/user.rb b/app/models/user.rb
index 4f876d0a6a7..acb4f84dd1b 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -565,8 +565,10 @@ class User < ActiveRecord::Base
# from some strange edge case, this handles it.
if is_system_user? &&
(
- (Group.auto_groups_between(:admins, :trust_level_4) - [Group::AUTO_GROUPS[:anonymous]]) &
- group_ids
+ (
+ Group.auto_groups_between(:admins, :trust_level_4) -
+ [Group::AUTO_GROUPS[:anonymous_users]]
+ ) & group_ids
).any?
return true
end
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 13d8eadeded..212a24ddb11 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -674,7 +674,7 @@ en:
admins: "admins"
moderators: "moderators"
staff: "staff"
- anonymous: "anonymous"
+ anonymous_users: "anonymous_users"
logged_in_users: "logged_in_users"
trust_level_0: "trust_level_0"
trust_level_1: "trust_level_1"
@@ -684,7 +684,7 @@ en:
default_descriptions:
everyone: "Automatic group including all members"
staff: "Automatic group including admins and moderators"
- anonymous: "Automatic pseudogroup representing unauthenticated visitors"
+ anonymous_users: "Automatic pseudogroup representing unauthenticated visitors"
logged_in_users: "Automatic pseudogroup including every logged-in user"
admins: "Responsible for configuring and maintaining this site, with access to all member and activity data"
moderators: "Responsible for responding to flags, moderating discussions, and helping members with their accounts"
@@ -2881,7 +2881,7 @@ en:
modernize_foundation_theme: "This change introduces design modifcations to the Foundation theme in Discourse."
enable_horizon_high_context_topic_cards: "Shows richer topic cards in the Horizon theme, including topic excerpts, tags, and plugin extended information."
reporting_improvements: "Improvements to navigation, design, and usability for Discourse's admin reports."
- granular_anonymous_and_logged_in_groups_permissions: "Makes group-based site setting permissions treat 'everyone' as 'all logged-in users', and enables the new 'anonymous' and 'logged_in_users' pseudogroups so admins can explicitly target anonymous visitors and authenticated users."
+ granular_anonymous_and_logged_in_groups_permissions: "Makes group-based site setting permissions treat 'everyone' as 'all logged-in users', and enables the new 'anonymous_users' and 'logged_in_users' pseudogroups so admins can explicitly target anonymous visitors and authenticated users."
dashboard_improvements: "Improvements to navigation, design, and usability for the Discourse admin dashboard."
errors:
diff --git a/db/migrate/20260424004343_ensure_anonymous_and_logged_in_users_auto_groups.rb b/db/migrate/20260424004343_ensure_anonymous_and_logged_in_users_auto_groups.rb
index 5caddeb5e37..06e40f8b9a4 100644
--- a/db/migrate/20260424004343_ensure_anonymous_and_logged_in_users_auto_groups.rb
+++ b/db/migrate/20260424004343_ensure_anonymous_and_logged_in_users_auto_groups.rb
@@ -2,66 +2,51 @@
class EnsureAnonymousAndLoggedInUsersAutoGroups < ActiveRecord::Migration[8.0]
def up
- legacy_anonymous_group_id = DB.query_single(<<~SQL).first
- SELECT id FROM groups WHERE name = 'anonymous' AND id <> 4;
+ legacy_anonymous_users_group_id = DB.query_single(<<~SQL).first
+ SELECT id FROM groups WHERE name = 'anonymous_users' AND id <> 4;
SQL
legacy_logged_in_users_group_id = DB.query_single(<<~SQL).first
SELECT id FROM groups WHERE name = 'logged_in_users' AND id <> 5;
SQL
- # Make sure any existing groups with the name `anonymous` or
+ # Make sure any existing groups with the name `anonymous_users` or
# `logged_in_users` are renamed to avoid conflicts with the
# new auto groups. Also make sure to rebake posts & update group mentions.
- if legacy_anonymous_group_id.present?
- execute(<<~SQL)
- UPDATE groups SET name = 'anonymous_legacy_' || id::text
- WHERE name = 'anonymous' AND id <> 4;
+ if legacy_anonymous_users_group_id.present?
+ renamed = DB.query_single(<<~SQL)
+ UPDATE groups SET name = 'anonymous_users_legacy_' || id::text
+ WHERE name = 'anonymous_users' AND id <> 4
+ RETURNING id;
SQL
- update_group_mentions(
- "anonymous",
- "anonymous_legacy_#{legacy_anonymous_group_id}",
- legacy_anonymous_group_id,
- )
+ if renamed.present?
+ Migration::GroupMentionSlugRewriter.update_posts!(
+ old_slug: "anonymous_users",
+ new_slug: "anonymous_users_legacy_#{renamed.first}",
+ group_id: legacy_anonymous_users_group_id,
+ )
+ end
end
if legacy_logged_in_users_group_id.present?
- execute(<<~SQL)
+ renamed = DB.query_single(<<~SQL)
UPDATE groups SET name = 'logged_in_users_legacy_' || id::text
- WHERE name = 'logged_in_users' AND id <> 5;
+ WHERE name = 'logged_in_users' AND id <> 5
+ RETURNING id;
SQL
- update_group_mentions(
- "logged_in_users",
- "logged_in_users_legacy_#{legacy_logged_in_users_group_id}",
- legacy_logged_in_users_group_id,
- )
+ if renamed.present?
+ Migration::GroupMentionSlugRewriter.update_posts!(
+ old_slug: "logged_in_users",
+ new_slug: "logged_in_users_legacy_#{renamed.first}",
+ group_id: legacy_logged_in_users_group_id,
+ )
+ end
end
end
def down
raise ActiveRecord::IrreversibleMigration
end
-
- def update_group_mentions(old_slug, new_slug, legacy_group_id)
- DB.exec(<<~SQL, old_slug:, new_slug:, group_id: legacy_group_id)
- UPDATE posts AS p SET
- raw = regexp_replace(
- p.raw,
- '(^|\s)(@' || :old_slug || ')(\s|$)',
- E'\\1@' || :new_slug || E'\\3',
- 'g'
- ),
- baked_version = NULL
- WHERE p.deleted_at IS NULL
- AND EXISTS (
- SELECT 1
- FROM group_mentions gm
- WHERE gm.post_id = p.id
- AND gm.group_id = :group_id
- )
- AND p.raw LIKE '%@' || :old_slug || '%';
- SQL
- end
end
diff --git a/db/post_migrate/20260601043020_rename_anonymous_auto_group_to_anonymous_users.rb b/db/post_migrate/20260601043020_rename_anonymous_auto_group_to_anonymous_users.rb
new file mode 100644
index 00000000000..803c50b3da2
--- /dev/null
+++ b/db/post_migrate/20260601043020_rename_anonymous_auto_group_to_anonymous_users.rb
@@ -0,0 +1,53 @@
+# frozen_string_literal: true
+
+class RenameAnonymousAutoGroupToAnonymousUsers < ActiveRecord::Migration[8.0]
+ # The `anonymous` auto group (id 4) was introduced in
+ # 20260424004343_ensure_anonymous_and_logged_in_users_auto_groups.rb. It
+ # should have been named `anonymous_users` for consistency with
+ # `logged_in_users`, so this renames the existing row and rewrites any posts
+ # that mention it.
+ def up
+ legacy_anonymous_users_group_id = DB.query_single(<<~SQL).first
+ SELECT id FROM groups WHERE name = 'anonymous_users' AND id <> 4;
+ SQL
+
+ # Move any pre-existing, non-auto group that happens to already be named
+ # `anonymous_users` out of the way so the rename below doesn't hit the unique
+ # name constraint. Rebake its mentions to point at the renamed slug.
+ if legacy_anonymous_users_group_id.present?
+ renamed = DB.query_single(<<~SQL)
+ UPDATE groups SET name = 'anonymous_users_legacy_' || id::text
+ WHERE name = 'anonymous_users' AND id <> 4
+ RETURNING id;
+ SQL
+
+ if renamed.present?
+ Migration::GroupMentionSlugRewriter.update_posts!(
+ old_slug: "anonymous_users",
+ new_slug: "anonymous_users_legacy_#{renamed.first}",
+ group_id: legacy_anonymous_users_group_id,
+ )
+ end
+ end
+
+ # Rename the auto group itself, then rewrite any `@anonymous` mentions of it
+ # to `@anonymous_users`.
+ renamed = DB.query_single(<<~SQL)
+ UPDATE groups SET name = 'anonymous_users'
+ WHERE id = 4 AND name = 'anonymous'
+ RETURNING id;
+ SQL
+
+ if renamed.present?
+ Migration::GroupMentionSlugRewriter.update_posts!(
+ old_slug: "anonymous",
+ new_slug: "anonymous_users",
+ group_id: 4,
+ )
+ end
+ end
+
+ def down
+ raise ActiveRecord::IrreversibleMigration
+ end
+end
diff --git a/db/structure.sql b/db/structure.sql
index a3870dd245f..c7959baf1f8 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -21795,6 +21795,7 @@ ALTER TABLE ONLY public.ad_plugin_house_ads_groups
SET search_path TO "$user", public;
INSERT INTO "schema_migrations" (version) VALUES
+('20260601043020'),
('20260525105009'),
('20260525105006'),
('20260522043337'),
diff --git a/frontend/discourse/admin/components/site-settings/group-list.gjs b/frontend/discourse/admin/components/site-settings/group-list.gjs
index b9127111a3a..86246f1f225 100644
--- a/frontend/discourse/admin/components/site-settings/group-list.gjs
+++ b/frontend/discourse/admin/components/site-settings/group-list.gjs
@@ -1,33 +1,125 @@
/* eslint-disable ember/no-classic-components */
import Component from "@ember/component";
import { action, computed } from "@ember/object";
+import { service } from "@ember/service";
import { tagName } from "@ember-decorators/component";
+import { AUTO_GROUPS } from "discourse/lib/constants";
import ListSetting from "discourse/select-kit/components/list-setting";
+// TODO (martin) Remove all this indirection when
+// granular_anonymous_and_logged_in_groups_permissions is Permanent
+const EVERYONE_ID = AUTO_GROUPS.everyone.id.toString();
+const LOGGED_IN_USERS_ID = AUTO_GROUPS.logged_in_users.id.toString();
+
+function normalizeIds(ids) {
+ return ids.map((id) => id.toString());
+}
+
+function mapEveryoneToLoggedInUsersIds(ids, granularPermissionsEnabled) {
+ ids = normalizeIds(ids);
+
+ if (!granularPermissionsEnabled || !ids.includes(EVERYONE_ID)) {
+ return ids;
+ }
+
+ return [
+ ...new Set(ids.map((id) => (id === EVERYONE_ID ? LOGGED_IN_USERS_ID : id))),
+ ];
+}
+
+function mapLoggedInUsersToEveryoneForStorage(
+ ids,
+ granularPermissionsEnabled,
+ storedValue,
+ tokenSeparator
+) {
+ if (!granularPermissionsEnabled) {
+ return normalizeIds(ids);
+ }
+
+ const storedIds = normalizeIds(
+ (storedValue || "").split(tokenSeparator).filter(Boolean)
+ );
+
+ if (
+ !storedIds.includes(EVERYONE_ID) ||
+ storedIds.includes(LOGGED_IN_USERS_ID)
+ ) {
+ return normalizeIds(ids);
+ }
+
+ return [
+ ...new Set(
+ normalizeIds(ids).map((id) =>
+ id === LOGGED_IN_USERS_ID ? EVERYONE_ID : id
+ )
+ ),
+ ];
+}
+
@tagName("")
export default class GroupList extends Component {
+ @service siteSettings;
+
tokenSeparator = "|";
nameProperty = "name";
valueProperty = "id";
- @computed("site.groups", "setting.disallowed_groups")
+ @computed(
+ "site.groups",
+ "setting.disallowed_groups",
+ "siteSettings.granular_anonymous_and_logged_in_groups_permissions"
+ )
get groupChoices() {
const disallowed = (this.setting?.disallowed_groups || "")
.split("|")
.filter(Boolean);
- return (this.site.groups || [])
- .filter((g) => !disallowed.includes(g.id.toString()))
- .map((g) => ({ name: g.name, id: g.id.toString() }));
+ const groups = (this.site.groups || []).filter(
+ (g) => !disallowed.includes(g.id.toString())
+ );
+ const groupsById = Object.fromEntries(
+ groups.map((g) => [g.id.toString(), g])
+ );
+ const choiceIds = mapEveryoneToLoggedInUsersIds(
+ groups.map((g) => g.id.toString()),
+ this.siteSettings.granular_anonymous_and_logged_in_groups_permissions
+ );
+
+ return choiceIds
+ .map((id) => {
+ const group = groupsById[id];
+ return group ? { name: group.name, id } : null;
+ })
+ .filter(Boolean);
}
- @computed("value")
+ @computed(
+ "value",
+ "siteSettings.granular_anonymous_and_logged_in_groups_permissions"
+ )
get settingValue() {
- return (this.value || "").split(this.tokenSeparator).filter(Boolean);
+ const ids = (this.value || "").split(this.tokenSeparator).filter(Boolean);
+ return mapEveryoneToLoggedInUsersIds(
+ ids,
+ this.siteSettings.granular_anonymous_and_logged_in_groups_permissions
+ );
}
@action
onChangeGroupListSetting(value) {
- this.set("value", value.join(this.tokenSeparator));
+ const ids = mapLoggedInUsersToEveryoneForStorage(
+ value,
+ this.siteSettings.granular_anonymous_and_logged_in_groups_permissions,
+ this.setting?.value,
+ this.tokenSeparator
+ );
+ const storedValue = ids.join(this.tokenSeparator);
+
+ if (this.changeValueCallback) {
+ this.changeValueCallback(storedValue);
+ } else {
+ this.set("value", storedValue);
+ }
}
<template>
diff --git a/frontend/discourse/app/lib/constants.js b/frontend/discourse/app/lib/constants.js
index 850100c6d2b..70d93ea5dbb 100644
--- a/frontend/discourse/app/lib/constants.js
+++ b/frontend/discourse/app/lib/constants.js
@@ -33,6 +33,8 @@ export const AUTO_GROUPS = {
admins: { id: 1, automatic: true, name: "admins" },
moderators: { id: 2, automatic: true, name: "moderators" },
staff: { id: 3, automatic: true, name: "staff" },
+ anonymous_users: { id: 4, automatic: true, name: "anonymous_users" },
+ logged_in_users: { id: 5, automatic: true, name: "logged_in_users" },
trust_level_0: { id: 10, automatic: true, name: "trust_level_0" },
trust_level_1: { id: 11, automatic: true, name: "trust_level_1" },
trust_level_2: { id: 12, automatic: true, name: "trust_level_2" },
@@ -74,6 +76,7 @@ export const ADMIN_SEARCH_RESULT_TYPES = [
"theme",
"component",
"report",
+ "upcomingChange",
];
export const API_KEY_SCOPE_MODES = ["global", "read_only", "granular"];
diff --git a/frontend/discourse/tests/fixtures/concerns/notification-types.js b/frontend/discourse/tests/fixtures/concerns/notification-types.js
index b328c544708..c89d6ce4ab2 100644
--- a/frontend/discourse/tests/fixtures/concerns/notification-types.js
+++ b/frontend/discourse/tests/fixtures/concerns/notification-types.js
@@ -43,6 +43,11 @@ export const NOTIFICATION_TYPES = {
admin_problems: 38,
linked_consolidated: 39,
chat_watched_thread: 40,
+ upcoming_change_available: 41,
+ upcoming_change_automatically_promoted: 42,
+ boost: 43,
+ suggested_edit_created: 44,
+ suggested_edit_accepted: 45,
following: 800,
following_created_topic: 801,
following_replied: 802,
diff --git a/frontend/discourse/tests/integration/components/group-list-setting-test.gjs b/frontend/discourse/tests/integration/components/group-list-setting-test.gjs
index 912eef74b71..d976eddcc94 100644
--- a/frontend/discourse/tests/integration/components/group-list-setting-test.gjs
+++ b/frontend/discourse/tests/integration/components/group-list-setting-test.gjs
@@ -103,6 +103,65 @@ module("Integration | Component | group-list site-setting", function (hooks) {
assert.dom(".selected-content button").hasClass("disabled");
});
+ test("swaps everyone for logged_in_users when granular permissions are enabled", async function (assert) {
+ this.siteSettings.granular_anonymous_and_logged_in_groups_permissions = true;
+
+ this.site.groups = [
+ {
+ id: 1,
+ name: "Donuts",
+ },
+ {
+ id: 5,
+ name: "logged_in_users",
+ },
+ {
+ id: 11,
+ name: "trust_level_1",
+ },
+ ];
+
+ this.set(
+ "setting",
+ SiteSetting.create({
+ category: "foo",
+ default: "",
+ description: "Choose groups",
+ placeholder: null,
+ preview: null,
+ secret: false,
+ setting: "foo_bar",
+ type: "group_list",
+ value: "0|11",
+ })
+ );
+
+ await render(
+ <template><SiteSettingComponent @setting={{this.setting}} /></template>
+ );
+
+ const subject = selectKit(".list-setting");
+
+ assert.strictEqual(
+ subject.header().value(),
+ "5,11",
+ "displays logged_in_users instead of everyone without mutating the stored value"
+ );
+
+ assert.strictEqual(this.setting.value, "0|11");
+ assert.strictEqual(this.setting.buffered.get("value"), "0|11");
+
+ await subject.expand();
+ await subject.selectRowByValue("1");
+
+ assert.strictEqual(
+ this.setting.buffered.get("value"),
+ "0|11|1",
+ "maps logged_in_users back to everyone in the buffered value"
+ );
+ assert.strictEqual(this.setting.value, "0|11");
+ });
+
test("disallowed groups", async function (assert) {
this.site.groups = [
{
diff --git a/lib/guardian.rb b/lib/guardian.rb
index 7f30e864ab6..0d280c0a13d 100644
--- a/lib/guardian.rb
+++ b/lib/guardian.rb
@@ -102,7 +102,7 @@ class Guardian
end
def in_any_groups?(group_ids)
- group_ids.include?(Group::AUTO_GROUPS[:anonymous])
+ group_ids.include?(Group::AUTO_GROUPS[:anonymous_users])
end
end
diff --git a/lib/migration/group_mention_slug_rewriter.rb b/lib/migration/group_mention_slug_rewriter.rb
new file mode 100644
index 00000000000..7231646d8a3
--- /dev/null
+++ b/lib/migration/group_mention_slug_rewriter.rb
@@ -0,0 +1,47 @@
+# frozen_string_literal: true
+
+module Migration
+ # Rewrites @group mentions in post raw when a group slug is renamed during a
+ # migration. Boundaries match Jobs::UpdateUsername (not trailing whitespace only).
+ module GroupMentionSlugRewriter
+ NON_TRAILING_MENTION_CHAR_CLASS = "[:alnum:]_\\-.`"
+
+ module_function
+
+ def update_posts!(old_slug:, new_slug:, group_id:)
+ pattern = mention_pattern_sql(old_slug)
+
+ DB.exec(<<~SQL, pattern:, new_slug:, group_id:, old_slug:)
+ UPDATE posts AS p SET
+ raw = regexp_replace(
+ p.raw,
+ :pattern,
+ E'\\1@' || :new_slug,
+ 'g'
+ ),
+ baked_version = NULL
+ WHERE p.deleted_at IS NULL
+ AND EXISTS (
+ SELECT 1
+ FROM group_mentions gm
+ WHERE gm.post_id = p.id
+ AND gm.group_id = :group_id
+ )
+ AND p.raw LIKE '%@' || :old_slug || '%';
+ SQL
+ end
+
+ # Ruby equivalent of the SQL pattern for unit tests.
+ def rewrite_text(text, old_slug, new_slug)
+ text.gsub(mention_pattern_ruby(old_slug), "\\1@#{new_slug}")
+ end
+
+ def mention_pattern_sql(old_slug)
+ "(^|[^#{NON_TRAILING_MENTION_CHAR_CLASS}])@#{Regexp.escape(old_slug)}(?=[^#{NON_TRAILING_MENTION_CHAR_CLASS}]|$)"
+ end
+
+ def mention_pattern_ruby(old_slug)
+ /(^|[^#{NON_TRAILING_MENTION_CHAR_CLASS}])@#{Regexp.escape(old_slug)}(?=[^#{NON_TRAILING_MENTION_CHAR_CLASS}]|$)/
+ end
+ end
+end
diff --git a/spec/lib/guardian_spec.rb b/spec/lib/guardian_spec.rb
index b6fdf025f3e..d6a28c87e4d 100644
--- a/spec/lib/guardian_spec.rb
+++ b/spec/lib/guardian_spec.rb
@@ -40,10 +40,10 @@ RSpec.describe Guardian do
describe "AnonymousUser#in_any_groups?" do
let(:anon) { Guardian::AnonymousUser.new }
- it "returns true when the anonymous auto group is in the list" do
- expect(anon.in_any_groups?([Group::AUTO_GROUPS[:anonymous]])).to eq(true)
+ it "returns true when the anonymous_users auto group is in the list" do
+ expect(anon.in_any_groups?([Group::AUTO_GROUPS[:anonymous_users]])).to eq(true)
expect(
- anon.in_any_groups?([Group::AUTO_GROUPS[:admins], Group::AUTO_GROUPS[:anonymous]]),
+ anon.in_any_groups?([Group::AUTO_GROUPS[:admins], Group::AUTO_GROUPS[:anonymous_users]]),
).to eq(true)
end
diff --git a/spec/lib/migration/group_mention_slug_rewriter_spec.rb b/spec/lib/migration/group_mention_slug_rewriter_spec.rb
new file mode 100644
index 00000000000..639a67b073a
--- /dev/null
+++ b/spec/lib/migration/group_mention_slug_rewriter_spec.rb
@@ -0,0 +1,57 @@
+# frozen_string_literal: true
+
+RSpec.describe Migration::GroupMentionSlugRewriter do
+ def rewrite(text, old_slug, new_slug)
+ described_class.rewrite_text(text, old_slug, new_slug)
+ end
+
+ describe ".rewrite_text" do
+ let(:old_slug) { "anonymous" }
+ let(:new_slug) { "anonymous_users" }
+
+ it "rewrites mentions followed by punctuation" do
+ expect(rewrite("@anonymous, please", old_slug, new_slug)).to eq("@anonymous_users, please")
+ end
+
+ it "rewrites mentions wrapped in parentheses" do
+ expect(rewrite("Ping (@anonymous) for help", old_slug, new_slug)).to eq(
+ "Ping (@anonymous_users) for help",
+ )
+ end
+
+ it "rewrites mentions at end of string" do
+ expect(rewrite("Thanks @anonymous", old_slug, new_slug)).to eq("Thanks @anonymous_users")
+ end
+
+ it "rewrites mentions followed by whitespace" do
+ expect(rewrite("Hey @anonymous there", old_slug, new_slug)).to eq(
+ "Hey @anonymous_users there",
+ )
+ end
+
+ it "does not rewrite when the slug is a prefix of a longer mention" do
+ expect(rewrite("@anonymous_users", old_slug, new_slug)).to eq("@anonymous_users")
+ end
+
+ it "does not rewrite when @ is preceded by a mention character" do
+ expect(rewrite("[email protected]", old_slug, new_slug)).to eq("[email protected]")
+ end
+
+ context "when renaming a conflicting legacy anonymous_users group" do
+ let(:old_slug) { "anonymous_users" }
+ let(:new_slug) { "anonymous_users_legacy_42" }
+
+ it "rewrites standalone legacy mentions" do
+ expect(rewrite("@anonymous_users, thanks", old_slug, new_slug)).to eq(
+ "@anonymous_users_legacy_42, thanks",
+ )
+ end
+
+ it "does not rewrite a longer slug that merely starts with the old name" do
+ expect(rewrite("@anonymous_users_extra", old_slug, new_slug)).to eq(
+ "@anonymous_users_extra",
+ )
+ end
+ end
+ end
+end
diff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rb
index df97287108a..64c3961cd5b 100644
--- a/spec/models/group_spec.rb
+++ b/spec/models/group_spec.rb
@@ -418,9 +418,9 @@ RSpec.describe Group do
expect(g.visibility_level).to eq(Group.visibility_levels[:staff])
end
- it "makes sure the anonymous and logged_in_users pseudogroups are hidden and have no members" do
- anon = Group.refresh_automatic_group!(:anonymous)
- expect(anon.id).to eq(Group::AUTO_GROUPS[:anonymous])
+ it "makes sure the anonymous_users and logged_in_users pseudogroups are hidden and have no members" do
+ anon = Group.refresh_automatic_group!(:anonymous_users)
+ expect(anon.id).to eq(Group::AUTO_GROUPS[:anonymous_users])
expect(anon.visibility_level).to eq(Group.visibility_levels[:staff])
expect(GroupUser.where(group_id: anon.id).count).to eq(0)
@@ -951,7 +951,7 @@ RSpec.describe Group do
).to eq(false)
end
- it "includes logged_in_users, anonymous and everyone groups when include_pseudogroups is true" do
+ it "includes logged_in_users, anonymous_users and everyone groups when include_pseudogroups is true" do
expect(
Group
.visible_groups(admin, [], include_pseudogroups: true)
@@ -961,7 +961,7 @@ RSpec.describe Group do
expect(
Group
.visible_groups(admin, [], include_pseudogroups: true)
- .where(id: Group::AUTO_GROUPS[:anonymous])
+ .where(id: Group::AUTO_GROUPS[:anonymous_users])
.exists?,
).to eq(true)
expect(
@@ -972,12 +972,12 @@ RSpec.describe Group do
).to eq(true)
end
- it "does not include logged_in_users, anonymous and everyone groups by default" do
+ it "does not include logged_in_users, anonymous_users and everyone groups by default" do
expect(
Group.visible_groups(admin, []).where(id: Group::AUTO_GROUPS[:everyone]).exists?,
).to eq(false)
expect(
- Group.visible_groups(admin, []).where(id: Group::AUTO_GROUPS[:anonymous]).exists?,
+ Group.visible_groups(admin, []).where(id: Group::AUTO_GROUPS[:anonymous_users]).exists?,
).to eq(false)
expect(
Group.visible_groups(admin, []).where(id: Group::AUTO_GROUPS[:logged_in_users]).exists?,
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index 7900fb8d0f6..95600cb8dcf 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -34,16 +34,21 @@ RSpec.describe User do
expect(user.in_any_groups?([Group::AUTO_GROUPS[:logged_in_users]])).to eq(true)
end
- it "never returns true for the 'anonymous' auto group — logged-in users are not anonymous" do
+ it "never returns true for the 'anonymous_users' auto group — logged-in users are not anonymous" do
GroupUser.where(user_id: Discourse::SYSTEM_USER_ID).delete_all
Discourse.system_user.reload
- expect(user.in_any_groups?([Group::AUTO_GROUPS[:anonymous]])).to eq(false)
- expect(Discourse.system_user.in_any_groups?([Group::AUTO_GROUPS[:anonymous]])).to eq(false)
+ expect(user.in_any_groups?([Group::AUTO_GROUPS[:anonymous_users]])).to eq(false)
+ expect(Discourse.system_user.in_any_groups?([Group::AUTO_GROUPS[:anonymous_users]])).to eq(
+ false,
+ )
end
- it "returns true for the 'anonymous' auto group for anonymous users" do
+ it "returns true for the 'anonymous_users' auto group for anonymous users" do
expect(
- Guardian.new.instance_variable_get(:@user).in_any_groups?([Group::AUTO_GROUPS[:anonymous]]),
+ Guardian
+ .new
+ .instance_variable_get(:@user)
+ .in_any_groups?([Group::AUTO_GROUPS[:anonymous_users]]),
).to eq(true)
end
@@ -66,11 +71,13 @@ RSpec.describe User do
expect(user.in_any_groups?([Group::AUTO_GROUPS[:logged_in_users]])).to eq(true)
end
- it "never returns true for the 'anonymous' auto group — logged-in users are not anonymous" do
+ it "never returns true for the 'anonymous_users' auto group — logged-in users are not anonymous" do
GroupUser.where(user_id: Discourse::SYSTEM_USER_ID).delete_all
Discourse.system_user.reload
- expect(user.in_any_groups?([Group::AUTO_GROUPS[:anonymous]])).to eq(false)
- expect(Discourse.system_user.in_any_groups?([Group::AUTO_GROUPS[:anonymous]])).to eq(false)
+ expect(user.in_any_groups?([Group::AUTO_GROUPS[:anonymous_users]])).to eq(false)
+ expect(Discourse.system_user.in_any_groups?([Group::AUTO_GROUPS[:anonymous_users]])).to eq(
+ false,
+ )
end
context "with granular_anonymous_and_logged_in_groups_permissions enabled" do
diff --git a/spec/requests/groups_controller_spec.rb b/spec/requests/groups_controller_spec.rb
index 89e6c38bf3c..86b7d731e68 100644
--- a/spec/requests/groups_controller_spec.rb
+++ b/spec/requests/groups_controller_spec.rb
@@ -384,7 +384,7 @@ RSpec.describe GroupsController do
Group::AUTO_GROUP_IDS.keys -
[
Group::AUTO_GROUPS[:everyone],
- Group::AUTO_GROUPS[:anonymous],
+ Group::AUTO_GROUPS[:anonymous_users],
Group::AUTO_GROUPS[:logged_in_users],
],
)
@@ -498,7 +498,7 @@ RSpec.describe GroupsController do
groups = Group::AUTO_GROUPS.keys
groups.delete(:everyone)
- groups.delete(:anonymous)
+ groups.delete(:anonymous_users)
groups.delete(:logged_in_users)
groups.push(group.name)
@@ -2882,7 +2882,7 @@ RSpec.describe GroupsController do
expected_ids = Group::AUTO_GROUPS.map { |name, id| id }
expected_ids.delete(Group::AUTO_GROUPS[:everyone])
expected_ids.delete(Group::AUTO_GROUPS[:logged_in_users])
- expected_ids.delete(Group::AUTO_GROUPS[:anonymous])
+ expected_ids.delete(Group::AUTO_GROUPS[:anonymous_users])
expected_ids << group.id
expect(groups.map { |group| group["id"] }).to contain_exactly(*expected_ids)
@@ -2949,7 +2949,7 @@ RSpec.describe GroupsController do
automatic_ids -
[
Group::AUTO_GROUPS[:everyone],
- Group::AUTO_GROUPS[:anonymous],
+ Group::AUTO_GROUPS[:anonymous_users],
Group::AUTO_GROUPS[:logged_in_users],
]
),
@@ -2965,7 +2965,10 @@ RSpec.describe GroupsController do
expect(groups.map { |group| group["id"] }).to contain_exactly(
group.id,
hidden_group.id,
- *(automatic_ids - [Group::AUTO_GROUPS[:anonymous], Group::AUTO_GROUPS[:logged_in_users]]),
+ *(
+ automatic_ids -
+ [Group::AUTO_GROUPS[:anonymous_users], Group::AUTO_GROUPS[:logged_in_users]]
+ ),
)
get "/groups/search.json?include_pseudogroups=true"
diff --git a/spec/services/anonymous_shadow_creator_spec.rb b/spec/services/anonymous_shadow_creator_spec.rb
index a7ea257edad..4635614af99 100644
--- a/spec/services/anonymous_shadow_creator_spec.rb
+++ b/spec/services/anonymous_shadow_creator_spec.rb
@@ -66,7 +66,7 @@ RSpec.describe AnonymousShadowCreator do
expect(shadow.id).to eq(shadow2.id)
expect(shadow.trust_level).to eq(1)
- expect(shadow.username).to eq("anonymous1")
+ expect(shadow.username).to eq("anonymous")
expect(shadow.created_at).not_to eq_time(user.created_at)
@@ -99,7 +99,7 @@ RSpec.describe AnonymousShadowCreator do
shadow = AnonymousShadowCreator.get(user)
- expect(shadow.username).to eq("anonymous1")
+ expect(shadow.username).to eq("anonymous")
end
end
end