2015-03-02 22:44:24 +00:00
|
|
|
var eejs = require('ep_etherpad-lite/node/eejs/'),
|
2016-02-25 03:31:09 +00:00
|
|
|
API = require('ep_etherpad-lite/node/db/API'),
|
2015-03-02 22:44:24 +00:00
|
|
|
padManager = require('ep_etherpad-lite/node/db/PadManager'),
|
2016-02-25 03:31:09 +00:00
|
|
|
padMessageHandler = require('ep_etherpad-lite/node/handler/PadMessageHandler'),
|
|
|
|
settings = require('ep_etherpad-lite/node/utils/Settings'),
|
|
|
|
async = require('ep_etherpad-lite/node_modules/async'),
|
|
|
|
fs = require('fs');
|
|
|
|
|
|
|
|
fs.mkdir('deleted_pads', function(err) {});
|
2015-03-02 22:44:24 +00:00
|
|
|
|
|
|
|
// Add client code
|
|
|
|
exports.eejsBlock_scripts = function (hook_name, args, cb) {
|
|
|
|
args.content = '<script src="../static/plugins/ep_delete_after_delay/static/js/reconnect.js"></script>' + args.content;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get settings
|
|
|
|
var areParamsOk = (settings.ep_delete_after_delay) ? true : false,
|
2016-02-25 03:31:09 +00:00
|
|
|
delay, replaceText, loopDelay, deleteAtStart;
|
2015-03-02 22:44:24 +00:00
|
|
|
if (areParamsOk) {
|
2016-02-25 03:31:09 +00:00
|
|
|
delay = settings.ep_delete_after_delay.delay;
|
|
|
|
loopDelay = settings.ep_delete_after_delay.loopDelay || 3600;
|
2016-05-04 07:39:25 +00:00
|
|
|
deleteAtStart = (settings.ep_delete_after_delay.deleteAtStart !== undefined) ? settings.ep_delete_after_delay.deleteAtStart : true;
|
2016-02-25 03:31:09 +00:00
|
|
|
replaceText = settings.ep_delete_after_delay.text || "The content of this pad has been deleted since it was older than the configured delay.";
|
|
|
|
areParamsOk = (typeof delay === 'number' && delay > 0) ? true : false;
|
2015-03-02 22:44:24 +00:00
|
|
|
if (areParamsOk === false) {
|
|
|
|
console.error('ep_delete_after_delay.delay must be a number an not negative! Check you settings.json.');
|
|
|
|
}
|
2016-02-25 03:31:09 +00:00
|
|
|
areParamsOk = (typeof loopDelay === 'number' && loopDelay > 0) ? true : false;
|
|
|
|
if (areParamsOk === false) {
|
|
|
|
console.error('ep_delete_after_delay.loopDelay must be a number an not negative! Check you settings.json.');
|
|
|
|
}
|
2015-03-02 22:44:24 +00:00
|
|
|
} else {
|
|
|
|
console.error('You need to configure ep_delete_after_delay in your settings.json!');
|
|
|
|
}
|
|
|
|
|
2016-02-25 03:31:09 +00:00
|
|
|
// Recurring deletion function
|
|
|
|
var waitForIt = function() {
|
|
|
|
setTimeout(function() {
|
|
|
|
console.info('New loop');
|
|
|
|
delete_old_pads();
|
|
|
|
waitForIt();
|
|
|
|
}, loopDelay * 1000);
|
|
|
|
};
|
2015-03-02 22:44:24 +00:00
|
|
|
|
2016-02-25 03:31:09 +00:00
|
|
|
// Delete old pads at startup
|
|
|
|
if (deleteAtStart) {
|
|
|
|
delete_old_pads();
|
|
|
|
}
|
2015-03-02 22:44:24 +00:00
|
|
|
|
2016-02-25 03:31:09 +00:00
|
|
|
// start the recurring deletion loop
|
|
|
|
waitForIt();
|
|
|
|
|
|
|
|
// deletion loop
|
|
|
|
function delete_old_pads() {
|
|
|
|
// Deletion queue (avoids max stack size error), 2 workers
|
|
|
|
var q = async.queue(function (pad, callback) {
|
|
|
|
API.getHTML(pad.id, function(err, d) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
var currentTime = (new Date).getTime();
|
|
|
|
fs.writeFile('deleted_pads/'+pad.id+'-'+currentTime+'.html', d.html, function(err) {
|
|
|
|
pad.remove(callback);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, 2);
|
|
|
|
// Emptyness test queue
|
|
|
|
var p = async.queue(function(padId, callback) {
|
|
|
|
padManager.getPad(padId, function(err, pad) {
|
|
|
|
// If this is a new pad, there's nothing to do
|
|
|
|
var head = pad.getHeadRevisionNumber();
|
|
|
|
if (head !== null && head !== undefined && head !== 0) {
|
2015-03-02 22:44:24 +00:00
|
|
|
pad.getLastEdit(function(callback, timestamp) {
|
2015-06-27 11:02:10 +00:00
|
|
|
if (timestamp !== undefined && timestamp !== null) {
|
|
|
|
var currentTime = (new Date).getTime();
|
|
|
|
// Are we over delay?
|
|
|
|
if ((currentTime - timestamp) > (delay * 1000)) {
|
2016-02-25 03:31:09 +00:00
|
|
|
console.debug('Pushing %s to q queue', pad.id);
|
2015-06-27 11:02:10 +00:00
|
|
|
// Remove pad
|
2016-02-25 03:31:09 +00:00
|
|
|
q.push(pad, function (err) {
|
|
|
|
console.info('Pad '+pad.id+' deleted since expired (delay: '+delay+' seconds, last edition: '+timestamp+').');
|
|
|
|
// Create new pad with an explanation
|
|
|
|
padManager.getPad(padId, replaceText, function() {
|
2015-06-27 11:02:10 +00:00
|
|
|
// Create disconnect message
|
|
|
|
var msg = {
|
|
|
|
type: "COLLABROOM",
|
|
|
|
data: {
|
|
|
|
type: "CUSTOM",
|
|
|
|
payload: {
|
2016-02-25 03:31:09 +00:00
|
|
|
authorId: null,
|
2015-06-27 11:02:10 +00:00
|
|
|
action: "requestRECONNECT",
|
|
|
|
padId: padId
|
|
|
|
}
|
2015-03-02 22:44:24 +00:00
|
|
|
}
|
2015-06-27 11:02:10 +00:00
|
|
|
};
|
|
|
|
// Send disconnect message to all clients
|
|
|
|
var sessions = padMessageHandler.sessioninfos;
|
|
|
|
Object.keys(sessions).forEach(function(key){
|
|
|
|
var session = sessions[key];
|
|
|
|
padMessageHandler.handleCustomObjectMessage(msg, false, function(){
|
|
|
|
// TODO: Error handling
|
|
|
|
}); // Send a message to this session
|
|
|
|
});
|
2016-02-25 03:31:09 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.debug('Nothing to do with '+padId+' (not expired)');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.debug('New or empty pad '+padId);
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}, 1);
|
|
|
|
padManager.listAllPads(function (err, data) {
|
|
|
|
for (var i = 0; i < data.padIDs.length; i++) {
|
|
|
|
var padId = data.padIDs[i];
|
|
|
|
console.debug('Pushing %s to p queue', padId);
|
|
|
|
p.push(padId, function (err) { });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.handleMessage = function(hook_name, context, cb) {
|
|
|
|
if (areParamsOk === false) return false;
|
|
|
|
|
|
|
|
var message = context.message,
|
|
|
|
type = message.type;
|
|
|
|
if (type === 'CLIENT_READY' || type === 'COLLABROOM') {
|
|
|
|
var padId = (type === 'CLIENT_READY') ? message.padId : context.client.rooms[1];
|
|
|
|
padManager.getPad(padId, function(callback, pad) {
|
|
|
|
|
|
|
|
// If this is a new pad, there's nothing to do
|
|
|
|
if (pad.getHeadRevisionNumber() !== 0) {
|
|
|
|
|
|
|
|
pad.getLastEdit(function(callback, timestamp) {
|
|
|
|
if (timestamp !== undefined && timestamp !== null) {
|
|
|
|
var currentTime = (new Date).getTime();
|
|
|
|
|
|
|
|
// Are we over delay?
|
|
|
|
if ((currentTime - timestamp) > (delay * 1000)) {
|
|
|
|
|
|
|
|
API.getHTML(padId, function(err, d) {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
2015-06-27 11:02:10 +00:00
|
|
|
}
|
2016-02-25 03:31:09 +00:00
|
|
|
fs.writeFile('deleted_pads/'+padId+'-'+currentTime+'.html', d.html, function(err) {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
// Remove pad
|
|
|
|
padManager.removePad(padId);
|
|
|
|
console.info('Pad '+padId+' deleted since expired (delay: '+delay+' seconds, last edition: '+timestamp+').');
|
|
|
|
|
|
|
|
// Create new pad with an explanation
|
|
|
|
padManager.getPad(padId, replaceText, function() {
|
|
|
|
// Create disconnect message
|
|
|
|
var msg = {
|
|
|
|
type: "COLLABROOM",
|
|
|
|
data: {
|
|
|
|
type: "CUSTOM",
|
|
|
|
payload: {
|
|
|
|
authorId: message.authorId,
|
|
|
|
action: "requestRECONNECT",
|
|
|
|
padId: padId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Send disconnect message to all clients
|
|
|
|
var sessions = padMessageHandler.sessioninfos;
|
|
|
|
Object.keys(sessions).forEach(function(key){
|
|
|
|
var session = sessions[key];
|
|
|
|
padMessageHandler.handleCustomObjectMessage(msg, false, function(){
|
|
|
|
// TODO: Error handling
|
|
|
|
}); // Send a message to this session
|
|
|
|
});
|
|
|
|
if (type === 'COLLABROOM') {
|
|
|
|
cb(null);
|
|
|
|
} else {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2015-06-27 11:02:10 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.info('Nothing to do with '+padId+' (not expired)');
|
|
|
|
cb();
|
|
|
|
}
|
2015-03-02 22:44:24 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.info('New or empty pad '+padId);
|
|
|
|
cb()
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-25 03:31:09 +00:00
|
|
|
exports.registerRoute = function (hook_name, args, cb) {
|
|
|
|
args.app.get('/ttl/:pad', function(req, res, next) {
|
|
|
|
var padId = req.params.pad;
|
|
|
|
|
|
|
|
res.header("Access-Control-Allow-Origin", "*");
|
|
|
|
res.setHeader('Content-Type', 'application/json');
|
|
|
|
|
|
|
|
padManager.getPad(padId, function(callback, pad) {
|
|
|
|
|
|
|
|
// If this is a new pad, there's nothing to do
|
|
|
|
if (pad.getHeadRevisionNumber() !== 0) {
|
|
|
|
|
|
|
|
pad.getLastEdit(function(callback, timestamp) {
|
|
|
|
if (timestamp !== undefined && timestamp !== null) {
|
|
|
|
var currentTime = (new Date).getTime();
|
|
|
|
|
|
|
|
var ttl = Math.floor((delay * 1000 - (currentTime - timestamp))/1000);
|
|
|
|
res.send('{"ttl": '+ttl+'}');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.send('{"ttl": null, "msg": "New or empty pad"}');
|
|
|
|
}
|
|
|
|
cb && cb();
|
|
|
|
});
|
|
|
|
});
|
2015-03-02 22:44:24 +00:00
|
|
|
}
|