matrix_sdk 2.2.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4f16ccd1ed10b02a963b9dbd736e577375ca41b26fb45743bc0344f219b1d70f
4
- data.tar.gz: 3d8eef54f71ccf9e04c0da896e6189fd8d4c2c34c269e2890de37dd4cec023a6
3
+ metadata.gz: fb2cd8492e977a8c63d80d3a250841111f872b45ef48a06ac610dd6c871c5704
4
+ data.tar.gz: b5932dc63938f86a4bec522f2d5736871f3a5461ad5d25581c2ba1cd79b20ab2
5
5
  SHA512:
6
- metadata.gz: 01de0cccbcf0724a101526190ea4e164ec1235daaa0e0c3d42ed445e760246b2b81a054ba4bd7e88229fdb0833e4fe3a8624a4de3dfd032bfa50b4f23946caf1
7
- data.tar.gz: 1b177862e3b01aef4c10de6a4318b8348a5c6e00885f0b137ce13a269bf0d5579e6a72b93a87a9366b8d75ec2549518a7d21f2da5db4d7f1d60fa464ff11d83b
6
+ metadata.gz: 0ed57a7d4b3296a15c244c88ca88777d6ed3f9b67a6c4cdaae017f56c6e15cf94ba0781c6831a70f7adf9c55605073c84c451b0c1b0b8bffe323015330294f19
7
+ data.tar.gz: f77aaa05a45917b80aa296b7ff73f37d60ba5fb36f29a3e753cec1a730681a428c33ccdae9f4c26648952d6a7b05f9636e7d7ffbbe8757d23d2c435378d3466b
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## 2.3.0 - 2021-03-26
2
+
3
+ - Adds support for Ruby 3.0 (#15)
4
+ - Adds support for requests against the Synapse admin API
5
+ - Adds helper methods for checking and changing user power levels
6
+ - Adds a proper caching system for room data
7
+ - Fixes argument error in `#get_room_messages`
8
+ - Removes unfinished and broken AS abstraction
9
+
1
10
  ## 2.2.0 - 2020-11-20
2
11
 
3
12
  - Adds direct message (1:1) room mapping to client abstraction
data/README.md CHANGED
@@ -4,7 +4,7 @@ A Ruby gem for easing the development of software that communicates with servers
4
4
 
5
5
  There is a Matrix room for the discussion about usage and development at [#ruby-matrix-sdk:kittenface.studio](https://matrix.to/#/#ruby-matrix-sdk:kittenface.studio).
6
6
 
7
- Live YARD documentation can be found at; http://aleol57.gitlab-pages.liu.se/ruby-matrix-sdk
7
+ Live YARD documentation can be found at; https://ruby-sdk.ananace.dev
8
8
 
9
9
  ## Example usage
10
10
 
data/lib/matrix_sdk.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'matrix_sdk/extensions'
3
+ require 'matrix_sdk/util/extensions'
4
4
  require 'matrix_sdk/version'
5
5
 
6
6
  require 'json'
@@ -22,6 +22,11 @@ module MatrixSdk
22
22
  autoload :MatrixTimeoutError, 'matrix_sdk/errors'
23
23
  autoload :MatrixUnexpectedResponseError, 'matrix_sdk/errors'
24
24
 
25
+ module Util
26
+ autoload :Tinycache, 'matrix_sdk/util/tinycache'
27
+ autoload :TinycacheAdapter, 'matrix_sdk/util/tinycache_adapter'
28
+ end
29
+
25
30
  module Protocols
26
31
  autoload :AS, 'matrix_sdk/protocols/as'
27
32
  autoload :CS, 'matrix_sdk/protocols/cs'
@@ -38,6 +38,7 @@ module MatrixSdk
38
38
  # @option params [Numeric] :read_timeout (240) The timeout in seconds for reading responses
39
39
  # @option params [Hash] :global_headers Additional headers to set for all requests
40
40
  # @option params [Boolean] :skip_login Should the API skip logging in if the HS URL contains user information
41
+ # @option params [Boolean] :synapse (true) Is the API connecting to a Synapse instance
41
42
  # @option params [Hash] :well_known The .well-known object that the server was discovered through, should not be set manually
42
43
  def initialize(homeserver, **params)
43
44
  @homeserver = homeserver
@@ -61,6 +62,7 @@ module MatrixSdk
61
62
  @well_known = params.fetch(:well_known, {})
62
63
  @global_headers = DEFAULT_HEADERS.dup
63
64
  @global_headers.merge!(params.fetch(:global_headers)) if params.key? :global_headers
65
+ @synapse = params.fetch(:synapse, true)
64
66
  @http = nil
65
67
 
66
68
  ([params.fetch(:protocols, [:CS])].flatten - protocols).each do |proto|
@@ -161,11 +163,13 @@ module MatrixSdk
161
163
 
162
164
  params[:well_known] = well_known if keep_wellknown
163
165
 
164
- new(uri,
165
- params.merge(
166
- address: target_uri.host,
167
- port: target_uri.port
168
- ))
166
+ new(
167
+ uri,
168
+ **params.merge(
169
+ address: target_uri.host,
170
+ port: target_uri.port
171
+ )
172
+ )
169
173
  end
170
174
 
171
175
  # Get a list of enabled protocols on the API client
@@ -377,6 +381,8 @@ module MatrixSdk
377
381
  end
378
382
 
379
383
  def api_to_path(api)
384
+ return "/_synapse/#{api.to_s.split('_').join('/')}" if @synapse && api.to_s.start_with?('admin_')
385
+
380
386
  # TODO: <api>_current / <api>_latest
381
387
  "/_matrix/#{api.to_s.split('_').join('/')}"
382
388
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'matrix_sdk'
4
+ require 'matrix_sdk/util/events'
4
5
 
5
6
  require 'English'
6
7
  require 'forwardable'
@@ -67,7 +68,7 @@ module MatrixSdk
67
68
  api.instance_variable_set("@#{k}", v) if api.instance_variable_defined? "@#{k}"
68
69
  end
69
70
  else
70
- @api = Api.new hs_url, params
71
+ @api = Api.new hs_url, **params
71
72
  end
72
73
 
73
74
  @cache = client_cache
@@ -360,7 +361,7 @@ module MatrixSdk
360
361
  # @return [Room] The resulting room
361
362
  # @see Protocols::CS#create_room
362
363
  def create_room(room_alias = nil, **params)
363
- data = api.create_room(params.merge(room_alias: room_alias))
364
+ data = api.create_room(**params.merge(room_alias: room_alias))
364
365
  ensure_room(data.room_id)
365
366
  end
366
367
 
@@ -469,7 +470,7 @@ module MatrixSdk
469
470
 
470
471
  @should_listen = cancel_token
471
472
  else
472
- thread = Thread.new { listen_forever(params) }
473
+ thread = Thread.new { listen_forever(**params) }
473
474
  end
474
475
  @sync_thread = thread
475
476
  thread.run
@@ -518,7 +519,7 @@ module MatrixSdk
518
519
  attempts = 0
519
520
  data = loop do
520
521
  begin
521
- break api.sync extra_params
522
+ break api.sync **extra_params
522
523
  rescue MatrixSdk::MatrixTimeoutError => e
523
524
  raise e if (attempts += 1) >= params.fetch(:allow_sync_retry, 0)
524
525
  end
@@ -552,7 +553,7 @@ module MatrixSdk
552
553
  orig_bad_sync_timeout = bad_sync_timeout + 0
553
554
  while @should_listen
554
555
  begin
555
- sync(params.merge(timeout: timeout))
556
+ sync(**params.merge(timeout: timeout))
556
557
 
557
558
  bad_sync_timeout = orig_bad_sync_timeout
558
559
  sleep(sync_interval) if sync_interval.positive?
@@ -588,33 +589,6 @@ module MatrixSdk
588
589
 
589
590
  room = ensure_room(room_id)
590
591
  room.send :put_state_event, state_event
591
- content = state_event[:content]
592
- case state_event[:type]
593
- when 'm.room.name'
594
- room.instance_variable_set '@name', content[:name]
595
- when 'm.room.canonical_alias'
596
- room.instance_variable_set '@canonical_alias', content[:alias]
597
- # Also add as a regular alias
598
- room.instance_variable_get('@aliases').concat [content[:alias]]
599
- when 'm.room.topic'
600
- room.instance_variable_set '@topic', content[:topic]
601
- when 'm.room.aliases'
602
- room.instance_variable_get('@aliases').concat content[:aliases]
603
- when 'm.room.join_rules'
604
- room.instance_variable_set '@join_rule', content[:join_rule].nil? ? nil : content[:join_rule].to_sym
605
- when 'm.room.guest_access'
606
- room.instance_variable_set '@guest_access', content[:guest_access].nil? ? nil : content[:guest_access].to_sym
607
- when 'm.room.member'
608
- return unless cache == :all
609
-
610
- if content[:membership] == 'join'
611
- room.send(:ensure_member, get_user(state_event[:state_key]).dup.tap do |u|
612
- u.instance_variable_set :@display_name, content[:displayname]
613
- end)
614
- elsif %w[leave kick invite].include? content[:membership]
615
- room.members.delete_if { |m| m.id == state_event[:state_key] }
616
- end
617
- end
618
592
  end
619
593
 
620
594
  def handle_sync_response(data)
@@ -663,6 +637,14 @@ module MatrixSdk
663
637
  end
664
638
  end
665
639
 
640
+ unless cache == :none
641
+ @rooms.each do |_id, room|
642
+ # Clean up old cache data after every sync
643
+ # TODO Run this in a thread?
644
+ room.tinycache_adapter.cleanup
645
+ end
646
+ end
647
+
666
648
  nil
667
649
  end
668
650
  end
@@ -41,9 +41,13 @@ module MatrixSdk
41
41
  end
42
42
 
43
43
  class MatrixNotAuthorizedError < MatrixRequestError; end
44
+
44
45
  class MatrixForbiddenError < MatrixRequestError; end
46
+
45
47
  class MatrixNotFoundError < MatrixRequestError; end
48
+
46
49
  class MatrixConflictError < MatrixRequestError; end
50
+
47
51
  class MatrixTooManyRequestsError < MatrixRequestError; end
48
52
 
49
53
  # An error raised when errors occur in the connection layer
@@ -584,7 +584,7 @@ module MatrixSdk::Protocols::CS
584
584
  }
585
585
  content.merge!(params.fetch(:extra_content)) if params.key? :extra_content
586
586
 
587
- send_message_event(room_id, 'm.room.message', content, params)
587
+ send_message_event(room_id, 'm.room.message', content, **params)
588
588
  end
589
589
 
590
590
  # Send a geographic location to a room
@@ -610,7 +610,7 @@ module MatrixSdk::Protocols::CS
610
610
  content[:info][:thumbnail_url] = params.delete(:thumbnail_url) if params.key? :thumbnail_url
611
611
  content[:info][:thumbnail_info] = params.delete(:thumbnail_info) if params.key? :thumbnail_info
612
612
 
613
- send_message_event(room_id, 'm.room.message', content, params)
613
+ send_message_event(room_id, 'm.room.message', content, **params)
614
614
  end
615
615
 
616
616
  # Send a plaintext message to a room
@@ -628,7 +628,7 @@ module MatrixSdk::Protocols::CS
628
628
  msgtype: params.delete(:msg_type) { 'm.text' },
629
629
  body: message
630
630
  }
