simplex-chat 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d837e851181b5797fc07d870cbb4170030de614570d1b960ff44a16272e4d765
4
- data.tar.gz: 9c031346b853a2a82a3b11314be2f56b65a381f15cdfddc0e12efd2084f899b6
3
+ metadata.gz: 1dde8b2fbd228f72ec1b9cb4f95269401aa2fa68774b38fdbef43d8eec2b7d63
4
+ data.tar.gz: 1c54b1064c53ceeb06ae4b0f68715e773e96753208ec87b7ad445e2863649969
5
5
  SHA512:
6
- metadata.gz: 32b2980044aeeb7a216ae9d8fa4a9f1c0dd075635740a71b3160a83d9e481f126b180397b6d0cae74562434149e3a0f7d60595eebcaacba66ec2c80640cfef65
7
- data.tar.gz: fee2870de270b270750f1e82421ba24ed1689df71a40c1113b1386f907beb11e582a2e3ddb2027c139ba273ff48e6ac4e8f66a12bcefe2d792382e3c0eb4f1c1
6
+ metadata.gz: 669c992a7cbab4e903581bae8def4f6a4dc6e269cc5041e8e0dd5901bc62e8d162aaf4aa95ab0f266932619e34c48d10400107dfd84d4a8cc60d64c51a40711a
7
+ data.tar.gz: f6c49bb438a61eb4015cd94e5f29197214b36a7eb3d18f5a8bee05ecc2d7cbcf284f10d792cb3b0aad8aa9f3c41be36a78875b5c38b13961770aa835b829823c
data/README.md CHANGED
@@ -5,3 +5,52 @@ A port for the SimpleX Chat client API for Ruby
5
5
  This project is licensed under the GNU AGPL-3.0 (no later versions).
6
6
 
7
7
  Read `LICENSE` for more information.
8
+
9
+ ## Showcase
10
+
11
+ ![showcase](showcase.png)
12
+
13
+ ## Usage
14
+
15
+ 1. Install the Gem from RubyGems
16
+ ```shell
17
+ gem install simplex-chat
18
+ ```
19
+
20
+
21
+ 2. Start your local simplex-chat client on port 5225 (or any port you wish)
22
+
23
+ ```shell
24
+ simplex-chat -p 5225
25
+ ```
26
+
27
+ 3. Connect the `SimpleXChat::ClientAgent` to your local client
28
+
29
+ ```rb
30
+ require 'simplex-chat'
31
+ require 'net/http'
32
+
33
+ client = SimpleXChat::ClientAgent.new URI('ws://localhost:5225')
34
+ ```
35
+
36
+
37
+ 4. Now the client is connected and you can start using the APIs
38
+
39
+ ```rb
40
+ # Get version
41
+ version = client.api_version
42
+ puts "SimpleX Chat version: #{version}"
43
+
44
+ # Listen to incoming client messages
45
+ loop do
46
+ chat_msg = client.next_chat_message
47
+ break if chat_msg == nil
48
+
49
+ # Reply if user sends '/say_hello'
50
+ if chat_msg[:msg_text] == "/say_hello"
51
+ client.api_send_text_message chat_msg[:chat_type], chat_msg[:sender], "Hello! This was sent automagically"
52
+ end
53
+ end
54
+
55
+ # Much more... Read the examples for more information
56
+ ```
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SimpleXChat
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/simplex-chat.rb CHANGED
@@ -8,6 +8,7 @@ module SimpleXChat
8
8
  require 'json'
9
9
  require 'websocket'
10
10
  require 'concurrent'
11
+ require 'time'
11
12
 
12
13
  # Fixes regex match for status line in HTTPResponse
13
14
  class HTTPResponse < Net::HTTPResponse
@@ -33,6 +34,7 @@ module SimpleXChat
33
34
  def initialize client_uri, connect: true, log_level: Logger::INFO
34
35
  @uri = client_uri
35
36
  @message_queue = SizedQueue.new 4096
37
+ @chat_message_queue = Queue.new
36
38
  @socket = nil
37
39
  @handshake = nil
38
40
 
@@ -97,7 +99,7 @@ module SimpleXChat
97
99
  rescue => e
98
100
  # TODO: Verify if this way of stopping the execution
99
101
  # is graceful enough after implementing reconnects
100
- puts "Unhandled exception caught: #{e}"
102
+ @logger.error "Unhandled exception caught: #{e}"
101
103
  @message_queue.close
102
104
  raise e
103
105
  end
@@ -111,10 +113,72 @@ module SimpleXChat
111
113
  @message_queue.pop
112
114
  end
113
115
 
116
+ def next_chat_message
117
+ # NOTE: There can be more than one message per
118
+ # client message. Because of that, we use
119
+ # a chat message queue to insert one or
120
+ # more messages at a time, but poll just
121
+ # one at a time
122
+ return @chat_message_queue.pop if not @chat_message_queue.empty?
123
+
124
+ loop do
125
+ msg = next_message
126
+ break if msg == nil
127
+ next if not ["chatItemUpdated", "newChatItems"].include?(msg["type"])
128
+
129
+ chat_info_types = {
130
+ "direct" => ChatType::DIRECT,
131
+ "group" => ChatType::GROUP
132
+ }
133
+
134
+ # Handle one or more chat messages in a single client message
135
+ new_chat_messages = nil
136
+ if msg["type"] == "chatItemUpdated"
137
+ new_chat_messages = [msg["chatItem"]]
138
+ else
139
+ new_chat_messages = msg["chatItems"]
140
+ end
141
+
142
+ new_chat_messages.each do |chat_item|
143
+ chat_type = chat_info_types.dig(chat_item["chatInfo"]["type"])
144
+ group = nil
145
+ sender = nil
146
+ contact = nil
147
+ if chat_type == ChatType::GROUP
148
+ contact = chat_item["chatItem"]["chatDir"]["groupMember"]["localDisplayName"]
149
+ group = chat_item["chatInfo"]["groupInfo"]["localDisplayName"]
150
+ sender = group
151
+ else
152
+ contact = chat_item["chatInfo"]["contact"]["localDisplayName"]
153
+ sender = contact
154
+ end
155
+
156
+ msg_text = chat_item["chatItem"]["meta"]["itemText"]
157
+ timestamp = chat_item["chatItem"]["meta"]["updatedAt"]
158
+
159
+ chat_message = {
160
+ :chat_type => chat_type,
161
+ :sender => sender,
162
+ :contact => contact,
163
+ :group => group,
164
+ :msg_text => msg_text,
165
+ :msg_timestamp => Time.parse(timestamp)
166
+ }
167
+
168
+ @chat_message_queue.push chat_message
169
+ end
170
+
171
+ return @chat_message_queue.pop
172
+ end
173
+
174
+ nil
175
+ end
176
+
114
177
  def disconnect
115
178
  @listener_thread.terminate
116
179
  @socket.close
117
180
  @message_queue.clear
181
+ @chat_message_queue.clear
118
182
  end
119
183
 
120
184
  # Sends a raw command to the SimpleX Chat client
data/showcase.png ADDED
Binary file
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.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - rdbo
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-02-22 00:00:00.000000000 Z
10
+ date: 2025-02-23 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: websocket
@@ -49,6 +49,7 @@ files:
49
49
  - Rakefile
50
50
  - lib/simplex-chat.rb
51
51
  - lib/simplex-chat/version.rb
52
+ - showcase.png
52
53
  homepage: https://github.com/rdbo/simplex-chat-ruby
53
54
  licenses:
54
55
  - AGPL-3.0-only