Class: MatrixSdk::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/matrix_sdk/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hs_url, client_cache: :all, **params) ⇒ Client

Returns a new instance of Client.

Parameters:

  • hs_url (String, URI, Api)

    The URL to the Matrix homeserver, without the /_matrix/ part, or an existing Api instance

  • client_cache (:all, :some, :none) (defaults to: :all)

    (:all) How much data should be cached in the client

  • params (Hash)

    Additional parameters on creation

Options Hash (**params):

  • :user_id (String, MXID)

    The user ID of the logged-in user

  • :sync_filter_limit (Integer) — default: 20

    Limit of timeline entries in syncs

Raises:

  • (ArgumentError)

See Also:

  • for additional usable params


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/matrix_sdk/client.rb', line 26

def initialize(hs_url, client_cache: :all, **params)
  event_initialize

  params[:user_id] ||= params[:mxid] if params[:mxid]

  if hs_url.is_a? Api
    @api = hs_url
    params.each do |k, v|
      api.instance_variable_set("@#{k}", v) if api.instance_variable_defined? "@#{k}"
    end
  else
    @api = Api.new hs_url, params
  end

  @rooms = {}
  @users = {}
  @cache = client_cache

  @sync_token = nil
  @sync_thread = nil
  @sync_filter = { room: { timeline: { limit: params.fetch(:sync_filter_limit, 20) } } }

  @should_listen = false
  @next_batch = nil

  @bad_sync_timeout_limit = 60 * 60

  params.each do |k, v|
    instance_variable_set("@#{k}", v) if instance_variable_defined? "@#{k}"
  end

  raise ArgumentError, 'Cache value must be one of of [:all, :some, :none]' unless i[all some none].include? @cache

  return unless params[:user_id]

  @mxid = params[:user_id]
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



9
10
11
# File 'lib/matrix_sdk/client.rb', line 9

def api
  @api
end

#cacheObject

Returns the value of attribute cache.



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

def cache
  @cache
end

#sync_filterObject

Returns the value of attribute sync_filter.



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

def sync_filter
  @sync_filter
end

Instance Method Details

#create_room(room_alias = nil, params = {}) ⇒ Object



148
149
150
# File 'lib/matrix_sdk/client.rb', line 148

def create_room(room_alias = nil, params = {})
  api.create_room(params.merge(room_alias: room_alias))
end

#find_room(room_id_or_alias) ⇒ Object



157
158
159
# File 'lib/matrix_sdk/client.rb', line 157

def find_room(room_id_or_alias)
  @rooms.fetch(room_id_or_alias, @rooms.values.find { |r| r.canonical_alias == room_id_or_alias })
end

#get_user(user_id) ⇒ Object



161
162
163
164
165
166
167
# File 'lib/matrix_sdk/client.rb', line 161

def get_user(user_id)
  if cache == :all
    @users[user_id] ||= User.new(self, user_id)
  else
    User.new(self, user_id)
  end
end

#join_room(room_id_or_alias) ⇒ Object



152
153
154
155
# File 'lib/matrix_sdk/client.rb', line 152

def join_room(room_id_or_alias)
  data = api.join_room(room_id_or_alias)
  ensure_room(data.fetch(:room_id, room_id_or_alias))
end

#listen_for_events(timeout: 30, **arguments) ⇒ Object



180
181
182
# File 'lib/matrix_sdk/client.rb', line 180

def listen_for_events(timeout: 30, **arguments)
  sync(arguments.merge(timeout: timeout))
end

#logged_in?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/matrix_sdk/client.rb', line 144

def logged_in?
  !(mxid.nil? || @api.access_token.nil?)
end

#loggerObject



64
65
66
# File 'lib/matrix_sdk/client.rb', line 64

def logger
  @logger ||= Logging.logger[self]
end

#login(username, password, sync_timeout: 15, full_state: false, **params) ⇒ Object

Raises:

  • (ArgumentError)


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/matrix_sdk/client.rb', line 104

