## Description AutoAssignment::RateLimiter#within_limit? returned true early for inboxes without an AssignmentPolicy, bypassing fair distribution entirely and allowing unlimited conversation assignment. ## Type of change - [ ] Bug fix (non-breaking change which fixes an issue) ## Script - Script to enable users with V2 assignment: https://www.notion.so/chatwoot/Script-to-migrate-account-to-assignment-V2-30ca5f274c9280f5b8ecfd15e28eeb9c?source=copy_link ## Checklist: - [ ] My code follows the style guidelines of this project - [ ] 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 - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules --------- Co-authored-by: Shivam Mishra <scm.mymail@gmail.com>
40 lines
963 B
Ruby
40 lines
963 B
Ruby
class AutoAssignment::RateLimiter
|
|
pattr_initialize [:inbox!, :agent!]
|
|
|
|
def within_limit?
|
|
current_count < limit
|
|
end
|
|
|
|
def track_assignment(conversation)
|
|
assignment_key = build_assignment_key(conversation.id)
|
|
Redis::Alfred.set(assignment_key, conversation.id.to_s, ex: window)
|
|
end
|
|
|
|
def current_count
|
|
pattern = assignment_key_pattern
|
|
Redis::Alfred.keys_count(pattern)
|
|
end
|
|
|
|
private
|
|
|
|
def limit
|
|
config&.fair_distribution_limit.present? ? config.fair_distribution_limit.to_i : 5
|
|
end
|
|
|
|
def window
|
|
config&.fair_distribution_window&.to_i || 5.minutes.to_i
|
|
end
|
|
|
|
def config
|
|
@config ||= inbox.assignment_policy
|
|
end
|
|
|
|
def assignment_key_pattern
|
|
format(Redis::RedisKeys::ASSIGNMENT_KEY_PATTERN, inbox_id: inbox.id, agent_id: agent.id)
|
|
end
|
|
|
|
def build_assignment_key(conversation_id)
|
|
format(Redis::RedisKeys::ASSIGNMENT_KEY, inbox_id: inbox.id, agent_id: agent.id, conversation_id: conversation_id)
|
|
end
|
|
end
|