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

@@ -79,7 +79,7 @@
"follow-replied-topics": "Follow topics that you reply to",
"default-notification-settings": "Default notification settings",
"categoryWatchState": "Default category watch state",
"categoryWatchState.watching": "Watching",
"categoryWatchState.tracking": "Tracking",
"categoryWatchState.notwatching": "Not Watching",
"categoryWatchState.ignoring": "Ignoring"
}

View File

@@ -13,13 +13,16 @@
"watch": "Watch",
"ignore": "Ignore",
"watching": "Watching",
"tracking": "Tracking",
"not-watching": "Not Watching",
"ignoring": "Ignoring",
"watching.description": "Show topics in unread and recent",
"watching.description": "Notify me of new topics.<br/>Show topics in unread & recent",
"tracking.description": "Shows topics in unread & recent",
"not-watching.description": "Do not show topics in unread, show in recent",
"ignoring.description": "Do not show topics in unread and recent",
"ignoring.description": "Do not show topics in unread & recent",
"watching.message": "You are now watching updates from this category and all subcategories",
"tracking.message": "You are now tracking updates from this category and all subcategories",
"notwatching.message": "You are not watching updates from this category and all subcategories",
"ignoring.message": "You are now ignoring updates from this category and all subcategories",

View File

@@ -15,6 +15,7 @@
"all": "All",
"topics": "Topics",
"tags": "Tags",
"categories": "Categories",
"replies": "Replies",
"chat": "Chats",
"group-chat": "Group Chats",
@@ -61,6 +62,8 @@
"user-posted-topic-with-tag-triple": "<strong>%1</strong> has posted a new topic with tags <strong>%2</strong>, <strong>%3</strong> and <strong>%4</strong>",
"user-posted-topic-with-tag-multiple": "<strong>%1</strong> has posted a new topic with tags <strong>%2</strong>",
"user-posted-topic-in-category": "<strong>%1</strong> has posted a new topic in <strong>%2</strong>",
"user-started-following-you": "<strong>%1</strong> started following you.",
"user-started-following-you-dual": "<strong>%1</strong> and <strong>%2</strong> started following you.",
"user-started-following-you-triple": "<strong>%1</strong>, <strong>%2</strong> and <strong>%3</strong> started following you.",
@@ -89,6 +92,7 @@
"notificationType-upvote": "When someone upvotes your post",
"notificationType-new-topic": "When someone you follow posts a topic",
"notificationType-new-topic-with-tag": "When a topic is posted with a tag you follow",
"notificationType-new-topic-in-category": "When a topic is posted in a category you are watching",
"notificationType-new-reply": "When a new reply is posted in a topic you are watching",
"notificationType-post-edit": "When a post is edited in a topic you are watching",
"notificationType-follow": "When someone starts following you",

View File

@@ -83,6 +83,9 @@ Settings:
notificationType_new-topic-with-tag:
type: string
description: Notification type for new topics with followed tag
notificationType_new-topic-in-category:
type: string
description: Notification type for new topics in watched category
notificationType_follow:
type: string
description: Notification type for another user following you

View File

@@ -52,6 +52,8 @@ get:
type: number
isWatched:
type: boolean
isTracked:
type: boolean
isNotWatched:
type: boolean
isIgnored:

View File

@@ -52,6 +52,8 @@ get:
type: boolean
isWatched:
type: boolean
isTracked:
type: boolean
isNotWatched:
type: boolean
imageClass:

View File

@@ -11,7 +11,9 @@ define('forum/account/categories', ['forum/account/header', 'alerts', 'api'], fu
handleIgnoreWatch(category.cid);
});
$('[component="category/watch/all"]').find('[component="category/watching"], [component="category/ignoring"], [component="category/notwatching"]').on('click', async (e) => {
$('[component="category/watch/all"]').find(
'[component="category/watching"], [component="category/tracking"], [component="category/ignoring"], [component="category/notwatching"]'
).on('click', async (e) => {
const cids = [];
const state = e.currentTarget.getAttribute('data-state');
const { uid } = ajaxify.data;
@@ -30,7 +32,9 @@ define('forum/account/categories', ['forum/account/header', 'alerts', 'api'], fu
function handleIgnoreWatch(cid) {
const category = $('[data-cid="' + cid + '"]');
category.find('[component="category/watching"], [component="category/ignoring"], [component="category/notwatching"]').on('click', async (e) => {
category.find(
'[component="category/watching"], [component="category/tracking"], [component="category/ignoring"], [component="category/notwatching"]'
).on('click', async (e) => {
const state = e.currentTarget.getAttribute('data-state');
const { uid } = ajaxify.data;
@@ -46,6 +50,9 @@ define('forum/account/categories', ['forum/account/header', 'alerts', 'api'], fu
category.find('[component="category/watching/menu"]').toggleClass('hidden', state !== 'watching');
category.find('[component="category/watching/check"]').toggleClass('fa-check', state === 'watching');
category.find('[component="category/tracking/menu"]').toggleClass('hidden', state !== 'tracking');
category.find('[component="category/tracking/check"]').toggleClass('fa-check', state === 'tracking');
category.find('[component="category/notwatching/menu"]').toggleClass('hidden', state !== 'notwatching');
category.find('[component="category/notwatching/check"]').toggleClass('fa-check', state === 'notwatching');

View File

@@ -65,7 +65,7 @@ define('forum/category', [
}
function handleIgnoreWatch(cid) {
$('[component="category/watching"], [component="category/ignoring"], [component="category/notwatching"]').on('click', function () {
$('[component="category/watching"], [component="category/tracking"], [component="category/ignoring"], [component="category/notwatching"]').on('click', function () {
const $this = $(this);
const state = $this.attr('data-state');
@@ -77,6 +77,9 @@ define('forum/category', [
$('[component="category/watching/menu"]').toggleClass('hidden', state !== 'watching');
$('[component="category/watching/check"]').toggleClass('fa-check', state === 'watching');
$('[component="category/tracking/menu"]').toggleClass('hidden', state !== 'tracking');
$('[component="category/tracking/check"]').toggleClass('fa-check', state === 'tracking');
$('[component="category/notwatching/menu"]').toggleClass('hidden', state !== 'notwatching');
$('[component="category/notwatching/check"]').toggleClass('fa-check', state === 'notwatching');

View File

@@ -7,7 +7,7 @@ define('categorySearch', ['alerts', 'bootstrap', 'api'], function (alerts, boots
let categoriesList = null;
options = options || {};
options.privilege = options.privilege || 'topics:read';
options.states = options.states || ['watching', 'notwatching', 'ignoring'];
options.states = options.states || ['watching', 'tracking', 'notwatching', 'ignoring'];
options.cacheList = options.hasOwnProperty('cacheList') ? options.cacheList : true;
let localCategories = [];

View File

@@ -34,7 +34,7 @@ define('topicList', [
categoryTools.init();
TopicList.watchForNewPosts();
const states = ['watching'];
const states = ['watching', 'tracking'];
if (ajaxify.data.selectedFilter && ajaxify.data.selectedFilter.filter === 'watched') {
states.push('notwatching', 'ignoring');
} else if (template !== 'unread') {

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>