Class: MatrixSdk::User

Inherits:
Object show all
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

Instance Method Summary collapse

Methods included from Extensions

events, ignore_inspect

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

#clientObject (readonly)

Returns the value of attribute client.



10
11
12
# File 'lib/matrix_sdk/user.rb', line 10

def client
  @client
end

#idObject (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

Note:

This information is not cached in the abstraction layer

Returns if the user is currently active.

Returns:

  • (Boolean)

    if the user is currently active



115
116
117
# File 'lib/matrix_sdk/user.rb', line 115

def active?
  raw_presence[:currently_active] == true
end

#admin?(room) ⇒ Boolean

Check if the user is an admin in a given room

Parameters:

  • room (String, MXID)

    the room to check

Returns:

  • (Boolean)

    If the user is an admin (PL >= 100)



81
82
83
# File 'lib/matrix_sdk/user.rb', line 81

def admin?(room)
  client.ensure_room(room).user_powerlevel(self) >= 100
end

#avatar_urlObject

Gets the avatar for the user



59
60
61
# File 'lib/matrix_sdk/user.rb', line 59

def avatar_url
  @avatar_url ||= client.api.get_avatar_url(id)[:avatar_url]
end

#avatar_url=(url) ⇒ Object

Note:

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)

Parameters:

  • url (String, URI::MXC)

    the new avatar URL

See Also:



72
73
74
75
# File 'lib/matrix_sdk/user.rb', line 72

def avatar_url=(url)
  client.api.set_avatar_url(id, url)
  @avatar_url = url
end

#device_keysObject

Returns all the current device keys for the user, retrieving them if necessary



156
157
158
159
160
# File 'lib/matrix_sdk/user.rb', line 156

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_roomRoom?

Gets a direct message room with the user if one exists

Returns:

  • (Room, nil)

    A direct message room if one exists

See Also:



151
152
153
# File 'lib/matrix_sdk/user.rb', line 151

def direct_room
  client.direct_room(id)
end

#display_nameString

Returns the display name.

Returns:

  • (String)

    the display name

See Also:



39
40
41
# File 'lib/matrix_sdk/user.rb', line 39

def display_name
  @display_name ||= client.api.get_display_name(id)[:displayname]
end

#display_name=(name) ⇒ Object

Parameters:

  • name (String)

    the display name to set

See Also:



45
46
47
48
# File 'lib/matrix_sdk/user.rb', line 45

def display_name=(name)
  client.api.set_display_name(id, name)
  @display_name = name
end

#friendly_nameString

Gets a friendly name of the user

Returns:

  • (String)

    either the display name or MXID if unset



52
53
54
# File 'lib/matrix_sdk/user.rb', line 52

def friendly_name
  display_name || id
end

#inspectString

An inspect method that skips a handful of instance variables to avoid flooding the terminal with debug data.

Returns:

  • (String)

    a regular inspect string without the data for some variables



17
# File 'lib/matrix_sdk/user.rb', line 17

ignore_inspect :client

#last_activeTime

Note:

This information is not cached in the abstraction layer

Gets the last time the user was active at, from the server’s side

Returns:

  • (Time)

    when the user was last active

See Also:



140
141
142
143
144
145
# File 'lib/matrix_sdk/user.rb', line 140

def last_active
  since = raw_presence[:last_active_ago]
  return unless since

  Time.now - (since / 1000)
end

#moderator?(room) ⇒ Boolean

Check if the user is a moderator in a given room

Parameters:

  • room (String, MXID)

    the room to check

Returns:

  • (Boolean)

    If the user is an admin (PL >= 50)



89
90
91
# File 'lib/matrix_sdk/user.rb', line 89

def moderator?(room)
  client.ensure_room(room).user_powerlevel(self) >= 50
end

#presenceSymbol

Note:

This information is not cached in the abstraction layer

Get the user’s current presence status

Returns:

  • (Symbol)

    One of :online, :offline, :unavailable

See Also:



98
99
100
# File 'lib/matrix_sdk/user.rb', line 98

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

Parameters:

  • new_presence (:online, :offline, :unavailable)

    The new presence status to set

Raises:

  • (ArgumentError)

See Also:



107
108
109
110
111
# File 'lib/matrix_sdk/user.rb', line 107

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_msgObject

Note:

This information is not cached in the abstraction layer

Gets the user-specified status message - if any



123
124
125
# File 'lib/matrix_sdk/user.rb', line 123

def status_msg
  raw_presence[:status_msg]
end

#status_msg=(message) ⇒ Object

Sets the user-specified status message

Parameters:

  • message (String, nil)

    The message to set, or nil for no message

See Also:



131
132
133
# File 'lib/matrix_sdk/user.rb', line 131

def status_msg=(message)
  client.api.set_presence_status(id, presence, message: message)
end

#to_sObject



31
32
33
34
35
# File 'lib/matrix_sdk/user.rb', line 31

def to_s
  "#{display_name} (#{id})" if @display_name

  @id.to_s
end