19 | | |
20 | | == CouchDB Views == |
21 | | [[PageOutline(3-4, Views)]] |
22 | | |
23 | | All views are in Javascript. |
24 | | |
25 | | === `_design/posts` === |
26 | | |
27 | | ==== `by_month` ==== |
28 | | {{{ |
29 | | #!javascript |
30 | | function(doc) { |
31 | | if (doc.type == 'Post') { |
32 | | var year = parseInt(doc.published.substr(0, 4), 10); |
33 | | var month = parseInt(doc.published.substr(5, 2), 10); |
34 | | emit([year, month, doc.published], { |
35 | | slug: doc.slug, title: doc.title, author: doc.author, summary: doc.summary, |
36 | | published: doc.published, updated: doc.updated, tags: doc.tags |
37 | | }); |
38 | | } |
39 | | } |
40 | | }}} |
41 | | |
42 | | ==== `by_slug` ==== |
43 | | {{{ |
44 | | #!javascript |
45 | | function(doc) { |
46 | | if (doc.type == 'Post') { |
47 | | var year = parseInt(doc.published.substr(0, 4), 10); |
48 | | var month = parseInt(doc.published.substr(5, 2), 10); |
49 | | emit([year, month, doc.slug], doc); |
50 | | } |
51 | | } |
52 | | |
53 | | }}} |
54 | | |
55 | | ==== `by_tag` ==== |
56 | | |
57 | | {{{ |
58 | | #!javascript |
59 | | function(doc) { |
60 | | if (doc.type == 'Post' && doc.tags) { |
61 | | for (var idx in doc.tags) { |
62 | | emit([doc.tags[idx], doc.published], { |
63 | | slug: doc.slug, title: doc.title, author: doc.author, published: doc.published, updated: doc.updated, |
64 | | summary: doc.summary, body: doc.body, tags: doc.tags, extended: doc.extended != '' ? '…' : null |
65 | | }); |
66 | | } |
67 | | } |
68 | | } |
69 | | }}} |
70 | | |
71 | | ==== `by_time` ==== |
72 | | {{{ |
73 | | #!javascript |
74 | | function(doc) { |
75 | | if (doc.type == 'Post') { |
76 | | emit([doc.published], { |
77 | | slug: doc.slug, title: doc.title, author: doc.author, summary: doc.summary, |
78 | | body: doc.body, published: doc.published, updated: doc.updated, extended: doc.extended != '' ? '…' : null, |
79 | | tags: doc.tags, allow_comments: doc.allow_comments, num_comments: doc.num_comments, |
80 | | allow_pings: doc.allow_pings, num_pings: doc.num_pings}); |
81 | | } |
82 | | } |
83 | | }}} |
84 | | |
85 | | ==== `by_update` ==== |
86 | | {{{ |
87 | | #!javascript |
88 | | function(doc) { |
89 | | if (doc.type == 'Post') { |
90 | | emit([doc.updated], null); |
91 | | } |
92 | | } |
93 | | }}} |
94 | | |
95 | | ==== `months` ==== |
96 | | ''Map'': |
97 | | {{{ |
98 | | #!javascript |
99 | | function(doc) { |
100 | | if (doc.type == 'Post') { |
101 | | var year = parseInt(doc.published.substr(0, 4), 10); |
102 | | var month = parseInt(doc.published.substr(5, 2), 10); |
103 | | emit([year, month], 1); |
104 | | } |
105 | | } |
106 | | }}} |
107 | | |
108 | | ''Reduce'': |
109 | | {{{ |
110 | | #!javascript |
111 | | function(keys, values) { |
112 | | return sum(values); |
113 | | } |
114 | | }}} |
115 | | |
116 | | ==== `tags` ==== |
117 | | ''Map'': |
118 | | {{{ |
119 | | #!javascript |
120 | | function(doc) { |
121 | | if (doc.type == 'Post' && doc.tags) { |
122 | | for (var idx in doc.tags) { |
123 | | emit(doc.tags[idx], 1); |
124 | | } |
125 | | } |
126 | | } |
127 | | }}} |
128 | | |
129 | | ''Reduce'': |
130 | | {{{ |
131 | | #!javascript |
132 | | function(keys, values) { |
133 | | return sum(values); |
134 | | } |
135 | | }}} |
136 | | |
137 | | === `_design/comments` === |
138 | | |
139 | | ==== `whitelist` ==== |
140 | | |
141 | | ''Map'': |
142 | | {{{ |
143 | | #!javascript |
144 | | function(doc) { |
145 | | if (doc.type == 'Comment' && doc.published) { |
146 | | emit(doc.openid, 1); |
147 | | } |
148 | | } |
149 | | }}} |
150 | | |
151 | | ''Reduce'': |
152 | | {{{ |
153 | | #!javascript |
154 | | function(keys, values) { |
155 | | return sum(values); |
156 | | } |
157 | | }}} |
158 | | |
159 | | === `_design/pings` === |
160 | | |
161 | | ==== `by_uri` ==== |
162 | | |
163 | | {{{ |
164 | | #!javascript |
165 | | function(doc) { |
166 | | if (doc.type == 'Ping') { |
167 | | emit([doc.post_id, doc.uri], null); |
168 | | } |
169 | | } |
170 | | }}} |
171 | | |
172 | | === `_design/reactions` === |
173 | | |
174 | | ==== `by_post` ==== |
175 | | |
176 | | {{{ |
177 | | #!javascript |
178 | | function(doc) { |
179 | | if (doc.type == 'Ping' && doc.published) { |
180 | | emit([doc.post_id, doc.time], { |
181 | | type: doc.type, uri: doc.uri, title: doc.title, |
182 | | author: doc.author, excerpt: doc.excerpt, |
183 | | time: doc.time |
184 | | }); |
185 | | } else if (doc.type == 'Comment' && doc.published) { |
186 | | emit([doc.post_id, doc.time], { |
187 | | type: doc.type, openid: doc.openid, author: doc.author, |
188 | | markup: doc.markup, time: doc.time |
189 | | }); |
190 | | } |
191 | | } |
192 | | }}} |
193 | | |
194 | | ==== `by_time` ==== |
195 | | |
196 | | {{{ |
197 | | #!javascript |
198 | | function(doc) { |
199 | | if (doc.type == 'Ping' && !doc.published) { |
200 | | emit([doc.time, doc.type], doc); |
201 | | } else if (doc.type == 'Comment' && doc.authenticated && !doc.published) { |
202 | | emit([doc.time, doc.type], doc); |
203 | | } |
204 | | } |
205 | | }}} |
206 | | |