2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
module Users
|
|
|
|
module ParticipableService
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
attr_reader :noteable
|
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
private
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
def noteable_owner
|
|
|
|
return [] unless noteable && noteable.author.present?
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
[noteable.author].tap do |users|
|
|
|
|
preload_status(users)
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def participants_in_noteable
|
|
|
|
return [] unless noteable
|
|
|
|
|
|
|
|
users = noteable.participants(current_user)
|
|
|
|
sorted(users)
|
|
|
|
end
|
|
|
|
|
|
|
|
def sorted(users)
|
2021-02-22 17:27:13 +05:30
|
|
|
users.uniq.to_a.compact.sort_by(&:username).tap do |users|
|
|
|
|
preload_status(users)
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def groups
|
2021-02-22 17:27:13 +05:30
|
|
|
current_user.authorized_groups.with_route.sort_by(&:path)
|
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
def render_participants_as_hash(participants)
|
|
|
|
participants.map(&method(:participant_as_hash))
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
def participant_as_hash(participant)
|
|
|
|
case participant
|
|
|
|
when Group
|
|
|
|
group_as_hash(participant)
|
|
|
|
when User
|
|
|
|
user_as_hash(participant)
|
|
|
|
else
|
|
|
|
participant
|
|
|
|
end
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
def user_as_hash(user)
|
|
|
|
{
|
|
|
|
type: user.class.name,
|
|
|
|
username: user.username,
|
|
|
|
name: user.name,
|
2021-01-29 00:20:46 +05:30
|
|
|
avatar_url: user.avatar_url,
|
2021-02-22 17:27:13 +05:30
|
|
|
availability: lazy_user_availability(user).itself # calling #itself to avoid returning a BatchLoader instance
|
2019-07-07 11:18:12 +05:30
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
def group_as_hash(group)
|
2019-07-07 11:18:12 +05:30
|
|
|
{
|
|
|
|
type: group.class.name,
|
|
|
|
username: group.full_path,
|
|
|
|
name: group.full_name,
|
|
|
|
avatar_url: group.avatar_url,
|
2020-01-01 13:55:28 +05:30
|
|
|
count: group_counts.fetch(group.id, 0),
|
|
|
|
mentionsDisabled: group.mentions_disabled
|
2019-07-07 11:18:12 +05:30
|
|
|
}
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
2021-02-22 17:27:13 +05:30
|
|
|
|
|
|
|
def group_counts
|
|
|
|
@group_counts ||= GroupMember
|
|
|
|
.of_groups(current_user.authorized_groups)
|
|
|
|
.non_request
|
|
|
|
.count_users_by_group_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def preload_status(users)
|
|
|
|
users.each { |u| lazy_user_availability(u) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def lazy_user_availability(user)
|
|
|
|
BatchLoader.for(user.id).batch do |user_ids, loader|
|
|
|
|
user_ids.each_slice(1_000) do |sliced_user_ids|
|
|
|
|
UserStatus
|
|
|
|
.select(:user_id, :availability)
|
|
|
|
.primary_key_in(sliced_user_ids)
|
|
|
|
.each { |status| loader.call(status.user_id, status.availability) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
end
|