Class: Chatrix::Room

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Wisper::Publisher
Defined in:
lib/chatrix/room.rb

Overview

Provides functionality for interacting with a room.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, users, matrix) ⇒ Room

Initializes a new Room instance.

Parameters:

  • id (String)

    The room ID.

  • users (Users)

    The User manager.

  • matrix (Matrix)

    The Matrix API instance.



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/chatrix/room.rb', line 47

def initialize(id, users, matrix)
  @id = id
  @users = users
  @matrix = matrix

  @state = Components::State.new self, @users
  @timeline = Components::Timeline.new self, @users
  @messaging = Components::Messaging.new self, @matrix
  @admin = Components::Admin.new self, @matrix

  @timeline.on(:message) { |r, m| broadcast(:message, r, m) }
end

Instance Attribute Details

#adminAdmin (readonly)

Returns Administration object for carrying out administrative actions like kicking and banning of users.

Returns:

  • (Admin)

    Administration object for carrying out administrative actions like kicking and banning of users.



28
29
30
# File 'lib/chatrix/room.rb', line 28

def admin
  @admin
end

#idString (readonly)

Returns The ID of this room.

Returns:

  • (String)

    The ID of this room.



18
19
20
# File 'lib/chatrix/room.rb', line 18

def id
  @id
end

#messagingMessaging (readonly)

Returns Handle various message actions through this object.

Returns:

  • (Messaging)

    Handle various message actions through this object.



31
32
33
# File 'lib/chatrix/room.rb', line 31

def messaging
  @messaging
end

#stateState (readonly)

Returns The state object for this room.

Returns:

  • (State)

    The state object for this room.



21
22
23
# File 'lib/chatrix/room.rb', line 21

def state
  @state
end

#timelineTimeline (readonly)

Returns The timeline object for this room.

Returns:

  • (Timeline)

    The timeline object for this room.



24
25
26
# File 'lib/chatrix/room.rb', line 24

def timeline
  @timeline
end

Instance Method Details

#process_invite(data) ⇒ Object

Process invite events for this room.

Parameters:

  • data (Hash)

    Event data containing special invite data.



69
70
71
# File 'lib/chatrix/room.rb', line 69

def process_invite(data)
  data['invite_state']['events'].each { |e| process_invite_event e }
end

#process_invite_event(event) ⇒ Object (private)

Process an invite event for this room.

Parameters:

  • event (Hash)

    Event data.



93
94
95
96
97
98
99
100
101
102
# File 'lib/chatrix/room.rb', line 93

def process_invite_event(event)
  return unless event['type'] == 'm.room.member'
  return unless event['content']['membership'] == 'invite'
  @users.process_invite self, event
  sender = @users[event['sender']]
  invitee = @users[event['state_key']]
  # Return early if the user is already in the room
  return if @state.member? invitee
  broadcast(:invited, sender, invitee)
end

#process_join(data) ⇒ Object

Process join events for this room.

Parameters:

  • data (Hash)

    Event data containing state and timeline events.



62
63
64
65
# File 'lib/chatrix/room.rb', line 62

def process_join(data)
  @state.update data['state'] if data.key? 'state'
  @timeline.update data['timeline'] if data.key? 'timeline'
end

#process_leave(data) ⇒ Object

Process leave events for this room.

Parameters:

  • data (Hash)

    Event data containing state and timeline events up until the point of leaving the room.



76
77
78
79
# File 'lib/chatrix/room.rb', line 76

def process_leave(data)
  @state.update data['state'] if data.key? 'state'
  @timeline.update data['timeline'] if data.key? 'timeline'
end

#to_sString

Gets a string representation of this room.

Returns:

  • (String)

    If the room has a name, that name is returned. If it has a canonical alias, the alias is returned. If it has neither a name nor alias, the room ID is returned.



85
86
87
# File 'lib/chatrix/room.rb', line 85

def to_s
  name || canonical_alias || @id
end