A platform for community discussion. Free, open, simple.
DEV: check if user can_chat inside the can_join_chat_channel guardian (#21812)
Andrei Prigorshnevd1760727cf75
diff --git a/plugins/chat/app/controllers/chat/chat_controller.rb b/plugins/chat/app/controllers/chat/chat_controller.rbindex aff6487826e..77a7090cd31 100644--- a/plugins/chat/app/controllers/chat/chat_controller.rb+++ b/plugins/chat/app/controllers/chat/chat_controller.rb@@ -212,8 +212,7 @@ module Chat.not_suspended.where(id: params[:user_ids])users.each do |user|- guardian = Guardian.new(user)- if guardian.can_chat? && guardian.can_join_chat_channel?(@chat_channel)+ if user.guardian.can_join_chat_channel?(@chat_channel)data = {message: "chat.invitation_notification",chat_channel_id: @chat_channel.id,diff --git a/plugins/chat/app/jobs/regular/chat/notify_watching.rb b/plugins/chat/app/jobs/regular/chat/notify_watching.rbindex 3bdae6f0fe6..4b62af0afce 100644--- a/plugins/chat/app/jobs/regular/chat/notify_watching.rb+++ b/plugins/chat/app/jobs/regular/chat/notify_watching.rb@@ -45,8 +45,7 @@ module Jobsdef send_notifications(membership)user = membership.user- guardian = ::Guardian.new(user)- return unless guardian.can_chat? && guardian.can_join_chat_channel?(@chat_channel)+ return unless user.guardian.can_join_chat_channel?(@chat_channel)return if ::Chat::Notifier.user_has_seen_message?(membership, @chat_message.id)return if online_user_ids.include?(user.id)diff --git a/plugins/chat/lib/chat/guardian_extensions.rb b/plugins/chat/lib/chat/guardian_extensions.rbindex cd9530dd5f0..989904a25d1 100644--- a/plugins/chat/lib/chat/guardian_extensions.rb+++ b/plugins/chat/lib/chat/guardian_extensions.rb@@ -103,6 +103,7 @@ module Chatdef can_join_chat_channel?(chat_channel)return false if anonymous?+ return false unless can_chat?can_preview_chat_channel?(chat_channel) &&(chat_channel.direct_message_channel? || can_post_in_category?(chat_channel.chatable))enddiff --git a/plugins/chat/lib/chat/notifier.rb b/plugins/chat/lib/chat/notifier.rbindex 0b731a29ea4..5a0dc633710 100644--- a/plugins/chat/lib/chat/notifier.rb+++ b/plugins/chat/lib/chat/notifier.rb@@ -159,10 +159,7 @@ module Chatdef group_users_to_notify(users)potential_participants, unreachable =- users.partition do |user|- guardian = Guardian.new(user)- guardian.can_chat? && guardian.can_join_chat_channel?(@chat_channel)- end+ users.partition { |user| user.guardian.can_join_chat_channel?(@chat_channel) }participants, welcome_to_join =potential_participants.partition do |participant|diff --git a/plugins/chat/spec/lib/chat/channel_fetcher_spec.rb b/plugins/chat/spec/lib/chat/channel_fetcher_spec.rbindex 6d0a31f3e9d..82de60560e6 100644--- a/plugins/chat/spec/lib/chat/channel_fetcher_spec.rb+++ b/plugins/chat/spec/lib/chat/channel_fetcher_spec.rb@@ -8,7 +8,8 @@ describe Chat::ChannelFetcher dofab!(:dm_channel2) { Fabricate(:direct_message) }fab!(:direct_message_channel1) { Fabricate(:direct_message_channel, chatable: dm_channel1) }fab!(:direct_message_channel2) { Fabricate(:direct_message_channel, chatable: dm_channel2) }- fab!(:user1) { Fabricate(:user) }+ fab!(:chatters) { Fabricate(:group) }+ fab!(:user1) { Fabricate(:user, group_ids: [chatters.id]) }fab!(:user2) { Fabricate(:user) }def guardian@@ -19,6 +20,8 @@ describe Chat::ChannelFetcher doChat::UserChatChannelMembership.where(user: user1)end+ before { SiteSetting.chat_allowed_groups = [chatters] }+describe ".structured" doit "returns open channel only" docategory_channel.user_chat_channel_memberships.create!(user: user1, following: true)diff --git a/plugins/chat/spec/lib/chat/guardian_extensions_spec.rb b/plugins/chat/spec/lib/chat/guardian_extensions_spec.rbindex 367ab23fd45..dec21b3ec14 100644--- a/plugins/chat/spec/lib/chat/guardian_extensions_spec.rb+++ b/plugins/chat/spec/lib/chat/guardian_extensions_spec.rb@@ -3,7 +3,8 @@require "rails_helper"RSpec.describe Chat::GuardianExtensions do- fab!(:user) { Fabricate(:user) }+ fab!(:chatters) { Fabricate(:group) }+ fab!(:user) { Fabricate(:user, group_ids: [chatters.id]) }fab!(:staff) { Fabricate(:user, admin: true) }fab!(:chat_group) { Fabricate(:group) }fab!(:channel) { Fabricate(:category_channel) }@@ -11,6 +12,8 @@ RSpec.describe Chat::GuardianExtensions dolet(:guardian) { Guardian.new(user) }let(:staff_guardian) { Guardian.new(staff) }+ before { SiteSetting.chat_allowed_groups = [chatters] }+it "cannot chat if the user is not in the Chat.allowed_group_ids" doSiteSetting.chat_allowed_groups = ""expect(guardian.can_chat?).to eq(false)diff --git a/plugins/chat/spec/lib/chat/message_bookmarkable_spec.rb b/plugins/chat/spec/lib/chat/message_bookmarkable_spec.rbindex 492ecce8755..f5d050dcf77 100644--- a/plugins/chat/spec/lib/chat/message_bookmarkable_spec.rb+++ b/plugins/chat/spec/lib/chat/message_bookmarkable_spec.rb@@ -5,7 +5,8 @@ require "rails_helper"describe Chat::MessageBookmarkable dosubject(:registered_bookmarkable) { RegisteredBookmarkable.new(described_class) }- fab!(:user) { Fabricate(:user) }+ fab!(:chatters) { Fabricate(:group) }+ fab!(:user) { Fabricate(:user, group_ids: [chatters.id]) }fab!(:guardian) { Guardian.new(user) }fab!(:other_category) { Fabricate(:private_category, group: Fabricate(:group)) }fab!(:category_channel) { Fabricate(:category_channel, chatable: other_category) }@@ -15,6 +16,7 @@ describe Chat::MessageBookmarkable dobefore doregister_test_bookmarkable(described_class)Chat::UserChatChannelMembership.create(chat_channel: channel, user: user, following: true)+ SiteSetting.chat_allowed_groups = [chatters]endafter { DiscoursePluginRegistry.reset_register!(:bookmarkables) }diff --git a/plugins/chat/spec/lib/chat/message_reactor_spec.rb b/plugins/chat/spec/lib/chat/message_reactor_spec.rbindex 2a6d3cb06ef..6efe970e746 100644--- a/plugins/chat/spec/lib/chat/message_reactor_spec.rb+++ b/plugins/chat/spec/lib/chat/message_reactor_spec.rb@@ -10,6 +10,8 @@ describe Chat::MessageReactor dofab!(:reactor) { described_class.new(reacting_user, channel) }fab!(:message_1) { Fabricate(:chat_message, chat_channel: channel, user: reacting_user) }+ before { SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone] }+it "calls guardian ensure_can_join_chat_channel!" doGuardian.any_instance.expects(:ensure_can_join_chat_channel!).oncemessage_reactor.react!(message_id: message_1.id, react_action: :add, emoji: ":+1:")diff --git a/plugins/chat/spec/requests/core_ext/bookmarks_controller_spec.rb b/plugins/chat/spec/requests/core_ext/bookmarks_controller_spec.rbindex 4bb99a2384e..c4d35355b31 100644--- a/plugins/chat/spec/requests/core_ext/bookmarks_controller_spec.rb+++ b/plugins/chat/spec/requests/core_ext/bookmarks_controller_spec.rb@@ -1,12 +1,14 @@# frozen_string_literal: trueRSpec.describe BookmarksController do- let(:current_user) { Fabricate(:user) }+ fab!(:chatters) { Fabricate(:group) }+ let(:current_user) { Fabricate(:user, group_ids: [chatters.id]) }let(:bookmark_message) { Fabricate(:chat_message) }let(:bookmark_user) { current_user }before doregister_test_bookmarkable(Chat::MessageBookmarkable)+ SiteSetting.chat_allowed_groups = [chatters]sign_in(current_user)enddiff --git a/plugins/chat/spec/services/chat/update_user_last_read_spec.rb b/plugins/chat/spec/services/chat/update_user_last_read_spec.rbindex 7345de703cd..9db2ee92fff 100644--- a/plugins/chat/spec/services/chat/update_user_last_read_spec.rb+++ b/plugins/chat/spec/services/chat/update_user_last_read_spec.rb@@ -9,16 +9,19 @@ RSpec.describe Chat::UpdateUserLastRead dodescribe ".call" dosubject(:result) { described_class.call(params) }- fab!(:current_user) { Fabricate(:user) }+ fab!(:chatters) { Fabricate(:group) }+ fab!(:current_user) { Fabricate(:user, group_ids: [chatters.id]) }fab!(:channel) { Fabricate(:chat_channel) }- fab!(:membership) do+ let(:membership) doFabricate(:user_chat_channel_membership, user: current_user, chat_channel: channel)end- fab!(:message_1) { Fabricate(:chat_message, chat_channel: membership.chat_channel) }+ let(:message_1) { Fabricate(:chat_message, chat_channel: membership.chat_channel) }let(:guardian) { Guardian.new(current_user) }let(:params) { { guardian: guardian, channel_id: channel.id, message_id: message_1.id } }+ before { SiteSetting.chat_allowed_groups = [chatters] }+context "when params are not valid" dobefore { params.delete(:message_id) }diff --git a/plugins/chat/spec/services/chat/update_user_thread_last_read_spec.rb b/plugins/chat/spec/services/chat/update_user_thread_last_read_spec.rbindex f8e48fb2342..c1bd66fcb03 100644--- a/plugins/chat/spec/services/chat/update_user_thread_last_read_spec.rb+++ b/plugins/chat/spec/services/chat/update_user_thread_last_read_spec.rb@@ -9,7 +9,8 @@ RSpec.describe Chat::UpdateUserThreadLastRead dodescribe ".call" dosubject(:result) { described_class.call(params) }- fab!(:current_user) { Fabricate(:user) }+ fab!(:chatters) { Fabricate(:group) }+ fab!(:current_user) { Fabricate(:user, group_ids: [chatters.id]) }fab!(:channel) { Fabricate(:chat_channel) }fab!(:thread) { Fabricate(:chat_thread, channel: channel, old_om: true) }fab!(:thread_reply_1) { Fabricate(:chat_message, chat_channel: channel, thread: thread) }@@ -18,6 +19,8 @@ RSpec.describe Chat::UpdateUserThreadLastRead dolet(:guardian) { Guardian.new(current_user) }let(:params) { { guardian: guardian, channel_id: channel.id, thread_id: thread.id } }+ before { SiteSetting.chat_allowed_groups = [chatters] }+context "when params are not valid" dobefore { params.delete(:thread_id) }diff --git a/plugins/chat/spec/system/react_to_message_spec.rb b/plugins/chat/spec/system/react_to_message_spec.rbindex fae323874e5..d675cca5b0e 100644--- a/plugins/chat/spec/system/react_to_message_spec.rb+++ b/plugins/chat/spec/system/react_to_message_spec.rb@@ -1,8 +1,8 @@# frozen_string_literal: trueRSpec.describe "React to message", type: :system do- fab!(:current_user) { Fabricate(:user) }- fab!(:other_user) { Fabricate(:user) }+ fab!(:current_user) { Fabricate(:user, group_ids: [Group::AUTO_GROUPS[:trust_level_1]]) }+ fab!(:other_user) { Fabricate(:user, group_ids: [Group::AUTO_GROUPS[:trust_level_1]]) }fab!(:category_channel_1) { Fabricate(:category_channel) }fab!(:message_1) { Fabricate(:chat_message, chat_channel: category_channel_1) }@@ -137,7 +137,7 @@ RSpec.describe "React to message", type: :system doendcontext "when current user and another have reacted" do- fab!(:other_user) { Fabricate(:user) }+ fab!(:other_user) { Fabricate(:user, group_ids: [Group::AUTO_GROUPS[:trust_level_1]]) }fab!(:reaction_1) doChat::MessageReactor.new(current_user, category_channel_1).react!(@@ -205,7 +205,7 @@ RSpec.describe "React to message", type: :system doendcontext "when receiving a duplicate reaction event" do- fab!(:user_1) { Fabricate(:user) }+ fab!(:user_1) { Fabricate(:user, group_ids: [Group::AUTO_GROUPS[:trust_level_1]]) }fab!(:reaction_2) doChat::MessageReactor.new(user_1, category_channel_1).react!(