mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-01-02 22:00:34 +01:00
refactor: topic mark read/unread routes
This commit is contained in:
@@ -142,6 +142,10 @@ paths:
|
|||||||
$ref: 'write/topics/tid/events.yaml'
|
$ref: 'write/topics/tid/events.yaml'
|
||||||
/topics/{tid}/events/{eventId}:
|
/topics/{tid}/events/{eventId}:
|
||||||
$ref: 'write/topics/tid/events/eventId.yaml'
|
$ref: 'write/topics/tid/events/eventId.yaml'
|
||||||
|
/topics/{tid}/read:
|
||||||
|
$ref: 'write/topics/tid/read.yaml'
|
||||||
|
/topics/{tid}/bump:
|
||||||
|
$ref: 'write/topics/tid/bump.yaml'
|
||||||
/posts/{pid}:
|
/posts/{pid}:
|
||||||
$ref: 'write/posts/pid.yaml'
|
$ref: 'write/posts/pid.yaml'
|
||||||
/posts/{pid}/index:
|
/posts/{pid}/index:
|
||||||
|
|||||||
29
public/openapi/write/topics/tid/bump.yaml
Normal file
29
public/openapi/write/topics/tid/bump.yaml
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
put:
|
||||||
|
tags:
|
||||||
|
- topics
|
||||||
|
summary: mark topic unread for all
|
||||||
|
description: |
|
||||||
|
This operation marks a topic as unread for all users.
|
||||||
|
|
||||||
|
**Note**: This is a privileged call and can only be executed by administrators, global moderators, or the moderator for the category of the passed-in topic.
|
||||||
|
parameters:
|
||||||
|
- in: path
|
||||||
|
name: tid
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
|
required: true
|
||||||
|
description: a valid topic id
|
||||||
|
example: 1
|
||||||
|
responses:
|
||||||
|
'200':
|
||||||
|
description: Topic successfully marked unread for all
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
status:
|
||||||
|
$ref: ../../../components/schemas/Status.yaml#/Status
|
||||||
|
response:
|
||||||
|
type: object
|
||||||
|
properties: {}
|
||||||
52
public/openapi/write/topics/tid/read.yaml
Normal file
52
public/openapi/write/topics/tid/read.yaml
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
delete:
|
||||||
|
tags:
|
||||||
|
- topics
|
||||||
|
summary: mark topic unread
|
||||||
|
description: This operation marks a topic as unread for the calling user.
|
||||||
|
parameters:
|
||||||
|
- in: path
|
||||||
|
name: tid
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
|
required: true
|
||||||
|
description: a valid topic id
|
||||||
|
example: 1
|
||||||
|
responses:
|
||||||
|
'200':
|
||||||
|
description: Topic successfully marked unread.
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
status:
|
||||||
|
$ref: ../../../components/schemas/Status.yaml#/Status
|
||||||
|
response:
|
||||||
|
type: object
|
||||||
|
properties: {}
|
||||||
|
put:
|
||||||
|
tags:
|
||||||
|
- topics
|
||||||
|
summary: mark topic read
|
||||||
|
description: This operation marks a topic as read for the calling user.
|
||||||
|
parameters:
|
||||||
|
- in: path
|
||||||
|
name: tid
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
|
required: true
|
||||||
|
description: a valid topic id
|
||||||
|
example: 1
|
||||||
|
responses:
|
||||||
|
'200':
|
||||||
|
description: Topic successfully marked read
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
status:
|
||||||
|
$ref: ../../../components/schemas/Status.yaml#/Status
|
||||||
|
response:
|
||||||
|
type: object
|
||||||
|
properties: {}
|
||||||
@@ -391,7 +391,7 @@ define('forum/topic', [
|
|||||||
currentUrl = newUrl;
|
currentUrl = newUrl;
|
||||||
|
|
||||||
if (index >= elementCount && app.user.uid) {
|
if (index >= elementCount && app.user.uid) {
|
||||||
socket.emit('topics.markAsRead', [ajaxify.data.tid]);
|
api.put(`/topics/${ajaxify.data.tid}/read`);
|
||||||
}
|
}
|
||||||
|
|
||||||
updateUserBookmark(index);
|
updateUserBookmark(index);
|
||||||
|
|||||||
@@ -60,6 +60,28 @@ define('forum/topic/threadTools', [
|
|||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
topicContainer.on('click', '[component="topic/mark-unread"]', function () {
|
||||||
|
topicCommand('del', '/read', undefined, () => {
|
||||||
|
if (app.previousUrl && !app.previousUrl.match('^/topic')) {
|
||||||
|
ajaxify.go(app.previousUrl, function () {
|
||||||
|
handleBack.onBackClicked(true);
|
||||||
|
});
|
||||||
|
} else if (ajaxify.data.category) {
|
||||||
|
ajaxify.go('category/' + ajaxify.data.category.slug, handleBack.onBackClicked);
|
||||||
|
}
|
||||||
|
|
||||||
|
alerts.success('[[topic:mark_unread.success]]');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
topicContainer.on('click', '[component="topic/mark-unread-for-all"]', function () {
|
||||||
|
const btn = $(this);
|
||||||
|
topicCommand('put', '/bump', undefined, () => {
|
||||||
|
alerts.success('[[topic:markAsUnreadForAll.success]]');
|
||||||
|
btn.parents('.thread-tools.open').find('.dropdown-toggle').trigger('click');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
topicContainer.on('click', '[component="topic/event/delete"]', function () {
|
topicContainer.on('click', '[component="topic/event/delete"]', function () {
|
||||||
const eventId = $(this).attr('data-topic-event-id');
|
const eventId = $(this).attr('data-topic-event-id');
|
||||||
const eventEl = $(this).parents('[component="topic/event"]');
|
const eventEl = $(this).parents('[component="topic/event"]');
|
||||||
@@ -74,38 +96,6 @@ define('forum/topic/threadTools', [
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// todo: should also use topicCommand, but no write api call exists for this yet
|
|
||||||
topicContainer.on('click', '[component="topic/mark-unread"]', function () {
|
|
||||||
socket.emit('topics.markUnread', tid, function (err) {
|
|
||||||
if (err) {
|
|
||||||
return alerts.error(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (app.previousUrl && !app.previousUrl.match('^/topic')) {
|
|
||||||
ajaxify.go(app.previousUrl, function () {
|
|
||||||
handleBack.onBackClicked(true);
|
|
||||||
});
|
|
||||||
} else if (ajaxify.data.category) {
|
|
||||||
ajaxify.go('category/' + ajaxify.data.category.slug, handleBack.onBackClicked);
|
|
||||||
}
|
|
||||||
|
|
||||||
alerts.success('[[topic:mark_unread.success]]');
|
|
||||||
});
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
|
|
||||||
topicContainer.on('click', '[component="topic/mark-unread-for-all"]', function () {
|
|
||||||
const btn = $(this);
|
|
||||||
socket.emit('topics.markAsUnreadForAll', [tid], function (err) {
|
|
||||||
if (err) {
|
|
||||||
return alerts.error(err);
|
|
||||||
}
|
|
||||||
alerts.success('[[topic:markAsUnreadForAll.success]]');
|
|
||||||
btn.parents('.thread-tools.open').find('.dropdown-toggle').trigger('click');
|
|
||||||
});
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
|
|
||||||
topicContainer.on('click', '[component="topic/move"]', function () {
|
topicContainer.on('click', '[component="topic/move"]', function () {
|
||||||
require(['forum/topic/move'], function (move) {
|
require(['forum/topic/move'], function (move) {
|
||||||
move.init([tid], ajaxify.data.cid);
|
move.init([tid], ajaxify.data.cid);
|
||||||
|
|||||||
@@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
|
|
||||||
define('forum/unread', [
|
define('forum/unread', [
|
||||||
'forum/header/unread', 'topicSelect', 'components', 'topicList', 'categorySelector', 'alerts',
|
'forum/header/unread', 'topicSelect', 'components', 'topicList', 'categorySelector', 'alerts', 'api',
|
||||||
], function (headerUnread, topicSelect, components, topicList, categorySelector, alerts) {
|
], function (headerUnread, topicSelect, components, topicList, categorySelector, alerts, api) {
|
||||||
const Unread = {};
|
const Unread = {};
|
||||||
|
|
||||||
Unread.init = function () {
|
Unread.init = function () {
|
||||||
@@ -37,11 +37,8 @@ define('forum/unread', [
|
|||||||
if (!tids.length) {
|
if (!tids.length) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
socket.emit('topics.markAsRead', tids, function (err) {
|
|
||||||
if (err) {
|
|
||||||
return alerts.error(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
Promise.all(tids.map(async tid => api.put(`/topics/${tid}/read`))).then(() => {
|
||||||
doneRemovingTids(tids);
|
doneRemovingTids(tids);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -268,3 +268,27 @@ topicsAPI.deleteEvent = async (caller, { tid, eventId }) => {
|
|||||||
|
|
||||||
await topics.events.purge(tid, [eventId]);
|
await topics.events.purge(tid, [eventId]);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
topicsAPI.markRead = async (caller, { tid }) => {
|
||||||
|
const hasMarked = await topics.markAsRead([tid], caller.uid);
|
||||||
|
const promises = [topics.markTopicNotificationsRead([tid], caller.uid)];
|
||||||
|
if (hasMarked) {
|
||||||
|
promises.push(topics.pushUnreadCount(caller.uid));
|
||||||
|
}
|
||||||
|
await Promise.all(promises);
|
||||||
|
};
|
||||||
|
|
||||||
|
topicsAPI.markUnread = async (caller, { tid }) => {
|
||||||
|
await topics.markUnread(tid, caller.uid);
|
||||||
|
topics.pushUnreadCount(caller.uid);
|
||||||
|
};
|
||||||
|
|
||||||
|
topicsAPI.bump = async (caller, { tid }) => {
|
||||||
|
const isAdminOrMod = await privileges.topics.isAdminOrMod(tid, caller.uid);
|
||||||
|
if (!isAdminOrMod) {
|
||||||
|
throw new Error('[[error:no-privileges]]');
|
||||||
|
}
|
||||||
|
|
||||||
|
await topics.markAsUnreadForAll(tid);
|
||||||
|
topics.pushUnreadCount(caller.uid);
|
||||||
|
};
|
||||||
|
|||||||
@@ -189,3 +189,21 @@ Topics.deleteEvent = async (req, res) => {
|
|||||||
|
|
||||||
helpers.formatApiResponse(200, res);
|
helpers.formatApiResponse(200, res);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Topics.markRead = async (req, res) => {
|
||||||
|
await api.topics.markRead(req, { ...req.params });
|
||||||
|
|
||||||
|
helpers.formatApiResponse(200, res);
|
||||||
|
};
|
||||||
|
|
||||||
|
Topics.markUnread = async (req, res) => {
|
||||||
|
await api.topics.markUnread(req, { ...req.params });
|
||||||
|
|
||||||
|
helpers.formatApiResponse(200, res);
|
||||||
|
};
|
||||||
|
|
||||||
|
Topics.bump = async (req, res) => {
|
||||||
|
await api.topics.bump(req, { ...req.params });
|
||||||
|
|
||||||
|
helpers.formatApiResponse(200, res);
|
||||||
|
};
|
||||||
|
|||||||
@@ -45,5 +45,9 @@ module.exports = function () {
|
|||||||
setupApiRoute(router, 'get', '/:tid/events', [middleware.assert.topic], controllers.write.topics.getEvents);
|
setupApiRoute(router, 'get', '/:tid/events', [middleware.assert.topic], controllers.write.topics.getEvents);
|
||||||
setupApiRoute(router, 'delete', '/:tid/events/:eventId', [middleware.assert.topic], controllers.write.topics.deleteEvent);
|
setupApiRoute(router, 'delete', '/:tid/events/:eventId', [middleware.assert.topic], controllers.write.topics.deleteEvent);
|
||||||
|
|
||||||
|
setupApiRoute(router, 'put', '/:tid/read', [...middlewares, middleware.assert.topic], controllers.write.topics.markRead);
|
||||||
|
setupApiRoute(router, 'delete', '/:tid/read', [...middlewares, middleware.assert.topic], controllers.write.topics.markUnread);
|
||||||
|
setupApiRoute(router, 'put', '/:tid/bump', [...middlewares, middleware.assert.topic], controllers.write.topics.bump);
|
||||||
|
|
||||||
return router;
|
return router;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,19 +1,19 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const user = require('../../user');
|
|
||||||
const topics = require('../../topics');
|
const topics = require('../../topics');
|
||||||
|
|
||||||
|
const api = require('../../api');
|
||||||
|
const sockets = require('..');
|
||||||
|
|
||||||
module.exports = function (SocketTopics) {
|
module.exports = function (SocketTopics) {
|
||||||
SocketTopics.markAsRead = async function (socket, tids) {
|
SocketTopics.markAsRead = async function (socket, tids) {
|
||||||
|
sockets.warnDeprecated(socket, 'PUT /api/v3/topics/:tid/read');
|
||||||
|
|
||||||
if (!Array.isArray(tids) || socket.uid <= 0) {
|
if (!Array.isArray(tids) || socket.uid <= 0) {
|
||||||
throw new Error('[[error:invalid-data]]');
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
}
|
||||||
const hasMarked = await topics.markAsRead(tids, socket.uid);
|
|
||||||
const promises = [topics.markTopicNotificationsRead(tids, socket.uid)];
|
await Promise.all(tids.map(async tid => api.topics.markRead(socket, { tid })));
|
||||||
if (hasMarked) {
|
|
||||||
promises.push(topics.pushUnreadCount(socket.uid));
|
|
||||||
}
|
|
||||||
await Promise.all(promises);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketTopics.markTopicNotificationsRead = async function (socket, tids) {
|
SocketTopics.markTopicNotificationsRead = async function (socket, tids) {
|
||||||
@@ -37,14 +37,18 @@ module.exports = function (SocketTopics) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
SocketTopics.markUnread = async function (socket, tid) {
|
SocketTopics.markUnread = async function (socket, tid) {
|
||||||
|
sockets.warnDeprecated(socket, 'DELETE /api/v3/topics/:tid/read');
|
||||||
|
|
||||||
if (!tid || socket.uid <= 0) {
|
if (!tid || socket.uid <= 0) {
|
||||||
throw new Error('[[error:invalid-data]]');
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
}
|
||||||
await topics.markUnread(tid, socket.uid);
|
|
||||||
topics.pushUnreadCount(socket.uid);
|
await api.topics.markUnread(socket, { tid });
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketTopics.markAsUnreadForAll = async function (socket, tids) {
|
SocketTopics.markAsUnreadForAll = async function (socket, tids) {
|
||||||
|
sockets.warnDeprecated(socket, 'PUT /api/v3/topics/:tid/bump');
|
||||||
|
|
||||||
if (!Array.isArray(tids)) {
|
if (!Array.isArray(tids)) {
|
||||||
throw new Error('[[error:invalid-tid]]');
|
throw new Error('[[error:invalid-tid]]');
|
||||||
}
|
}
|
||||||
@@ -52,18 +56,7 @@ module.exports = function (SocketTopics) {
|
|||||||
if (socket.uid <= 0) {
|
if (socket.uid <= 0) {
|
||||||
throw new Error('[[error:no-privileges]]');
|
throw new Error('[[error:no-privileges]]');
|
||||||
}
|
}
|
||||||
const isAdmin = await user.isAdministrator(socket.uid);
|
|
||||||
await Promise.all(tids.map(async (tid) => {
|
await Promise.all(tids.map(async tid => api.topics.bump(socket, { tid })));
|
||||||
const topicData = await topics.getTopicFields(tid, ['tid', 'cid']);
|
|
||||||
if (!topicData.tid) {
|
|
||||||
throw new Error('[[error:no-topic]]');
|
|
||||||
}
|
|
||||||
const isMod = await user.isModerator(socket.uid, topicData.cid);
|
|
||||||
if (!isAdmin && !isMod) {
|
|
||||||
throw new Error('[[error:no-privileges]]');
|
|
||||||
}
|
|
||||||
await topics.markAsUnreadForAll(tid);
|
|
||||||
}));
|
|
||||||
topics.pushUnreadCount(socket.uid);
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1470,29 +1470,18 @@ describe('Topic\'s', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should fail with invalid data', (done) => {
|
it('should fail with invalid data', async () => {
|
||||||
socketTopics.markUnread({ uid: adminUid }, null, (err) => {
|
assert.rejects(apiTopics.markUnread({ uid: adminUid }, null), '[[error:invalid-data]]');
|
||||||
assert.equal(err.message, '[[error:invalid-data]]');
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should fail if topic does not exist', (done) => {
|
it('should fail if topic does not exist', async () => {
|
||||||
socketTopics.markUnread({ uid: adminUid }, 1231082, (err) => {
|
assert.rejects(apiTopics.markUnread({ uid: adminUid }, { tid: 1231082 }), '[[error:no-topic]]');
|
||||||
assert.equal(err.message, '[[error:no-topic]]');
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should mark topic unread', (done) => {
|
it('should mark topic unread', async () => {
|
||||||
socketTopics.markUnread({ uid: adminUid }, tid, (err) => {
|
await apiTopics.markUnread({ uid: adminUid }, { tid });
|
||||||
assert.ifError(err);
|
const hasRead = await topics.hasReadTopic(tid, adminUid);
|
||||||
topics.hasReadTopic(tid, adminUid, (err, hasRead) => {
|
assert.equal(hasRead, false);
|
||||||
assert.ifError(err);
|
|
||||||
assert.equal(hasRead, false);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should fail with invalid data', (done) => {
|
it('should fail with invalid data', (done) => {
|
||||||
@@ -1566,51 +1555,25 @@ describe('Topic\'s', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should fail with invalid data', (done) => {
|
it('should fail with invalid data', async () => {
|
||||||
socketTopics.markAsUnreadForAll({ uid: adminUid }, null, (err) => {
|
assert.rejects(apiTopics.bump({ uid: adminUid }, null, '[[error:invalid-tid]]'));
|
||||||
assert.equal(err.message, '[[error:invalid-tid]]');
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should fail with invalid data', (done) => {
|
it('should fail with invalid data', async () => {
|
||||||
socketTopics.markAsUnreadForAll({ uid: 0 }, [tid], (err) => {
|
assert.rejects(apiTopics.bump({ uid: 0 }, { tid: [tid] }, '[[error:no-privileges]]'));
|
||||||
assert.equal(err.message, '[[error:no-privileges]]');
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should fail if user is not admin', (done) => {
|
it('should fail if user is not admin', async () => {
|
||||||
socketTopics.markAsUnreadForAll({ uid: uid }, [tid], (err) => {
|
assert.rejects(apiTopics.bump({ uid: uid }, { tid }, '[[error:no-privileges]]'));
|
||||||
assert.equal(err.message, '[[error:no-privileges]]');
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should fail if topic does not exist', (done) => {
|
it('should mark topic unread for everyone', async () => {
|
||||||
socketTopics.markAsUnreadForAll({ uid: uid }, [12312313], (err) => {
|
await apiTopics.bump({ uid: adminUid }, { tid });
|
||||||
assert.equal(err.message, '[[error:no-topic]]');
|
const adminRead = await topics.hasReadTopic(tid, adminUid);
|
||||||
done();
|
const regularRead = await topics.hasReadTopic(tid, uid);
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should mark topic unread for everyone', (done) => {
|
assert.equal(adminRead, false);
|
||||||
socketTopics.markAsUnreadForAll({ uid: adminUid }, [tid], (err) => {
|
assert.equal(regularRead, false);
|
||||||
assert.ifError(err);
|
|
||||||
async.parallel({
|
|
||||||
adminRead: function (next) {
|
|
||||||
topics.hasReadTopic(tid, adminUid, next);
|
|
||||||
},
|
|
||||||
regularRead: function (next) {
|
|
||||||
topics.hasReadTopic(tid, uid, next);
|
|
||||||
},
|
|
||||||
}, (err, results) => {
|
|
||||||
assert.ifError(err);
|
|
||||||
assert.equal(results.adminRead, false);
|
|
||||||
assert.equal(results.regularRead, false);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not do anything if tids is empty array', (done) => {
|
it('should not do anything if tids is empty array', (done) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user