Files
3fae800936 feat: base layer for unread counts (store, counter and builder) (1/3)[CW-6851] (#14368)
## Description

This is the first PR in a series of PRs for Introducing unread counts in
the sidebar for inboxes and labels.

In this PR:

* Added the unread store, counter and builder modules
* Added redis keys for unread count management
* Added specs for all 3 modules, some specs are for testing enterprise
only feature like specific roles and permissions which are added in the
respective enterprise folder itself.

**Note**
None of this changes affect anything else and nothing is wired to
existing modules.

Issue:
https://linear.app/chatwoot/issue/CW-6851/support-unread-conversation-counts

## Type of change

Please delete options that are not relevant.

- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality not to work as expected)
- [ ] This change requires a documentation update

## How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. Please also list any relevant details
for your test configuration.


## Checklist:

- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my code
- [ ] I have commented on my code, particularly in hard-to-understand
areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged and published in downstream
modules

---------

Co-authored-by: Sojan Jose <sojan@pepalo.com>
2026-05-20 14:26:21 +05:30

76 lines
1.9 KiB
Ruby

class Conversations::UnreadCounts::Builder
BATCH_SIZE = 1000
attr_reader :account
def initialize(account)
@account = account
end
def build_base!
store.clear_account!(account.id)
write_memberships(assignment: false)
store.mark_base_ready!(account.id)
end
def build_assignment!
store.clear_assignment!(account.id)
write_memberships(assignment: true)
store.mark_assignment_ready!(account.id)
end
def build_all!
build_base!
build_assignment!
end
private
def write_memberships(assignment:)
unread_conversations.in_batches(of: BATCH_SIZE) do |relation|
columns = %i[id inbox_id assignee_id cached_label_list team_id]
memberships = relation.pluck(*columns).map do |id, inbox_id, assignee_id, cached_label_list, team_id|
{
conversation_id: id,
inbox_id: inbox_id,
assignee_id: assignee_id,
team_id: team_id,
label_ids: label_ids_for(cached_label_list)
}
end
store.add_memberships(account_id: account.id, memberships: memberships, assignment: assignment)
end
end
def unread_conversations
account.conversations
.open
.joins(:messages)
.merge(Message.incoming.reorder(nil))
.where(messages: { account_id: account.id })
.where(unread_since_last_seen_condition)
.distinct
end
def unread_since_last_seen_condition
conversations = Conversation.arel_table
messages = Message.arel_table
conversations[:agent_last_seen_at].eq(nil).or(messages[:created_at].gt(conversations[:agent_last_seen_at]))
end
def label_ids_for(cached_label_list)
label_titles = cached_label_list.to_s.split(',').map(&:strip).compact_blank
labels_by_title.values_at(*label_titles).compact
end
def labels_by_title
@labels_by_title ||= account.labels.pluck(:title, :id).to_h
end
def store
::Conversations::UnreadCounts::Store
end
end