group_interactions.rb 1.06 KB
Newer Older
Rob Colbert's avatar
Rob Colbert committed
1 2 3 4 5 6 7 8 9 10 11 12
# 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)
2458773093's avatar
2458773093 committed
13 14 15 16 17 18
      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
Rob Colbert's avatar
Rob Colbert committed
19 20 21 22 23 24

    private

    def follow_mapping(query, field)
      query.pluck(field).each_with_object({}) { |id, mapping| mapping[id] = true }
    end
2458773093's avatar
2458773093 committed
25 26 27 28

    def unread_count_mapping(query, field)
      query.pluck(:group_id, :unread_count).each_with_object({}) { |e, mapping| mapping[e[0]] = e[1] }
    end
Rob Colbert's avatar
Rob Colbert committed
29 30
  end

2458773093's avatar
2458773093 committed
31 32 33 34 35 36
  def accounts_for_local_distribution
    accounts.local
      .joins(:user)
      .where('users.current_sign_in_at > ?', User::ACTIVE_DURATION.ago)
  end

Rob Colbert's avatar
Rob Colbert committed
37
end