matrix_sdk 2.1.2 → 2.1.3

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: 5af1e7af0f473a5c44df1ede83655cdbdb120e215e42f125b6af576740b88aa6
4
- data.tar.gz: cca80fe97ca22f0ddb0c5a2b39b24b70345baa25fb20bea305fa019f46ca7d3a
3
+ metadata.gz: 550080056a38ee354ff8e633069d71f207e1927a567fcaed4f7ed9792bcbb79a
4
+ data.tar.gz: ae8909f5a1092873453c4b7a876315efd29882cee353252d9bd92f069ce3c4ac
5
5
  SHA512:
6
- metadata.gz: 0e4c694d8489dae1b2602d01f4aab08b20119c6697e83b41a2a463adb7ebcaf7b1afd439db04a6e54a0979c5a93be865c8afa3d2d03b4ca34b98f82afae87a5f
7
- data.tar.gz: 8f52c12d1eb8af55ac5bc011852c9d5f648e9de1cc3241ea1ed76530036b02cff373cb69b8a4c721956add3df1359377d79b2fa2562e8dab8efe5ce877596e2d
6
+ metadata.gz: 5838b47c8ea7df7becdb7dadcb07e57c31526c8c3cdd025a18cc2add7db9e9da9f0aff62a16ed4a3a5306566785f941e7cdba90e664f2b872959c7774b4ec4bc
7
+ data.tar.gz: 3fcad5ea834b2dd0e6d68e55a8e88e5a88b444d07491f381420c4749ca1ff5375030f3a7c4b1b535ff947422caf7c4b86f81633d425614a05e5577aa9a4963ff
@@ -1,3 +1,11 @@
1
+ ## 2.1.3 - 2020-09-18
2
+
3
+ - Adds separate state event handler as Client#on_state_event
4
+ - Changes Client sync interval to by-default run at full speed
5
+ - Fixes state events being sent twice if included in both timeline and state of a sync
6
+ - Improves error reporting of broken 200 responses
7
+ - Improves event handlers for rooms, to not depend on a specific room object instance anymore
8
+
1
9
  ## 2.1.2 - 2020-09-10
2
10
 
3
11
  - Adds method for reading complete member lists for rooms, improves the CS spec adherence
@@ -101,30 +101,30 @@ module MatrixSdk
101
101
  elsif target == :server
102
102
  # Attempt SRV record discovery
103
103
  target_uri = begin
104
- require 'resolv'
105
- resolver = Resolv::DNS.new
106
- srv = "_matrix._tcp.#{domain}"
107
- logger.debug "Trying DNS #{srv}..."
108
- d = resolver.getresource(srv, Resolv::DNS::Resource::IN::SRV)
109
- d
110
- rescue StandardError => e
111
- logger.debug "DNS lookup failed with #{e.class}: #{e.message}"
112
- nil
113
- end
104
+ require 'resolv'
105
+ resolver = Resolv::DNS.new
106
+ srv = "_matrix._tcp.#{domain}"
107
+ logger.debug "Trying DNS #{srv}..."
108
+ d = resolver.getresource(srv, Resolv::DNS::Resource::IN::SRV)
109
+ d
110
+ rescue StandardError => e
111
+ logger.debug "DNS lookup failed with #{e.class}: #{e.message}"
112
+ nil
113
+ end
114
114
 
115
115
  if target_uri.nil?
116
116
  # Attempt .well-known discovery for server-to-server
117
117
  well_known = begin
118
- wk_uri = URI("https://#{domain}/.well-known/matrix/server")
119
- logger.debug "Trying #{wk_uri}..."
120
- data = Net::HTTP.start(wk_uri.host, wk_uri.port, use_ssl: true, open_timeout: 5, read_timeout: 5, write_timeout: 5) do |http|
121
- http.get(wk_uri.path).body
122
- end
123
- JSON.parse(data)
124
- rescue StandardError => e
125
- logger.debug "Well-known failed with #{e.class}: #{e.message}"
126
- nil
127
- end
118
+ wk_uri = URI("https://#{domain}/.well-known/matrix/server")
119
+ logger.debug "Trying #{wk_uri}..."
120
+ data = Net::HTTP.start(wk_uri.host, wk_uri.port, use_ssl: true, open_timeout: 5, read_timeout: 5, write_timeout: 5) do |http|
121
+ http.get(wk_uri.path).body
122
+ end
123
+ JSON.parse(data)
124
+ rescue StandardError => e
125
+ logger.debug "Well-known failed with #{e.class}: #{e.message}"
126
+ nil
127
+ end
128
128
 
