class Fdchat

Constants

VERSION

Attributes

sala[RW]

Public Class Methods

new(id: nil, owner: nil,owner_id: nil) click to toggle source
# File lib/fdchat.rb, line 9
def initialize(id: nil, owner: nil,owner_id: nil)
  if owner_id
    @sala = ChatSala.find_by(
        owner_id: BSON::ObjectId.from_string(owner_id)
    )
  else
  @sala = ChatSala.find_by(:$or => [
    { _id: id},
    { owner: owner}
  ])
  end
  unless @sala
   @sala = ChatSala.create!( owner: owner)
   if @sala
    add_participant_sala(owner)
   end
  end
end

Public Instance Methods

add_participant_sala(participant) click to toggle source
# File lib/fdchat.rb, line 65
def add_participant_sala(participant)
  @sala.chat_sala_alloweds
      .find_or_create_by!(participant: participant)
end
all_chats() click to toggle source
# File lib/fdchat.rb, line 27
def all_chats
  @sala.chats.order_by(:created_at => 'desc')
end
check_participants() click to toggle source
# File lib/fdchat.rb, line 62
def check_participants
  @sala.chat_sala_alloweds.only(:participant_id, :participant_type) if @sala.chat_sala_alloweds
end
close(participant_id) click to toggle source
# File lib/fdchat.rb, line 43
def close(participant_id)
  if has_participant(participant_id)
    @sala.owner.update!(closed: true)
    @sala.update!(closed: true)
    true
  else
    false
  end
end
create_mensaje(sender:nil,text:nil) click to toggle source
# File lib/fdchat.rb, line 30
def create_mensaje(sender:nil,text:nil)
  mensaje = @sala.chats.create!(
        sender:sender,
        text:text
    )
  self.add_participant_sala(sender)
  mensaje
end
has_participant(participant_id) click to toggle source
# File lib/fdchat.rb, line 52
def has_participant(participant_id)
  rtn = false
  check_participants.each do |part|
   if  part.participant_id.to_s == participant_id.to_s
     rtn =  true
     break
   end
  end
  rtn
end
read(chat_id) click to toggle source
# File lib/fdchat.rb, line 38
def read(chat_id)
  mensaje = @sala.chats.find(chat_id)
  mensaje.update!(read: true)
  mensaje
end
remove_participant_sala(participant) click to toggle source
# File lib/fdchat.rb, line 69
def remove_participant_sala(participant)
  @sala.chat_sala_alloweds.find_by(participant: participant)&.delete
end