DEV: Remove the need for splat operator in services

This commit is contained in:
Loïc Guitaut 2024-03-07 15:44:12 +01:00 committed by Loïc Guitaut
parent e89bdea830
commit ac0808a320
36 changed files with 221 additions and 222 deletions

View File

@ -51,12 +51,12 @@ module Chat
private
def can_add_users_to_channel(guardian:, channel:, **)
def can_add_users_to_channel(guardian:, channel:)
(guardian.user.admin? || channel.joined_by?(guardian.user)) &&
channel.direct_message_channel? && channel.chatable.group
end
def fetch_users(contract:, channel:, **)
def fetch_users(contract:, channel:)
::Chat::UsersFromUsernamesAndGroupsQuery.call(
usernames: contract.usernames,
groups: contract.groups,
@ -64,17 +64,17 @@ module Chat
)
end
def fetch_channel(contract:, **)
def fetch_channel(contract:)
::Chat::Channel.includes(:chatable).find_by(id: contract.channel_id)
end
def validate_user_count(channel:, users:, **)
def validate_user_count(channel:, users:)
if channel.user_count + users.length > SiteSetting.chat_max_direct_message_users
fail!("should have less than #{SiteSetting.chat_max_direct_message_users} elements")
end
end
def upsert_memberships(channel:, users:, **)
def upsert_memberships(channel:, users:)
always_level = ::Chat::UserChatChannelMembership::NOTIFICATION_LEVELS[:always]
memberships =
@ -119,7 +119,7 @@ module Chat
)
end
def recompute_users_count(channel:, **)
def recompute_users_count(channel:)
return if context.added_user_ids.blank?
channel.update!(
@ -128,7 +128,7 @@ module Chat
)
end
def notice_channel(guardian:, channel:, users:, **)
def notice_channel(guardian:, channel:, users:)
added_users = users.select { |u| context.added_user_ids.include?(u.id) }
return if added_users.blank?

View File

@ -44,16 +44,16 @@ module Chat
private
def fetch_channel(contract:, **)
def fetch_channel(contract:)
::Chat::CategoryChannel.find_by(id: contract.channel_id, auto_join_users: true)
end
def create_memberships(channel:, contract:, **)
def create_memberships(channel:, contract:)
context.added_user_ids =
::Chat::Action::CreateMembershipsForAutoJoin.call(channel: channel, contract: contract)
end
def recalculate_user_count(channel:, added_user_ids:, **)
def recalculate_user_count(channel:, added_user_ids:)
# Only do this if we are running auto-join for a single user, if we
# are doing it for many then we should do it after all batches are
# complete for the channel in Jobs::AutoJoinChannelMemberships
@ -61,7 +61,7 @@ module Chat
::Chat::ChannelMembershipManager.new(channel).recalculate_user_count
end
def publish_new_channel(channel:, added_user_ids:, **)
def publish_new_channel(channel:, added_user_ids:)
::Chat::Publisher.publish_new_channel(channel, User.where(id: added_user_ids))
end
end

View File

