matrix_sdk 2.7.0 → 2.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/lib/matrix_sdk/bot/base.rb +5 -1
- data/lib/matrix_sdk/client.rb +6 -3
- data/lib/matrix_sdk/mxid.rb +1 -1
- data/lib/matrix_sdk/protocols/cs.rb +2 -0
- data/lib/matrix_sdk/room.rb +64 -67
- data/lib/matrix_sdk/util/state_event_cache.rb +92 -0
- data/lib/matrix_sdk/util/tinycache.rb +4 -10
- data/lib/matrix_sdk/util/tinycache_adapter.rb +6 -0
- data/lib/matrix_sdk/version.rb +1 -1
- data/lib/matrix_sdk.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fd493a721423c1f1a7da7b2ff678ad8e1a949f258fe613616ebedab39d5ae204
|
4
|
+
data.tar.gz: 52e6563d1dbb42cd5acddf3809df720fb6ecc5b6f49cbdc14606450e908c32c6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1fa0a6cbae2b6fa8ab91961df79082017be6a50028cc7b318ad70a5be9c76b8488b4b24cbef5e5a768ac34076236430683d518c18fccb296290599427bd71095
|
7
|
+
data.tar.gz: f62ad6791ee5f3921869d2431e55e3a1f44501af1b8c966a957fbcacbcaf809edb2e47b8d8b65d1393c268ff58b19e21fc966b1660c671d4d257121bf832dadf
|
data/CHANGELOG.md
CHANGED
data/lib/matrix_sdk/bot/base.rb
CHANGED
@@ -47,11 +47,15 @@ module MatrixSdk::Bot
|
|
47
47
|
client.join_room(ev[:room_id])
|
48
48
|
end
|
49
49
|
|
50
|
+
@event = nil
|
51
|
+
|
50
52
|
logger.warn 'The bot abstraction is not fully finalized and can be expected to change.'
|
51
53
|
end
|
52
54
|
|
53
55
|
def logger
|
54
|
-
@logger
|
56
|
+
return @logger if instance_variable_defined?(:@logger) && @logger
|
57
|
+
|
58
|
+
self.class.logger
|
55
59
|
end
|
56
60
|
|
57
61
|
def self.logger
|
data/lib/matrix_sdk/client.rb
CHANGED
@@ -21,8 +21,8 @@ module MatrixSdk
|
|
21
21
|
# @!attribute sync_filter [rw] The global sync filter
|
22
22
|
# @return [Hash,String] A filter definition, either as defined by the
|
23
23
|
# Matrix spec, or as an identifier returned by a filter creation request
|
24
|
-
attr_reader :api
|
25
|
-
attr_accessor :cache, :sync_filter, :
|
24
|
+
attr_reader :api
|
25
|
+
attr_accessor :cache, :sync_filter, :next_batch
|
26
26
|
|
27
27
|
events :error, :event, :account_data, :presence_event, :invite_event, :leave_event, :ephemeral_event, :state_event
|
28
28
|
ignore_inspect :api,
|
@@ -75,7 +75,6 @@ module MatrixSdk
|
|
75
75
|
@identity_server = nil
|
76
76
|
@mxid = nil
|
77
77
|
|
78
|
-
@sync_token = nil
|
79
78
|
@sync_thread = nil
|
80
79
|
@sync_filter = { room: { timeline: { limit: params.fetch(:sync_filter_limit, 20) }, state: { lazy_load_members: true } } }
|
81
80
|
|
@@ -99,6 +98,9 @@ module MatrixSdk
|
|
99
98
|
@mxid = params[:user_id]
|
100
99
|
end
|
101
100
|
|
101
|
+
alias sync_token next_batch
|
102
|
+
alias sync_token= next_batch=
|
103
|
+
|
102
104
|
# Gets the currently logged in user's MXID
|
103
105
|
#
|
104
106
|
# @return [MXID] The MXID of the current user
|
@@ -684,6 +686,7 @@ module MatrixSdk
|
|
684
686
|
# TODO Run this in a thread?
|
685
687
|
room.tinycache_adapter.cleanup
|
686
688
|
room.account_data.tinycache_adapter.cleanup
|
689
|
+
room.room_state.tinycache_adapter.cleanup
|
687
690
|
end
|
688
691
|
end
|
689
692
|
|
data/lib/matrix_sdk/mxid.rb
CHANGED
@@ -12,7 +12,7 @@ module MatrixSdk
|
|
12
12
|
|
13
13
|
# TODO: Community-as-a-Room / Profile-as-a-Room, in case they're going for room aliases
|
14
14
|
@sigil = identifier[0]
|
15
|
-
@localpart, @domain, @port = identifier[1
|
15
|
+
@localpart, @domain, @port = identifier[1..].split(':')
|
16
16
|
@port = @port.to_i if @port
|
17
17
|
|
18
18
|
raise ArgumentError, 'Identifier is not a valid MXID' unless valid?
|
@@ -519,6 +519,8 @@ module MatrixSdk::Protocols::CS
|
|
519
519
|
request(:put, client_api_latest, "/rooms/#{room_id}/state/#{event_type}#{"/#{state_key}" unless state_key.nil?}", body: content, query: query)
|
520
520
|
end
|
521
521
|
|
522
|
+
alias set_room_state send_state_event
|
523
|
+
|
522
524
|
# Sends a message event to a room
|
523
525
|
# @param room_id [MXID,String] The room ID to send the message event to
|
524
526
|
# @param event_type [String] The event type of the message
|
data/lib/matrix_sdk/room.rb
CHANGED
@@ -37,9 +37,6 @@ module MatrixSdk
|
|
37
37
|
cached :aliases, unless: proc { |args| args.any? }, cache_level: :all, expires_in: 60 * 60
|
38
38
|
cached :all_members, unless: proc { |args| args.any? }, cache_level: :all, expires_in: 60 * 60
|
39
39
|
|
40
|
-
# Much simpler to look up, and lighter data-wise, so the cache is wider
|
41
|
-
cached :canonical_alias, :name, :avatar_url, :topic, :guest_access, :join_rule, :power_levels, cache_level: :some, expires_in: 15 * 60
|
42
|
-
|
43
40
|
alias room_id id
|
44
41
|
alias members joined_members
|
45
42
|
|
@@ -84,6 +81,12 @@ module MatrixSdk
|
|
84
81
|
|
85
82
|
@prev_batch = nil
|
86
83
|
|
84
|
+
%i[name topic canonical_alias avatar_url].each do |type|
|
85
|
+
room_state.tinycache_adapter.write("m.room.#{type}", { type => data.delete(type) }) if data.key? type
|
86
|
+
end
|
87
|
+
room_state.tinycache_adapter.write('m.room.join_rules', { join_rule: data.delete(:join_rule) }) if data.key? :join_rule
|
88
|
+
room_state.tinycache_adapter.write('m.room.history_visibility', { history_visibility: data.delete(:world_readable) ? :world_readable : nil }) if data.key? :world_readable
|
89
|
+
|
87
90
|
data.each do |k, v|
|
88
91
|
next if %i[client].include? k
|
89
92
|
|
@@ -175,7 +178,7 @@ module MatrixSdk
|
|
175
178
|
|
176
179
|
# @return [String, nil] the canonical alias of the room
|
177
180
|
def canonical_alias
|
178
|
-
|
181
|
+
get_state('m.room.canonical_alias')[:alias]
|
179
182
|
rescue MatrixSdk::MatrixNotFoundError
|
180
183
|
nil
|
181
184
|
end
|
@@ -208,7 +211,7 @@ module MatrixSdk
|
|
208
211
|
#
|
209
212
|
# @return [String,nil] The room name - if any
|
210
213
|
def name
|
211
|
-
|
214
|
+
get_state('m.room.name')[:name]
|
212
215
|
rescue MatrixNotFoundError
|
213
216
|
# No room name has been specified
|
214
217
|
nil
|
@@ -244,7 +247,7 @@ module MatrixSdk
|
|
244
247
|
#
|
245
248
|
# @return [String,nil] The avatar URL - if any
|
246
249
|
def avatar_url
|
247
|
-
|
250
|
+
get_state('m.room.avatar_url')[:url]
|
248
251
|
rescue MatrixNotFoundError
|
249
252
|
# No avatar has been set
|
250
253
|
nil
|
@@ -254,7 +257,7 @@ module MatrixSdk
|
|
254
257
|
#
|
255
258
|
# @return [String,nil] The topic of the room
|
256
259
|
def topic
|
257
|
-
|
260
|
+
get_state('m.room.topic')[:topic]
|
258
261
|
rescue MatrixNotFoundError
|
259
262
|
# No room name has been specified
|
260
263
|
nil
|
@@ -264,14 +267,14 @@ module MatrixSdk
|
|
264
267
|
#
|
265
268
|
# @return [:can_join,:forbidden] The current guest access right
|
266
269
|
def guest_access
|
267
|
-
|
270
|
+
get_state('m.room.guest_access')[:guest_access]&.to_sym
|
268
271
|
end
|
269
272
|
|
270
273
|
# Gets the join rule for the room
|
271
274
|
#
|
272
275
|
# @return [:public,:knock,:invite,:private] The current join rule
|
273
276
|
def join_rule
|
274
|
-
|
277
|
+
get_state('m.room.join_rules')[:join_rule]&.to_sym
|
275
278
|
end
|
276
279
|
|
277
280
|
# Checks if +guest_access+ is set to +:can_join+
|
@@ -289,11 +292,27 @@ module MatrixSdk
|
|
289
292
|
join_rule == :knock
|
290
293
|
end
|
291
294
|
|
295
|
+
def room_state
|
296
|
+
return MatrixSdk::Util::StateEventCache.new self if client.cache == :none
|
297
|
+
|
298
|
+
@room_state ||= MatrixSdk::Util::StateEventCache.new self
|
299
|
+
end
|
300
|
+
|
301
|
+
# Gets a state object in the room
|
302
|
+
def get_state(type, state_key: nil)
|
303
|
+
room_state[type, state_key]
|
304
|
+
end
|
305
|
+
|
306
|
+
# Sets a state object in the room
|
307
|
+
def set_state(type, data, state_key: nil)
|
308
|
+
room_state[type, state_key] = data
|
309
|
+
end
|
310
|
+
|
292
311
|
# Gets the history visibility of the room
|
293
312
|
#
|
294
313
|
# @return [:invited,:joined,:shared,:world_readable] The current history visibility for the room
|
295
314
|
def history_visibility
|
296
|
-
|
315
|
+
get_state('m.room.history_visibility')[:history_visibility]&.to_sym
|
297
316
|
end
|
298
317
|
|
299
318
|
# Checks if the room history is world readable
|
@@ -309,7 +328,7 @@ module MatrixSdk
|
|
309
328
|
# @param canonical_only [Boolean] Should the list of aliases only contain the canonical ones
|
310
329
|
# @return [Array[String]] The assigned room aliases
|
311
330
|
def aliases(canonical_only: true)
|
312
|
-
canonical =
|
331
|
+
canonical = get_state('m.room.canonical_alias') rescue {}
|
313
332
|
aliases = ([canonical[:alias]].compact + (canonical[:alt_aliases] || [])).uniq.sort
|
314
333
|
return aliases if canonical_only
|
315
334
|
|
@@ -598,8 +617,7 @@ module MatrixSdk
|
|
598
617
|
#
|
599
618
|
# @return [Response] The content of the m.room.create event
|
600
619
|
def creation_info
|
601
|
-
|
602
|
-
client.api.get_room_creation_info(id)
|
620
|
+
room_state['m.room.create']
|
603
621
|
end
|
604
622
|
|
605
623
|
# Retrieves the type of the room
|
@@ -607,9 +625,6 @@ module MatrixSdk
|
|
607
625
|
# @return ['m.space',String,nil] The type of the room
|
608
626
|
def room_type
|
609
627
|
# Can't change, so a permanent cache is ok
|
610
|
-
return @room_type if @room_type_retrieved || @room_type
|
611
|
-
|
612
|
-
@room_type_retrieved = true
|
613
628
|
@room_type ||= creation_info[:type]
|
614
629
|
end
|
615
630
|
|
@@ -617,6 +632,7 @@ module MatrixSdk
|
|
617
632
|
#
|
618
633
|
# @return [String] The version of the room
|
619
634
|
def room_version
|
635
|
+
# Can't change, so a permanent cache is ok
|
620
636
|
@room_version ||= creation_info[:room_version]
|
621
637
|
end
|
622
638
|
|
@@ -691,8 +707,7 @@ module MatrixSdk
|
|
691
707
|
#
|
692
708
|
# @param name [String] The new name to set
|
693
709
|
def name=(name)
|
694
|
-
|
695
|
-
client.api.set_room_name(id, name)
|
710
|
+
room_state['m.room.name'] = { name: name }
|
696
711
|
name
|
697
712
|
end
|
698
713
|
|
@@ -700,7 +715,7 @@ module MatrixSdk
|
|
700
715
|
#
|
701
716
|
# @return [Boolean] if the name was changed or not
|
702
717
|
def reload_name!
|
703
|
-
|
718
|
+
room_state.expire('m.room.name')
|
704
719
|
end
|
705
720
|
alias refresh_name! reload_name!
|
706
721
|
|
@@ -708,8 +723,7 @@ module MatrixSdk
|
|
708
723
|
#
|
709
724
|
# @param topic [String] The new topic to set
|
710
725
|
def topic=(topic)
|
711
|
-
|
712
|
-
client.api.set_room_topic(id, topic)
|
726
|
+
room_state['m.room.topic'] = { topic: topic }
|
713
727
|
topic
|
714
728
|
end
|
715
729
|
|
@@ -717,7 +731,7 @@ module MatrixSdk
|
|
717
731
|
#
|
718
732
|
# @return [Boolean] if the topic was changed or not
|
719
733
|
def reload_topic!
|
720
|
-
|
734
|
+
room_state.expire('m.room.topic')
|
721
735
|
end
|
722
736
|
alias refresh_topic! reload_topic!
|
723
737
|
|
@@ -736,6 +750,7 @@ module MatrixSdk
|
|
736
750
|
# @note The list of aliases is not sorted, ordering changes will result in
|
737
751
|
# alias list updates.
|
738
752
|
def reload_aliases!
|
753
|
+
room_state.expire('m.room.canonical_alias')
|
739
754
|
clear_aliases_cache
|
740
755
|
end
|
741
756
|
alias refresh_aliases! reload_aliases!
|
@@ -752,8 +767,7 @@ module MatrixSdk
|
|
752
767
|
#
|
753
768
|
# @param join_rule [:invite,:public] The join rule of the room
|
754
769
|
def join_rule=(join_rule)
|
755
|
-
|
756
|
-
tinycache_adapter.write(:join_rule, join_rule)
|
770
|
+
room_state['m.room.join_rules'] = { join_rule: join_rule }
|
757
771
|
join_rule
|
758
772
|
end
|
759
773
|
|
@@ -769,8 +783,7 @@ module MatrixSdk
|
|
769
783
|
#
|
770
784
|
# @param guest_access [:can_join,:forbidden] The new guest access status of the room
|
771
785
|
def guest_access=(guest_access)
|
772
|
-
|
773
|
-
tinycache_adapter.write(:guest_access, guest_access)
|
786
|
+
room_state['m.room.guest_access'] = { guest_access: guest_access }
|
774
787
|
guest_access
|
775
788
|
end
|
776
789
|
|
@@ -781,8 +794,7 @@ module MatrixSdk
|
|
781
794
|
avatar_url = URI(avatar_url) unless avatar_url.is_a? URI
|
782
795
|
raise ArgumentError, 'Must be a valid MXC URL' unless avatar_url.is_a? URI::MXC
|
783
796
|
|
784
|
-
|
785
|
-
tinycache_adapter.write(:avatar_url, avatar_url)
|
797
|
+
room_state['m.room.avatar_url'] = { avatar_url: avatar_url }
|
786
798
|
avatar_url
|
787
799
|
end
|
788
800
|
|
@@ -792,7 +804,7 @@ module MatrixSdk
|
|
792
804
|
# @return [Hash] The current power levels as set for the room
|
793
805
|
# @see Protocols::CS#get_power_levels
|
794
806
|
def power_levels
|
795
|
-
|
807
|
+
get_state('m.room.power_levels')
|
796
808
|
end
|
797
809
|
|
798
810
|
# Gets the power level of a user in the room
|
@@ -889,8 +901,9 @@ module MatrixSdk
|
|
889
901
|
def modify_user_power_levels(users = nil, users_default = nil)
|
890
902
|
return false if users.nil? && users_default.nil?
|
891
903
|
|
892
|
-
|
893
|
-
|
904
|
+
room_state.tinycache_adapter.expire 'm.room.power_levels'
|
905
|
+
|
906
|
+
data = power_levels
|
894
907
|
data[:users_default] = users_default unless users_default.nil?
|
895
908
|
|
896
909
|
if users
|
@@ -908,7 +921,7 @@ module MatrixSdk
|
|
908
921
|
end
|
909
922
|
end
|
910
923
|
|
911
|
-
|
924
|
+
room_state['m.room.power_levels'] = data
|
912
925
|
true
|
913
926
|
end
|
914
927
|
|
@@ -920,8 +933,9 @@ module MatrixSdk
|
|
920
933
|
def modify_required_power_levels(events = nil, params = {})
|
921
934
|
return false if events.nil? && (params.nil? || params.empty?)
|
922
935
|
|
923
|
-
|
924
|
-
|
936
|
+
room_state.tinycache_adapter.expire 'm.room.power_levels'
|
937
|
+
|
938
|
+
data = power_levels
|
925
939
|
data.merge!(params)
|
926
940
|
data.delete_if { |_k, v| v.nil? }
|
927
941
|
|
@@ -931,7 +945,7 @@ module MatrixSdk
|
|
931
945
|
data[:events].delete_if { |_k, v| v.nil? }
|
932
946
|
end
|
933
947
|
|
934
|
-
|
948
|
+
room_state['m.room.power_levels'] = data
|
935
949
|
true
|
936
950
|
end
|
937
951
|
|
@@ -948,26 +962,6 @@ module MatrixSdk
|
|
948
962
|
tinycache_adapter.write(:joined_members, members)
|
949
963
|
end
|
950
964
|
|
951
|
-
def handle_power_levels(event)
|
952
|
-
tinycache_adapter.write(:power_levels, event[:content])
|
953
|
-
end
|
954
|
-
|
955
|
-
def handle_room_name(event)
|
956
|
-
tinycache_adapter.write(:name, event.dig(*%i[content name]))
|
957
|
-
end
|
958
|
-
|
959
|
-
def handle_room_topic(event)
|
960
|
-
tinycache_adapter.write(:topic, event.dig(*%i[content topic]))
|
961
|
-
end
|
962
|
-
|
963
|
-
def handle_room_guest_access(event)
|
964
|
-
tinycache_adapter.write(:guest_access, event.dig(*%i[content guest_access])&.to_sym)
|
965
|
-
end
|
966
|
-
|
967
|
-
def handle_room_join_rules(event)
|
968
|
-
tinycache_adapter.write(:join_rule, event.dig(*%i[content join_rule])&.to_sym)
|
969
|
-
end
|
970
|
-
|
971
965
|
def handle_room_member(event)
|
972
966
|
return unless client.cache == :all
|
973
967
|
|
@@ -982,7 +976,8 @@ module MatrixSdk
|
|
982
976
|
end
|
983
977
|
|
984
978
|
def handle_room_canonical_alias(event)
|
985
|
-
|
979
|
+
room_state.tinycache_adapter.write('m.room.canonical_alias', event[:content], expires_in: room_state.cache_time)
|
980
|
+
canonical_alias = event.dig(*%i[content alias])
|
986
981
|
|
987
982
|
data = tinycache_adapter.read(:aliases) || []
|
988
983
|
data << canonical_alias
|
@@ -1003,16 +998,6 @@ module MatrixSdk
|
|
1003
998
|
}
|
1004
999
|
end
|
1005
1000
|
|
1006
|
-
INTERNAL_HANDLERS = {
|
1007
|
-
'm.room.canonical_alias' => :handle_room_canonical_alias,
|
1008
|
-
'm.room.guest_access' => :handle_room_guest_access,
|
1009
|
-
'm.room.join_rules' => :handle_room_join_rules,
|
1010
|
-
'm.room.member' => :handle_room_member,
|
1011
|
-
'm.room.name' => :handle_room_name,
|
1012
|
-
'm.room.power_levels' => :handle_power_levels,
|
1013
|
-
'm.room.topic' => :handle_room_topic
|
1014
|
-
}.freeze
|
1015
|
-
|
1016
1001
|
def put_event(event)
|
1017
1002
|
ensure_room_handlers[:event].fire(MatrixEvent.new(self, event), event[:type]) if room_handlers?
|
1018
1003
|
|
@@ -1037,8 +1022,20 @@ module MatrixSdk
|
|
1037
1022
|
ensure_room_handlers[:ephemeral_event].fire(MatrixEvent.new(self, event), event[:type])
|
1038
1023
|
end
|
1039
1024
|
|
1025
|
+
INTERNAL_HANDLERS = {
|
1026
|
+
'm.room.canonical_alias' => :handle_room_canonical_alias,
|
1027
|
+
'm.room.member' => :handle_room_member
|
1028
|
+
}.freeze
|
1029
|
+
|
1040
1030
|
def put_state_event(event)
|
1041
|
-
|
1031
|
+
if INTERNAL_HANDLERS.key? event[:type]
|
1032
|
+
send(INTERNAL_HANDLERS[event[:type]], event)
|
1033
|
+
elsif client.cache != :none
|
1034
|
+
adapter = room_state.tinycache_adapter
|
1035
|
+
key = event[:type]
|
1036
|
+
key += "|#{event[:state_key]}" unless event[:state_key].nil? || event[:state_key].empty?
|
1037
|
+
adapter.write(key, event[:content], expires_in: room_state.cache_time)
|
1038
|
+
end
|
1042
1039
|
|
1043
1040
|
return unless room_handlers?
|
1044
1041
|
|
@@ -0,0 +1,92 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module MatrixSdk::Util
|
4
|
+
class StateEventCache
|
5
|
+
extend MatrixSdk::Extensions
|
6
|
+
extend MatrixSdk::Util::Tinycache
|
7
|
+
include Enumerable
|
8
|
+
|
9
|
+
attr_reader :room
|
10
|
+
|
11
|
+
attr_accessor :cache_time
|
12
|
+
|
13
|
+
ignore_inspect :client, :room, :tinycache_adapter
|
14
|
+
|
15
|
+
def initialize(room, cache_time: 30 * 60, **_params)
|
16
|
+
raise ArgumentError, 'Must be given a Room instance' unless room.is_a? MatrixSdk::Room
|
17
|
+
|
18
|
+
@room = room
|
19
|
+
@cache_time = cache_time
|
20
|
+
end
|
21
|
+
|
22
|
+
def client
|
23
|
+
@room.client
|
24
|
+
end
|
25
|
+
|
26
|
+
def reload!
|
27
|
+
tinycache_adapter.clear
|
28
|
+
end
|
29
|
+
|
30
|
+
def keys
|
31
|
+
tinycache_adapter.send(:cache).keys.map do |type|
|
32
|
+
real_type = type.split('|').first
|
33
|
+
state_key = type.split('|').last
|
34
|
+
state_key = nil if state_key == real_type
|
35
|
+
|
36
|
+
[real_type, state_key]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def values
|
41
|
+
keys.map { |key| tinycache_adapter.read(key) }
|
42
|
+
end
|
43
|
+
|
44
|
+
def size
|
45
|
+
keys.count
|
46
|
+
end
|
47
|
+
|
48
|
+
def key?(type, key = nil)
|
49
|
+
keys.key?("#{type}#{key ? "|#{key}" : ''}")
|
50
|
+
end
|
51
|
+
|
52
|
+
def expire(type, key = nil)
|
53
|
+
tinycache_adapter.expire("#{type}#{key ? "|#{key}" : ''}")
|
54
|
+
end
|
55
|
+
|
56
|
+
def each(live: false)
|
57
|
+
return to_enum(__method__, live: live) { keys.count } unless block_given?
|
58
|
+
|
59
|
+
keys.each do |type|
|
60
|
+
real_type = type.split('|').first
|
61
|
+
state_key = type.split('|').last
|
62
|
+
state_key = nil if state_key == real_type
|
63
|
+
|
64
|
+
v = live ? self[real_type, key: state_key] : tinycache_adapter.read(type)
|
65
|
+
# hash = v.hash
|
66
|
+
yield [real_type, state_key], v
|
67
|
+
# self[key] = v if hash != v.hash
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def delete(type, key = nil)
|
72
|
+
type = type.to_s unless type.is_a? String
|
73
|
+
client.api.set_room_state(room.id, type, {}, **{ state_key: key }.compact)
|
74
|
+
tinycache_adapter.delete("#{type}#{key ? "|#{key}" : ''}")
|
75
|
+
end
|
76
|
+
|
77
|
+
def [](type, key = nil)
|
78
|
+
type = type.to_s unless type.is_a? String
|
79
|
+
tinycache_adapter.fetch("#{type}#{key ? "|#{key}" : ''}", expires_in: @cache_time) do
|
80
|
+
client.api.get_room_state(room.id, type, **{ key: key }.compact)
|
81
|
+
rescue MatrixSdk::MatrixNotFoundError
|
82
|
+
{}
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def []=(type, key = nil, value) # rubocop:disable Style/OptionalArguments Not possible to put optional last
|
87
|
+
type = type.to_s unless type.is_a? String
|
88
|
+
client.api.set_room_state(room.id, type, value, **{ state_key: key }.compact)
|
89
|
+
tinycache_adapter.write("#{type}#{key ? "|#{key}" : ''}", value)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
@@ -76,24 +76,18 @@ module MatrixSdk::Util
|
|
76
76
|
define_method(method_names[:with_cache]) do |*args|
|
77
77
|
tinycache_adapter.fetch(__send__(method_names[:cache_key], *args), expires_in: expires_in) do
|
78
78
|
named = args.delete_at(-1) if args.last.is_a? Hash
|
79
|
+
named ||= {}
|
79
80
|
|
80
|
-
|
81
|
-
__send__(method_names[:without_cache], *args, **named)
|
82
|
-
else
|
83
|
-
__send__(method_names[:without_cache], *args)
|
84
|
-
end
|
81
|
+
__send__(method_names[:without_cache], *args, **named)
|
85
82
|
end
|
86
83
|
end
|
87
84
|
|
88
85
|
define_method(method_names[:without_cache]) do |*args|
|
89
86
|
orig = method(method_name).super_method
|
90
87
|
named = args.delete_at(-1) if args.last.is_a? Hash
|
88
|
+
named ||= {}
|
91
89
|
|
92
|
-
|
93
|
-
orig.call(*args, **named)
|
94
|
-
else
|
95
|
-
orig.call(*args)
|
96
|
-
end
|
90
|
+
orig.call(*args, **named)
|
97
91
|
end
|
98
92
|
|
99
93
|
define_method(method_names[:clear_cache]) do |*args|
|
data/lib/matrix_sdk/version.rb
CHANGED
data/lib/matrix_sdk.rb
CHANGED
@@ -38,6 +38,7 @@ module MatrixSdk
|
|
38
38
|
|
39
39
|
module Util
|
40
40
|
autoload :AccountDataCache, 'matrix_sdk/util/account_data_cache'
|
41
|
+
autoload :StateEventCache, 'matrix_sdk/util/state_event_cache'
|
41
42
|
autoload :Tinycache, 'matrix_sdk/util/tinycache'
|
42
43
|
autoload :TinycacheAdapter, 'matrix_sdk/util/tinycache_adapter'
|
43
44
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: matrix_sdk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Olofsson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-08-
|
11
|
+
date: 2022-08-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mocha
|
@@ -113,6 +113,7 @@ files:
|
|
113
113
|
- lib/matrix_sdk/util/account_data_cache.rb
|
114
114
|
- lib/matrix_sdk/util/events.rb
|
115
115
|
- lib/matrix_sdk/util/extensions.rb
|
116
|
+
- lib/matrix_sdk/util/state_event_cache.rb
|
116
117
|
- lib/matrix_sdk/util/tinycache.rb
|
117
118
|
- lib/matrix_sdk/util/tinycache_adapter.rb
|
118
119
|
- lib/matrix_sdk/util/uri.rb
|