631
- send_message_event(room_id, 'm.room.message', content, params)
631
+ send_message_event(room_id, 'm.room.message', content, **params)
632
632
  end
633
633
 
634
634
  # Send a plaintext emote to a room
@@ -646,7 +646,7 @@ module MatrixSdk::Protocols::CS
646
646
  msgtype: params.delete(:msg_type) { 'm.emote' },
647
647
  body: emote
648
648
  }
649
- send_message_event(room_id, 'm.room.message', content, params)
649
+ send_message_event(room_id, 'm.room.message', content, **params)
650
650
  end
651
651
 
652
652
  # Send a plaintext notice to a room
@@ -664,7 +664,7 @@ module MatrixSdk::Protocols::CS
664
664
  msgtype: params.delete(:msg_type) { 'm.notice' },
665
665
  body: notice
666
666
  }
667
- send_message_event(room_id, 'm.room.message', content, params)
667
+ send_message_event(room_id, 'm.room.message', content, **params)
668
668
  end
669
669
 
670
670
  # Report an event in a room
@@ -702,7 +702,7 @@ module MatrixSdk::Protocols::CS
702
702
  # @return [Response] A response hash with the message information containing :start, :end, and :chunk fields
703
703
  # @see https://matrix.org/docs/spec/client_server/latest.html#get-matrix-client-r0-rooms-roomid-messages
