Skip to content

Commit

Permalink
Merge branch 'f24' into verify-message
Browse files Browse the repository at this point in the history
  • Loading branch information
KesterTan authored Sep 25, 2024
2 parents 653c107 + 64d9e6f commit 2a447be
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 26 deletions.
Binary file added dump.rdb
Binary file not shown.
2 changes: 2 additions & 0 deletions public/openapi/components/schemas/PostObject.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ PostObject:
type: boolean
verify:
type: number
answered:
type: number
upvotes:
type: number
downvotes:
Expand Down
4 changes: 4 additions & 0 deletions public/openapi/read/topic/topic_id.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ get:
type: number
verifyUid:
type: number
answered:
type: number
upvotes:
type: number
downvotes:
Expand All @@ -79,6 +81,8 @@ get:
type: number
deleterUid:
type: number
answererUid:
type: number
edited:
type: number
timestampISO:
Expand Down
4 changes: 4 additions & 0 deletions public/openapi/write/posts/pid.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ get:
type: number
verifyUid:
type: number
answered:
type: number
upvotes:
type: number
downvotes:
type: number
deleterUid:
type: number
answererUid:
type: number
edited:
type: number
replies:
Expand Down
2 changes: 2 additions & 0 deletions public/openapi/write/posts/pid/replies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ get:
type: number
verify:
type: number
answered:
type: number
upvotes:
type: number
downvotes:
Expand Down
7 changes: 7 additions & 0 deletions public/src/client/topic/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ define('forum/topic/events', [

'posts.verify': toggleVerifyMessage,
'posts.unverify': toggleVerifyMessage,

Check failure on line 45 in public/src/client/topic/events.js

View workflow job for this annotation

GitHub Actions / test

Trailing spaces not allowed
'posts.answer': toggleAnswered,
'posts.unanswer': toggleAnswered,

'posts.upvote': togglePostVote,
'posts.downvote': togglePostVote,
Expand Down Expand Up @@ -229,6 +232,10 @@ define('forum/topic/events', [
el.find('[component="post/bookmark/off"]').toggleClass('hidden', data.isBookmarked);
}

function toggleAnswered() {
console.log('Toggling answered field.');
}

function togglePostVote(data) {
const post = $('[data-pid="' + data.post.pid + '"]');
post.find('[component="post/upvote"]').filter(function (index, el) {
Expand Down
23 changes: 12 additions & 11 deletions public/src/modules/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,19 @@ define('chat', [
}
if (module.modalExists(roomId)) {
loadAndCenter(module.getModal(roomId));
} else {
api.get(`/chats/${roomId}`, {
uid: uid || app.user.uid,
}).then((roomData) => {
roomData.users = roomData.users.filter(function (user) {
return user && parseInt(user.uid, 10) !== parseInt(app.user.uid, 10);
});
roomData.uid = uid || app.user.uid;
roomData.isSelf = true;
module.createModal(roomData, loadAndCenter);
}).catch(alerts.error);
return;
}

api.get(`/chats/${roomId}`, {
uid: uid || app.user.uid,
}).then((roomData) => {
roomData.users = roomData.users.filter(function (user) {
return user && parseInt(user.uid, 10) !== parseInt(app.user.uid, 10);
});
roomData.uid = uid || app.user.uid;
roomData.isSelf = true;
module.createModal(roomData, loadAndCenter);
}).catch(alerts.error);
});
};

Expand Down
8 changes: 8 additions & 0 deletions src/api/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ postsAPI.edit = async function (caller, data) {
return returnData;
};

postsAPI.answer = async function (caller, data) {
return await apiHelpers.postCommand(caller, 'answer', 'answered', '', data);
};

postsAPI.unanswer = async function (caller, data) {
return await apiHelpers.postCommand(caller, 'unanswer', 'answered', '', data);
};

postsAPI.delete = async function (caller, data) {
await deleteOrRestore(caller, data, {
command: 'delete',
Expand Down
11 changes: 11 additions & 0 deletions src/controllers/write/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,17 @@ Posts.verify = async (req, res) => {
Posts.unverify = async (req, res) => {
const data = await mock(req);
await api.posts.unverify(req, data);

Posts.answer = async (req, res) => {
const data = await mock(req);
await api.posts.answer(req, data);
helpers.formatApiResponse(200, res);
};

Posts.unanswer = async (req, res) => {
const data = await mock(req);
await api.posts.unanswer(req, data);

helpers.formatApiResponse(200, res);
};

Expand Down
27 changes: 27 additions & 0 deletions src/posts/answer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

const plugins = require('../plugins');

module.exports = function (Posts) {
Posts.answer = async function (pid, uid) {
return await toggleAnswered('answer', pid, uid);
};

Posts.unanswer = async function (pid, uid) {
return await toggleAnswered('unanswer', pid, uid);
};

async function toggleAnswered(type, pid, uid) {
const isAnswering = type === 'answer';
await plugins.hooks.fire(`filter:post.${type}`, { pid: pid, uid: uid });

await Posts.setPostFields(pid, {
answered: isAnswering ? 1 : 0,
answererUid: isAnswering ? uid : 0,
});

const postData = await Posts.getPostFields(pid, ['pid', 'tid', 'uid', 'content', 'timestamp']);

return postData;
}
};
1 change: 1 addition & 0 deletions src/posts/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const intFields = [
'uid', 'pid', 'tid', 'deleted', 'timestamp',
'upvotes', 'downvotes', 'deleterUid', 'edited',
'replies', 'bookmarks', 'verify', 'verifyUid',
'answered', 'answererUid',

Check failure on line 11 in src/posts/data.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 2 spaces
];

module.exports = function (Posts) {
Expand Down
1 change: 1 addition & 0 deletions src/posts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require('./recent')(Posts);
require('./tools')(Posts);
require('./votes')(Posts);
require('./bookmarks')(Posts);
require('./answer')(Posts);
require('./queue')(Posts);
require('./verify')(Posts);
require('./diffs')(Posts);
Expand Down
3 changes: 2 additions & 1 deletion src/posts/summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ module.exports = function (Posts) {
options.parse = options.hasOwnProperty('parse') ? options.parse : true;
options.extraFields = options.hasOwnProperty('extraFields') ? options.extraFields : [];

const fields = ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted', 'upvotes', 'downvotes', 'replies', 'handle', 'verify'].concat(options.extraFields);

const fields = ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted', 'upvotes', 'downvotes', 'replies', 'handle', 'answered', 'verify'].concat(options.extraFields);

let posts = await Posts.getPostsFields(pids, fields);
posts = posts.filter(Boolean);
Expand Down
21 changes: 12 additions & 9 deletions src/socket.io/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,17 @@ function deserializePayload(payload) {
return { params, callback };
}

async function callMethod(methodToCall, socket, params, callback) {
if (methodToCall.constructor && methodToCall.constructor.name === 'AsyncFunction') {
const result = await methodToCall(socket, params);
callback(null, result);
} else {
methodToCall(socket, params, (err, result) => {
callback(err ? { message: err.message } : null, result);
});
}
}

async function onMessage(socket, payload) {
const { event, params, callback } = payload;
try {
Expand Down Expand Up @@ -202,15 +213,7 @@ async function onMessage(socket, payload) {
if (Namespaces[namespace].before) {
await Namespaces[namespace].before(socket, event, params);
}

if (methodToCall.constructor && methodToCall.constructor.name === 'AsyncFunction') {
const result = await methodToCall(socket, params);
callback(null, result);
} else {
methodToCall(socket, params, (err, result) => {
callback(err ? { message: err.message } : null, result);
});
}
await callMethod(methodToCall, socket, params, callback);
} catch (err) {
winston.debug(`${event}\n${err.stack ? err.stack : err.message}`);
callback({ message: err.message });
Expand Down
5 changes: 3 additions & 2 deletions src/user/uploads.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ module.exports = function (User) {

// Dissociate the upload from pids, if any
const pids = await db.getSortedSetsMembers(uploadNames.map(relativePath => `upload:${md5(relativePath)}:pids`));
await Promise.all(pids.map(async (pids, idx) => Promise.all(
const dissociatePids = (pids, idx) => Promise.all(
pids.map(async pid => posts.uploads.dissociate(pid, uploadNames[idx]))
)));
);
await Promise.all(pids.map(async (pids, idx) => dissociatePids(pids, idx)));
}, { batch: 50 });
};

Expand Down
15 changes: 12 additions & 3 deletions test/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,20 @@ describe('Post\'s', () => {
assert.equal(hasBookmarked[0], false);
});
});

describe('answering', async () => {

Check failure on line 283 in test/posts.js

View workflow job for this annotation

GitHub Actions / test

Trailing spaces not allowed
describe('verifying', async () => {

Check failure on line 284 in test/posts.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 2 spaces
it('should mark post verified', async () => {
await apiPosts.verify({ uid: voterUid }, { pid: postData.pid, room_id: `topic_${postData.tid}` });
const isAnswered = await posts.getPostField(postData.pid, 'verify');
const isVerified = await posts.getPostField(postData.pid, 'verify');
assert.strictEqual(isVerified, 1);

Check failure on line 288 in test/posts.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 3 tabs but found 6 spaces
});

Check failure on line 289 in test/posts.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 tabs but found 6 spaces
});

describe('answering', async () => {
it('should mark post answered', async () => {
const data = await apiPosts.answer({ uid: voterUid }, { pid: postData.pid, room_id: `topic_${postData.tid}` });
const isAnswered = await posts.getPostField(postData.pid, 'answered');

assert.strictEqual(isAnswered, 1);
});
});
Expand Down

0 comments on commit 2a447be

Please sign in to comment.