Commit 736b5ba

mo <mo.khan@gmail.com>
2018-05-19 16:51:58
extract methods.
1 parent 35392f8
Changed files (1)
lib/del/connection.rb
@@ -7,51 +7,16 @@ module Del
 
     def initialize(configuration:)
       @configuration = configuration
-      @mucs = {}
+      @rooms = {}
     end
 
     def connect(robot)
-      client.on_exception do |error, _connection, _error_source|
-        Del.logger.error(error)
-        disconnect
-      end
-      client.connect(configuration.host)
-      sleep 0.0001 until client.is_connected?
-      client.auth(configuration.password)
-      roster = Jabber::Roster::Helper.new(client, false)
-      roster.add_update_callback do |_old_item, item|
-        configuration.users.upsert(item['jid'], item.attributes) if item
-      end
-      roster.get_roster
-      roster.wait_for_roster
-      client.add_message_callback do |message|
-        next if message.type == :error || message.body.nil?
-        user = configuration.users.find(message.from.strip)
-        robot.receive(message.body, source: Source.new(user: user))
-      end
-      client.send(Jabber::Presence.new(:chat))
-      configuration.default_rooms.each do |room|
-        Del.logger.debug("Joining room '#{room}' as '#{robot.name}'")
-        room_jid = jid_for(room, configuration.muc_domain.dup, robot.name)
-        stripped_jid = room_jid.strip.to_s
-        next if @mucs[stripped_jid]
-
-        muc = Jabber::MUC::SimpleMUCClient.new(client)
-        @mucs[stripped_jid] = muc
-        muc.on_message do |_, nickname, message|
-          Del.logger.debug([nickname, message].inspect)
-          other_jid = roster.items.find { |_jid, item| item.iname == nickname }
-          source = Source.new(
-            user: User.new(other_jid[0], other_jid[1]),
-            room: stripped_jid
-          )
-          robot.receive(message, source: source)
-        end
-        muc.join(room_jid)
-      end
-      # list_rooms(configuration.muc_domain).each do |room|
-      # rooms.upsert(room)
-      # end
+      record_exceptions
+      connect_to_xmpp_server
+      roster = discover_users
+      listen_for_direct_messages(robot)
+      update_status
+      discover_rooms(robot, roster)
     end
 
     def deliver(jid, message)
@@ -61,8 +26,8 @@ module Del
     end
 
     def deliver_to_room(jid, message)
-      muc = @mucs[jid.strip.to_s]
-      muc&.say(encode_string(message))
+      multi_user_chat = @rooms[jid.strip.to_s]
+      multi_user_chat&.say(encode_string(message))
     end
 
     def disconnect
@@ -82,9 +47,9 @@ module Del
       @jid ||= jid_for(configuration.jid, 'chat.hipchat.com', 'bot')
     end
 
-    def list_rooms(muc_domain)
+    def list_rooms(multi_user_chat_domain)
       browser = Jabber::MUC::MUCBrowser.new(client)
-      browser.muc_rooms(muc_domain).map do |jid, _name|
+      browser.muc_rooms(multi_user_chat_domain).map do |jid, _name|
         jid.to_s
       end
     end
@@ -102,5 +67,72 @@ module Del
       end
       jid
     end
+
+    def record_exceptions
+      client.on_exception do |error, _connection, _error_source|
+        Del.logger.error(error)
+        disconnect
+      end
+    end
+
+    def connect_to_xmpp_server
+      client.connect(configuration.host)
+      sleep 0.0001 until client.is_connected?
+      client.auth(configuration.password)
+    end
+
+    def discover_users
+      roster = Jabber::Roster::Helper.new(client, false)
+      roster.add_update_callback do |_old_item, item|
+        configuration.users.upsert(item['jid'], item.attributes) if item
+      end
+      roster.get_roster
+      roster.wait_for_roster
+      roster
+    end
+
+    def listen_for_direct_messages(robot)
+      client.add_message_callback do |message|
+        next if message.type == :error || message.body.nil?
+        user = configuration.users.find(message.from.strip)
+        robot.receive(message.body, source: Source.new(user: user))
+      end
+    end
+
+    def update_status(status = :chat)
+      client.send(Jabber::Presence.new(status))
+    end
+
+    def discover_rooms(robot, roster)
+      configuration.default_rooms.each do |room|
+        Del.logger.debug("Joining room '#{room}' as '#{robot.name}'")
+        room_jid = jid_for(room, configuration.muc_domain.dup, robot.name)
+        stripped_jid = room_jid.strip.to_s
+        next if @rooms[stripped_jid]
+
+        multi_user_chat =
+          @rooms[stripped_jid] = Jabber::MUC::SimpleMUCClient.new(client)
+        listen_for_room_messages(multi_user_chat, room_jid, robot, roster)
+      end
+      # list_rooms(configuration.muc_domain).each do |room|
+      # rooms.upsert(room)
+      # end
+    end
+
+    def listen_for_room_messages(multi_user_chat, room_jid, robot, roster)
+      multi_user_chat.on_message do |_, nickname, message|
+        Del.logger.debug([nickname, message].inspect)
+        user = find_user_with(nickname, roster)
+        source = Source.new(user: user, room: room_jid.strip.to_s)
+        robot.receive(message, source: source)
+      end
+      multi_user_chat.join(room_jid)
+    end
+
+    def find_user_with(nickname, roster)
+      other_jid = roster.items.find { |_jid, item| item.iname == nickname }
+      user = configuration.users.find(other_jid)
+      user.nil? ? User.new(other_jid[0], other_jid[1]) : user
+    end
   end
 end