simplex-chat 0.5.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a5abf1b7b550f998760cb68e89636e031dcf96aff3449e9422903db050521bd2
4
- data.tar.gz: 4602313dfbf09bfd9b650c5668f16389d885cf16202b1896fc81f4dd71dfa869
3
+ metadata.gz: 1085de134ea131845256d9109e043629b6f83ab59adf94625c89287272c9966a
4
+ data.tar.gz: e2a1ac6403338b1cc07238d4dc6a2cdb15992a5e938b3df04c98d72d7cce3a93
5
5
  SHA512:
6
- metadata.gz: 5f77b4edf376118de56ff6dde9a99ee06fc49c12f6ef348053a8ba1ec807b885fac6c2ef4c5c6fe48c21d95a10d7b3351a6ad07a8429603f0ec1ee997ea2923e
7
- data.tar.gz: f59fb84f87c57f491eda4e3ab58c30451a376c1c8b79c8e6b190df433f53d4394c4c41b459e8e18940bea91c95517ebd82643cf1e3e65c6093462be3910455c1
6
+ metadata.gz: 516773178824c494adaa70bef60b323774c8775456eeef1de33ca3bc2131073808d85db998ebade717254286374db2f25b8bec2ba4fd903502b229a70ecc2467
7
+ data.tar.gz: 9c4848683f657f145538979603d2d5edfa83e32aaf030b6422b5d60ca38613ff8a517abd30f800c508744e255f48d4a172fa4297e2d9ae7d2f55b6ef948db5e1
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SimpleXChat
4
- VERSION = "0.5.0"
4
+ VERSION = "0.6.1"
5
5
  end
data/lib/simplex-chat.rb CHANGED
@@ -18,7 +18,7 @@ module SimpleXChat
18
18
 
19
19
  def initialize(client_uri, connect: true, log_level: Logger::INFO, timeout_ms: 10_000, interval_ms: 100)
20
20
  @uri = client_uri
21
- @message_queue = SizedQueue.new 4096
21
+ @message_queue = Queue.new
22
22
  @chat_message_queue = Queue.new
23
23
  @socket = nil
24
24
  @handshake = nil
@@ -74,8 +74,8 @@ module SimpleXChat
74
74
  single_use_queue.push(resp)
75
75
  @logger.debug("Message sent to waiter with corrId '#{corr_id}'")
76
76
  else
77
- @logger.debug("Message put on message queue")
78
77
  @message_queue.push resp
78
+ @logger.debug("Message put on message queue (number of messages in queue: #{@message_queue.size})")
79
79
  end
80
80
  rescue IO::WaitReadable
81
81
  IO.select([@socket])
@@ -97,7 +97,9 @@ module SimpleXChat
97
97
  end
98
98
 
99
99
  def next_message
100
- @message_queue.pop
100
+ msg = @message_queue.pop
101
+ @logger.debug("Message retrieved from queue (number of messages in queue: #{@message_queue.size})")
102
+ msg
101
103
  end
102
104
 