129
129
  target_uri = well_known['m.server'] if well_known&.key?('m.server')
130
130
  else
@@ -133,16 +133,16 @@ module MatrixSdk
133
133
  elsif %i[client identity].include? target
134
134
  # Attempt .well-known discovery
135
135
  well_known = begin
136
- wk_uri = URI("https://#{domain}/.well-known/matrix/client")
137
- logger.debug "Trying #{wk_uri}..."
138
- data = Net::HTTP.start(wk_uri.host, wk_uri.port, use_ssl: true, open_timeout: 5, read_timeout: 5, write_timeout: 5) do |http|
139
- http.get(wk_uri.path).body
140
- end
141
- data = JSON.parse(data)
142
- rescue StandardError => e
143
- logger.debug "Well-known failed with #{e.class}: #{e.message}"
144
- nil
145
- end
136
+ wk_uri = URI("https://#{domain}/.well-known/matrix/client")
137
+ logger.debug "Trying #{wk_uri}..."
138
+ data = Net::HTTP.start(wk_uri.host, wk_uri.port, use_ssl: true, open_timeout: 5, read_timeout: 5, write_timeout: 5) do |http|
139
+ http.get(wk_uri.path).body
140
+ end
141
+ JSON.parse(data)
142
+ rescue StandardError => e
143
+ logger.debug "Well-known failed with #{e.class}: #{e.message}"
144
+ nil
145
+ end
146
146
 
147
147
  if well_known
148
148
  key = 'm.homeserver'
@@ -304,7 +304,12 @@ module MatrixSdk
304
304
  end
305
305
  print_http(response, duration: duration, id: req_id)
306
306
 
307
- data = JSON.parse(response.body, symbolize_names: true) rescue nil
307
+ begin
308
+ data = JSON.parse(response.body, symbolize_names: true)
309
+ rescue JSON::JSONError => e
310
+ logger.debug "#{e.class} error when parsing response. #{e}"
311
+ data = nil
312
+ end
308
313
 
309
314
  if response.is_a? Net::HTTPTooManyRequests
310
315
  raise MatrixRequestError.new_by_code(data, response.code) unless autoretry
@@ -315,7 +320,13 @@ module MatrixSdk
315
320
  next
316
321
  end
317
322
 
318
- return MatrixSdk::Response.new self, data if response.is_a? Net::HTTPSuccess
323
+ if response.is_a? Net::HTTPSuccess
324
+ unless data
325
+ logger.error "Received non-parsable data in 200 response; #{response.body.inspect}"
326
+ raise MatrixConnectionError, response
327
+ end
328
+ return MatrixSdk::Response.new self, data
329
+ end
319
330
  raise MatrixRequestError.new_by_code(data, response.code) if data
320
331
 
321
332
  raise MatrixConnectionError.class_by_code(response.code), response
@@ -23,7 +23,7 @@ module MatrixSdk
23
23
  attr_reader :api, :next_batch
24
24
  attr_accessor :cache, :sync_filter
25
25
 
26
- events :error, :event, :presence_event, :invite_event, :leave_event, :ephemeral_event
26
+ events :error, :event, :presence_event, :invite_event, :leave_event, :ephemeral_event, :state_event
27
27
  ignore_inspect :api,
28
28
  :on_event, :on_presence_event, :on_invite_event, :on_leave_event, :on_ephemeral_event
29
29
 
@@ -70,8 +70,6 @@ module MatrixSdk
70
70
  @api = Api.new hs_url, params
