mirror of
https://github.com/discourse/discourse.git
synced 2025-05-31 11:58:33 +08:00
DEV: Refactor some services from chat
Extracted from https://github.com/discourse/discourse/pull/29129. This patch makes the code more compliant with the upcoming service docs best practices.
This commit is contained in:

committed by
Loïc Guitaut

parent
43a0ea876a
commit
08e9364573
@ -47,6 +47,10 @@ module Chat
|
||||
|
||||
private
|
||||
|
||||
def fetch_channel(contract:)
|
||||
::Chat::Channel.includes(:chatable).find_by(id: contract.channel_id)
|
||||
end
|
||||
|
||||
def can_add_users_to_channel(guardian:, channel:)
|
||||
(guardian.user.admin? || channel.joined_by?(guardian.user)) &&
|
||||
channel.direct_message_channel? && channel.chatable.group
|
||||
@ -60,10 +64,6 @@ module Chat
|
||||
)
|
||||
end
|
||||
|
||||
def fetch_channel(contract:)
|
||||
::Chat::Channel.includes(:chatable).find_by(id: contract.channel_id)
|
||||
end
|
||||
|
||||
def upsert_memberships(channel:, target_users:)
|
||||
always_level = ::Chat::UserChatChannelMembership::NOTIFICATION_LEVELS[:always]
|
||||
|
||||
|
@ -14,13 +14,13 @@ module Chat
|
||||
class HandleCategoryUpdated
|
||||
include Service::Base
|
||||
|
||||
policy :chat_enabled
|
||||
contract do
|
||||
attribute :category_id, :integer
|
||||
|
||||
validates :category_id, presence: true
|
||||
end
|
||||
step :assign_defaults
|
||||
policy :chat_enabled
|
||||
model :category
|
||||
model :category_channel_ids
|
||||
model :users
|
||||
|
@ -21,13 +21,13 @@ module Chat
|
||||
class HandleDestroyedGroup
|
||||
include Service::Base
|
||||
|
||||
policy :chat_enabled
|
||||
contract do
|
||||
attribute :destroyed_group_user_ids
|
||||
attribute :destroyed_group_user_ids, :array
|
||||
|
||||
validates :destroyed_group_user_ids, presence: true
|
||||
end
|
||||
step :assign_defaults
|
||||
policy :chat_enabled
|
||||
policy :not_everyone_allowed
|
||||
model :scoped_users
|
||||
step :remove_users_outside_allowed_groups
|
||||
@ -48,7 +48,7 @@ module Chat
|
||||
!SiteSetting.chat_allowed_groups_map.include?(Group::AUTO_GROUPS[:everyone])
|
||||
end
|
||||
|
||||
def fetch_scoped_users(destroyed_group_user_ids:)
|
||||
def fetch_scoped_users(contract:)
|
||||
User
|
||||
.real
|
||||
.activated
|
||||
@ -56,7 +56,7 @@ module Chat
|
||||
.not_staged
|
||||
.includes(:group_users)
|
||||
.where("NOT admin AND NOT moderator")
|
||||
.where(id: destroyed_group_user_ids)
|
||||
.where(id: contract.destroyed_group_user_ids)
|
||||
.joins(:user_chat_channel_memberships)
|
||||
.distinct
|
||||
end
|
||||
|
@ -20,13 +20,13 @@ module Chat
|
||||
class HandleUserRemovedFromGroup
|
||||
include Service::Base
|
||||
|
||||
policy :chat_enabled
|
||||
contract do
|
||||
attribute :user_id, :integer
|
||||
|
||||
validates :user_id, presence: true
|
||||
end
|
||||
step :assign_defaults
|
||||
policy :chat_enabled
|
||||
policy :not_everyone_allowed
|
||||
model :user
|
||||
policy :user_not_staff
|
||||
|
@ -37,7 +37,7 @@ module Chat
|
||||
|
||||
validates :message_id, presence: true
|
||||
validates :channel_id, presence: true
|
||||
validates :flag_type_id, inclusion: { in: ->(_flag_type) { ::ReviewableScore.types.values } }
|
||||
validates :flag_type_id, inclusion: { in: -> { ::ReviewableScore.types.values } }
|
||||
end
|
||||
model :message
|
||||
policy :can_flag_message_in_channel
|
||||
|
@ -39,7 +39,6 @@ module Chat
|
||||
},
|
||||
allow_nil: true
|
||||
end
|
||||
|
||||
model :channel
|
||||
policy :can_view_channel
|
||||
model :membership, optional: true
|
||||
|
@ -34,7 +34,7 @@ module Chat
|
||||
private
|
||||
|
||||
def clean_term(contract:)
|
||||
context[:term] = contract.term&.downcase&.strip&.gsub(/^[@#]+/, "")
|
||||
contract.term = contract.term&.downcase&.strip&.gsub(/^[@#]+/, "")
|
||||
end
|
||||
|
||||
def fetch_memberships(guardian:)
|
||||
@ -44,31 +44,31 @@ module Chat
|
||||
def fetch_users(guardian:, contract:)
|
||||
return unless contract.include_users
|
||||
return unless guardian.can_create_direct_message?
|
||||
search_users(context, guardian)
|
||||
search_users(contract, guardian)
|
||||
end
|
||||
|
||||
def fetch_groups(guardian:, contract:)
|
||||
return unless contract.include_groups
|
||||
return unless guardian.can_create_direct_message?
|
||||
search_groups(context, guardian)
|
||||
search_groups(contract, guardian)
|
||||
end
|
||||
|
||||
def fetch_category_channels(guardian:, contract:)
|
||||
return unless contract.include_category_channels
|
||||
return unless SiteSetting.enable_public_channels
|
||||
search_category_channels(context, guardian)
|
||||
search_category_channels(contract, guardian)
|
||||
end
|
||||
|
||||
def fetch_direct_message_channels(guardian:, contract:, users:)
|
||||
return unless contract.include_direct_message_channels
|
||||
return unless guardian.can_create_direct_message?
|
||||
search_direct_message_channels(context, guardian, contract, users)
|
||||
search_direct_message_channels(guardian, contract, users)
|
||||
end
|
||||
|
||||
def search_users(context, guardian)
|
||||
user_search = ::UserSearch.new(context.term, limit: SEARCH_RESULT_LIMIT)
|
||||
def search_users(contract, guardian)
|
||||
user_search = ::UserSearch.new(contract.term, limit: SEARCH_RESULT_LIMIT)
|
||||
|
||||
if context.term.blank?
|
||||
if contract.term.blank?
|
||||
user_search = user_search.scoped_users
|
||||
else
|
||||
user_search = user_search.search
|
||||
@ -80,45 +80,45 @@ module Chat
|
||||
user_search = user_search.real(allowed_bot_user_ids: allowed_bot_user_ids)
|
||||
user_search = user_search.includes(:user_option)
|
||||
|
||||
if context.excluded_memberships_channel_id
|
||||
if contract.excluded_memberships_channel_id
|
||||
user_search =
|
||||
user_search.where(
|
||||
"NOT EXISTS (SELECT 1 FROM user_chat_channel_memberships WHERE user_id = users.id AND chat_channel_id = ?)",
|
||||
context.excluded_memberships_channel_id,
|
||||
contract.excluded_memberships_channel_id,
|
||||
)
|
||||
end
|
||||
|
||||
user_search
|
||||
end
|
||||
|
||||
def search_groups(context, guardian)
|
||||
def search_groups(contract, guardian)
|
||||
Group
|
||||
.visible_groups(guardian.user)
|
||||
.includes(users: :user_option)
|
||||
.where(
|
||||
"groups.name ILIKE :term_like OR groups.full_name ILIKE :term_like",
|
||||
term_like: "%#{context.term}%",
|
||||
term_like: "%#{contract.term}%",
|
||||
)
|
||||
.limit(SEARCH_RESULT_LIMIT)
|
||||
end
|
||||
|
||||
def search_category_channels(context, guardian)
|
||||
def search_category_channels(contract, guardian)
|
||||
::Chat::ChannelFetcher.secured_public_channel_search(
|
||||
guardian,
|
||||
status: :open,
|
||||
filter: context.term,
|
||||
filter: contract.term,
|
||||
filter_on_category_name: false,
|
||||
match_filter_on_starts_with: false,
|
||||
limit: SEARCH_RESULT_LIMIT,
|
||||
)
|
||||
end
|
||||
|
||||
def search_direct_message_channels(context, guardian, contract, users)
|
||||
def search_direct_message_channels(guardian, contract, users)
|
||||
channels =
|
||||
::Chat::ChannelFetcher.secured_direct_message_channels_search(
|
||||
guardian.user.id,
|
||||
guardian,
|
||||
filter: context.term,
|
||||
filter: contract.term,
|
||||
match_filter_on_starts_with: false,
|
||||
limit: SEARCH_RESULT_LIMIT,
|
||||
) || []
|
||||
|
@ -65,13 +65,11 @@ module Chat
|
||||
end
|
||||
|
||||
def update_channel(channel:, contract:)
|
||||
channel.assign_attributes(contract.attributes)
|
||||
context[:threading_enabled_changed] = channel.threading_enabled_changed?
|
||||
channel.save!
|
||||
channel.update!(contract.attributes)
|
||||
end
|
||||
|
||||
def mark_all_threads_as_read_if_needed(channel:)
|
||||
return if !(context.threading_enabled_changed && channel.threading_enabled)
|
||||
return unless channel.threading_enabled_previously_changed?(to: true)
|
||||
Jobs.enqueue(Jobs::Chat::MarkAllChannelThreadsRead, channel_id: channel.id)
|
||||
end
|
||||
|
||||
|
@ -17,7 +17,7 @@ module Chat
|
||||
|
||||
model :channel, :fetch_channel
|
||||
contract do
|
||||
attribute :status
|
||||
attribute :status, :string
|
||||
|
||||
validates :status, inclusion: { in: Chat::Channel.editable_statuses.keys }
|
||||
end
|
||||
@ -30,13 +30,13 @@ module Chat
|
||||
Chat::Channel.find_by(id: channel_id)
|
||||
end
|
||||
|
||||
def check_channel_permission(guardian:, channel:, status:)
|
||||
def check_channel_permission(guardian:, channel:, contract:)
|
||||
guardian.can_preview_chat_channel?(channel) &&
|
||||
guardian.can_change_channel_status?(channel, status.to_sym)
|
||||
guardian.can_change_channel_status?(channel, contract.status.to_sym)
|
||||
end
|
||||
|
||||
def change_status(channel:, status:, guardian:)
|
||||
channel.public_send("#{status}!", guardian.user)
|
||||
def change_status(channel:, contract:, guardian:)
|
||||
channel.public_send("#{contract.status}!", guardian.user)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user