Files
NodeBB/src/user/posts.js
Barış Soner Uşaklı cb8d38ee2f Squashed commit of the following:
commit 072a11b89f
Merge: 25ef62dc6b 8fb0ed82b8
Author: Barış Soner Uşaklı <barisusakli@gmail.com>
Date:   Thu Feb 29 13:10:01 2024 -0500

    Merge branch 'develop' of https://github.com/NodeBB/NodeBB into develop

commit 25ef62dc6b
Author: Barış Soner Uşaklı <barisusakli@gmail.com>
Date:   Thu Feb 29 13:09:56 2024 -0500

    chore: up harmony

commit 8fb0ed82b8
Author: Misty Release Bot <deploy@nodebb.org>
Date:   Thu Feb 29 18:08:50 2024 +0000

    chore(i18n): fallback strings for new resources: nodebb.themes-harmony

commit 96bc5d6f84
Author: Barış Soner Uşaklı <barisusakli@gmail.com>
Date:   Thu Feb 29 13:08:25 2024 -0500

    add harmony setting label

commit 516c8d0424
Author: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Date:   Thu Feb 29 12:53:39 2024 -0500

    chore(deps): update commitlint monorepo to v19 (#12369)

    Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

commit a8abc7aea4
Merge: 3aaa072756 aa6859dfae
Author: Barış Soner Uşaklı <barisusakli@gmail.com>
Date:   Thu Feb 29 10:42:42 2024 -0500

    Merge branch 'develop' of https://github.com/NodeBB/NodeBB into develop

commit 3aaa072756
Author: Barış Soner Uşaklı <barisusakli@gmail.com>
Date:   Thu Feb 29 10:42:37 2024 -0500

    chore: up peace

commit aa6859dfae
Author: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Date:   Thu Feb 29 10:42:16 2024 -0500

    fix(deps): update dependency nodemailer to v6.9.11 (#12380)

    Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

commit 2c3540b25c
Author: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Date:   Thu Feb 29 10:42:07 2024 -0500

    fix(deps): update dependency express to v4.18.3 (#12379)

    Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

commit c8a5e24aee
Author: Barış Soner Uşaklı <barisusakli@gmail.com>
Date:   Thu Feb 29 10:39:02 2024 -0500

    test: fix test

commit 5556c1d549
Author: Barış Soner Uşaklı <barisusakli@gmail.com>
Date:   Thu Feb 29 10:29:37 2024 -0500

    add missing btn class

commit fb98dc054d
Author: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Date:   Thu Feb 29 09:43:17 2024 -0500

    fix(deps): update dependency nodebb-plugin-markdown to v12.2.6 (#12371)

    Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

commit 9de0ab3165
Author: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Date:   Thu Feb 29 09:42:13 2024 -0500

    fix(deps): update dependency archiver to v7 (#12374)

    Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

commit 131d2fe78b
Author: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Date:   Thu Feb 29 09:41:56 2024 -0500

    fix(deps): update dependency chart.js to v4.4.2 (#12376)

    Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

commit 0ab1982cb8
Author: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Date:   Thu Feb 29 09:41:37 2024 -0500

    chore(deps): update dependency smtp-server to v3.13.3 (#12378)

    Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

commit 65f3c73493
Author: Barış Soner Uşaklı <barisusakli@gmail.com>
Date:   Thu Feb 29 09:31:17 2024 -0500

    chore: up peace

commit a5101dcbe1
Author: Misty Release Bot <deploy@nodebb.org>
Date:   Thu Feb 29 09:19:05 2024 +0000

    Latest translations and fallbacks

commit 0f11533c23
Author: Barış Soner Uşaklı <barisusakli@gmail.com>
Date:   Wed Feb 28 16:04:25 2024 -0500

    cache page changes

commit 53b3c5d669
Author: Barış Soner Uşaklı <barisusakli@gmail.com>
Date:   Wed Feb 28 11:41:18 2024 -0500

    feat: add chat widget areas, closes #12375

commit 1ed4d76363
Author: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Date:   Wed Feb 28 11:14:20 2024 -0500

    fix(deps): update dependency bootswatch to v5.3.3 (#12373)

    Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

commit ea99401a0d
Merge: f4c651fe25 caf8722fd5
Author: Barış Soner Uşaklı <barisusakli@gmail.com>
Date:   Wed Feb 28 09:19:09 2024 -0500

    Merge branch 'master' into develop

commit caf8722fd5
Author: Misty Release Bot <deploy@nodebb.org>
Date:   Wed Feb 28 14:17:10 2024 +0000

    chore: update changelog for v3.6.7

commit 78b8fab3e7
Author: Misty Release Bot <deploy@nodebb.org>
Date:   Wed Feb 28 14:17:09 2024 +0000

    chore: incrementing version number - v3.6.7

commit f4c651fe25
Author: Misty Release Bot <deploy@nodebb.org>
Date:   Wed Feb 28 09:18:50 2024 +0000

    Latest translations and fallbacks

commit 5eb503440d
Merge: 7207814b90 5f597dc97f
Author: Barış Soner Uşaklı <barisusakli@gmail.com>
Date:   Tue Feb 27 15:22:23 2024 -0500

    Merge branch 'master' into develop

commit 5f597dc97f
Author: Barış Soner Uşaklı <barisusakli@gmail.com>
Date:   Tue Feb 27 15:13:06 2024 -0500

    align center

commit 107f5613bf
Author: Barış Soner Uşaklı <barisusakli@gmail.com>
Date:   Tue Feb 27 15:07:23 2024 -0500

    fix: #12372, fix manual digest buttons

commit 7207814b90
Author: Barış Soner Uşaklı <barisusakli@gmail.com>
Date:   Tue Feb 27 09:20:44 2024 -0500

    feat: add hook into user.posts.isReady

    can be used to disable post delays or apply custom logic
    closes https://github.com/NodeBB/NodeBB/pull/12367

commit 1df37c0153
Author: Misty Release Bot <deploy@nodebb.org>
Date:   Tue Feb 27 09:19:09 2024 +0000

    Latest translations and fallbacks
2024-02-29 15:04:29 -05:00

149 lines
4.6 KiB
JavaScript

'use strict';
const db = require('../database');
const meta = require('../meta');
const privileges = require('../privileges');
const plugins = require('../plugins');
const groups = require('../groups');
module.exports = function (User) {
User.isReadyToPost = async function (uid, cid) {
await isReady(uid, cid, 'lastposttime');
};
User.isReadyToQueue = async function (uid, cid) {
await isReady(uid, cid, 'lastqueuetime');
};
User.checkMuted = async function (uid) {
const now = Date.now();
const mutedUntil = await User.getUserField(uid, 'mutedUntil');
if (mutedUntil > now) {
let muteLeft = ((mutedUntil - now) / (1000 * 60));
if (muteLeft > 60) {
muteLeft = (muteLeft / 60).toFixed(0);
throw new Error(`[[error:user-muted-for-hours, ${muteLeft}]]`);
} else {
throw new Error(`[[error:user-muted-for-minutes, ${muteLeft.toFixed(0)}]]`);
}
}
};
async function isReady(uid, cid, field) {
if (parseInt(uid, 10) === 0) {
return;
}
const [userData, isAdminOrMod, isMemberOfExempt] = await Promise.all([
User.getUserFields(uid, ['uid', 'mutedUntil', 'joindate', 'email', 'reputation'].concat([field])),
privileges.categories.isAdminOrMod(cid, uid),
groups.isMemberOfAny(uid, meta.config.groupsExemptFromNewUserRestrictions),
]);
if (!userData.uid) {
throw new Error('[[error:no-user]]');
}
if (isAdminOrMod) {
return;
}
await User.checkMuted(uid);
const { shouldIgnoreDelays } = await plugins.hooks.fire('filter:user.posts.isReady', {
shouldIgnoreDelays: false,
user: userData,
cid,
field,
isAdminOrMod,
isMemberOfExempt,
});
if (shouldIgnoreDelays) {
return;
}
const now = Date.now();
if (now - userData.joindate < meta.config.initialPostDelay * 1000) {
throw new Error(`[[error:user-too-new, ${meta.config.initialPostDelay}]]`);
}
const lasttime = userData[field] || 0;
if (
!isMemberOfExempt &&
meta.config.newbiePostDelay > 0 &&
meta.config.newbieReputationThreshold > userData.reputation &&
now - lasttime < meta.config.newbiePostDelay * 1000
) {
if (meta.config.newbiewPostDelay % 60 === 0) {
throw new Error(`[[error:too-many-posts-newbie-minutes, ${Math.floor(meta.config.newbiePostDelay / 60)}, ${meta.config.newbieReputationThreshold}]]`);
} else {
throw new Error(`[[error:too-many-posts-newbie, ${meta.config.newbiePostDelay}, ${meta.config.newbieReputationThreshold}]]`);
}
} else if (now - lasttime < meta.config.postDelay * 1000) {
throw new Error(`[[error:too-many-posts, ${meta.config.postDelay}]]`);
}
}
User.onNewPostMade = async function (postData) {
// For scheduled posts, use "action" time. It'll be updated in related cron job when post is published
const lastposttime = postData.timestamp > Date.now() ? Date.now() : postData.timestamp;
await Promise.all([
User.addPostIdToUser(postData),
User.setUserField(postData.uid, 'lastposttime', lastposttime),
User.updateLastOnlineTime(postData.uid),
]);
};
User.addPostIdToUser = async function (postData) {
await db.sortedSetsAdd([
`uid:${postData.uid}:posts`,
`cid:${postData.cid}:uid:${postData.uid}:pids`,
], postData.timestamp, postData.pid);
await User.updatePostCount(postData.uid);
};
User.updatePostCount = async (uids) => {
uids = Array.isArray(uids) ? uids : [uids];
const exists = await User.exists(uids);
uids = uids.filter((uid, index) => exists[index]);
if (uids.length) {
const counts = await db.sortedSetsCard(uids.map(uid => `uid:${uid}:posts`));
await Promise.all([
db.setObjectBulk(uids.map((uid, index) => ([`user:${uid}`, { postcount: counts[index] }]))),
db.sortedSetAdd('users:postcount', counts, uids),
]);
}
};
User.incrementUserPostCountBy = async function (uid, value) {
return await incrementUserFieldAndSetBy(uid, 'postcount', 'users:postcount', value);
};
User.incrementUserReputationBy = async function (uid, value) {
return await incrementUserFieldAndSetBy(uid, 'reputation', 'users:reputation', value);
};
User.incrementUserFlagsBy = async function (uid, value) {
return await incrementUserFieldAndSetBy(uid, 'flags', 'users:flags', value);
};
async function incrementUserFieldAndSetBy(uid, field, set, value) {
value = parseInt(value, 10);
if (!value || !field || !(parseInt(uid, 10) > 0)) {
return;
}
const exists = await User.exists(uid);
if (!exists) {
return;
}
const newValue = await User.incrementUserFieldBy(uid, field, value);
await db.sortedSetAdd(set, newValue, uid);
return newValue;
}
User.getPostIds = async function (uid, start, stop) {
return await db.getSortedSetRevRange(`uid:${uid}:posts`, start, stop);
};
};