Skip to content

Commit

Permalink
feat: #8521, allow editing title before posting from queue
Browse files Browse the repository at this point in the history
  • Loading branch information
barisusakli committed Jul 21, 2020
1 parent 8ddc8dd commit 2485a55
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 14 deletions.
23 changes: 15 additions & 8 deletions public/src/admin/manage/post-queue.js
Expand Up @@ -22,31 +22,38 @@ define('admin/manage/post-queue', function () {
return false;
});

$('.posts-list').on('click', '.post-content', function () {
handleContentEdit('.post-content', '.post-content-editable', 'textarea');
handleContentEdit('.topic-title', '.topic-title-editable', 'input');
};

function handleContentEdit(displayClass, editableClass, inputSelector) {
$('.posts-list').on('click', displayClass, function () {
var el = $(this);
el.addClass('hidden');
var textareaParent = el.parent().find('.post-content-editable');
textareaParent.removeClass('hidden').find('textarea').focus();
var inputEl = el.parent().find(editableClass);
inputEl.removeClass('hidden').find(inputSelector).focus();
});

$('.posts-list').on('blur', '.post-content-editable textarea', function () {
$('.posts-list').on('blur', editableClass + ' ' + inputSelector, function () {
var textarea = $(this);
var preview = textarea.parent().parent().find('.post-content');
var preview = textarea.parent().parent().find(displayClass);
var id = textarea.parents('[data-id]').attr('data-id');
var titleEdit = displayClass === '.topic-title';

socket.emit('posts.editQueuedContent', {
id: id,
content: textarea.val(),
title: titleEdit ? textarea.val() : undefined,
content: titleEdit ? undefined : textarea.val(),
}, function (err, data) {
if (err) {
return app.alertError(err);
}
preview.html(data.postData.content);
preview.html(titleEdit ? data.postData.title : data.postData.content);
textarea.parent().addClass('hidden');
preview.removeClass('hidden');
});
});
};
}

return PostQueue;
});
9 changes: 7 additions & 2 deletions src/posts/queue.js
Expand Up @@ -147,7 +147,7 @@ module.exports = function (Posts) {
socketHelpers.notifyNew(data.uid, 'newPost', result);
}

Posts.editQueuedContent = async function (uid, id, content) {
Posts.editQueuedContent = async function (uid, id, content, title) {
const canEditQueue = await Posts.canEditQueue(uid, id);
if (!canEditQueue) {
throw new Error('[[error:no-privileges]]');
Expand All @@ -156,7 +156,12 @@ module.exports = function (Posts) {
if (!data) {
return;
}
data.data.content = content;
if (content !== undefined) {
data.data.content = content;
}
if (title !== undefined) {
data.data.title = title;
}
await db.setObjectField('post:queue:' + id, 'data', JSON.stringify(data.data));
};

Expand Down
4 changes: 2 additions & 2 deletions src/socket.io/posts.js
Expand Up @@ -174,10 +174,10 @@ async function acceptOrReject(method, socket, data) {
}

SocketPosts.editQueuedContent = async function (socket, data) {
if (!data || !data.id || !data.content) {
if (!data || !data.id || (!data.content && !data.title)) {
throw new Error('[[error:invalid-data]]');
}
await posts.editQueuedContent(socket.uid, data.id, data.content);
await posts.editQueuedContent(socket.uid, data.id, data.content, data.title);
return await plugins.fireHook('filter:parse.post', { postData: data });
};

Expand Down
9 changes: 7 additions & 2 deletions src/views/admin/manage/post-queue.tpl
Expand Up @@ -19,7 +19,7 @@
<tr>
<th>[[admin/manage/post-queue:user]]</th>
<th>[[admin/manage/post-queue:category]]</th>
<th>[[admin/manage/post-queue:title]]</th>
<th>[[admin/manage/post-queue:title]] <i class="fa fa-info-circle" data-toggle="tooltip" title="[[admin/manage/post-queue:content-editable]]"></i></th>
<th>[[admin/manage/post-queue:content]] <i class="fa fa-info-circle" data-toggle="tooltip" title="[[admin/manage/post-queue:content-editable]]"></i></th>
<th>[[admin/manage/post-queue:posted]]</th>
<th></th>
Expand All @@ -44,9 +44,14 @@
<!-- ENDIF posts.data.tid -->
{posts.data.title}
</td>
{{{if !posts.data.tid}}}
<td class="col-md-2 topic-title-editable hidden">
<input class="form-control" type="text" value="{posts.data.title}"/>
</td>
{{{end}}}
<td class="col-md-5 post-content">{posts.data.content}</td>
<td class="col-md-5 post-content-editable hidden">
<textarea>{posts.data.rawContent}</textarea>
<textarea class="form-control">{posts.data.rawContent}</textarea>
</td>
<td class="col-md-1">
<span class="timeago" title={posts.data.timestampISO}></span>
Expand Down
14 changes: 14 additions & 0 deletions test/posts.js
Expand Up @@ -1012,6 +1012,7 @@ describe('Post\'s', function () {
describe('post queue', function () {
var uid;
var queueId;
var topicQueueId;
var jar;
before(function (done) {
meta.config.postQueue = 1;
Expand All @@ -1033,6 +1034,7 @@ describe('Post\'s', function () {
assert.ifError(err);
assert.strictEqual(result.queued, true);
assert.equal(result.message, '[[success:post-queued]]');
topicQueueId = result.id;

done();
});
Expand Down Expand Up @@ -1082,6 +1084,18 @@ describe('Post\'s', function () {
});
});

it('should edit topic title in queue', function (done) {
socketPosts.editQueuedContent({ uid: globalModUid }, { id: topicQueueId, title: 'new topic title' }, function (err) {
assert.ifError(err);
request(nconf.get('url') + '/api/post-queue', { jar: jar, json: true }, function (err, res, body) {
assert.ifError(err);
assert.equal(body.posts[0].type, 'topic');
assert.equal(body.posts[0].data.title, 'new topic title');
done();
});
});
});

it('should prevent regular users from approving posts', function (done) {
socketPosts.accept({ uid: uid }, { id: queueId }, function (err) {
assert.equal(err.message, '[[error:no-privileges]]');
Expand Down

0 comments on commit 2485a55

Please sign in to comment.