Class: Chat

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
lib/generators/kablam/templates/chat.rb

Instance Method Summary collapse

Instance Method Details

#broadcast_selfObject



29
30
31
# File 'lib/generators/kablam/templates/chat.rb', line 29

def broadcast_self
  ActionCable.server.broadcast "ChatChannel_#{self.id}", {chat: messages_array.last }
end

#latest_messageObject



21
22
23
# File 'lib/generators/kablam/templates/chat.rb', line 21

def latest_message
  messages_array.last
end

#messages_arrayObject



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/generators/kablam/templates/chat.rb', line 7

def messages_array
  y = messages.map do |x|
    u = User.find(x.sender_id)
    h = x.serializable_hash
    # h["status"] = x.status(user_id)
    h["chat_id"] = id.to_s
    h["image"] = u.name? ? u.avatar_url(50) : "/assets/default.png"
    h['username'] = u.name || "An Amazing Person~"
    h["user_time"] = x.user_time
    h
  end
  y.sort_by{|x|x["created_at"]}
end

#not_answered(user_id) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/generators/kablam/templates/chat.rb', line 37

def not_answered(user_id)
  if !messages.blank?
    return messages.last.sender_id != user_id
  else
    return false
  end
end

#unread_messages(user_id) ⇒ Object



25
26
27
# File 'lib/generators/kablam/templates/chat.rb', line 25

def unread_messages(user_id)
  message_statuses.where(user_id: user_id.to_i, read: false).count
end

#usersObject



33
34
35
# File 'lib/generators/kablam/templates/chat.rb', line 33

def users
  messages.map{ |m| m.sender_id}.uniq
end