Class: CustomerServiceIm::Conversation

Inherits:
ApplicationRecord show all
Defined in:
app/models/customer_service_im/conversation.rb

Instance Method Summary collapse

Instance Method Details

#all_messages_read!Object



25
26
27
28
29
# File 'app/models/customer_service_im/conversation.rb', line 25

def all_messages_read!
  messages.unread.includes(:user).update(state: :read)
  update(staff_unread_message_count: 0)
  broadcast_to_channel
end

#broadcast_to_channelObject



31
32
33
34
# File 'app/models/customer_service_im/conversation.rb', line 31

def broadcast_to_channel
  conversation_data = JSON.parse(ApplicationController.render partial: "customer_service_im/conversations/conversation", locals: { conversation: self })
  ActionCable.server.broadcast "customer_service_im/conversations_channel", conversation_data
end

#increment_unread_message_count(message) ⇒ Object

Parameters:



37
38
39
40
41
42
43
44
45
# File 'app/models/customer_service_im/conversation.rb', line 37

def increment_unread_message_count(message)
  if message.from_user?
    increment(:staff_unread_message_count)
  elsif message.from_staff?
    increment(:user_unread_message_count)
  else
    # never arrived
  end
end

#last_messageObject

validates_presence_of :title



11
12
13
# File 'app/models/customer_service_im/conversation.rb', line 11

def last_message
  messages.last
end

#message_created!(message) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'app/models/customer_service_im/conversation.rb', line 15

def message_created!(message)
  with_lock do
    increment_unread_message_count(message)
    update_last_messaged_on(message)
    broadcast_to_channel

    save!
  end
end

#update_last_messaged_on(message) ⇒ Object



47
48
49
# File 'app/models/customer_service_im/conversation.rb', line 47

def update_last_messaged_on(message)
  self.last_messaged_on = message.created_at
end