Skip to content

Instantly share code, notes, and snippets.

@avital
Created September 17, 2009 16:09
Show Gist options
  • Save avital/188551 to your computer and use it in GitHub Desktop.
Save avital/188551 to your computer and use it in GitHub Desktop.
(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))
(def db (atom {}))
(def log (atom ""))
(def sync-table (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 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 map-map-keys [f m]
(map-map #(do [(f %1) %2]) m))
(defn-log map-map-values [f m]
(map-map #(do [%1 (f %2)]) m))
(defn-log zipmapf [keys f]
(zipmap keys (map f keys)))
(defn-log blip-replace-operation [id text]
(str id ": " text))
(defn-log blip-data-to-db-structure [blip-id blip-data]
[[(blip-data "waveId") (blip-data "waveletId") blip-id] blip-data])
(defn-log disj1 [set x]
(if set
(disj set x)
nil))
(defn-log updates-step1 [sync-table updates]
(map-map
(fn [id event]
[(disj1 (first (filter #(contains? % id) sync-table)) id) (event "content")])
updates))
(defn-log updates-step2 [set-table]
(apply union (map
(fn [[ids text]]
(into #{} (for [id ids] (blip-replace-operation id text))))
set-table)))
(defn-log update-db [sync-table data]
(let [updates (map-map blip-data-to-db-structure ((data "blips") "map"))]
(swap! db merge updates)
(updates-step2 (updates-step1 sync-table updates))))
(defroutes greeter
(ANY "/wave"
(let [data (read-json (params :events))]
(update-db @sync-table data))))
(run-server {:port 31337}
"/*" (servlet greeter))
; TESTS
(def events1
{"blips"
{"map"
{"b+MfdUDFjy%F"
{"lastModifiedTime" 1252401883999,"contributors"
{"javaClass" "java.util.ArrayList","list"
["avital@wavesandbox.com","thewe-experiments@appspot.com","thewe-0@appspot.com","thewe-operator@appspot.com"]
},
"waveletId" "wavesandbox.com!conv+root",
"waveId" "wavesandbox.com!w+MfdUDFjy%E",
"parentBlipId" nil,
"version" 125,
"creator" "avital@wavesandbox.com",
"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" "avital@wavesandbox.com","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" ["avital@wavesandbox.com","thewe-experiments@appspot.com","thewe-0@appspot.com","thewe-operator@appspot.com"]},"creator" "avital@wavesandbox.com","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