feat: add tracking categories and make watching send notifications (#12147)

* feat: add tracking categories and make watching send notifications

upgrade script to change the defaults

* add missing spec

* test: one more spec
This commit is contained in:
Barış Soner Uşaklı
2023-11-03 12:49:17 -04:00
committed by GitHub
parent f8cc8548bb
commit 84fed97b41
22 changed files with 131 additions and 17 deletions

View File

@@ -17,7 +17,7 @@ searchApi.categories = async (caller, data) => {
let cids = [];
let matchedCids = [];
const privilege = data.privilege || 'topics:read';
data.states = (data.states || ['watching', 'notwatching', 'ignoring']).map(
data.states = (data.states || ['watching', 'tracking', 'notwatching', 'ignoring']).map(
state => categories.watchStates[state]
);
data.parentCid = parseInt(data.parentCid || 0, 10);

View File

@@ -53,6 +53,7 @@ Categories.getCategoryById = async function (data) {
category.nextStart = topics.nextStart;
category.topic_count = topicCount;
category.isWatched = watchState[0] === Categories.watchStates.watching;
category.isTracked = watchState[0] === Categories.watchStates.tracking;
category.isNotWatched = watchState[0] === Categories.watchStates.notwatching;
category.isIgnored = watchState[0] === Categories.watchStates.ignoring;
category.parent = parent;

View File

@@ -6,6 +6,9 @@ const plugins = require('../plugins');
const meta = require('../meta');
const privileges = require('../privileges');
const user = require('../user');
const notifications = require('../notifications');
const translator = require('../translator');
const batch = require('../batch');
module.exports = function (Categories) {
Categories.getCategoryTopics = async function (data) {
@@ -203,4 +206,41 @@ module.exports = function (Categories) {
const now = Date.now();
return tids.filter((tid, index) => tid && (!scores[index] || scores[index] <= now));
}
Categories.notifyCategoryFollowers = async (postData, exceptUid) => {
const { cid } = postData.topic;
const followers = [];
await batch.processSortedSet(`cid:${cid}:uid:watch:state`, async (uids) => {
followers.push(
...await privileges.categories.filterUids('topics:read', cid, uids)
);
}, {
batch: 500,
min: Categories.watchStates.watching,
max: Categories.watchStates.watching,
});
if (!followers.length) {
return;
}
const { displayname } = postData.user;
const categoryName = await Categories.getCategoryField(cid, 'name');
const notifBase = 'notifications:user-posted-topic-in-category';
const bodyShort = translator.compile(notifBase, displayname, categoryName);
const notification = await notifications.create({
type: 'new-topic-in-category',
nid: `new_topic:tid:${postData.topic.tid}:uid:${exceptUid}`,
subject: bodyShort,
bodyShort: bodyShort,
bodyLong: postData.content,
pid: postData.pid,
path: `/post/${postData.pid}`,
tid: postData.topic.tid,
from: exceptUid,
});
notifications.push(notification, followers);
};
};

View File

@@ -7,7 +7,8 @@ module.exports = function (Categories) {
Categories.watchStates = {
ignoring: 1,
notwatching: 2,
watching: 3,
tracking: 3,
watching: 4,
};
Categories.isIgnored = async function (cids, uid) {

View File

@@ -24,9 +24,10 @@ categoriesController.get = async function (req, res) {
categoriesData.forEach((category) => {
if (category) {
category.isIgnored = states[category.cid] === categories.watchStates.ignoring;
category.isWatched = states[category.cid] === categories.watchStates.watching;
category.isTracked = states[category.cid] === categories.watchStates.tracking;
category.isNotWatched = states[category.cid] === categories.watchStates.notwatching;
category.isIgnored = states[category.cid] === categories.watchStates.ignoring;
}
});

View File

@@ -13,6 +13,7 @@ notificationsController.get = async function (req, res, next) {
{ name: '[[global:topics]]', filter: 'new-topic' },
{ name: '[[notifications:replies]]', filter: 'new-reply' },
{ name: '[[notifications:tags]]', filter: 'new-topic-with-tag' },
{ name: '[[notifications:categories]]', filter: 'new-topic-in-category' },
{ name: '[[notifications:chat]]', filter: 'new-chat' },
{ name: '[[notifications:group-chat]]', filter: 'new-group-chat' },
{ name: '[[notifications:public-chat]]', filter: 'new-public-chat' },

View File

@@ -31,6 +31,7 @@ Notifications.baseTypes = [
'notificationType_upvote',
'notificationType_new-topic',
'notificationType_new-topic-with-tag',
'notificationType_new-topic-in-category',
'notificationType_new-reply',
'notificationType_post-edit',
'notificationType_follow',

View File

@@ -153,6 +153,7 @@ module.exports = function (Topics) {
if (parseInt(uid, 10) && !topicData.scheduled) {
user.notifications.sendTopicNotificationToFollowers(uid, topicData, postData);
Topics.notifyTagFollowers(postData, uid);
categories.notifyCategoryFollowers(postData, uid);
}
return {

View File

@@ -154,7 +154,8 @@ module.exports = function (Topics) {
(!filterCids || filterCids.includes(topic.cid)) &&
(!filterTags || filterTags.every(tag => topic.tags.find(topicTag => topicTag.value === tag))) &&
!blockedUids.includes(topic.uid)) {
if (isTopicsFollowed[topic.tid] || userCidState[topic.cid] === categories.watchStates.watching) {
if (isTopicsFollowed[topic.tid] ||
[categories.watchStates.watching, categories.watchStates.tracking].includes(userCidState[topic.cid])) {
tidsByFilter[''].push(topic.tid);
}
@@ -192,11 +193,22 @@ module.exports = function (Topics) {
if (params.filter === 'watched') {
return [];
}
const cids = params.cid || await user.getWatchedCategories(params.uid);
const cids = params.cid || await getWatchedTrackedCids(params.uid);
const keys = cids.map(cid => `cid:${cid}:tids:lastposttime`);
return await db.getSortedSetRevRangeByScoreWithScores(keys, 0, -1, '+inf', params.cutoff);
}
async function getWatchedTrackedCids(uid) {
if (!(parseInt(uid, 10) > 0)) {
return [];
}
const cids = await user.getCategoriesByStates(uid, [
categories.watchStates.watching, categories.watchStates.tracking,
]);
const categoryData = await categories.getCategoriesFields(cids, ['disabled']);
return cids.filter((cid, index) => categoryData[index] && !categoryData[index].disabled);
}
async function getFollowedTids(params) {
let tids = await db.getSortedSetMembers(`uid:${params.uid}:followed_tids`);
const filterCids = params.cid && params.cid.map(cid => parseInt(cid, 10));

View File

@@ -0,0 +1,32 @@
/* eslint-disable no-await-in-loop */
'use strict';
const db = require('../../database');
const user = require('../../user');
const batch = require('../../batch');
module.exports = {
name: 'Add tracking category state',
timestamp: Date.UTC(2023, 10, 3),
method: async function () {
const { progress } = this;
const current = await db.getObjectField('config', 'categoryWatchState');
if (current === 'watching') {
await db.setObjectField('config', 'categoryWatchState', 'tracking');
}
await batch.processSortedSet(`users:joindate`, async (uids) => {
const userSettings = await user.getMultipleUserSettings(uids);
const change = userSettings.filter(s => s && s.categoryWatchState === 'watching');
await db.setObjectBulk(
change.map(s => [`user:${s.uid}:settings`, { categoryWatchState: 'tracking' }])
);
progress.incr(uids.length);
}, {
batch: 500,
progress,
});
},
};

View File

@@ -60,10 +60,10 @@ module.exports = function (User) {
};
User.getCategoriesByStates = async function (uid, states) {
if (!(parseInt(uid, 10) > 0)) {
return await categories.getAllCidsFromSet('categories:cid');
}
const cids = await categories.getAllCidsFromSet('categories:cid');
if (!(parseInt(uid, 10) > 0)) {
return cids;
}
const userState = await categories.getWatchState(cids, uid);
return cids.filter((cid, index) => states.includes(userState[index]));
};

View File

@@ -297,7 +297,7 @@
<div class="mb-3">
<label class="form-label" for="categoryWatchState">[[admin/settings/user:categoryWatchState]]</label>
<select id="categoryWatchState" class="form-select" data-field="categoryWatchState">
<option value="watching">[[admin/settings/user:categoryWatchState.watching]]</option>
<option value="tracking">[[admin/settings/user:categoryWatchState.watching]]</option>
<option value="notwatching">[[admin/settings/user:categoryWatchState.notwatching]]</option>
<option value="ignoring">[[admin/settings/user:categoryWatchState.ignoring]]</option>
</select>