1 function(newDoc, oldDoc, userCtx)
4 if(newDoc.doc_type == "User")
9 for(var n in newDoc.devices)
11 var device = newDoc.devices[n];
13 if(device_ids.indexOf(device.id) > -1)
15 throw({forbidden: "Duplicate Device-Id " + device.id +
16 " for user " + newDoc._id});
19 device_ids.push(device.id);
21 if(device_uids.indexOf(device.uid) > -1)
23 throw({forbidden: "Duplicate Device-UID " + device.uid +
24 " for user " + newDoc._id});
27 if(!device.uid.match("^[\\w_.-]+$"))
29 throw({forbidden: "Invalid Device-UID " + device.uid +
30 " for user " + newDoc._id});
33 device_uids.push(device.uid);
36 for(var n in newDoc.sync_groups)
38 var group = newDoc.sync_groups[n];
42 throw({forbidden: "Sync-Group " + n + " must contain at " +
43 "least two devices for user " + newDoc._id});
48 var device_id = group[i];
50 if(device_ids.indexOf(device_id) < 0)
52 throw({forbidden: "Invalid Device-Id " + device_id +
53 " in Sync-Group " + n + " for user " +
60 if(newDoc.doc_type == "PodcastUserState")
62 subscribed_devices = [];
63 last_timestamp = null;
65 for(n in newDoc.actions)
67 action = newDoc.actions[n];
70 if((last_timestamp != null) && (action.timestamp < last_timestamp))
72 throw({forbidden: "The actions in PodcastUserState " + newDoc._id + " must be in order"});
74 last_timestamp = action.timestamp;
77 index = subscribed_devices.indexOf(action.device);
78 if(action.action == "subscribe")
82 throw({forbidden: "Can not subscribe twice on device " + action.device + " in podcast state " + newDoc._id});
85 subscribed_devices.push(action.device);
91 throw({forbidden: "Can not unsubscribe on device " + action.device + " on which the podcast is not subscribed in podcast state " + newDoc._id});
94 subscribed_devices.splice(index, 1);