Module: MatrixSdk::Protocols::CS

Included in:
Api
Defined in:
lib/matrix_sdk/protocols/cs.rb

Overview

rubocop:disable Metrics/ModuleLength

Instance Method Summary collapse

Instance Method Details

#add_user_tag(user_id, room_id, tag, **params) ⇒ Object



942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
# File 'lib/matrix_sdk/protocols/cs.rb', line 942

def add_user_tag(user_id, room_id, tag, **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  if params[:body]
    content = params[:body]
  else
    content = {}
    content[:order] = params[:order] if params.key? :order
  end

  room_id = ERB::Util.url_encode room_id.to_s
  user_id = ERB::Util.url_encode user_id.to_s
  tag = ERB::Util.url_encode tag.to_s

  request(:put, :client_r0, "/user/#{user_id}/rooms/#{room_id}/tags/#{tag}", body: content, query: query)
end

#allowed_login_methodsResponse

Gets the list of available methods for logging in

Returns:



44
45
46
# File 'lib/matrix_sdk/protocols/cs.rb', line 44

def 
  request(:get, :client_r0, '/login')
end

#ban_user(room_id, user_id, reason: '', **params) ⇒ Object



896
897
898
899
900
901
902
903
904
905
906
907
# File 'lib/matrix_sdk/protocols/cs.rb', line 896

def ban_user(room_id, user_id, reason: '', **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  content = {
    user_id: user_id,
    reason: reason
  }
  room_id = ERB::Util.url_encode room_id.to_s

  request(:post, :client_r0, "/rooms/#{room_id}/ban", body: content, query: query)
end

#client_api_unstable_featuresHash

Gets the list of available unstable client API features

Examples:

Checking for unstable features

api.client_api_unstable_features
# => { :"m.lazy_load_members" => true }
api.client_api_unstable_features.has? 'm.lazy_load_members'
# => true

Returns:

  • (Hash)


31
32
33
34
35
36
37
38
39
40
# File 'lib/matrix_sdk/protocols/cs.rb', line 31

def client_api_unstable_features
  (@client_api_versions ||= request(:get, :client, '/versions')).unstable_features.tap do |vers|
    vers.instance_eval <<-'CODE', __FILE__, __LINE__ + 1
      def has?(feature)
        feature = feature.to_s.to_sym unless feature.is_a? Symbol
        fetch(feature, nil)
      end
    CODE
  end
end

#client_api_versionsArray

Gets the available client API versions

Examples:

Getting API versions

api.client_api_versions
# => [ 'r0.1.0', 'r0.2.0', ...
api.client_api_versions.latest
# => 'r0.5.0'

Returns:

  • (Array)


13
14
15
16
17
18
19
20
21
# File 'lib/matrix_sdk/protocols/cs.rb', line 13

def client_api_versions
  (@client_api_versions ||= request(:get, :client, '/versions')).versions.tap do |vers|
    vers.instance_eval <<-'CODE', __FILE__, __LINE__ + 1
      def latest
        last
      end
    CODE
  end
end

#create_filter(user_id, filter_params, **params) ⇒ Object



1012
1013
1014
1015
1016
1017
1018
1019
# File 'lib/matrix_sdk/protocols/cs.rb', line 1012

def create_filter(user_id, filter_params, **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  user_id = ERB::Util.url_encode user_id.to_s

  request(:post, :client_r0, "/user/#{user_id}/filter", body: filter_params, query: query)
end

#create_room(visibility: :public, **params) ⇒ Response

Creates a new room

Parameters:

  • params (Hash)

    The room creation details

Options Hash (**params):

  • :visibility (Symbol) — default: :public

    The room visibility

  • :room_alias (String)

    A room alias to apply on creation

  • :invite (Boolean)

    Should the room be created invite-only

Returns:

  • (Response)

    A response hash with …

See Also:



220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/matrix_sdk/protocols/cs.rb', line 220

def create_room(visibility: :public, **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  content = {
    visibility: visibility
  }
  content[:room_alias_name] = params.delete(:room_alias) if params[:room_alias]
  content[:invite] = [params.delete(:invite)].flatten if params[:invite]
  content.merge! params

  request(:post, :client_r0, '/createRoom', body: content, query: query)
end

#delete_device(device_id, auth:) ⇒ Object

Removes a device from the current user

Parameters:

  • device_id (String)

    The device to remove

  • auth (Hash)

    Authentication data for the removal request

Raises:

  • (MatrixNotAuthorizeedError)

    The request did not contain enough authentication information, the data in this error will include the necessary information to perform interactive auth

See Also:



1202
1203
1204
1205
1206
# File 'lib/matrix_sdk/protocols/cs.rb', line 1202

def delete_device(device_id, auth:)
  device_id = ERB::Util.url_encode device_id.to_s

  request(:delete, :client_r0, "/devices/#{device_id}", body: { auth: auth })
end

#forget_room(room_id, **params) ⇒ Object



840
841
842
843
844
845
846
847
# File 'lib/matrix_sdk/protocols/cs.rb', line 840

def forget_room(room_id, **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  room_id = ERB::Util.url_encode room_id.to_s

  request(:post, :client_r0, "/rooms/#{room_id}/forget", query: query)
end

#get_account_data(user_id, type_key, **params) ⇒ Object



960
961
962
963
964
965
966
967
968
# File 'lib/matrix_sdk/protocols/cs.rb', line 960

def (user_id, type_key, **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  user_id = ERB::Util.url_encode user_id.to_s
  type_key = ERB::Util.url_encode type_key.to_s

  request(:get, :client_r0, "/user/#{user_id}/account_data/#{type_key}", query: query)
end

#get_avatar_url(user_id, **params) ⇒ Object



1050
1051
1052
1053
1054
1055
1056
1057
# File 'lib/matrix_sdk/protocols/cs.rb', line 1050

def get_avatar_url(user_id, **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  user_id = ERB::Util.url_encode user_id.to_s

  request(:get, :client_r0, "/profile/#{user_id}/avatar_url", query: query)
end

#get_device(device_id) ⇒ Response

Gets the information about a certain client device

Parameters:

  • device_id (String)

    The ID of the device to look up

Returns:

  • (Response)

    An object containing all available device information

See Also:



1178
1179
1180
1181
1182
# File 'lib/matrix_sdk/protocols/cs.rb', line 1178

def get_device(device_id)
  device_id = ERB::Util.url_encode device_id.to_s

  request(:get, :client_r0, "/devices/#{device_id}")
end

#get_devicesResponse

Gets a list of the current users registered devices

Returns:

  • (Response)

    An object including all information about the users devices.

See Also:



1169
1170
1171
# File 'lib/matrix_sdk/protocols/cs.rb', line 1169

def get_devices
  request(:get, :client_r0, '/devices')
end

#get_display_name(user_id, **params) ⇒ Object



1028
1029
1030
1031
1032
1033
1034
1035
# File 'lib/matrix_sdk/protocols/cs.rb', line 1028

def get_display_name(user_id, **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  user_id = ERB::Util.url_encode user_id.to_s

  request(:get, :client_r0, "/profile/#{user_id}/displayname", query: query)
end

#get_download_url(mxcurl, source: nil, **_params) ⇒ URI

Converts a Matrix content URL (mxc://) to a media download URL

Examples:

Converting a MXC URL

url = 'mxc://example.com/media_hash'

api.get_download_url(url)
# => #<URI::HTTPS https://example.com/_matrix/media/r0/download/example.com/media_hash>
api.get_download_url(url, source: 'matrix.org')
# => #<URI::HTTPS https://matrix.org/_matrix/media/r0/download/example.com/media_hash>

Parameters:

  • mxcurl (String, URI)

    The Matrix content URL to convert

  • source (String, URI) (defaults to: nil)

    A source HS to use for the convertion, defaults to the connected HS

Returns:

  • (URI)

    The full download URL for the requested piece of media



1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
# File 'lib/matrix_sdk/protocols/cs.rb', line 1093

def get_download_url(mxcurl, source: nil, **_params)
  mxcurl = URI.parse(mxcurl.to_s) unless mxcurl.is_a? URI
  raise 'Not a mxc:// URL' unless mxcurl.is_a? URI::MATRIX

  if source
    source = "https://#{source}" unless source.include? '://'
    source = URI(source.to_s) unless source.is_a?(URI)
  end

  source ||= homeserver.dup
  source.tap do |u|
    full_path = mxcurl.full_path.to_s
    u.path = "/_matrix/media/r0/download/#{full_path}"
  end
end

#get_filter(user_id, filter_id, **params) ⇒ Object



1002
1003
1004
1005
1006
1007
1008
1009
1010
# File 'lib/matrix_sdk/protocols/cs.rb', line 1002

def get_filter(user_id, filter_id, **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  user_id = ERB::Util.url_encode user_id.to_s
  filter_id = ERB::Util.url_encode filter_id.to_s

  request(:get, :client_r0, "/user/#{user_id}/filter/#{filter_id}", query: query)
end

#get_joined_rooms(**params) ⇒ Response

Gets the list of rooms joined by the current user

Returns:

  • (Response)

    An array of room IDs under the key :joined_rooms

See Also:



172
173
174
175
176
177
# File 'lib/matrix_sdk/protocols/cs.rb', line 172

def get_joined_rooms(**params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  request(:get, :client_r0, '/joined_rooms', query: query)
end

#get_membership(room_id, user_id, **params) ⇒ Object



875
876
877
878
879
880
881
882
883
# File 'lib/matrix_sdk/protocols/cs.rb', line 875

def get_membership(room_id, user_id, **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  room_id = ERB::Util.url_encode room_id.to_s
  user_id = ERB::Util.url_encode user_id.to_s

  request(:get, :client_r0, "/rooms/#{room_id}/state/m.room.member/#{user_id}", query: query)
end

#get_profile(user_id, **params) ⇒ Object



1072
1073
1074
1075
1076
1077
1078
1079
# File 'lib/matrix_sdk/protocols/cs.rb', line 1072

def get_profile(user_id, **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  user_id = ERB::Util.url_encode user_id.to_s

  request(:get, :client_r0, "/profile/#{user_id}", query: query)
end

#get_public_rooms(server: nil, **params) ⇒ Response

Gets the list of public rooms on a Matrix server

Parameters:

  • limit (Integer)

    Limits the number of results returned

  • since (String)

    A pagination token received from an earlier call

  • server (String) (defaults to: nil)

    The Matrix server to request public rooms from

Returns:

  • (Response)

    An array of public rooms in the :chunk key, along with :next_batch, :prev_batch, and :total_room_count_estimate for pagination

See Also:



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/matrix_sdk/protocols/cs.rb', line 189

def get_public_rooms(server: nil, **params)
  query = {
    server: server
  }.compact
  body = nil
  method = :get

  if !params[:filter].nil? || !params[:include_all_networks].nil? || !params[:third_party_instance_id].nil?
    body = {
      limit: params[:limit],
      since: params[:since],
      filter: params[:filter],
      include_all_networks: params[:include_all_networks],
      third_party_instance_id: params[:third_party_instance_id]
    }.merge(params).compact
    method = :post
  else
    query = query.merge(params).compact
  end

  request(method, :client_r0, '/publicRooms', query: query, body: body)
end

#get_room_account_data(user_id, room_id, type_key, **params) ⇒ Object



980
981
982
983
984
985
986
987
988
989
# File 'lib/matrix_sdk/protocols/cs.rb', line 980

def (user_id, room_id, type_key, **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  user_id = ERB::Util.url_encode user_id.to_s
  room_id = ERB::Util.url_encode room_id.to_s
  type_key = ERB::Util.url_encode type_key.to_s

  request(:get, :client_r0, "/user/#{user_id}/rooms/#{room_id}/account_data/#{type_key}", query: query)
end

#get_room_aliases(room_id, **params) ⇒ Response

Gets a list of current aliases of a room

Examples:

Looking up aliases for a room

api.get_room_aliases('!QtykxKocfZaZOUrTwp:matrix.org')
# MatrixSdk::MatrixNotFoundError: HTTP 404 (M_NOT_FOUND): Event not found.
api.get_room_aliases('!QtykxKocfZaZOUrTwp:matrix.org', key: 'matrix.org')
# => {:aliases=>["#matrix:matrix.org"]}
api.get_room_aliases('!QtykxKocfZaZOUrTwp:matrix.org', key: 'kittenface.studio')
# => {:aliases=>["#worlddominationhq:kittenface.studio"]}

A way to find all aliases for a room

api.get_room_state('!mjbDjyNsRXndKLkHIe:matrix.org')
   .select { |ch| ch[:type] == 'm.room.aliases' }
   .map { |ch| ch[:content][:aliases] }
   .flatten
   .compact
# => ["#synapse:im.kabi.tk", "#synapse:matrix.org", "#synapse-community:matrix.org", "#synapse-ops:matrix.org", "#synops:matrix.org", ...

Parameters:

  • room_id (MXID, String)

    The room ID to look up

  • params (Hash)

    Extra options to provide to the request, see #get_room_state

Returns:

  • (Response)

    A response hash with the array :aliases

Raises:

See Also:



613
614
615
# File 'lib/matrix_sdk/protocols/cs.rb', line 613

def get_room_aliases(room_id, **params)
  get_room_state(room_id, 'm.room.aliases', params)
end

#get_room_avatar(room_id, **params) ⇒ Response

Gets the current avatar URL of a room

Parameters:

  • room_id (MXID, String)

    The room ID to look up

  • params (Hash)

    Extra options to provide to the request, see #get_room_state

Returns:

  • (Response)

    A response hash with the parameters :url and (optionally) :info

Raises:

See Also:



571
572
573
# File 'lib/matrix_sdk/protocols/cs.rb', line 571

def get_room_avatar(room_id, **params)
  get_room_state(room_id, 'm.room.avatar', params)
end

#get_room_creation_info(room_id, **params) ⇒ Response

Gets the creation configuration object for a room

Parameters:

  • room_id (MXID, String)

    The room ID to look up

  • params (Hash)

    Extra options to provide to the request, see #get_room_state

Returns:

  • (Response)

    A response hash with the configuration the room was created for

See Also:



736
737
738
# File 'lib/matrix_sdk/protocols/cs.rb', line 736

def get_room_creation_info(room_id, **params)
  get_room_state(room_id, 'm.room.create', params)
end

#get_room_encryption_settings(room_id, **params) ⇒ Response

Gets the encryption configuration for a room

Parameters:

  • room_id (MXID, String)

    The room ID to look up

  • params (Hash)

    Extra options to provide to the request, see #get_room_state

Returns:

  • (Response)

    A response hash with the configuration the room was created for

See Also:



748
749
750
# File 'lib/matrix_sdk/protocols/cs.rb', line 748

def get_room_encryption_settings(room_id, **params)
  get_room_state(room_id, 'm.room.encryption', params)
end

#get_room_guest_access(room_id, **params) ⇒ Response

Gets the guest access settings for a room

Parameters:

  • room_id (MXID, String)

    The room ID to look up

  • params (Hash)

    Extra options to provide to the request, see #get_room_state

Returns:

  • (Response)

    A response hash with the key :guest_acces, either :can_join or :forbidden

See Also:



708
709
710
# File 'lib/matrix_sdk/protocols/cs.rb', line 708

def get_room_guest_access(room_id, **params)
  get_room_state(room_id, 'm.room.guest_access', params)
end

#get_room_history_visibility(room_id, **params) ⇒ Response

Gets the history availabiilty for a room

Parameters:

  • room_id (MXID, String)

    The room ID to look up

  • params (Hash)

    Extra options to provide to the request, see #get_room_state

Returns:

  • (Response)

    A response hash with the key :history_visibility

See Also:



779
780
781
# File 'lib/matrix_sdk/protocols/cs.rb', line 779

def get_room_history_visibility(room_id, **params)
  get_room_state(room_id, 'm.room.history_visibility', params)
end

#get_room_id(room_alias, **params) ⇒ Response

Gets the room ID for an alias

Parameters:

  • room_alias (String, MXID)

    The room alias to look up

Returns:

  • (Response)

    An object containing the :room_id key and a key of :servers that know of the room

Raises:



1113
1114
1115
1116
1117
1118
1119
1120
# File 'lib/matrix_sdk/protocols/cs.rb', line 1113

def get_room_id(room_alias, **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  room_alias = ERB::Util.url_encode room_alias.to_s

  request(:get, :client_r0, "/directory/room/#{room_alias}", query: query)
end

#get_room_join_rules(room_id, **params) ⇒ Response

Gets the join rules for a room

Parameters:

  • room_id (MXID, String)

    The room ID to look up

  • params (Hash)

    Extra options to provide to the request, see #get_room_state

Returns:

  • (Response)

    A response hash with the key :join_rule

See Also:



680
681
682
# File 'lib/matrix_sdk/protocols/cs.rb', line 680

def get_room_join_rules(room_id, **params)
  get_room_state(room_id, 'm.room.join_rules', params)
end

#get_room_members(room_id, **params) ⇒ Response

Gets a list of all the members in a room

Parameters:

  • room_id (String, MXID)

    The ID of the room

Returns:

See Also:



1156
1157
1158
1159
1160
1161
1162
1163
# File 'lib/matrix_sdk/protocols/cs.rb', line 1156

def get_room_members(room_id, **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  room_id = ERB::Util.url_encode room_id.to_s

  request(:get, :client_r0, "/rooms/#{room_id}/members", query: query)
end

#get_room_messages(room_id, token, direction, limit: 10, **params) ⇒ Response

Retrieve additional messages in a room

Parameters:

  • room_id (MXID, String)

    The room ID to retrieve messages for

  • token (String)

    The token to start retrieving from, can be from a sync or from an earlier get_room_messages call

  • direction (:b, :f)

    The direction to retrieve messages

  • limit (Integer) (defaults to: 10)

    (10) The limit of messages to retrieve

  • params (Hash)

    Additional options for the request

Options Hash (**params):

  • :to (String)

    A token to limit retrieval to

  • :filter (String)

    A filter to limit the retrieval to

Returns:

  • (Response)

    A response hash with the message information containing :start, :end, and :chunk fields

See Also:



470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/matrix_sdk/protocols/cs.rb', line 470

def get_room_messages(room_id, token, direction, limit: 10, **params)
  query = {
    from: token,
    dir: direction,
    limit: limit
  }
  query[:to] = params[:to] if params.key? :to
  query[:filter] = params.fetch(:filter) if params.key? :filter
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  room_id = ERB::Util.url_encode room_id.to_s

  request(:get, :client_r0, "/rooms/#{room_id}/messages", query: query)
end

#get_room_name(room_id, **params) ⇒ Response

Gets the current display name of a room

Parameters:

  • room_id (MXID, String)

    The room ID to look up

  • params (Hash)

    Extra options to provide to the request, see #get_room_state

Returns:

  • (Response)

    A response hash with the parameter :name

Raises:

See Also:



515
516
517
# File 'lib/matrix_sdk/protocols/cs.rb', line 515

def get_room_name(room_id, **params)
  get_room_state(room_id, 'm.room.name', params)
end

#get_room_pinned_events(room_id, **params) ⇒ Response

Gets a list of pinned events in a room

Parameters:

  • room_id (MXID, String)

    The room ID to look up

  • params (Hash)

    Extra options to provide to the request, see #get_room_state

Returns:

  • (Response)

    A response hash with the array :pinned

Raises:

See Also:



626
627
628
# File 'lib/matrix_sdk/protocols/cs.rb', line 626

def get_room_pinned_events(room_id, **params)
  get_room_state(room_id, 'm.room.pinned_events', params)
end

#get_room_power_levels(room_id, **params) ⇒ Response Also known as: get_power_levels

Gets the configured power levels for a room

Parameters:

  • room_id (MXID, String)

    The room ID to look up

  • params (Hash)

    Extra options to provide to the request, see #get_room_state

Returns:

  • (Response)

    A response hash with power level information

See Also:



653
654
655
# File 'lib/matrix_sdk/protocols/cs.rb', line 653

def get_room_power_levels(room_id, **params)
  get_room_state(room_id, 'm.room.power_levels', params)
end

#get_room_server_acl(room_id, **params) ⇒ Response

Gets the server ACLs for a room

Parameters:

  • room_id (MXID, String)

    The room ID to look up

  • params (Hash)

    Extra options to provide to the request, see #get_room_state

Returns:

  • (Response)

    A response hash with server ACL information

See Also:



807
808
809
# File 'lib/matrix_sdk/protocols/cs.rb', line 807

def get_room_server_acl(room_id, **params)
  get_room_state(room_id, 'm.room.server_acl', params)
end

#get_room_state(room_id, state_type = nil, key: nil, **params) ⇒ Response

Reads the latest instance of a room state event

Parameters:

  • room_id (MXID, String)

    The room ID to read from

  • state_type (String) (defaults to: nil)

    The state type to read

Returns:

  • (Response)

    A response hash with the contents of the state event

See Also:



492
493
494
495
496
497
498
499
500
501
# File 'lib/matrix_sdk/protocols/cs.rb', line 492

def get_room_state(room_id, state_type = nil, key: nil, **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  room_id = ERB::Util.url_encode room_id.to_s
  state_type = ERB::Util.url_encode state_type.to_s
  key = ERB::Util.url_encode key.to_s

  request(:get, :client_r0, "/rooms/#{room_id}/state#{state_type.empty? ? nil : "/#{state_type}"}#{key.empty? ? nil : "/#{key}"}", query: query)
end

#get_room_topic(room_id, **params) ⇒ Response

Gets the current topic of a room

Parameters:

  • room_id (MXID, String)

    The room ID to look up

  • params (Hash)

    Extra options to provide to the request, see #get_room_state

Returns:

  • (Response)

    A response hash with the parameter :topic

Raises:

See Also:



543
544
545
# File 'lib/matrix_sdk/protocols/cs.rb', line 543

def get_room_topic(room_id, **params)
  get_room_state(room_id, 'm.room.topic', params)
end

#get_user_tags(user_id, room_id, **params) ⇒ Object



921
922
923
924
925
926
927
928
929
# File 'lib/matrix_sdk/protocols/cs.rb', line 921

def get_user_tags(user_id, room_id, **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  room_id = ERB::Util.url_encode room_id.to_s
  user_id = ERB::Util.url_encode user_id.to_s

  request(:get, :client_r0, "/user/#{user_id}/rooms/#{room_id}/tags", query: query)
end

#invite_user(room_id, user_id, **params) ⇒ Object



849
850
851
852
853
854
855
856
857
858
859
860
# File 'lib/matrix_sdk/protocols/cs.rb', line 849

def invite_user(room_id, user_id, **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  content = {
    user_id: user_id
  }

  room_id = ERB::Util.url_encode room_id.to_s

  request(:post, :client_r0, "/rooms/#{room_id}/invite", body: content, query: query)
end

#join_room(id_or_alias, **params) ⇒ Response

TODO:

Add support for 3rd-party signed objects

Joins a room

Parameters:

  • id_or_alias (MXID, String)

    The room ID or Alias to join

  • params (Hash)

    Extra room join options

Options Hash (**params):

  • :server_name (String[])

    A list of servers to perform the join through

Returns:

  • (Response)

    A response hash with the parameter :room_id

See Also:



242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/matrix_sdk/protocols/cs.rb', line 242

def join_room(id_or_alias, **params)
  query = {}
  query[:server_name] = params[:server_name] if params[:server_name]
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  # id_or_alias = MXID.new id_or_alias.to_s unless id_or_alias.is_a? MXID
  # raise ArgumentError, 'Not a room ID or alias' unless id_or_alias.room?

  id_or_alias = ERB::Util.url_encode id_or_alias.to_s

  request(:post, :client_r0, "/join/#{id_or_alias}", query: query)
end

#keys_query(timeout: nil, device_keys:, token: nil, **params) ⇒ Object

Run a query for device keys

Examples:

Looking up all the device keys for a user

api.keys_query(device_keys: { '@alice:example.com': [] })
# => { :device_keys => { :'@alice:example.com' => { :JLAFKJWSCS => { ...

Looking up a specific device for a user

api.keys_query(device_keys: { '@alice:example.com': ['ABCDEFGHIJ'] })
# => { :device_keys => { :'@alice:example.com' => { :ABCDEFGHIJ => { ...

Parameters:

  • timeout (Numeric) (defaults to: nil)

    The timeout - in seconds - for the query

  • device_keys (Array)

    The list of devices to query

  • token (String) (defaults to: nil)

    The sync token that led to this query - if any

  • params (Hash)

    Additional parameters

Options Hash (**params):

  • timeout_ms (Integer)

    The timeout in milliseconds for the query, overrides timeout

See Also:



1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
# File 'lib/matrix_sdk/protocols/cs.rb', line 1222

def keys_query(timeout: nil, device_keys:, token: nil, **params)
  body = {
    timeout: (timeout || 10) * 1000,
    device_keys: device_keys
  }
  body[:timeout] = params[:timeout_ms] if params.key? :timeout_ms
  body[:token] = token if token

  request(:post, :client_r0, '/keys/query', body: body)
end

#kick_user(room_id, user_id, reason: '', **params) ⇒ Object



862
863
864
865
866
867
868
869
870
871
872
873
# File 'lib/matrix_sdk/protocols/cs.rb', line 862

def kick_user(room_id, user_id, reason: '', **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  content = {
    user_id: user_id,
    reason: reason
  }
  room_id = ERB::Util.url_encode room_id.to_s

  request(:post, :client_r0, "/rooms/#{room_id}/kick", body: content, query: query)
end

#leave_room(room_id, **params) ⇒ Object



831
832
833
834
835
836
837
838
# File 'lib/matrix_sdk/protocols/cs.rb', line 831

def leave_room(room_id, **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  room_id = ERB::Util.url_encode room_id.to_s

  request(:post, :client_r0, "/rooms/#{room_id}/leave", query: query)
end

#login(login_type: 'm.login.password', **params) ⇒ Response

Logs in using the client API /login endpoint, and optionally stores the resulting access for API usage

Examples:

Logging in with username and password

api.(user: 'example', password: 'NotARealPass')
# => { user_id: '@example:matrix.org', access_token: '...', home_server: 'matrix.org', device_id: 'ABCD123' }
api.whoami?
# => { user_id: '@example:matrix.org' }

Advanced login, without storing details

api.whoami?
# => { user_id: '@example:matrix.org' }
api.(medium: 'email', address: '[email protected]', password: '...', store_token: false)
# => { user_id: '@someone:matrix.org', access_token: ...
api.whoami?.user_id
# => '@example:matrix.org'

Parameters:

  • login_type (String) (defaults to: 'm.login.password')

    (‘m.login.password’) The type of login to attempt

  • params (Hash)

    The login information to use, along with options for said log in

Options Hash (**params):

  • :store_token (Boolean) — default: true

    Should the resulting access token be stored for the API

  • :store_device_id (Boolean) — default: store_token value

    Should the resulting device ID be stored for the API

  • :initial_device_display_name (String) — default: USER_AGENT

    The device display name to specify for this login attempt

  • :device_id (String)

    The device ID to set on the login

Returns:

  • (Response)

    A response hash with the parameters :user_id, :access_token, :home_server, and :device_id.

See Also:



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/matrix_sdk/protocols/cs.rb', line 137

def (login_type: 'm.login.password', **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  options = {}
  options[:store_token] = params.delete(:store_token) { true }
  options[:store_device_id] = params.delete(:store_device_id) { options[:store_token] }

  data = {
    type: ,
    initial_device_display_name: params.delete(:initial_device_display_name) { MatrixSdk::Api::USER_AGENT }
  }.merge params
  data[:device_id] = device_id if device_id

  request(:post, :client_r0, '/login', body: data, query: query).tap do |resp|
    @access_token = resp.token if resp.key?(:token) && options[:store_token]
    @device_id = resp.device_id if resp.key?(:device_id) && options[:store_device_id]
  end
end

#logout(**params) ⇒ Response

Logs out the currently logged in user

Returns:

  • (Response)

    An empty response if the logout was successful

See Also:



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

def logout(**params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  request(:post, :client_r0, '/logout', query: query)
end

#media_upload(content, content_type, **params) ⇒ Object



1021
1022
1023
1024
1025
1026
# File 'lib/matrix_sdk/protocols/cs.rb', line 1021

def media_upload(content, content_type, **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  request(:post, :media_r0, '/upload', body: content, headers: { 'content-type' => content_type }, query: query)
end

#redact_event(room_id, event_id, **params) ⇒ Response

Redact an event in a room

Parameters:

  • room_id (MXID, String)

    The room ID to send the message event to

  • event_id (String)

    The event ID of the event to redact

  • params (Hash)

    Options for the request

Options Hash (**params):

  • :reason (String)

    The reason for the redaction

  • :txn_id (Integer)

    The ID of the transaction, or automatically generated

Returns:

  • (Response)

    A response hash with the parameter :event_id

See Also:



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/matrix_sdk/protocols/cs.rb', line 308

def redact_event(room_id, event_id, **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  content = {}
  content[:reason] = params[:reason] if params[:reason]

  txn_id = transaction_id
  txn_id = params.fetch(:txn_id, "#{txn_id}#{Time.now.to_i}")

  room_id = ERB::Util.url_encode room_id.to_s
  event_id = ERB::Util.url_encode event_id.to_s
  txn_id = ERB::Util.url_encode txn_id.to_s

  request(:put, :client_r0, "/rooms/#{room_id}/redact/#{event_id}/#{txn_id}", body: content, query: query)
end

#register(kind: 'user', **params) ⇒ Response

Registers a user using the client API /register endpoint

Examples:

Regular user registration and login

api.register(username: 'example', password: 'NotARealPass')
# => { user_id: '@example:matrix.org', access_token: '...', home_server: 'matrix.org', device_id: 'ABCD123' }
api.whoami?
# => { user_id: '@example:matrix.org' }

Parameters:

  • kind (String, Symbol) (defaults to: 'user')

    (‘user’) The kind of registration to use

  • params (Hash)

    The registration information, all not handled by Ruby will be passed as JSON in the body

Options Hash (**params):

  • :store_token (Boolean) — default: true

    Should the resulting access token be stored for the API

  • :store_device_id (Boolean) — default: store_token value

    Should the resulting device ID be stored for the API

Returns:

See Also:



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/matrix_sdk/protocols/cs.rb', line 85

def register(kind: 'user', **params)
  query = {}
  query[:kind] = kind
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  store_token = params.delete(:store_token) { !protocol?(:AS) }
  store_device_id = params.delete(:store_device_id) { store_token }

  request(:post, :client_r0, '/register', body: params, query: query).tap do |resp|
    @access_token = resp.token if resp.key?(:token) && store_token
    @device_id = resp.device_id if resp.key?(:device_id) && store_device_id
  end
end

#remove_room_alias(room_alias, **params) ⇒ Response

Remove an alias from its room

Parameters:

  • room_alias (String, MXID)

    The alias to remove

Returns:

  • (Response)

    An empty object denoting success



1142
1143
1144
1145
1146
1147
1148
1149
# File 'lib/matrix_sdk/protocols/cs.rb', line 1142

def remove_room_alias(room_alias, **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  room_alias = ERB::Util.url_encode room_alias.to_s

  request(:delete, :client_r0, "/directory/room/#{room_alias}", query: query)
end

#remove_user_tag(user_id, room_id, tag, **params) ⇒ Object



931
932
933
934
935
936
937
938
939
940
# File 'lib/matrix_sdk/protocols/cs.rb', line 931

def remove_user_tag(user_id, room_id, tag, **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  room_id = ERB::Util.url_encode room_id.to_s
  user_id = ERB::Util.url_encode user_id.to_s
  tag = ERB::Util.url_encode tag.to_s

  request(:delete, :client_r0, "/user/#{user_id}/rooms/#{room_id}/tags/#{tag}", query: query)
end

#send_content(room_id, url, name, msg_type, **params) ⇒ Response

Send a content message to a room

Examples:

Sending an image to a room

send_content('!abcd123:localhost',
             'mxc://localhost/1234567',
             'An image of a cat',
             'm.image',
             extra_information: {
               h: 128,
               w: 128,
               mimetype: 'image/png',
               size: 1024
             })

Sending a file to a room

send_content('!example:localhost',
             'mxc://localhost/fileurl',
             'Contract.pdf',
             'm.file',
             extra_content: {
               filename: 'contract.pdf'
             },
             extra_information: {
               mimetype: 'application/pdf',
               size: 96674
             })

Parameters:

  • room_id (MXID, String)

    The room ID to send the content to

  • url (URI, String)

    The URL to the content

  • name (String)

    The name of the content

  • msg_type (String)

    The message type of the content

  • params (Hash)

    Options for the request

Options Hash (**params):

  • :extra_information (Hash) — default: {}

    Extra information for the content

  • :extra_content (Hash)

    Extra data to insert into the content hash

Returns:

  • (Response)

    A response hash with the parameter :event_id

See Also:



366
367
368
369
370
371
372
373
374
375
376
# File 'lib/matrix_sdk/protocols/cs.rb', line 366

def send_content(room_id, url, name, msg_type, **params)
  content = {
    url: url,
    msgtype: msg_type,
    body: name,
    info: params.delete(:extra_information) { {} }
  }
  content.merge!(params.fetch(:extra_content)) if params.key? :extra_content

  send_message_event(room_id, 'm.room.message', content, params)
end

#send_emote(room_id, emote, **params) ⇒ Response

Send a plaintext emote to a room

Parameters:

  • room_id (MXID, String)

    The room ID to send the message to

  • emote (String)

    The emote to send

  • params (Hash)

    Options for the request

Options Hash (**params):

  • :msg_type (String) — default: 'm.emote'

    The message type to send

Returns:

  • (Response)

    A response hash with the parameter :event_id

See Also:



432
433
434
435
436
437
438
# File 'lib/matrix_sdk/protocols/cs.rb', line 432

def send_emote(room_id, emote, **params)
  content = {
    msgtype: params.delete(:msg_type) { 'm.emote' },
    body: emote
  }
  send_message_event(room_id, 'm.room.message', content, params)
end

#send_location(room_id, geo_uri, name, **params) ⇒ Response

Send a geographic location to a room

Parameters:

  • room_id (MXID, String)

    The room ID to send the location to

  • geo_uri (URI, String)

    The geographical URI to send

  • name (String)

    The name of the location

  • params (Hash)

    Options for the request

Options Hash (**params):

  • :extra_information (Hash) — default: {}

    Extra information for the location

  • :thumbnail_url (URI, String)

    The URL to a thumbnail of the location

  • :thumbnail_info (Hash)

    Image information about the location thumbnail

Returns:

  • (Response)

    A response hash with the parameter :event_id

See Also:



391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/matrix_sdk/protocols/cs.rb', line 391

def send_location(room_id, geo_uri, name, **params)
  content = {
    geo_uri: geo_uri,
    msgtype: 'm.location',
    body: name,
    info: params.delete(:extra_information) { {} }
  }
  content[:info][:thumbnail_url] = params.delete(:thumbnail_url) if params.key? :thumbnail_url
  content[:info][:thumbnail_info] = params.delete(:thumbnail_info) if params.key? :thumbnail_info

  send_message_event(room_id, 'm.room.message', content, params)
end

#send_message(room_id, message, **params) ⇒ Response

Send a plaintext message to a room

Parameters:

  • room_id (MXID, String)

    The room ID to send the message to

  • message (String)

    The message to send

  • params (Hash)

    Options for the request

Options Hash (**params):

  • :msg_type (String) — default: 'm.text'

    The message type to send

Returns:

  • (Response)

    A response hash with the parameter :event_id

See Also:



414
415
416
417
418
419
420
# File 'lib/matrix_sdk/protocols/cs.rb', line 414

def send_message(room_id, message, **params)
  content = {
    msgtype: params.delete(:msg_type) { 'm.text' },
    body: message
  }
  send_message_event(room_id, 'm.room.message', content, params)
end

#send_message_event(room_id, event_type, content, **params) ⇒ Response

Sends a message event to a room

Parameters:

  • room_id (MXID, String)

    The room ID to send the message event to

  • event_type (String)

    The event type of the message

  • content (Hash)

    The contents of the message

  • params (Hash)

    Options for the request

Options Hash (**params):

  • :txn_id (Integer)

    The ID of the transaction, or automatically generated

Returns:

  • (Response)

    A response hash with the parameter :event_id

See Also:



285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/matrix_sdk/protocols/cs.rb', line 285

def send_message_event(room_id, event_type, content, **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  txn_id = transaction_id
  txn_id = params.fetch(:txn_id, "#{txn_id}#{Time.now.to_i}")

  room_id = ERB::Util.url_encode room_id.to_s
  event_type = ERB::Util.url_encode event_type.to_s
  txn_id = ERB::Util.url_encode txn_id.to_s

  request(:put, :client_r0, "/rooms/#{room_id}/send/#{event_type}/#{txn_id}", body: content, query: query)
end

#send_notice(room_id, notice, **params) ⇒ Response

Send a plaintext notice to a room

Parameters:

  • room_id (MXID, String)

    The room ID to send the message to

  • notice (String)

    The notice to send

  • params (Hash)

    Options for the request

Options Hash (**params):

  • :msg_type (String) — default: 'm.notice'

    The message type to send

Returns:

  • (Response)

    A response hash with the parameter :event_id

See Also:



450
451
452
453
454
455
456
# File 'lib/matrix_sdk/protocols/cs.rb', line 450

def send_notice(room_id, notice, **params)
  content = {
    msgtype: params.delete(:msg_type) { 'm.notice' },
    body: notice
  }
  send_message_event(room_id, 'm.room.message', content, params)
end

#send_state_event(room_id, event_type, content, **params) ⇒ Response

Sends a state event to a room

Parameters:

  • room_id (MXID, String)

    The room ID to send the state event to

  • event_type (String)

    The event type to send

  • content (Hash)

    The contents of the state event

  • params (Hash)

    Options for the request

Options Hash (**params):

  • :state_key (String)

    The state key of the event, if there is one

Returns:

  • (Response)

    A response hash with the parameter :event_id

See Also:



265
266
267
268
269
270
271
272
273
274
# File 'lib/matrix_sdk/protocols/cs.rb', line 265

def send_state_event(room_id, event_type, content, **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  room_id = ERB::Util.url_encode room_id.to_s
  event_type = ERB::Util.url_encode event_type.to_s
  state_key = ERB::Util.url_encode params[:state_key].to_s if params.key? :state_key

  request(:put, :client_r0, "/rooms/#{room_id}/state/#{event_type}#{"/#{state_key}" unless state_key.nil?}", body: content, query: query)
end

#set_account_data(user_id, type_key, account_data, **params) ⇒ Object



970
971
972
973
974
975
976
977
978
# File 'lib/matrix_sdk/protocols/cs.rb', line 970

def (user_id, type_key, , **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  user_id = ERB::Util.url_encode user_id.to_s
  type_key = ERB::Util.url_encode type_key.to_s

  request(:put, :client_r0, "/user/#{user_id}/account_data/#{type_key}", body: , query: query)
end

#set_avatar_url(user_id, url, **params) ⇒ Object



1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
# File 'lib/matrix_sdk/protocols/cs.rb', line 1059

def set_avatar_url(user_id, url, **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  content = {
    avatar_url: url
  }

  user_id = ERB::Util.url_encode user_id.to_s

  request(:put, :client_r0, "/profile/#{user_id}/avatar_url", body: content, query: query)
end

#set_device(device_id, display_name:) ⇒ Object

Sets the metadata for a device

Parameters:

  • device_id (String)

    The ID of the device to modify

  • display_name (String)

    The new display name to set for the device

See Also:



1189
1190
1191
1192
1193
# File 'lib/matrix_sdk/protocols/cs.rb', line 1189

def set_device(device_id, display_name:)
  device_id = ERB::Util.url_encode device_id.to_s

  request(:put, :client_r0, "/devices/#{device_id}", body: { display_name: display_name })
end

#set_display_name(user_id, display_name, **params) ⇒ Object



1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
# File 'lib/matrix_sdk/protocols/cs.rb', line 1037

def set_display_name(user_id, display_name, **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  content = {
    displayname: display_name
  }

  user_id = ERB::Util.url_encode user_id.to_s

  request(:put, :client_r0, "/profile/#{user_id}/displayname", body: content, query: query)
end

#set_membership(room_id, user_id, membership, reason: '', **params) ⇒ Object



885
886
887
888
889
890
891
892
893
894
# File 'lib/matrix_sdk/protocols/cs.rb', line 885

def set_membership(room_id, user_id, membership, reason: '', **params)
  content = {
    membership: membership,
    reason: reason
  }
  content[:displayname] = params.delete(:displayname) if params.key? :displayname
  content[:avatar_url] = params.delete(:avatar_url) if params.key? :avatar_url

  send_state_event(room_id, 'm.room.member', content, params.merge(state_key: user_id))
end

#set_room_account_data(user_id, room_id, type_key, account_data, **params) ⇒ Object



991
992
993
994
995
996
997
998
999
1000
# File 'lib/matrix_sdk/protocols/cs.rb', line 991

def (user_id, room_id, type_key, , **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  user_id = ERB::Util.url_encode user_id.to_s
  room_id = ERB::Util.url_encode room_id.to_s
  type_key = ERB::Util.url_encode type_key.to_s

  request(:put, :client_r0, "/user/#{user_id}/rooms/#{room_id}/account_data/#{type_key}", body: , query: query)
end

#set_room_alias(room_id, room_alias, **params) ⇒ Response

Sets the room ID for an alias

Parameters:

  • room_id (String, MXID)

    The room to set an alias for

  • room_alias (String, MXID)

    The alias to configure for the room

Returns:

  • (Response)

    An empty object denoting success

Raises:



1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
# File 'lib/matrix_sdk/protocols/cs.rb', line 1127

def set_room_alias(room_id, room_alias, **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  content = {
    room_id: room_id
  }
  room_alias = ERB::Util.url_encode room_alias.to_s

  request(:put, :client_r0, "/directory/room/#{room_alias}", body: content, query: query)
end

#set_room_avatar(room_id, url, **params) ⇒ Response

Sets the avatar URL for a room

Parameters:

  • room_id (MXID, String)

    The room ID to work on

  • url (String, URI)

    The new avatar URL for the room

  • params (Hash)

    Extra options to set on the request, see #send_state_event

Returns:

  • (Response)

    The resulting state event

See Also:



583
584
585
586
587
588
# File 'lib/matrix_sdk/protocols/cs.rb', line 583

def set_room_avatar(room_id, url, **params)
  content = {
    url: url
  }
  send_state_event(room_id, 'm.room.avatar', content, params)
end

#set_room_encryption_settings(room_id, algorithm: 'm.megolm.v1.aes-sha2', rotation_period_ms: 1 * 7 * 24 * 60 * 60 * 1000, rotation_period_msgs: 100, **params) ⇒ Response

Sets the encryption configuration for a room

Parameters:

  • room_id (MXID, String)

    The room ID to work on

  • algorithm ('m.megolm.v1.aes-sha2') (defaults to: 'm.megolm.v1.aes-sha2')

    The encryption algorithm to use

  • rotation_period_ms (Integer) (defaults to: 1 * 7 * 24 * 60 * 60 * 1000)

    The interval between key rotation in milliseconds

  • rotation_period_msgs (Integer) (defaults to: 100)

    The interval between key rotation in messages

  • params (Hash)

    Extra options to set on the request, see #send_state_event

Returns:

  • (Response)

    The resulting state event

See Also:



762
763
764
765
766
767
768
769
# File 'lib/matrix_sdk/protocols/cs.rb', line 762

def set_room_encryption_settings(room_id, algorithm: 'm.megolm.v1.aes-sha2', rotation_period_ms: 1 * 7 * 24 * 60 * 60 * 1000, rotation_period_msgs: 100, **params)
  content = {
    algorithm: algorithm,
    rotation_period_ms: rotation_period_ms,
    rotation_period_msgs: rotation_period_msgs
  }
  send_state_event(room_id, 'm.room.encryption', content, params)
end

#set_room_guest_access(room_id, guest_access, **params) ⇒ Response

Sets the guest access settings for a room

Parameters:

  • room_id (MXID, String)

    The room ID to work on

  • guest_access (:can_join, :forbidden)

    The new guest access setting for the room

  • params (Hash)

    Extra options to set on the request, see #send_state_event

Returns:

  • (Response)

    The resulting state event

See Also:



720
721
722
723
724
725
726
# File 'lib/matrix_sdk/protocols/cs.rb', line 720

def set_room_guest_access(room_id, guest_access, **params)
  content = {
    guest_access: guest_access
  }

  send_state_event(room_id, 'm.room.guest_access', content, params)
end

#set_room_history_visibility(room_id, visibility, **params) ⇒ Response

Sets the history availability for a room

Parameters:

  • room_id (MXID, String)

    The room ID to work on

  • visibility (:invited, :joined, :shared, :world_readable)

    The new history visibility level

  • params (Hash)

    Extra options to set on the request, see #send_state_event

Returns:

  • (Response)

    The resulting state event

See Also:



791
792
793
794
795
796
797
# File 'lib/matrix_sdk/protocols/cs.rb', line 791

def set_room_history_visibility(room_id, visibility, **params)
  content = {
    history_visibility: visibility
  }

  send_state_event(room_id, 'm.room.history_visibility', content, params)
end

#set_room_join_rules(room_id, join_rule, **params) ⇒ Response

Sets the join rules for a room

Parameters:

  • room_id (MXID, String)

    The room ID to work on

  • join_rule (String, Symbol)

    The new join rule setting (Currently only public and invite are implemented)

  • params (Hash)

    Extra options to set on the request, see #send_state_event

Returns:

  • (Response)

    The resulting state event

See Also:



692
693
694
695
696
697
698
# File 'lib/matrix_sdk/protocols/cs.rb', line 692

def set_room_join_rules(room_id, join_rule, **params)
  content = {
    join_rule: join_rule
  }

  send_state_event(room_id, 'm.room.join_rules', content, params)
end

#set_room_name(room_id, name, **params) ⇒ Response

Sets the display name of a room

Parameters:

  • room_id (MXID, String)

    The room ID to work on

  • name (String)

    The new name of the room

  • params (Hash)

    Extra options to set on the request, see #send_state_event

Returns:

  • (Response)

    The resulting state event

See Also:



527
528
529
530
531
532
# File 'lib/matrix_sdk/protocols/cs.rb', line 527

def set_room_name(room_id, name, **params)
  content = {
    name: name
  }
  send_state_event(room_id, 'm.room.name', content, params)
end

#set_room_pinned_events(room_id, events, **params) ⇒ Response

Sets the list of pinned events in a room

Parameters:

  • room_id (MXID, String)

    The room ID to work on

  • events (Array[String])

    The new list of events to set as pinned

  • params (Hash)

    Extra options to set on the request, see #send_state_event

Returns:

  • (Response)

    The resulting state event

See Also:



638
639
640
641
642
643
# File 'lib/matrix_sdk/protocols/cs.rb', line 638

def set_room_pinned_events(room_id, events, **params)
  content = {
    pinned: events
  }
  send_state_event(room_id, 'm.room.pinned_events', content, params)
end

#set_room_power_levels(room_id, content, **params) ⇒ Response Also known as: set_power_levels

Sets the configuration for power levels in a room

Parameters:

  • room_id (MXID, String)

    The room ID to work on

  • content (Hash)

    The new power level configuration

  • params (Hash)

    Extra options to set on the request, see #send_state_event

Returns:

  • (Response)

    The resulting state event

See Also:



666
667
668
669
# File 'lib/matrix_sdk/protocols/cs.rb', line 666

def set_room_power_levels(room_id, content, **params)
  content[:events] = {} unless content.key? :events
  send_state_event(room_id, 'm.room.power_levels', content, params)
end

#set_room_server_acl(room_id, allow_ip_literals: false, allow:, deny:, **params) ⇒ Response

Sets the server ACL configuration for a room

Parameters:

  • room_id (MXID, String)

    The room ID to work on

  • allow_ip_literals (Boolean) (defaults to: false)

    If HSes with literal IP domains should be allowed

  • allow (Array[String])

    A list of HS wildcards that are allowed to communicate with the room

  • deny (Array[String])

    A list of HS wildcards that are denied from communicating with the room

  • params (Hash)

    Extra options to set on the request, see #send_state_event

Returns:

  • (Response)

    The resulting state event

See Also:



821
822
823
824
825
826
827
828
829
# File 'lib/matrix_sdk/protocols/cs.rb', line 821

def set_room_server_acl(room_id, allow_ip_literals: false, allow:, deny:, **params)
  content = {
    allow_ip_literals: allow_ip_literals,
    allow: allow,
    deny: deny
  }

  send_state_event(room_id, 'm.room.server_acl', content, params)
end

#set_room_topic(room_id, topic, **params) ⇒ Response

Sets the topic of a room

Parameters:

  • room_id (MXID, String)

    The room ID to work on

  • topic (String)

    The new topic of the room

  • params (Hash)

    Extra options to set on the request, see #send_state_event

Returns:

  • (Response)

    The resulting state event

See Also:



555
556
557
558
559
560
# File 'lib/matrix_sdk/protocols/cs.rb', line 555

def set_room_topic(room_id, topic, **params)
  content = {
    topic: topic
  }
  send_state_event(room_id, 'm.room.topic', content, params)
end

#sync(timeout: 30.0, **params) ⇒ Response

Runs the client API /sync method

Parameters:

  • timeout (Numeric) (defaults to: 30.0)

    (30.0) The timeout in seconds for the sync

  • params (Hash)

    The sync options to use

Options Hash (**params):

  • :since (String)

    The value of the batch token to base the sync from

  • :filter (String, Hash)

    The filter to use on the sync

  • :full_state (Boolean)

    Should the sync include the full state

  • :set_presence (Boolean)

    Should the sync set the user status to online

Returns:

See Also:



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/matrix_sdk/protocols/cs.rb', line 58

def sync(timeout: 30.0, **params)
  query = params.select do |k, _v|
    %i[since filter full_state set_presence].include? k
  end

  query[:timeout] = (timeout * 1000).to_i if timeout
  query[:timeout] = params.delete(:timeout_ms).to_i if params.key? :timeout_ms
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  request(:get, :client_r0, '/sync', query: query)
end

#unban_user(room_id, user_id, **params) ⇒ Object



909
910
911
912
913
914
915
916
917
918
919
# File 'lib/matrix_sdk/protocols/cs.rb', line 909

def unban_user(room_id, user_id, **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  content = {
    user_id: user_id
  }
  room_id = ERB::Util.url_encode room_id.to_s

  request(:post, :client_r0, "/rooms/#{room_id}/unban", body: content, query: query)
end

#username_available?(username) ⇒ Response

Checks if a given username is available and valid for registering

Examples:

Verifying a username

api.username_available?('example')
# => { available: true }

Parameters:

  • username (String)

    The username to check

Returns:

See Also:



108
109
110
# File 'lib/matrix_sdk/protocols/cs.rb', line 108

def username_available?(username)
  request(:get, :client_r0, '/register/available', query: { username: username })
end

#whoami?(**params) ⇒ Response

Gets the MXID of the currently logged-in user

Returns:

  • (Response)

    An object containing the key :user_id



1235
1236
1237
1238
1239
1240
# File 'lib/matrix_sdk/protocols/cs.rb', line 1235

def whoami?(**params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  request(:get, :client_r0, '/account/whoami', query: query)
end