Module: Denshobato::MessageHelper

Included in:
CoreHelper
Defined in:
lib/denshobato/helpers/core_modules/message_helper.rb

Instance Method Summary collapse

Instance Method Details

#send_message(text, recipient) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/denshobato/helpers/core_modules/message_helper.rb', line 3

def send_message(text, recipient)
  # This method sends message directly to the recipient
  # Takes responsibility to create conversation if it doesn`t exist yet
  # sends message to an existing conversation

  # Find conversation.
  room = hato_conversation.find_by(sender: self, recipient: recipient)

  # If conversation doesn`t exist, create one.
  conversation = room.nil? ? create_conversation(self, recipient) : room

  # Return validation error, if conversation is a String (see create_conversation method)
  return errors.add(:blacklist, conversation) if conversation.is_a?(String)

  # Create message for this conversation.
  send_message_to(conversation.id, body: text)
end

#send_message_to(id, params) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/denshobato/helpers/core_modules/message_helper.rb', line 21

def send_message_to(id, params)
  # This method sends message directly to conversation

  return errors.add(:message, 'Conversation not present') unless id

  # Expect record id
  # If id == active record object, get it`s id
  id = id.id if id.is_a?(ActiveRecord::Base)

  room = hato_conversation.find(id)

  # Show validation error if the author of a message is not in conversation
  return message_error(id, self) unless user_in_conversation(room, self)

  # If everything is ok, build message
  hato_messages.build(params)
end