group unread count

This commit is contained in:
2458773093 2019-07-20 02:04:08 +03:00
parent a6f09dc0c7
commit c5c2585239
3 changed files with 24 additions and 8 deletions

View File

@ -13,11 +13,19 @@ module GroupInteractions
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

View File

@ -1,7 +1,7 @@
# 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 }
@ -9,11 +9,13 @@ class GroupRelationshipsPresenter
@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 = []
@ -46,6 +49,7 @@ class GroupRelationshipsPresenter
maps_for_account = {
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)

View File

@ -1,7 +1,7 @@
# 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