Class: Chatrix::Rooms

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

Overview

Manages the rooms known to the client.

Instance Method Summary collapse

Constructor Details

#initialize(users, matrix) ⇒ Rooms

Initializes a new Rooms instance.

Parameters:

  • users (Users)

    The User manager.

  • matrix (Matrix)

    The Matrix API instance.



17
18
19
20
21
22
23
24
# File 'lib/chatrix/rooms.rb', line 17

def initialize(users, matrix)
  @matrix = matrix

  @users = users

  # room_id => room
  @rooms = {}
end

Instance Method Details

#[](id) ⇒ Room?

Gets a room by its ID, alias, or name.

Returns:

  • (Room, nil)

    Returns the room instance if the room was found, otherwise nil.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/chatrix/rooms.rb', line 30

def [](id)
  return @rooms[id] if id.start_with? '!'

  if id.start_with? '#'
    res = @rooms.find { |_, r| r.canonical_alias == id }
    return res.last if res.respond_to? :last
  end

  res = @rooms.find { |_, r| r.name == id }
  res.last if res.respond_to? :last
end

#get_room(id) ⇒ Room (private)

Gets the Room instance associated with a room ID. If there is no Room instance for the ID, one is created and returned.

Parameters:

  • id (String)

    The room ID to get an instance for.

Returns:

  • (Room)

    An instance of the Room class for the specified ID.



67
68
69
70
71
72
73
# File 'lib/chatrix/rooms.rb', line 67

def get_room(id)
  return @rooms[id] if @rooms.key? id
  room = Room.new id, @users, @matrix
  @rooms[id] = room
  broadcast(:added, room)
  room
end

#join(id) ⇒ Room

Attempts to join the specified room.

Parameters:

  • id (String)

    The room ID to join.

Returns:

  • (Room)

    The Room instance for the joined room.

Raises:

  • (ForbiddenError)

    Raised if the user does not have sufficient permissions to join the room.



47
48
49
# File 'lib/chatrix/rooms.rb', line 47

def join(id)
  get_room(id).tap(&:join)
end

#process_events(events) ⇒ Object

Processes a list of room events from syncs.

Parameters:

  • events (Hash)

    A hash of room events as returned from the server.



54
55
56
57
58
# File 'lib/chatrix/rooms.rb', line 54

def process_events(events)
  process_join events['join'] if events.key? 'join'
  process_invite events['invite'] if events.key? 'invite'
  process_leave events['leave'] if events.key? 'leave'
end

#process_invite(events) ⇒ Object (private)

Process invite room events.

Parameters:

  • events (Hash{String=>Hash})

    Events to process.



87
88
89
90
91
# File 'lib/chatrix/rooms.rb', line 87

def process_invite(events)
  events.each do |room, data|
    get_room(room).process_invite data
  end
end

#process_join(events) ⇒ Object (private)

Process join room events.

Parameters:

  • events (Hash{String=>Hash})

    Events to process.



78
79
80
81
82
# File 'lib/chatrix/rooms.rb', line 78

def process_join(events)
  events.each do |room, data|
    get_room(room).process_join data
  end
end

#process_leave(events) ⇒ Object (private)

Process leave room events.

Parameters:

  • events (Hash{String=>Hash})

    Events to process.



96
97
98
99
100
# File 'lib/chatrix/rooms.rb', line 96

def process_leave(events)
  events.each do |room, data|
    get_room(room).process_leave data
  end
end