704
704
  # The Matrix Spec, for more information about the call and response
705
- def get_room_messages(room_id, token, direction, limit: 10, **params)
705
+ def get_room_messages(room_id, token, direction:, limit: 10, **params)
706
706
  query = {
707
707
  from: token,
708
708
  dir: direction,
@@ -804,7 +804,7 @@ module MatrixSdk::Protocols::CS
804
804
  # @see https://matrix.org/docs/spec/client_server/latest.html#m-room-name
805
805
  # The Matrix Spec, for more information about the event and data
806
806
  def get_room_name(room_id, **params)
807
- get_room_state(room_id, 'm.room.name', params)
807
+ get_room_state(room_id, 'm.room.name', **params)
808
808
  end
809
809
 
810
810
  # Sets the display name of a room
@@ -819,7 +819,7 @@ module MatrixSdk::Protocols::CS
819
819
  content = {
820
820
  name: name
821
821
  }
822
- send_state_event(room_id, 'm.room.name', content, params)
822
+ send_state_event(room_id, 'm.room.name', content, **params)
823
823
  end
824
824
 
825
825
  # Gets the current topic of a room
@@ -832,7 +832,7 @@ module MatrixSdk::Protocols::CS
832
832
  # @see https://matrix.org/docs/spec/client_server/latest.html#m-room-topic
833
833
  # The Matrix Spec, for more information about the event and data
834
834
  def get_room_topic(room_id, **params)
835
- get_room_state(room_id, 'm.room.topic', params)
835
+ get_room_state(room_id, 'm.room.topic', **params)
836
836
  end
837
837
 
838
838
  # Sets the topic of a room
@@ -847,7 +847,7 @@ module MatrixSdk::Protocols::CS
847
847
  content = {
848
848
  topic: topic
849
849
  }
850
- send_state_event(room_id, 'm.room.topic', content, params)
850
+ send_state_event(room_id, 'm.room.topic', content, **params)
851
851
  end
852
852
 
853
853
  # Gets the current avatar URL of a room
@@ -860,7 +860,7 @@ module MatrixSdk::Protocols::CS
860
860
  # @see https://matrix.org/docs/spec/client_server/latest.html#m-room-avatar
861
861
  # The Matrix Spec, for more information about the event and data
862
862
  def get_room_avatar(room_id, **params)
863
- get_room_state(room_id, 'm.room.avatar', params)
863
+ get_room_state(room_id, 'm.room.avatar', **params)
864
864
  end
865
865
 
866
866
  # Sets the avatar URL for a room
@@ -875,7 +875,7 @@ module MatrixSdk::Protocols::CS
875
875
  content = {
876
876
  url: url
877
877
  }
878
- send_state_event(room_id, 'm.room.avatar', content, params)
878
+ send_state_event(room_id, 'm.room.avatar', content, **params)
879
879
  end
880
880
 
881
881
  # Gets a list of current aliases of a room
@@ -902,7 +902,7 @@ module MatrixSdk::Protocols::CS
902
902
  # .compact
903
903
  # # => ["#synapse:im.kabi.tk", "#synapse:matrix.org", "#synapse-community:matrix.org", "#synapse-ops:matrix.org", "#synops:matrix.org", ...
904
904
  def get_room_aliases(room_id, **params)
905
- get_room_state(room_id, 'm.room.aliases', params)
905
+ get_room_state(room_id, 'm.room.aliases', **params)
906
906
  end
907
907
 
908
908
  # Gets a list of pinned events in a room
@@ -915,7 +915,7 @@ module MatrixSdk::Protocols::CS
915
915
  # @see https://matrix.org/docs/spec/client_server/latest.html#m-room-pinned-events
916
916
  # The Matrix Spec, for more information about the event and data
917
917
  def get_room_pinned_events(room_id, **params)
918
- get_room_state(room_id, 'm.room.pinned_events', params)
918
+ get_room_state(room_id, 'm.room.pinned_events', **params)
919
919
  end
920
920
 
921
921
  # Sets the list of pinned events in a room
@@ -930,7 +930,7 @@ module MatrixSdk::Protocols::CS
930
930
  content = {
931
931
  pinned: events
932
932
  }
933
- send_state_event(room_id, 'm.room.pinned_events', content, params)
933
+ send_state_event(room_id, 'm.room.pinned_events', content, **params)
934
934
  end
935
935
 
936
936
  # Gets the configured power levels for a room
@@ -942,7 +942,7 @@ module MatrixSdk::Protocols::CS
942
942
  # @see https://matrix.org/docs/spec/client_server/latest.html#m-room-power-levels
943
943
  # The Matrix Spec, for more information about the event and data
944
944
  def get_room_power_levels(room_id, **params)
945
- get_room_state(room_id, 'm.room.power_levels', params)
945
+ get_room_state(room_id, 'm.room.power_levels', **params)
946
946
  end
947
947
  alias get_power_levels get_room_power_levels
948
948
 
@@ -956,7 +956,7 @@ module MatrixSdk::Protocols::CS
956
956
  # The Matrix Spec, for more information about the event and data
957
957
  def set_room_power_levels(room_id, content, **params)
958
958
  content[:events] = {} unless content.key? :events
959
- send_state_event(room_id, 'm.room.power_levels', content, params)
959
+ send_state_event(room_id, 'm.room.power_levels', content, **params)
960
960
  end
961
961
  alias set_power_levels set_room_power_levels
962
962
 
@@ -969,7 +969,7 @@ module MatrixSdk::Protocols::CS
969
969
  # @see https://matrix.org/docs/spec/client_server/latest.html#m-room-join-rules
970
970
  # The Matrix Spec, for more information about the event and data
971
971
  def get_room_join_rules(room_id, **params)
972
- get_room_state(room_id, 'm.room.join_rules', params)
972
+ get_room_state(room_id, 'm.room.join_rules', **params)
973
973
  end
974
974
 
975
975
  # Sets the join rules for a room
@@ -985,7 +985,7 @@ module MatrixSdk::Protocols::CS
985
985
  join_rule: join_rule
986
986
  }
