class Denshobato::Message

Attributes

conversation_id[RW]

Public Instance Methods

message_time() click to toggle source
# File lib/denshobato/models/message.rb, line 30
def message_time
  # Formatted time for chat panel

  created_at.strftime('%a %b %d | %I:%M %p')
end
send_notification(id) click to toggle source

Methods

# File lib/denshobato/models/message.rb, line 22
def send_notification(id)
  # Find current conversation
  conversation = hato_conversation.find(id)

  # Create Notifications
  create_notifications_for(conversation)
end

Private Instance Methods

access_to_posting_message() click to toggle source
# File lib/denshobato/models/message.rb, line 45
def access_to_posting_message
  return unless conversation_id

  room = hato_conversation.find(conversation_id)

  # If author of message is not present in conversation, show error

  errors.add(:message, 'You can`t post to this conversation') unless user_in_conversation(room, author)
end
create_conversation_for_recipient(sender, recipient) click to toggle source
# File lib/denshobato/models/message.rb, line 76
def create_conversation_for_recipient(sender, recipient)
  # Create Conversation for recipient
  # Skip callbacks, because conversation for sender exists already

  conv = hato_conversation.new(sender: recipient, recipient: sender)
  hato_conversation.skip_callback(:create, :after, :recipient_conversation)
  conv.save
  conv
end
create_notifications_for(conversation) click to toggle source
# File lib/denshobato/models/message.rb, line 55
def create_notifications_for(conversation)
  # Take sender and recipient
  sender         = conversation.sender
  recipient      = conversation.recipient

  # Find conversation where sender it's recipient
  conversation_2 = recipient.find_conversation_with(sender)

  # If recipient deletes conversation, create it for him
  conversation_2 = create_conversation_for_recipient(sender, recipient) if conversation_2.nil?

  # Send notifications for new messages to sender and recipient
  [conversation.id, conversation_2.id].each { |id| notifications.create(conversation_id: id) }
end
hato_conversation() click to toggle source
# File lib/denshobato/models/message.rb, line 92
def hato_conversation
  Denshobato::Conversation
end
message_belongs_to_conversation?() click to toggle source
# File lib/denshobato/models/message.rb, line 86
def message_belongs_to_conversation?
  # Check if message has live notifications for any conversation

  hato_conversation.where(id: notifications.pluck(:conversation_id)).present?
end
skip_deleting_messages() click to toggle source
# File lib/denshobato/models/message.rb, line 38
def skip_deleting_messages
  errors.add(:base, 'Can`t delete message, as long as it belongs to the conversation')

  # The before_destroy callback needs a true/false value to determine whether or not to proceeed
  false
end
user_in_conversation(room, author) click to toggle source
# File lib/denshobato/models/message.rb, line 70
def user_in_conversation(room, author)
  # Check if user is already in conversation

  hato_conversation.where(id: room.id, sender: author).present? || hato_conversation.where(id: room.id, recipient: author).present?
end