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, params = {}) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


23
24
25
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
# File 'lib/matrix_sdk/client.rb', line 23

def initialize(hs_url, params = {})
  event_initialize

  params[:user_id] = params[:mxid] if params[:mxid]
  raise ArgumentError, 'Must provide user_id with access_token' if params[:access_token] && !params[:user_id]

  @api = Api.new hs_url, params

  @rooms = {}
  @cache = :all

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

  @should_listen = false

  @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]
  sync
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

#mxidObject Also known as: user_id

Returns the value of attribute mxid.



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

def mxid
  @mxid
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



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

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

#find_room(room_id_or_alias) ⇒ Object



93
94
95
# File 'lib/matrix_sdk/client.rb', line 93

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



97
98
99
# File 'lib/matrix_sdk/client.rb', line 97

def get_user(user_id)
  User.new(self, user_id)
end

#join_room(room_id_or_alias) ⇒ Object



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

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) ⇒ Object



111
112
113
# File 'lib/matrix_sdk/client.rb', line 111

def listen_for_events(timeout = 30)
  sync(timeout: timeout)
end

#loggerObject



53
54
55
# File 'lib/matrix_sdk/client.rb', line 53

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

#login(username, password, params = {}) ⇒ Object



71
72
73
74
75
76
# File 'lib/matrix_sdk/client.rb', line 71

def (username, password, params = {})
  data = api.(user: username, password: password)
  post_authentication(data)

  sync(timeout: params.fetch(:sync_timeout, 15)) unless params[:no_sync]
end

#logoutObject



78
79
80
81
82
# File 'lib/matrix_sdk/client.rb', line 78

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

#register_as_guestObject



61
62
63
64
# File 'lib/matrix_sdk/client.rb', line 61

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

#register_with_password(username, password) ⇒ Object



66
67
68
69
# File 'lib/matrix_sdk/client.rb', line 66

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

#remove_room_alias(room_alias) ⇒ Object



101
102
103
# File 'lib/matrix_sdk/client.rb', line 101

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

#roomsObject



57
58
59
# File 'lib/matrix_sdk/client.rb', line 57

def rooms
  @rooms.values
end

#start_listener_thread(params = {}) ⇒ Object



115
116
117
118
119
120
# File 'lib/matrix_sdk/client.rb', line 115

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

#stop_listener_threadObject



122
123
124
125
126
127
# File 'lib/matrix_sdk/client.rb', line 122

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



105
106
107
108
109
# File 'lib/matrix_sdk/client.rb', line 105

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