987
987
 
988
- send_state_event(room_id, 'm.room.join_rules', content, params)
988
+ send_state_event(room_id, 'm.room.join_rules', content, **params)
989
989
  end
990
990
 
991
991
  # Gets the guest access settings for a room
@@ -997,7 +997,7 @@ module MatrixSdk::Protocols::CS
997
997
  # @see https://matrix.org/docs/spec/client_server/latest.html#m-room-guest-access
998
998
  # The Matrix Spec, for more information about the event and data
999
999
  def get_room_guest_access(room_id, **params)
1000
- get_room_state(room_id, 'm.room.guest_access', params)
1000
+ get_room_state(room_id, 'm.room.guest_access', **params)
1001
1001
  end
1002
1002
 
1003
1003
  # Sets the guest access settings for a room
@@ -1013,7 +1013,7 @@ module MatrixSdk::Protocols::CS
1013
1013
  guest_access: guest_access
1014
1014
  }
1015
1015
 
1016
- send_state_event(room_id, 'm.room.guest_access', content, params)
1016
+ send_state_event(room_id, 'm.room.guest_access', content, **params)
1017
1017
  end
1018
1018
 
1019
1019
  # Gets the creation configuration object for a room
@@ -1025,7 +1025,7 @@ module MatrixSdk::Protocols::CS
1025
1025
  # @see https://matrix.org/docs/spec/client_server/latest.html#m-room-create