@ -39,15 +39,15 @@ module Chat
SiteSetting.chat_enabled
end
def fetch_category(contract:, **)
def fetch_category(contract:)
Category.find_by(id: contract.category_id)
end
def fetch_category_channel_ids(category:, **)
def fetch_category_channel_ids(category:)
Chat::Channel.where(chatable: category).pluck(:id)
end
def fetch_users(category_channel_ids:, **)
def fetch_users(category_channel_ids:)
User
.real
.activated
@ -58,7 +58,7 @@ module Chat
.where("NOT admin AND NOT moderator")
end
def remove_users_without_channel_permission(users:, category_channel_ids:, **)
def remove_users_without_channel_permission(users:, category_channel_ids:)
memberships_to_remove =
Chat::Action::CalculateMembershipsForRemoval.call(
scoped_users: users,
@ -72,7 +72,7 @@ module Chat
)
end
def publish(users_removed_map:, **)
def publish(users_removed_map:)
Chat::Action::PublishAutoRemovedUser.call(
event_type: :category_updated,
users_removed_map: users_removed_map,

View File

@ -32,15 +32,15 @@ module Chat
SiteSetting.chat_enabled
end
def cast_new_allowed_groups_to_array(new_allowed_groups:, **)
def cast_new_allowed_groups_to_array(new_allowed_groups:)
context[:new_allowed_groups] = new_allowed_groups.to_s.split("|").map(&:to_i)
end
def not_everyone_allowed(new_allowed_groups:, **)
def not_everyone_allowed(new_allowed_groups:)
!new_allowed_groups.include?(Group::AUTO_GROUPS[:everyone])
end
def fetch_users(new_allowed_groups:, **)
def fetch_users(new_allowed_groups:)
User
.real
.activated
@ -61,20 +61,20 @@ module Chat
end
end
def fetch_memberships_to_remove(users:, **)
def fetch_memberships_to_remove(users:)
Chat::UserChatChannelMembership
.joins(:chat_channel)
.where(user_id: users.pluck(:id))
.where.not(chat_channel: { type: "DirectMessageChannel" })
end
def remove_users_outside_allowed_groups(memberships_to_remove:, **)
def remove_users_outside_allowed_groups(memberships_to_remove:)
context[:users_removed_map] = Chat::Action::RemoveMemberships.call(
memberships: memberships_to_remove,
)
end
def publish(users_removed_map:, **)
def publish(users_removed_map:)
Chat::Action::PublishAutoRemovedUser.call(
event_type: :chat_allowed_groups_changed,
users_removed_map: users_removed_map,

View File

@ -50,7 +50,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(destroyed_group_user_ids:)
User
.real
.activated
@ -63,7 +63,7 @@ module Chat
.distinct
end
def remove_users_outside_allowed_groups(scoped_users:, **)
def remove_users_outside_allowed_groups(scoped_users:)
users = scoped_users
# Remove any of these users from all category channels if they
@ -96,7 +96,7 @@ module Chat
)
end
def remove_users_without_channel_permission(scoped_users:, **)
def remove_users_without_channel_permission(scoped_users:)
memberships_to_remove =
Chat::Action::CalculateMembershipsForRemoval.call(scoped_users: scoped_users)
@ -110,7 +110,7 @@ module Chat
)
end
def publish(users_removed_map:, **)
def publish(users_removed_map:)
Chat::Action::PublishAutoRemovedUser.call(
event_type: :destroyed_group,
users_removed_map: users_removed_map,

View File

@ -50,15 +50,15 @@ module Chat
!SiteSetting.chat_allowed_groups_map.include?(Group::AUTO_GROUPS[:everyone])
end
def fetch_user(contract:, **)
def fetch_user(contract:)
User.find_by(id: contract.user_id)
end
def user_not_staff(user:, **)
def user_not_staff(user:)
!user.staff?
end
def remove_if_outside_chat_allowed_groups(user:, **)
def remove_if_outside_chat_allowed_groups(user:)
if SiteSetting.chat_allowed_groups_map.empty? ||
!GroupUser.exists?(group_id: SiteSetting.chat_allowed_groups_map, user: user)
memberships_to_remove =
@ -75,7 +75,7 @@ module Chat
end
end
def remove_from_private_channels(user:, **)
def remove_from_private_channels(user:)
memberships_to_remove =
Chat::Action::CalculateMembershipsForRemoval.call(scoped_users: [user])
@ -89,7 +89,7 @@ module Chat
)
end
def publish(users_removed_map:, **)
def publish(users_removed_map:)
Chat::Action::PublishAutoRemovedUser.call(
event_type: :user_removed_from_group,
users_removed_map: users_removed_map,

View File

@ -62,19 +62,19 @@ module Chat
SiteSetting.enable_public_channels
end
def can_create_channel(guardian:, **)
def can_create_channel(guardian:)
guardian.can_create_chat_channel?
end
def fetch_category(contract:, **)
def fetch_category(contract:)
Category.find_by(id: contract.category_id)
end
def category_channel_does_not_exist(category:, contract:, **)
def category_channel_does_not_exist(category:, contract:)
!Chat::Channel.exists?(chatable: category, name: contract.name)
end
def create_channel(category:, contract:, **)
def create_channel(category:, contract:)
category.create_chat_channel(
name: contract.name,
slug: contract.slug,
@ -85,11 +85,11 @@ module Chat
)
end
def create_membership(channel:, guardian:, **)
def create_membership(channel:, guardian:)
channel.user_chat_channel_memberships.create(user: guardian.user, following: true)
end
def enforce_automatic_channel_memberships(channel:, **)
def enforce_automatic_channel_memberships(channel:)
return if !channel.auto_join_users?
Chat::ChannelMembershipManager.new(channel).enforce_automatic_channel_memberships
end

View File

@ -53,7 +53,7 @@ module Chat
private
def can_create_direct_message(guardian:, target_users:, **)
def can_create_direct_message(guardian:, target_users:)
guardian.can_create_direct_message? &&
DiscoursePluginRegistry.apply_modifier(
:chat_can_create_direct_message_channel,
@ -62,28 +62,28 @@ module Chat
)
end
def fetch_target_users(guardian:, contract:, **)
def fetch_target_users(guardian:, contract:)
::Chat::UsersFromUsernamesAndGroupsQuery.call(
usernames: [*contract.target_usernames, guardian.user.username],
groups: contract.target_groups,
)
end
def fetch_user_comm_screener(target_users:, guardian:, **)
def fetch_user_comm_screener(target_users:, guardian:)
UserCommScreener.new(acting_user: guardian.user, target_user_ids: target_users.map(&:id))
end
def validate_user_count(target_users:, **)
def validate_user_count(target_users:)
if target_users.length > SiteSetting.chat_max_direct_message_users
fail!("should have less than #{SiteSetting.chat_max_direct_message_users} elements")
end
end
def actor_allows_dms(user_comm_screener:, **)
def actor_allows_dms(user_comm_screener:)
!user_comm_screener.actor_disallowing_all_pms?
end
def fetch_or_create_direct_message(target_users:, contract:, **)
def fetch_or_create_direct_message(target_users:, contract:)
ids = target_users.map(&:id)
if ids.size > 2 || contract.name.present?
@ -93,15 +93,15 @@ module Chat
end
end
def fetch_or_create_channel(direct_message:, **)
def fetch_or_create_channel(direct_message:)
::Chat::DirectMessageChannel.find_or_create_by(chatable: direct_message)
end
def set_optional_name(channel:, contract:, **)
def set_optional_name(channel:, contract:)
channel.update!(name: contract.name) if contract.name&.length&.positive?
end
def update_memberships(channel:, target_users:, **)
def update_memberships(channel:, target_users:)
always_level = ::Chat::UserChatChannelMembership::NOTIFICATION_LEVELS[:always]
memberships =
@ -124,7 +124,7 @@ module Chat
)
end
def recompute_users_count(channel:, **)
def recompute_users_count(channel:)
channel.update!(
user_count: ::Chat::ChannelMembershipsQuery.count(channel),
user_count_stale: false,

View File

@ -70,15 +70,15 @@ module Chat
private
def no_silenced_user(guardian:, **)
def no_silenced_user(guardian:)
!guardian.is_silenced?
end
def fetch_channel(contract:, **)
def fetch_channel(contract:)
Chat::Channel.find_by_id_or_slug(contract.chat_channel_id)
end
def enforce_system_membership(guardian:, channel:, contract:, **)
def enforce_system_membership(guardian:, channel:, contract:)
if guardian.user&.is_system_user? || contract.enforce_membership
channel.add(guardian.user)
@ -88,24 +88,24 @@ module Chat
end
end
def allowed_to_join_channel(guardian:, channel:, **)
def allowed_to_join_channel(guardian:, channel:)
guardian.can_join_chat_channel?(channel)
end
def fetch_channel_membership(guardian:, channel:, **)
def fetch_channel_membership(guardian:, channel:)
Chat::ChannelMembershipManager.new(channel).find_for_user(guardian.user)
end
def fetch_reply(contract:, **)
def fetch_reply(contract:)
Chat::Message.find_by(id: contract.in_reply_to_id)
end
def ensure_reply_consistency(channel:, contract:, reply:, **)
def ensure_reply_consistency(channel:, contract:, reply:)
return true if contract.in_reply_to_id.blank?
reply&.chat_channel == channel
end
def fetch_thread(contract:, reply:, channel:, **)
def fetch_thread(contract:, reply:, channel:)
return Chat::Thread.find_by(id: contract.thread_id) if contract.thread_id.present?
return unless reply
reply.thread ||
@ -117,22 +117,22 @@ module Chat
)
end
def ensure_valid_thread_for_channel(thread:, contract:, channel:, **)
def ensure_valid_thread_for_channel(thread:, contract:, channel:)
return true if contract.thread_id.blank?
thread&.channel == channel
end
def ensure_thread_matches_parent(thread:, reply:, **)
def ensure_thread_matches_parent(thread:, reply:)
return true unless thread && reply
reply.thread == thread
end
def fetch_uploads(contract:, guardian:, **)
def fetch_uploads(contract:, guardian:)
return [] if !SiteSetting.chat_allow_uploads
guardian.user.uploads.where(id: contract.upload_ids)
end
def instantiate_message(channel:, guardian:, contract:, uploads:, thread:, reply:, **)
def instantiate_message(channel:, guardian:, contract:, uploads:, thread:, reply:)
channel.chat_messages.new(
user: guardian.user,
last_editor: guardian.user,
@ -146,15 +146,15 @@ module Chat
)
end
def save_message(message_instance:, **)
def save_message(message_instance:)
message_instance.save!
end
def delete_drafts(channel:, guardian:, **)
def delete_drafts(channel:, guardian:)
Chat::Draft.where(user: guardian.user, chat_channel: channel).destroy_all
end
def post_process_thread(thread:, message_instance:, guardian:, **)
def post_process_thread(thread:, message_instance:, guardian:)
return unless thread
thread.update!(last_message: message_instance)
@ -163,36 +163,36 @@ module Chat
thread.add(thread.original_message_user)
end
def create_webhook_event(contract:, message_instance:, **)
def create_webhook_event(contract:, message_instance:)
return if contract.incoming_chat_webhook.blank?
message_instance.create_chat_webhook_event(
incoming_chat_webhook: contract.incoming_chat_webhook,
)
end
def update_channel_last_message(channel:, message_instance:, **)
def update_channel_last_message(channel:, message_instance:)
return if message_instance.in_thread?
channel.update!(last_message: message_instance)
end
def update_membership_last_read(channel_membership:, message_instance:, **)
def update_membership_last_read(channel_membership:, message_instance:)
return if message_instance.in_thread?
channel_membership.update!(last_read_message: message_instance)
end
def process_direct_message_channel(channel_membership:, **)
def process_direct_message_channel(channel_membership:)
Chat::Action::PublishAndFollowDirectMessageChannel.call(
channel_membership: channel_membership,
)
end
def publish_new_thread(reply:, contract:, channel:, thread:, **)
def publish_new_thread(reply:, contract:, channel:, thread:)
return unless channel.threading_enabled? || thread&.force
return unless reply&.thread_id_previously_changed?(from: nil)
Chat::Publisher.publish_thread_created!(channel, reply, thread.id)
end
def process(channel:, message_instance:, contract:, thread:, **)
def process(channel:, message_instance:, contract:, thread:)
::Chat::Publisher.publish_new!(channel, message_instance, contract.staged_id)
DiscourseEvent.trigger(
@ -222,7 +222,7 @@ module Chat
end
end
def publish_user_tracking_state(message_instance:, channel:, channel_membership:, guardian:, **)
def publish_user_tracking_state(message_instance:, channel:, channel_membership:, guardian:)
message_to_publish = message_instance
message_to_publish =
channel_membership.last_read_message || message_instance if message_instance.in_thread?

View File

@ -41,26 +41,26 @@ module Chat
private
def fetch_channel(contract:, **)
def fetch_channel(contract:)
::Chat::Channel.find_by(id: contract.channel_id)
end
def fetch_original_message(channel:, contract:, **)
def fetch_original_message(channel:, contract:)
::Chat::Message.find_by(
id: contract.original_message_id,
chat_channel_id: contract.channel_id,
)
end
def can_view_channel(guardian:, channel:, **)
def can_view_channel(guardian:, channel:)
guardian.can_preview_chat_channel?(channel)
end
def threading_enabled_for_channel(channel:, **)
def threading_enabled_for_channel(channel:)
channel.threading_enabled
end
def find_or_create_thread(channel:, original_message:, contract:, **)
def find_or_create_thread(channel:, original_message:, contract:)
if original_message.thread_id.present?
return context.thread = ::Chat::Thread.find_by(id: original_message.thread_id)
end
@ -74,15 +74,15 @@ module Chat
fail!(context.thread.errors.full_messages.join(", ")) if context.thread.invalid?
end
def associate_thread_to_message(original_message:, **)
def associate_thread_to_message(original_message:)
original_message.update(thread: context.thread)
end
def fetch_membership(guardian:, **)
def fetch_membership(guardian:)
context.membership = context.thread.membership_for(guardian.user)
end
def publish_new_thread(channel:, original_message:, **)
def publish_new_thread(channel:, original_message:)
::Chat::Publisher.publish_thread_created!(channel, original_message, context.thread.id)
end

View File

@ -50,14 +50,14 @@ module Chat
private
def fetch_message(contract:, **)
def fetch_message(contract:)
Chat::Message.includes(:chat_channel, :revisions).find_by(
id: contract.message_id,
chat_channel_id: contract.channel_id,
)
end
def can_flag_message_in_channel(guardian:, contract:, message:, **)
def can_flag_message_in_channel(guardian:, contract:, message:)
guardian.can_join_chat_channel?(message.chat_channel) &&
guardian.can_flag_chat_message?(message) &&
guardian.can_flag_message_as?(
@ -71,7 +71,7 @@ module Chat
)
end
def flag_message(message:, contract:, guardian:, **)
def flag_message(message:, contract:, guardian:)
Chat::ReviewQueue.new.flag_message(
message,
guardian,

View File

@ -35,15 +35,15 @@ module Chat
private
def fetch_channel(contract:, **)
def fetch_channel(contract:)
::Chat::Channel.find_by(id: contract.channel_id)
end
def can_view_channel(guardian:, channel:, **)
def can_view_channel(guardian:, channel:)
guardian.can_preview_chat_channel?(channel)
end
def fetch_users(contract:, **)
def fetch_users(contract:)
::User
.joins(:user_option)
.where(user_options: { chat_enabled: true })
@ -52,7 +52,7 @@ module Chat
.limit(50)
end
def send_invite_notifications(channel:, guardian:, users:, contract:, **)
def send_invite_notifications(channel:, guardian:, users:, contract:)
users&.each do |invited_user|
next if !invited_user.guardian.can_join_chat_channel?(channel)

View File

@ -30,11 +30,11 @@ module Chat
private
def fetch_channel(contract:, **)
def fetch_channel(contract:)
Chat::Channel.find_by(id: contract.channel_id)
end
def leave(channel:, guardian:, **)
def leave(channel:, guardian:)
ActiveRecord::Base.transaction do
if channel.direct_message_channel? && channel.chatable&.group
channel.membership_for(guardian.user)&.destroy!
@ -45,7 +45,7 @@ module Chat
end
end
def recompute_users_count(channel:, **)
def recompute_users_count(channel:)
channel.update!(
user_count: ::Chat::ChannelMembershipsQuery.count(channel),
user_count_stale: false,

View File

@ -58,23 +58,23 @@ module Chat
private
def fetch_channel(contract:, **)
def fetch_channel(contract:)
::Chat::Channel.includes(:chatable).find_by(id: contract.channel_id)
end
def fetch_optional_membership(channel:, guardian:, **)
def fetch_optional_membership(channel:, guardian:)
context.membership = channel.membership_for(guardian.user)
end
def enabled_threads?(channel:, **)
def enabled_threads?(channel:)
context.enabled_threads = channel.threading_enabled
end
def can_view_channel(guardian:, channel:, **)
def can_view_channel(guardian:, channel:)
guardian.can_preview_chat_channel?(channel)
end
def determine_target_message_id(contract:, **)
def determine_target_message_id(contract:)
if contract.fetch_from_last_read
context.target_message_id = context.membership&.last_read_message_id
else
@ -82,7 +82,7 @@ module Chat
end
end
def target_message_exists(channel:, guardian:, **)
def target_message_exists(channel:, guardian:)
return true if context.target_message_id.blank?
target_message =
@ -98,7 +98,7 @@ module Chat
true
end
def fetch_messages(channel:, contract:, guardian:, enabled_threads:, **)
def fetch_messages(channel:, contract:, guardian:, enabled_threads:)
messages_data =
::Chat::MessagesQuery.call(
channel: channel,
@ -131,7 +131,7 @@ module Chat
].flatten.compact
end
def fetch_tracking(guardian:, **)
def fetch_tracking(guardian:)
context.tracking = {}
return if !context.thread_ids.present?
@ -144,18 +144,18 @@ module Chat
)
end
def fetch_thread_ids(messages:, **)
def fetch_thread_ids(messages:)
context.thread_ids = messages.map(&:thread_id).compact.uniq
end
def fetch_thread_participants(messages:, **)
def fetch_thread_participants(messages:)
return if context.thread_ids.empty?
context.thread_participants =
::Chat::ThreadParticipantQuery.call(thread_ids: context.thread_ids)
end
def fetch_thread_memberships(guardian:, **)
def fetch_thread_memberships(guardian:)
return if context.thread_ids.empty?
context.thread_memberships =
@ -165,7 +165,7 @@ module Chat
)
end
def update_membership_last_viewed_at(guardian:, **)
def update_membership_last_viewed_at(guardian:)
Scheduler::Defer.later "Chat::ListChannelMessages - defer update_membership_last_viewed_at" do
context.membership&.update!(last_viewed_at: Time.zone.now)
end

View File

@ -52,23 +52,23 @@ module Chat
private
def fetch_optional_membership(thread:, guardian:, **)
def fetch_optional_membership(thread:, guardian:)
context.membership = thread.membership_for(guardian.user)
end
def fetch_thread(contract:, **)
def fetch_thread(contract:)
::Chat::Thread.strict_loading.includes(channel: :chatable).find_by(id: contract.thread_id)
end
def ensure_thread_enabled(thread:, **)
def ensure_thread_enabled(thread:)
thread.channel.threading_enabled || thread.force
end
def can_view_thread(guardian:, thread:, **)
def can_view_thread(guardian:, thread:)
guardian.can_preview_chat_channel?(thread.channel)
end
def determine_target_message_id(contract:, membership:, guardian:, **)
def determine_target_message_id(contract:, membership:, guardian:)
if contract.fetch_from_last_read
context.target_message_id = membership&.last_read_message_id
else
@ -76,7 +76,7 @@ module Chat
end
end
def target_message_exists(contract:, guardian:, **)
def target_message_exists(contract:, guardian:)
return true if context.target_message_id.blank?
target_message =
::Chat::Message.with_deleted.find_by(
@ -88,7 +88,7 @@ module Chat
target_message.user_id == guardian.user.id || guardian.is_staff?
end
def fetch_messages(thread:, guardian:, contract:, **)
def fetch_messages(thread:, guardian:, contract:)
messages_data =
::Chat::MessagesQuery.call(
channel: thread.channel,

View File

@ -54,27 +54,27 @@ module Chat
private
def set_limit(contract:, **)
def set_limit(contract:)
context.limit = (contract.limit || THREADS_LIMIT).to_i.clamp(1, THREADS_LIMIT)
end
def set_offset(contract:, **)
def set_offset(contract:)
context.offset = [contract.offset || 0, 0].max
end
def fetch_channel(contract:, **)
def fetch_channel(contract:)
::Chat::Channel.strict_loading.includes(:chatable).find_by(id: contract.channel_id)
end
def threading_enabled_for_channel(channel:, **)
def threading_enabled_for_channel(channel:)
channel.threading_enabled
end
def can_view_channel(guardian:, channel:, **)
def can_view_channel(guardian:, channel:)
guardian.can_preview_chat_channel?(channel)
end
def fetch_threads(guardian:, channel:, **)
def fetch_threads(guardian:, channel:)
::Chat::Thread
.includes(
:channel,
@ -121,7 +121,7 @@ module Chat
.offset(context.offset)
end
def fetch_tracking(guardian:, threads:, **)
def fetch_tracking(guardian:, threads:)
context.tracking =
::Chat::TrackingStateReportQuery.call(
guardian: guardian,
@ -130,7 +130,7 @@ module Chat
).thread_tracking
end
def fetch_memberships(guardian:, threads:, **)
def fetch_memberships(guardian:, threads:)
context.memberships =
::Chat::UserChatThreadMembership.where(
thread_id: threads.map(&:id),
@ -138,11 +138,11 @@ module Chat
)
end
def fetch_participants(threads:, **)
def fetch_participants(threads:)
context.participants = ::Chat::ThreadParticipantQuery.call(thread_ids: threads.map(&:id))
end
def build_load_more_url(contract:, **)
def build_load_more_url(contract:)
load_more_params = { offset: context.offset + context.limit }.to_query
context.load_more_url =
::URI::HTTP.build(

View File

@ -33,7 +33,7 @@ module Chat
private
def fetch_thread(contract:, **)
def fetch_thread(contract:)
Chat::Thread.includes(
:channel,
original_message_user: :user_status,
@ -41,19 +41,19 @@ module Chat
).find_by(id: contract.thread_id, channel_id: contract.channel_id)
end
def invalid_access(guardian:, thread:, **)
def invalid_access(guardian:, thread:)
guardian.can_preview_chat_channel?(thread.channel)
end
def threading_enabled_for_channel(thread:, **)
def threading_enabled_for_channel(thread:)
thread.channel.threading_enabled || thread.force
end
def fetch_membership(thread:, guardian:, **)
def fetch_membership(thread:, guardian:)
context.membership = thread.membership_for(guardian.user)
end
def fetch_participants(thread:, **)
def fetch_participants(thread:)
context.participants = ::Chat::ThreadParticipantQuery.call(thread_ids: [thread.id])[thread.id]
end
end

View File

@ -37,15 +37,15 @@ module Chat
private
def set_limit(contract:, **)
def set_limit(contract:)
context.limit = (contract.limit || THREADS_LIMIT).to_i.clamp(1, THREADS_LIMIT)
end
def set_offset(contract:, **)
def set_offset(contract:)
context.offset = [contract.offset || 0, 0].max
end
def fetch_threads(guardian:, **)
def fetch_threads(guardian:)
::Chat::Thread
.includes(
:channel,
@ -114,7 +114,7 @@ module Chat
.offset(context.offset)
end
def fetch_tracking(guardian:, threads:, **)
def fetch_tracking(guardian:, threads:)
context.tracking =
::Chat::TrackingStateReportQuery.call(
guardian: guardian,
@ -123,7 +123,7 @@ module Chat
).thread_tracking
end
def fetch_memberships(guardian:, threads:, **)
def fetch_memberships(guardian:, threads:)
context.memberships =
::Chat::UserChatThreadMembership.where(
thread_id: threads.map(&:id),
@ -131,11 +131,11 @@ module Chat
)
end
def fetch_participants(threads:, **)
def fetch_participants(threads:)
context.participants = ::Chat::ThreadParticipantQuery.call(thread_ids: threads.map(&:id))
end
def build_load_more_url(contract:, **)
def build_load_more_url(contract:)
load_more_params = { limit: context.limit, offset: context.offset + context.limit }.to_query
context.load_more_url =

View File

@ -22,7 +22,7 @@ module Chat
private
def update_last_read_message_ids(guardian:, **)
def update_last_read_message_ids(guardian:)
updated_memberships = DB.query(<<~SQL, user_id: guardian.user.id)
UPDATE user_chat_channel_memberships
SET last_read_message_id = chat_channels.last_message_id
@ -38,7 +38,7 @@ module Chat
context[:updated_memberships] = updated_memberships
end
def mark_associated_mentions_as_read(guardian:, updated_memberships:, **)
def mark_associated_mentions_as_read(guardian:, updated_memberships:)
return if updated_memberships.empty?
::Chat::Action::MarkMentionsRead.call(
@ -47,7 +47,7 @@ module Chat
)
end
def publish_user_tracking_state(guardian:, updated_memberships:, **)
def publish_user_tracking_state(guardian:, updated_memberships:)
data =
updated_memberships.each_with_object({}) do |membership, data_hash|
data_hash[membership.channel_id] = {

View File

@ -37,31 +37,31 @@ module Chat
private
def fetch_message(contract:, **)
def fetch_message(contract:)
Chat::Message
.with_deleted
.includes(chat_channel: :chatable)
.find_by(id: contract.message_id, chat_channel_id: contract.channel_id)
end
def invalid_access(guardian:, message:, **)
def invalid_access(guardian:, message:)
guardian.can_restore_chat?(message, message.chat_channel.chatable)
end
def restore_message(message:, **)
def restore_message(message:)
message.recover!
end
def update_thread_reply_cache(message:, **)
def update_thread_reply_cache(message:)
message.thread&.increment_replies_count_cache
end
def update_last_message_ids(message:, **)
def update_last_message_ids(message:)
message.thread&.update_last_message_id!
message.chat_channel.update_last_message_id!
end
def publish_events(guardian:, message:, **)
def publish_events(guardian:, message:)
DiscourseEvent.trigger(:chat_message_restored, message, message.chat_channel, guardian.user)
Chat::Publisher.publish_restore!(message.chat_channel, message)

View File

@ -34,27 +34,27 @@ module Chat
private
def clean_term(contract:, **)
def clean_term(contract:)
context.term = contract.term.downcase&.gsub(/^#+/, "")&.gsub(/^@+/, "")&.strip
end
def fetch_memberships(guardian:, **)
def fetch_memberships(guardian:)
::Chat::ChannelMembershipManager.all_for_user(guardian.user)
end
def fetch_users(guardian:, contract:, **)
def fetch_users(guardian:, contract:)
return unless contract.include_users
return unless guardian.can_create_direct_message?
search_users(context, guardian, contract)
end
def fetch_groups(guardian:, contract:, **)
def fetch_groups(guardian:, contract:)
return unless contract.include_groups
return unless guardian.can_create_direct_message?
search_groups(context, guardian, contract)
end
def fetch_category_channels(guardian:, contract:, **)
def fetch_category_channels(guardian:, contract:)
return unless contract.include_category_channels
return if !SiteSetting.enable_public_channels

View File

@ -29,24 +29,24 @@ module Chat
private
def fetch_message(contract:, **)
def fetch_message(contract:)
::Chat::Message.find_by(id: contract.message_id)
end
def can_join_channel(guardian:, message:, **)
def can_join_channel(guardian:, message:)
guardian.can_join_chat_channel?(message.chat_channel)
end
def can_stop_streaming(guardian:, message:, **)
def can_stop_streaming(guardian:, message:)
guardian.is_admin? || message.user.id == guardian.user.id ||
message.in_reply_to && message.in_reply_to.user_id == guardian.user.id
end
def stop_message_streaming(message:, **)
def stop_message_streaming(message:)
message.update!(streaming: false)
end
def publish_message_streaming_state(guardian:, message:, contract:, **)
def publish_message_streaming_state(guardian:, message:, contract:)
::Chat::Publisher.publish_edit!(message.chat_channel, message)
end
end

View File

@ -48,12 +48,12 @@ module Chat
private
def cast_thread_and_channel_ids_to_integer(contract:, **)
def cast_thread_and_channel_ids_to_integer(contract:)
contract.thread_ids = contract.thread_ids.map(&:to_i)
contract.channel_ids = contract.channel_ids.map(&:to_i)
end
def fetch_report(contract:, guardian:, **)
def fetch_report(contract:, guardian:)
::Chat::TrackingStateReportQuery.call(
guardian: guardian,
channel_ids: contract.channel_ids,

View File

@ -28,15 +28,15 @@ module Chat
private
def fetch_channel(channel_id:, **)
def fetch_channel(channel_id:)
Chat::Channel.find_by(id: channel_id)
end
def invalid_access(guardian:, channel:, **)
def invalid_access(guardian:, channel:)
guardian.can_preview_chat_channel?(channel) && guardian.can_delete_chat_channel?
end
def prevents_slug_collision(channel:, **)
def prevents_slug_collision(channel:)
channel.update!(
slug:
"#{Time.current.strftime("%Y%m%d-%H%M")}-#{channel.slug}-deleted".truncate(
@ -46,18 +46,18 @@ module Chat
)
end
def soft_delete_channel(guardian:, channel:, **)
def soft_delete_channel(guardian:, channel:)
channel.trash!(guardian.user)
end
def log_channel_deletion(guardian:, channel:, **)
def log_channel_deletion(guardian:, channel:)
StaffActionLogger.new(guardian.user).log_custom(
DELETE_CHANNEL_LOG_KEY,
{ chat_channel_id: channel.id, chat_channel_name: channel.title(guardian.user) },
)
end
def enqueue_delete_channel_relations_job(channel:, **)
def enqueue_delete_channel_relations_job(channel:)
Jobs.enqueue(Jobs::Chat::ChannelDelete, chat_channel_id: channel.id)
end
end

View File

@ -39,22 +39,22 @@ module Chat
private
def fetch_message(contract:, **)
def fetch_message(contract:)
Chat::Message.includes(chat_channel: :chatable).find_by(
id: contract.message_id,
chat_channel_id: contract.channel_id,
)
end
def invalid_access(guardian:, message:, **)
def invalid_access(guardian:, message:)
guardian.can_delete_chat?(message, message.chat_channel.chatable)
end
def trash_message(message:, guardian:, **)
def trash_message(message:, guardian:)
message.trash!(guardian.user)
end
def destroy_notifications(message:, **)
def destroy_notifications(message:)
Notification.where(
id:
Chat::Mention
@ -64,23 +64,23 @@ module Chat
).destroy_all
end
def update_tracking_state(message:, **)
def update_tracking_state(message:)
::Chat::Action::ResetUserLastReadChannelMessage.call([message.id], [message.chat_channel_id])
if message.thread_id.present?
::Chat::Action::ResetUserLastReadThreadMessage.call([message.id], [message.thread_id])
end
end
def update_thread_reply_cache(message:, **)
def update_thread_reply_cache(message:)
message.thread&.decrement_replies_count_cache
end
def update_last_message_ids(message:, **)
def update_last_message_ids(message:)
message.thread&.update_last_message_id!
message.chat_channel.update_last_message_id!
end
def publish_events(guardian:, message:, **)
def publish_events(guardian:, message:)
DiscourseEvent.trigger(:chat_message_trashed, message, message.chat_channel, guardian.user)
Chat::Publisher.publish_delete!(message.chat_channel, message)

View File

@ -29,11 +29,11 @@ module Chat
private
def fetch_channel(contract:, **)
def fetch_channel(contract:)
Chat::Channel.find_by(id: contract.channel_id)
end
def unfollow(channel:, guardian:, **)
def unfollow(channel:, guardian:)
context.membership = channel.remove(guardian.user)
end
end

View File

@ -59,21 +59,21 @@ module Chat
private
def fetch_channel(channel_id:, **)
def fetch_channel(channel_id:)
Chat::Channel.find_by(id: channel_id)
end
def check_channel_permission(guardian:, channel:, **)
def check_channel_permission(guardian:, channel:)
guardian.can_preview_chat_channel?(channel) && guardian.can_edit_chat_channel?(channel)
end
def update_channel(channel:, contract:, **)
def update_channel(channel:, contract:)
channel.assign_attributes(contract.attributes)
context.threading_enabled_changed = channel.threading_enabled_changed?
channel.save!
end
def mark_all_threads_as_read_if_needed(channel:, **)
def mark_all_threads_as_read_if_needed(channel:)
return if !(context.threading_enabled_changed && channel.threading_enabled)
Jobs.enqueue(Jobs::Chat::MarkAllChannelThreadsRead, channel_id: channel.id)
end
@ -82,11 +82,11 @@ module Chat
SiteSetting.chat_threads_enabled = Chat::Channel.exists?(threading_enabled: true)
end
def publish_channel_update(channel:, guardian:, **)
def publish_channel_update(channel:, guardian:)
Chat::Publisher.publish_chat_channel_edit(channel, guardian.user)
end
def auto_join_users_if_needed(channel:, **)
def auto_join_users_if_needed(channel:)
return unless channel.auto_join_users?
Chat::ChannelMembershipManager.new(channel).enforce_automatic_channel_memberships
end

View File

@ -28,16 +28,16 @@ module Chat
private
def fetch_channel(channel_id:, **)
def fetch_channel(channel_id:)
Chat::Channel.find_by(id: channel_id)
end
def check_channel_permission(guardian:, channel:, status:, **)
def check_channel_permission(guardian:, channel:, status:)
guardian.can_preview_chat_channel?(channel) &&
guardian.can_change_channel_status?(channel, status.to_sym)
end
def change_status(channel:, status:, guardian:, **)
def change_status(channel:, status:, guardian:)
channel.public_send("#{status}!", guardian.user)
end
end

View File

@ -45,11 +45,11 @@ module Chat
private
def enforce_system_membership(guardian:, message:, **)
def enforce_system_membership(guardian:, message:)
message.chat_channel.add(guardian.user) if guardian.user.is_system_user?
end
def fetch_message(contract:, **)
def fetch_message(contract:)
::Chat::Message.includes(
:chat_mentions,
:bookmarks,
@ -67,20 +67,20 @@ module Chat
).find_by(id: contract.message_id)
end
def fetch_uploads(contract:, guardian:, **)
def fetch_uploads(contract:, guardian:)
return if !SiteSetting.chat_allow_uploads
guardian.user.uploads.where(id: contract.upload_ids)
end
def can_modify_channel_message(guardian:, message:, **)
def can_modify_channel_message(guardian:, message:)
guardian.can_modify_channel_message?(message.chat_channel)
end
def can_modify_message(guardian:, message:, **)
def can_modify_message(guardian:, message:)
guardian.can_edit_chat?(message)
end
def modify_message(contract:, message:, guardian:, uploads:, **)
def modify_message(contract:, message:, guardian:, uploads:)
message.message = contract.message
message.last_editor_id = guardian.user.id
message.cook
@ -95,11 +95,11 @@ module Chat
message.upload_ids = new_upload_ids
end
def save_message(message:, **)
def save_message(message:)
message.save!
end
def save_revision(message:, guardian:, **)
def save_revision(message:, guardian:)
return false if message.streaming_before_last_save
prev_message = message.message_before_last_save || message.message_was
@ -135,7 +135,7 @@ module Chat
chars_edited > max_edited_chars
end
def publish(message:, guardian:, contract:, **)
def publish(message:, guardian:, contract:)
edit_timestamp = context.revision&.created_at&.iso8601(6) || Time.zone.now.iso8601(6)
::Chat::Publisher.publish_edit!(message.chat_channel, message)

View File

@ -38,28 +38,28 @@ module Chat
private
def fetch_thread(contract:, **)
def fetch_thread(contract:)
Chat::Thread.find_by(id: contract.thread_id)
end
def can_view_channel(guardian:, thread:, **)
def can_view_channel(guardian:, thread:)
guardian.can_preview_chat_channel?(thread.channel)
end
def can_edit_thread(guardian:, thread:, **)
def can_edit_thread(guardian:, thread:)
guardian.can_edit_thread?(thread)
end
def threading_enabled_for_channel(thread:, **)
def threading_enabled_for_channel(thread:)
thread.channel.threading_enabled
end
def update(thread:, contract:, **)
def update(thread:, contract:)
thread.update(title: contract.title)
fail!(thread.errors.full_messages.join(", ")) if thread.invalid?
end
def publish_metadata(thread:, **)
def publish_metadata(thread:)
Chat::Publisher.publish_thread_original_message_metadata!(thread)
end
end

View File

@ -45,19 +45,19 @@ module Chat
private
def fetch_thread(contract:, **)
def fetch_thread(contract:)
Chat::Thread.find_by(id: contract.thread_id, channel_id: contract.channel_id)
end
def can_view_channel(guardian:, thread:, **)
def can_view_channel(guardian:, thread:)
guardian.can_preview_chat_channel?(thread.channel)
end
def threading_enabled_for_channel(thread:, **)
def threading_enabled_for_channel(thread:)
thread.channel.threading_enabled
end
def create_or_update_membership(thread:, guardian:, contract:, **)
def create_or_update_membership(thread:, guardian:, contract:)
membership = thread.membership_for(guardian.user)
if !membership
membership = thread.add(guardian.user)

View File

@ -37,32 +37,32 @@ module Chat
private
def fetch_channel(contract:, **)
def fetch_channel(contract:)
::Chat::Channel.find_by(id: contract.channel_id)
end
def fetch_active_membership(guardian:, channel:, **)
def fetch_active_membership(guardian:, channel:)
::Chat::ChannelMembershipManager.new(channel).find_for_user(guardian.user, following: true)
end
def invalid_access(guardian:, active_membership:, **)
def invalid_access(guardian:, active_membership:)
guardian.can_join_chat_channel?(active_membership.chat_channel)
end
def fetch_message(channel:, contract:, **)
def fetch_message(channel:, contract:)
::Chat::Message.with_deleted.find_by(chat_channel_id: channel.id, id: contract.message_id)
end
def ensure_message_id_recency(message:, active_membership:, **)
def ensure_message_id_recency(message:, active_membership:)
!active_membership.last_read_message_id ||
message.id >= active_membership.last_read_message_id
end
def update_membership_state(message:, active_membership:, **)
def update_membership_state(message:, active_membership:)
active_membership.update!(last_read_message_id: message.id, last_viewed_at: Time.zone.now)
end
def mark_associated_mentions_as_read(active_membership:, message:, **)
def mark_associated_mentions_as_read(active_membership:, message:)
::Chat::Action::MarkMentionsRead.call(
active_membership.user,
channel_ids: [active_membership.chat_channel.id],
@ -70,7 +70,7 @@ module Chat
)
end
def publish_new_last_read_to_clients(guardian:, channel:, message:, **)
def publish_new_last_read_to_clients(guardian:, channel:, message:)
::Chat::Publisher.publish_user_tracking_state!(guardian.user, channel, message)
end
end

View File

@ -36,22 +36,22 @@ module Chat
private
def fetch_thread(contract:, **)
def fetch_thread(contract:)
::Chat::Thread.find_by(id: contract.thread_id, channel_id: contract.channel_id)
end
def invalid_access(guardian:, thread:, **)
def invalid_access(guardian:, thread:)
guardian.can_join_chat_channel?(thread.channel)
end
# NOTE: In future we will pass in a specific last_read_message_id
# to the service, so this will need to change because currently it's
# just using the thread's last_message_id.
def mark_thread_read(thread:, guardian:, **)
def mark_thread_read(thread:, guardian:)
thread.mark_read_for_user!(guardian.user)
end
def mark_associated_mentions_as_read(thread:, guardian:, **)
def mark_associated_mentions_as_read(thread:, guardian:)
::Chat::Action::MarkMentionsRead.call(
guardian.user,
channel_ids: [thread.channel_id],
@ -59,7 +59,7 @@ module Chat
)
end
def publish_new_last_read_to_clients(guardian:, thread:, **)
def publish_new_last_read_to_clients(guardian:, thread:)
::Chat::Publisher.publish_user_tracking_state!(
guardian.user,
thread.channel,

View File

@ -37,21 +37,21 @@ module Chat
private
def fetch_channel(contract:, **)
def fetch_channel(contract:)
Chat::Channel.find_by(id: contract.channel_id)
end
def can_upsert_draft(guardian:, channel:, **)
def can_upsert_draft(guardian:, channel:)
guardian.can_chat? && guardian.can_join_chat_channel?(channel)
end
def check_thread_exists(contract:, channel:, **)
def check_thread_exists(contract:, channel:)
if contract.thread_id.present?
fail!("Thread not found") if !channel.threads.exists?(id: contract.thread_id)
end
end
def upsert_draft(contract:, guardian:, **)
def upsert_draft(contract:, guardian:)
if contract.data.present?
draft =
Chat::Draft.find_or_initialize_by(

View File

@ -53,27 +53,27 @@ module Service
#
# private
#
# def fetch_channel(channel_id:, **)
# def fetch_channel(channel_id:)
# Chat::Channel.find_by(id: channel_id)
# end
#
# def invalid_access(guardian:, channel:, **)
# def invalid_access(guardian:, channel:)
# guardian.can_preview_chat_channel?(channel) && guardian.can_delete_chat_channel?
# end
#
# def prevents_slug_collision(channel:, **)
# def prevents_slug_collision(channel:)
# …
# end
#
# def soft_delete_channel(guardian:, channel:, **)
# def soft_delete_channel(guardian:, channel:)
# …
# end
#
# def log_channel_deletion(guardian:, channel:, **)
# def log_channel_deletion(guardian:, channel:)
# …
# end
#
# def enqueue_delete_channel_relations_job(channel:, **)
# def enqueue_delete_channel_relations_job(channel:)
# …
# end
# end

View File

@ -112,8 +112,7 @@ module Service
def call(instance, context)
object = class_name&.new(context)
method = object&.method(:call) || instance.method(method_name)
args = {}
args = context.to_h if method.arity.nonzero?
args = context.to_h.slice(*method.parameters.select { _1[0] == :keyreq }.map(&:last))
context[result_key] = Context.build(object: object)
instance.instance_exec(**args, &method)
end
@ -254,7 +253,7 @@ module Service
#
# private
#
# def fetch_channel(channel_id:, **)
# def fetch_channel(channel_id:)
# Chat::Channel.find_by(id: channel_id)
# end
@ -277,7 +276,7 @@ module Service
#
# private
#
# def no_direct_message_channel(channel:, **)
# def no_direct_message_channel(channel:)
# !channel.direct_message_channel?
# end
#
@ -326,7 +325,7 @@ module Service
#
# private
#
# def update_channel(channel:, params_to_edit:, **)
# def update_channel(channel:, params_to_edit:)
# channel.update!(params_to_edit)
# end
# @example using {#fail!} in a step
@ -334,7 +333,7 @@ module Service
#
# private
#
# def save_channel(channel:, **)
# def save_channel(channel:)
# fail!("something went wrong") if !channel.save
# end