def (username, password, sync_timeout: 15, full_state: false, **params)
  username = username.to_s unless username.is_a?(String)
  password = password.to_s unless password.is_a?(String)

  raise ArgumentError, "Username can't be nil or empty" if username.nil? || username.empty?
  raise ArgumentError, "Password can't be nil or empty" if password.nil? || password.empty?

  data = api.(user: username, password: password)
  post_authentication(data)

  return if params[:no_sync]

  sync timeout: sync_timeout,
       full_state: full_state,
       allow_sync_retry: params.fetch(:allow_sync_retry, nil)
end

#login_with_token(username, token, sync_timeout: 15, full_state: false, **params) ⇒ Object

Raises:

  • (ArgumentError)


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/matrix_sdk/client.rb', line 121

def (username, token, sync_timeout: 15, full_state: false, **params)
  username = username.to_s unless username.is_a?(String)
  token = token.to_s unless token.is_a?(String)

  raise ArgumentError, "Username can't be nil or empty" if username.nil? || username.empty?
  raise ArgumentError, "Token can't be nil or empty" if token.nil? || token.empty?

  data = api.(user: username, token: token, type: 'm.login.token')
  post_authentication(data)

  return if params[:no_sync]

  sync timeout: sync_timeout,
       full_state: full_state,
       allow_sync_retry: params.fetch(:allow_sync_retry, nil)
end

#logoutObject



138
139
140
141
142
# File 'lib/matrix_sdk/client.rb', line 138

def logout
  api.logout
  @api.access_token = nil
  @mxid = nil
end

#mxidObject Also known as: user_id



68
69
70
71
72
# File 'lib/matrix_sdk/client.rb', line 68

def mxid
  @mxid ||= begin
    api.whoami?[:user_id] if api && api.access_token
  end
end

#mxid=(id) ⇒ Object Also known as: user_id=

Raises:

  • (ArgumentError)


74
75
76
77
78
79
# File 'lib/matrix_sdk/client.rb', line 74

def mxid=(id)
  id = MXID.new id.to_s unless id.is_a? MXID
  raise ArgumentError, 'Must be a User ID' unless id.user?

  @mxid = id
end

#register_as_guestObject



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

def register_as_guest
  data = api.register(kind: :guest)
  post_authentication(data)
end

#register_with_password(username, password) ⇒ Object

Raises:

  • (ArgumentError)


93
94
95
96
97
98
99
100
101
102
# File 'lib/matrix_sdk/client.rb', line 93

def register_with_password(username, password)
  username = username.to_s unless username.is_a?(String)
  password = password.to_s unless password.is_a?(String)

  raise ArgumentError, "Username can't be nil or empty" if username.nil? || username.empty?
  raise ArgumentError, "Password can't be nil or empty" if password.nil? || username.empty?

  data = api.register(auth: { type: 'm.login.dummy' }, username: username, password: password)
  post_authentication(data)
end

#remove_room_alias(room_alias) ⇒ Object



169
170
171
# File 'lib/matrix_sdk/client.rb', line 169

def remove_room_alias(room_alias)
  api.remove_room_alias(room_alias)
end

#roomsObject



84
85
86
# File 'lib/matrix_sdk/client.rb', line 84

def rooms
  @rooms.values
end

#start_listener_thread(params = {}) ⇒ Object



184
185
186
187
188
189
# File 'lib/matrix_sdk/client.rb', line 184

def start_listener_thread(params = {})
  @should_listen = true
  thread = Thread.new { listen_forever(params) }
  @sync_thread = thread
  thread.run
end

#stop_listener_threadObject



191
192
193
194
195
196
197
# File 'lib/matrix_sdk/client.rb', line 191

def stop_listener_thread
  return unless @sync_thread

  @should_listen = false
  @sync_thread.join if @sync_thread.alive?
  @sync_thread = nil
end

#upload(content, content_type) ⇒ Object



173
174
175
176
177
178
# File 'lib/matrix_sdk/client.rb', line 173

def upload(content, content_type)
  data = api.media_upload(content, content_type)
  return data[:content_uri] if data.key? :content_uri

  raise MatrixUnexpectedResponseError, 'Upload succeeded, but no media URI returned'
end