1 function(newDoc, oldDoc, userCtx)
3 function require(doc, field, message)
5 message = message || "Document must have a " + field;
6 if (doc[field] == void 0 || doc[field] == null || doc[field].length == 0)
8 throw({forbidden: message});
12 if(newDoc.doc_type == "PodcastUserState")
14 require(newDoc, "podcast");
16 subscribed_devices = [];
17 last_timestamp = null;
19 for(n in newDoc.actions)
21 action = newDoc.actions[n];
24 if((last_timestamp != null) && (action.timestamp < last_timestamp))
26 throw({forbidden: "The actions in PodcastUserState " + newDoc._id + " must be in order"});
28 last_timestamp = action.timestamp;
31 index = subscribed_devices.indexOf(action.device);
32 if(action.action == "subscribe")
36 throw({forbidden: "Can not subscribe twice on device " + action.device + " in podcast state " + newDoc._id});
39 subscribed_devices.push(action.device);
41 else if (action.action == "unsubscribe")
45 throw({forbidden: "Can not unsubscribe on device " + action.device + " on which the podcast is not subscribed in podcast state " + newDoc._id});
48 subscribed_devices.splice(index, 1);