~singpolyma/jabber-client-demo

497faad180943c70340620f33db25973830b7dc3 — Stephen Paul Weber 2 years ago 23293c0
Jabber Client MVP
3 files changed, 139 insertions(+), 0 deletions(-)

A .gitignore
A Gemfile
A client.rb
A .gitignore => .gitignore +3 -0
@@ 0,0 1,3 @@
.bundle/
.gems/
Gemfile.lock
\ No newline at end of file

A Gemfile => Gemfile +6 -0
@@ 0,0 1,6 @@
# frozen_string_literal: true

source "https://rubygems.org"

gem "blather", git: "https://github.com/adhearsion/blather", branch: "develop"
gem "glimmer-dsl-libui", "~> 0.5.24"

A client.rb => client.rb +130 -0
@@ 0,0 1,130 @@
# frozen_string_literal: true

require "glimmer-dsl-libui"
require "blather/client"

BLATHER = self
include Glimmer

$conversations = {}
$roster = [["", ""]]

class Conversation
	include Glimmer

	def self.open(jid, m=nil)
		return if $conversations[jid]

		($conversations[jid] = new(jid, m)).launch
	end

	def initialize(jid, m=nil)
		@jid = jid
		@messages = [["", ""]]
		new_message(m) if m
	end

	def launch
		window("Conversation With #{@jid}") {
			vertical_box {
				table {
					text_column("Sender")
					text_column("Message")
					editable false
					cell_rows @messages
					@messages.clear
				}

				horizontal_box {
					stretchy false

					message_entry = entry
					button("Send") {
						stretchy false

						on_clicked do
							BLATHER.say(@jid, message_entry.text)
							@messages << [ARGV[0], message_entry.text]
							message_entry.text = ""
						end
					}
				}
			}

			on_closing do
				$conversations.delete(@jid.to_s)
			end
		}.show
	end

	def format_sender(jid)
		BLATHER.my_roster[jid]&.name || jid
	end

	def message_row(m)
		[
			format_sender(m.from&.stripped || BLATHER.jid.stripped),
			m.body
		]
	end

	def new_message(m)
		@messages << message_row(m)
	end
end

Thread.new do
	window("Contacts") {
		vertical_box {
			table {
				button_column("Contact") {
					on_clicked do |row|
						Conversation.open($roster[row][1].to_s)
					end
				}
				editable false
				cell_rows $roster
			}

			horizontal_box {
				stretchy false

				jid_entry = entry {
					label("Jabber ID")
				}

				button("Start Conversation") {
					stretchy false

					on_clicked do
						Conversation.open(jid_entry.text)
					end
				}
			}
		}

		on_destroy {
			BLATHER.shutdown
		}
	}.show
end

message :body do |m|
	LibUI.queue_main do
		conversation = $conversations[m.from.stripped.to_s]
		if conversation
			conversation.new_message(m)
		else
			Conversation.open(m.from.stripped.to_s, m)
		end
	end
end

after(:roster) do
	LibUI.queue_main do
		$roster.clear
		my_roster.each do |item|
			$roster << [item.name || item.jid, item.jid]
		end
	end
end