d4d50712ce66b71fd773b6792444b1582c12bbc8
[mygpo.git] / couchdb / general / _design / history / views / by_device / map.js
blobd4d50712ce66b71fd773b6792444b1582c12bbc8
1 function(doc)
3     function processEpisodeAction(action)
4     {
5         if(action.device == null)
6         {
7             return;
8         }
10         action_obj = {
11                 type:          "Episode",
12                 podcast_url:   doc.podcast_ref_url,
13                 episode_url:   doc.ref_url,
14                 podcast_id:    doc.podcast,
15                 episode_id:    doc.episode,
16                 action:        action.action,
17                 timestamp:     action.timestamp.slice(0, action.timestamp.length-1),
18             }
20         if(action.device != null)
21         {
22             action_obj["device_id"] = action.device;
23         }
24         if(action.started != null)
25         {
26             action_obj["started"] = action.started;
27         }
28         if(action.playmark != null)
29         {
30             action_obj["position"] = action.playmark;
31         }
32         if(action.total != null)
33         {
34             action_obj["total"] = action.total;
35         }
37         emit([doc.user, action.device, action_obj.timestamp], action_obj);
38     }
40     function processSubscriptionAction(action)
41     {
42         action_obj = {
43                 type:          "Subscription",
44                 podcast_url:   doc.ref_url,
45                 podcast_id:    doc.podcast,
46                 action:        action.action,
47                 timestamp:     action.timestamp.slice(0, action.timestamp.length-1),
48                 device_id:     action.device,
49             }
51         emit([doc.user, action.device, action_obj.timestamp], action_obj);
52     }
55     if(doc.doc_type == "EpisodeUserState")
56     {
57         for(var n in doc.actions)
58         {
59             processEpisodeAction(doc.actions[n]);
60         }
61     }
62     if(doc.doc_type == "PodcastUserState")
63     {
64         for(var n in doc.actions)
65         {
66             processSubscriptionAction(doc.actions[n]);
67         }
68     }