implement bulk (un)subscribe
[mygpo.git] / mygpo / directory / _design / views / episode_toplist / map.js
blob003f7cc671937313627f8ad0e22801a4b13c4c69
1 function(doc)
3     if(doc.doc_type != "Episode")
4     {
5         return;
6     }
8     function searchObject(obj, languages, types)
9     {
10         if (obj.language)
11         {
12             languages.push(obj.language.slice(0, 2));
13         }
15         if (obj.content_types)
16         {
17             for(n in obj.content_types)
18             {
19                 types.push(obj.content_types[n]);
20             }
21         }
22     };
24     function doEmit(date_str, types, languages, value)
25     {
26         if(value > 0)
27         {
28             emit([date_str, "none", value], null);
30             for(n in types)
31             {
32                 emit([date_str, "type", types[n], value], null);
34                 for(m in languages)
35                 {
36                     emit([date_str, "type-language", types[n], languages[m], value], null);
37                 }
38             }
40             for(m in languages)
41             {
42                 emit([date_str, "language", languages[m], value], null);
43             }
44         }
45     };
47     var toplist_period = 7;
49     if(doc.listeners <= 0 || doc.released == null)
50     {
51         return;
52     }
54     dateString = doc.released.slice(0, 10);
55     dateParts = dateString.split("-");
56     dateParts = dateParts.map(Number);
58     languages = [];
59     types = [];
61     searchObject(doc, languages, types);
63     for(i=0; i<toplist_period; i++)
64     {
65         dateObj = new Date(dateParts[0], dateParts[1]-1, dateParts[2]+i);
66         year = dateObj.getFullYear();
67         month = Number(dateObj.getMonth()) + 1;
68         if(month < 10)
69         {
70             month = "0" + month;
71         }
72         day = Number(dateObj.getDate());
73         if(day < 10)
74         {
75             day = "0" + day;
76         }
78         dateStr = year + "-" + month + "-" + day;
80         doEmit(dateStr, types, languages, doc.listeners);
81     }