ep_comments_page
Advanced tools
Comparing version 0.0.18 to 0.0.19
@@ -30,2 +30,12 @@ | ||
}); | ||
}; | ||
exports.addPadCommentReply = function(padID, data, callback) | ||
{ | ||
commentManager.addCommentReply(padID, data, function (err, replyID, reply) | ||
{ | ||
if(ERR(err, callback)) return; | ||
if(replyID !== null) callback(null, replyID, reply); | ||
}); | ||
}; |
106
index.js
var eejs = require('ep_etherpad-lite/node/eejs/'); | ||
var fs = require("fs"); | ||
var formidable = require('formidable'); | ||
@@ -7,14 +6,4 @@ var clientIO = require('socket.io-client'); | ||
var comments = require('./comments'); | ||
var padManager = require("ep_etherpad-lite/node/db/PadManager"); | ||
var settings = require("ep_etherpad-lite/node/utils/Settings"); | ||
var apiUtils = require('./apiUtils'); | ||
//ensure we have an apikey | ||
var apikey = ""; | ||
try { | ||
apikey = fs.readFileSync("./APIKEY.txt","utf8"); | ||
} | ||
catch(e){ | ||
console.warn('Could not find APIKEY'); | ||
} | ||
exports.handleMessageSecurity = function(hook_name, context, callback){ | ||
@@ -94,2 +83,12 @@ if(context.message && context.message.data && context.message.data.apool){ | ||
// comment reply added via API | ||
socket.on('apiAddCommentReply', function (data) { | ||
var padId = data.padId; | ||
var replyId = data.replyId; | ||
var reply = data.reply; | ||
reply.replyId = replyId; | ||
socket.broadcast.to(padId).emit('pushAddCommentReply', replyId, reply); | ||
}); | ||
}); | ||
@@ -127,15 +126,11 @@ }; | ||
// check the api key | ||
apiKeyReceived = fields.apikey || fields.api_key; | ||
if(apiKeyReceived !== apikey.trim()) { | ||
res.statusCode = 401; | ||
res.json({code: 4, message: "no or wrong API Key", data: null}); | ||
return; | ||
} | ||
if(!apiUtils.validateApiKey(fields, res)) return; | ||
// check comment data | ||
var error = checkCommentData(fields); | ||
if(error) { | ||
res.json({code: 1, message: error, data: null}); | ||
return; | ||
} | ||
// check required fields from comment data | ||
if(!apiUtils.validateRequiredFields(fields, ['name', 'text'], res)) return; | ||
// sanitize pad id before continuing | ||
var padIdReceived = apiUtils.sanitizePadId(req); | ||
// create data to hold comment information: | ||
var data = { | ||
@@ -148,8 +143,2 @@ author: "empty", | ||
// sanitize pad id before continuing | ||
var padIdReceived = req.params.pad | ||
padManager.sanitizePadId(padIdReceived, function(padId) { | ||
padIdReceived = padId; | ||
}); | ||
comments.addPadComment(padIdReceived, data, function(err, commentId, comment) { | ||
@@ -166,9 +155,35 @@ if(err) { | ||
} | ||
args.app.post('/p/:pad/:rev?/commentReplies', function(req, res) { | ||
new formidable.IncomingForm().parse(req, function (err, fields, files) { | ||
// check the api key | ||
if(!apiUtils.validateApiKey(fields, res)) return; | ||
var checkCommentData = function(fields) { | ||
if(typeof fields.name === 'undefined') return "name is required"; | ||
if(typeof fields.text === 'undefined') return "text is required"; | ||
// check required fields from comment data | ||
if(!apiUtils.validateRequiredFields(fields, ['commentId', 'name', 'text'], res)) return; | ||
return false; | ||
// sanitize pad id before continuing | ||
var padIdReceived = apiUtils.sanitizePadId(req); | ||
// create data to hold comment reply information: | ||
var comment = { | ||
name: fields.name | ||
}; | ||
var data = { | ||
author: "empty", | ||
commentId: fields.commentId, | ||
reply: fields.text, | ||
comment: comment | ||
}; | ||
comments.addPadCommentReply(padIdReceived, data, function(err, replyId, reply) { | ||
if(err) { | ||
res.json({code: 2, message: "internal error", data: null}); | ||
} else { | ||
broadcastCommentReplyAdded(padIdReceived, replyId, reply); | ||
res.json({code: 0, replyId: replyId}); | ||
} | ||
}); | ||
}); | ||
}); | ||
} | ||
@@ -188,13 +203,14 @@ | ||
var buildBroadcastUrl = function() { | ||
var url = ""; | ||
if(settings.ssl) { | ||
url += "https://"; | ||
} else { | ||
url += "http://"; | ||
} | ||
url += settings.ip + ":" + settings.port + "/comment"; | ||
var broadcastCommentReplyAdded = function(padId, replyId, reply) { | ||
var socket = clientIO.connect(broadcastUrl); | ||
return url; | ||
var data = { | ||
padId: padId, | ||
replyId: replyId, | ||
reply: reply | ||
}; | ||
socket.emit('apiAddCommentReply', data); | ||
} | ||
var broadcastUrl = buildBroadcastUrl(); | ||
var broadcastUrl = apiUtils.broadcastUrlFor("/comment"); |
{ | ||
"description": "Adds comments on sidebar and link it to the text. Support for Page View, requires ep_page_view", | ||
"name": "ep_comments_page", | ||
"version": "0.0.18", | ||
"version": "0.0.19", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Nicolas Lescop", |
@@ -30,2 +30,7 @@ var _, $, jQuery; | ||
this.init(); | ||
// If we're on a read only pad then hide the ability to attempt to merge a suggestion | ||
if(clientVars.readonly){ | ||
this.padInner.append("<style>.comment-changeTo-approve{display:none;}</style>"); | ||
} | ||
} | ||
@@ -117,2 +122,21 @@ | ||
// Listen for include suggested change toggle | ||
this.container.on("change", '#reply-suggestion-checkbox', function(){ | ||
if($(this).is(':checked')){ | ||
// Get current text -- cake | ||
var commentId = $(this).parent().parent().parent()[0].id; | ||
var padOuter = $('iframe[name="ace_outer"]').contents(); | ||
var padInner = padOuter.find('iframe[name="ace_inner"]'); | ||
var currentString = padInner.contents().find("."+commentId).html(); | ||
console.log("cS", currentString); | ||
$(this).parent().parent().find(".comment-suggest-from").html(currentString); | ||
$('iframe[name="ace_outer"]').contents().find('.suggestion').show(); | ||
}else{ | ||
$('iframe[name="ace_outer"]').contents().find('.suggestion').hide(); | ||
} | ||
}); | ||
// Create hover modal | ||
@@ -297,3 +321,3 @@ $('iframe[name="ace_outer"]').contents().find("body") | ||
var content = $("#replyTemplate").tmpl(replies); | ||
$('iframe[name="ace_outer"]').contents().find('#'+commentId + ' input').before(content); | ||
$('iframe[name="ace_outer"]').contents().find('#'+commentId + ' .comment-reply-input').before(content); | ||
}); | ||
@@ -549,2 +573,8 @@ }; | ||
// Gets the original text of a comment | ||
ep_comments.prototype.getOriginalText = function(commentId){ | ||
console.log(this.text); | ||
console.log("CAKE need TODO -- get the text of a given commentId, this would return a string such as 'Hello world'"); | ||
} | ||
// Listen for comment replies | ||
@@ -551,0 +581,0 @@ ep_comments.prototype.commentRepliesListen = function(){ |
@@ -1,24 +0,20 @@ | ||
var appUrl = 'http://localhost:9001'; | ||
var supertest = require('ep_etherpad-lite/node_modules/supertest'), | ||
io = require('socket.io-client'), | ||
utils = require('../../../utils'), | ||
createPad = utils.createPad, | ||
createComment = utils.createComment, | ||
appUrl = utils.appUrl, | ||
apiKey = utils.apiKey, | ||
codeToBe0 = utils.codeToBe0, | ||
codeToBe1 = utils.codeToBe1, | ||
codeToBe4 = utils.codeToBe4, | ||
commentsEndPointFor = utils.commentsEndPointFor, | ||
api = supertest(appUrl); | ||
var supertest = require('ep_etherpad-lite/node_modules/supertest'), | ||
fs = require('fs'), | ||
path = require('path'), | ||
io = require('socket.io-client'), | ||
request = require('request'), | ||
api = supertest(appUrl), | ||
randomString = require('ep_etherpad-lite/static/js/pad_utils').randomString; | ||
describe('create comment API', function(){ | ||
var padID; | ||
var apiVersion = 1; | ||
var padID; | ||
var etherpad_root = '/../../../../../../ep_etherpad-lite/../..'; | ||
var filePath = path.join(__dirname, etherpad_root + '/APIKEY.txt'); | ||
var apiKey = fs.readFileSync(filePath, {encoding: 'utf-8'}); | ||
apiKey = apiKey.replace(/\n$/, ""); | ||
describe('addComment', function(){ | ||
//create a new pad before each test run | ||
beforeEach(function(done){ | ||
padID = randomString(5); | ||
createPad(padID, done); | ||
padID = createPad(done); | ||
}); | ||
@@ -85,3 +81,4 @@ | ||
describe('addComment broadcast', function(){ | ||
describe('create comment API broadcast', function(){ | ||
var padID; | ||
var messageReceived; | ||
@@ -96,8 +93,7 @@ | ||
//create a new pad before each test run... | ||
padID = randomString(5); | ||
createPad(padID, function() { | ||
padID = createPad(function(err, pad) { | ||
// ... and listens to the broadcast message: | ||
var socket = io.connect(appUrl + "/comment"); | ||
var req = { padId: padID }; | ||
// needs to get comments to be able to join the padID room, where the messages will be broadcast to: | ||
var req = { padId: pad }; | ||
// needs to get comments to be able to join the pad room, where the messages will be broadcast to: | ||
socket.emit('getComments', req, function (res){ | ||
@@ -114,24 +110,11 @@ socket.on('pushAddComment', function(data) { | ||
it('broadcasts comment creation to other clients of same pad', function(done) { | ||
var url = appUrl + commentsEndPointFor(padID); | ||
request.post(url, | ||
{ form: { | ||
'apikey': apiKey, | ||
'name': 'John Doe', | ||
'text': 'This is a comment', | ||
} }, | ||
function(error, res, body) { | ||
if(error) { | ||
throw error; | ||
} | ||
else if(res.statusCode != 200) { | ||
throw new Error("Failed on calling API. Status code: " + res.statusCode); | ||
} | ||
else { | ||
setTimeout(function() { //give it a second to process the message on the client | ||
if(!messageReceived) throw new Error("Message should had been received"); | ||
done(); | ||
}, 1000); | ||
} | ||
} | ||
); | ||
createComment(padID, function(err, commentId) { | ||
if(err) throw err; | ||
if(!commentId) throw new Error("Comment should had been created"); | ||
setTimeout(function() { //give it some time to process the message on the client | ||
if(!messageReceived) throw new Error("Message should had been received"); | ||
done(); | ||
}, 100); | ||
}); | ||
}); | ||
@@ -141,27 +124,13 @@ | ||
// creates another pad... | ||
otherPadId = randomString(5); | ||
createPad(otherPadId, function() { | ||
createPad(function(err, otherPadId) { | ||
// ... and add comment to it: | ||
var url = appUrl + commentsEndPointFor(otherPadId); | ||
request.post(url, | ||
{ form: { | ||
'apikey': apiKey, | ||
'name': 'Another author', | ||
'text': 'Comment for the other pad', | ||
} }, | ||
function(error, res, body) { | ||
if(error) { | ||
throw error; | ||
} | ||
else if(res.statusCode != 200) { | ||
throw new Error("Failed on calling API. Status code: " + res.statusCode); | ||
} | ||
else { | ||
setTimeout(function() { //give it a second to process the message on the client | ||
if(messageReceived) throw new Error("Message should had been received only for pad " + padID); | ||
done(); | ||
}, 1000); | ||
} | ||
} | ||
); | ||
createComment(otherPadId, function(err, commentId) { | ||
if(err) throw err; | ||
if(!commentId) throw new Error("Comment should had been created"); | ||
setTimeout(function() { //give it some time to process the message on the client | ||
if(messageReceived) throw new Error("Message should had been received only for pad " + padID); | ||
done(); | ||
}, 100); | ||
}); | ||
}); | ||
@@ -171,26 +140,1 @@ }); | ||
}) | ||
/* ***** helper functions ***** */ | ||
var createPad = function(pad, done) { | ||
api.get('/api/'+apiVersion+'/createPad?apikey='+apiKey+"&padID="+pad) | ||
.end(function(err, res){ | ||
if(err || (res.body.code !== 0)) done(new Error("Unable to create new Pad")); | ||
}) | ||
done(); | ||
} | ||
function commentsEndPointFor(pad) { | ||
return '/p/'+pad+'/comments' | ||
} | ||
var codeToBe = function(expectedCode, res) { | ||
if(res.body.code !== expectedCode){ | ||
throw new Error("Code should be " + expectedCode + ", was " + res.body.code); | ||
} | ||
} | ||
function codeToBe0(res) { codeToBe(0, res) } | ||
function codeToBe1(res) { codeToBe(1, res) } | ||
function codeToBe4(res) { codeToBe(4, res) } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
69239
22
1612