class Chatrix::Components::Timeline

Manages the timeline for a room.

Public Class Methods

new(room, users) click to toggle source

Initializes a new Timeline instance.

@param room [Room] The room this timeline belongs to. @param users [Users] The user manager.

# File lib/chatrix/components/timeline.rb, line 19
def initialize(room, users)
  @room = room
  @users = users
end

Public Instance Methods

update(data) click to toggle source

Process timeline events. @param data [Hash] Events to process.

# File lib/chatrix/components/timeline.rb, line 26
def update(data)
  data['events'].each { |e| process_event e } if data.key? 'events'

  # Pass the event data to state to handle any state updates
  # in the timeline
  @room.state.update data
end

Private Instance Methods

handle_message(event) click to toggle source

Process a message event. @param event [Hash] Event data.

# File lib/chatrix/components/timeline.rb, line 46
def handle_message(event)
  sender = @users[event['sender']]
  timestamp = event['origin_server_ts'] || Time.now.to_i
  content = event['content']
  message = Message.new sender, timestamp, content
  broadcast(:message, @room, message)
  Events.processed event
end
process_event(event) click to toggle source

Processes a timeline event. @param event [Hash] Event data.

# File lib/chatrix/components/timeline.rb, line 38
def process_event(event)
  return if Events.processed? event
  name = 'handle_' + event['type'].match(/\w+$/).to_s
  send(name, event) if respond_to? name, true
end