debian-mirror-gitlab/app/services/concerns/users/participable_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

99 lines
2.3 KiB
Ruby
Raw Normal View History

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
2022-11-25 23:54:43 +05:30
return [] unless current_user
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)
2023-04-23 21:23:45 +05:30
participants.map { |participant| participant_as_hash(participant) }
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