From 841e775439cbfff741a899e6ba1b50daecafbd87 Mon Sep 17 00:00:00 2001 From: Chauncey Date: Tue, 11 Jun 2013 13:17:56 -0600 Subject: [PATCH] Fixing issue where private messaging jarvis would not reply with the executed command --- project.clj | 5 +++-- src/main/clojure/jarvis/bot.clj | 17 +++++++++++------ src/main/clojure/jarvis/command.clj | 17 +++++++++++------ src/main/clojure/jarvis/test_util.clj | 14 +++++++++++++- src/test/clojure/jarvis/command_test.clj | 14 +++++++++----- src/test/clojure/jarvis/plugins/join_test.clj | 6 ++++++ 6 files changed, 53 insertions(+), 20 deletions(-) create mode 100644 src/test/clojure/jarvis/plugins/join_test.clj diff --git a/project.clj b/project.clj index 537ae45..661f739 100644 --- a/project.clj +++ b/project.clj @@ -1,10 +1,10 @@ -(defproject com.rallydev/jarvis-core "1.0.0" +(defproject com.rallydev/jarvis-core "1.1.0" :description "A robot for FlowDock" :url "http://github.com/RallySoftware/jarvis-core" :license {:name "The MIT License (MIT)" :url "http://opensource.org/licenses/MIT"} :dependencies [[org.clojure/clojure "1.5.0"] - [com.rallydev/clj-flowdock "1.1.0"] + [com.rallydev/clj-flowdock "1.1.1"] [org.codehaus.groovy/groovy-all "2.1.0"] [org.clojure/java.classpath "0.2.0"] [org.clojure/tools.logging "0.2.6"] @@ -15,5 +15,6 @@ :java-source-paths ["src/main/java"] :test-paths ["src/test/clojure"] :resource-paths ["src/main/resources"] + :main jarvis.bot :plugins [[lein-clojars "0.9.1"]] :jvm-opts ["-Xmx1G" "-DLOG_DIR=./logs"]) diff --git a/src/main/clojure/jarvis/bot.clj b/src/main/clojure/jarvis/bot.clj index 3e2d6b8..b389683 100644 --- a/src/main/clojure/jarvis/bot.clj +++ b/src/main/clojure/jarvis/bot.clj @@ -1,5 +1,6 @@ (ns jarvis.bot (:require [jarvis.command :as command] + [jarvis.bot :as jarvis] [jarvis.plugins :as plugins] [jarvis.util :as util] [clj-flowdock.api.flow :as flow] @@ -13,7 +14,7 @@ (def threadpool (atom (Executors/newFixedThreadPool (util/config-property "THREAD_POOL_SIZE" 100)))) (defmacro listen [[flow message-sym flow-con-sym] & body] - `(with-open [~flow-con-sym (streaming/open (flow/flow->flow-id ~flow))] + `(with-open [~flow-con-sym (streaming/open ~flow)] (loop [] (when-let [~message-sym (.read ~flow-con-sym)] ~@body) @@ -37,12 +38,13 @@ (defn user-stream [plugins] (listen ["" msg flow-connection] - (cond - (command/join-command? msg) (init-flow-thread (get msg "content") plugins) - (command/private-message? msg) (command/private-message msg (command/command->plugin msg plugins))))) + (let [enhanced-message (util/enhance-message msg)] + (cond + (command/join-command? enhanced-message) (init-flow-thread (get enhanced-message "content") plugins) + (command/private-message? enhanced-message) (command/private-message enhanced-message (command/command->plugin enhanced-message plugins)))))) (defn init-flow-thread [flow plugins] - (.submit @threadpool #(flow-stream flow plugins))) + (.submit @threadpool #(flow-stream (flow/flow->flow-id flow) plugins))) (defn init-user-thread [plugins] (.submit @threadpool #(user-stream plugins))) @@ -56,4 +58,7 @@ (log/info "Starting Jarvis...") (when-let [plugins (plugins/load-plugins)] (log/info "Starting to read from flowdock streams") - (init-threads plugins))) \ No newline at end of file + (init-threads plugins))) + +(defn -main [] + (jarvis/init)) \ No newline at end of file diff --git a/src/main/clojure/jarvis/command.clj b/src/main/clojure/jarvis/command.clj index 4d9be58..0cd98a4 100644 --- a/src/main/clojure/jarvis/command.clj +++ b/src/main/clojure/jarvis/command.clj @@ -27,7 +27,9 @@ (.startsWith content "~~"))) (defn private-message? [message] - (contains? message "to")) + (and + (contains? message "to") + (= "message" (get message "event")))) (defn leave-command? [message] (when-let [content (m/content message)] @@ -35,8 +37,8 @@ (not (map? content)) (.equals content "~leave")))) -(defn join-command? [msg] - (= "flow-add" (get msg "event"))) +(defn join-command? [message] + (= "flow-add" (get message "event"))) (defn parse-command [message-content] (let [content-vec (s/split message-content #" ") @@ -54,9 +56,12 @@ (get-in message ["user" "id"])) (defn get-user-ids-from-tags [message] - (->> (message "tags") - (filter #(.startsWith % ":user:")) - (map #(user->id %)))) + (if (< 0 (count (message "tags"))) + (->> message + (message "tags") + (filter #(.startsWith % ":user:")) + (map #(user->id %))) + (get-user-id-from-message message))) (defn- user->id [user] (re-find #"\d+" user)) \ No newline at end of file diff --git a/src/main/clojure/jarvis/test_util.clj b/src/main/clojure/jarvis/test_util.clj index 2c9b1bf..bba62f7 100644 --- a/src/main/clojure/jarvis/test_util.clj +++ b/src/main/clojure/jarvis/test_util.clj @@ -75,4 +75,16 @@ "content" {"id" "rally-software:devellopers", "name" "developers", "organization" "Rally Software", - "open" true}}) \ No newline at end of file + "open" true}}) + +(defn flow-user-typing-message [] + {"app" nil, + "user" 29988, + "attachments" [], + "sent" 1370968225230, + "tags" [], + "content" {"typing" "29988-35899-chat-input"}, + "event" "activity.user", + "id" 283833720, + "to" 35899, + "uuid" nil}) \ No newline at end of file diff --git a/src/test/clojure/jarvis/command_test.clj b/src/test/clojure/jarvis/command_test.clj index 0d0ff25..1744907 100644 --- a/src/test/clojure/jarvis/command_test.clj +++ b/src/test/clojure/jarvis/command_test.clj @@ -33,7 +33,9 @@ (deftest get-user-id-from-message (is (= :29983 - (command/get-user-id-from-message (test-util/private-message "~help"))))) + (command/get-user-id-from-message (test-util/private-message "~help")))) + (is (= :29983 + (command/get-user-id-from-message (test-util/message "~help"))))) (deftest leave-command? (is (= true @@ -47,14 +49,16 @@ (is (= true (command/join-command? (test-util/flow-add-message)))) (is (= false - (command/leave-command? (test-util/message "test")))) + (command/join-command? (test-util/message "test")))) (is (= false - (command/leave-command? (test-util/private-message "foo"))))) + (command/join-command? (test-util/private-message "foo"))))) (deftest private-message? (is (= true (command/private-message? (test-util/private-message "private message")))) (is (= false - (command/leave-command? (test-util/message "test")))) + (command/private-message? (test-util/message "test")))) + (is (= false + (command/private-message? (test-util/flow-add-message)))) (is (= false - (command/leave-command? (test-util/flow-add-message))))) \ No newline at end of file + (command/private-message? (test-util/flow-user-typing-message))))) \ No newline at end of file diff --git a/src/test/clojure/jarvis/plugins/join_test.clj b/src/test/clojure/jarvis/plugins/join_test.clj new file mode 100644 index 0000000..e4fa237 --- /dev/null +++ b/src/test/clojure/jarvis/plugins/join_test.clj @@ -0,0 +1,6 @@ +(ns jarvis.plugins.join-test + (:use clojure.test) + (:require [jarvis.plugins.join :as join] + [jarvis.test-util :as test-util])) + +; test stuff