feat: add downvoteVisibility setting, closes #12698

This commit is contained in:
Barış Soner Uşaklı
2024-07-17 17:43:31 -04:00
parent cb4bf9dd53
commit 269fc06835
10 changed files with 79 additions and 24 deletions

View File

@@ -137,7 +137,8 @@
"sitemapTopics": 500,
"maintenanceMode": 0,
"maintenanceModeStatus": 503,
"voteVisibility": "privileged",
"upvoteVisibility": "all",
"downvoteVisibility": "privileged",
"maximumInvites": 0,
"username:disableEdit": 0,
"email:disableEdit": 0,

View File

@@ -2,10 +2,14 @@
"reputation": "Reputation Settings",
"disable": "Disable Reputation System",
"disable-down-voting": "Disable Down Voting",
"vote-visibility": "Vote visibility",
"vote-visibility-all": "Everyone can see votes",
"vote-visibility-loggedin": "Only logged in users can see votes",
"vote-visibility-privileged": "Only privileged users like admins & moderators can see votes",
"upvote-visibility": "Up Vote visibility",
"upvote-visibility-all": "Everyone can see up votes",
"upvote-visibility-loggedin": "Only logged in users can see up votes",
"upvote-visibility-privileged": "Only privileged users like admins & moderators can see up votes",
"downvote-visibility": "Down Vote visibility",
"downvote-visibility-all": "Everyone can see down votes",
"downvote-visibility-loggedin": "Only logged in users can see down votes",
"downvote-visibility-privileged": "Only privileged users like admins & moderators can see down votes",
"thresholds": "Activity Thresholds",
"min-rep-upvote": "Minimum reputation to upvote posts",
"upvotes-per-day": "Upvotes per day (set to 0 for unlimited upvotes)",

View File

@@ -382,7 +382,9 @@ get:
type: number
downvote:disabled:
type: number
voteVisibility:
upvoteVisibility:
type: string
downvoteVisibility:
type: string
feeds:disableRSS:
type: number

View File

@@ -28,6 +28,8 @@ get:
type: number
downvoteCount:
type: number
showUpvotes:
type: boolean
showDownvotes:
type: boolean
upvoters:

View File

