Add scripts to create myops-getqueryview:
[myops.git] / web / query / lists / comments.js
1 function(head, req) {
2   var ddoc = this;
3   var Mustache = require("lib/mustache");
4   var List = require("vendor/couchapp/lib/list");
5   var path = require("vendor/couchapp/lib/path").init(req);
6   var Atom = require("vendor/couchapp/lib/atom");
7
8   var indexPath = path.list('index','recent-posts',{descending:true, limit:10});
9   var feedPath = path.list('index','recent-posts',{descending:true, limit:10, format:"atom"});
10   var commentsFeed = path.list('comments','comments',{descending:true, limit:10, format:"atom"});
11
12   // if the client requests an atom feed and not html, 
13   // we run this function to generate the feed.
14   provides("atom", function() {    
15     var path = require("vendor/couchapp/lib/path").init(req);
16     var markdown = require("vendor/couchapp/lib/markdown");
17     var textile = require("vendor/textile/textile");
18
19     // we load the first row to find the most recent change date
20     var row = getRow();
21     
22     // generate the feed header
23     var feedHeader = Atom.header({
24       updated : (row ? new Date(row.value.created_at) : new Date()),
25       title : ddoc.blog.title + " comments",
26       feed_id : path.absolute(indexPath),
27       feed_link : path.absolute(commentsFeed)
28     });
29     
30     // send the header to the client
31     send(feedHeader);
32
33     // loop over all rows
34     if (row) {
35       do {
36         var v = row.value;
37         
38         // generate the entry for this row
39         var feedEntry = Atom.entry({
40           entry_id : path.absolute('/'+encodeURIComponent(req.info.db_name)+'/'+encodeURIComponent(row.id)),
41           title : "comment on "+v.post_id,
42           content : markdown.encode(Mustache.escape(v.comment)),
43           updated : new Date(v.created_at),
44           author : v.commenter.nickname || v.commenter.name,
45           alternate : path.absolute(path.list('post','post-page', {startkey:[v.post_id]}))
46         });
47         // send the entry to client
48         send(feedEntry);
49       } while (row = getRow());
50     }
51
52     // close the loop after all rows are rendered
53     return "</feed>";
54   });
55 }