forked from chatwoot/chatwoot
-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathmessage_receipt_update.rb
More file actions
40 lines (30 loc) · 1.08 KB
/
message_receipt_update.rb
File metadata and controls
40 lines (30 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
module Whatsapp::BaileysHandlers::MessageReceiptUpdate
include Whatsapp::BaileysHandlers::Helpers
private
def process_message_receipt_update
receipts = processed_params[:data]
receipts.each do |receipt|
@message = nil
@raw_message = receipt
next handle_receipt_update if incoming?
# NOTE: Shared lock with Whatsapp::SendOnWhatsappService
# Avoids race conditions when sending messages.
with_baileys_channel_lock_on_outgoing_message(inbox.channel.id) { handle_receipt_update }
end
end
def handle_receipt_update
return unless find_message_by_source_id(raw_message_id)
new_status = receipt_status
return if new_status.nil?
return unless receipt_status_transition_allowed?(new_status)
@message.update!(status: new_status)
end
def receipt_status
'delivered' if @raw_message.dig(:receipt, :receiptTimestamp).present?
end
def receipt_status_transition_allowed?(new_status)
return false if @message.status == 'read'
return false if @message.status == 'delivered' && new_status == 'delivered'
true
end
end