Class: CustomerServiceIm::Message

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

Instance Method Summary collapse

Instance Method Details

#attachment_urlObject



66
67
68
69
70
# File 'app/models/customer_service_im/message.rb', line 66

def attachment_url
  return if !attachment.blob

  Rails.application.routes.url_helpers.rails_blob_url(attachment)
end

#broadcast_to_conversationObject



52
53
54
55
56
57
58
59
# File 'app/models/customer_service_im/message.rb', line 52

def broadcast_to_conversation
  message_data = JSON.parse(ApplicationController.render partial: "customer_service_im/messages/message", locals: { message: self})
  CustomerServiceIm::MessagesChannel.broadcast_to(conversation, message_data)

  if target_receivable?
    sent!
  end
end

#from_staff?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'app/models/customer_service_im/message.rb', line 44

def from_staff?
  receiver_type === 'User'
end

#from_user?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'app/models/customer_service_im/message.rb', line 40

def from_user?
  receiver_type === 'Staff'
end

#read!Object



61
62
63
64
# File 'app/models/customer_service_im/message.rb', line 61

def read!
  conversation.message_read!(self)
  super
end

#speech_recognizeObject



76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/models/customer_service_im/message.rb', line 76

def speech_recognize
  result = TxAsr::SentenceRecognition.from(attachment_url)

  if result.success?
    update( content: result.data["Result"].presence || "[空白语音]" )
    broadcast_to_conversation
  else
    logger = Logger.new Rails.root.join("log/speech_recognition.log")
    logger.error result.errors
    raise CustomerServiceIm::AsrRecognizeError.new(result.errors)
  end
end

#speech_unrecognized?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'app/models/customer_service_im/message.rb', line 72

def speech_unrecognized?
  attachment.blob.present? && content.blank?
end

#target_receivable?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'app/models/customer_service_im/message.rb', line 48

def target_receivable?
  (from_user? && conversation.staff&.online?) || (from_staff? && user&.online?)
end