Class: Chatrix::Components::Timeline

Inherits:
Object
  • Object
show all
Includes:
Wisper::Publisher
Defined in:
lib/chatrix/components/timeline.rb

Overview

Manages the timeline for a room.

Instance Method Summary collapse

Constructor Details

#initialize(room, users) ⇒ Timeline

Initializes a new Timeline instance.

Parameters:

  • room (Room)

    The room this timeline belongs to.

  • users (Users)

    The user manager.



19
20
21
22
# File 'lib/chatrix/components/timeline.rb', line 19

def initialize(room, users)
  @room = room
  @users = users
end

Instance Method Details

#handle_message(event) ⇒ Object (private)

Process a message event.

Parameters:

  • event (Hash)

    Event data.



46
47
48
49
50
51
52
53
# 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) ⇒ Object (private)

Processes a timeline event.

Parameters:

  • event (Hash)

    Event data.



38
39
40
41
42
# 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

#update(data) ⇒ Object

Process timeline events.

Parameters:

  • data (Hash)

    Events to process.



26
27
28
29
30
31
32
# 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