Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
40 lines (34 sloc) 1.1 KB
class Message
def self.add(params)
id = Time.now.to_f.to_s + Digest::SHA1.hexdigest(params[:message])
message = REDIS.hmset id,
:id, id,
:name, params[:name],
:text, params[:message],
:internal, params[:internal] == "1",
:time, Time.now.utc.to_f
REDIS.rpush "m.#{params[:room]}", id
end
def self.clear(room)
REDIS.del "m.#{room}"
end
def self.all(room, staff)
# fetch the last few messages in the room
# todo: fetch messages since the last one we saw locally
# as this will definitely skip if there are a ton
# since keys are "timestamp.uniq" we can get a list
# of keys, maybe, and just fetch the data since then?
# other idea: store the messages a user has/has not seen in
# a separate list and use something like lpop when they have
ids = REDIS.lrange "m.#{room}", -10, -1
ids.map do |id|
hash = REDIS.hgetall id
# don't show internal messages to plebs
if hash['internal'] == "true" && !staff
nil
else
hash
end
end.compact
end
end