class Chatrix::Components::State

Manages state for a room.

Attributes

canonical_alias[R]

@return [String,nil] The canonical alias, or `nil` if none has

been set.
creator[R]

@return [User] The user who created the room.

guest_access[R]

@return [Boolean] `true` if guests are allowed in the room,

otherwise `false`.
history_visibility[R]

@return [String] The room's history visibility.

join_rule[R]

@return [String] Join rules for the room.

name[R]

@return [String,nil] The name, or `nil` if none has been set.

permissions[R]

@return [Permissions] Check room permissions.

topic[R]

@return [String,nil] The topic, or `nil` if none has been set.

Public Class Methods

new(room, users) click to toggle source

Initializes a new State instance.

@param room [Room] The room the state belongs to. @param users [Users] The user manager.

# File lib/chatrix/components/state.rb, line 47
def initialize(room, users)
  @room = room
  @users = users

  @permissions = Permissions.new @room

  @aliases = []
  @members = Set.new
end

Public Instance Methods

member?(user) click to toggle source

Returns whether the specified user is a member of the room.

@param user [User] The user to check. @return [Boolean] `true` if the user is a member of the room,

otherwise `false`.
# File lib/chatrix/components/state.rb, line 62
def member?(user)
  @members.member? user
end
update(data) click to toggle source

Updates the state with new event data. @param data [Hash] Event data.

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

Private Instance Methods

handle_aliases(event) click to toggle source

Handle the `m.room.aliases` event. @param (see handle_create)

# File lib/chatrix/components/state.rb, line 101
def handle_aliases(event)
  @aliases.replace event['content']['aliases']
  broadcast :aliases, @room, @aliases
end
handle_canonical_alias(event) click to toggle source

Handle the `m.room.canonical_alias` event. @param (see handle_create)

# File lib/chatrix/components/state.rb, line 94
def handle_canonical_alias(event)
  @canonical_alias = event['content']['alias']
  broadcast :canonical_alias, @room, @canonical_alias
end
handle_create(event) click to toggle source

Handle the `m.room.create` event. @param event [Hash] Event data.

# File lib/chatrix/components/state.rb, line 87
def handle_create(event)
  @creator = @users.send(:get_user, event['content']['creator'])
  broadcast :creator, @room, @creator
end
handle_guest_access(event) click to toggle source

Handle the `m.room.guest_access` event. @param (see handle_create)

# File lib/chatrix/components/state.rb, line 120
def handle_guest_access(event)
  @guest_access = event['content']['guest_access'] == 'can_join'
  broadcast :guest_access, @room, @guest_access
end
handle_history_visibility(event) click to toggle source

Handle the `m.room.history_visibility` event. @param (see handle_create)

# File lib/chatrix/components/state.rb, line 127
def handle_history_visibility(event)
  @history_visibility = event['content']['history_visibility']
  broadcast :history_visibility, @room, @history_visibility
end
handle_join_rules(event) click to toggle source

Handle the `m.room.join_rules` event. @param (see handle_create)

# File lib/chatrix/components/state.rb, line 134
def handle_join_rules(event)
  @join_rule = event['content']['join_rule']
  broadcast :join_rule, @room, @join_rule
end
handle_member(event) click to toggle source

Process a member event. @param event [Hash] The member event.

# File lib/chatrix/components/state.rb, line 141
def handle_member(event)
  @users.process_member_event self, event
  user = @users[event['state_key']]
  membership = event['content']['membership'].to_sym

  # Don't process invite state change if the user is already a
  # member in the room.
  return if membership == :invite && member?(user)

  if membership == :join
    @members.add user
  else
    @members.delete user
  end

  broadcast(membership, @room, user)
end
handle_name(event) click to toggle source

Handle the `m.room.name` event. @param (see handle_create)

# File lib/chatrix/components/state.rb, line 108
def handle_name(event)
  broadcast :name, @room, @name = event['content']['name']
end
handle_power_levels(event) click to toggle source

Process a power level event. @param event [Hash] Event data.

# File lib/chatrix/components/state.rb, line 161
def handle_power_levels(event)
  content = event['content']
  @permissions.update content
  @users.process_power_levels @room, content['users']
end
handle_topic(event) click to toggle source

Handle the `m.room.topic` event. @param (see handle_create)

# File lib/chatrix/components/state.rb, line 114
def handle_topic(event)
  broadcast :topic, @room, @topic = event['content']['topic']
end
process_event(event) click to toggle source

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

# File lib/chatrix/components/state.rb, line 76
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

  Events.processed event
end