mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
refactor(socket.io): deprecate categories.loadMore in favour of api.categories.getTopics
This commit is contained in:
@@ -118,6 +118,8 @@ paths:
|
|||||||
$ref: 'write/categories/cid/posts.yaml'
|
$ref: 'write/categories/cid/posts.yaml'
|
||||||
/categories/{cid}/children:
|
/categories/{cid}/children:
|
||||||
$ref: 'write/categories/cid/children.yaml'
|
$ref: 'write/categories/cid/children.yaml'
|
||||||
|
/categories/{cid}/topics:
|
||||||
|
$ref: 'write/categories/cid/topics.yaml'
|
||||||
/categories/{cid}/watch:
|
/categories/{cid}/watch:
|
||||||
$ref: 'write/categories/cid/watch.yaml'
|
$ref: 'write/categories/cid/watch.yaml'
|
||||||
/categories/{cid}/privileges:
|
/categories/{cid}/privileges:
|
||||||
|
|||||||
75
public/openapi/write/categories/cid/topics.yaml
Normal file
75
public/openapi/write/categories/cid/topics.yaml
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
get:
|
||||||
|
tags:
|
||||||
|
- categories
|
||||||
|
summary: get topics
|
||||||
|
description: |
|
||||||
|
This operation returns a set of topics in the requested category.
|
||||||
|
|
||||||
|
The number of topics returned is defined by the "Topics per Page" (`topicsPerPage`) setting under ACP > Settings > Pagination.
|
||||||
|
parameters:
|
||||||
|
- in: path
|
||||||
|
name: cid
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
|
required: true
|
||||||
|
description: a valid category id, `0` for global privileges, `admin` for admin privileges
|
||||||
|
example: 1
|
||||||
|
- in: query
|
||||||
|
name: 'query'
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
|
required: false
|
||||||
|
description: Likely unused — a URI-encoded JSON string containing values that are passed to `getCategoryTopics`.
|
||||||
|
example: ''
|
||||||
|
- in: query
|
||||||
|
name: 'after'
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
|
required: false
|
||||||
|
description: The index to start at when querying for the next set of topics. This parameter would be more aptly named `start`.
|
||||||
|
example: '0'
|
||||||
|
- in: query
|
||||||
|
name: 'sort'
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
|
required: false
|
||||||
|
description: Likely deprecated — the sorting method of topics (use `categoryTopicSort` instead.)
|
||||||
|
example: ''
|
||||||
|
- in: query
|
||||||
|
name: 'categoryTopicSort'
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
|
required: false
|
||||||
|
description: The sorting method of topics
|
||||||
|
example: 'newest_to_oldest'
|
||||||
|
- in: query
|
||||||
|
name: 'direction'
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
|
required: false
|
||||||
|
description: The sorting of returned results (if you scroll up you want the topics reversed). Set to "-1" for reversed results.
|
||||||
|
example: '1'
|
||||||
|
responses:
|
||||||
|
'200':
|
||||||
|
description: categories topics successfully retrieved
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
status:
|
||||||
|
$ref: ../../../components/schemas/Status.yaml#/Status
|
||||||
|
response:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
topics:
|
||||||
|
type: array
|
||||||
|
items:
|
||||||
|
$ref: ../../../components/schemas/TopicObject.yaml#/TopicObject
|
||||||
|
nextStart:
|
||||||
|
type: number
|
||||||
|
privileges:
|
||||||
|
type: object
|
||||||
|
additionalProperties:
|
||||||
|
type: boolean
|
||||||
|
description: A set of privileges with either true or false
|
||||||
@@ -123,7 +123,7 @@ define('forum/category', [
|
|||||||
|
|
||||||
hooks.fire('action:topics.loading');
|
hooks.fire('action:topics.loading');
|
||||||
const params = utils.params();
|
const params = utils.params();
|
||||||
infinitescroll.loadMore('categories.loadMore', {
|
infinitescroll.loadMore(`/categories/${ajaxify.data.cid}/topics`, {
|
||||||
cid: ajaxify.data.cid,
|
cid: ajaxify.data.cid,
|
||||||
after: after,
|
after: after,
|
||||||
direction: direction,
|
direction: direction,
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
|
||||||
define('forum/infinitescroll', ['hooks', 'alerts'], function (hooks, alerts) {
|
define('forum/infinitescroll', ['hooks', 'alerts', 'api'], function (hooks, alerts, api) {
|
||||||
const scroll = {};
|
const scroll = {};
|
||||||
let callback;
|
let callback;
|
||||||
let previousScrollTop = 0;
|
let previousScrollTop = 0;
|
||||||
@@ -72,7 +72,9 @@ define('forum/infinitescroll', ['hooks', 'alerts'], function (hooks, alerts) {
|
|||||||
const hookData = { method: method, data: data };
|
const hookData = { method: method, data: data };
|
||||||
hooks.fire('action:infinitescroll.loadmore', hookData);
|
hooks.fire('action:infinitescroll.loadmore', hookData);
|
||||||
|
|
||||||
socket.emit(hookData.method, hookData.data, function (err, data) {
|
const call = hookData.method.startsWith('/') ? api.get : socket.emit;
|
||||||
|
|
||||||
|
call(hookData.method, hookData.data, function (err, data) {
|
||||||
if (err) {
|
if (err) {
|
||||||
loadingMore = false;
|
loadingMore = false;
|
||||||
return alerts.error(err);
|
return alerts.error(err);
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
const meta = require('../meta');
|
||||||
const categories = require('../categories');
|
const categories = require('../categories');
|
||||||
const topics = require('../topics');
|
const topics = require('../topics');
|
||||||
const events = require('../events');
|
const events = require('../events');
|
||||||
@@ -106,6 +107,47 @@ categoriesAPI.getChildren = async (caller, { cid, start }) => {
|
|||||||
return { categories: payload };
|
return { categories: payload };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
categoriesAPI.getTopics = async (caller, data) => {
|
||||||
|
data.query = data.query || {};
|
||||||
|
const [userPrivileges, settings, targetUid] = await Promise.all([
|
||||||
|
privileges.categories.get(data.cid, caller.uid),
|
||||||
|
user.getSettings(caller.uid),
|
||||||
|
user.getUidByUserslug(data.query.author),
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!userPrivileges.read) {
|
||||||
|
throw new Error('[[error:no-privileges]]');
|
||||||
|
}
|
||||||
|
|
||||||
|
const infScrollTopicsPerPage = 20;
|
||||||
|
const sort = data.sort || data.categoryTopicSort || meta.config.categoryTopicSort || 'newest_to_oldest';
|
||||||
|
|
||||||
|
let start = Math.max(0, parseInt(data.after || 0, 10));
|
||||||
|
|
||||||
|
if (data.direction === -1) {
|
||||||
|
start -= infScrollTopicsPerPage;
|
||||||
|
}
|
||||||
|
|
||||||
|
let stop = start + infScrollTopicsPerPage - 1;
|
||||||
|
|
||||||
|
start = Math.max(0, start);
|
||||||
|
stop = Math.max(0, stop);
|
||||||
|
const result = await categories.getCategoryTopics({
|
||||||
|
uid: caller.uid,
|
||||||
|
cid: data.cid,
|
||||||
|
start,
|
||||||
|
stop,
|
||||||
|
sort,
|
||||||
|
settings,
|
||||||
|
query: data.query,
|
||||||
|
tag: data.query.tag,
|
||||||
|
targetUid,
|
||||||
|
});
|
||||||
|
categories.modifyTopicsByPrivilege(result.topics, userPrivileges);
|
||||||
|
|
||||||
|
return { ...result, privileges: userPrivileges };
|
||||||
|
};
|
||||||
|
|
||||||
categoriesAPI.setWatchState = async (caller, { cid, state, uid }) => {
|
categoriesAPI.setWatchState = async (caller, { cid, state, uid }) => {
|
||||||
let targetUid = caller.uid;
|
let targetUid = caller.uid;
|
||||||
const cids = Array.isArray(cid) ? cid.map(cid => parseInt(cid, 10)) : [parseInt(cid, 10)];
|
const cids = Array.isArray(cid) ? cid.map(cid => parseInt(cid, 10)) : [parseInt(cid, 10)];
|
||||||
|
|||||||
@@ -51,6 +51,13 @@ Categories.getChildren = async (req, res) => {
|
|||||||
helpers.formatApiResponse(200, res, await api.categories.getChildren(req, { cid, start }));
|
helpers.formatApiResponse(200, res, await api.categories.getChildren(req, { cid, start }));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Categories.getTopics = async (req, res) => {
|
||||||
|
const { cid } = req.params;
|
||||||
|
const result = await api.categories.getTopics(req, { ...req.query, cid });
|
||||||
|
|
||||||
|
helpers.formatApiResponse(200, res, result);
|
||||||
|
};
|
||||||
|
|
||||||
Categories.setWatchState = async (req, res) => {
|
Categories.setWatchState = async (req, res) => {
|
||||||
const { cid } = req.params;
|
const { cid } = req.params;
|
||||||
let { uid, state } = req.body;
|
let { uid, state } = req.body;
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ module.exports = function () {
|
|||||||
setupApiRoute(router, 'get', '/:cid/count', [...middlewares, middleware.assert.category], controllers.write.categories.getTopicCount);
|
setupApiRoute(router, 'get', '/:cid/count', [...middlewares, middleware.assert.category], controllers.write.categories.getTopicCount);
|
||||||
setupApiRoute(router, 'get', '/:cid/posts', [...middlewares, middleware.assert.category], controllers.write.categories.getPosts);
|
setupApiRoute(router, 'get', '/:cid/posts', [...middlewares, middleware.assert.category], controllers.write.categories.getPosts);
|
||||||
setupApiRoute(router, 'get', '/:cid/children', [...middlewares, middleware.assert.category], controllers.write.categories.getChildren);
|
setupApiRoute(router, 'get', '/:cid/children', [...middlewares, middleware.assert.category], controllers.write.categories.getChildren);
|
||||||
|
setupApiRoute(router, 'get', '/:cid/topics', [...middlewares, middleware.assert.category], controllers.write.categories.getTopics);
|
||||||
|
|
||||||
setupApiRoute(router, 'put', '/:cid/watch', [...middlewares, middleware.assert.category], controllers.write.categories.setWatchState);
|
setupApiRoute(router, 'put', '/:cid/watch', [...middlewares, middleware.assert.category], controllers.write.categories.setWatchState);
|
||||||
setupApiRoute(router, 'delete', '/:cid/watch', [...middlewares, middleware.assert.category], controllers.write.categories.setWatchState);
|
setupApiRoute(router, 'delete', '/:cid/watch', [...middlewares, middleware.assert.category], controllers.write.categories.setWatchState);
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const categories = require('../categories');
|
const categories = require('../categories');
|
||||||
const privileges = require('../privileges');
|
|
||||||
const user = require('../user');
|
const user = require('../user');
|
||||||
const topics = require('../topics');
|
const topics = require('../topics');
|
||||||
const api = require('../api');
|
const api = require('../api');
|
||||||
@@ -38,47 +37,15 @@ SocketCategories.loadMore = async function (socket, data) {
|
|||||||
throw new Error('[[error:invalid-data]]');
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
}
|
||||||
data.query = data.query || {};
|
data.query = data.query || {};
|
||||||
const [userPrivileges, settings, targetUid] = await Promise.all([
|
|
||||||
privileges.categories.get(data.cid, socket.uid),
|
|
||||||
user.getSettings(socket.uid),
|
|
||||||
user.getUidByUserslug(data.query.author),
|
|
||||||
]);
|
|
||||||
|
|
||||||
if (!userPrivileges.read) {
|
const result = await api.categories.getTopics(socket, data);
|
||||||
throw new Error('[[error:no-privileges]]');
|
|
||||||
}
|
|
||||||
|
|
||||||
const infScrollTopicsPerPage = 20;
|
// Backwards compatibility — unsure of current usage.
|
||||||
const sort = data.sort || data.categoryTopicSort;
|
|
||||||
|
|
||||||
let start = Math.max(0, parseInt(data.after, 10));
|
|
||||||
|
|
||||||
if (data.direction === -1) {
|
|
||||||
start -= infScrollTopicsPerPage;
|
|
||||||
}
|
|
||||||
|
|
||||||
let stop = start + infScrollTopicsPerPage - 1;
|
|
||||||
|
|
||||||
start = Math.max(0, start);
|
|
||||||
stop = Math.max(0, stop);
|
|
||||||
const result = await categories.getCategoryTopics({
|
|
||||||
uid: socket.uid,
|
|
||||||
cid: data.cid,
|
|
||||||
start: start,
|
|
||||||
stop: stop,
|
|
||||||
sort: sort,
|
|
||||||
settings: settings,
|
|
||||||
query: data.query,
|
|
||||||
tag: data.query.tag,
|
|
||||||
targetUid: targetUid,
|
|
||||||
});
|
|
||||||
categories.modifyTopicsByPrivilege(result.topics, userPrivileges);
|
|
||||||
|
|
||||||
result.privileges = userPrivileges;
|
|
||||||
result.template = {
|
result.template = {
|
||||||
category: true,
|
category: true,
|
||||||
name: 'category',
|
name: 'category',
|
||||||
};
|
};
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user