]> git.za3k.com Git - petchat.git/commitdiff
Make all couchdb documents objects
authorZachary Vance <vanceza@gmail.com>
Tue, 7 Jul 2015 19:47:32 +0000 (12:47 -0700)
committerZachary Vance <vanceza@gmail.com>
Tue, 7 Jul 2015 19:47:32 +0000 (12:47 -0700)
chat.js
sync [new file with mode: 0644]

diff --git a/chat.js b/chat.js
index fab5b6e33a0bc12bec1fc8596bfd568b4fd71f70..4409c07c1626e23b31e4cab9e989a61d6c4b98b8 100644 (file)
--- a/chat.js
+++ b/chat.js
@@ -17,38 +17,36 @@ Datastore.prototype = {
     storeChat: function(callback, key, messages, unlockDate, title) {
         var self = this;
         this.get(function(chats) {
-            chats.push({key: key, unlockDate: unlockDate, title: title});
+            chats.list.push({key: key, unlockDate: unlockDate, title: title});
             self.store(function() {
-                self.store(callback, "chat-" + key, messages);
+                self.store(callback, "chat-" + key, {messages:messages});
             }, "chats", chats);
-        }, "chats", []);
+        }, "chats", {list:[]});
     },
     getAvailableChats: function(callback, isAdmin) {
-        this.get(function(allChats) {
+        this.get(function(chats) {
+            var allChats = chats.list;
             var now = new Date();
             var unlockedChats = _.filter(allChats, function(chat) {
                 return chat.unlockDate < now;
             });
             callback(isAdmin ? allChats : unlockedChats);
-        }, "chats", []);
+        }, "chats", {list:[]});
     },
     getChat: function(callback, key) {
-        this.get(callback, "chat-" + key, []);
+        this.get(callback, "chat-" + key, {messages:[]});
     },
     store: function(callback, key, value) {
         //localStorage[key] = JSON.stringify(value);
         var rev;
-        if (value._rev) {
-            rev = value._rev;
-            delete value._rev;
+        if (value.rev) {
+            value._rev = value._rev + 1;
+            delete value.rev;
         }
-        var data = { content: JSON.stringify(value) };
-        if (rev) data._rev = rev;
-
         $.ajax({
             type: 'PUT',
             url: this.url + "/chats/" + key,
-            data: JSON.stringify(data),
+            data: JSON.stringify(value),
             contentType: 'application/json',
             dataType: 'application/json',
             success: callback,
@@ -62,10 +60,9 @@ Datastore.prototype = {
             type: 'get',
             dataType: 'jsonp',
             success: function(value) {
-                if (value && value.content) {
-                    var content = JSON.parse(value.content);
-                    content._rev = value._rev;
-                    callback(content);
+                if (value) {
+                    if (value.content) value = value.content;
+                    callback(value);
                 } else {
                     callback(default_);
                 }
@@ -89,8 +86,8 @@ Playback = function(key) {
 Playback.prototype = {
     start: function() {
         var self = this;
-        this.datastore.getChat(function(messages) {
-            self.messages = messages;
+        this.datastore.getChat(function(chat) {
+            self.messages = chat.messages;
             _.each(self.messages, self.queueSend, self);
             var lastTime = self.messages[self.messages.length - 1].delayMs;
             setTimeout(function() {
diff --git a/sync b/sync
new file mode 100644 (file)
index 0000000..48c98e4
--- /dev/null
+++ b/sync
@@ -0,0 +1 @@
+rsync -r . petchat@deadtree:public_html