From d2eb73df96d3059c64d666ef76e7885b3d4ef8da Mon Sep 17 00:00:00 2001 From: Baris Usakli Date: Fri, 30 Aug 2013 11:40:20 -0400 Subject: [PATCH] can add and remove images from posts --- public/src/modules/composer.js | 2 +- src/posts.js | 38 ++++++++++++++++++---------------- src/websockets.js | 14 ++++++++++--- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/public/src/modules/composer.js b/public/src/modules/composer.js index 8d8f0cb7ca..8b34f063fe 100644 --- a/public/src/modules/composer.js +++ b/public/src/modules/composer.js @@ -152,7 +152,7 @@ define(['taskbar'], function(taskbar) { pid: threadData.pid, title: threadData.title || '', body: threadData.body || '', - images: [] + images: threadData.uploadedImages }; composer.load(uuid); } else { diff --git a/src/posts.js b/src/posts.js index ccced14a70..ac127bc57c 100644 --- a/src/posts.js +++ b/src/posts.js @@ -369,38 +369,40 @@ var RDB = require('./redis.js'), var imgur = require('./imgur'); imgur.setClientID(meta.config.imgurClientID); - var uploadedImages = []; + if(!images) + return callback(null, []); + + var uploadedImages = images.filter(function(image) { return !!image.url; }); + + function uploadImage(image, next) { + if(!image.data) + return next(null); - function uploadImage(image, callback) { imgur.upload(image.data, 'base64', function(err, data) { if(err) { - callback(err); + next(err); } else { if(data.success) { var img= {url:data.data.link, name:image.name}; uploadedImages.push(img); - callback(null); + next(null); } else { winston.error('Can\'t upload image, did you set imgurClientID?'); - callback(data); + next(data); } } }); } - if(!images) { - callback(null, uploadedImages); - } else { - async.each(images, uploadImage, function(err) { - if(!err) { - Posts.setPostField(pid, 'uploadedImages', JSON.stringify(uploadedImages)); - callback(null, uploadedImages); - } else { - console.log(err); - callback(err, null); - } - }); - } + async.each(images, uploadImage, function(err) { + if(!err) { + Posts.setPostField(pid, 'uploadedImages', JSON.stringify(uploadedImages)); + callback(null, uploadedImages); + } else { + console.log(err); + callback(err, null); + } + }); } Posts.getPostsByUid = function(uid, start, end, callback) { diff --git a/src/websockets.js b/src/websockets.js index f007ec127d..49adf7fd1f 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -583,7 +583,6 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }), if (uid > 0) { if (parseInt(data.tid) > 0) { topics.getTopicData(data.tid, function(topicData) { - if (data.body) topicData.body = data.body; @@ -606,9 +605,17 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }), } }); } else if (parseInt(data.pid) > 0) { + async.parallel([ function(next) { - posts.getPostField(data.pid, 'content', function(raw) { + posts.getPostFields(data.pid, ['content', 'uploadedImages'], function(raw) { + try { + raw.uploadedImages = JSON.parse(raw.uploadedImages); + } catch(e) { + winston.err(e); + raw.uploadedImages = []; + } + next(null, raw); }); }, @@ -621,7 +628,8 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }), socket.emit('api:composer.push', { title: results[1], pid: data.pid, - body: results[0] + body: results[0].content, + uploadedImages: results[0].uploadedImages }); }); }