103
105
  def next_chat_message(
@@ -108,7 +110,13 @@ module SimpleXChat
108
110
  # a chat message queue to insert one or
109
111
  # more messages at a time, but poll just
110
112
  # one at a time
111
- return @chat_message_queue.pop if not @chat_message_queue.empty?
113
+ # NOTE: We use non-blocking pop for thread safety
114
+ begin
115
+ chat_msg = @chat_message_queue.pop(true)
116
+ return chat_msg
117
+ rescue ThreadError
118
+ @logger.debug("Chat message queue is empty, waiting for new messages...")
119
+ end
112
120
 
113
121
  loop do
114
122
  msg = next_message
@@ -133,9 +141,20 @@ module SimpleXChat
133
141
  end
134
142
 
135
143
  @chat_message_queue.push chat_message
144
+ @logger.debug("Chat message pushed to queue: #{chat_message}")
136
145
  end
137
146
 
138
- return @chat_message_queue.pop
147
+ # NOTE: Even after parsing the messages, the
148
+ # chat message queue can be empty because
149
+ # all the messages are too old, so we have
150
+ # to check again
151
+ begin
152
+ chat_msg = @chat_message_queue.pop(true)
153
+ @logger.debug("Chat message popped from queue")
154
+ return chat_msg
155
+ rescue ThreadError
156
+ @logger.debug("Chat message queue is still empty, waiting for new messages...")
157
+ end
139
158
  end
140
159
 
141
160
  nil
@@ -365,6 +384,19 @@ module SimpleXChat
365
384
  end.filter { |x| x != nil }
366
385
  end
367
386
 
387
+ # TODO: Add `/_reaction members` support, either on this
388
+ # function or in a separate one
389
+ def api_reaction(chat_type, chat_id, message_item_id, add: true, emoji: '👍')
390
+ onoff = add ? "on" : "off"
391
+ param_obj = {
392
+ "type" => "emoji",
393
+ "emoji" => emoji
394
+ }
395
+ cmd = "/_reaction #{chat_type}#{chat_id} #{message_item_id} #{onoff} #{param_obj.to_json}"
396
+ resp = send_command cmd
397
+ check_response_type(resp, "chatItemReaction")
398
+ end
399
+
368
400
  private
369
401
 
370
402
  def check_response_type(resp, expected_resp_type)
@@ -389,35 +421,52 @@ module SimpleXChat
389
421
  def parse_chat_item(chat_item)
390
422
  chat_type = parse_chat_info_type chat_item["chatInfo"]["type"]
391
423
  group = nil
424
+ group_id = nil
392
425
  sender = nil
426
+ sender_id = nil
393
427
  contact = nil
428
+ contact_id = nil
394
429
  contact_role = nil
395
430
  if chat_type == ChatType::GROUP
396
431
  # NOTE: The group can "send messages" without a contact
397
432
  # For example, when a member is removed, the group
398
433
  # sends a message about his removal, with no contact
399
434
  contact = chat_item.dig "chatItem", "chatDir", "groupMember", "localDisplayName"
435
+ contact_id = chat_item.dig "chatItem", "chatDir", "groupMember", "groupMemberId"
400
436
  contact_role = chat_item.dig "chatItem", "chatDir", "groupMember", "memberRole"
401
437
  group = chat_item["chatInfo"]["groupInfo"]["localDisplayName"]
438
+ group_id = chat_item["chatInfo"]["groupInfo"]["groupId"]
402
439
  sender = group
440
+ sender_id = group_id
403
441
  else
404
442
  contact = chat_item["chatInfo"]["contact"]["localDisplayName"]
443
+ contact_id = chat_item["chatInfo"]["contact"]["contactId"]
405
444
  sender = contact
445
+ sender_id = contact_id
406
446
  end
407
447
 
408
448
  msg_text = chat_item["chatItem"]["meta"]["itemText"]
449
+ msg_item_id = chat_item["chatItem"]["meta"]["itemId"]
409
450
  timestamp = Time.parse(chat_item["chatItem"]["meta"]["updatedAt"])
410
- image_preview = chat_item.dig "chatItem", "content", "msgContent", "image"
451
+ msg_image_preview = chat_item.dig "chatItem", "content", "msgContent", "image"
411
452
 
412
453
  chat_message = {
413
454
  :chat_type => chat_type,
455
+
414
456
  :sender => sender,
415
- :contact_role => contact_role,
457
+ :sender_id => sender_id,
458
+
416
459
  :contact => contact,
460
+ :contact_id => contact_id,
461
+ :contact_role => contact_role,
462
+
417
463
  :group => group,
464
+ :group_id => group_id,
465
+
418
466
  :msg_text => msg_text,
467
+ :msg_item_id => msg_item_id,
419
468
  :msg_timestamp => timestamp,
420
- :img_preview => image_preview
469
+ :msg_img_preview => msg_image_preview
421
470
  }
422
471
  end
423
472
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simplex-chat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - rdbo
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-03-04 00:00:00.000000000 Z
10
+ date: 2025-03-05 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: websocket