class Chatrix::Users
Manages the users known to the client.
Public Class Methods
Initializes a new Users
instance.
# File lib/chatrix/users.rb, line 15 def initialize # user_id => user @users = {} end
Public Instance Methods
Gets a user by ID or display name.
@param id [String] A user's ID or display name. @return [User,nil] The User
instance for the specified user, or
`nil` if the user could not be found.
# 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
Process an invite event for a room.
@param room [Room] The room from which the event originated. @param event [Hash] Event data.
# 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 a member event.
@param room [Room] Which room the events are related to. @param event [Hash] Event data.
# 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 level updates.
@param room [Room] The room this event came from. @param data [Hash{String=>Fixnum}] Power level data, a hash of user IDs
and their associated power level.
# 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
Private Instance Methods
Get the user instance for a specified user ID. If an instance does not exist for the user, one is created and returned.
@param id [String] The user ID to get an instance for. @return [User] An instance of User
for the specified ID.
# 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