From 564662ee0052c78d444f898b14f5c4bd2018958c Mon Sep 17 00:00:00 2001 From: Baris Usakli Date: Tue, 17 Sep 2013 13:37:03 -0400 Subject: [PATCH] closes #294 --- public/src/forum/topic.js | 11 ---- public/src/modules/composer.js | 95 +++++++++++++++------------------- public/templates/topic.tpl | 10 ---- src/postTools.js | 15 ++---- src/posts.js | 70 ++++++------------------- src/topics.js | 4 +- src/websockets.js | 20 +++---- 7 files changed, 73 insertions(+), 152 deletions(-) diff --git a/public/src/forum/topic.js b/public/src/forum/topic.js index 3e6b572d3c..7959da5d47 100644 --- a/public/src/forum/topic.js +++ b/public/src/forum/topic.js @@ -377,17 +377,6 @@ $(this).fadeIn(250); }); - if(data.uploadedImages && data.uploadedImages.length) { - $('#images_'+data.pid).html(''); - for(var i=0; i< data.uploadedImages.length; ++i) { - var img = $(' '+data.uploadedImages[i].name+'
'); - $('#images_' + data.pid).append(img); - } - } else { - $('#images_'+data.pid).html(''); - } - - console.log('time to recreate images', data); }); socket.on('api:posts.favourite', function(data) { diff --git a/public/src/modules/composer.js b/public/src/modules/composer.js index b6c3715dd4..8326ec0245 100644 --- a/public/src/modules/composer.js +++ b/public/src/modules/composer.js @@ -1,35 +1,39 @@ define(['taskbar'], function(taskbar) { var composer = { - initialized: false, - active: undefined, - taskbar: taskbar, - posts: {}, - postContainer: undefined, - }; - - function createImageLabel(img, postImages) { - var imageLabel = $('' + img.name +''); - var closeButton = $(''); - - closeButton.on('click', function(e) { - - imageLabel.remove(); - var index = postImages.indexOf(img); - if(index !== -1) { - postImages.splice(index, 1); - } - }); + initialized: false, + active: undefined, + taskbar: taskbar, + posts: {}, + postContainer: undefined, + }; + + var uploadsInProgress = []; + + function createImagePlaceholder(img) { + var text = $('.post-window textarea').val(), + textarea = $('.post-window textarea'), + imgText = "!["+img.name+"](uploading...)"; + + text += imgText; + textarea.val(text + " "); + uploadsInProgress.push(1); + socket.emit("api:posts.uploadImage", img, function(err, data) { - imageLabel.append(closeButton); - return imageLabel; + var currentText = textarea.val(); + imgText = "!["+data.name+"](uploading...)"; + + if(!err) + textarea.val(currentText.replace(imgText, "!["+data.name+"]("+data.url+")")); + else + textarea.val(currentText.replace(imgText, "!["+data.name+"](upload error)")); + uploadsInProgress.pop(); + }); } function loadFile(file) { var reader = new FileReader(), dropDiv = $('.post-window .imagedrop'), - imagelist = $('.post-window .imagelist'), - uuid = dropDiv.parents('[data-uuid]').attr('data-uuid'), - posts = composer.posts[uuid]; + uuid = dropDiv.parents('[data-uuid]').attr('data-uuid'); $(reader).on('loadend', function(e) { var bin = this.result; @@ -40,11 +44,8 @@ define(['taskbar'], function(taskbar) { data: bin }; - posts.images.push(img); - - var imageLabel = createImageLabel(img, posts.images); + createImagePlaceholder(img); - imagelist.append(imageLabel); dropDiv.hide(); }); @@ -89,7 +90,6 @@ define(['taskbar'], function(taskbar) { drop.on('drop', function(e) { e.preventDefault(); var uuid = drop.parents('[data-uuid]').attr('data-uuid'), - posts = composer.posts[uuid], dt = e.dataTransfer, files = dt.files; @@ -121,7 +121,6 @@ define(['taskbar'], function(taskbar) { '' + '' + '' + - '
'+ '
Drag and Drop Images Here
'+ '
' + '
' + @@ -151,8 +150,7 @@ define(['taskbar'], function(taskbar) { cid: threadData.cid, pid: threadData.pid, title: threadData.title || '', - body: threadData.body || '', - images: threadData.uploadedImages || [] + body: threadData.body || '' }; composer.load(uuid); } else { @@ -265,18 +263,6 @@ define(['taskbar'], function(taskbar) { }); } - function createPostImages(images) { - var imagelist = $(composer.postContainer).find('.imagelist'); - imagelist.empty(); - - if(images && images.length) { - for(var i=0; i 0) { @@ -334,6 +319,16 @@ define(['taskbar'], function(taskbar) { titleEl.value = titleEl.value.trim(); bodyEl.value = bodyEl.value.trim(); + if(uploadsInProgress.length) { + return app.alert({ + type: 'warning', + timeout: 2000, + title: 'Still uploading', + message: "Please wait for uploads to complete.", + alert_id: 'post_error' + }); + } + if (titleEl.value.length < config.minimumTitleLength) { return app.alert({ type: 'danger', @@ -359,21 +354,18 @@ define(['taskbar'], function(taskbar) { socket.emit('api:topics.post', { 'title' : titleEl.value, 'content' : bodyEl.value, - 'category_id' : postData.cid, - images: composer.posts[post_uuid].images + 'category_id' : postData.cid }); } else if (parseInt(postData.tid) > 0) { socket.emit('api:posts.reply', { 'topic_id' : postData.tid, - 'content' : bodyEl.value, - images: composer.posts[post_uuid].images + 'content' : bodyEl.value }); } else if (parseInt(postData.pid) > 0) { socket.emit('api:posts.edit', { pid: postData.pid, content: bodyEl.value, - title: titleEl.value, - images: composer.posts[post_uuid].images + title: titleEl.value }); } @@ -383,7 +375,6 @@ define(['taskbar'], function(taskbar) { composer.discard = function(post_uuid) { if (composer.posts[post_uuid]) { $(composer.postContainer).find('.imagedrop').hide(); - $(composer.postContainer).find('.imagelist').empty(); delete composer.posts[post_uuid]; composer.minimize(); taskbar.discard('composer', post_uuid); diff --git a/public/templates/topic.tpl b/public/templates/topic.tpl index 624c233416..3cd470c361 100644 --- a/public/templates/topic.tpl +++ b/public/templates/topic.tpl @@ -56,11 +56,6 @@
{main_posts.content}
-
- - {main_posts.uploadedImages.name}
- -
{main_posts.signature}
@@ -116,11 +111,6 @@
{posts.content}
-
- - {posts.uploadedImages.name}
- -
{posts.signature}
diff --git a/src/postTools.js b/src/postTools.js index 2006abc0c2..f8d89887fb 100644 --- a/src/postTools.js +++ b/src/postTools.js @@ -56,7 +56,7 @@ var RDB = require('./redis.js'), }); } - PostTools.edit = function(uid, pid, title, content, images) { + PostTools.edit = function(uid, pid, title, content) { var success = function() { posts.setPostField(pid, 'content', content); posts.setPostField(pid, 'edited', Date.now()); @@ -67,11 +67,6 @@ var RDB = require('./redis.js'), }); async.parallel([ - function(next) { - posts.uploadPostImages(pid, images, function(err, uploadedImages) { - next(err, uploadedImages); - }); - }, function(next) { posts.getPostField(pid, 'tid', function(tid) { PostTools.isMain(pid, tid, function(isMainPost) { @@ -90,12 +85,12 @@ var RDB = require('./redis.js'), PostTools.toHTML(content, next); } ], function(err, results) { - io.sockets.in('topic_' + results[1].tid).emit('event:post_edited', { + console.log("TEEEST"); + io.sockets.in('topic_' + results[0].tid).emit('event:post_edited', { pid: pid, title: title, - isMainPost: results[1].isMainPost, - content: results[2], - uploadedImages:results[0] + isMainPost: results[0].isMainPost, + content: results[1] }); }); }; diff --git a/src/posts.js b/src/posts.js index ca56201021..c218186705 100644 --- a/src/posts.js +++ b/src/posts.js @@ -166,17 +166,6 @@ var RDB = require('./redis.js'), postData['edited-class'] = postData.editor !== '' ? '' : 'none'; postData['relativeEditTime'] = postData.edited !== '0' ? utils.relativeTime(postData.edited) : ''; - if(postData.uploadedImages) { - try { - postData.uploadedImages = JSON.parse(postData.uploadedImages); - } catch(err) { - postData.uploadedImages = []; - winston.err(err); - } - } else { - postData.uploadedImages = []; - } - postTools.toHTML(postData.content, function(err, content) { postData.content = content; posts.push(postData); @@ -226,7 +215,7 @@ var RDB = require('./redis.js'), }); } - Posts.reply = function(tid, uid, content, images, callback) { + Posts.reply = function(tid, uid, content, callback) { if(content) { content = content.trim(); } @@ -242,7 +231,7 @@ var RDB = require('./redis.js'), return; } - Posts.create(uid, tid, content, images, function(postData) { + Posts.create(uid, tid, content, function(postData) { if (postData) { topics.markUnRead(tid); @@ -270,7 +259,7 @@ var RDB = require('./redis.js'), }); }; - Posts.create = function(uid, tid, content, images, callback) { + Posts.create = function(uid, tid, content, callback) { if (uid === null) { callback(null); return; @@ -293,7 +282,6 @@ var RDB = require('./redis.js'), 'editor': '', 'edited': 0, 'deleted': 0, - 'uploadedImages': '[]', 'fav_button_class': '', 'fav_star_class': 'icon-star-empty', 'show_banned': 'hide', @@ -331,16 +319,6 @@ var RDB = require('./redis.js'), user.onNewPostMade(uid, tid, pid, timestamp); async.parallel({ - uploadedImages: function(next) { - Posts.uploadPostImages(postData.pid, images, function(err, uploadedImages) { - if(err) { - winston.error('Uploading images failed!', err.stack); - next(null, []); - } else { - next(null, uploadedImages); - } - }); - }, content: function(next) { plugins.fireHook('filter:post.get', postData, function(postData) { postTools.toHTML(postData.content, function(err, content) { @@ -349,7 +327,6 @@ var RDB = require('./redis.js'), }); } }, function(err, results) { - postData.uploadedImages = results.uploadedImages; postData.content = results.content; callback(postData); }); @@ -365,42 +342,25 @@ var RDB = require('./redis.js'), }); } - Posts.uploadPostImages = function(pid, images, callback) { + Posts.uploadPostImage = function(image, callback) { var imgur = require('./imgur'); imgur.setClientID(meta.config.imgurClientID); - if(!images) - return callback(null, []); + if(!image) + return callback('invalid image', null); - var uploadedImages = images.filter(function(image) { return !!image.url; }); - - function uploadImage(image, next) { - if(!image.data) - return next(null); + imgur.upload(image.data, 'base64', function(err, data) { + if(err) { + callback('Can\'t upload image!', null); + } else { + if(data.success) { + var img= {url:data.data.link, name:image.name}; - imgur.upload(image.data, 'base64', function(err, data) { - if(err) { - next(err); + callback(null, img); } else { - if(data.success) { - var img= {url:data.data.link, name:image.name}; - uploadedImages.push(img); - next(null); - } else { - winston.error('Can\'t upload image, did you set imgurClientID?'); - next(data); - } + winston.error('Can\'t upload image, did you set imgurClientID?'); + callback("upload error", 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); } }); } diff --git a/src/topics.js b/src/topics.js index e8aa3ea557..a92f8dd444 100644 --- a/src/topics.js +++ b/src/topics.js @@ -604,7 +604,7 @@ var RDB = require('./redis.js') }); } - Topics.post = function(uid, title, content, category_id, images, callback) { + Topics.post = function(uid, title, content, category_id, callback) { if (!category_id) throw new Error('Attempted to post without a category_id'); @@ -678,7 +678,7 @@ var RDB = require('./redis.js') feed.updateCategory(category_id); - posts.create(uid, tid, content, images, function(postData) { + posts.create(uid, tid, content, function(postData) { if (postData) { // Auto-subscribe the post creator to the newly created topic diff --git a/src/websockets.js b/src/websockets.js index ea18ef15bc..c365e416bc 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -322,7 +322,7 @@ var SocketIO = require('socket.io').listen(global.server, { socket.on('api:topics.post', function(data) { - topics.post(uid, data.title, data.content, data.category_id, data.images, function(err, result) { + topics.post(uid, data.title, data.content, data.category_id, function(err, result) { if(err) { if(err.message === 'not-logged-in') { socket.emit('event:alert', { @@ -379,7 +379,7 @@ var SocketIO = require('socket.io').listen(global.server, { return; } - posts.reply(data.topic_id, uid, data.content, data.images, function(err, result) { + posts.reply(data.topic_id, uid, data.content, function(err, result) { if(err) { if(err.message === 'content-too-short') { posts.emitContentTooShortAlert(socket); @@ -477,6 +477,10 @@ var SocketIO = require('socket.io').listen(global.server, { }); }); + socket.on('api:posts.uploadImage', function(data, callback) { + posts.uploadPostImage(data, callback); + }); + socket.on('api:posts.getRawPost', function(data) { posts.getPostField(data.pid, 'content', function(raw) { socket.emit('api:posts.getRawPost', { post: raw }); @@ -623,14 +627,7 @@ var SocketIO = require('socket.io').listen(global.server, { async.parallel([ function(next) { - posts.getPostFields(data.pid, ['content', 'uploadedImages'], function(raw) { - try { - raw.uploadedImages = JSON.parse(raw.uploadedImages); - } catch(e) { - winston.err(e); - raw.uploadedImages = []; - } - + posts.getPostFields(data.pid, ['content'], function(raw) { next(null, raw); }); }, @@ -643,8 +640,7 @@ var SocketIO = require('socket.io').listen(global.server, { socket.emit('api:composer.push', { title: results[1], pid: data.pid, - body: results[0].content, - uploadedImages: results[0].uploadedImages + body: results[0].content }); }); }