-
-
Save ayal/190278 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns we | |
(:require clojure.contrib.json.read) | |
(:require clojure.contrib.json.write) | |
(:require clojure.set) | |
(:use clojure.contrib.json.read) | |
(:use clojure.contrib.json.write) | |
(:use clojure.set) | |
(:use compojure)) | |
; db is a map between replocs and blipData (= the event json we get from wave) | |
(def db (atom {})) | |
(def sync-table (atom #{})) | |
(def log-buffer (atom "")) | |
(defmacro defn-log [name- args & body] | |
`(defn ~name- ~args | |
(println "method call: " '~name-) | |
(println "args:" ~args) | |
(let [return-value# (do ~@body)] | |
(println "return value:" return-value#) | |
return-value#))) | |
(defn log [& what] | |
(swap! log-buffer str what "\n\n") | |
(last what)) | |
(defn substring [a b] | |
(.substring a b)) | |
(defn read-and-clean-log [] | |
(let [old-buffer @log-buffer] | |
(swap! log-buffer substring (count old-buffer)) | |
old-buffer)) | |
(defmacro forex [seq-exprs & body-expr] | |
`(apply concat (for ~seq-exprs (list ~@body-expr)))) | |
(defn-log pairs-to-map [pairs] | |
(zipmap (map first pairs) (map second pairs))) | |
(defn-log map-map [f m] | |
(pairs-to-map (map #(f (first %) (second %)) m))) | |
(defn-log blip-data-to-db-structure [blip-id blip-data] | |
[{:wave-id (blip-data "waveId"), :wavelet-id (blip-data "waveletId"), :blip-id blip-id} blip-data]) | |
(defn-log disj1 [set x] | |
(if set | |
(disj set x) | |
nil)) | |
; updates has the same structure as db (see db comment) | |
; returns a map from reploc to its new content | |
(defn-log updates-to-log-opers [sync-table updates] | |
(into {} (for [update updates | |
sync-class sync-table :when (contains? sync-class (key update)) | |
reploc sync-class :when (log "not=" (not= (log "Reploc: " reploc) (log "Key: " (key update))))] | |
[reploc ((val update) "content")]))) | |
(def log-reploc {:wave-id "wavesandbox.com!w+1g-r16RU%A" | |
:wavelet-id "wavesandbox.com!conv+root" | |
:blip-id "b+1g-r16RU%B"}) | |
(defn-log log-opers-to-json [log-opers] | |
{ | |
"javaClass" "com.google.wave.api.impl.OperationMessageBundle", | |
"operations" { | |
"javaClass" "java.util.ArrayList", | |
"list" (conj (log "Operations:" (forex [log-oper log-opers :let [reploc (key log-oper)]] | |
{ | |
"index" -1, | |
"waveletId" (:wavelet-id reploc), | |
"blipId" (:blip-id reploc), | |
"javaClass" "com.google.wave.api.impl.OperationImpl", | |
"property" nil, | |
"waveId" (:wave-id reploc), | |
"type" "DOCUMENT_DELETE" | |
} | |
{ | |
"index" 0, | |
"waveletId" (:wavelet-id reploc), | |
"blipId" (:blip-id reploc), | |
"javaClass" "com.google.wave.api.impl.OperationImpl", | |
"property" (val log-oper), | |
"waveId" (:wave-id reploc), | |
"type" "DOCUMENT_APPEND" | |
})) | |
{ | |
"index" 0, | |
"waveletId" (:wavelet-id log-reploc), | |
"blipId" (:blip-id log-reploc), | |
"javaClass" "com.google.wave.api.impl.OperationImpl", | |
"property" (read-and-clean-log), | |
"waveId" (:wave-id log-reploc), | |
"type" "DOCUMENT_APPEND" | |
; @todo MAKE THESE FUNCTIONS | |
}) | |
} | |
"version" "19" ; @todo WTF | |
}) | |
(defn containsex? [big small] | |
(some #{small} big)) | |
(defn-log update-db [sync-table data] | |
(let [db-updates (map-map blip-data-to-db-structure ((data "blips") "map"))] | |
(swap! db merge db-updates) | |
(json-str (log-opers-to-json (updates-to-log-opers sync-table | |
(into {} (filter #(log "contains?" (containsex? | |
(log "Mapx: " (vec (map (fn [x] (get-in x ["properties" "map" "blipId"])) | |
(get-in data ["events" "list"])))) | |
(log "Keyx: " ((key %) :blip-id)))) | |
db-updates))))))) | |
(defn-log sync-with-text [text] | |
(swap! sync-table conj (disj | |
(set (keysdfs (filter (fn [[key val]] (.contains (val "content") text)) @db))) | |
log-reploc))) | |
(defroutes greeter | |
(ANY "/wave" | |
(let [data (log "Events: " (read-json (params :events)))] | |
(update-db @sync-table data)))) | |
(run-server {:port 31337} | |
"/*" (servlet greeter)) | |
; TESTS | |
(reset! sync-table #{}) | |
(def events1 | |
{"blips" | |
{"map" | |
{"b+MfdUDFjy%F" | |
{"lastModifiedTime" 1252401883999,"contributors" | |
{"javaClass" "java.util.ArrayList","list" | |
["[email protected]","[email protected]","[email protected]","[email protected]"] | |
}, | |
"waveletId" "wavesandbox.com!conv+root", | |
"waveId" "wavesandbox.com!w+MfdUDFjy%E", | |
"parentBlipId" nil, | |
"version" 125, | |
"creator" "[email protected]", | |
"content" "hi hi hi\n","blipId" "b+MfdUDFjy%F","javaClass" "com.google.wave.api.impl.BlipData","annotations" {"javaClass" "java.util.ArrayList","list" []},"elements" {"map" {"0" {"javaClass" "com.google.wave.api.Gadget","properties" {"map" {"test" "jjkklll;qqpopslKL\n","_view" "stateUpdated = function(){};","url" "http //wave.thewe.net/gadgets/theWE-wave/theWE-container.xml","_gadget-id" "http //wave.thewe.net/gadgets/theWE-wave/theWE-container.xml0.607755900749904"},"javaClass" "java.util.HashMap"},"type" "GADGET"}},"javaClass" "java.util.HashMap"},"childBlipIds" {"javaClass" "java.util.ArrayList","list" []}}},"javaClass" "java.util.HashMap"},"events" {"javaClass" "java.util.ArrayList","list" [{"timestamp" 1252402678257,"modifiedBy" "[email protected]","javaClass" "com.google.wave.api.impl.EventData","properties" {"map" {"blipId" "b+MfdUDFjy%F"},"javaClass" "java.util.HashMap"},"type" "DOCUMENT_CHANGED"}]},"wavelet" {"lastModifiedTime" 1252402678257,"title" "","waveletId" "wavesandbox.com!conv+root","rootBlipId" "b+MfdUDFjy%F","javaClass" "com.google.wave.api.impl.WaveletData","dataDocuments" {"map" {},"javaClass" "java.util.HashMap"},"creationTime" 1252400711330,"waveId" "wavesandbox.com!w+MfdUDFjy%E","participants" {"javaClass" "java.util.ArrayList","list" ["[email protected]","[email protected]","[email protected]","[email protected]"]},"creator" "[email protected]","version" 129}} | |
) | |
(def sync-table1 #{#{["wavesandbox.com!w+MfdUDFjy%E" "wavesandbox.com!conv+root" "b+MfdUDFjy%F"] ["a" "b" "c"]}}) | |
(update-db sync-table1 events1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment