Class: Chatrix::Users
- Inherits:
-
Object
- Object
- Chatrix::Users
- Includes:
- Wisper::Publisher
- Defined in:
- lib/chatrix/users.rb
Overview
Manages the users known to the client.
Instance Method Summary collapse
-
#[](id) ⇒ User?
Gets a user by ID or display name.
-
#get_user(id) ⇒ User
private
Get the user instance for a specified user ID.
-
#initialize ⇒ Users
constructor
Initializes a new Users instance.
-
#process_invite(room, event) ⇒ Object
Process an invite event for a room.
-
#process_member_event(room, event) ⇒ Object
Process a member event.
-
#process_power_levels(room, data) ⇒ Object
Process power level updates.
Constructor Details
#initialize ⇒ Users
Initializes a new Users instance.
15 16 17 18 |
# File 'lib/chatrix/users.rb', line 15 def initialize # user_id => user @users = {} end |
Instance Method Details
#[](id) ⇒ User?
Gets a user by ID or display name.
25 26 27 28 29 30 |
# File 'lib/chatrix/users.rb', line 25 def [](id) return @users[id] if id.start_with? '@' res = @users.find { |_, u| u.displayname == id } res.last if res.respond_to? :last end |
#get_user(id) ⇒ User (private)
Get the user instance for a specified user ID. If an instance does not exist for the user, one is created and returned.
70 71 72 73 74 75 76 |
# File 'lib/chatrix/users.rb', line 70 def get_user(id) return @users[id] if @users.key? id user = User.new id @users[id] = user broadcast(:added, user) user end |
#process_invite(room, event) ⇒ Object
Process an invite event for a room.
57 58 59 60 61 |
# File 'lib/chatrix/users.rb', line 57 def process_invite(room, event) sender = get_user(event['sender']) invitee = get_user(event['state_key']) invitee.process_invite room, sender, event end |
#process_member_event(room, event) ⇒ Object
Process a member event.
36 37 38 39 40 |
# File 'lib/chatrix/users.rb', line 36 def process_member_event(room, event) return if Events.processed? event id = event['state_key'] || event['sender'] get_user(id).process_member_event room, event end |
#process_power_levels(room, data) ⇒ Object
Process power level updates.
47 48 49 50 51 |
# File 'lib/chatrix/users.rb', line 47 def process_power_levels(room, data) data.each do |id, level| get_user(id).process_power_level room, level end end |