Class: Chatrix::Components::State

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

Overview

Manages state for a room.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(room, users) ⇒ State

Initializes a new State instance.

Parameters:

  • room (Room)

    The room the state belongs to.

  • users (Users)

    The user manager.



47
48
49
50
51
52
53
54
55
# 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

Instance Attribute Details

#canonical_aliasString? (readonly)

Returns The canonical alias, or nil if none has been set.

Returns:

  • (String, nil)

    The canonical alias, or nil if none has been set.



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

def canonical_alias
  @canonical_alias
end

#creatorUser (readonly)

Returns The user who created the room.

Returns:

  • (User)

    The user who created the room.



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

def creator
  @creator
end

#guest_accessBoolean (readonly)

Returns true if guests are allowed in the room, otherwise false.

Returns:

  • (Boolean)

    true if guests are allowed in the room, otherwise false.



32
33
34
# File 'lib/chatrix/components/state.rb', line 32

def guest_access
  @guest_access
end

#history_visibilityString (readonly)

Returns The room's history visibility.

Returns:

  • (String)

    The room's history visibility.



38
39
40
# File 'lib/chatrix/components/state.rb', line 38

def history_visibility
  @history_visibility
end

#join_ruleString (readonly)

Returns Join rules for the room.

Returns:

  • (String)

    Join rules for the room.



35
36
37
# File 'lib/chatrix/components/state.rb', line 35

def join_rule
  @join_rule
end

#nameString? (readonly)

Returns The name, or nil if none has been set.

Returns:

  • (String, nil)

    The name, or nil if none has been set.



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

def name
  @name
end

#permissionsPermissions (readonly)

Returns Check room permissions.

Returns:



41
42
43
# File 'lib/chatrix/components/state.rb', line 41

def permissions
  @permissions
end

#topicString? (readonly)

Returns The topic, or nil if none has been set.

Returns:

  • (String, nil)

    The topic, or nil if none has been set.



25
26
27
# File 'lib/chatrix/components/state.rb', line 25

def topic
  @topic
end

Instance Method Details

#handle_aliases(event) ⇒ Object (private)

Handle the m.room.aliases event.

Parameters:

  • event (Hash)

    Event data.



101
102
103
104
# 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) ⇒ Object (private)

Handle the m.room.canonical_alias event.

Parameters:

  • event (Hash)

    Event data.



94
95
96
97
# 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) ⇒ Object (private)

Handle the m.room.create event.

Parameters:

  • event (Hash)

    Event data.



87
88
89
90
# 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) ⇒ Object (private)

Handle the m.room.guest_access event.

Parameters:

  • event (Hash)

    Event data.



120
121
122
123
# 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) ⇒ Object (private)

Handle the m.room.history_visibility event.

Parameters:

  • event (Hash)

    Event data.



127
128
129
130
# 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) ⇒ Object (private)

Handle the m.room.join_rules event.

Parameters:

  • event (Hash)

    Event data.



134
135
136
137
# 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) ⇒ Object (private)

Process a member event.

Parameters:

  • event (Hash)

    The member event.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/chatrix/components/state.rb', line 141

def handle_member(event)
  @users.process_member_event self, event
  user = @users[event['sender']]
  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) ⇒ Object (private)

Handle the m.room.name event.

Parameters:

  • event (Hash)

    Event data.



108
109
110
# File 'lib/chatrix/components/state.rb', line 108

def handle_name(event)
  broadcast :name, @room, @name = event['content']['name']
end

#handle_power_levels(event) ⇒ Object (private)

Process a power level event.

Parameters:

  • event (Hash)

    Event data.



161
162
163
164
165
# 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) ⇒ Object (private)

Handle the m.room.topic event.

Parameters:

  • event (Hash)

    Event data.



114
115
116
# File 'lib/chatrix/components/state.rb', line 114

def handle_topic(event)
  broadcast :topic, @room, @topic = event['content']['topic']
end

#member?(user) ⇒ Boolean

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

Parameters:

  • user (User)

    The user to check.

Returns:

  • (Boolean)

    true if the user is a member of the room, otherwise false.



62
63
64
# File 'lib/chatrix/components/state.rb', line 62

def member?(user)
  @members.member? user
end

#process_event(event) ⇒ Object (private)

Processes a state event.

Parameters:

  • event (Hash)

    Event data.



76
77
78
79
80
81
82
83
# 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

#update(data) ⇒ Object

Updates the state with new event data.

Parameters:

  • data (Hash)

    Event data.



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

def update(data)
  data['events'].each { |e| process_event e } if data.key? 'events'
end