71
71
  end
72
72
 
73
- @rooms = {}
74
- @users = {}
75
73
  @cache = client_cache
76
74
  @identity_server = nil
77
75
 
@@ -79,7 +77,6 @@ module MatrixSdk
79
77
  @sync_thread = nil
80
78
  @sync_filter = { room: { timeline: { limit: params.fetch(:sync_filter_limit, 20) }, state: { lazy_load_members: true } } }
81
79
 
82
- @should_listen = false
83
80
  @next_batch = nil
84
81
 
85
82
  @bad_sync_timeout_limit = 60 * 60
@@ -88,6 +85,11 @@ module MatrixSdk
88
85
  instance_variable_set("@#{k}", v) if instance_variable_defined? "@#{k}"
89
86
  end
90
87
 
88
+ @rooms = {}
89
+ @room_handlers = {}
90
+ @users = {}
91
+ @should_listen = false
92
+
91
93
  raise ArgumentError, 'Cache value must be one of of [:all, :some, :none]' unless %i[all some none].include? @cache
92
94
 
93
95
  return unless params[:user_id]
@@ -524,7 +526,7 @@ module MatrixSdk
524
526
  end
525
527
  end
526
528
 
527
- def listen_forever(timeout: 30, bad_sync_timeout: 5, sync_interval: 30, **params)
529
+ def listen_forever(timeout: 30, bad_sync_timeout: 5, sync_interval: 0, **params)
528
530
  orig_bad_sync_timeout = bad_sync_timeout + 0
529
531
  while @should_listen
530
532
  begin
@@ -560,6 +562,8 @@ module MatrixSdk
560
562
  def handle_state(room_id, state_event)
561
563
  return unless state_event.key? :type
562
564
 
565
+ on_state_event.fire(MatrixEvent.new(self, state_event), state_event[:type])
566
+
563
567
  room = ensure_room(room_id)
564
568
  room.send :put_state_event, state_event
565
569
  content = state_event[:content]
@@ -618,7 +622,12 @@ module MatrixSdk
618
622
 
619
623
  join[:timeline][:events].each do |event|
620
624
  event[:room_id] = room_id.to_s
621
- handle_state(room_id, event) if event.key? :state_key
625
+ # Avoid sending two identical state events if it's both in state and timeline
626
+ if event.key?(:state_key)
627
+ state_event = join.dig(:state, :events).find { |ev| ev[:event_id] == event[:event_id] }
628
+
629
+ handle_state(room_id, event) unless event == state_event
630
+ end
622
631
  room.send :put_event, event
623
632
 
624
633
  fire_event(MatrixEvent.new(self, event), event[:type])
@@ -42,19 +42,11 @@ module MatrixSdk
42
42
  # The timeline events are what will end up in here
43
43
  attr_reader :id, :client, :topic, :aliases, :members, :events
44
44
 
45
- # @!attribute [r] on_event
46
- # @return [EventHandlerArray] The list of event handlers for all events
47
- # @!attribute [r] on_state_event
48
- # @return [EventHandlerArray] The list of event handlers for only state events
49
- # @!attribute [r] on_ephemeral_event
50
- # @return [EventHandlerArray] The list of event handlers for only ephemeral events
51
- events :event, :state_event, :ephemeral_event
52
45
  # @!method inspect
53
46
  # An inspect method that skips a handful of instance variables to avoid
54
47
  # flooding the terminal with debug data.
55
48
  # @return [String] a regular inspect string without the data for some variables
56
- ignore_inspect :client, :members, :events, :prev_batch, :logger,
57
- :on_event, :on_state_event, :on_ephemeral_event
49
+ ignore_inspect :client, :members, :events, :prev_batch, :logger
58
50
 
59
51
  alias room_id id
60
52
 
@@ -85,8 +77,6 @@ module MatrixSdk
85
77
  room_id = MXID.new room_id unless room_id.is_a?(MXID)
86
78
  raise ArgumentError, 'room_id must be a valid Room ID' unless room_id.room_id?
87
79
 
