Skip to content

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

UX: allow anonymous chat viewers to flag posts (#44348)

Kris7d3d93e083b4
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 3e012ee6cf4..fd116c44510 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -2707,7 +2707,7 @@ en:
reviewable_default_visibility: "Don't show reviewable items unless they meet this priority"
reviewable_low_priority_threshold: "The priority filter hides reviewable items that don't meet this score unless the '(any)' filter is used."
high_trust_flaggers_auto_hide_posts: "New user posts are automatically hidden after being flagged as spam by a TL3+ user"
- allow_all_users_to_flag_illegal_content: "Anonymous users will see information that they have to e-mail administrators to report illegal content. This setting takes precedence over {{setting:flag_post_allowed_groups}}."
+ allow_all_users_to_flag_illegal_content: "Let anyone report illegal content, including trust level 0 and silenced users. Anonymous users will see information that they have to e-mail administrators to report illegal content. This setting takes precedence over {{setting:flag_post_allowed_groups}}."
email_address_to_report_illegal_content: "If left blank the default site admin email will be used."
cooldown_hours_until_reflag: "How much time users will have to wait until they are able to reflag a post"
slow_mode_prevents_editing: "Does 'Slow mode' prevent editing, after {{setting:editing_grace_period}}?"
diff --git a/frontend/discourse/app/components/modal/anonymous-flag.gjs b/frontend/discourse/app/components/modal/anonymous-flag.gjs
index 633460c4f96..957c95166f6 100644
--- a/frontend/discourse/app/components/modal/anonymous-flag.gjs
+++ b/frontend/discourse/app/components/modal/anonymous-flag.gjs
@@ -2,7 +2,6 @@ import Component from "@glimmer/component";
import { service } from "@ember/service";
import { trustHTML } from "@ember/template";
import { isEmpty } from "@ember/utils";
-import { getAbsoluteURL } from "discourse/lib/get-url";
import DModal from "discourse/ui-kit/d-modal";
import { i18n } from "discourse-i18n";
@@ -10,11 +9,10 @@ export default class AnonymousFlagModal extends Component {
@service siteSettings;
get description() {
- return i18n("anonymous_flagging.description", {
- email: this.#email,
- topic_title: this.args.model.flagModel.topic.title,
- url: getAbsoluteURL(this.args.model.flagModel.url),
- });
+ return this.args.model.flagTarget.anonymousFlagDescription(
+ this.args.model.flagModel,
+ this.#email
+ );
}
get #email() {
diff --git a/frontend/discourse/app/lib/flag-targets/post-flag.js b/frontend/discourse/app/lib/flag-targets/post-flag.js
index 6a07ca10253..ad995fb909a 100644
--- a/frontend/discourse/app/lib/flag-targets/post-flag.js
+++ b/frontend/discourse/app/lib/flag-targets/post-flag.js
@@ -1,5 +1,7 @@
import Flag from "discourse/lib/flag-targets/flag";
+import { getAbsoluteURL } from "discourse/lib/get-url";
import { applyValueTransformer } from "discourse/lib/transformer";
+import { i18n } from "discourse-i18n";
export default class PostFlag extends Flag {
title() {
@@ -14,6 +16,14 @@ export default class PostFlag extends Flag {
return "flagging.action";
}
+ anonymousFlagDescription(post, email) {
+ return i18n("anonymous_flagging.description", {
+ email,
+ topic_title: post.topic.title,
+ url: getAbsoluteURL(post.url),
+ });
+ }
+
flagCreatedEvent() {
return "post:flag-created";
}
diff --git a/lib/guardian/post_guardian.rb b/lib/guardian/post_guardian.rb
index c03440bc956..b6f0642b94f 100644
--- a/lib/guardian/post_guardian.rb
+++ b/lib/guardian/post_guardian.rb
@@ -53,8 +53,11 @@ module PostGuardian
return SiteSetting.allow_likes_in_anonymous_mode? && (action_key == :like)
end
- # Silenced users can't flag
- return false if is_flag && @user.silenced?
+ # Silenced users can't flag, except to report illegal content
+ if is_flag && @user.silenced? &&
+ !(action_key == :illegal && SiteSetting.allow_all_users_to_flag_illegal_content)
+ return false
+ end
# Silenced users can't like
return false if action_key == :like && @user.silenced?
diff --git a/plugins/chat/app/serializers/chat/message_serializer.rb b/plugins/chat/app/serializers/chat/message_serializer.rb
index 7d724d06b7b..8368a132b2b 100644
--- a/plugins/chat/app/serializers/chat/message_serializer.rb
+++ b/plugins/chat/app/serializers/chat/message_serializer.rb
@@ -184,6 +184,10 @@ module Chat
return [] if !scope.can_flag_chat_message?(object)
return [] if reviewable_id.present? && user_flag_status == ReviewableScore.statuses[:pending]
+ if scope.can_only_flag_chat_messages_as_illegal?
+ return PostActionType.flag_types.slice(:illegal).keys
+ end
+
PostActionType.flag_types.map do |sym, id|
if channel_object.direct_message_channel? && %i[notify_moderators notify_user].include?(sym)
next
diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-message.gjs b/plugins/chat/assets/javascripts/discourse/components/chat-message.gjs
index c87ee7d5bef..4afb6fe2865 100644
--- a/plugins/chat/assets/javascripts/discourse/components/chat-message.gjs
+++ b/plugins/chat/assets/javascripts/discourse/components/chat-message.gjs
@@ -627,10 +627,6 @@ export default class ChatMessage extends Component {
cancel(this._onMouseEnterMessageDebouncedHandler);
- if (!this.chat.userCanInteractWithChat) {
- return;
- }
-
if (!this.args.message.expanded) {
return;
}
diff --git a/plugins/chat/assets/javascripts/discourse/lib/chat-message-flag.js b/plugins/chat/assets/javascripts/discourse/lib/chat-message-flag.js
index ee1474d26b1..5bf8834f608 100644
--- a/plugins/chat/assets/javascripts/discourse/lib/chat-message-flag.js
+++ b/plugins/chat/assets/javascripts/discourse/lib/chat-message-flag.js
@@ -1,7 +1,7 @@
import { setOwner } from "@ember/owner";
import { service } from "@ember/service";
import { popupAjaxError } from "discourse/lib/ajax-error";
-import getURL from "discourse/lib/get-url";
+import getURL, { getAbsoluteURL } from "discourse/lib/get-url";
import { i18n } from "discourse-i18n";
export default class ChatMessageFlag {
@@ -23,6 +23,14 @@ export default class ChatMessageFlag {
return "chat.flagging.action";
}
+ anonymousFlagDescription(message, email) {
+ return i18n("chat.anonymous_flagging.description", {
+ email,
+ channel_title: message.channel.escapedTitle,
+ url: getAbsoluteURL(message.url),
+ });
+ }
+
targetsTopic() {
return false;
}
diff --git a/plugins/chat/assets/javascripts/discourse/lib/chat-message-interactor.js b/plugins/chat/assets/javascripts/discourse/lib/chat-message-interactor.js
index 25ea220762c..76e733f50eb 100644
--- a/plugins/chat/assets/javascripts/discourse/lib/chat-message-interactor.js
+++ b/plugins/chat/assets/javascripts/discourse/lib/chat-message-interactor.js
@@ -4,6 +4,7 @@ import { getOwner, setOwner } from "@ember/owner";
import { service } from "@ember/service";
import { isSkinTonableEmoji } from "pretty-text/emoji";
import EmojiPickerDetached from "discourse/components/emoji-picker/detached";
+import AnonymousFlagModal from "discourse/components/modal/anonymous-flag";
import BookmarkModal from "discourse/components/modal/bookmark";
import FlagModal from "discourse/components/modal/flag";
import { popupAjaxError } from "discourse/lib/ajax-error";
@@ -95,6 +96,7 @@ export default class ChatMessageInteractor {
get canInteractWithMessage() {
return (
+ this.#canParticipate &&
!this.message?.deletedAt &&
this.message?.channel?.canModifyMessages(this.currentUser) &&
this.message?.channel?.isFollowing
@@ -113,7 +115,10 @@ export default class ChatMessageInteractor {
}
get canBookmark() {
- return this.message?.channel?.canModifyMessages?.(this.currentUser);
+ return (
+ this.#canParticipate &&
+ this.message?.channel?.canModifyMessages?.(this.currentUser)
+ );
}
get canReply() {
@@ -128,6 +133,14 @@ export default class ChatMessageInteractor {
}
get canFlagMessage() {
+ if (!this.currentUser) {
+ return (
+ this.siteSettings.allow_all_users_to_flag_illegal_content &&
+ !this.message?.chatWebhookEvent &&
+ !this.message?.deletedAt
+ );
+ }
+
return (
this.currentUser.id !== this.message?.user?.id &&
this.message?.userFlagStatus === undefined &&
@@ -172,6 +185,10 @@ export default class ChatMessageInteractor {
}
get secondaryActions() {
+ if (!this.#canParticipate) {
+ return this.#readOnlySecondaryActions;
+ }
+
const buttons = [];
buttons.push({
@@ -255,6 +272,31 @@ export default class ChatMessageInteractor {
return buttons.filter((button) => !removedSecondaryActions.has(button.id));
}
+ // Visitors and silenced users can't post, but can still link to and report messages.
+ get #canParticipate() {
+ return this.currentUser && this.chat.userCanInteractWithChat;
+ }
+
+ get #readOnlySecondaryActions() {
+ const buttons = [
+ { id: "copyLink", name: i18n("chat.copy_link"), icon: "link" },
+ ];
+
+ if (this.site.mobileView) {
+ buttons.push({
+ id: "copyText",
+ name: i18n("chat.copy_text"),
+ icon: "clipboard",
+ });
+ }
+
+ if (this.canFlagMessage) {
+ buttons.push({ id: "flag", name: i18n("chat.flag"), icon: "flag" });
+ }
+
+ return buttons.filter((button) => !removedSecondaryActions.has(button.id));
+ }
+
select(checked = true) {
this.message.selected = checked;
this.pane.onSelectMessage(this.message);
@@ -394,7 +436,7 @@ export default class ChatMessageInteractor {
const model = new ChatMessage(this.message.channel, this.message);
model.username = this.message.user?.username;
model.user_id = this.message.user?.id;
- this.modal.show(FlagModal, {
+ this.modal.show(this.currentUser ? FlagModal : AnonymousFlagModal, {
model: {
flagTarget: new ChatMessageFlag(getOwner(this)),
flagModel: model,
diff --git a/plugins/chat/assets/stylesheets/common/chat-message-actions.scss b/plugins/chat/assets/stylesheets/common/chat-message-actions.scss
index 8f458e72291..ac74d550e2e 100644
--- a/plugins/chat/assets/stylesheets/common/chat-message-actions.scss
+++ b/plugins/chat/assets/stylesheets/common/chat-message-actions.scss
@@ -122,6 +122,14 @@
}
}
+ &:first-child .select-kit-header:not(:hover) {
+ border-left-color: var(--primary-300);
+ }
+
+ &:first-child .select-kit-header {
+ border-radius: var(--d-border-radius);
+ }
+
.select-kit-row {
.texts .name {
font-size: var(--font-0);
diff --git a/plugins/chat/config/locales/client.en.yml b/plugins/chat/config/locales/client.en.yml
index 9776743336d..01c0e4bfa50 100644
--- a/plugins/chat/config/locales/client.en.yml
+++ b/plugins/chat/config/locales/client.en.yml
@@ -759,6 +759,9 @@ en:
flagging:
action: "Flag message"
+ anonymous_flagging:
+ description: "To report illegal content, please contact <a href='mailto:%{email}?subject=Illegal content: %{channel_title}&body=This chat message %{url} contains illegal content.'>%{email}</a>"
+
emoji_picker:
favorites: "Frequently used"
smileys_&_emotion: "Smileys and emotion"
diff --git a/plugins/chat/lib/chat/guardian_extensions.rb b/plugins/chat/lib/chat/guardian_extensions.rb
index 6b9a7e02cb6..b9604676065 100644
--- a/plugins/chat/lib/chat/guardian_extensions.rb
+++ b/plugins/chat/lib/chat/guardian_extensions.rb
@@ -207,6 +207,11 @@ module Chat
@user.in_any_groups?(SiteSetting.chat_message_flag_allowed_groups_map)
end
+ # Illegal content must be reportable by anyone, regardless of flagging permissions.
+ def can_only_flag_chat_messages_as_illegal?
+ SiteSetting.allow_all_users_to_flag_illegal_content && !can_flag_chat_messages?
+ end
+
def can_flag_in_chat_channel?(chat_channel, post_allowed_category_ids: nil)
return false if !can_modify_channel_message?(chat_channel)
@@ -220,12 +225,17 @@ module Chat
return false if chat_message.user.staff? && !SiteSetting.allow_flagging_staff
return false if chat_message.user_id == @user.id
- can_flag_chat_messages? && can_flag_in_chat_channel?(chat_message.chat_channel)
+ (can_flag_chat_messages? || SiteSetting.allow_all_users_to_flag_illegal_content) &&
+ can_flag_in_chat_channel?(chat_message.chat_channel)
end
def can_flag_message_as?(chat_message, flag_type_id, opts)
return false if !is_staff? && (opts[:take_action] || opts[:queue_for_review])
+ if can_only_flag_chat_messages_as_illegal? && flag_type_id != ReviewableScore.types[:illegal]
+ return false
+ end
+
if flag_type_id == ReviewableScore.types[:notify_user]
is_warning = ActiveRecord::Type::Boolean.new.deserialize(opts[:is_warning])
diff --git a/plugins/chat/spec/lib/chat/guardian_extensions_spec.rb b/plugins/chat/spec/lib/chat/guardian_extensions_spec.rb
index 0b054bc2207..32a9bb00905 100644
--- a/plugins/chat/spec/lib/chat/guardian_extensions_spec.rb
+++ b/plugins/chat/spec/lib/chat/guardian_extensions_spec.rb
@@ -463,6 +463,47 @@ RSpec.describe Chat::GuardianExtensions do
expect(staff_guardian.can_flag_chat_message?(message)).to eq(true)
end
end
+
+ context "when allow_all_users_to_flag_illegal_content is enabled" do
+ before do
+ SiteSetting.email_address_to_report_illegal_content = "[email protected]"
+ SiteSetting.allow_all_users_to_flag_illegal_content = true
+ end
+
+ it "returns true for users outside the allowed groups" do
+ expect(guardian.can_flag_chat_message?(message)).to eq(true)
+ end
+
+ it "returns true for silenced users" do
+ UserSilencer.silence(user, staff)
+ expect(Guardian.new(user).can_flag_chat_message?(message)).to eq(true)
+ end
+ end
+ end
+
+ describe "#can_flag_message_as?" do
+ let!(:message) { Fabricate(:chat_message, chat_channel: channel) }
+
+ before do
+ SiteSetting.chat_message_flag_allowed_groups = ""
+ SiteSetting.email_address_to_report_illegal_content = "[email protected]"
+ SiteSetting.allow_all_users_to_flag_illegal_content = true
+ end
+
+ it "only allows the illegal flag for users outside the allowed groups" do
+ expect(guardian.can_flag_message_as?(message, ReviewableScore.types[:illegal], {})).to eq(
+ true,
+ )
+ expect(guardian.can_flag_message_as?(message, ReviewableScore.types[:spam], {})).to eq(
+ false,
+ )
+ end
+
+ it "allows any flag for users in the allowed groups" do
+ expect(
+ staff_guardian.can_flag_message_as?(message, ReviewableScore.types[:spam], {}),
+ ).to eq(true)
+ end
end
describe "#can_see_chat_message?" do
diff --git a/plugins/chat/spec/serializer/chat/chat_message_serializer_spec.rb b/plugins/chat/spec/serializer/chat/chat_message_serializer_spec.rb
index e2b7c3dc1ef..9323495da38 100644
--- a/plugins/chat/spec/serializer/chat/chat_message_serializer_spec.rb
+++ b/plugins/chat/spec/serializer/chat/chat_message_serializer_spec.rb
@@ -260,6 +260,29 @@ describe Chat::MessageSerializer do
expect(serialized[:available_flags]).to be_empty
end
+ context "when allow_all_users_to_flag_illegal_content is enabled" do
+ before do
+ SiteSetting.email_address_to_report_illegal_content = "[email protected]"
+ SiteSetting.allow_all_users_to_flag_illegal_content = true
+ end
+
+ it "returns only the illegal flag for silenced users" do
+ guardian.user.update!(silenced_till: 1.month.from_now)
+
+ serialized = described_class.new(message_1, options).as_json
+
+ expect(serialized[:available_flags]).to contain_exactly(:illegal)
+ end
+
+ it "returns only the illegal flag for users outside the allowed groups" do
+ SiteSetting.chat_message_flag_allowed_groups = ""
+
+ serialized = described_class.new(message_1, options).as_json
+
+ expect(serialized[:available_flags]).to contain_exactly(:illegal)
+ end
+ end
+
it "returns an empty list if the message was deleted" do
message_1.trash!
diff --git a/plugins/chat/spec/system/anonymous_public_channels_spec.rb b/plugins/chat/spec/system/anonymous_public_channels_spec.rb
index 8f9b01b96d1..808a0aa6855 100644
--- a/plugins/chat/spec/system/anonymous_public_channels_spec.rb
+++ b/plugins/chat/spec/system/anonymous_public_channels_spec.rb
@@ -92,6 +92,49 @@ RSpec.describe "Anonymous public chat channels" do
expect(sidebar).to have_no_section("community")
end
+ context "when flagging messages" do
+ let(:anonymous_flag_modal) { PageObjects::Modals::AnonymousFlag.new }
+
+ it "doesn't offer visitors a flag action by default" do
+ chat_page.visit_channel(public_channel)
+ channel_page.expand_message_actions(existing_message)
+
+ expect(page).to have_css("[data-value='copyLink']")
+ expect(page).to have_no_css("[data-value='flag']")
+ end
+
+ let(:expected_description) do
+ ActionView::Base.full_sanitizer.sanitize(
+ I18n.t(
+ "js.chat.anonymous_flagging.description",
+ email: "[email protected]",
+ channel_title: public_channel.title,
+ url: "#{Discourse.base_url}/chat/c/-/#{public_channel.id}/#{existing_message.id}",
+ ),
+ )
+ end
+
+ before { SiteSetting.contact_email = "[email protected]" }
+
+ it "lets visitors email a report when allow_all_users_to_flag_illegal_content is enabled" do
+ SiteSetting.allow_all_users_to_flag_illegal_content = true
+
+ chat_page.visit_channel(public_channel)
+ channel_page.messages.flag(existing_message)
+
+ expect(anonymous_flag_modal.body).to have_content(expected_description)
+ end
+
+ it "lets visitors on mobile email a report", mobile: true do
+ SiteSetting.allow_all_users_to_flag_illegal_content = true
+
+ chat_page.visit_channel(public_channel)
+ channel_page.messages.flag(existing_message)
+
+ expect(anonymous_flag_modal.body).to have_content(expected_description)
+ end
+ end
+
it "keeps visitors out of browse" do
visit("/chat/browse/open")
diff --git a/plugins/chat/spec/system/flag_message_spec.rb b/plugins/chat/spec/system/flag_message_spec.rb
index 4cc525dddd9..a0b833d55b0 100644
--- a/plugins/chat/spec/system/flag_message_spec.rb
+++ b/plugins/chat/spec/system/flag_message_spec.rb
@@ -41,4 +41,24 @@ RSpec.describe "Flag message" do
expect(page).to have_css("[data-value='flag']")
end
end
+
+ context "when user is outside the flag allowed groups" do
+ fab!(:category_channel_1, :category_channel)
+ fab!(:message_1) { Fabricate(:chat_message, chat_channel: category_channel_1) }
+ let(:flag_modal) { PageObjects::Modals::Flag.new }
+
+ before do
+ SiteSetting.chat_message_flag_allowed_groups = ""
+ SiteSetting.email_address_to_report_illegal_content = "[email protected]"
+ SiteSetting.allow_all_users_to_flag_illegal_content = true
+ category_channel_1.add(current_user)
+ end
+
+ it "only allows flagging as illegal" do
+ chat.visit_channel(category_channel_1)
+ channel.messages.flag(message_1)
+
+ expect(flag_modal).to have_choices(I18n.t("js.flagging.formatted_name.illegal"))
+ end
+ end
end
diff --git a/plugins/chat/spec/system/silenced_user_spec.rb b/plugins/chat/spec/system/silenced_user_spec.rb
index 302f352e3eb..101fe479e31 100644
--- a/plugins/chat/spec/system/silenced_user_spec.rb
+++ b/plugins/chat/spec/system/silenced_user_spec.rb
@@ -38,7 +38,28 @@ RSpec.describe "Silenced user" do
chat.visit_channel(channel_1)
channel.hover_message(message_1)
- expect(page).to have_no_css(".chat-message-actions")
+ expect(page).to have_css(".chat-message-actions")
+ expect(page).to have_no_css(".chat-message-actions .react-btn")
+ expect(page).to have_no_css(".chat-message-actions .reply-btn")
+ end
+
+ it "allows flagging messages as illegal when allow_all_users_to_flag_illegal_content is enabled" do
+ SiteSetting.email_address_to_report_illegal_content = "[email protected]"
+ SiteSetting.allow_all_users_to_flag_illegal_content = true
+ message_1 = Fabricate(:chat_message, chat_channel: channel_1)
+ flag_modal = PageObjects::Modals::Flag.new
+
+ chat.visit_channel(channel_1)
+ channel.messages.flag(message_1)
+
+ expect(flag_modal).to have_choices(I18n.t("js.flagging.formatted_name.illegal"))
+
+ flag_modal.choose_type(:illegal)
+ flag_modal.fill_message("This looks totally illegal to me.")
+ flag_modal.check_confirmation
+ flag_modal.confirm_flag
+
+ expect(channel.message_by_id(message_1.id)).to have_css(".chat-message-info__flag")
end
end
end
diff --git a/spec/lib/guardian/post_guardian_spec.rb b/spec/lib/guardian/post_guardian_spec.rb
index feb7e8c7b23..6303a41e160 100644
--- a/spec/lib/guardian/post_guardian_spec.rb
+++ b/spec/lib/guardian/post_guardian_spec.rb
@@ -167,6 +167,16 @@ RSpec.describe PostGuardian do
expect(Guardian.new(user).post_can_act?(post, :bookmark)).to be_truthy
end
+ it "lets silenced users flag illegal content when allow_all_users_to_flag_illegal_content" do
+ UserSilencer.silence(user, admin)
+ expect(Guardian.new(user).post_can_act?(post, :illegal)).to be false
+
+ SiteSetting.email_address_to_report_illegal_content = "[email protected]"
+ SiteSetting.allow_all_users_to_flag_illegal_content = true
+ expect(Guardian.new(user).post_can_act?(post, :illegal)).to be true
+ expect(Guardian.new(user).post_can_act?(post, :spam)).to be false
+ end
+
it "allows flagging archived posts" do
post.topic.archived = true
expect(Guardian.new(user).post_can_act?(post, :spam)).to be_truthy
diff --git a/spec/system/flagging_post_spec.rb b/spec/system/flagging_post_spec.rb
index f130e864656..8c9fdb34d84 100644
--- a/spec/system/flagging_post_spec.rb
+++ b/spec/system/flagging_post_spec.rb
@@ -102,6 +102,33 @@ describe "Flagging post" do
end
end
+ context "when silenced" do
+ fab!(:silenced_user) { Fabricate(:user, refresh_auto_groups: true) }
+
+ before do
+ UserSilencer.silence(silenced_user)
+ sign_in(silenced_user)
+ end
+
+ it "allows to mark posts as illegal when allow_all_users_to_flag_illegal_content setting is enabled" do
+ SiteSetting.email_address_to_report_illegal_content = "[email protected]"
+ SiteSetting.allow_all_users_to_flag_illegal_content = true
+
+ topic_page.visit_topic(topic)
+ topic_page.expand_post_actions(post_to_flag)
+ topic_page.click_post_action_button(post_to_flag, :flag)
+
+ expect(flag_modal).to have_choices(I18n.t("js.flagging.formatted_name.illegal"))
+
+ flag_modal.choose_type(:illegal)
+ flag_modal.fill_message("This looks totally illegal to me.")
+ flag_modal.check_confirmation
+ flag_modal.confirm_flag
+
+ expect(page).to have_content(I18n.t("js.post.actions.by_you.illegal"))
+ end
+ end
+
context "when tl0" do
fab!(:tl0_user) { Fabricate(:user, trust_level: TrustLevel[0]) }
before { sign_in(tl0_user) }