Class: ActiveMatrix::User
- Inherits:
-
Object
- Object
- ActiveMatrix::User
- Extended by:
- Extensions
- Includes:
- Cacheable
- Defined in:
- lib/active_matrix/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.
-
#admin?(room) ⇒ Boolean
Check if the user is an admin in a given room.
-
#avatar_url ⇒ Object
Gets the avatar for the user.
-
#avatar_url=(url) ⇒ Object
Set a new avatar for the user.
-
#cache_attributes ⇒ Object
Define what attributes to cache.
-
#device_keys ⇒ Object
Returns all the current device keys for the user, retrieving them if necessary.
-
#direct_room ⇒ Room?
Gets a direct message room with the user if one exists.
-
#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.
-
#moderator?(room) ⇒ Boolean
Check if the user is a moderator in a given room.
-
#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.
- #to_s ⇒ Object
Methods included from Extensions
Methods included from Cacheable
#cache_id, #cache_key, #from_cache?, #to_cache
Constructor Details
#initialize(client, id, data = {}) ⇒ User
Returns a new instance of User.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/active_matrix/user.rb', line 18 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.
9 10 11 |
# File 'lib/active_matrix/user.rb', line 9 def client @client end |
#id ⇒ Object (readonly) Also known as: user_id
Returns the value of attribute id.
9 10 11 |
# File 'lib/active_matrix/user.rb', line 9 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.
114 115 116 |
# File 'lib/active_matrix/user.rb', line 114 def active? raw_presence[:currently_active] == true end |
#admin?(room) ⇒ Boolean
Check if the user is an admin in a given room
80 81 82 |
# File 'lib/active_matrix/user.rb', line 80 def admin?(room) client.ensure_room(room).user_powerlevel(self) >= 100 end |
#avatar_url ⇒ Object
Gets the avatar for the user
58 59 60 |
# File 'lib/active_matrix/user.rb', line 58 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)
71 72 73 74 |
# File 'lib/active_matrix/user.rb', line 71 def avatar_url=(url) client.api.set_avatar_url(id, url) @avatar_url = url end |
#cache_attributes ⇒ Object
Define what attributes to cache
162 163 164 165 166 167 168 |
# File 'lib/active_matrix/user.rb', line 162 def cache_attributes { id: @id, display_name: @display_name, avatar_url: @avatar_url } end |
#device_keys ⇒ Object
Returns all the current device keys for the user, retrieving them if necessary
155 156 157 158 159 |
# File 'lib/active_matrix/user.rb', line 155 def device_keys @device_keys ||= client.api.keys_query(device_keys: { id => [] }).yield_self do |resp| # rubocop:disable Style/ObjectThen # Keep Ruby 2.5 support a little longer resp.dig(:device_keys, id.to_sym) end end |
#direct_room ⇒ Room?
Gets a direct message room with the user if one exists
150 151 152 |
# File 'lib/active_matrix/user.rb', line 150 def direct_room client.direct_room(id) end |
#display_name ⇒ String
Returns the display name.
38 39 40 |
# File 'lib/active_matrix/user.rb', line 38 def display_name @display_name ||= client.api.get_display_name(id)[:displayname] end |
#display_name=(name) ⇒ Object
44 45 46 47 |
# File 'lib/active_matrix/user.rb', line 44 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
51 52 53 |
# File 'lib/active_matrix/user.rb', line 51 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.
16 |
# File 'lib/active_matrix/user.rb', line 16 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
139 140 141 142 143 144 |
# File 'lib/active_matrix/user.rb', line 139 def last_active since = raw_presence[:last_active_ago] return unless since Time.zone.now - (since / 1000) end |
#moderator?(room) ⇒ Boolean
Check if the user is a moderator in a given room
88 89 90 |
# File 'lib/active_matrix/user.rb', line 88 def moderator?(room) client.ensure_room(room).user_powerlevel(self) >= 50 end |
#presence ⇒ Symbol
This information is not cached in the abstraction layer
Get the user’s current presence status
97 98 99 |
# File 'lib/active_matrix/user.rb', line 97 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
106 107 108 109 110 |
# File 'lib/active_matrix/user.rb', line 106 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
122 123 124 |
# File 'lib/active_matrix/user.rb', line 122 def status_msg raw_presence[:status_msg] end |
#status_msg=(message) ⇒ Object
Sets the user-specified status message
130 131 132 |
# File 'lib/active_matrix/user.rb', line 130 def status_msg=() client.api.set_presence_status(id, presence, message: ) end |
#to_s ⇒ Object
30 31 32 33 34 |
# File 'lib/active_matrix/user.rb', line 30 def to_s "#{display_name} (#{id})" if @display_name @id.to_s end |