Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Category tags #8938

Merged
merged 10 commits into from Dec 6, 2020
7 changes: 7 additions & 0 deletions src/categories/delete.js
Expand Up @@ -28,6 +28,7 @@ module.exports = function (Categories) {
async function purgeCategory(cid) {
await db.sortedSetRemove('categories:cid', cid);
await removeFromParent(cid);
await deleteTags(cid);
await db.deleteAll([
'cid:' + cid + ':tids',
'cid:' + cid + ':tids:pinned',
Expand Down Expand Up @@ -71,4 +72,10 @@ module.exports = function (Categories) {
'cid:' + cid + ':tag:whitelist',
]);
}

async function deleteTags(cid) {
const tags = await db.getSortedSetMembers('cid:' + cid + ':tags');
await db.deleteAll(tags.map(tag => 'cid:' + cid + ':tag:' + tag + ':topics'));
await db.delete('cid:' + cid + ':tags');
}
};
13 changes: 9 additions & 4 deletions src/controllers/tags.js
Expand Up @@ -24,13 +24,17 @@ tagsController.getTag = async function (req, res) {
breadcrumbs: helpers.buildBreadcrumbs([{ text: '[[tags:tags]]', url: '/tags' }, { text: tag }]),
title: '[[pages:tag, ' + tag + ']]',
};
const settings = await user.getSettings(req.uid);
const [settings, cids] = await Promise.all([
user.getSettings(req.uid),
categories.getCidsByPrivilege('categories:cid', req.uid, 'topics:read'),
]);
const start = Math.max(0, (page - 1) * settings.topicsPerPage);
const stop = start + settings.topicsPerPage - 1;
const states = [categories.watchStates.watching, categories.watchStates.notwatching, categories.watchStates.ignoring];

const [topicCount, tids, categoriesData] = await Promise.all([
topics.getTagTopicCount(tag),
topics.getTagTids(tag, start, stop),
topics.getTagTopicCount(tag, cids),
topics.getTagTidsByCids(tag, cids, start, stop),
helpers.getCategoriesByStates(req.uid, '', states),
]);

Expand Down Expand Up @@ -59,9 +63,10 @@ tagsController.getTag = async function (req, res) {
};

tagsController.getTags = async function (req, res) {
const cids = await categories.getCidsByPrivilege('categories:cid', req.uid, 'topics:read');
const [canSearch, tags] = await Promise.all([
privileges.global.can('search:tags', req.uid),
topics.getTags(0, 99),
topics.getCategoryTagsData(cids, 0, 99),
]);

res.render('tags', {
Expand Down
24 changes: 20 additions & 4 deletions src/socket.io/topics/tags.js
Expand Up @@ -16,11 +16,20 @@ module.exports = function (SocketTopics) {
};

SocketTopics.autocompleteTags = async function (socket, data) {
return await topics.autocompleteTags(data);
if (data.cid) {
const canRead = await privileges.categories.can('topics:read', data.cid, socket.uid);
if (!canRead) {
throw new Error('[[error:no-privileges]]');
}
}
data.cids = await categories.getCidsByPrivilege('categories:cid', socket.uid, 'topics:read');
const result = await topics.autocompleteTags(data);
return result.map(tag => tag.value);
};

SocketTopics.searchTags = async function (socket, data) {
return await searchTags(socket.uid, topics.searchTags, data);
const result = await searchTags(socket.uid, topics.searchTags, data);
return result.map(tag => tag.value);
};

SocketTopics.searchAndLoadTags = async function (socket, data) {
Expand All @@ -32,6 +41,13 @@ module.exports = function (SocketTopics) {
if (!allowed) {
throw new Error('[[error:no-privileges]]');
}
if (data.cid) {
const canRead = await privileges.categories.can('topics:read', data.cid, uid);
if (!canRead) {
throw new Error('[[error:no-privileges]]');
}
}
data.cids = await categories.getCidsByPrivilege('categories:cid', uid, 'topics:read');
return await method(data);
}

Expand All @@ -42,8 +58,8 @@ module.exports = function (SocketTopics) {

const start = parseInt(data.after, 10);
const stop = start + 99;
const tags = await topics.getTags(start, stop);

const cids = await categories.getCidsByPrivilege('categories:cid', socket.uid, 'topics:read');
const tags = await topics.getCategoryTagsData(cids, start, stop);
return { tags: tags.filter(Boolean), nextStart: stop + 1 };
};
};