88
- event_initialize
89
-
90
80
  @name = nil
91
81
  @topic = nil
92
82
  @canonical_alias = nil
@@ -114,6 +104,30 @@ module MatrixSdk
114
104
  logger.debug "Created room #{room_id}"
115
105
  end
116
106
 
107
+
108
+ #
109
+ # Event handlers
110
+ #
111
+
112
+ # @!attribute [r] on_event
113
+ # @return [EventHandlerArray] The list of event handlers for all events
114
+ def on_event
115
+ ensure_room_handlers[:event]
116
+ end
117
+
118
+ # @!attribute [r] on_state_event
119
+ # @return [EventHandlerArray] The list of event handlers for only state events
120
+ def on_state_event
121
+ ensure_room_handlers[:state_event]
122
+ end
123
+
124
+ # @!attribute [r] on_ephemeral_event
125
+ # @return [EventHandlerArray] The list of event handlers for only ephemeral events
126
+ def on_ephemeral_event
127
+ ensure_room_handlers[:ephemeral_event]
128
+ end
129
+
130
+
117
131
  #
118
132
  # State readers
119
133
  #
@@ -523,10 +537,10 @@ module MatrixSdk
523
537
  # @return [Boolean] if the name was changed or not
524
538
  def reload_name!
525
539
  data = begin
526
- client.api.get_room_name(id)
527
- rescue MatrixNotFoundError
528
- nil
529
- end
540
+ client.api.get_room_name(id)
541
+ rescue MatrixNotFoundError
542
+ nil
543
+ end
530
544
  changed = data[:name] != @name
531
545
  @name = data[:name] if changed
532
546
  changed
@@ -546,10 +560,10 @@ module MatrixSdk
546
560
  # @return [Boolean] if the topic was changed or not
547
561
  def reload_topic!
548
562
  data = begin
549
- client.api.get_room_topic(id)
550
- rescue MatrixNotFoundError
551
- nil
552
- end
563
+ client.api.get_room_topic(id)
564
+ rescue MatrixNotFoundError
565
+ nil
566
+ end
553
567
  changed = data[:topic] != @topic
554
568
  @topic = data[:topic] if changed
555
569
  changed
@@ -681,19 +695,35 @@ module MatrixSdk
681
695
  members << member unless members.any? { |m| m.id == member.id }
682
696
  end
683
697
 
698
+ def room_handlers?
699
+ client.instance_variable_get(:@room_handlers).key? id
700
+ end
701
+
702
+ def ensure_room_handlers
703
+ client.instance_variable_get(:@room_handlers)[id] ||= {
704
+ event: MatrixSdk::EventHandlerArray.new,
705
+ state_event: MatrixSdk::EventHandlerArray.new,
706
+ ephemeral_event: MatrixSdk::EventHandlerArray.new
707
+ }
708
+ end
709
+
684
710
  def put_event(event)
711
+ ensure_room_handlers[:event].fire(MatrixEvent.new(self, event), event[:type]) if room_handlers?
712
+
685
713
  @events.push event
686
714
  @events.shift if @events.length > @event_history_limit
687
-
688
- fire_event MatrixEvent.new(self, event)
689
715
  end
690
716
 
691
717
  def put_ephemeral_event(event)
692
- fire_ephemeral_event MatrixEvent.new(self, event)
718
+ return unless room_handlers?
719
+
720
+ ensure_room_handlers[:ephemeral_event].fire(MatrixEvent.new(self, event), event[:type])
693
721
  end
694
722
 
695
723
  def put_state_event(event)
696
- fire_state_event MatrixEvent.new(self, event)
724
+ return unless room_handlers?
725
+
726
+ ensure_room_handlers[:state_event].fire(MatrixEvent.new(self, event), event[:type])
697
727
  end
698
728
  end
699
729
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MatrixSdk
4
- VERSION = '2.1.2'
4
+ VERSION = '2.1.3'
5
5
  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.1.2
4
+ version: 2.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Olofsson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-10 00:00:00.000000000 Z
11
+ date: 2020-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mocha