class Denshobato::Conversation

Public Instance Methods

from_trash() click to toggle source
# File lib/denshobato/models/conversation.rb, line 44
def from_trash
  # Move conversation from trash

  to_trash { false }
end
messages() click to toggle source

Methods

# File lib/denshobato/models/conversation.rb, line 30
def messages
  # Return all messages of conversation

  ids = notifications.pluck(:message_id)
  hato_message.where(id: ids)
end
to_trash() { |: true| ... } click to toggle source
# File lib/denshobato/models/conversation.rb, line 37
def to_trash
  # Move conversation to trash

  bool = block_given? ? yield : true
  update(trashed: bool)
end

Private Instance Methods

blocked_user() click to toggle source
# File lib/denshobato/models/conversation.rb, line 89
def blocked_user
  if hato_blacklist.where(blocker: recipient, blocked: sender).present?
    errors.add(:blacklist, 'You`re in blacklist')
  end

  if hato_blacklist.where(blocker: sender, blocked: recipient).present?
    errors.add(:blacklist, 'Remove user from blacklist, to start conversation')
  end
end
both_conversation_removed?() click to toggle source
# File lib/denshobato/models/conversation.rb, line 83
def both_conversation_removed?
  # Check when both conversations are removed

  hato_conversation.where(sender: recipient, recipient: sender).empty?
end
check_sender() click to toggle source
# File lib/denshobato/models/conversation.rb, line 63
def check_sender
  errors.add(:conversation, 'You can`t create conversation with yourself') if sender == recipient
end
columns() click to toggle source
# File lib/denshobato/models/conversation.rb, line 99
def columns
  [['sender_id', sender_id], ['sender_type', sender_type], ['recipient_id', recipient_id], ['recipient_type', recipient_type]]
end
conversation_uniqueness() click to toggle source
# File lib/denshobato/models/conversation.rb, line 67
def conversation_uniqueness
  # Check conversation for uniqueness, when recipient is sender, and vice versa.

  hash = Hash[*columns.flatten] # => { sender_id: 1, sender_type: 'User' ... }

  errors.add(:conversation, 'You already have conversation with this user.') if hato_conversation.where(hash).present?
end
recipient_conversation() click to toggle source
# File lib/denshobato/models/conversation.rb, line 55
def recipient_conversation
  if hato_conversation.where(recipient: sender, sender: recipient).present?
    errors.add(:conversation, 'You already have conversation with this user')
  else
    recipient.make_conversation_with(sender).save
  end
end
remove_messages() click to toggle source
# File lib/denshobato/models/conversation.rb, line 75
def remove_messages
  # When sender and recipient remove their conversation together
  # remove all messages and notifications belonging to this conversation

  hato_message.where(id: messages.map(&:id)).destroy_all
  notifications.destroy_all
end