Class: Chatrix::Components::State
- Inherits:
-
Object
- Object
- Chatrix::Components::State
- Includes:
- Wisper::Publisher
- Defined in:
- lib/chatrix/components/state.rb
Overview
Manages state for a room.
Instance Attribute Summary collapse
-
#canonical_alias ⇒ String?
readonly
The canonical alias, or
nil
if none has been set. -
#creator ⇒ User
readonly
The user who created the room.
-
#guest_access ⇒ Boolean
readonly
true
if guests are allowed in the room, otherwisefalse
. -
#history_visibility ⇒ String
readonly
The room's history visibility.
-
#join_rule ⇒ String
readonly
Join rules for the room.
-
#name ⇒ String?
readonly
The name, or
nil
if none has been set. -
#permissions ⇒ Permissions
readonly
Check room permissions.
-
#topic ⇒ String?
readonly
The topic, or
nil
if none has been set.
Instance Method Summary collapse
-
#handle_aliases(event) ⇒ Object
private
Handle the
m.room.aliases
event. -
#handle_canonical_alias(event) ⇒ Object
private
Handle the
m.room.canonical_alias
event. -
#handle_create(event) ⇒ Object
private
Handle the
m.room.create
event. -
#handle_guest_access(event) ⇒ Object
private
Handle the
m.room.guest_access
event. -
#handle_history_visibility(event) ⇒ Object
private
Handle the
m.room.history_visibility
event. -
#handle_join_rules(event) ⇒ Object
private
Handle the
m.room.join_rules
event. -
#handle_member(event) ⇒ Object
private
Process a member event.
-
#handle_name(event) ⇒ Object
private
Handle the
m.room.name
event. -
#handle_power_levels(event) ⇒ Object
private
Process a power level event.
-
#handle_topic(event) ⇒ Object
private
Handle the
m.room.topic
event. -
#initialize(room, users) ⇒ State
constructor
Initializes a new State instance.
-
#member?(user) ⇒ Boolean
Returns whether the specified user is a member of the room.
-
#process_event(event) ⇒ Object
private
Processes a state event.
-
#update(data) ⇒ Object
Updates the state with new event data.
Constructor Details
#initialize(room, users) ⇒ State
Initializes a new State instance.
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_alias ⇒ String? (readonly)
Returns 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 |
#creator ⇒ User (readonly)
Returns The user who created the room.
28 29 30 |
# File 'lib/chatrix/components/state.rb', line 28 def creator @creator end |
#guest_access ⇒ Boolean (readonly)
Returns 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_visibility ⇒ String (readonly)
Returns The room's history visibility.
38 39 40 |
# File 'lib/chatrix/components/state.rb', line 38 def history_visibility @history_visibility end |
#join_rule ⇒ String (readonly)
Returns Join rules for the room.
35 36 37 |
# File 'lib/chatrix/components/state.rb', line 35 def join_rule @join_rule end |
#name ⇒ String? (readonly)
Returns The name, or nil
if none has been set.
22 23 24 |
# File 'lib/chatrix/components/state.rb', line 22 def name @name end |
#permissions ⇒ Permissions (readonly)
Returns Check room permissions.
41 42 43 |
# File 'lib/chatrix/components/state.rb', line 41 def @permissions end |
#topic ⇒ String? (readonly)
Returns 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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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 |