2020-04-20 14:03:57 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module User::InboxMethods
|
|
|
|
include CursorPaginatable
|
|
|
|
|
|
|
|
define_cursor_paginator :cursored_inbox, :ordered_inbox
|
|
|
|
|
2023-01-28 12:17:08 -08:00
|
|
|
# @return [ActiveRecord::Relation<Inbox>] the user's inbox entries
|
2020-04-20 14:03:57 -07:00
|
|
|
def ordered_inbox
|
|
|
|
inboxes
|
2023-01-28 12:17:08 -08:00
|
|
|
.includes(:question, user: :profile)
|
2020-04-20 14:03:57 -07:00
|
|
|
.order(:created_at)
|
|
|
|
.reverse_order
|
|
|
|
end
|
2023-02-25 06:46:11 -08:00
|
|
|
|
|
|
|
def unread_inbox_count
|
2024-01-25 12:15:04 -08:00
|
|
|
Rails.cache.fetch(inbox_cache_key, expires_in: 12.hours) do
|
2024-01-27 04:02:58 -08:00
|
|
|
count = InboxEntry.where(new: true, user_id: id).count(:id)
|
2023-02-25 06:46:11 -08:00
|
|
|
|
2023-02-27 09:45:49 -08:00
|
|
|
# Returning +nil+ here in order to not display a counter
|
|
|
|
# at all when there isn't anything in the user's inbox
|
|
|
|
return nil unless count.positive?
|
|
|
|
|
|
|
|
count
|
2023-02-25 06:46:11 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-27 09:45:49 -08:00
|
|
|
def inbox_cache_key = "#{cache_key}/unread_inbox_count-#{inbox_updated_at}"
|
2020-04-20 14:03:57 -07:00
|
|
|
end
|