Class: Chatrix::Users

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

Overview

Manages the users known to the client.

Instance Method Summary collapse

Constructor Details

#initializeUsers

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.

Parameters:

  • id (String)

    A user's ID or display name.

Returns:

  • (User, nil)

    The User instance for the specified user, or nil if the user could not be found.



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.

Parameters:

  • id (String)

    The user ID to get an instance for.

Returns:

  • (User)

    An instance of User for the specified ID.



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.

Parameters:

  • room (Room)

    The room from which the event originated.

  • event (Hash)

    Event data.



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.

Parameters:

  • room (Room)

    Which room the events are related to.

  • event (Hash)

    Event data.



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.

Parameters:

  • room (Room)

    The room this event came from.

  • data (Hash{String=>Fixnum})

    Power level data, a hash of user IDs and their associated power level.



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