1026
1026
  # The Matrix Spec, for more information about the event and data
1027
1027
  def get_room_creation_info(room_id, **params)
1028
- get_room_state(room_id, 'm.room.create', params)
1028
+ get_room_state(room_id, 'm.room.create', **params)
1029
1029
  end
1030
1030
 
1031
1031
  # Gets the encryption configuration for a room
@@ -1037,7 +1037,7 @@ module MatrixSdk::Protocols::CS
1037
1037
  # @see https://matrix.org/docs/spec/client_server/latest.html#m-room-encryption
1038
1038
  # The Matrix Spec, for more information about the event and data
1039
1039
  def get_room_encryption_settings(room_id, **params)
1040
- get_room_state(room_id, 'm.room.encryption', params)
1040
+ get_room_state(room_id, 'm.room.encryption', **params)
1041
1041
  end
1042
1042
 
1043
1043
  # Sets the encryption configuration for a room
@@ -1056,7 +1056,7 @@ module MatrixSdk::Protocols::CS
1056
1056
  rotation_period_ms: rotation_period_ms,
1057
1057
  rotation_period_msgs: rotation_period_msgs
1058
1058
  }
1059
- send_state_event(room_id, 'm.room.encryption', content, params)
1059
+ send_state_event(room_id, 'm.room.encryption', content, **params)
1060
1060
  end
