2019-07-02 08:10:25 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module GroupInteractions
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
class_methods do
|
|
|
|
|
|
|
|
def member_map(target_group_ids, account_id)
|
|
|
|
follow_mapping(GroupAccount.where(group_id: target_group_ids, account_id: account_id), :group_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def admin_map(target_group_ids, account_id)
|
2019-07-20 00:04:08 +01:00
|
|
|
follow_mapping(GroupAccount.where(group_id: target_group_ids, account_id: account_id, role: :admin), :group_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def unread_count_map(target_group_ids, account_id)
|
|
|
|
unread_count_mapping(GroupAccount.where(group_id: target_group_ids, account_id: account_id), :unread_count)
|
|
|
|
end
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def follow_mapping(query, field)
|
|
|
|
query.pluck(field).each_with_object({}) { |id, mapping| mapping[id] = true }
|
|
|
|
end
|
2019-07-20 00:04:08 +01:00
|
|
|
|
|
|
|
def unread_count_mapping(query, field)
|
|
|
|
query.pluck(:group_id, :unread_count).each_with_object({}) { |e, mapping| mapping[e[0]] = e[1] }
|
|
|
|
end
|
2019-07-02 08:10:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|