move categories into own database
[mygpo.git] / couchdb / general / _design / podcast_states / validate_doc_update.js
bloba42b00f50389faf30f71a590699fd524eb3f74f0
1 function(newDoc, oldDoc, userCtx)
3     function require(doc, field, message)
4     {
5         message = message || "Document must have a " + field;
6         if (doc[field] == void 0 || doc[field] == null || doc[field].length == 0)
7         {
8             throw({forbidden: message});
9         }
10     }
12     if(newDoc.doc_type == "PodcastUserState")
13     {
14         require(newDoc, "podcast");
16         subscribed_devices = [];
17         last_timestamp = null;
19         for(n in newDoc.actions)
20         {
21             action = newDoc.actions[n];
24             if((last_timestamp != null) && (action.timestamp < last_timestamp))
25             {
26                 throw({forbidden: "The actions in PodcastUserState " + newDoc._id + " must be in order"});
27             }
28             last_timestamp = action.timestamp;
31             index = subscribed_devices.indexOf(action.device);
32             if(action.action == "subscribe")
33             {
34                 if(index > -1)
35                 {
36                     throw({forbidden: "Can not subscribe twice on device " + action.device + " in podcast state " + newDoc._id});
37                 }
39                 subscribed_devices.push(action.device);
40             }
41             else if (action.action == "unsubscribe")
42             {
43                 if(index == -1)
44                 {
45                     throw({forbidden: "Can not unsubscribe on device " + action.device + " on which the podcast is not subscribed in podcast state " + newDoc._id});
46                 }
48                 subscribed_devices.splice(index, 1);
49             }
51         }
52     }