Skip to content

Commit 16f8f74

Browse files
committed
Add lock/reply options #2
1 parent d57ddd9 commit 16f8f74

6 files changed

Lines changed: 86 additions & 13 deletions

File tree

‎README.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ The app can be configured to remove posts or comments for deleted users or shado
44

55
## Change History
66

7-
### v1.1.2
7+
### v1.2
88

9+
* Add ability to remove modqueued items from recently banned users
10+
* Add ability to lock removed items when removing from the modqueue
911
* Reduce the chances of incorrect removals during periods of Reddit infrastructure instability
1012
* Update Devvit and dependencies
1113
* Job execution is staggered on different subreddits to reduce spikes in activity on Devvit infrastructure
@@ -16,4 +18,4 @@ The app can be configured to remove posts or comments for deleted users or shado
1618

1719
## About this app
1820

19-
Modqueue Pruner is open source. You can find the source [here](https://github.com/fsvreddit/queue-pruner).
21+
Modqueue Pruner is open source. [You can find the source here](https://github.com/fsvreddit/queue-pruner).

‎package-lock.json‎

Lines changed: 18 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
"dependencies": {
88
"@devvit/protos": "0.12.6",
99
"@devvit/public-api": "0.12.6",
10+
"@types/pluralize": "^0.0.33",
1011
"date-fns": "^4.1.0",
1112
"devvit-helpers": "^0.8.5",
12-
"lodash": "^4.17.21"
13+
"lodash": "^4.17.21",
14+
"pluralize": "^8.0.0"
1315
},
1416
"devDependencies": {
1517
"@eslint/js": "^9.10.0",

‎src/installActions.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { AppInstall, AppUpgrade } from "@devvit/protos";
22
import { TriggerContext } from "@devvit/public-api";
33
import { ScheduledJob } from "./constants.js";
44

5-
export async function handleInstallOrUpgrade (event: AppInstall | AppUpgrade, context: TriggerContext) {
5+
export async function handleInstallOrUpgrade (_: AppInstall | AppUpgrade, context: TriggerContext) {
66
const jobs = await context.scheduler.listJobs();
77
await Promise.all(jobs.map(job => context.scheduler.cancelJob(job.id)));
88

@@ -12,4 +12,6 @@ export async function handleInstallOrUpgrade (event: AppInstall | AppUpgrade, co
1212
name: ScheduledJob.CheckQueue,
1313
cron: `${randomMinute}/5 * * * *`,
1414
});
15+
16+
console.log("App installed or upgraded: scheduled jobs have been set up.");
1517
}

‎src/pruneQueue.ts‎

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,42 @@ import { uniq } from "lodash";
44
import { ScheduledJob } from "./constants.js";
55
import { AppSetting } from "./settings.js";
66
import { isBanned } from "devvit-helpers";
7+
import { isLinkId } from "@devvit/public-api/types/tid.js";
8+
import pluralize from "pluralize";
79

810
const USER_QUEUE_KEY = "userQueue";
911
const REMOVE_QUEUE = "removeQueue";
1012

13+
async function getPostOrCommentById (itemId: string, context: JobContext) {
14+
if (isLinkId(itemId)) {
15+
return context.reddit.getPostById(itemId);
16+
} else {
17+
return context.reddit.getCommentById(itemId);
18+
}
19+
}
20+
21+
async function removeItems (itemIds: string[], lock: boolean, replyComment: string | undefined, context: JobContext) {
22+
await Promise.all(itemIds.map(item => context.reddit.remove(item, false)));
23+
24+
if (lock) {
25+
await Promise.all(itemIds.map(async (itemId) => {
26+
const item = await getPostOrCommentById(itemId, context);
27+
await item.lock();
28+
}));
29+
}
30+
31+
if (replyComment && replyComment.trim().length > 0) {
32+
for (const itemId of itemIds) {
33+
const newComment = await context.reddit.submitComment({
34+
id: itemId,
35+
text: replyComment,
36+
});
37+
await newComment.distinguish();
38+
await newComment.lock();
39+
}
40+
}
41+
}
42+
1143
export async function checkQueue (_: unknown, context: JobContext) {
1244
const modQueue = await context.reddit.getModQueue({
1345
subreddit: context.subredditName ?? await context.reddit.getCurrentSubredditName(),
@@ -26,8 +58,9 @@ export async function checkQueue (_: unknown, context: JobContext) {
2658
// Remove items from deleted users
2759
const itemsToRemove = modQueue.filter(item => item.authorName === "[deleted]");
2860
if (itemsToRemove.length > 0) {
29-
await Promise.all(itemsToRemove.map(item => context.reddit.remove(item.id, false)));
30-
console.log(`Check step: Removed ${itemsToRemove.length} item(s) from the mod queue due to deleted users.`);
61+
const shouldLock = settings[AppSetting.LockOnRemove] as boolean | undefined ?? false;
62+
await removeItems(itemsToRemove.map(item => item.id), shouldLock, undefined, context);
63+
console.log(`Check step: Removed ${itemsToRemove.length} ${pluralize("item", itemsToRemove.length)} from the mod queue due to deleted users.`);
3164
}
3265
}
3366

@@ -49,7 +82,7 @@ export async function checkQueue (_: unknown, context: JobContext) {
4982
}
5083

5184
await context.redis.zAdd(USER_QUEUE_KEY, ...newUsers.map(user => ({ member: user, score: Date.now() })));
52-
console.log(`Check step: Added ${newUsers.length} new user(s) to the queue.`);
85+
console.log(`Check step: Added ${newUsers.length} new ${pluralize("user", newUsers.length)} to the queue.`);
5386

5487
await context.scheduler.runJob({
5588
name: ScheduledJob.PruneUsers,
@@ -61,7 +94,7 @@ export async function checkQueue (_: unknown, context: JobContext) {
6194
});
6295
}
6396

64-
export async function userIsActive (username: string, context: JobContext): Promise<boolean> {
97+
async function userIsActive (username: string, context: JobContext): Promise<boolean> {
6598
try {
6699
const user = await context.reddit.getUserByUsername(username);
67100
return user !== undefined;
@@ -125,10 +158,10 @@ export async function pruneUsers (event: ScheduledJobEvent<JSONObject | undefine
125158
}
126159

127160
await context.redis.zRem(USER_QUEUE_KEY, processedUsers);
128-
console.log(`Prune step: Processed ${processed} user(s) in the prune job.`);
161+
console.log(`Prune step: Processed ${processed} ${pluralize("user", processed)} in the prune job.`);
129162

130163
if (queue.length > 0) {
131-
console.log(`Prune step: There are still ${queue.length} user(s) left in the queue.`);
164+
console.log(`Prune step: ${queue.length} ${pluralize("user", queue.length)} left in the queue.`);
132165

133166
await context.scheduler.runJob({
134167
name: ScheduledJob.PruneUsers,
@@ -167,8 +200,11 @@ export async function removeUsers (_: unknown, context: JobContext) {
167200

168201
const itemsToRemove = modQueue.filter(item => removeQueue.some(user => user.member === item.authorName));
169202
if (itemsToRemove.length > 0) {
170-
await Promise.all(itemsToRemove.map(item => context.reddit.remove(item.id, false)));
171-
console.log(`Remove step: Removed ${itemsToRemove.length} item(s) from the mod queue for shadowbanned or suspended users.`);
203+
const settings = await context.settings.getAll();
204+
const shouldLock = settings[AppSetting.LockOnRemove] as boolean | undefined ?? false;
205+
const replyComment = settings[AppSetting.ReplyCommentForShadowbanned] as string | undefined;
206+
await removeItems(itemsToRemove.map(item => item.id), shouldLock, replyComment, context);
207+
console.log(`Remove step: Removed ${itemsToRemove.length} ${pluralize("item", itemsToRemove.length)} from the mod queue for shadowbanned or suspended users.`);
172208
} else {
173209
console.log("Remove step: No items found in the mod queue for users to remove.");
174210
}

‎src/settings.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ export enum AppSetting {
44
RemoveDeleted = "removeDeleted",
55
RemoveShadowbanned = "removeShadowbanned",
66
RemoveBanned = "removeBanned",
7+
LockOnRemove = "lockOnRemove",
8+
ReplyCommentForShadowbanned = "replyCommentForShadowbanned",
79
}
810

911
export const appSettings: SettingsFormField[] = [
@@ -25,4 +27,16 @@ export const appSettings: SettingsFormField[] = [
2527
defaultValue: false,
2628
label: "Remove modqueued content for banned users",
2729
},
30+
{
31+
name: AppSetting.LockOnRemove,
32+
type: "boolean",
33+
defaultValue: false,
34+
label: "Lock posts or comments when removing from modqueue",
35+
},
36+
{
37+
name: AppSetting.ReplyCommentForShadowbanned,
38+
type: "paragraph",
39+
label: "If a user is found to be shadowbanned or suspended, reply to their content with this comment before removal.",
40+
helpText: "Leave blank to skip replying. Markdown is supported. You may wish to use this to alert users to the appeal process.",
41+
},
2842
];

0 commit comments

Comments
 (0)