1061
1061
 
1062
1062
  # Gets the history availabiilty for a room
@@ -1068,7 +1068,7 @@ module MatrixSdk::Protocols::CS
1068
1068
  # @see https://matrix.org/docs/spec/client_server/latest.html#m-room-history-visibility
1069
1069
  # The Matrix Spec, for more information about the event and data
1070
1070
  def get_room_history_visibility(room_id, **params)
1071
- get_room_state(room_id, 'm.room.history_visibility', params)
1071
+ get_room_state(room_id, 'm.room.history_visibility', **params)
1072
1072
  end
1073
1073
 
1074
1074
  # Sets the history availability for a room
@@ -1084,7 +1084,7 @@ module MatrixSdk::Protocols::CS
1084
1084
  history_visibility: visibility
1085
1085
  }
1086
1086
 
1087
- send_state_event(room_id, 'm.room.history_visibility', content, params)
1087
+ send_state_event(room_id, 'm.room.history_visibility', content, **params)
1088
1088
  end
1089
1089
 
1090
1090
  # Gets the server ACLs for a room
@@ -1096,7 +1096,7 @@ module MatrixSdk::Protocols::CS
1096
1096
  # @see https://matrix.org/docs/spec/client_server/latest.html#m-room-server-acl
1097
1097
  # The Matrix Spec, for more information about the event and data
1098
1098
  def get_room_server_acl(room_id, **params)
1099
- get_room_state(room_id, 'm.room.server_acl', params)
1099
+ get_room_state(room_id, 'm.room.server_acl', **params)
1100
1100
  end
1101
1101
 
1102
1102
  # Sets the server ACL configuration for a room
@@ -1116,7 +1116,7 @@ module MatrixSdk::Protocols::CS
1116
1116
  deny: deny
1117
1117
  }
1118
1118
 
1119
- send_state_event(room_id, 'm.room.server_acl', content, params)
1119
+ send_state_event(room_id, 'm.room.server_acl', content, **params)
1120
1120
  end
1121
1121
 
1122
1122
  def leave_room(room_id, **params)