From 7d6665667c5093314d4a2e4aca45430dc81ad0f2 Mon Sep 17 00:00:00 2001 From: Zachary Vance Date: Tue, 7 Jul 2015 12:47:32 -0700 Subject: [PATCH] Make all couchdb documents objects --- chat.js | 35 ++++++++++++++++------------------- sync | 1 + 2 files changed, 17 insertions(+), 19 deletions(-) create mode 100644 sync diff --git a/chat.js b/chat.js index fab5b6e..4409c07 100644 --- 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 index 0000000..48c98e4 --- /dev/null +++ b/sync @@ -0,0 +1 @@ +rsync -r . petchat@deadtree:public_html -- 2.47.3