@@ -9,17 +9,24 @@ define('forum/topic/votes', [
Votes.addVoteHandler = function () {
_showTooltip = {};
if (canSeeVotes()) {
if (canSeeUpVotes()) {
components.get('topic').on('mouseenter', '[data-pid] [component="post/vote-count"]', loadDataAndCreateTooltip);
components.get('topic').on('mouseleave', '[data-pid] [component="post/vote-count"]', destroyTooltip);
}
};
function canSeeVotes() {
const { voteVisibility, privileges } = ajaxify.data;
function canSeeUpVotes() {
const { upvoteVisibility, privileges } = ajaxify.data;
return privileges.isAdminOrMod ||
voteVisibility === 'all' ||
(voteVisibility === 'loggedin' && config.loggedIn);
upvoteVisibility === 'all' ||
(upvoteVisibility === 'loggedin' && config.loggedIn);
}
function canSeeVotes() {
const { upvoteVisibility, downvoteVisibility, privileges } = ajaxify.data;
return privileges.isAdminOrMod ||
upvoteVisibility === 'all' || downvoteVisibility === 'all' ||
((upvoteVisibility === 'loggedin' || downvoteVisibility === 'loggedin') && config.loggedIn);
}
function destroyTooltip() {

View File

@@ -314,12 +314,19 @@ postsAPI.getVoters = async function (caller, data) {
}
const { pid } = data;
const cid = await posts.getCidByPid(pid);
if (!await canSeeVotes(caller.uid, cid)) {
const [canSeeUpvotes, canSeeDownvotes] = await Promise.all([
canSeeVotes(caller.uid, cid, 'upvoteVisibility'),
canSeeVotes(caller.uid, cid, 'downvoteVisibility'),
]);
if (!canSeeUpvotes && !canSeeDownvotes) {
throw new Error('[[error:no-privileges]]');
}
const showDownvotes = !meta.config['downvote:disabled'];
const repSystemDisabled = meta.config['reputation:disabled'];
const showUpvotes = canSeeUpvotes && !repSystemDisabled;
const showDownvotes = canSeeDownvotes && !meta.config['downvote:disabled'] && !repSystemDisabled;
const [upvoteUids, downvoteUids] = await Promise.all([
db.getSetMembers(`pid:${data.pid}:upvote`),
showUpvotes ? db.getSetMembers(`pid:${data.pid}:upvote`) : [],
showDownvotes ? db.getSetMembers(`pid:${data.pid}:downvote`) : [],
]);
@@ -331,6 +338,7 @@ postsAPI.getVoters = async function (caller, data) {
return {
upvoteCount: upvoters.length,
downvoteCount: downvoters.length,
showUpvotes: showUpvotes,
showDownvotes: showDownvotes,
upvoters: upvoters,
downvoters: downvoters,
@@ -343,7 +351,7 @@ postsAPI.getUpvoters = async function (caller, data) {
}
const { pid } = data;
const cid = await posts.getCidByPid(pid);
if (!await canSeeVotes(caller.uid, cid)) {
if (!await canSeeVotes(caller.uid, cid, 'upvoteVisibility')) {
throw new Error('[[error:no-privileges]]');
}
@@ -370,7 +378,7 @@ postsAPI.getUpvoters = async function (caller, data) {
};
};
async function canSeeVotes(uid, cids) {
async function canSeeVotes(uid, cids, type) {
const isArray = Array.isArray(cids);
if (!isArray) {
cids = [cids];
@@ -389,8 +397,8 @@ async function canSeeVotes(uid, cids) {
(
cidToAllowed[cid] &&
(
meta.config.voteVisibility === 'all' ||
(meta.config.voteVisibility === 'loggedin' && parseInt(uid, 10) > 0)
meta.config[type] === 'all' ||
(meta.config[type] === 'loggedin' && parseInt(uid, 10) > 0)
)
)
);

View File

@@ -97,7 +97,8 @@ topicsController.get = async function getTopic(req, res, next) {
topicData.topicStaleDays = meta.config.topicStaleDays;
topicData['reputation:disabled'] = meta.config['reputation:disabled'];
topicData['downvote:disabled'] = meta.config['downvote:disabled'];
topicData.voteVisibility = meta.config.voteVisibility;
topicData.upvoteVisibility = meta.config.upvoteVisibility;
topicData.downvoteVisibility = meta.config.downvoteVisibility;
topicData['feeds:disableRSS'] = meta.config['feeds:disableRSS'] || 0;
topicData['signatures:hideDuplicates'] = meta.config['signatures:hideDuplicates'];
topicData.bookmarkThreshold = meta.config.bookmarkThreshold;

View File

@@ -0,0 +1,20 @@
/* eslint-disable no-await-in-loop */
'use strict';
const db = require('../../database');
module.exports = {
name: 'Add downvote visibility config field',
timestamp: Date.UTC(2024, 6, 17),
method: async function () {
const current = await db.getObjectField('config', 'voteVisibility');
if (current) {
await db.setObject('config', {
upvoteVisibility: current,
downvoteVisibility: current,
});
await db.deleteObjectField('config', 'voteVisibility');
}
},
};

View File

@@ -14,12 +14,20 @@
<input type="checkbox" class="form-check-input" id="downvote:disabled" data-field="downvote:disabled">
<label for="downvote:disabled" class="form-check-label">[[admin/settings/reputation:disable-down-voting]]</label>
</div>
<div class="mb-3">
<label for="upvoteVisibility" class="form-check-label">[[admin/settings/reputation:upvote-visibility]]</label>
<select id="upvoteVisibility" data-field="upvoteVisibility" class="form-select">
<option value="all">[[admin/settings/reputation:upvote-visibility-all]]</option>
<option value="loggedin">[[admin/settings/reputation:upvote-visibility-loggedin]]</option>
<option value="privileged">[[admin/settings/reputation:upvote-visibility-privileged]]</option>
</select>
</div>
<div>
<label for="voteVisibility" class="form-check-label">[[admin/settings/reputation:vote-visibility]]</label>
<select id="voteVisibility" data-field="voteVisibility" class="form-select">
<option value="all">[[admin/settings/reputation:vote-visibility-all]]</option>
<option value="loggedin">[[admin/settings/reputation:vote-visibility-loggedin]]</option>
<option value="privileged">[[admin/settings/reputation:vote-visibility-privileged]]</option>
<label for="downvoteVisibility" class="form-check-label">[[admin/settings/reputation:downvote-visibility]]</label>
<select id="downvoteVisibility" data-field="downvoteVisibility" class="form-select">
<option value="all">[[admin/settings/reputation:downvote-visibility-all]]</option>
<option value="loggedin">[[admin/settings/reputation:downvote-visibility-loggedin]]</option>
<option value="privileged">[[admin/settings/reputation:downvote-visibility-privileged]]</option>
</select>
</div>
</div>

View File

@@ -1,9 +1,11 @@
{{{ if showUpvotes }}}
<div class="mb-3">
<h4>[[global:upvoters]] <small>({upvoteCount})</small></h4>
{{{ each upvoters }}}
<a class="text-decoration-none" href="{config.relative_path}/user/{./userslug}">{buildAvatar(@value, "24px", true)}</a>
{{{ end }}}
</div>
{{{ end }}}
{{{ if showDownvotes }}}
<div>
<h4>[[global:downvoters]] <small>({downvoteCount})</small></h4>