Skip to content

Commit

Permalink
many compository and improvements
Browse files Browse the repository at this point in the history
grooves are specify their grammar just once, and separately which channels can can carry them, as well as how to match for that groove on the grammar, and also what elements of the content (or the groove match) to display in the preview.

Updated the UI to use this new info and display previews based on the groove definition.
  • Loading branch information
zippy committed Nov 3, 2011
1 parent 543c160 commit b0401a2
Show file tree
Hide file tree
Showing 15 changed files with 241 additions and 171 deletions.
13 changes: 6 additions & 7 deletions public/ss/src/droplet.cljs
Expand Up @@ -60,15 +60,15 @@
groove-id (keyword groove-name)
groove (groove-id s/*grooves*)
selected-channel (. *channel-select* (getSelectedItem))
groove-spec ((ssu/get-channel-type-from-name (. selected-channel (getValue))) groove)]
grammar (s/get-groove-grammar groove-id)]
(doseq [idx (range (. *channel-select* (getItemCount)))]
(let [item (. *channel-select* (getItemAt idx))
chan (. item (getValue))
valid (contains? groove (ssu/get-channel-type-from-name chan))]
valid (contains? (:carriers groove) (ssu/get-channel-type-from-name chan))]
(. item (setEnabled valid))))
(let [valid-channel (and selected-channel (. selected-channel (isEnabled)))]
(if valid-channel
(render-groove :droplet-content groove-spec)
(render-groove :droplet-content grammar)
(d/remove-children :droplet-content))
(. *incorp-button* (setEnabled valid-channel)))
)
Expand Down Expand Up @@ -135,15 +135,14 @@
(defn send []
(let [groove-name (. *groove-select* (getValue))
groove-id (keyword groove-name)
groove (groove-id s/*grooves*)
selected-channel (. *channel-select* (getSelectedItem))
channel-name (. selected-channel (getValue))
groove-spec ((ssu/get-channel-type-from-name channel-name) groove)
grammar (s/get-groove-grammar groove-id)
to (. *to-select* (getValue))
from (. *from-select* (getValue))
groove (get-selected-groove)]
(ssu/send-ss-signal {:aspect "matrice" :signal "incorporate"
:params {:deliver :immediate :to (js/parseInt to) :from (js/parseInt from) :channel channel-name :envelope groove-spec
:content (get-groove-content groove-spec)}}
:params {:deliver :immediate :to (js/parseInt to) :from (js/parseInt from) :channel channel-name :envelope grammar
:content (get-groove-content grammar)}}
incorporate-callback)
(ui/cancel-modal)))
17 changes: 12 additions & 5 deletions public/ss/src/state.cljs
Expand Up @@ -27,7 +27,6 @@
(defn clear-session []
(def *current-state* nil)
(def *grooves* nil)
(def *groove-actions* nil)
(def *me* nil)
(def *page* 1)
(clear-scape-query)
Expand All @@ -45,11 +44,19 @@
[g]
(def *grooves* g)
)
(defn set-groove-actions
"set the current streamscapes groove actions"
[g]
(def *groove-actions* g)

(defn get-groove-grammar [groove]
(-> *grooves* groove :grammar)
)

(defn get-groove-preview [groove]
(-> *grooves* groove :preview)
)

(defn get-groove-channel-actions [groove channel-type]
(-> *grooves* groove :carriers channel-type :actions)
)

(defn set-me
"store my contact address"
[c]
Expand Down
22 changes: 8 additions & 14 deletions public/ss/src/stream.cljs
Expand Up @@ -160,27 +160,21 @@
(defn get-droplet-grammar [d channel-type s]
(let [dg (:values (:droplet-grooves-scape (:scapes s)))
groove-name (keyword (first ((keyword (str (:address d))) dg )))

]
[groove-name
(channel-type (groove-name s/*grooves*))
(channel-type (groove-name s/*groove-actions*))]))
(s/get-groove-grammar groove-name)
(s/get-groove-channel-actions groove-name channel-type)]))

;;TODO: groove droplets should be auto-detected by some appropriate
;;programmatic method, not by channel-type!
(defn groove-preview [d channel-type s]
(let [[groove grammar actions] (get-droplet-grammar d channel-type s)
p (if (contains? grammar :subject )
[:div.subject
(str (:subject (:content d)))]
[:div.content (str (if (nil? (:text (:content d)))
(:message (:content d))
(if (nil? (:text (:content d)))
(:description (:content d))
(:text (:content d)))))])
]
[p actions groove]
))
preview (s/get-groove-preview groove)
p (if (vector? preview)
[:div.content (apply str (map (fn [i] (if (vector? i) ((keyword (i 0)) (-> d :matched-grooves groove)) i)) preview))]
[:div.content ((keyword preview) (:content d))]
)]
[p actions groove]))

;; Actions

Expand Down
7 changes: 3 additions & 4 deletions public/ss/src/streamscapes.cljs
Expand Up @@ -78,10 +78,9 @@
(fn [e] (let [{status :status result :result} (ceptr/handle-xhr e)]
(if (= status "ok")
(let [receptors (:receptors result)
grooves (into {} (map (fn [[groove-name addr]] [groove-name (:grammars ((keyword addr) receptors))]) (-> result :scapes :groove-scape :values)))
actions (into {} (map (fn [[groove-name addr]] [groove-name (:actions ((keyword addr) receptors))]) (-> result :scapes :groove-scape :values)))]
(s/set-grooves grooves)
(s/set-groove-actions actions))
grooves (into {} (map (fn [[groove-name addr]] [groove-name ((keyword addr) receptors)]) (-> result :scapes :groove-scape :values)))
]
(s/set-grooves grooves))
)
)) )
)
Expand Down
55 changes: 4 additions & 51 deletions src/anansi/receptor/host.clj
Expand Up @@ -6,57 +6,10 @@
[anansi.receptor.user]
[anansi.receptor.scape]
[anansi.streamscapes.streamscapes]
[anansi.streamscapes.groove :only [groove-def]]
[anansi.streamscapes.groove :only [groove-def compository]]
[anansi.libs.sha :only [sha]])
(:use [clj-time.core :only [now]]))

;;(when-carried-by :hash :subject-body-message {:match-fun (fn [hash params] (every? #(contains? hash %) params)) :match-params [:subject :body]})
;;(def carrier-grammars {:hash {:subject-body-message {:subject "text/plain" :body "text/html"}}})
(def compository
{:simple-message {:actions {:streamscapes [:create :reply]
:irc [:create :reply]}
:grammars {:streamscapes {:message "text/plain"}
:twitter {:text "text/plain"}
:irc {:message "text/plain"}}}
:subject-body-message {:actions {:streamscapes {:create true :reply true}
:email [:create :reply]}
:grammars {:streamscapes {:subject "text/plain" :body "text/html"}
:email {:subject "text/plain" :body "text/html"}}}
:punkmoney {:actions {:streamscapes [:create]
:email {:create true :reply true}}
:grammars
{:streamscapes {:promised-good "text/plain"
:expiration "text/plain"}
:email {:subject {"text" ["Punkmoney Promise"]}
:body {"text"
["I promise to pay (.*), on demand, ([^.]+)\\. Expires (.*)\\."
{:payee 1
:promised-good 2
:expiration 3}
]}}
:twitter {:text {"text"
["^(@[^\\W]+) I promise to pay, on demand, ([^.]+)\\. Expires (.*)\\. #punkmoney"
{:payee 1
:promised-good 2
:expiration 3}
]}}}}
:poll {:actions {:streamscapes [:create]}
:grammars {:streamscapes {:poll-name "text/plain"
:options "enumeration/yes,no,abstain"}}}
:bookmark {:actions {:streamscapes [:create]}
:grammars {:streamscapes {:url "text/plain"
:subject "text/html"}}}
:lazyweb-thanks {:actions {:streamscapes [:create]}
:grammars
{:streamscapes {:thankee "text/plain"
:expiration "text/plain"}
:twitter {:text {"text"
["#lazyweb thanks (@[^\\W]+)"
{:thankee 1}
]}}}}

}
)
(def host-def (receptor-def
"host"
(scapes
Expand All @@ -70,7 +23,7 @@
(let [grooves (get-scape _r :groove)]
(doseq [[groove-name spec] compository]
(if (nil? (--> key->resolve _r grooves groove-name))
(let [groove (make-receptor groove-def _r {:attributes spec})]
(let [groove (make-receptor groove-def _r {:attributes {:name groove-name}})]
(--> key->set _r grooves groove-name (address-of groove)))))))))

(defn resolve-name [_r user]
Expand Down Expand Up @@ -113,11 +66,11 @@
(signal self host-groove [_r _f {receptor-name :name grammars :grammars}]
(rsync _r
(let [names (get-scape _r :groove)
qualified-name (keyword (str _f "." receptor-name))
qualified-name (keyword (str _f "." (name receptor-name)))
existing-addr (--> key->resolve _r names qualified-name)
x (if existing-addr (throw (RuntimeException. (str "A groove already exists with the name: " qualified-name))))
creators (get-scape _r :creator)
r (make-receptor groove-def _r {:attributes {:grammars grammars}})
r (make-receptor groove-def _r {:attributes {:name qualified-name}})
addr (address-of r)]
(--> key->set _r names qualified-name addr)
(--> key->set _r creators addr _f)
Expand Down
4 changes: 2 additions & 2 deletions src/anansi/streamscapes/channels/twitter_bridge_in.clj
Expand Up @@ -27,9 +27,9 @@
:to to-id
:sent (standard-date-string (:created_at msg))
:from from-id
:envelope {:from "twitter/screen_name" :text "text/plain"}
:envelope {:from "twitter/screen_name" :message "text/plain"}
:content {:from from-address
:text text}}))
:message text}}))
(first da))))

(signal controller receive [_r _f msg]
Expand Down
2 changes: 1 addition & 1 deletion src/anansi/streamscapes/channels/twitter_controller.clj
Expand Up @@ -29,7 +29,7 @@
:url (str "https://search.twitter.com/search.json?q=" (url-encode (contents _r :search-query)))
:auto-transform true
}
5000)
6000)
(catch Exception e
(do
(.printStackTrace e *err*)
Expand Down
2 changes: 1 addition & 1 deletion src/anansi/streamscapes/droplet.clj
Expand Up @@ -5,6 +5,6 @@
(:use [anansi.ceptr]))

(def droplet-def (receptor-def "droplet"
(attributes :id :from :to :channel :envelope :content)
(attributes :id :from :to :channel :envelope :content :matched-grooves)
(manifest [_r id from to channel envelope content]
{:id (if (or (nil? id) (= id "")) (str (address-of (parent-of _r)) "." (address-of _r)) id) :from from :to to :channel channel :envelope envelope :content content})))
131 changes: 130 additions & 1 deletion src/anansi/streamscapes/groove.clj
Expand Up @@ -5,5 +5,134 @@
(:use [anansi.ceptr]
[anansi.receptor.scape]
))
(declare compository)
(def groove-def (receptor-def "groove" (attributes :name :grammar :preview :carriers)
(manifest [_r {{name :name} :attributes}]
{:name name
:grammar (-> compository name :grammar)
:preview (-> compository name :preview)
:carriers (-> compository name :carriers)
})))

(def groove-def (receptor-def "groove" (attributes :grammars :actions)))
(def compository
{:subject-body-message {:grammar {:subject "text/plain" :body "text/html"}
:preview :subject
:carriers {:email {:encoding nil
:actions {:create true :reply true}}
:streamscapes {:encoding nil
:actions {:create true :reply true}}}
}
:simple-message {:grammar {:message "text/plain"}
:preview :message
:carriers {:twitter {:encoding nil}
:irc {:encoding nil
:actions {:create true :reply true}}
:streamscapes {:encoding nil
:actions {:create true :reply true}}}}
:punkmoney {:actions {:create true :reply true}
:grammar {:promised-to "address" :promised-item "text/plain" :expiration "text/plain"}
:preview ["I promise to pay " [:promised-to] ", on demand, " [:promised-item] ". Expires " [:expiration]]
:carriers {:twitter {:encoding :simple-message}
:streamscapes {:encoding nil :actions {:create true}}
:email {:encoding :subject-body-message :actions {:create true}}}
:matchers {:subject-body-message
{:subject {"text" [#"Punkmoney Promise"]}
:body {"text"
[#"I promise( to pay)* ([^\\W,]+)(, on demand)*,* ([^.]+)\. Expires (.*)\."
{:promised-to 2
:promised-item 4
:expiration 5}]}}
:simple-message
{:message {"text"
[#"^([^\\W]+) I promise( to pay)*(, on demand)*,* ([^.]+)\. Expires (.*)\.\W+#punkmoney"
{:promised-to 1
:promised-item 4
:expiration 5}
]}}}}})

(defn grammar-match?
"returns whether or not a grammar matches the carrier and content of a particular signal"
[grammar carrier content]
(if (nil? grammar)
false
(let [matches
(into [] (map (fn [[k sub-grammar]]
(if (string? sub-grammar)

;; if the grammar doesn't care about the content of the signal,
;; then we have a match if just the keys in the carrier and the
;; grammar match
(contains? carrier k)

;; othewise we have have make sure the
;; content matches.
;; TODO: for now this assumes only one
;; sub-grammar specification, "text" for
;; which the pattern matching is regex. This
;; needs to be generalized
(let [[re field-match-map] (sub-grammar "text")]
(if (and (not (nil? re))
(let [content-type (k carrier)] (and (not (nil? content-type)) (re-find #"^text" content-type) )))
(let [match (re-find re (k content))]
(if match
(if (nil? field-match-map)
true
(into {} (map (fn [[field idx]] [field (match idx)]) field-match-map)))
false))
false
))
))
grammar))]
(if (every? identity matches)
(let [m (into [] (filter #(map? %) matches))]
(if (empty? m) true
(apply merge m)))
false))))

;;(when-carried-by :hash :subject-body-message {:match-fun (fn [hash params] (every? #(contains? hash %) params)) :match-params [:subject :body]})
;;(def carrier-grammars {:hash {:subject-body-message {:subject "text/plain" :body "text/html"}}})
(comment def compository
{:simple-message {:actions {:streamscapes [:create :reply]
:irc [:create :reply]}
:grammars {:streamscapes {:message "text/plain"}
:twitter {:text "text/plain"}
:irc {:message "text/plain"}}}
:subject-body-message {:actions {:streamscapes {:create true :reply true}
:email [:create :reply]}
:grammars {:streamscapes {:subject "text/plain" :body "text/html"}
:email {:subject "text/plain" :body "text/html"}}}
:punkmoney {:actions {:streamscapes [:create]
:email {:create true :reply true}}
:grammars
{:streamscapes {:promised-good "text/plain"
:expiration "text/plain"}
:email {:subject {"text" ["Punkmoney Promise"]}
:body {"text"
["I promise to pay (.*), on demand, ([^.]+)\\. Expires (.*)\\."
{:payee 1
:promised-good 2
:expiration 3}
]}}
:twitter {:text {"text"
["^(@[^\\W]+) I promise to pay, on demand, ([^.]+)\\. Expires (.*)\\. #punkmoney"
{:payee 1
:promised-good 2
:expiration 3}
]}}}}
:poll {:actions {:streamscapes [:create]}
:grammars {:streamscapes {:poll-name "text/plain"
:options "enumeration/yes,no,abstain"}}}
:bookmark {:actions {:streamscapes [:create]}
:grammars {:streamscapes {:url "text/plain"
:subject "text/html"}}}
:lazyweb-thanks {:actions {:streamscapes [:create]}
:grammars
{:streamscapes {:thankee "text/plain"
:expiration "text/plain"}
:twitter {:text {"text"
["#lazyweb thanks (@[^\\W]+)"
{:thankee 1}
]}}}}

}
)

0 comments on commit b0401a2

Please sign in to comment.