Class: MatrixSdk::User
- Extended by:
- Extensions
- Defined in:
- lib/matrix_sdk/user.rb
Overview
A class for tracking information about a user on Matrix
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#id ⇒ Object
(also: #user_id)
readonly
Returns the value of attribute id.
Instance Method Summary collapse
-
#active? ⇒ Boolean
If the user is currently active.
-
#avatar_url ⇒ Object
Gets the avatar for the user.
-
#avatar_url=(url) ⇒ Object
Set a new avatar for the user.
-
#device_keys ⇒ Object
Returns all the current device keys for the user, retrieving them if necessary.
-
#display_name ⇒ String
The display name.
- #display_name=(name) ⇒ Object
-
#friendly_name ⇒ String
Gets a friendly name of the user.
-
#initialize(client, id, data = {}) ⇒ User
constructor
A new instance of User.
-
#inspect ⇒ String
An inspect method that skips a handful of instance variables to avoid flooding the terminal with debug data.
-
#last_active ⇒ Time
Gets the last time the user was active at, from the server’s side.
-
#presence ⇒ Symbol
Get the user’s current presence status.
-
#presence=(new_presence) ⇒ Object
Sets the user’s current presence status Should be one of :online, :offline, or :unavailable.
-
#status_msg ⇒ Object
Gets the user-specified status message - if any.
-
#status_msg=(message) ⇒ Object
Sets the user-specified status message.
Methods included from Extensions
Constructor Details
#initialize(client, id, data = {}) ⇒ User
Returns a new instance of User.
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/matrix_sdk/user.rb', line 19 def initialize(client, id, data = {}) @client = client @id = id @display_name = nil @avatar_url = nil data.each do |k, v| instance_variable_set("@#{k}", v) if instance_variable_defined? "@#{k}" end end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
10 11 12 |
# File 'lib/matrix_sdk/user.rb', line 10 def client @client end |
#id ⇒ Object (readonly) Also known as: user_id
Returns the value of attribute id.
10 11 12 |
# File 'lib/matrix_sdk/user.rb', line 10 def id @id end |
Instance Method Details
#active? ⇒ Boolean
This information is not cached in the abstraction layer
Returns if the user is currently active.
93 94 95 |
# File 'lib/matrix_sdk/user.rb', line 93 def active? raw_presence[:currently_active] == true end |
#avatar_url ⇒ Object
Gets the avatar for the user
53 54 55 |
# File 'lib/matrix_sdk/user.rb', line 53 def avatar_url @avatar_url ||= client.api.get_avatar_url(id)[:avatar_url] end |
#avatar_url=(url) ⇒ Object
Requires a mxc:// URL, check example on Protocols::CS#set_avatar_url for how this can be done
Set a new avatar for the user
Only works for the current user object, as requested by
client.get_user(:self)
66 67 68 69 |
# File 'lib/matrix_sdk/user.rb', line 66 def avatar_url=(url) client.api.set_avatar_url(id, url) @avatar_url = url end |
#device_keys ⇒ Object
Returns all the current device keys for the user, retrieving them if necessary
126 127 128 129 130 |
# File 'lib/matrix_sdk/user.rb', line 126 def device_keys @device_keys ||= client.api.keys_query(device_keys: { id => [] }).yield_self do |resp| resp[:device_keys][id.to_sym] end end |
#display_name ⇒ String
Returns the display name.
33 34 35 |
# File 'lib/matrix_sdk/user.rb', line 33 def display_name @display_name ||= client.api.get_display_name(id)[:displayname] end |
#display_name=(name) ⇒ Object
39 40 41 42 |
# File 'lib/matrix_sdk/user.rb', line 39 def display_name=(name) client.api.set_display_name(id, name) @display_name = name end |
#friendly_name ⇒ String
Gets a friendly name of the user
46 47 48 |
# File 'lib/matrix_sdk/user.rb', line 46 def friendly_name display_name || id end |
#inspect ⇒ String
An inspect method that skips a handful of instance variables to avoid flooding the terminal with debug data.
17 |
# File 'lib/matrix_sdk/user.rb', line 17 ignore_inspect :client |
#last_active ⇒ Time
This information is not cached in the abstraction layer
Gets the last time the user was active at, from the server’s side
118 119 120 121 122 123 |
# File 'lib/matrix_sdk/user.rb', line 118 def last_active since = raw_presence[:last_active_ago] return unless since Time.now - (since / 1000) end |
#presence ⇒ Symbol
This information is not cached in the abstraction layer
Get the user’s current presence status
76 77 78 |
# File 'lib/matrix_sdk/user.rb', line 76 def presence raw_presence[:presence].to_sym end |
#presence=(new_presence) ⇒ Object
Sets the user’s current presence status Should be one of :online, :offline, or :unavailable
85 86 87 88 89 |
# File 'lib/matrix_sdk/user.rb', line 85 def presence=(new_presence) raise ArgumentError, 'Presence must be one of :online, :offline, :unavailable' unless %i[online offline unavailable].include?(presence) client.api.set_presence_status(id, new_presence) end |
#status_msg ⇒ Object
This information is not cached in the abstraction layer
Gets the user-specified status message - if any
101 102 103 |
# File 'lib/matrix_sdk/user.rb', line 101 def status_msg raw_presence[:status_msg] end |
#status_msg=(message) ⇒ Object
Sets the user-specified status message
109 110 111 |
# File 'lib/matrix_sdk/user.rb', line 109 def status_msg=() client.api.set_presence_status(id, presence, message: ) end |