Class: Chatrix::Room

Inherits:
Object
  • Object
show all
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.



36
37
38
39
40
41
42
43
44
45
# File 'lib/chatrix/room.rb', line 36

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
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.



26
27
28
# File 'lib/chatrix/room.rb', line 26

def admin
  @admin
end

#idString (readonly)

Returns The ID of this room.

Returns:

  • (String)

    The ID of this room.



16
17
18
# File 'lib/chatrix/room.rb', line 16

def id
  @id
end

#messagingMessaging (readonly)

Returns Handle various message actions through this object.

Returns:

  • (Messaging)

    Handle various message actions through this object.



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

def messaging
  @messaging
end

#stateState (readonly)

Returns The state object for this room.

Returns:

  • (State)

    The state object for this room.



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

def state
  @state
end

#timelineTimeline (readonly)

Returns The timeline object for this room.

Returns:

  • (Timeline)

    The timeline object for this room.



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

def timeline
  @timeline
end

Instance Method Details

#canonical_aliasString

Convenience method to get the canonical alias from this room's state.

Returns:

  • (String)

    The canonical alias for this room.



49
50
51
# File 'lib/chatrix/room.rb', line 49

def canonical_alias
  @state.canonical_alias
end

#nameString

Convenience method to get the name from this room's state.

Returns:

  • (String)

    The name for this room.



55
56
57
# File 'lib/chatrix/room.rb', line 55

def name
  @state.name
end

#process_invite(data) ⇒ Object

Process invite events for this room.

Parameters:

  • data (Hash)

    Event data containing special invite data.



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

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.



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

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.



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

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.



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

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.



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

def to_s
  name || canonical_alias || @id
end