Commit c5c25852 authored by 2458773093's avatar 2458773093

group unread count

parent a6f09dc0
......@@ -10,14 +10,22 @@ module GroupInteractions
end
def admin_map(target_group_ids, account_id)
follow_mapping(GroupAccount.where(group_id: target_group_ids, account_id: account_id, role: :admin), :group_id)
end
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
private
def follow_mapping(query, field)
query.pluck(field).each_with_object({}) { |id, mapping| mapping[id] = true }
end
def unread_count_mapping(query, field)
query.pluck(:group_id, :unread_count).each_with_object({}) { |e, mapping| mapping[e[0]] = e[1] }
end
end
end
# frozen_string_literal: true
class GroupRelationshipsPresenter
attr_reader :member, :admin
attr_reader :member, :admin, :unread_count
def initialize(group_ids, current_account_id, **options)
@group_ids = group_ids.map { |a| a.is_a?(Group) ? a.id : a }
@current_account_id = current_account_id
@member = cached[:member].merge(Group.member_map(@uncached_group_ids, @current_account_id))
@admin = cached[:admin].merge(Group.admin_map(@uncached_group_ids, @current_account_id))
@member = cached[:member].merge(Group.member_map(@uncached_group_ids, @current_account_id))
@admin = cached[:admin].merge(Group.admin_map(@uncached_group_ids, @current_account_id))
@unread_count = cached[:unread_count].merge(Group.unread_count_map(@uncached_group_ids, @current_account_id))
cache_uncached!
@member.merge!(options[:member_map] || {})
@admin.merge!(options[:admin_map] || {})
@unread_count.merge!(options[:unread_count_map] || {})
end
private
......@@ -24,6 +26,7 @@ class GroupRelationshipsPresenter
@cached = {
member: {},
admin: {},
unread_count: {},
}
@uncached_group_ids = []
......@@ -44,8 +47,9 @@ class GroupRelationshipsPresenter
def cache_uncached!
@uncached_group_ids.each do |group_id|
maps_for_account = {
member: { group_id => member[group_id] },
admin: { group_id => admin[group_id] },
member: { group_id => member[group_id] },
admin: { group_id => admin[group_id] },
unread_count: { group_id => unread_count[group_id] },
}
Rails.cache.write("relationship:#{@current_account_id}:group#{group_id}", maps_for_account, expires_in: 1.day)
......
# frozen_string_literal: true
class REST::GroupRelationshipSerializer < ActiveModel::Serializer
attributes :id, :member, :admin
attributes :id, :member, :admin, :unread_count
def id
object.id.to_s
......@@ -14,4 +14,8 @@ class REST::GroupRelationshipSerializer < ActiveModel::Serializer
def admin
instance_options[:relationships].admin[object.id] ? true : false
end
def unread_count
instance_options[:relationships].unread_count[object.id] || 0
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment