Skip to content

Commit

Permalink
working caching hopefully
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnMcLear committed Mar 27, 2013
1 parent 50b99c5 commit 2f8cd3b
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 93 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -2,7 +2,7 @@
Creates an RSS feed and prefixes it to the end of a pad IE as atom.xml IE http://bacon.com/p/yummi/atom.xml

# Installation
Just install then click the Embed button to get the link
Just install then click the Share and Embed button to get the RSS feed URL

# TODO
Everything
Smarter creation of new items upon modification
4 changes: 2 additions & 2 deletions client.js
@@ -1,7 +1,7 @@
var eejs = require("ep_etherpad-lite/node/eejs");

exports.eejsBlock_embedPopup = function (hook_name, args, cb) {
var rssFeed = { padId:"test" };
args.content = args.content + eejs.require("ep_rss/templates/embedFrame.html", rssFeed, module);
var feedURL = args.renderContext.req.url + "/feed";
args.content = args.content + eejs.require("ep_rss/templates/embedFrame.ejs", {feed: feedURL});
return cb();
};
1 change: 0 additions & 1 deletion ep.json
Expand Up @@ -3,7 +3,6 @@
{
"name": "ep_rss",
"hooks": {
"padUpdate": "ep_rss/update",
"eejsBlock_htmlHead": "ep_rss/index",
"expressCreateServer" : "ep_rss/index:registerRoute",
"eejsBlock_embedPopup": "ep_rss/client:eejsBlock_embedPopup"
Expand Down
70 changes: 43 additions & 27 deletions index.js
Expand Up @@ -43,20 +43,33 @@ exports.registerRoute = function (hook_name, args, cb) {
var padId=path[2];
var padURL = req.protocol + "://" + req.get('host') + "/p/" +padId;
var dateString = new Date();
var isPublished = false;
var isPublished = false; // is this item already published?

/* When was this pad last edited and should we publish an RSS update? */
async.series([
function(cb){
API.getLastEdited(padId, function(callback, message){
var currTS = new Date().getTime();
if(currTS - message.lastEdited < staleTime){
if(!feeds[padId]){
feeds[padId] = {};
}

if(currTS - message.lastEdited < staleTime){ // was the pad edited within the last 5 minutes?
isPublished = isAlreadyPublished(padId, message.lastEdited);
if(!isPublished){
feeds[padId] = message.lastEdited; // Add it to the timer object

if(!isPublished){ // If it's not already published and it's gone stale
feeds[padId].lastEdited = message.lastEdited; // Add it to the timer object
}
cb();

}else{
if(!feeds[padId].feed){ // If it's not already stored in memory
console.debug("Feed not already in memory so writing memory", feeds);
isPublished = false;
feeds[padId].lastEdited = message.lastEdited; // Add it to the timer object
}else{
isPublished = true;
}
cb();
}
});
Expand All @@ -65,7 +78,6 @@ exports.registerRoute = function (hook_name, args, cb) {
/* Get the pad text */
function(cb){
if(!isPublished){
console.warn("here");
var padText = padManager.getPad(padId, function(err, _pad){
pad = _pad;
text = safe_tags(pad.text()).replace(/\n/g,"<br/>");
Expand All @@ -80,34 +92,38 @@ exports.registerRoute = function (hook_name, args, cb) {
/* Create a new RSS item */
function(cb){
if(isPublished){
console.debug("Sending RSS from memory for "+padId);
res.contentType("rss");
res.send(feeds[padId].feed);
cb();
}
else{
console.debug("Building RSS for "+padId);
/* Why don't we use EEJS require here? Well EEJS require isn't ASYNC so on first load
it would bring in the .ejs content only and then on second load pad contents would be included..
Sorry this is ugly but that's how the plugin FW was designed by @redhog -- bug him for a fix! */

res.contentType("rss");
args.content = '<rss xmlns:media="'+fullURL+'" version="2.0">\n';
args.content += '<channel>\n';
args.content += '<title>'+padId+'</title>\n';
args.content += '<description/>\n';
args.content += '<language>en-us</language>\n';
args.content += '<pubDate>'+dateString+'</pubDate>\n';
args.content += '<lastBuildDate>'+dateString+'</lastBuildDate>\n';
args.content += '<item>\n';
args.content += '<title>\n';
args.content += padId + ' has been changed\n';
args.content += '</title>\n';
args.content += '<description>\n';
args.content += '<![CDATA['+text+']]>\n';
args.content += '<link>'+padURL+'</link>\n';
args.content += '</item>\n';
args.content += '</channel>\n';
args.content += '</rss>';
feeds[padId].feed = args.content;
res.send(args.content); // Send it to the requester
cb(); // Am I even called?
res.contentType("rss");
args.content = '<rss xmlns:media="'+fullURL+'" version="2.0">\n';
args.content += '<channel>\n';
args.content += '<title>'+padId+'</title>\n';
args.content += '<description/>\n';
args.content += '<language>en-us</language>\n';
args.content += '<pubDate>'+dateString+'</pubDate>\n';
args.content += '<lastBuildDate>'+dateString+'</lastBuildDate>\n';
args.content += '<item>\n';
args.content += '<title>\n';
args.content += padId + ' has been changed\n';
args.content += '</title>\n';
args.content += '<description>\n';
args.content += '<![CDATA['+text+']]>\n';
args.content += '<link>'+padURL+'</link>\n';
args.content += '</item>\n';
args.content += '</channel>\n';
args.content += '</rss>';
feeds[padId].feed = args.content;
res.send(args.content); // Send it to the requester
cb(); // Am I even called?
}

},function(){
/* Todo - Some error handling */
Expand Down
Binary file added static/img/rss.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions templates/embedFrame.ejs
@@ -0,0 +1,8 @@
<br>
<h2>Rss Feeds</h2>
<p>
<a class="rssfeed" href="<%= feed%>" title="Pad Feed">
<img src="/static/plugins/ep_rss/static/img/rss.gif">
</a>

</p>
61 changes: 0 additions & 61 deletions update.js

This file was deleted.

0 comments on commit 2f8cd3b

Please sign in to comment.