simplex-chat 0.4.0 → 0.5.0
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 +4 -4
- data/lib/simplex-chat/version.rb +1 -1
- data/lib/simplex-chat.rb +105 -73
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a5abf1b7b550f998760cb68e89636e031dcf96aff3449e9422903db050521bd2
|
4
|
+
data.tar.gz: 4602313dfbf09bfd9b650c5668f16389d885cf16202b1896fc81f4dd71dfa869
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5f77b4edf376118de56ff6dde9a99ee06fc49c12f6ef348053a8ba1ec807b885fac6c2ef4c5c6fe48c21d95a10d7b3351a6ad07a8429603f0ec1ee997ea2923e
|
7
|
+
data.tar.gz: f59fb84f87c57f491eda4e3ab58c30451a376c1c8b79c8e6b190df433f53d4394c4c41b459e8e18940bea91c95517ebd82643cf1e3e65c6093462be3910455c1
|
data/lib/simplex-chat/version.rb
CHANGED
data/lib/simplex-chat.rb
CHANGED
@@ -100,7 +100,9 @@ module SimpleXChat
|
|
100
100
|
@message_queue.pop
|
101
101
|
end
|
102
102
|
|
103
|
-
def next_chat_message
|
103
|
+
def next_chat_message(
|
104
|
+
max_backlog_secs: 15.0 # if nil, it will process any incoming messages, including old ones
|
105
|
+
)
|
104
106
|
# NOTE: There can be more than one message per
|
105
107
|
# client message. Because of that, we use
|
106
108
|
# a chat message queue to insert one or
|
@@ -113,11 +115,6 @@ module SimpleXChat
|
|
113
115
|
break if msg == nil
|
114
116
|
next if not ["chatItemUpdated", "newChatItems"].include?(msg["type"])
|
115
117
|
|
116
|
-
chat_info_types = {
|
117
|
-
"direct" => ChatType::DIRECT,
|
118
|
-
"group" => ChatType::GROUP
|
119
|
-
}
|
120
|
-
|
121
118
|
# Handle one or more chat messages in a single client message
|
122
119
|
new_chat_messages = nil
|
123
120
|
if msg["type"] == "chatItemUpdated"
|
@@ -127,38 +124,13 @@ module SimpleXChat
|
|
127
124
|
end
|
128
125
|
|
129
126
|
new_chat_messages.each do |chat_item|
|
130
|
-
|
131
|
-
group = nil
|
132
|
-
sender = nil
|
133
|
-
contact = nil
|
134
|
-
contact_role = nil
|
135
|
-
if chat_type == ChatType::GROUP
|
136
|
-
# NOTE: The group can "send messages" without a contact
|
137
|
-
# For example, when a member is removed, the group
|
138
|
-
# sends a message about his removal, with no contact
|
139
|
-
contact = chat_item.dig "chatItem", "chatDir", "groupMember", "localDisplayName"
|
140
|
-
contact_role = chat_item.dig "chatItem", "chatDir", "groupMember", "memberRole"
|
141
|
-
group = chat_item["chatInfo"]["groupInfo"]["localDisplayName"]
|
142
|
-
sender = group
|
143
|
-
else
|
144
|
-
contact = chat_item["chatInfo"]["contact"]["localDisplayName"]
|
145
|
-
sender = contact
|
146
|
-
end
|
127
|
+
chat_message = parse_chat_item chat_item
|
147
128
|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
:chat_type => chat_type,
|
154
|
-
:sender => sender,
|
155
|
-
:contact_role => contact_role,
|
156
|
-
:contact => contact,
|
157
|
-
:group => group,
|
158
|
-
:msg_text => msg_text,
|
159
|
-
:msg_timestamp => Time.parse(timestamp),
|
160
|
-
:img_preview => image_preview
|
161
|
-
}
|
129
|
+
time_diff = Time.now - chat_message[:msg_timestamp]
|
130
|
+
if max_backlog_secs != nil && time_diff > max_backlog_secs
|
131
|
+
@logger.debug("Skipped message (time diff: #{time_diff}, max allowed: #{max_backlog_secs}): #{chat_message}")
|
132
|
+
next
|
133
|
+
end
|
162
134
|
|
163
135
|
@chat_message_queue.push chat_message
|
164
136
|
end
|
@@ -224,18 +196,14 @@ module SimpleXChat
|
|
224
196
|
|
225
197
|
def api_version
|
226
198
|
resp = send_command '/version'
|
227
|
-
|
228
|
-
expected_resp_type = "versionInfo"
|
229
|
-
raise UnexpectedResponseError.new(resp_type, expected_resp_type) unless resp_type == expected_resp_type
|
199
|
+
check_response_type(resp, "versionInfo")
|
230
200
|
|
231
201
|
resp["versionInfo"]["version"]
|
232
202
|
end
|
233
203
|
|
234
204
|
def api_profile
|
235
205
|
resp = send_command '/profile'
|
236
|
-
|
237
|
-
expected_resp_type = "userProfile"
|
238
|
-
raise UnexpectedResponseError.new(resp_type, expected_resp_type) unless resp_type == expected_resp_type
|
206
|
+
check_response_type(resp, "userProfile")
|
239
207
|
|
240
208
|
{
|
241
209
|
"name" => resp["user"]["profile"]["displayName"],
|
@@ -251,54 +219,42 @@ module SimpleXChat
|
|
251
219
|
if resp_type == "chatCmdError" && resp.dig("chatError", "storeError", "type") == "userContactLinkNotFound"
|
252
220
|
return nil
|
253
221
|
end
|
254
|
-
|
255
|
-
expected_resp_type = "userContactLink"
|
256
|
-
raise UnexpectedResponseError.new(resp_type, expected_resp_type) unless resp_type == expected_resp_type
|
222
|
+
check_response_type(resp, "userContactLink")
|
257
223
|
|
258
224
|
resp["contactLink"]["connReqContact"]
|
259
225
|
end
|
260
226
|
|
261
227
|
def api_create_user_address
|
262
228
|
resp = send_command '/address'
|
263
|
-
|
264
|
-
expected_resp_type = "userContactLinkCreated"
|
265
|
-
raise UnexpectedResponseError.new(resp_type, expected_resp_type) unless resp_type == expected_resp_type
|
229
|
+
check_response_type(resp, "userContactLinkCreated")
|
266
230
|
|
267
231
|
resp["connReqContact"]
|
268
232
|
end
|
269
233
|
|
270
234
|
def api_send_text_message(chat_type, receiver, message)
|
271
235
|
resp = send_command "#{chat_type}#{receiver} #{message}"
|
272
|
-
|
273
|
-
expected_resp_type = "newChatItems"
|
274
|
-
raise UnexpectedResponseError.new(resp_type, expected_resp_type) unless resp_type == expected_resp_type
|
236
|
+
check_response_type(resp, "newChatItems")
|
275
237
|
|
276
238
|
resp["chatItems"]
|
277
239
|
end
|
278
240
|
|
279
241
|
def api_send_image(chat_type, receiver, file_path)
|
280
242
|
resp = send_command "/image #{chat_type}#{receiver} #{file_path}"
|
281
|
-
|
282
|
-
expected_resp_type = "newChatItems"
|
283
|
-
raise UnexpectedResponseError.new(resp_type, expected_resp_type) unless resp_type == expected_resp_type
|
243
|
+
check_response_type(resp, "newChatItems")
|
284
244
|
|
285
245
|
resp["chatItems"]
|
286
246
|
end
|
287
247
|
|
288
248
|
def api_send_file(chat_type, receiver, file_path)
|
289
249
|
resp = send_command "/file #{chat_type}#{receiver} #{file_path}"
|
290
|
-
|
291
|
-
expected_resp_type = "newChatItems"
|
292
|
-
raise UnexpectedResponseError.new(resp_type, expected_resp_type) unless resp_type == expected_resp_type
|
250
|
+
check_response_type(resp, "newChatItems")
|
293
251
|
|
294
252
|
resp["chatItems"]
|
295
253
|
end
|
296
254
|
|
297
255
|
def api_contacts
|
298
256
|
resp = send_command "/contacts"
|
299
|
-
|
300
|
-
expected_resp_type = "contactsList"
|
301
|
-
raise UnexpectedResponseError.new(resp_type, expected_resp_type) unless resp_type == expected_resp_type
|
257
|
+
check_response_type(resp, "contactsList")
|
302
258
|
|
303
259
|
contacts = resp["contacts"]
|
304
260
|
contacts.map{ |c| {
|
@@ -313,9 +269,7 @@ module SimpleXChat
|
|
313
269
|
|
314
270
|
def api_groups
|
315
271
|
resp = send_command "/groups"
|
316
|
-
|
317
|
-
expected_resp_type = "groupsList"
|
318
|
-
raise UnexpectedResponseError.new(resp_type, expected_resp_type) unless resp_type == expected_resp_type
|
272
|
+
check_response_type(resp, "groupsList")
|
319
273
|
|
320
274
|
groups = resp["groups"]
|
321
275
|
groups.map{ |entry|
|
@@ -341,18 +295,14 @@ module SimpleXChat
|
|
341
295
|
onoff = is_enabled && "on" || "off"
|
342
296
|
|
343
297
|
resp = send_command "/auto_accept #{onoff}"
|
344
|
-
|
345
|
-
expected_resp_type = "userContactLinkUpdated"
|
346
|
-
raise UnexpectedResponseError.new(resp_type, expected_resp_type) unless resp_type == expected_resp_type
|
298
|
+
check_response_type(resp, "userContactLinkUpdated")
|
347
299
|
|
348
300
|
nil
|
349
301
|
end
|
350
302
|
|
351
303
|
def api_kick_group_member(group, member)
|
352
304
|
resp = send_command "/remove #{group} #{member}"
|
353
|
-
|
354
|
-
expected_resp_type = "userDeletedMember"
|
355
|
-
raise UnexpectedResponseError.new(resp_type, expected_resp_type) unless resp_type == expected_resp_type
|
305
|
+
check_response_type(resp, "userDeletedMember")
|
356
306
|
end
|
357
307
|
|
358
308
|
# Parameters for /network:
|
@@ -375,18 +325,100 @@ module SimpleXChat
|
|
375
325
|
command += " #{param}=#{value}"
|
376
326
|
end
|
377
327
|
resp = send_command command
|
378
|
-
|
379
|
-
expected_resp_type = "networkConfig"
|
380
|
-
raise UnexpectedResponseError.new(resp_type, expected_resp_type) unless resp_type == expected_resp_type
|
328
|
+
check_response_type(resp, "networkConfig")
|
381
329
|
|
382
330
|
resp["networkConfig"]
|
383
331
|
end
|
384
332
|
|
333
|
+
def api_tail(chat_type: nil, conversation: nil, message_count: nil)
|
334
|
+
cmd = "/tail"
|
335
|
+
cmd += " #{chat_type}#{conversation}" if chat_type != nil && conversation != nil
|
336
|
+
cmd += " #{message_count}" if message_count != nil
|
337
|
+
resp = send_command cmd
|
338
|
+
check_response_type(resp, "chatItems")
|
339
|
+
|
340
|
+
resp["chatItems"].map{|chat_item| parse_chat_item chat_item}
|
341
|
+
end
|
342
|
+
|
343
|
+
def api_chats(
|
344
|
+
chat_count=20 # if nil, will return all the chats
|
345
|
+
)
|
346
|
+
param = chat_count != nil ? "#{chat_count}" : "all"
|
347
|
+
cmd = "/chats #{param}"
|
348
|
+
resp = send_command cmd
|
349
|
+
check_response_type(resp, "chats")
|
350
|
+
|
351
|
+
resp["chats"].map do |chat|
|
352
|
+
chat_type = parse_chat_info_type chat["chatInfo"]["type"]
|
353
|
+
next if chat_type == nil # WARN: Chat type "local" is currently ignored
|
354
|
+
conversation = nil
|
355
|
+
if chat_type == ChatType::GROUP
|
356
|
+
conversation = chat["chatInfo"]["groupInfo"]["localDisplayName"]
|
357
|
+
else
|
358
|
+
conversation = chat["chatInfo"]["contact"]["localDisplayName"]
|
359
|
+
end
|
360
|
+
|
361
|
+
{
|
362
|
+
:chat_type => chat_type,
|
363
|
+
:conversation => conversation
|
364
|
+
}
|
365
|
+
end.filter { |x| x != nil }
|
366
|
+
end
|
367
|
+
|
385
368
|
private
|
386
369
|
|
370
|
+
def check_response_type(resp, expected_resp_type)
|
371
|
+
resp_type = resp["type"]
|
372
|
+
raise UnexpectedResponseError.new(resp_type, expected_resp_type) unless resp_type == expected_resp_type
|
373
|
+
end
|
374
|
+
|
387
375
|
def next_corr_id
|
388
376
|
# The correlation ID has to be a string
|
389
377
|
(@corr_id.update { |x| x + 1 } - 1).to_s(10)
|
390
378
|
end
|
379
|
+
|
380
|
+
def parse_chat_info_type(type)
|
381
|
+
chat_info_types = {
|
382
|
+
"direct" => ChatType::DIRECT,
|
383
|
+
"group" => ChatType::GROUP
|
384
|
+
}
|
385
|
+
|
386
|
+
chat_info_types.dig(type)
|
387
|
+
end
|
388
|
+
|
389
|
+
def parse_chat_item(chat_item)
|
390
|
+
chat_type = parse_chat_info_type chat_item["chatInfo"]["type"]
|
391
|
+
group = nil
|
392
|
+
sender = nil
|
393
|
+
contact = nil
|
394
|
+
contact_role = nil
|
395
|
+
if chat_type == ChatType::GROUP
|
396
|
+
# NOTE: The group can "send messages" without a contact
|
397
|
+
# For example, when a member is removed, the group
|
398
|
+
# sends a message about his removal, with no contact
|
399
|
+
contact = chat_item.dig "chatItem", "chatDir", "groupMember", "localDisplayName"
|
400
|
+
contact_role = chat_item.dig "chatItem", "chatDir", "groupMember", "memberRole"
|
401
|
+
group = chat_item["chatInfo"]["groupInfo"]["localDisplayName"]
|
402
|
+
sender = group
|
403
|
+
else
|
404
|
+
contact = chat_item["chatInfo"]["contact"]["localDisplayName"]
|
405
|
+
sender = contact
|
406
|
+
end
|
407
|
+
|
408
|
+
msg_text = chat_item["chatItem"]["meta"]["itemText"]
|
409
|
+
timestamp = Time.parse(chat_item["chatItem"]["meta"]["updatedAt"])
|
410
|
+
image_preview = chat_item.dig "chatItem", "content", "msgContent", "image"
|
411
|
+
|
412
|
+
chat_message = {
|
413
|
+
:chat_type => chat_type,
|
414
|
+
:sender => sender,
|
415
|
+
:contact_role => contact_role,
|
416
|
+
:contact => contact,
|
417
|
+
:group => group,
|
418
|
+
:msg_text => msg_text,
|
419
|
+
:msg_timestamp => timestamp,
|
420
|
+
:img_preview => image_preview
|
421
|
+
}
|
422
|
+
end
|
391
423
|
end
|
392
424
|
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.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- rdbo
|
8
8
|
bindir: exe
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-03-
|
10
|
+
date: 2025-03-04 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: websocket
|