Compare commits

...

28 Commits
0.2.1 ... 0.3.0

Author SHA1 Message Date
FoxxMD
20acc12460 Add footer to comment/ban content
Closes #5
2021-06-16 22:29:28 -04:00
FoxxMD
60c0569e21 Add missing actions
Closes #19
2021-06-16 22:04:44 -04:00
FoxxMD
879807390d Add YAML as configuration language to readme 2021-06-16 21:30:43 -04:00
FoxxMD
08413dbe16 Implement YAML parsing 2021-06-16 21:27:48 -04:00
FoxxMD
75cbde8b8b Improve logging levels and add end-run stats
* Increase some noisy log statements to verbose
* Display action summary on info level
* verbose -- Display run stats (checks, rules, actions) at end of event
* verbose -- Display reddit api stats (initial, current, used) at end of event
2021-06-16 13:31:37 -04:00
FoxxMD
3acf268313 Check notes length before trying to get current note 2021-06-16 12:30:28 -04:00
FoxxMD
97b9391f3b Remove debug statement derp 2021-06-16 12:21:19 -04:00
FoxxMD
f8ec0d7ee0 Fix and/or condition logic for checks and rulesets 2021-06-16 12:20:38 -04:00
FoxxMD
0002c1bc11 Allow rules to be optional, increase startup logging, and change default log level
* Allow rules to be optional on json -- if no rules actions are run immediately after check passes author/item tests
* When verbose logging show much more detail about check stats, rules, and actions on startup
* Set verbose as default log level. New users should have more information of the box so they can understand how things work.
2021-06-16 11:38:45 -04:00
FoxxMD
a09f3fe4f1 Finally got action example working correctly 2021-06-16 10:39:27 -04:00
FoxxMD
daf66083d0 Schema documentation improvements 2021-06-16 10:33:36 -04:00
FoxxMD
7acd62d787 Refactor window criteria to actually work as described 2021-06-16 00:27:04 -04:00
FoxxMD
75889cc927 Add more error handling for reddit timeout issues 2021-06-15 17:05:14 -04:00
FoxxMD
db0440356c Refactor author usage to be more universal and change name to match item behavior
* (BC) rename authors to authorIs to match itemIs -- since they have the same behavior
* Add author filter to Check so it matches usage of itemIs
2021-06-15 16:12:06 -04:00
FoxxMD
016952128c Update documentation for Toolbox User Notes 2021-06-15 15:31:59 -04:00
FoxxMD
884966b8d3 Remove more debugging statements
ugh
2021-06-15 14:33:47 -04:00
FoxxMD
0ad7c66e9d Fix error display on config error 2021-06-15 14:26:42 -04:00
FoxxMD
c075e5fb24 Remove debug statements 2021-06-15 14:11:07 -04:00
FoxxMD
a3de885620 Add some action checks 2021-06-15 14:04:06 -04:00
FoxxMD
e29d19ada8 Implement itemIs test for Checks and Rules 2021-06-15 13:54:54 -04:00
FoxxMD
c52e1d5e1d Implement toolbox usernote action 2021-06-15 12:09:26 -04:00
FoxxMD
257563a3b8 Implement toolbox usernote author filter and rule criteria 2021-06-15 10:39:16 -04:00
FoxxMD
7761372091 Implement toolbox usernote read/write 2021-06-14 22:45:48 -04:00
FoxxMD
eb62e39975 Add unmoderated run command 2021-06-14 10:26:05 -04:00
FoxxMD
bdd72dc28e Add more schema examples 2021-06-12 00:54:23 -04:00
FoxxMD
e7b5a9bb60 Change repeat activity behavior when useSubmissionAsReference=true but not a link
Return false result instead of throwing an error since this is probably the expected behavior
2021-06-12 00:02:32 -04:00
FoxxMD
699f2577e5 Fix return value of author filter 2021-06-11 23:59:57 -04:00
FoxxMD
a22096a667 Fix snoowrap logger code
Accidentally not instantiating if debug not true
2021-06-11 23:43:52 -04:00
50 changed files with 3791 additions and 710 deletions

View File

@@ -19,12 +19,15 @@ Some feature highlights:
* Simple rule-action behavior can be combined to create any level of complexity in behavior
* One instance can handle managing many subreddits (as many as it has moderator permissions in!)
* Per-subreddit configuration is handled by JSON stored in the subreddit wiki
* Any text-based actions (comment, submission, message, etc...) can be configured via a wiki page or raw text in JSON
* Any text-based actions (comment, submission, message, usernotes, etc...) can be configured via a wiki page or raw text in JSON
* All text-based actions support [mustache](https://mustache.github.io) templating
* History-based rules support multiple "valid window" types -- [ISO 8601 Durations](https://en.wikipedia.org/wiki/ISO_8601#Durations), [Day.js Durations](https://day.js.org/docs/en/durations/creating), and submission/comment count limits.
* All rules support skipping behavior based on author criteria -- name, css flair/text, and moderator status
* Checks/Rules support skipping behavior based on:
* author criteria (name, css flair/text, moderator status, and [Toolbox User Notes](https://www.reddit.com/r/toolbox/wiki/docs/usernotes))
* Activity state (removed, locked, distinguished, etc.)
* Rules and Actions support named references so you write rules/actions once and reference them anywhere
* User-configurable global/subreddit-level API caching
* Support for [Toolbox User Notes](https://www.reddit.com/r/toolbox/wiki/docs/usernotes) as criteria or Actions (writing notes)
* Docker container support
# Table of Contents
@@ -87,7 +90,7 @@ docker run -e "CLIENT_ID=myId" ... foxxmd/reddit-context-bot
## Configuration
Context Bot's configuration can be written in JSON or [JSON5](https://json5.org/). It's [schema](/src/Schema/App.json) conforms to [JSON Schema Draft 7](https://json-schema.org/).
Context Bot's configuration can be written in JSON, [JSON5](https://json5.org/) or YAML. It's [schema](/src/Schema/App.json) conforms to [JSON Schema Draft 7](https://json-schema.org/).
I suggest using [Atlassian JSON Schema Viewer](https://json-schema.app/start) ([direct link](https://json-schema.app/view/%23?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Freddit-context-bot%2Fmaster%2Fsrc%2FSchema%2FApp.json)) so you can view all documentation while also interactively writing and validating your config! From there you can drill down into any object, see its requirements, view an example JSON document, and live-edit your configuration on the right-hand side.
@@ -188,6 +191,7 @@ Options:
Commands:
run Runs bot normally
check [options] <activityIdentifier> [type] Run check(s) on a specific activity
unmoderated [options] <subreddits...> Run checks on all unmoderated activity in the modqueue
help [command] display help for command
```
@@ -225,6 +229,7 @@ Visit https://not-an-aardvark.github.io/reddit-oauth-helper/
* report
* submit
* wikiread
* wikiedit (if you are using Toolbox User Notes)
* Click **Generate tokens**, you will get a popup asking you to approve access (or login) -- **the account you approve access with is the account that Bot will control.**
* After approving an **Access Token** and **Refresh Token** will be shown at the bottom of the page. Write these down.

View File

@@ -34,6 +34,7 @@ This directory contains example of valid, ready-to-go configurations for Context
* [Repeat Activity](/examples/repeatActivity)
* [History](/examples/history)
* [Author](/examples/author)
* [Toolbox User Notes](/examples/userNotes)
* [Advanced Concepts](/examples/advancedConcepts)
* [Rule Sets](/examples/advancedConcepts/ruleSets.json5)
* [Name Rules](/examples/advancedConcepts/ruleNameReuse.json5)

View File

@@ -2,13 +2,14 @@
## Rule
The **Author** rule can if any [AuthorCriteria](https://json-schema.app/view/%23%2Fdefinitions%2FAuthorCriteria?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Freddit-context-bot%2Fmaster%2Fsrc%2FSchema%2FApp.json) from a list are either **included** or **excluded**, depending on which property you put them in.
The **Author** rule triggers if any [AuthorCriteria](https://json-schema.app/view/%23%2Fdefinitions%2FAuthorCriteria?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Freddit-context-bot%2Fmaster%2Fsrc%2FSchema%2FApp.json) from a list are either **included** or **excluded**, depending on which property you put them in.
**AuthorCriteria** that can be checked:
* name (u/userName)
* author's subreddit flair text
* author's subreddit flair css
* author's subreddit mod status
* [Toolbox User Notes](/examples/userNotes)
The Author **Rule** is best used in conjunction with other Rules to short-circuit a Check based on who the Author is. It is easier to use a Rule to do this then to write **author filters** for every Rule (and makes Rules more re-useable).
@@ -24,9 +25,13 @@ Consult the [schema](https://json-schema.app/view/%23%2Fdefinitions%2FAuthorRule
## Filter
All **Rules** have an optional `authors` property that takes an [AuthorOptions](https://json-schema.app/view/%23%2Fdefinitions%2FAuthorOptions?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Freddit-context-bot%2Fmaster%2Fsrc%2FSchema%2FApp.json) object.
All **Rules** and **Checks** have an optional `authorIs` property that takes an [AuthorOptions](https://json-schema.app/view/%23%2Fdefinitions%2FAuthorOptions?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Freddit-context-bot%2Fmaster%2Fsrc%2FSchema%2FApp.json) object.
**This property works the same as the Author Rule but if any criteria pass the Rule is skipped.** If a Rule is skipped **it does not fail or pass** and so does not affect the outcome of the Check. However, if all Rules on a Check are skipped the Check will fail.
**This property works the same as the Author Rule except that:**
* On **Rules** if all criteria fail the Rule is **skipped.**
* If a Rule is skipped **it does not fail or pass** and so does not affect the outcome of the Check.
* However, if all Rules on a Check are skipped the Check will fail.
* On **Checks** if all criteria fail the Check **fails**.
### Examples

View File

@@ -28,7 +28,7 @@
"kind": "recentActivity",
// authors filter will be checked before a rule is run. If anything passes then the Rule is skipped -- it is not failed or triggered.
// if *all* Rules for a Check are skipped due to authors filter then the Check will fail
"authors": {
"authorIs": {
// each property (include/exclude) can contain multiple AuthorCriteria
// if any AuthorCriteria passes its test the Rule is skipped
//

View File

@@ -0,0 +1,26 @@
# [Toolbox](https://www.reddit.com/r/toolbox/wiki/docs) [User Notes](https://www.reddit.com/r/toolbox/wiki/docs/usernotes)
Context Bot supports reading and writing [User Notes](https://www.reddit.com/r/toolbox/wiki/docs/usernotes) for the [Toolbox](https://www.reddit.com/r/toolbox/wiki/docs) extension.
**You must have Toolbox setup for your subreddit and at least one User Note created before you can use User Notes related features on Context Bot.**
[Click here for the Toolbox Quickstart Guide](https://www.reddit.com/r/toolbox/wiki/docs/quick_start)
## Filter
User Notes are an additional criteria on [AuthorCriteria](https://json-schema.app/view/%23%2Fdefinitions%2FAuthorCriteria?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Freddit-context-bot%2Fmaster%2Fsrc%2FSchema%2FApp.json) that can be used alongside other Author properties for both [filtering rules and in the AuthorRule.](/examples/author/)
Consult the [schema](https://json-schema.app/view/%23%2Fdefinitions%2FUserNoteCriteria?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Freddit-context-bot%2Fmaster%2Fsrc%2FSchema%2FApp.json) for a complete reference of the **UserNoteCriteria** object that can be used in AuthorCriteria.
### Examples
* [Do not tag user with Good User note](/examples/userNotes/usernoteFilter.json5)
## Action
A User Note can also be added to the Author of a Submission or Comment with the [UserNoteAction.](https://json-schema.app/view/%23%2Fdefinitions%2FUserNoteActionJson?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Freddit-context-bot%2Fmaster%2Fsrc%2FSchema%2FApp.json)
### Examples
* [Add note on user doing self promotion](/examples/userNotes/usernoteSP.json5)

View File

@@ -0,0 +1,47 @@
{
"checks": [
{
"name": "Self Promo Activities",
"description": "Tag SP only if user does not have good contributor user note",
// check will run on a new submission in your subreddit and look at the Author of that submission
"kind": "submission",
"rules": [
{
"name": "attr10all",
"kind": "attribution",
"author": {
"exclude": [
{
// the key of the usernote type to look for https://github.com/toolbox-team/reddit-moderator-toolbox/wiki/Subreddit-Wikis%3A-usernotes#working-with-note-types
// rule will not run if current usernote on Author is of type 'gooduser'
"type": "gooduser"
}
]
},
"criteria": [
{
"threshold": "10%",
"window": {
"days": 90
}
},
{
"threshold": "10%",
"window": 100
}
],
}
],
"actions": [
{
"kind": "usernote",
// the key of usernote type
// https://github.com/toolbox-team/reddit-moderator-toolbox/wiki/Subreddit-Wikis%3A-usernotes#working-with-note-types
"type": "spamwarn",
// content is mustache templated as usual
"content": "Self Promotion: {{rules.attr10all.refDomainTitle}} {{rules.attr10sub.largestPercent}}%"
}
]
}
]
}

View File

@@ -0,0 +1,38 @@
{
"checks": [
{
"name": "Self Promo Activities",
"description": "Check if any of Author's aggregated submission origins are >10% of entire history",
// check will run on a new submission in your subreddit and look at the Author of that submission
"kind": "submission",
"rules": [
{
"name": "attr10all",
"kind": "attribution",
"criteria": [
{
"threshold": "10%",
"window": {
"days": 90
}
},
{
"threshold": "10%",
"window": 100
}
],
}
],
"actions": [
{
"kind": "usernote",
// the key of usernote type
// https://github.com/toolbox-team/reddit-moderator-toolbox/wiki/Subreddit-Wikis%3A-usernotes#working-with-note-types
"type": "spamwarn",
// content is mustache templated as usual
"content": "Self Promotion: {{rules.attr10all.refDomainTitle}} {{rules.attr10sub.largestPercent}}%"
}
]
}
]
}

84
package-lock.json generated
View File

@@ -14,25 +14,30 @@
"es6-error": "^4.1.1",
"fast-deep-equal": "^3.1.3",
"he": "^1.2.0",
"js-yaml": "^4.1.0",
"json5": "^2.2.0",
"memory-cache": "^0.2.0",
"mustache": "^4.2.0",
"object-hash": "^2.2.0",
"p-event": "^4.2.0",
"pako": "^0.2.6",
"safe-stable-stringify": "^1.1.1",
"snoostorm": "^1.5.2",
"snoowrap": "^1.23.0",
"winston": "FoxxMD/winston#fbab8de969ecee578981c77846156c7f43b5f01e",
"winston-daily-rotate-file": "^4.5.5"
"winston-daily-rotate-file": "^4.5.5",
"zlib": "^1.0.5"
},
"devDependencies": {
"@tsconfig/node14": "^1.0.0",
"@types/he": "^1.1.1",
"@types/js-yaml": "^4.0.1",
"@types/memory-cache": "^0.2.1",
"@types/minimist": "^1.2.1",
"@types/mustache": "^4.1.1",
"@types/node": "^15.6.1",
"@types/object-hash": "^2.1.0",
"@types/pako": "^1.0.1",
"ts-auto-guard": "*",
"ts-json-schema-generator": "^0.93.0",
"typescript-json-schema": "^0.50.1"
@@ -137,6 +142,12 @@
"integrity": "sha512-jpzrsR1ns0n3kyWt92QfOUQhIuJGQ9+QGa7M62rO6toe98woQjnsnzjdMtsQXCdvjjmqjS2ZBCC7xKw0cdzU+Q==",
"dev": true
},
"node_modules/@types/js-yaml": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/@types/js-yaml/-/js-yaml-4.0.1.tgz",
"integrity": "sha512-xdOvNmXmrZqqPy3kuCQ+fz6wA0xU5pji9cd1nDrflWaAWtYLLGk5ykW0H6yg5TVyehHP1pfmuuSaZkhP+kspVA==",
"dev": true
},
"node_modules/@types/json-schema": {
"version": "7.0.7",
"resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.7.tgz",
@@ -185,6 +196,12 @@
"integrity": "sha512-RW3VRiuQIMo5PJ4Q1IwBtdLHL/t8ACpzUY40norN9ejE6CUBwKetmSxJnITJ0NlzN/ymF1nvPvlpvegtns7yOg==",
"dev": true
},
"node_modules/@types/pako": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/@types/pako/-/pako-1.0.1.tgz",
"integrity": "sha512-GdZbRSJ3Cv5fiwT6I0SQ3ckeN2PWNqxd26W9Z2fCK1tGrrasGy4puvNFtnddqH9UJFMQYXxEuuB7B8UK+LLwSg==",
"dev": true
},
"node_modules/@types/strip-bom": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/@types/strip-bom/-/strip-bom-3.0.0.tgz",
@@ -239,6 +256,11 @@
"integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==",
"dev": true
},
"node_modules/argparse": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz",
"integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q=="
},
"node_modules/array-back": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/array-back/-/array-back-3.1.0.tgz",
@@ -1005,6 +1027,17 @@
"resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz",
"integrity": "sha1-R+Y/evVa+m+S4VAOaQ64uFKcCZo="
},
"node_modules/js-yaml": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz",
"integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==",
"dependencies": {
"argparse": "^2.0.1"
},
"bin": {
"js-yaml": "bin/js-yaml.js"
}
},
"node_modules/jsbn": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/jsbn/-/jsbn-0.1.1.tgz",
@@ -1288,6 +1321,11 @@
"node": ">=8"
}
},
"node_modules/pako": {
"version": "0.2.6",
"resolved": "https://registry.npmjs.org/pako/-/pako-0.2.6.tgz",
"integrity": "sha1-PgxUg1O4WaucgAX6xwa91sevUF8="
},
"node_modules/path-is-absolute": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz",
@@ -2189,6 +2227,15 @@
"engines": {
"node": ">=6"
}
},
"node_modules/zlib": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/zlib/-/zlib-1.0.5.tgz",
"integrity": "sha1-bnyXL8NxxkWmr7A6sUdp3vEU/MA=",
"hasInstallScript": true,
"engines": {
"node": ">=0.2.0"
}
}
},
"dependencies": {
@@ -2276,6 +2323,12 @@
"integrity": "sha512-jpzrsR1ns0n3kyWt92QfOUQhIuJGQ9+QGa7M62rO6toe98woQjnsnzjdMtsQXCdvjjmqjS2ZBCC7xKw0cdzU+Q==",
"dev": true
},
"@types/js-yaml": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/@types/js-yaml/-/js-yaml-4.0.1.tgz",
"integrity": "sha512-xdOvNmXmrZqqPy3kuCQ+fz6wA0xU5pji9cd1nDrflWaAWtYLLGk5ykW0H6yg5TVyehHP1pfmuuSaZkhP+kspVA==",
"dev": true
},
"@types/json-schema": {
"version": "7.0.7",
"resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.7.tgz",
@@ -2324,6 +2377,12 @@
"integrity": "sha512-RW3VRiuQIMo5PJ4Q1IwBtdLHL/t8ACpzUY40norN9ejE6CUBwKetmSxJnITJ0NlzN/ymF1nvPvlpvegtns7yOg==",
"dev": true
},
"@types/pako": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/@types/pako/-/pako-1.0.1.tgz",
"integrity": "sha512-GdZbRSJ3Cv5fiwT6I0SQ3ckeN2PWNqxd26W9Z2fCK1tGrrasGy4puvNFtnddqH9UJFMQYXxEuuB7B8UK+LLwSg==",
"dev": true
},
"@types/strip-bom": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/@types/strip-bom/-/strip-bom-3.0.0.tgz",
@@ -2368,6 +2427,11 @@
"integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==",
"dev": true
},
"argparse": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz",
"integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q=="
},
"array-back": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/array-back/-/array-back-3.1.0.tgz",
@@ -2994,6 +3058,14 @@
"resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz",
"integrity": "sha1-R+Y/evVa+m+S4VAOaQ64uFKcCZo="
},
"js-yaml": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz",
"integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==",
"requires": {
"argparse": "^2.0.1"
}
},
"jsbn": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/jsbn/-/jsbn-0.1.1.tgz",
@@ -3217,6 +3289,11 @@
"p-finally": "^1.0.0"
}
},
"pako": {
"version": "0.2.6",
"resolved": "https://registry.npmjs.org/pako/-/pako-0.2.6.tgz",
"integrity": "sha1-PgxUg1O4WaucgAX6xwa91sevUF8="
},
"path-is-absolute": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz",
@@ -3890,6 +3967,11 @@
"resolved": "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz",
"integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==",
"dev": true
},
"zlib": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/zlib/-/zlib-1.0.5.tgz",
"integrity": "sha1-bnyXL8NxxkWmr7A6sUdp3vEU/MA="
}
}
}

View File

@@ -30,25 +30,30 @@
"es6-error": "^4.1.1",
"fast-deep-equal": "^3.1.3",
"he": "^1.2.0",
"js-yaml": "^4.1.0",
"json5": "^2.2.0",
"memory-cache": "^0.2.0",
"mustache": "^4.2.0",
"object-hash": "^2.2.0",
"p-event": "^4.2.0",
"pako": "^0.2.6",
"safe-stable-stringify": "^1.1.1",
"snoostorm": "^1.5.2",
"snoowrap": "^1.23.0",
"winston": "FoxxMD/winston#fbab8de969ecee578981c77846156c7f43b5f01e",
"winston-daily-rotate-file": "^4.5.5"
"winston-daily-rotate-file": "^4.5.5",
"zlib": "^1.0.5"
},
"devDependencies": {
"@tsconfig/node14": "^1.0.0",
"@types/he": "^1.1.1",
"@types/js-yaml": "^4.0.1",
"@types/memory-cache": "^0.2.1",
"@types/minimist": "^1.2.1",
"@types/mustache": "^4.1.1",
"@types/node": "^15.6.1",
"@types/object-hash": "^2.1.0",
"@types/pako": "^1.0.1",
"ts-auto-guard": "*",
"ts-json-schema-generator": "^0.93.0",
"typescript-json-schema": "^0.50.1"

View File

@@ -5,6 +5,8 @@ import {ReportAction, ReportActionJson} from "./ReportAction";
import {FlairAction, FlairActionJson} from "./SubmissionAction/FlairAction";
import Action, {ActionJson} from "./index";
import {Logger} from "winston";
import {UserNoteAction, UserNoteActionJson} from "./UserNoteAction";
import ApproveAction, {ApproveActionConfig} from "./ApproveAction";
export function actionFactory
(config: ActionJson, logger: Logger, subredditName: string): Action {
@@ -19,6 +21,10 @@ export function actionFactory
return new ReportAction({...config as ReportActionJson, logger, subredditName});
case 'flair':
return new FlairAction({...config as FlairActionJson, logger, subredditName});
case 'approve':
return new ApproveAction({...config as ApproveActionConfig, logger, subredditName});
case 'usernote':
return new UserNoteAction({...config as UserNoteActionJson, logger, subredditName});
default:
throw new Error('rule "kind" was not recognized.');
}

View File

@@ -0,0 +1,35 @@
import {ActionJson, ActionConfig} from "./index";
import Action from "./index";
import Snoowrap, {Comment, Submission} from "snoowrap";
import {RuleResult} from "../Rule";
export class ApproveAction extends Action {
getKind() {
return 'Approve';
}
async process(item: Comment | Submission, ruleResults: RuleResult[]): Promise<void> {
//snoowrap typing issue, thinks comments can't be locked
// @ts-ignore
if (item.approved) {
this.logger.warn('Item is already approved');
}
if (!this.dryRun) {
// @ts-ignore
await item.approve();
}
}
}
export interface ApproveActionConfig extends ActionConfig {
}
/**
* Ban the Author of the Activity this Check is run on
* */
export interface ApproveActionJson extends ApproveActionConfig, ActionJson {
kind: 'approve'
}
export default ApproveAction;

104
src/Action/BanAction.ts Normal file
View File

@@ -0,0 +1,104 @@
import {ActionJson, ActionConfig, ActionOptions} from "./index";
import Action from "./index";
import Snoowrap, {Comment, Submission} from "snoowrap";
import {RuleResult} from "../Rule";
import {renderContent} from "../Utils/SnoowrapUtils";
import {generateFooter} from "../util";
export class BanAction extends Action {
message?: string;
reason?: string;
duration?: number;
note?: string;
constructor(options: BanActionOptions) {
super(options);
const {
message,
reason,
duration,
note
} = options;
this.message = message;
this.reason = reason;
this.duration = duration;
this.note = note;
}
getKind() {
return 'Ban';
}
async process(item: Comment | Submission, ruleResults: RuleResult[]): Promise<void> {
const content = this.message === undefined ? undefined : await this.resources.getContent(this.message, item.subreddit);
const renderedContent = content === undefined ? undefined : await renderContent(content, item, ruleResults);
const footer = await generateFooter(item);
let banPieces = [];
banPieces.push(`Message: ${renderedContent === undefined ? 'None' : `${renderedContent.length > 100 ? `\r\n${renderedContent}` : renderedContent}`}`);
banPieces.push(`Reason: ${this.reason || 'None'}`);
banPieces.push(`Note: ${this.note || 'None'}`);
const durText = this.duration === undefined ? 'permanently' : `for ${this.duration} days`;
this.logger.verbose(`Banning ${item.author.name} ${durText}\r\n${banPieces.join('\r\n')}`);
if (!this.dryRun) {
// @ts-ignore
await item.subreddit.banUser({
name: item.author.id,
banMessage: renderedContent === undefined ? undefined : `${renderedContent}${footer}`,
banReason: this.reason,
banNote: this.note,
duration: this.duration
});
}
}
}
export interface BanActionConfig extends ActionConfig {
/**
* The message that is sent in the ban notification. `message` is interpreted as reddit-flavored Markdown.
*
* If value starts with `wiki:` then the proceeding value will be used to get a wiki page
*
* EX `wiki:botconfig/mybot` tries to get `https://reddit.com/mySubredditExample/wiki/botconfig/mybot`
*
* EX `this is plain text` => "this is plain text"
*
* EX `this is **bold** markdown text` => "this is **bold** markdown text"
*
* @examples ["This is the content of a comment/report/usernote", "this is **bold** markdown text", "wiki:botconfig/acomment" ]
* */
message?: string
/**
* Reason for ban.
* @maximum 100
* @examples ["repeat spam"]
* */
reason?: string
/**
* Number of days to ban the Author. If not specified Author will be banned permanently.
* @minimum 1
* @maximum 999
* @examples [90]
* */
duration?: number
/**
* A mod note for this ban
* @maximum 100
* @examples ["Sock puppet for u/AnotherUser"]
* */
note?: string
}
export interface BanActionOptions extends BanActionConfig, ActionOptions {
}
/**
* Ban the Author of the Activity this Check is run on
* */
export interface BanActionJson extends BanActionConfig, ActionJson {
kind: 'ban',
}
export default BanAction;

View File

@@ -2,8 +2,9 @@ import Action, {ActionJson, ActionOptions} from "./index";
import {Comment} from "snoowrap";
import Submission from "snoowrap/dist/objects/Submission";
import {renderContent} from "../Utils/SnoowrapUtils";
import {RichContent} from "../Common/interfaces";
import {RequiredRichContent, RichContent} from "../Common/interfaces";
import {RuleResult} from "../Rule";
import {generateFooter} from "../util";
export class CommentAction extends Action {
content: string;
@@ -30,19 +31,24 @@ export class CommentAction extends Action {
}
async process(item: Comment | Submission, ruleResults: RuleResult[]): Promise<void> {
const content = await this.cache.getContent(this.content, item.subreddit);
const content = await this.resources.getContent(this.content, item.subreddit);
const renderedContent = await renderContent(content, item, ruleResults);
this.logger.verbose(`Contents:\r\n${renderedContent}`);
if(item.archived) {
this.logger.warn('Cannot comment because Item is archived');
return;
}
const footer = await generateFooter(item);
// @ts-ignore
const reply: Comment = await item.reply(renderedContent);
const reply: Comment = await item.reply(`${renderedContent}${footer}`);
if (this.lock) {
if(item instanceof Submission) {
if(!this.dryRun) {
// @ts-ignore
await item.lock();
}
} else {
this.logger.warn('Snoowrap does not support locking Comments');
if (!this.dryRun) {
// snoopwrap typing issue, thinks comments can't be locked
// @ts-ignore
await item.lock();
}
}
if (this.distinguish && !this.dryRun) {
@@ -52,7 +58,7 @@ export class CommentAction extends Action {
}
}
export interface CommentActionConfig extends RichContent {
export interface CommentActionConfig extends RequiredRichContent {
/**
* Lock the comment after creation?
* */
@@ -67,12 +73,12 @@ export interface CommentActionConfig extends RichContent {
distinguish?: boolean,
}
export interface CommentActionOptions extends CommentActionConfig,ActionOptions {
export interface CommentActionOptions extends CommentActionConfig, ActionOptions {
}
/**
* Reply to the Activity. For a submission the reply will be a top-level comment.
* */
export interface CommentActionJson extends CommentActionConfig, ActionJson {
kind: 'comment'
}

View File

@@ -8,14 +8,16 @@ export class LockAction extends Action {
return 'Lock';
}
async process(item: Comment|Submission, ruleResults: RuleResult[]): Promise<void> {
if (item instanceof Submission) {
if(!this.dryRun) {
// @ts-ignore
await item.lock();
}
} else {
this.logger.warn('Snoowrap does not support locking Comments');
async process(item: Comment | Submission, ruleResults: RuleResult[]): Promise<void> {
//snoowrap typing issue, thinks comments can't be locked
// @ts-ignore
if (item.locked) {
this.logger.warn('Item is already locked');
}
if (!this.dryRun) {
//snoowrap typing issue, thinks comments can't be locked
// @ts-ignore
await item.lock();
}
}
}
@@ -28,7 +30,7 @@ export interface LockActionConfig extends ActionConfig {
* Lock the Activity
* */
export interface LockActionJson extends LockActionConfig, ActionJson {
kind: 'lock'
}
export default LockAction;

View File

@@ -8,8 +8,14 @@ export class RemoveAction extends Action {
return 'Remove';
}
async process(item: Comment|Submission, ruleResults: RuleResult[]): Promise<void> {
if(!this.dryRun) {
async process(item: Comment | Submission, ruleResults: RuleResult[]): Promise<void> {
// issue with snoowrap typings, doesn't think prop exists on Submission
// @ts-ignore
if (item.removed === true) {
this.logger.warn('Item is already removed');
return;
}
if (!this.dryRun) {
// @ts-ignore
await item.remove();
}
@@ -24,5 +30,5 @@ export interface RemoveActionConfig extends ActionConfig {
* Remove the Activity
* */
export interface RemoveActionJson extends RemoveActionConfig, ActionJson {
kind: 'remove'
}

View File

@@ -4,6 +4,7 @@ import Snoowrap, {Comment, Submission} from "snoowrap";
import {truncateStringToLength} from "../util";
import {renderContent} from "../Utils/SnoowrapUtils";
import {RuleResult} from "../Rule";
import {RichContent} from "../Common/interfaces";
// https://www.reddit.com/dev/api/oauth#POST_api_report
// denotes 100 characters maximum
@@ -23,7 +24,7 @@ export class ReportAction extends Action {
}
async process(item: Comment | Submission, ruleResults: RuleResult[]): Promise<void> {
const content = await this.cache.getContent(this.content, item.subreddit);
const content = await this.resources.getContent(this.content, item.subreddit);
const renderedContent = await renderContent(content, item, ruleResults);
this.logger.verbose(`Contents:\r\n${renderedContent}`);
const truncatedContent = reportTrunc(renderedContent);
@@ -34,11 +35,7 @@ export class ReportAction extends Action {
}
}
export interface ReportActionConfig {
/**
* The text of the report. If longer than 100 characters will be truncated to "[content]..."
* */
content: string,
export interface ReportActionConfig extends RichContent {
}
export interface ReportActionOptions extends ReportActionConfig, ActionOptions {
@@ -48,5 +45,5 @@ export interface ReportActionOptions extends ReportActionConfig, ActionOptions {
* Report the Activity
* */
export interface ReportActionJson extends ReportActionConfig, ActionJson {
kind: 'report'
}

View File

@@ -55,5 +55,5 @@ export interface FlairActionOptions extends FlairActionConfig,ActionOptions {
* Flair the Submission
* */
export interface FlairActionJson extends FlairActionConfig, ActionJson {
kind: 'flair'
}

View File

@@ -0,0 +1,65 @@
import {ActionJson, ActionConfig, ActionOptions} from "./index";
import Action from "./index";
import {Comment} from "snoowrap";
import {renderContent} from "../Utils/SnoowrapUtils";
import {RuleResult} from "../Rule";
import {UserNote, UserNoteJson} from "../Subreddit/UserNotes";
import Submission from "snoowrap/dist/objects/Submission";
export class UserNoteAction extends Action {
content: string;
type: string;
allowDuplicate: boolean;
constructor(options: UserNoteActionOptions) {
super(options);
const {type, content = '', allowDuplicate = false} = options;
this.type = type;
this.content = content;
this.allowDuplicate = allowDuplicate;
}
getKind() {
return 'User Note';
}
async process(item: Comment | Submission, ruleResults: RuleResult[]): Promise<void> {
const content = await this.resources.getContent(this.content, item.subreddit);
const renderedContent = await renderContent(content, item, ruleResults);
this.logger.verbose(`Note:\r\n(${this.type}) ${renderedContent}`);
if (!this.allowDuplicate) {
const notes = await this.resources.userNotes.getUserNotes(item.author);
const existingNote = notes.find((x) => x.link.includes(item.id));
if (existingNote) {
this.logger.info(`Will not add note because one already exists for this Activity (${existingNote.time.local().format()}) and allowDuplicate=false`);
return;
}
}
if (!this.dryRun) {
await this.resources.userNotes.addUserNote(item, this.type, renderedContent);
} else if (!await this.resources.userNotes.warningExists(this.type)) {
this.logger.warn(`UserNote type '${this.type}' does not exist. If you meant to use this please add it through Toolbox first.`);
}
}
}
export interface UserNoteActionConfig extends ActionConfig,UserNoteJson {
/**
* Add Note even if a Note already exists for this Activity
* @examples [false]
* @default false
* */
allowDuplicate?: boolean,
}
export interface UserNoteActionOptions extends UserNoteActionConfig, ActionOptions {
}
/**
* Add a Toolbox User Note to the Author of this Activity
* */
export interface UserNoteActionJson extends UserNoteActionConfig, ActionJson {
kind: 'usernote'
}

View File

@@ -1,12 +1,12 @@
import {Comment, Submission} from "snoowrap";
import {Logger} from "winston";
import {RuleResult} from "../Rule";
import CacheManager, {SubredditCache} from "../Subreddit/SubredditCache";
import ResourceManager, {SubredditResources} from "../Subreddit/SubredditResources";
export abstract class Action {
name?: string;
logger: Logger;
cache: SubredditCache;
resources: SubredditResources;
dryRun: boolean;
constructor(options: ActionOptions) {
@@ -19,16 +19,19 @@ export abstract class Action {
this.name = name;
this.dryRun = dryRun;
this.cache = CacheManager.get(subredditName);
const uniqueName = this.name === this.getKind() ? this.getKind() : `${this.getKind()} - ${this.name}`;
this.logger = logger.child({labels: ['Action', uniqueName]});
this.resources = ResourceManager.get(subredditName) as SubredditResources;
this.logger = logger.child({labels: ['Action', this.getActionUniqueName()]});
}
abstract getKind(): string;
getActionUniqueName() {
return this.name === this.getKind() ? this.getKind() : `${this.getKind()} - ${this.name}`;
}
async handle(item: Comment | Submission, ruleResults: RuleResult[]): Promise<void> {
await this.process(item, ruleResults);
this.logger.debug(`${this.dryRun ? 'DRYRUN - ' : ''}Done`);
this.logger.verbose(`${this.dryRun ? 'DRYRUN - ' : ''}Done`);
}
abstract process(item: Comment | Submission, ruleResults: RuleResult[]): Promise<void>;
@@ -46,12 +49,14 @@ export interface ActionConfig {
* Can only contain letters, numbers, underscore, spaces, and dashes
*
* @pattern ^[a-zA-Z]([\w -]*[\w])?$
* @examples ["myDescriptiveAction"]
* */
name?: string;
/**
* If `true` the Action will not make the API request to Reddit to perform its action.
*
* @default false
* @examples [false, true]
* */
dryRun?: boolean;
}
@@ -60,7 +65,7 @@ export interface ActionJson extends ActionConfig {
/**
* The type of action that will be performed
*/
kind: 'comment' | 'lock' | 'remove' | 'report' | 'flair'
kind: 'comment' | 'lock' | 'remove' | 'report' | 'approve' | 'ban' | 'flair' | 'usernote'
}
export const isActionJson = (obj: object): obj is ActionJson => {

View File

@@ -1,13 +1,13 @@
import Snoowrap from "snoowrap";
import {Manager} from "./Subreddit/Manager";
import winston, {Logger} from "winston";
import {argParseInt, labelledFormat, parseBool, sleep} from "./util";
import {argParseInt, labelledFormat, parseBool, parseFromJsonOrYamlToObject, sleep} from "./util";
import snoowrap from "snoowrap";
import pEvent from "p-event";
import JSON5 from 'json5';
import EventEmitter from "events";
import CacheManager from './Subreddit/SubredditCache';
import dayjs from "dayjs";
import CacheManager from './Subreddit/SubredditResources';
import dayjs, {Dayjs} from "dayjs";
import LoggedError from "./Utils/LoggedError";
const {transports} = winston;
@@ -15,6 +15,8 @@ const snooLogWrapper = (logger: Logger) => {
return {
warn: (...args: any[]) => logger.warn(args.slice(0, 2).join(' '), [args.slice(2)]),
debug: (...args: any[]) => logger.debug(args.slice(0, 2).join(' '), [args.slice(2)]),
info: (...args: any[]) => logger.info(args.slice(0, 2).join(' '), [args.slice(2)]),
trace: (...args: any[]) => logger.debug(args.slice(0, 2).join(' '), [args.slice(2)]),
}
}
@@ -28,6 +30,7 @@ export class App {
dryRun?: true | undefined;
heartbeatInterval: number;
apiLimitWarning: number;
heartBeating: boolean = false;
constructor(options: any = {}) {
const {
@@ -37,7 +40,7 @@ export class App {
accessToken,
refreshToken,
logDir = process.env.LOG_DIR || `${process.cwd()}/logs`,
logLevel = process.env.LOG_LEVEL || 'info',
logLevel = process.env.LOG_LEVEL || 'verbose',
wikiConfig = process.env.WIKI_CONFIG || 'botconfig/contextbot',
snooDebug = process.env.SNOO_DEBUG || false,
dryRun = process.env.DRYRUN || false,
@@ -121,19 +124,12 @@ export class App {
accessToken,
};
let shouldDebug = parseBool(snooDebug);
let snooLogger;
if (shouldDebug) {
const clogger = this.logger.child({labels: ['Snoowrap']});
snooLogger = snooLogWrapper(clogger);
}
this.client = new snoowrap(creds);
this.client.config({
warnings: true,
maxRetryAttempts: 5,
debug: shouldDebug,
// @ts-ignore
logger: snooLogger,
debug: parseBool(snooDebug),
logger: snooLogWrapper(this.logger.child({labels: ['Snoowrap']})),
continueAfterRatelimitError: true,
});
}
@@ -173,7 +169,6 @@ export class App {
// get configs for subs we want to run on and build/validate them
for (const sub of subsToRun) {
let content = undefined;
let json = undefined;
try {
const wiki = sub.getWikiPage(this.wikiLocation);
content = await wiki.content_md;
@@ -181,45 +176,106 @@ export class App {
this.logger.error(`[${sub.display_name_prefixed}] Could not read wiki configuration. Please ensure the page https://reddit.com${sub.url}wiki/${this.wikiLocation} exists and is readable -- error: ${err.message}`);
continue;
}
try {
json = JSON5.parse(content);
} catch (err) {
this.logger.error(`[${sub.display_name_prefixed}] Wiki page contents was not valid -- error: ${err.message}`);
if(content === '') {
this.logger.error(`[${sub.display_name_prefixed}] Wiki page contents was empty`);
continue;
}
const [configObj, jsonErr, yamlErr] = parseFromJsonOrYamlToObject(content);
if (configObj === undefined) {
this.logger.error(`[${sub.display_name_prefixed}] Could not parse wiki page contents as JSON or YAML`);
this.logger.error(jsonErr);
this.logger.error(yamlErr);
continue;
}
try {
subSchedule.push(new Manager(sub, this.client, this.logger, json, {dryRun: this.dryRun}));
subSchedule.push(new Manager(sub, this.client, this.logger, configObj, {dryRun: this.dryRun}));
} catch (err) {
this.logger.error(`[${sub.display_name_prefixed}] Config was not valid`, undefined, err);
if(!(err instanceof LoggedError)) {
this.logger.error(`[${sub.display_name_prefixed}] Config was not valid`, err);
}
}
}
this.subManagers = subSchedule;
}
async heartbeat() {
while (true) {
await sleep(this.heartbeatInterval * 1000);
const heartbeat = `HEARTBEAT -- Reddit API Rate Limit remaining: ${this.client.ratelimitRemaining}`
if (this.apiLimitWarning >= this.client.ratelimitRemaining) {
this.logger.warn(heartbeat);
} else {
this.logger.info(heartbeat);
try {
this.heartBeating = true;
while (true) {
await sleep(this.heartbeatInterval * 1000);
const heartbeat = `HEARTBEAT -- Reddit API Rate Limit remaining: ${this.client.ratelimitRemaining}`
if (this.apiLimitWarning >= this.client.ratelimitRemaining) {
this.logger.warn(heartbeat);
} else {
this.logger.info(heartbeat);
}
}
} finally {
this.heartBeating = false;
}
}
async runManagers() {
for (const manager of this.subManagers) {
manager.handle();
}
// basic backoff delay if reddit is under load and not responding
let timeoutCount = 0;
let maxTimeoutCount = 4;
let otherRetryCount = 0;
// not sure should even allow so set to 0 for now
let maxOtherCount = 0;
let keepRunning = true;
let lastErrorAt: Dayjs | undefined;
if (this.heartbeatInterval !== 0) {
this.heartbeat();
}
while (keepRunning) {
try {
for (const manager of this.subManagers) {
if (!manager.running) {
manager.handle();
}
}
const emitter = new EventEmitter();
await pEvent(emitter, 'end');
if (this.heartbeatInterval !== 0 && !this.heartBeating) {
this.heartbeat();
}
const emitter = new EventEmitter();
await pEvent(emitter, 'end');
keepRunning = false;
} catch (err) {
if (lastErrorAt !== undefined && dayjs().diff(lastErrorAt, 'minute') >= 5) {
// if its been longer than 5 minutes since last error clear counters
timeoutCount = 0;
otherRetryCount = 0;
}
lastErrorAt = dayjs();
if (err.message.includes('ETIMEDOUT')) {
timeoutCount++;
if (timeoutCount > maxTimeoutCount) {
this.logger.error(`Timeouts (${timeoutCount}) exceeded max allowed (${maxTimeoutCount})`);
throw err;
}
// exponential backoff
const ms = (Math.pow(2, timeoutCount - 1) + (Math.random() - 0.3)) * 1000;
this.logger.warn(`Reddit response timed out. Will wait ${ms / 1000} seconds before restarting managers`);
await sleep(ms);
} else {
// linear backoff
otherRetryCount++;
if (maxOtherCount > otherRetryCount) {
throw err;
}
const ms = (3 * 1000) * otherRetryCount;
this.logger.warn(`Non-timeout error occurred. Will wait ${ms / 1000} seconds before restarting managers`);
await sleep(ms);
}
}
}
}
}

View File

@@ -1,8 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommentCheck = void 0;
const index_1 = require("./index");
class CommentCheck extends index_1.Check {
}
exports.CommentCheck = CommentCheck;
//# sourceMappingURL=CommentCheck.js.map

View File

@@ -1 +0,0 @@
{"version":3,"file":"CommentCheck.js","sourceRoot":"","sources":["CommentCheck.ts"],"names":[],"mappings":";;;AAAA,mCAA8B;AAE9B,MAAa,YAAa,SAAQ,aAAK;CAEtC;AAFD,oCAEC"}

View File

@@ -1,5 +1,13 @@
import {Check} from "./index";
import {Check, CheckOptions} from "./index";
import {CommentState} from "../Common/interfaces";
export class CommentCheck extends Check {
itemIs: CommentState[];
constructor(options: CheckOptions) {
super(options);
const {itemIs = []} = options;
this.itemIs = itemIs;
this.logSummary('comment');
}
}

View File

@@ -1,8 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SubmissionCheck = void 0;
const index_1 = require("./index");
class SubmissionCheck extends index_1.Check {
}
exports.SubmissionCheck = SubmissionCheck;
//# sourceMappingURL=SubmissionCheck.js.map

View File

@@ -1 +0,0 @@
{"version":3,"file":"SubmissionCheck.js","sourceRoot":"","sources":["SubmissionCheck.ts"],"names":[],"mappings":";;;AACA,mCAA8B;AAE9B,MAAa,eAAgB,SAAQ,aAAK;CAEzC;AAFD,0CAEC"}

View File

@@ -1,6 +1,14 @@
import {Check} from "./index";
import {Check, CheckOptions} from "./index";
import {SubmissionState} from "../Common/interfaces";
export class SubmissionCheck extends Check {
itemIs: SubmissionState[];
constructor(options: CheckOptions) {
super(options);
const {itemIs = []} = options;
this.itemIs = itemIs;
this.logSummary('submission');
}
}

View File

@@ -1,17 +1,25 @@
import {RuleSet, IRuleSet, RuleSetJson, RuleSetObjectJson} from "../Rule/RuleSet";
import {IRule,Rule, RuleJSONConfig, RuleResult} from "../Rule";
import {Author, AuthorOptions, IRule, Rule, RuleJSONConfig, RuleResult} from "../Rule";
import Action, {ActionConfig, ActionJson} from "../Action";
import {Logger} from "winston";
import {Comment, Submission} from "snoowrap";
import {actionFactory} from "../Action/ActionFactory";
import {ruleFactory} from "../Rule/RuleFactory";
import {createAjvFactory, mergeArr, ruleNamesFromResults} from "../util";
import {JoinCondition, JoinOperands} from "../Common/interfaces";
import {
ChecksActivityState,
CommentState,
JoinCondition,
JoinOperands,
SubmissionState,
TypedActivityStates
} from "../Common/interfaces";
import * as RuleSchema from '../Schema/Rule.json';
import * as RuleSetSchema from '../Schema/RuleSet.json';
import * as ActionSchema from '../Schema/Action.json';
import Ajv from 'ajv';
import {ActionObjectJson, RuleJson, RuleObjectJson, ActionJson as ActionTypeJson} from "../Common/types";
import {isItem} from "../Utils/SnoowrapUtils";
import ResourceManager, {SubredditResources} from "../Subreddit/SubredditResources";
export class Check implements ICheck {
actions: Action[] = [];
@@ -20,7 +28,10 @@ export class Check implements ICheck {
condition: JoinOperands;
rules: Array<RuleSet | Rule> = [];
logger: Logger;
itemIs: TypedActivityStates;
authorIs: AuthorOptions;
dryRun?: boolean;
resources: SubredditResources;
constructor(options: CheckOptions) {
const {
@@ -30,6 +41,11 @@ export class Check implements ICheck {
rules = [],
actions = [],
subredditName,
itemIs = [],
authorIs: {
include = [],
exclude = [],
} = {},
dryRun,
} = options;
@@ -37,9 +53,16 @@ export class Check implements ICheck {
const ajv = createAjvFactory(this.logger);
this.resources = ResourceManager.get(subredditName) as SubredditResources;
this.name = name;
this.description = description;
this.condition = condition;
this.itemIs = itemIs;
this.authorIs = {
exclude: exclude.map(x => new Author(x)),
include: include.map(x => new Author(x)),
}
this.dryRun = dryRun;
for (const r of rules) {
if (r instanceof Rule || r instanceof RuleSet) {
@@ -84,12 +107,79 @@ export class Check implements ICheck {
}
}
}
}
async run(item: Submission | Comment, existingResults: RuleResult[] = []): Promise<[boolean, RuleResult[]]> {
logSummary(type: string) {
const runStats = [];
const ruleSetCount = this.rules.reduce((x, r) => r instanceof RuleSet ? x + 1: x, 0);
const rulesInSetsCount = this.rules.reduce((x, r) => r instanceof RuleSet ? x + r.rules.length : x,0);
if(ruleSetCount > 0) {
runStats.push(`${ruleSetCount} Rule Sets (${rulesInSetsCount} Rules)`);
}
const topRuleCount = this.rules.reduce((x, r) => r instanceof Rule ? x + 1: x, 0);
if(topRuleCount > 0) {
runStats.push(`${topRuleCount} Top-Level Rules`);
}
runStats.push(`${this.actions.length} Actions`);
// not sure if this should be info or verbose
this.logger.info(`${type.toUpperCase()} (${this.condition}) => ${runStats.join(' | ')}${this.description !== undefined ? ` => ${this.description}` : ''}`);
if(this.rules.length === 0) {
this.logger.warn('No rules found -- this check will ALWAYS PASS!');
}
let ruleSetIndex = 1;
for(const r of this.rules) {
if(r instanceof RuleSet) {
for(const ru of r.rules) {
this.logger.verbose(`(Rule Set ${ruleSetIndex} ${r.condition}) => ${ru.getRuleUniqueName()}`);
}
ruleSetIndex++;
} else {
this.logger.verbose(`(Rule) => ${r.getRuleUniqueName()}`);
}
}
for(const a of this.actions) {
this.logger.verbose(`(Action) => ${a.getActionUniqueName()}`);
}
}
async runRules(item: Submission | Comment, existingResults: RuleResult[] = []): Promise<[boolean, RuleResult[]]> {
let allResults: RuleResult[] = [];
const [itemPass, crit] = isItem(item, this.itemIs, this.logger);
if(!itemPass) {
this.logger.verbose(`❌ => Item did not pass 'itemIs' test`);
return [false, allResults];
}
let authorPass = null;
if (this.authorIs.include !== undefined && this.authorIs.include.length > 0) {
for (const auth of this.authorIs.include) {
if (await this.resources.testAuthorCriteria(item, auth)) {
authorPass = true;
break;
}
}
if(!authorPass) {
this.logger.verbose('❌ => Inclusive author criteria not matched');
return Promise.resolve([false, allResults]);
}
}
if (authorPass === null && this.authorIs.exclude !== undefined && this.authorIs.exclude.length > 0) {
for (const auth of this.authorIs.exclude) {
if (await this.resources.testAuthorCriteria(item, auth, false)) {
authorPass = true;
break;
}
}
if(!authorPass) {
this.logger.verbose('❌ => Exclusive author criteria not matched');
return Promise.resolve([false, allResults]);
}
}
if(this.rules.length === 0) {
this.logger.info(`✔️ => No rules to run, check auto-passes`);
return [true, allResults];
}
let runOne = false;
for (const r of this.rules) {
const combinedResults = [...existingResults, ...allResults];
@@ -110,39 +200,69 @@ export class Check implements ICheck {
}
}
if (!runOne) {
this.logger.info('❌ => All Rules skipped because of Author checks');
this.logger.verbose('❌ => All Rules skipped because of Author checks or itemIs tests');
return [false, allResults];
} else if(this.condition === 'OR') {
// if OR and did not return already then none passed
this.logger.verbose(`❌ => Rules (OR): ${ruleNamesFromResults(allResults)}`);
return [false, allResults];
}
// otherwise AND and did not return already so all passed
this.logger.info(`✔️ => Rules (AND) : ${ruleNamesFromResults(allResults)}`);
return [true, allResults];
}
async runActions(item: Submission | Comment, ruleResults: RuleResult[]): Promise<void> {
async runActions(item: Submission | Comment, ruleResults: RuleResult[]): Promise<Action[]> {
this.logger.debug(`${this.dryRun ? 'DRYRUN - ' : ''}Running Actions`);
const runActions: Action[] = [];
for (const a of this.actions) {
await a.handle(item, ruleResults);
try {
await a.handle(item, ruleResults);
runActions.push(a);
} catch(err) {
this.logger.error(`Action ${a.getActionUniqueName()} encountered an error while running`);
this.logger.error(err);
}
}
this.logger.info(`${this.dryRun ? 'DRYRUN - ' : ''}Ran Actions`);
this.logger.info(`${this.dryRun ? 'DRYRUN - ' : ''}Ran Actions: ${runActions.map(x => x.getActionUniqueName()).join(' | ')}`);
return runActions;
}
}
export interface ICheck extends JoinCondition {
export interface ICheck extends JoinCondition, ChecksActivityState {
/**
* Friendly name for this Check EX "crosspostSpamCheck"
*
* Can only contain letters, numbers, underscore, spaces, and dashes
*
* @pattern ^[a-zA-Z]([\w -]*[\w])?$
* @examples ["myNewCheck"]
* */
name: string,
/**
* @examples ["A short description of what this check looks for and actions it performs"]
* */
description?: string,
/**
* Use this option to override the `dryRun` setting for all of its `Actions`
*
* @default undefined
* @examples [false, true]
* */
dryRun?: boolean;
/**
* A list of criteria to test the state of the `Activity` against before running the check.
*
* If any set of criteria passes the Check will be run. If the criteria fails then the Check will fail.
*
* * @examples [[{"over_18": true, "removed': false}]]
* */
itemIs?: TypedActivityStates
/**
* If present then these Author criteria are checked before running the Check. If criteria fails then the Check will fail.
* */
authorIs?: AuthorOptions
}
export interface CheckOptions extends ICheck {
@@ -155,26 +275,52 @@ export interface CheckOptions extends ICheck {
export interface CheckJson extends ICheck {
/**
* The type of event (new submission or new comment) this check should be run against
* @examples ["submission", "comment"]
*/
kind: 'submission' | 'comment'
/**
* A list of Rules to run. If `Rule` objects are triggered based on `condition` then `Actions` will be performed.
* A list of Rules to run.
*
* Can be `Rule`, `RuleSet`, or the `name` of any **named** `Rule` in your subreddit's configuration
* @minItems 1
* If `Rule` objects are triggered based on `condition` then `actions` will be performed.
*
* Can be `Rule`, `RuleSet`, or the `name` of any **named** `Rule` in your subreddit's configuration.
*
* **If `rules` is an empty array or not present then `actions` are performed immediately.**
* */
rules: Array<RuleSetJson | RuleJson>
rules?: Array<RuleSetJson | RuleJson>
/**
* The `Actions` to run after the check is successfully triggered. ALL `Actions` will run in the order they are listed
*
* Can be `Action` or the `name` of any **named** `Action` in your subreddit's configuration
*
* @minItems 1
* @examples [[{"kind": "comment", "content": "this is the content of the comment", "distinguish": true}, {"kind": "lock"}]]
* */
actions: Array<ActionTypeJson>
}
export interface CheckStructuredJson extends CheckJson {
export interface SubmissionCheckJson extends CheckJson {
kind: 'submission'
itemIs?: SubmissionState[]
}
export interface CommentCheckJson extends CheckJson {
kind: 'comment'
itemIs?: CommentState[]
}
export type CheckStructuredJson = SubmissionCheckStructuredJson | CommentCheckStructuredJson;
// export interface CheckStructuredJson extends CheckJson {
// rules: Array<RuleSetObjectJson | RuleObjectJson>
// actions: Array<ActionObjectJson>
// }
export interface SubmissionCheckStructuredJson extends SubmissionCheckJson {
rules: Array<RuleSetObjectJson | RuleObjectJson>
actions: Array<ActionObjectJson>
}
export interface CommentCheckStructuredJson extends CommentCheckJson {
rules: Array<RuleSetObjectJson | RuleObjectJson>
actions: Array<ActionObjectJson>
}

View File

@@ -3,46 +3,98 @@
* @pattern ^(-?)P(?=\d|T\d)(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)([DW]))?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+(?:\.\d+)?)S)?)?$
* */
export type ISO8601 = string;
export type ActivityWindowType = Duration | number | ActivityWindowCriteria;
export type Duration = ISO8601 | DurationObject;
export type ActivityWindowType = ActivityWindowCriteria | DurationVal | number;
export type DurationVal = ISO8601 | DurationObject;
/**
* If both properties are defined then the first criteria met will be used IE if # of activities = count before duration is reached then count will be used, or vice versa
* The criteria used to define what range of Activity to retrieve.
*
* May specify one, or both properties along with the `satisfyOn` property, to affect the retrieval behavior.
*
* @examples [{"count": 100, "duration": {"days": 90}}]
* @minProperties 1
* @additionalProperties false
* */
export interface ActivityWindowCriteria {
/**
* The number of activities (submission/comments) to consider
* @examples [15]
* */
count?: number,
/**
* An ISO 8601 duration or Day.js duration object.
* An [ISO 8601 duration string](https://en.wikipedia.org/wiki/ISO_8601#Durations) or [Day.js duration object](https://day.js.org/docs/en/durations/creating).
*
* The duration will be subtracted from the time when the rule is run to create a time range like this:
*
* endTime = NOW <----> startTime = (NOW - duration)
* endTime = NOW <----> startTime = (NOW - `duration`)
*
* EX endTime = 3:00PM <----> startTime = (NOW - 15 minutes) = 2:45PM -- so look for activities between 2:45PM and 3:00PM
* @examples ["PT1M", {"minutes": 15}]
* EX `PT15M` or `{"minutes": 15}`
* * `endTime` = NOW (3:00PM)
* * `startTime` = (NOW - 15 minutes) = 2:45PM
*
* So look for Activities between 2:45PM and 3:00PM
* @examples ["PT15M", {"minutes": 15}]
* */
duration?: Duration
duration?: DurationVal
/**
* Define the condition under which both criteria are considered met
*
* **If `any` then it will retrieve Activities until one of the criteria is met, whichever occurs first**
*
* EX `{count: 100, duration: {days: 90}}`:
* * If 90 days of activities = 40 activities => returns 40 activities
* * If 100 activities is only 20 days => 100 activities
*
* **If `all` then both criteria must be met.**
*
* Effectively, whichever criteria produces the most Activities...
*
* EX `{count: 100, duration: {days: 90}}`:
* * If at 90 days of activities => 40 activities, continue retrieving results until 100 => results in >90 days of activities
* * If at 100 activities => 20 days of activities, continue retrieving results until 90 days => results in >100 activities
*
* @examples ["any"]
* @default any
* */
satisfyOn?: 'any' | 'all';
}
/**
* A Day.js duration object
* A [Day.js duration object](https://day.js.org/docs/en/durations/creating)
*
* https://day.js.org/docs/en/durations/creating
* @examples [{"minutes": 30, "hours": 1}]
* @minProperties 1
* @additionalProperties false
* */
export interface DurationObject {
/**
* @examples [15]
* */
seconds?: number
/**
* @examples [50]
* */
minutes?: number
/**
* @examples [4]
* */
hours?: number
/**
* @examples [7]
* */
days?: number
/**
* @examples [2]
* */
weeks?: number
/**
* @examples [3]
* */
months?: number
/**
* @examples [0]
* */
years?: number
}
@@ -71,10 +123,8 @@ export interface ActivityWindow {
/**
* Criteria for defining what set of activities should be considered.
*
* The value of this property may be either count OR duration -- to use both write it as an ActivityWindowCriteria
* The value of this property may be either count OR duration -- to use both write it as an `ActivityWindowCriteria`
*
* See ActivityWindowCriteria for descriptions of what count/duration do
* @examples require('./interfaces.ts').windowExample
* @default 15
*/
window?: ActivityWindowType,
@@ -92,17 +142,21 @@ export interface RichContent {
/**
* The Content to submit for this Action. Content is interpreted as reddit-flavored Markdown.
*
* If value starts with 'wiki:' then the proceeding value will be used to get a wiki page
* If value starts with `wiki:` then the proceeding value will be used to get a wiki page
*
* EX "wiki:botconfig/mybot" tries to get https://reddit.com/mySubredditExample/wiki/botconfig/mybot
* EX `wiki:botconfig/mybot` tries to get `https://reddit.com/mySubredditExample/wiki/botconfig/mybot`
*
* EX "this is plain text"
* EX `this is plain text` => "this is plain text"
*
* EX "this is **bold** markdown text"
* EX `this is **bold** markdown text` => "this is **bold** markdown text"
*
* @examples ["this is plain text", "this is **bold** markdown text", "wiki:botconfig/acomment" ]
* @examples ["This is the content of a comment/report/usernote", "this is **bold** markdown text", "wiki:botconfig/acomment" ]
* */
content: string,
content?: string,
}
export interface RequiredRichContent extends RichContent {
content: string
}
/**
@@ -115,7 +169,14 @@ export interface RichContent {
export type SubredditList = string[];
export interface SubredditCriteria {
subreddits: SubredditList
/**
* A list of subreddits (case-insensitive) to look for. Do not include "r/" prefix.
*
* EX to match against /r/mealtimevideos and /r/askscience use ["mealtimevideos","askscience"]
* @examples [["mealtimevideos","askscience"]]
* @minItems 2
* */
subreddits: string[]
}
export type JoinOperands = 'OR' | 'AND';
@@ -129,43 +190,51 @@ export interface JoinCondition {
* If `AND` then **all** `Rule` objects must be triggered to result in success.
*
* @default "AND"
* @examples ["AND"]
* */
condition?: JoinOperands,
}
/**
* You may specify polling options independently for submissions/comments
* @examples [{"submissions": {"limit": 10, "interval": 10000}, "comments": {"limit": 15, "interval": 10000}}]
* */
export interface PollingOptions {
/**
* Polling options for submission events
* @examples [{"limit": 10, "interval": 10000}]
* */
submissions?: {
/**
* The number of submissions to pull from /r/subreddit/new on every request
* @default 10
* @examples [10]
* */
limit?: number,
/**
* Amount of time, in milliseconds, to wait between requests to /r/subreddit/new
*
* @default 10000
* @examples [10000]
* */
interval?: number,
},
/**
* Polling options for comment events
* @examples [{"limit": 10, "interval": 10000}]
* */
comments?: {
/**
* The number of new comments to pull on every request
* @default 10
* @examples [10]
* */
limit?: number,
/**
* Amount of time, in milliseconds, to wait between requests for new comments
*
* @default 10000
* @examples [10000]
* */
interval?: number,
}
@@ -174,20 +243,38 @@ export interface PollingOptions {
export interface SubredditCacheConfig {
/**
* Amount of time, in milliseconds, author activities (Comments/Submission) should be cached
* @examples [10000]
* @default 10000
* */
authorTTL?: number;
/**
* Amount of time, in milliseconds, wiki content pages should be cached
* @examples [300000]
* @default 300000
* */
wikiTTL?: number;
/**
* Amount of time, in milliseconds, [Toolbox User Notes](https://www.reddit.com/r/toolbox/wiki/docs/usernotes) should be cached
* @examples [60000]
* @default 60000
* */
userNotesTTL?: number;
}
export interface ManagerOptions {
polling?: PollingOptions
/**
* Per-subreddit config for caching TTL values. If set to `false` caching is disabled.
* */
caching?: false | SubredditCacheConfig
/**
* Use this option to override the `dryRun` setting for all `Checks`
*
* @default undefined
* @examples [false,true]
* */
dryRun?: boolean;
}
@@ -200,8 +287,52 @@ export interface ThresholdCriteria {
* * If `threshold` is a `string` with percentage (EX `40%`) then it is the percentage of the total this item must reach to trigger
*
* @default 10%
* @examples ["10%", 15]
* */
threshold: number | string
/**
* @examples [">",">=","<","<="]
* */
condition: '>' | '>=' | '<' | '<='
}
export interface ChecksActivityState {
itemIs?: TypedActivityStates
}
export interface ActivityState {
removed?: boolean
locked?: boolean
spam?: boolean
stickied?: boolean
distinguished?: boolean
approved?: boolean
}
/**
* Different attributes a `Submission` can be in. Only include a property if you want to check it.
* @examples [{"over_18": true, "removed": false}]
* */
export interface SubmissionState extends ActivityState {
pinned?: boolean
spoiler?: boolean
/**
* NSFW
* */
over_18?: boolean
is_self?: boolean
}
/**
* Different attributes a `Comment` can be in. Only include a property if you want to check it.
* @examples [{"op": true, "removed": false}]
* */
export interface CommentState extends ActivityState {
/*
* Is this Comment Author also the Author of the Submission this comment is in?
* */
op?: boolean
}
export type TypedActivityStates = SubmissionState[] | CommentState[];

View File

@@ -8,9 +8,12 @@ import {ReportActionJson} from "../Action/ReportAction";
import {LockActionJson} from "../Action/LockAction";
import {RemoveActionJson} from "../Action/RemoveAction";
import {HistoryJSONConfig} from "../Rule/HistoryRule";
import {UserNoteActionJson} from "../Action/UserNoteAction";
import {ApproveActionJson} from "../Action/ApproveAction";
import {BanActionJson} from "../Action/BanAction";
export type RuleJson = RecentActivityRuleJSONConfig | RepeatActivityJSONConfig | AuthorRuleJSONConfig | AttributionJSONConfig | HistoryJSONConfig | string;
export type RuleObjectJson = Exclude<RuleJson, string>
export type ActionJson = FlairActionJson | CommentActionJson | ReportActionJson | LockActionJson | RemoveActionJson | string;
export type ActionJson = CommentActionJson | FlairActionJson | ReportActionJson | LockActionJson | RemoveActionJson | ApproveActionJson | BanActionJson | UserNoteActionJson | string;
export type ActionObjectJson = Exclude<ActionJson, string>;

View File

@@ -47,7 +47,7 @@ export class ConfigBuilder {
this.configLogger.error(`${err.keyword}: ${err.schemaPath} => ${err.message}${suffix}`);
}
}
throw new LoggedError();
throw new LoggedError('Config schema validity failure');
}
}
@@ -56,13 +56,15 @@ export class ConfigBuilder {
let namedActions: Map<string, ActionObjectJson> = new Map();
const {checks = []} = config;
for (const c of checks) {
namedRules = extractNamedRules(c.rules, namedRules);
const { rules = [] } = c;
namedRules = extractNamedRules(rules, namedRules);
namedActions = extractNamedActions(c.actions, namedActions);
}
const structuredChecks: CheckStructuredJson[] = [];
for (const c of checks) {
const strongRules = insertNamedRules(c.rules, namedRules);
const { rules = [] } = c;
const strongRules = insertNamedRules(rules, namedRules);
const strongActions = insertNamedActions(c.actions, namedActions);
const strongCheck = {...c, rules: strongRules, actions: strongActions} as CheckStructuredJson;
structuredChecks.push(strongCheck);

View File

@@ -1,4 +1,4 @@
import {CheckJson} from "./Check";
import {CheckJson, CommentCheckJson, SubmissionCheckJson} from "./Check";
import {ManagerOptions} from "./Common/interfaces";
export interface JSONConfig extends ManagerOptions {
@@ -12,5 +12,5 @@ export interface JSONConfig extends ManagerOptions {
* When a check "passes", and actions are performed, then all subsequent checks are skipped.
* @minItems 1
* */
checks: CheckJson[]
checks: Array<SubmissionCheckJson|CommentCheckJson>
}

View File

@@ -56,14 +56,14 @@ export class AuthorRule extends Rule {
protected async process(item: Comment | Submission): Promise<[boolean, RuleResult[]]> {
if (this.include.length > 0) {
for (const auth of this.include) {
if (await this.cache.testAuthorCriteria(item, auth)) {
if (await this.resources.testAuthorCriteria(item, auth)) {
return Promise.resolve([true, [this.getResult(true)]]);
}
}
return Promise.resolve([false, [this.getResult(false)]]);
}
for (const auth of this.exclude) {
if (await this.cache.testAuthorCriteria(item, auth, false)) {
if (await this.resources.testAuthorCriteria(item, auth, false)) {
return Promise.resolve([true, [this.getResult(true)]]);
}
}

View File

@@ -80,7 +80,7 @@ export class HistoryRule extends Rule {
const {comment, window, submission, minActivityCount = 5} = criteria;
let activities = await this.cache.getAuthorActivities(item.author, {window: window});
let activities = await this.resources.getAuthorActivities(item.author, {window: window});
activities = activities.filter(act => {
if (this.include.length > 0) {
return this.include.some(x => x === act.subreddit.display_name.toLowerCase());

View File

@@ -49,13 +49,13 @@ export class RecentActivityRule extends Rule {
switch (this.lookAt) {
case 'comments':
activities = await this.cache.getAuthorComments(item.author, {window: this.window});
activities = await this.resources.getAuthorComments(item.author, {window: this.window});
break;
case 'submissions':
activities = await this.cache.getAuthorSubmissions(item.author, {window: this.window});
activities = await this.resources.getAuthorSubmissions(item.author, {window: this.window});
break;
default:
activities = await this.cache.getAuthorActivities(item.author, {window: this.window});
activities = await this.resources.getAuthorActivities(item.author, {window: this.window});
break;
}
@@ -165,11 +165,13 @@ export interface SubThreshold extends SubredditCriteria {
/**
* The number of activities in each subreddit from the list that will trigger this rule
* @minimum 1
* @examples [1]
* */
count?: number,
/**
* The total number of activities across all listed subreddits that will trigger this rule
* @minimum 1
* @examples [1]
* */
totalCount?: number
}
@@ -177,6 +179,7 @@ export interface SubThreshold extends SubredditCriteria {
interface RecentActivityConfig extends ActivityWindow, ReferenceSubmission {
/**
* If present restricts the activities that are considered for count from SubThreshold
* @examples ["submissions","comments"]
* */
lookAt?: 'comments' | 'submissions',
/**
@@ -201,6 +204,9 @@ export interface RecentActivityRuleOptions extends RecentActivityConfig, RuleOpt
* ```
* */
export interface RecentActivityRuleJSONConfig extends RecentActivityConfig, RuleJSONConfig {
/**
* @examples ["recentActivity"]
* */
kind: 'recentActivity'
}

View File

@@ -57,6 +57,11 @@ export class RuleSet implements IRuleSet, Triggerable {
if (!runOne) {
return [false, results];
}
if(this.condition === 'OR') {
// if OR and did not return already then none passed
return [false, results];
}
// otherwise AND and did not return already so all passed
return [true, results];
}
}

View File

@@ -114,7 +114,7 @@ export class AttributionRule extends SubmissionRule {
percentVal = Number.parseInt(threshold.replace('%', '')) / 100;
}
let activities = thresholdOn === 'submissions' ? await this.cache.getAuthorSubmissions(item.author, {window: window}) : await this.cache.getAuthorActivities(item.author, {window: window});
let activities = thresholdOn === 'submissions' ? await this.resources.getAuthorSubmissions(item.author, {window: window}) : await this.resources.getAuthorActivities(item.author, {window: window});
activities = activities.filter(act => {
if (this.include.length > 0) {
return this.include.some(x => x === act.subreddit.display_name.toLowerCase());

View File

@@ -79,16 +79,17 @@ export class RepeatActivityRule extends SubmissionRule {
async process(item: Submission): Promise<[boolean, RuleResult[]]> {
const referenceUrl = await item.url;
if (referenceUrl === undefined && this.useSubmissionAsReference) {
throw new Error(`Cannot run Rule ${this.name} because submission is not a link`);
this.logger.warn(`Rule not triggered because useSubmissionAsReference=true but submission is not a link`);
return Promise.resolve([false, [this.getResult(false)]]);
}
let activities: (Submission | Comment)[] = [];
switch (this.lookAt) {
case 'submissions':
activities = await this.cache.getAuthorSubmissions(item.author, {window: this.window});
activities = await this.resources.getAuthorSubmissions(item.author, {window: this.window});
break;
default:
activities = await this.cache.getAuthorActivities(item.author, {window: this.window});
activities = await this.resources.getAuthorActivities(item.author, {window: this.window});
break;
}

View File

@@ -2,12 +2,14 @@ import {Comment} from "snoowrap";
import Submission from "snoowrap/dist/objects/Submission";
import {Logger} from "winston";
import {findResultByPremise, mergeArr} from "../util";
import {testAuthorCriteria} from "../Utils/SnoowrapUtils";
import CacheManager, {SubredditCache} from "../Subreddit/SubredditCache";
import ResourceManager, {SubredditResources} from "../Subreddit/SubredditResources";
import {ChecksActivityState, TypedActivityStates} from "../Common/interfaces";
import {isItem} from "../Utils/SnoowrapUtils";
export interface RuleOptions {
name?: string;
authors?: AuthorOptions;
authorIs?: AuthorOptions;
itemIs?: TypedActivityStates;
logger: Logger
subredditName: string;
}
@@ -35,29 +37,32 @@ export interface Triggerable {
export abstract class Rule implements IRule, Triggerable {
name: string;
logger: Logger
authors: AuthorOptions;
cache: SubredditCache;
authorIs: AuthorOptions;
itemIs: TypedActivityStates;
resources: SubredditResources;
constructor(options: RuleOptions) {
const {
name = this.getKind(),
logger,
authors: {
authorIs: {
include = [],
exclude = [],
} = {},
itemIs = [],
subredditName,
} = options;
this.name = name;
this.cache = CacheManager.get(subredditName);
this.resources = ResourceManager.get(subredditName) as SubredditResources;
this.authors = {
this.authorIs = {
exclude: exclude.map(x => new Author(x)),
include: include.map(x => new Author(x)),
}
const ruleUniqueName = this.name === undefined ? this.getKind() : `${this.getKind()} - ${this.name}`;
this.logger = logger.child({labels: ['Rule',`${ruleUniqueName}`]}, mergeArr);
this.itemIs = itemIs;
this.logger = logger.child({labels: ['Rule',`${this.getRuleUniqueName()}`]}, mergeArr);
}
async run(item: Comment | Submission, existingResults: RuleResult[] = []): Promise<[(boolean | null), RuleResult[]]> {
@@ -66,23 +71,28 @@ export abstract class Rule implements IRule, Triggerable {
this.logger.debug(`Returning existing result of ${existingResult.triggered ? '✔️' : '❌'}`);
return Promise.resolve([existingResult.triggered, [{...existingResult, name: this.name}]]);
}
if (this.authors.include !== undefined && this.authors.include.length > 0) {
for (const auth of this.authors.include) {
if (await this.cache.testAuthorCriteria(item, auth)) {
const [itemPass, crit] = isItem(item, this.itemIs, this.logger);
if(!itemPass) {
this.logger.verbose(`Item did not pass 'itemIs' test, rule running skipped`);
return Promise.resolve([null, [this.getResult(null, {result: `Item did not pass 'itemIs' test, rule running skipped`})]]);
}
if (this.authorIs.include !== undefined && this.authorIs.include.length > 0) {
for (const auth of this.authorIs.include) {
if (await this.resources.testAuthorCriteria(item, auth)) {
return this.process(item);
}
}
this.logger.verbose('Inclusive author criteria not matched, rule running skipped');
return Promise.resolve([false, [this.getResult(null, {result: 'Inclusive author criteria not matched, rule running skipped'})]]);
return Promise.resolve([null, [this.getResult(null, {result: 'Inclusive author criteria not matched, rule running skipped'})]]);
}
if (this.authors.exclude !== undefined && this.authors.exclude.length > 0) {
for (const auth of this.authors.exclude) {
if (await this.cache.testAuthorCriteria(item, auth, false)) {
if (this.authorIs.exclude !== undefined && this.authorIs.exclude.length > 0) {
for (const auth of this.authorIs.exclude) {
if (await this.resources.testAuthorCriteria(item, auth, false)) {
return this.process(item);
}
}
this.logger.verbose('Exclusive author criteria not matched, rule running skipped');
return Promise.resolve([false, [this.getResult(null, {result: 'Exclusive author criteria not matched, rule running skipped'})]]);
return Promise.resolve([null, [this.getResult(null, {result: 'Exclusive author criteria not matched, rule running skipped'})]]);
}
return this.process(item);
}
@@ -91,6 +101,10 @@ export abstract class Rule implements IRule, Triggerable {
abstract getKind(): string;
getRuleUniqueName() {
return this.name === undefined ? this.getKind() : `${this.getKind()} - ${this.name}`;
}
protected abstract getSpecificPremise(): object;
getPremise(): RulePremise {
@@ -98,7 +112,8 @@ export abstract class Rule implements IRule, Triggerable {
return {
kind: this.getKind(),
config: {
authors: this.authors,
authorIs: this.authorIs,
itemIs: this.itemIs,
...config,
},
};
@@ -119,20 +134,49 @@ export class Author implements AuthorCriteria {
flairCssClass?: string[];
flairText?: string[];
isMod?: boolean;
userNotes?: UserNoteCriteria[];
constructor(options: AuthorCriteria) {
this.name = options.name;
this.flairCssClass = options.flairCssClass;
this.flairText = options.flairText;
this.isMod = options.isMod;
this.userNotes = options.userNotes;
}
}
export interface UserNoteCriteria {
/**
* User Note type key
* @examples ["spamwarn"]
* */
type: string;
/**
* Number of occurrences of this type. Ignored if `search` is `current`
* @examples [1]
* @default 1
* */
count?: number;
/**
* * If `current` then only the most recent note is checked
* * If `consecutive` then `count` number of `type` notes must be found in a row, based on `order` direction
* * If `total` then `count` number of `type` must be found within all notes
* @examples ["current"]
* @default current
* */
search?: 'current' | 'consecutive' | 'total'
/**
* Time-based order to search Notes in for `consecutive` search
* @examples ["descending"]
* @default descending
* */
order?: 'ascending' | 'descending'
}
/**
* If present then these Author criteria are checked before running the rule. If criteria fails then the rule is skipped.
* @minProperties 1
* @additionalProperties false
* @TJS-type object
* @examples [{"include": [{"flairText": ["Contributor","Veteran"]}, {"isMod": true}]}]
* */
export interface AuthorOptions {
/**
@@ -153,6 +197,7 @@ export interface AuthorOptions {
*
* @minProperties 1
* @additionalProperties false
* @examples [{"flairText": ["Contributor","Veteran"], "isMod": true, "name": ["FoxxMD", "AnotherUser"] }]
* */
export interface AuthorCriteria {
/**
@@ -164,19 +209,25 @@ export interface AuthorCriteria {
name?: string[],
/**
* A list of (user) flair css class values from the subreddit to match against
* @examples ["red"]
* */
flairCssClass?: string[],
/**
* A list of (user) flair text values from the subreddit to match against
* @examples ["Approved"]
* */
flairText?: string[],
/**
* Is the author a moderator?
* */
isMod?: boolean,
/**
* A list of UserNote properties to check against the User Notes attached to this Author in this Subreddit (must have Toolbox enabled and used User Notes at least once)
* */
userNotes?: UserNoteCriteria[]
}
export interface IRule {
export interface IRule extends ChecksActivityState {
/**
* An optional, but highly recommended, friendly name for this rule. If not present will default to `kind`.
*
@@ -184,17 +235,26 @@ export interface IRule {
*
* name is used to reference Rule result data during Action content templating. See CommentAction or ReportAction for more details.
* @pattern ^[a-zA-Z]([\w -]*[\w])?$
* @examples ["myNewRule"]
* */
name?: string
/**
* If present then these Author criteria are checked before running the rule. If criteria fails then the rule is skipped.
* */
authors?: AuthorOptions
authorIs?: AuthorOptions
/**
* A list of criteria to test the state of the `Activity` against before running the Rule.
*
* If any set of criteria passes the Rule will be run. If the criteria fails then the Rule is skipped.
*
* */
itemIs?: TypedActivityStates
}
export interface RuleJSONConfig extends IRule {
/**
* The kind of rule to run
* @examples ["recentActivity", "repeatActivity", "author", "attribution", "history"]
*/
kind: 'recentActivity' | 'repeatActivity' | 'author' | 'attribution' | 'history'
}

View File

@@ -4,21 +4,31 @@
"dryRun": {
"default": false,
"description": "If `true` the Action will not make the API request to Reddit to perform its action.",
"examples": [
false,
true
],
"type": "boolean"
},
"kind": {
"description": "The type of action that will be performed",
"enum": [
"approve",
"ban",
"comment",
"flair",
"lock",
"remove",
"report"
"report",
"usernote"
],
"type": "string"
},
"name": {
"description": "An optional, but highly recommended, friendly name for this Action. If not present will default to `kind`.\n\nCan only contain letters, numbers, underscore, spaces, and dashes",
"examples": [
"myDescriptiveAction"
],
"pattern": "^[a-zA-Z]([\\w -]*[\\w])?$",
"type": "string"
}

File diff suppressed because it is too large Load Diff

View File

@@ -23,11 +23,22 @@
"definitions": {
"ActivityWindowCriteria": {
"additionalProperties": false,
"description": "If both properties are defined then the first criteria met will be used IE if # of activities = count before duration is reached then count will be used, or vice versa",
"description": "The criteria used to define what range of Activity to retrieve.\n\nMay specify one, or both properties along with the `satisfyOn` property, to affect the retrieval behavior.",
"examples": [
{
"count": 100,
"duration": {
"days": 90
}
}
],
"minProperties": 1,
"properties": {
"count": {
"description": "The number of activities (submission/comments) to consider",
"examples": [
15
],
"type": "number"
},
"duration": {
@@ -39,13 +50,25 @@
"type": "string"
}
],
"description": "An ISO 8601 duration or Day.js duration object.\n\nThe duration will be subtracted from the time when the rule is run to create a time range like this:\n\nendTime = NOW <----> startTime = (NOW - duration)\n\nEX endTime = 3:00PM <----> startTime = (NOW - 15 minutes) = 2:45PM -- so look for activities between 2:45PM and 3:00PM",
"description": "An [ISO 8601 duration string](https://en.wikipedia.org/wiki/ISO_8601#Durations) or [Day.js duration object](https://day.js.org/docs/en/durations/creating).\n\nThe duration will be subtracted from the time when the rule is run to create a time range like this:\n\nendTime = NOW <----> startTime = (NOW - `duration`)\n\nEX `PT15M` or `{\"minutes\": 15}`\n* `endTime` = NOW (3:00PM)\n* `startTime` = (NOW - 15 minutes) = 2:45PM\n\nSo look for Activities between 2:45PM and 3:00PM",
"examples": [
"PT1M",
"PT15M",
{
"minutes": 15
}
]
},
"satisfyOn": {
"default": "any",
"description": "Define the condition under which both criteria are considered met\n\n**If `any` then it will retrieve Activities until one of the criteria is met, whichever occurs first**\n\nEX `{count: 100, duration: {days: 90}}`:\n* If 90 days of activities = 40 activities => returns 40 activities\n* If 100 activities is only 20 days => 100 activities\n\n**If `all` then both criteria must be met.**\n\nEffectively, whichever criteria produces the most Activities...\n\nEX `{count: 100, duration: {days: 90}}`:\n* If at 90 days of activities => 40 activities, continue retrieving results until 100 => results in >90 days of activities\n* If at 100 activities => 20 days of activities, continue retrieving results until 90 days => results in >100 activities",
"enum": [
"all",
"any"
],
"examples": [
"any"
],
"type": "string"
}
},
"type": "object"
@@ -80,10 +103,10 @@
"window": {
"anyOf": [
{
"$ref": "#/definitions/DurationObject"
"$ref": "#/definitions/ActivityWindowCriteria"
},
{
"$ref": "#/definitions/ActivityWindowCriteria"
"$ref": "#/definitions/DurationObject"
},
{
"type": [
@@ -108,11 +131,24 @@
"description": "Should the rule aggregate recognized media domains into the parent domain?\n\nSubmissions to major media domains (youtube, vimeo) can be identified by individual Channel/Author...\n\n* If `false` then aggregate will occur at the channel level IE Youtube Channel A (2 counts), Youtube Channel B (3 counts)\n* If `true` then then aggregation will occur at the domain level IE youtube.com (5 counts)",
"type": "boolean"
},
"authors": {
"additionalProperties": false,
"authorIs": {
"$ref": "#/definitions/AuthorOptions",
"description": "If present then these Author criteria are checked before running the rule. If criteria fails then the rule is skipped.",
"minProperties": 1,
"type": "object"
"examples": [
{
"include": [
{
"flairText": [
"Contributor",
"Veteran"
]
},
{
"isMod": true
}
]
}
]
},
"criteria": {
"description": "A list threshold-window values to test attribution against\n\nIf none is provided the default set used is:\n\n```\nthreshold: 10%\nwindow: 100\n```",
@@ -159,6 +195,23 @@
"description": "Include reddit `self.*` domains in aggregation?\n\nSelf-posts are aggregated under the domain `self.[subreddit]`. If you wish to include these domains in aggregation set this to `true`",
"type": "boolean"
},
"itemIs": {
"anyOf": [
{
"items": {
"$ref": "#/definitions/SubmissionState"
},
"type": "array"
},
{
"items": {
"$ref": "#/definitions/CommentState"
},
"type": "array"
}
],
"description": "A list of criteria to test the state of the `Activity` against before running the Rule.\n\nIf any set of criteria passes the Rule will be run. If the criteria fails then the Rule is skipped."
},
"kind": {
"description": "The kind of rule to run",
"enum": [
@@ -177,6 +230,9 @@
},
"name": {
"description": "An optional, but highly recommended, friendly name for this rule. If not present will default to `kind`.\n\nCan only contain letters, numbers, underscore, spaces, and dashes\n\nname is used to reference Rule result data during Action content templating. See CommentAction or ReportAction for more details.",
"examples": [
"myNewRule"
],
"pattern": "^[a-zA-Z]([\\w -]*[\\w])?$",
"type": "string"
},
@@ -194,10 +250,26 @@
"AuthorCriteria": {
"additionalProperties": false,
"description": "Criteria with which to test against the author of an Activity. The outcome of the test is based on:\n\n1. All present properties passing and\n2. If a property is a list then any value from the list matching",
"examples": [
{
"flairText": [
"Contributor",
"Veteran"
],
"isMod": true,
"name": [
"FoxxMD",
"AnotherUser"
]
}
],
"minProperties": 1,
"properties": {
"flairCssClass": {
"description": "A list of (user) flair css class values from the subreddit to match against",
"examples": [
"red"
],
"items": {
"type": "string"
},
@@ -205,6 +277,9 @@
},
"flairText": {
"description": "A list of (user) flair text values from the subreddit to match against",
"examples": [
"Approved"
],
"items": {
"type": "string"
},
@@ -224,17 +299,72 @@
"type": "string"
},
"type": "array"
},
"userNotes": {
"description": "A list of UserNote properties to check against the User Notes attached to this Author in this Subreddit (must have Toolbox enabled and used User Notes at least once)",
"items": {
"$ref": "#/definitions/UserNoteCriteria"
},
"type": "array"
}
},
"type": "object"
},
"AuthorOptions": {
"description": "If present then these Author criteria are checked before running the rule. If criteria fails then the rule is skipped.",
"examples": [
{
"include": [
{
"flairText": [
"Contributor",
"Veteran"
]
},
{
"isMod": true
}
]
}
],
"properties": {
"exclude": {
"description": "Only runs if include is not present. Will \"pass\" if any of set of the AuthorCriteria does not pass",
"items": {
"$ref": "#/definitions/AuthorCriteria"
},
"type": "array"
},
"include": {
"description": "Will \"pass\" if any set of AuthorCriteria passes",
"items": {
"$ref": "#/definitions/AuthorCriteria"
},
"type": "array"
}
},
"type": "object"
},
"AuthorRuleJSONConfig": {
"properties": {
"authors": {
"additionalProperties": false,
"authorIs": {
"$ref": "#/definitions/AuthorOptions",
"description": "If present then these Author criteria are checked before running the rule. If criteria fails then the rule is skipped.",
"minProperties": 1,
"type": "object"
"examples": [
{
"include": [
{
"flairText": [
"Contributor",
"Veteran"
]
},
{
"isMod": true
}
]
}
]
},
"exclude": {
"description": "Only runs if include is not present. Will \"pass\" if any of set of the AuthorCriteria does not pass",
@@ -250,6 +380,23 @@
},
"type": "array"
},
"itemIs": {
"anyOf": [
{
"items": {
"$ref": "#/definitions/SubmissionState"
},
"type": "array"
},
{
"items": {
"$ref": "#/definitions/CommentState"
},
"type": "array"
}
],
"description": "A list of criteria to test the state of the `Activity` against before running the Rule.\n\nIf any set of criteria passes the Rule will be run. If the criteria fails then the Rule is skipped."
},
"kind": {
"description": "The kind of rule to run",
"enum": [
@@ -259,6 +406,9 @@
},
"name": {
"description": "An optional, but highly recommended, friendly name for this rule. If not present will default to `kind`.\n\nCan only contain letters, numbers, underscore, spaces, and dashes\n\nname is used to reference Rule result data during Action content templating. See CommentAction or ReportAction for more details.",
"examples": [
"myNewRule"
],
"pattern": "^[a-zA-Z]([\\w -]*[\\w])?$",
"type": "string"
}
@@ -270,6 +420,39 @@
],
"type": "object"
},
"CommentState": {
"description": "Different attributes a `Comment` can be in. Only include a property if you want to check it.",
"examples": [
{
"op": true,
"removed": false
}
],
"properties": {
"approved": {
"type": "boolean"
},
"distinguished": {
"type": "boolean"
},
"locked": {
"type": "boolean"
},
"op": {
"type": "boolean"
},
"removed": {
"type": "boolean"
},
"spam": {
"type": "boolean"
},
"stickied": {
"type": "boolean"
}
},
"type": "object"
},
"CommentThresholdCriteria": {
"properties": {
"asOp": {
@@ -283,11 +466,21 @@
">",
">="
],
"examples": [
">",
">=",
"<",
"<="
],
"type": "string"
},
"threshold": {
"default": "10%",
"description": "The number or percentage to trigger this criteria at\n\n* If `threshold` is a `number` then it is the absolute number of items to trigger at\n* If `threshold` is a `string` with percentage (EX `40%`) then it is the percentage of the total this item must reach to trigger",
"examples": [
"10%",
15
],
"type": [
"string",
"number"
@@ -302,28 +495,55 @@
},
"DurationObject": {
"additionalProperties": false,
"description": "A Day.js duration object\n\nhttps://day.js.org/docs/en/durations/creating",
"description": "A [Day.js duration object](https://day.js.org/docs/en/durations/creating)",
"examples": [
{
"hours": 1,
"minutes": 30
}
],
"minProperties": 1,
"properties": {
"days": {
"examples": [
7
],
"type": "number"
},
"hours": {
"examples": [
4
],
"type": "number"
},
"minutes": {
"examples": [
50
],
"type": "number"
},
"months": {
"examples": [
3
],
"type": "number"
},
"seconds": {
"examples": [
15
],
"type": "number"
},
"weeks": {
"examples": [
2
],
"type": "number"
},
"years": {
"examples": [
0
],
"type": "number"
}
},
@@ -349,10 +569,10 @@
"window": {
"anyOf": [
{
"$ref": "#/definitions/DurationObject"
"$ref": "#/definitions/ActivityWindowCriteria"
},
{
"$ref": "#/definitions/ActivityWindowCriteria"
"$ref": "#/definitions/DurationObject"
},
{
"type": [
@@ -372,11 +592,24 @@
"HistoryJSONConfig": {
"description": "Aggregates an Author's submission and comment history. Rule can be triggered on count/percent of total (for either or both comment/sub totals) as well as comment OP total.\n\nAvailable data for [Action templating](https://github.com/FoxxMD/reddit-context-bot#action-templating):\n\n```\nactivityTotal => Total number of activities\nsubmissionTotal => Total number of submissions\ncommentTotal => Total number of comments\nopTotal => Total number of comments as OP\nthresholdSummary => A text summary of the first Criteria triggered with totals/percentages\ncriteria => The ThresholdCriteria object\nwindow => A text summary of the range of Activities considered (# of Items if number, time range if Duration)\n```",
"properties": {
"authors": {
"additionalProperties": false,
"authorIs": {
"$ref": "#/definitions/AuthorOptions",
"description": "If present then these Author criteria are checked before running the rule. If criteria fails then the rule is skipped.",
"minProperties": 1,
"type": "object"
"examples": [
{
"include": [
{
"flairText": [
"Contributor",
"Veteran"
]
},
{
"isMod": true
}
]
}
]
},
"criteria": {
"description": "A list threshold-window values to test activities against.",
@@ -418,6 +651,23 @@
"minItems": 1,
"type": "array"
},
"itemIs": {
"anyOf": [
{
"items": {
"$ref": "#/definitions/SubmissionState"
},
"type": "array"
},
{
"items": {
"$ref": "#/definitions/CommentState"
},
"type": "array"
}
],
"description": "A list of criteria to test the state of the `Activity` against before running the Rule.\n\nIf any set of criteria passes the Rule will be run. If the criteria fails then the Rule is skipped."
},
"kind": {
"description": "The kind of rule to run",
"enum": [
@@ -427,6 +677,9 @@
},
"name": {
"description": "An optional, but highly recommended, friendly name for this rule. If not present will default to `kind`.\n\nCan only contain letters, numbers, underscore, spaces, and dashes\n\nname is used to reference Rule result data during Action content templating. See CommentAction or ReportAction for more details.",
"examples": [
"myNewRule"
],
"pattern": "^[a-zA-Z]([\\w -]*[\\w])?$",
"type": "string"
}
@@ -440,17 +693,50 @@
"RecentActivityRuleJSONConfig": {
"description": "Checks a user's history for any Activity (Submission/Comment) in the subreddits specified in thresholds\n\nAvailable data for [Action templating](https://github.com/FoxxMD/reddit-context-bot#action-templating):\n\n```\nsummary => comma-deliminated list of subreddits that hit the threshold and their count EX subredditA(1), subredditB(4),...\nsubCount => Total number of subreddits that hit the threshold\ntotalCount => Total number of all activity occurrences in subreddits\n```",
"properties": {
"authors": {
"additionalProperties": false,
"authorIs": {
"$ref": "#/definitions/AuthorOptions",
"description": "If present then these Author criteria are checked before running the rule. If criteria fails then the rule is skipped.",
"minProperties": 1,
"type": "object"
"examples": [
{
"include": [
{
"flairText": [
"Contributor",
"Veteran"
]
},
{
"isMod": true
}
]
}
]
},
"itemIs": {
"anyOf": [
{
"items": {
"$ref": "#/definitions/SubmissionState"
},
"type": "array"
},
{
"items": {
"$ref": "#/definitions/CommentState"
},
"type": "array"
}
],
"description": "A list of criteria to test the state of the `Activity` against before running the Rule.\n\nIf any set of criteria passes the Rule will be run. If the criteria fails then the Rule is skipped."
},
"kind": {
"description": "The kind of rule to run",
"enum": [
"recentActivity"
],
"examples": [
"recentActivity"
],
"type": "string"
},
"lookAt": {
@@ -459,10 +745,17 @@
"comments",
"submissions"
],
"examples": [
"submissions",
"comments"
],
"type": "string"
},
"name": {
"description": "An optional, but highly recommended, friendly name for this rule. If not present will default to `kind`.\n\nCan only contain letters, numbers, underscore, spaces, and dashes\n\nname is used to reference Rule result data during Action content templating. See CommentAction or ReportAction for more details.",
"examples": [
"myNewRule"
],
"pattern": "^[a-zA-Z]([\\w -]*[\\w])?$",
"type": "string"
},
@@ -482,10 +775,10 @@
"window": {
"anyOf": [
{
"$ref": "#/definitions/DurationObject"
"$ref": "#/definitions/ActivityWindowCriteria"
},
{
"$ref": "#/definitions/ActivityWindowCriteria"
"$ref": "#/definitions/DurationObject"
},
{
"type": [
@@ -495,25 +788,7 @@
}
],
"default": 15,
"description": "Criteria for defining what set of activities should be considered.\n\nThe value of this property may be either count OR duration -- to use both write it as an ActivityWindowCriteria\n\nSee ActivityWindowCriteria for descriptions of what count/duration do",
"examples": [
15,
"PT1M",
{
"count": 10
},
{
"duration": {
"hours": 5
}
},
{
"count": 5,
"duration": {
"minutes": 15
}
}
]
"description": "Criteria for defining what set of activities should be considered.\n\nThe value of this property may be either count OR duration -- to use both write it as an `ActivityWindowCriteria`"
}
},
"required": [
@@ -525,11 +800,24 @@
"RepeatActivityJSONConfig": {
"description": "Checks a user's history for Submissions with identical content\n\nAvailable data for [Action templating](https://github.com/FoxxMD/reddit-context-bot#action-templating):\n\n```\ncount => Total number of repeat Submissions\nthreshold => The threshold you configured for this Rule to trigger\nurl => Url of the submission that triggered the rule\n```",
"properties": {
"authors": {
"additionalProperties": false,
"authorIs": {
"$ref": "#/definitions/AuthorOptions",
"description": "If present then these Author criteria are checked before running the rule. If criteria fails then the rule is skipped.",
"minProperties": 1,
"type": "object"
"examples": [
{
"include": [
{
"flairText": [
"Contributor",
"Veteran"
]
},
{
"isMod": true
}
]
}
]
},
"exclude": {
"description": "Do not include Submissions from this list of Subreddits.\n\nA list of subreddits (case-insensitive) to look for. Do not include \"r/\" prefix.\n\nEX to match against /r/mealtimevideos and /r/askscience use [\"mealtimevideos\",\"askscience\"]",
@@ -559,6 +847,23 @@
"minItems": 1,
"type": "array"
},
"itemIs": {
"anyOf": [
{
"items": {
"$ref": "#/definitions/SubmissionState"
},
"type": "array"
},
{
"items": {
"$ref": "#/definitions/CommentState"
},
"type": "array"
}
],
"description": "A list of criteria to test the state of the `Activity` against before running the Rule.\n\nIf any set of criteria passes the Rule will be run. If the criteria fails then the Rule is skipped."
},
"kind": {
"description": "The kind of rule to run",
"enum": [
@@ -577,6 +882,9 @@
},
"name": {
"description": "An optional, but highly recommended, friendly name for this rule. If not present will default to `kind`.\n\nCan only contain letters, numbers, underscore, spaces, and dashes\n\nname is used to reference Rule result data during Action content templating. See CommentAction or ReportAction for more details.",
"examples": [
"myNewRule"
],
"pattern": "^[a-zA-Z]([\\w -]*[\\w])?$",
"type": "string"
},
@@ -593,10 +901,10 @@
"window": {
"anyOf": [
{
"$ref": "#/definitions/DurationObject"
"$ref": "#/definitions/ActivityWindowCriteria"
},
{
"$ref": "#/definitions/ActivityWindowCriteria"
"$ref": "#/definitions/DurationObject"
},
{
"type": [
@@ -606,25 +914,7 @@
}
],
"default": 15,
"description": "Criteria for defining what set of activities should be considered.\n\nThe value of this property may be either count OR duration -- to use both write it as an ActivityWindowCriteria\n\nSee ActivityWindowCriteria for descriptions of what count/duration do",
"examples": [
15,
"PT1M",
{
"count": 10
},
{
"duration": {
"hours": 5
}
},
{
"count": 5,
"duration": {
"minutes": 15
}
}
]
"description": "Criteria for defining what set of activities should be considered.\n\nThe value of this property may be either count OR duration -- to use both write it as an `ActivityWindowCriteria`"
}
},
"required": [
@@ -639,23 +929,31 @@
"properties": {
"count": {
"description": "The number of activities in each subreddit from the list that will trigger this rule",
"examples": [
1
],
"minimum": 1,
"type": "number"
},
"subreddits": {
"description": "A list of subreddits (case-insensitive) to look for. Do not include \"r/\" prefix.\n\nEX to match against /r/mealtimevideos and /r/askscience use [\"mealtimevideos\",\"askscience\"]",
"examples": [
"mealtimevideos",
"askscience"
[
"mealtimevideos",
"askscience"
]
],
"items": {
"type": "string"
},
"minItems": 1,
"minItems": 2,
"type": "array"
},
"totalCount": {
"description": "The total number of activities across all listed subreddits that will trigger this rule",
"examples": [
1
],
"minimum": 1,
"type": "number"
}
@@ -665,6 +963,49 @@
],
"type": "object"
},
"SubmissionState": {
"description": "Different attributes a `Submission` can be in. Only include a property if you want to check it.",
"examples": [
{
"over_18": true,
"removed": false
}
],
"properties": {
"approved": {
"type": "boolean"
},
"distinguished": {
"type": "boolean"
},
"is_self": {
"type": "boolean"
},
"locked": {
"type": "boolean"
},
"over_18": {
"description": "NSFW",
"type": "boolean"
},
"pinned": {
"type": "boolean"
},
"removed": {
"type": "boolean"
},
"spam": {
"type": "boolean"
},
"spoiler": {
"type": "boolean"
},
"stickied": {
"type": "boolean"
}
},
"type": "object"
},
"ThresholdCriteria": {
"properties": {
"condition": {
@@ -674,11 +1015,21 @@
">",
">="
],
"examples": [
">",
">=",
"<",
"<="
],
"type": "string"
},
"threshold": {
"default": "10%",
"description": "The number or percentage to trigger this criteria at\n\n* If `threshold` is a `number` then it is the absolute number of items to trigger at\n* If `threshold` is a `string` with percentage (EX `40%`) then it is the percentage of the total this item must reach to trigger",
"examples": [
"10%",
15
],
"type": [
"string",
"number"
@@ -690,6 +1041,54 @@
"threshold"
],
"type": "object"
},
"UserNoteCriteria": {
"properties": {
"count": {
"default": 1,
"description": "Number of occurrences of this type. Ignored if `search` is `current`",
"examples": [
1
],
"type": "number"
},
"order": {
"default": "descending",
"description": "Time-based order to search Notes in for `consecutive` search",
"enum": [
"ascending",
"descending"
],
"examples": [
"descending"
],
"type": "string"
},
"search": {
"default": "current",
"description": "* If `current` then only the most recent note is checked\n* If `consecutive` then `count` number of `type` notes must be found in a row, based on `order` direction\n* If `total` then `count` number of `type` must be found within all notes",
"enum": [
"consecutive",
"current",
"total"
],
"examples": [
"current"
],
"type": "string"
},
"type": {
"description": "User Note type key",
"examples": [
"spamwarn"
],
"type": "string"
}
},
"required": [
"type"
],
"type": "object"
}
}
}

View File

@@ -3,11 +3,22 @@
"definitions": {
"ActivityWindowCriteria": {
"additionalProperties": false,
"description": "If both properties are defined then the first criteria met will be used IE if # of activities = count before duration is reached then count will be used, or vice versa",
"description": "The criteria used to define what range of Activity to retrieve.\n\nMay specify one, or both properties along with the `satisfyOn` property, to affect the retrieval behavior.",
"examples": [
{
"count": 100,
"duration": {
"days": 90
}
}
],
"minProperties": 1,
"properties": {
"count": {
"description": "The number of activities (submission/comments) to consider",
"examples": [
15
],
"type": "number"
},
"duration": {
@@ -19,13 +30,25 @@
"type": "string"
}
],
"description": "An ISO 8601 duration or Day.js duration object.\n\nThe duration will be subtracted from the time when the rule is run to create a time range like this:\n\nendTime = NOW <----> startTime = (NOW - duration)\n\nEX endTime = 3:00PM <----> startTime = (NOW - 15 minutes) = 2:45PM -- so look for activities between 2:45PM and 3:00PM",
"description": "An [ISO 8601 duration string](https://en.wikipedia.org/wiki/ISO_8601#Durations) or [Day.js duration object](https://day.js.org/docs/en/durations/creating).\n\nThe duration will be subtracted from the time when the rule is run to create a time range like this:\n\nendTime = NOW <----> startTime = (NOW - `duration`)\n\nEX `PT15M` or `{\"minutes\": 15}`\n* `endTime` = NOW (3:00PM)\n* `startTime` = (NOW - 15 minutes) = 2:45PM\n\nSo look for Activities between 2:45PM and 3:00PM",
"examples": [
"PT1M",
"PT15M",
{
"minutes": 15
}
]
},
"satisfyOn": {
"default": "any",
"description": "Define the condition under which both criteria are considered met\n\n**If `any` then it will retrieve Activities until one of the criteria is met, whichever occurs first**\n\nEX `{count: 100, duration: {days: 90}}`:\n* If 90 days of activities = 40 activities => returns 40 activities\n* If 100 activities is only 20 days => 100 activities\n\n**If `all` then both criteria must be met.**\n\nEffectively, whichever criteria produces the most Activities...\n\nEX `{count: 100, duration: {days: 90}}`:\n* If at 90 days of activities => 40 activities, continue retrieving results until 100 => results in >90 days of activities\n* If at 100 activities => 20 days of activities, continue retrieving results until 90 days => results in >100 activities",
"enum": [
"all",
"any"
],
"examples": [
"any"
],
"type": "string"
}
},
"type": "object"
@@ -60,10 +83,10 @@
"window": {
"anyOf": [
{
"$ref": "#/definitions/DurationObject"
"$ref": "#/definitions/ActivityWindowCriteria"
},
{
"$ref": "#/definitions/ActivityWindowCriteria"
"$ref": "#/definitions/DurationObject"
},
{
"type": [
@@ -88,11 +111,24 @@
"description": "Should the rule aggregate recognized media domains into the parent domain?\n\nSubmissions to major media domains (youtube, vimeo) can be identified by individual Channel/Author...\n\n* If `false` then aggregate will occur at the channel level IE Youtube Channel A (2 counts), Youtube Channel B (3 counts)\n* If `true` then then aggregation will occur at the domain level IE youtube.com (5 counts)",
"type": "boolean"
},
"authors": {
"additionalProperties": false,
"authorIs": {
"$ref": "#/definitions/AuthorOptions",
"description": "If present then these Author criteria are checked before running the rule. If criteria fails then the rule is skipped.",
"minProperties": 1,
"type": "object"
"examples": [
{
"include": [
{
"flairText": [
"Contributor",
"Veteran"
]
},
{
"isMod": true
}
]
}
]
},
"criteria": {
"description": "A list threshold-window values to test attribution against\n\nIf none is provided the default set used is:\n\n```\nthreshold: 10%\nwindow: 100\n```",
@@ -139,6 +175,23 @@
"description": "Include reddit `self.*` domains in aggregation?\n\nSelf-posts are aggregated under the domain `self.[subreddit]`. If you wish to include these domains in aggregation set this to `true`",
"type": "boolean"
},
"itemIs": {
"anyOf": [
{
"items": {
"$ref": "#/definitions/SubmissionState"
},
"type": "array"
},
{
"items": {
"$ref": "#/definitions/CommentState"
},
"type": "array"
}
],
"description": "A list of criteria to test the state of the `Activity` against before running the Rule.\n\nIf any set of criteria passes the Rule will be run. If the criteria fails then the Rule is skipped."
},
"kind": {
"description": "The kind of rule to run",
"enum": [
@@ -157,6 +210,9 @@
},
"name": {
"description": "An optional, but highly recommended, friendly name for this rule. If not present will default to `kind`.\n\nCan only contain letters, numbers, underscore, spaces, and dashes\n\nname is used to reference Rule result data during Action content templating. See CommentAction or ReportAction for more details.",
"examples": [
"myNewRule"
],
"pattern": "^[a-zA-Z]([\\w -]*[\\w])?$",
"type": "string"
},
@@ -174,10 +230,26 @@
"AuthorCriteria": {
"additionalProperties": false,
"description": "Criteria with which to test against the author of an Activity. The outcome of the test is based on:\n\n1. All present properties passing and\n2. If a property is a list then any value from the list matching",
"examples": [
{
"flairText": [
"Contributor",
"Veteran"
],
"isMod": true,
"name": [
"FoxxMD",
"AnotherUser"
]
}
],
"minProperties": 1,
"properties": {
"flairCssClass": {
"description": "A list of (user) flair css class values from the subreddit to match against",
"examples": [
"red"
],
"items": {
"type": "string"
},
@@ -185,6 +257,9 @@
},
"flairText": {
"description": "A list of (user) flair text values from the subreddit to match against",
"examples": [
"Approved"
],
"items": {
"type": "string"
},
@@ -204,17 +279,72 @@
"type": "string"
},
"type": "array"
},
"userNotes": {
"description": "A list of UserNote properties to check against the User Notes attached to this Author in this Subreddit (must have Toolbox enabled and used User Notes at least once)",
"items": {
"$ref": "#/definitions/UserNoteCriteria"
},
"type": "array"
}
},
"type": "object"
},
"AuthorOptions": {
"description": "If present then these Author criteria are checked before running the rule. If criteria fails then the rule is skipped.",
"examples": [
{
"include": [
{
"flairText": [
"Contributor",
"Veteran"
]
},
{
"isMod": true
}
]
}
],
"properties": {
"exclude": {
"description": "Only runs if include is not present. Will \"pass\" if any of set of the AuthorCriteria does not pass",
"items": {
"$ref": "#/definitions/AuthorCriteria"
},
"type": "array"
},
"include": {
"description": "Will \"pass\" if any set of AuthorCriteria passes",
"items": {
"$ref": "#/definitions/AuthorCriteria"
},
"type": "array"
}
},
"type": "object"
},
"AuthorRuleJSONConfig": {
"properties": {
"authors": {
"additionalProperties": false,
"authorIs": {
"$ref": "#/definitions/AuthorOptions",
"description": "If present then these Author criteria are checked before running the rule. If criteria fails then the rule is skipped.",
"minProperties": 1,
"type": "object"
"examples": [
{
"include": [
{
"flairText": [
"Contributor",
"Veteran"
]
},
{
"isMod": true
}
]
}
]
},
"exclude": {
"description": "Only runs if include is not present. Will \"pass\" if any of set of the AuthorCriteria does not pass",
@@ -230,6 +360,23 @@
},
"type": "array"
},
"itemIs": {
"anyOf": [
{
"items": {
"$ref": "#/definitions/SubmissionState"
},
"type": "array"
},
{
"items": {
"$ref": "#/definitions/CommentState"
},
"type": "array"
}
],
"description": "A list of criteria to test the state of the `Activity` against before running the Rule.\n\nIf any set of criteria passes the Rule will be run. If the criteria fails then the Rule is skipped."
},
"kind": {
"description": "The kind of rule to run",
"enum": [
@@ -239,6 +386,9 @@
},
"name": {
"description": "An optional, but highly recommended, friendly name for this rule. If not present will default to `kind`.\n\nCan only contain letters, numbers, underscore, spaces, and dashes\n\nname is used to reference Rule result data during Action content templating. See CommentAction or ReportAction for more details.",
"examples": [
"myNewRule"
],
"pattern": "^[a-zA-Z]([\\w -]*[\\w])?$",
"type": "string"
}
@@ -250,6 +400,39 @@
],
"type": "object"
},
"CommentState": {
"description": "Different attributes a `Comment` can be in. Only include a property if you want to check it.",
"examples": [
{
"op": true,
"removed": false
}
],
"properties": {
"approved": {
"type": "boolean"
},
"distinguished": {
"type": "boolean"
},
"locked": {
"type": "boolean"
},
"op": {
"type": "boolean"
},
"removed": {
"type": "boolean"
},
"spam": {
"type": "boolean"
},
"stickied": {
"type": "boolean"
}
},
"type": "object"
},
"CommentThresholdCriteria": {
"properties": {
"asOp": {
@@ -263,11 +446,21 @@
">",
">="
],
"examples": [
">",
">=",
"<",
"<="
],
"type": "string"
},
"threshold": {
"default": "10%",
"description": "The number or percentage to trigger this criteria at\n\n* If `threshold` is a `number` then it is the absolute number of items to trigger at\n* If `threshold` is a `string` with percentage (EX `40%`) then it is the percentage of the total this item must reach to trigger",
"examples": [
"10%",
15
],
"type": [
"string",
"number"
@@ -282,28 +475,55 @@
},
"DurationObject": {
"additionalProperties": false,
"description": "A Day.js duration object\n\nhttps://day.js.org/docs/en/durations/creating",
"description": "A [Day.js duration object](https://day.js.org/docs/en/durations/creating)",
"examples": [
{
"hours": 1,
"minutes": 30
}
],
"minProperties": 1,
"properties": {
"days": {
"examples": [
7
],
"type": "number"
},
"hours": {
"examples": [
4
],
"type": "number"
},
"minutes": {
"examples": [
50
],
"type": "number"
},
"months": {
"examples": [
3
],
"type": "number"
},
"seconds": {
"examples": [
15
],
"type": "number"
},
"weeks": {
"examples": [
2
],
"type": "number"
},
"years": {
"examples": [
0
],
"type": "number"
}
},
@@ -329,10 +549,10 @@
"window": {
"anyOf": [
{
"$ref": "#/definitions/DurationObject"
"$ref": "#/definitions/ActivityWindowCriteria"
},
{
"$ref": "#/definitions/ActivityWindowCriteria"
"$ref": "#/definitions/DurationObject"
},
{
"type": [
@@ -352,11 +572,24 @@
"HistoryJSONConfig": {
"description": "Aggregates an Author's submission and comment history. Rule can be triggered on count/percent of total (for either or both comment/sub totals) as well as comment OP total.\n\nAvailable data for [Action templating](https://github.com/FoxxMD/reddit-context-bot#action-templating):\n\n```\nactivityTotal => Total number of activities\nsubmissionTotal => Total number of submissions\ncommentTotal => Total number of comments\nopTotal => Total number of comments as OP\nthresholdSummary => A text summary of the first Criteria triggered with totals/percentages\ncriteria => The ThresholdCriteria object\nwindow => A text summary of the range of Activities considered (# of Items if number, time range if Duration)\n```",
"properties": {
"authors": {
"additionalProperties": false,
"authorIs": {
"$ref": "#/definitions/AuthorOptions",
"description": "If present then these Author criteria are checked before running the rule. If criteria fails then the rule is skipped.",
"minProperties": 1,
"type": "object"
"examples": [
{
"include": [
{
"flairText": [
"Contributor",
"Veteran"
]
},
{
"isMod": true
}
]
}
]
},
"criteria": {
"description": "A list threshold-window values to test activities against.",
@@ -398,6 +631,23 @@
"minItems": 1,
"type": "array"
},
"itemIs": {
"anyOf": [
{
"items": {
"$ref": "#/definitions/SubmissionState"
},
"type": "array"
},
{
"items": {
"$ref": "#/definitions/CommentState"
},
"type": "array"
}
],
"description": "A list of criteria to test the state of the `Activity` against before running the Rule.\n\nIf any set of criteria passes the Rule will be run. If the criteria fails then the Rule is skipped."
},
"kind": {
"description": "The kind of rule to run",
"enum": [
@@ -407,6 +657,9 @@
},
"name": {
"description": "An optional, but highly recommended, friendly name for this rule. If not present will default to `kind`.\n\nCan only contain letters, numbers, underscore, spaces, and dashes\n\nname is used to reference Rule result data during Action content templating. See CommentAction or ReportAction for more details.",
"examples": [
"myNewRule"
],
"pattern": "^[a-zA-Z]([\\w -]*[\\w])?$",
"type": "string"
}
@@ -420,17 +673,50 @@
"RecentActivityRuleJSONConfig": {
"description": "Checks a user's history for any Activity (Submission/Comment) in the subreddits specified in thresholds\n\nAvailable data for [Action templating](https://github.com/FoxxMD/reddit-context-bot#action-templating):\n\n```\nsummary => comma-deliminated list of subreddits that hit the threshold and their count EX subredditA(1), subredditB(4),...\nsubCount => Total number of subreddits that hit the threshold\ntotalCount => Total number of all activity occurrences in subreddits\n```",
"properties": {
"authors": {
"additionalProperties": false,
"authorIs": {
"$ref": "#/definitions/AuthorOptions",
"description": "If present then these Author criteria are checked before running the rule. If criteria fails then the rule is skipped.",
"minProperties": 1,
"type": "object"
"examples": [
{
"include": [
{
"flairText": [
"Contributor",
"Veteran"
]
},
{
"isMod": true
}
]
}
]
},
"itemIs": {
"anyOf": [
{
"items": {
"$ref": "#/definitions/SubmissionState"
},
"type": "array"
},
{
"items": {
"$ref": "#/definitions/CommentState"
},
"type": "array"
}
],
"description": "A list of criteria to test the state of the `Activity` against before running the Rule.\n\nIf any set of criteria passes the Rule will be run. If the criteria fails then the Rule is skipped."
},
"kind": {
"description": "The kind of rule to run",
"enum": [
"recentActivity"
],
"examples": [
"recentActivity"
],
"type": "string"
},
"lookAt": {
@@ -439,10 +725,17 @@
"comments",
"submissions"
],
"examples": [
"submissions",
"comments"
],
"type": "string"
},
"name": {
"description": "An optional, but highly recommended, friendly name for this rule. If not present will default to `kind`.\n\nCan only contain letters, numbers, underscore, spaces, and dashes\n\nname is used to reference Rule result data during Action content templating. See CommentAction or ReportAction for more details.",
"examples": [
"myNewRule"
],
"pattern": "^[a-zA-Z]([\\w -]*[\\w])?$",
"type": "string"
},
@@ -462,10 +755,10 @@
"window": {
"anyOf": [
{
"$ref": "#/definitions/DurationObject"
"$ref": "#/definitions/ActivityWindowCriteria"
},
{
"$ref": "#/definitions/ActivityWindowCriteria"
"$ref": "#/definitions/DurationObject"
},
{
"type": [
@@ -475,25 +768,7 @@
}
],
"default": 15,
"description": "Criteria for defining what set of activities should be considered.\n\nThe value of this property may be either count OR duration -- to use both write it as an ActivityWindowCriteria\n\nSee ActivityWindowCriteria for descriptions of what count/duration do",
"examples": [
15,
"PT1M",
{
"count": 10
},
{
"duration": {
"hours": 5
}
},
{
"count": 5,
"duration": {
"minutes": 15
}
}
]
"description": "Criteria for defining what set of activities should be considered.\n\nThe value of this property may be either count OR duration -- to use both write it as an `ActivityWindowCriteria`"
}
},
"required": [
@@ -505,11 +780,24 @@
"RepeatActivityJSONConfig": {
"description": "Checks a user's history for Submissions with identical content\n\nAvailable data for [Action templating](https://github.com/FoxxMD/reddit-context-bot#action-templating):\n\n```\ncount => Total number of repeat Submissions\nthreshold => The threshold you configured for this Rule to trigger\nurl => Url of the submission that triggered the rule\n```",
"properties": {
"authors": {
"additionalProperties": false,
"authorIs": {
"$ref": "#/definitions/AuthorOptions",
"description": "If present then these Author criteria are checked before running the rule. If criteria fails then the rule is skipped.",
"minProperties": 1,
"type": "object"
"examples": [
{
"include": [
{
"flairText": [
"Contributor",
"Veteran"
]
},
{
"isMod": true
}
]
}
]
},
"exclude": {
"description": "Do not include Submissions from this list of Subreddits.\n\nA list of subreddits (case-insensitive) to look for. Do not include \"r/\" prefix.\n\nEX to match against /r/mealtimevideos and /r/askscience use [\"mealtimevideos\",\"askscience\"]",
@@ -539,6 +827,23 @@
"minItems": 1,
"type": "array"
},
"itemIs": {
"anyOf": [
{
"items": {
"$ref": "#/definitions/SubmissionState"
},
"type": "array"
},
{
"items": {
"$ref": "#/definitions/CommentState"
},
"type": "array"
}
],
"description": "A list of criteria to test the state of the `Activity` against before running the Rule.\n\nIf any set of criteria passes the Rule will be run. If the criteria fails then the Rule is skipped."
},
"kind": {
"description": "The kind of rule to run",
"enum": [
@@ -557,6 +862,9 @@
},
"name": {
"description": "An optional, but highly recommended, friendly name for this rule. If not present will default to `kind`.\n\nCan only contain letters, numbers, underscore, spaces, and dashes\n\nname is used to reference Rule result data during Action content templating. See CommentAction or ReportAction for more details.",
"examples": [
"myNewRule"
],
"pattern": "^[a-zA-Z]([\\w -]*[\\w])?$",
"type": "string"
},
@@ -573,10 +881,10 @@
"window": {
"anyOf": [
{
"$ref": "#/definitions/DurationObject"
"$ref": "#/definitions/ActivityWindowCriteria"
},
{
"$ref": "#/definitions/ActivityWindowCriteria"
"$ref": "#/definitions/DurationObject"
},
{
"type": [
@@ -586,25 +894,7 @@
}
],
"default": 15,
"description": "Criteria for defining what set of activities should be considered.\n\nThe value of this property may be either count OR duration -- to use both write it as an ActivityWindowCriteria\n\nSee ActivityWindowCriteria for descriptions of what count/duration do",
"examples": [
15,
"PT1M",
{
"count": 10
},
{
"duration": {
"hours": 5
}
},
{
"count": 5,
"duration": {
"minutes": 15
}
}
]
"description": "Criteria for defining what set of activities should be considered.\n\nThe value of this property may be either count OR duration -- to use both write it as an `ActivityWindowCriteria`"
}
},
"required": [
@@ -619,23 +909,31 @@
"properties": {
"count": {
"description": "The number of activities in each subreddit from the list that will trigger this rule",
"examples": [
1
],
"minimum": 1,
"type": "number"
},
"subreddits": {
"description": "A list of subreddits (case-insensitive) to look for. Do not include \"r/\" prefix.\n\nEX to match against /r/mealtimevideos and /r/askscience use [\"mealtimevideos\",\"askscience\"]",
"examples": [
"mealtimevideos",
"askscience"
[
"mealtimevideos",
"askscience"
]
],
"items": {
"type": "string"
},
"minItems": 1,
"minItems": 2,
"type": "array"
},
"totalCount": {
"description": "The total number of activities across all listed subreddits that will trigger this rule",
"examples": [
1
],
"minimum": 1,
"type": "number"
}
@@ -645,6 +943,49 @@
],
"type": "object"
},
"SubmissionState": {
"description": "Different attributes a `Submission` can be in. Only include a property if you want to check it.",
"examples": [
{
"over_18": true,
"removed": false
}
],
"properties": {
"approved": {
"type": "boolean"
},
"distinguished": {
"type": "boolean"
},
"is_self": {
"type": "boolean"
},
"locked": {
"type": "boolean"
},
"over_18": {
"description": "NSFW",
"type": "boolean"
},
"pinned": {
"type": "boolean"
},
"removed": {
"type": "boolean"
},
"spam": {
"type": "boolean"
},
"spoiler": {
"type": "boolean"
},
"stickied": {
"type": "boolean"
}
},
"type": "object"
},
"ThresholdCriteria": {
"properties": {
"condition": {
@@ -654,11 +995,21 @@
">",
">="
],
"examples": [
">",
">=",
"<",
"<="
],
"type": "string"
},
"threshold": {
"default": "10%",
"description": "The number or percentage to trigger this criteria at\n\n* If `threshold` is a `number` then it is the absolute number of items to trigger at\n* If `threshold` is a `string` with percentage (EX `40%`) then it is the percentage of the total this item must reach to trigger",
"examples": [
"10%",
15
],
"type": [
"string",
"number"
@@ -670,6 +1021,54 @@
"threshold"
],
"type": "object"
},
"UserNoteCriteria": {
"properties": {
"count": {
"default": 1,
"description": "Number of occurrences of this type. Ignored if `search` is `current`",
"examples": [
1
],
"type": "number"
},
"order": {
"default": "descending",
"description": "Time-based order to search Notes in for `consecutive` search",
"enum": [
"ascending",
"descending"
],
"examples": [
"descending"
],
"type": "string"
},
"search": {
"default": "current",
"description": "* If `current` then only the most recent note is checked\n* If `consecutive` then `count` number of `type` notes must be found in a row, based on `order` direction\n* If `total` then `count` number of `type` must be found within all notes",
"enum": [
"consecutive",
"current",
"total"
],
"examples": [
"current"
],
"type": "string"
},
"type": {
"description": "User Note type key",
"examples": [
"spamwarn"
],
"type": "string"
}
},
"required": [
"type"
],
"type": "object"
}
},
"description": "A RuleSet is a \"nested\" set of `Rule` objects that can be used to create more complex AND/OR behavior. Think of the outcome of a `RuleSet` as the result of all of its run `Rule` objects (based on `condition`)",
@@ -681,6 +1080,9 @@
"AND",
"OR"
],
"examples": [
"AND"
],
"type": "string"
},
"rules": {

View File

@@ -15,7 +15,7 @@ import Submission from "snoowrap/dist/objects/Submission";
import {itemContentPeek} from "../Utils/SnoowrapUtils";
import dayjs from "dayjs";
import LoggedError from "../Utils/LoggedError";
import CacheManager from "./SubredditCache";
import ResourceManager, {SubredditResources} from "./SubredditResources";
export class Manager {
subreddit: Subreddit;
@@ -24,6 +24,7 @@ export class Manager {
pollOptions: PollingOptions;
submissionChecks: SubmissionCheck[];
commentChecks: CommentCheck[];
resources: SubredditResources;
subListedOnce = false;
streamSub?: SubmissionStream;
@@ -34,6 +35,8 @@ export class Manager {
displayLabel: string;
currentLabels?: string[];
running: boolean = false;
getCurrentLabels = () => {
return this.currentLabels;
}
@@ -60,18 +63,24 @@ export class Manager {
this.client = client;
this.dryRun = opts.dryRun || dryRun;
const cacheConfig = caching === false ? {enabled: false, logger: this.logger} : {
const cacheConfig = caching === false ? {enabled: false, logger: this.logger, subreddit: sub} : {
...caching,
enabled: true,
logger: this.logger
logger: this.logger,
subreddit: sub,
};
CacheManager.get(sub.display_name, cacheConfig);
this.resources = ResourceManager.set(sub.display_name, cacheConfig);
const commentChecks: Array<CommentCheck> = [];
const subChecks: Array<SubmissionCheck> = [];
const structuredChecks = configBuilder.parseToStructured(validJson);
for (const jCheck of structuredChecks) {
const checkConfig = {...jCheck, dryRun: this.dryRun || jCheck.dryRun, logger: this.logger, subredditName: sub.display_name};
const checkConfig = {
...jCheck,
dryRun: this.dryRun || jCheck.dryRun,
logger: this.logger,
subredditName: sub.display_name
};
if (jCheck.kind === 'comment') {
commentChecks.push(new CommentCheck(checkConfig));
} else if (jCheck.kind === 'submission') {
@@ -79,13 +88,7 @@ export class Manager {
}
}
for (const subc of subChecks) {
this.logger.info(`Submission Check: ${subc.name}${subc.description !== undefined ? ` => ${subc.description}` : ''}`);
}
this.submissionChecks = subChecks;
for (const comm of commentChecks) {
this.logger.info(`Comment Check: ${comm.name}${comm.description !== undefined ? ` => ${comm.description}` : ''}`);
}
this.commentChecks = commentChecks;
const checkSummary = `Found Checks -- Submission: ${this.submissionChecks.length} | Comment: ${this.commentChecks.length}`;
if (subChecks.length === 0 && commentChecks.length === 0) {
@@ -103,18 +106,26 @@ export class Manager {
this.currentLabels = [this.displayLabel, itemIdentifier];
const [peek, _] = await itemContentPeek(item);
this.logger.info(`<EVENT> ${peek}`);
const startingApiLimit = this.client.ratelimitRemaining;
let checksRun = 0;
let actionsRun = 0;
let totalRulesRun = 0;
try {
let triggered = false;
for (const check of checks) {
if (checkNames.length > 0 && !checkNames.map(x => x.toLowerCase()).some(x => x === check.name.toLowerCase())) {
this.logger.warn(`Check ${check} not in array of requested checks to run, skipping`);
continue;
}
let triggered = false;
checksRun++;
triggered = false;
let currentResults: RuleResult[] = [];
try {
const [checkTriggered, checkResults] = await check.run(item, allRuleResults);
const [checkTriggered, checkResults] = await check.runRules(item, allRuleResults);
currentResults = checkResults;
totalRulesRun += checkResults.length;
allRuleResults = allRuleResults.concat(determineNewResults(allRuleResults, checkResults));
triggered = checkTriggered;
} catch (e) {
@@ -122,80 +133,95 @@ export class Manager {
}
if (triggered) {
await check.runActions(item, currentResults);
const runActions = await check.runActions(item, currentResults);
actionsRun = runActions.length;
break;
}
}
if(!triggered) {
this.logger.info('No checks triggered');
}
} catch (err) {
if (!(err instanceof LoggedError)) {
this.logger.error('An unhandled error occurred while running checks', err);
}
} finally {
this.logger.verbose(`Run Stats: Checks ${checksRun} | Rules => Total: ${totalRulesRun} Unique: ${allRuleResults.length} Cached: ${totalRulesRun - allRuleResults.length} | Actions ${actionsRun}`);
this.logger.verbose(`Reddit API Stats: Initial Limit ${startingApiLimit} | Current Limit ${this.client.ratelimitRemaining} | Calls Made ${startingApiLimit - this.client.ratelimitRemaining}`);
this.currentLabels = [this.displayLabel];
this.logger.debug(`Reddit API Rate Limit remaining: ${this.client.ratelimitRemaining}`);
}
}
async handle(): Promise<void> {
if (this.submissionChecks.length > 0) {
const {
submissions: {
limit = 10,
interval = 10000,
} = {}
} = this.pollOptions
this.streamSub = new SubmissionStream(this.client, {
subreddit: this.subreddit.display_name,
limit,
pollTime: interval,
});
try {
if (this.submissionChecks.length > 0) {
const {
submissions: {
limit = 10,
interval = 10000,
} = {}
} = this.pollOptions
this.streamSub = new SubmissionStream(this.client, {
subreddit: this.subreddit.display_name,
limit,
pollTime: interval,
});
this.streamSub.once('listing', async (listing) => {
this.subListedOnce = true;
});
this.streamSub.on('item', async (item) => {
if (!this.subListedOnce) {
return;
}
await this.runChecks('Submission', item)
});
//this.streamSub.on('listing', (_) => this.logger.debug('Polled Submissions'));
this.streamSub.once('listing', async (listing) => {
this.subListedOnce = true;
});
this.streamSub.on('item', async (item) => {
if (!this.subListedOnce) {
return;
}
await this.runChecks('Submission', item)
});
//this.streamSub.on('listing', (_) => this.logger.debug('Polled Submissions'));
}
if (this.commentChecks.length > 0) {
const {
comments: {
limit = 10,
interval = 10000,
} = {}
} = this.pollOptions
this.streamComments = new CommentStream(this.client, {
subreddit: this.subreddit.display_name,
limit,
pollTime: interval,
});
this.streamComments.once('listing', () => this.commentsListedOnce = true);
this.streamComments.on('item', async (item) => {
if (!this.commentsListedOnce) {
return;
}
await this.runChecks('Comment', item)
});
//this.streamComments.on('listing', (_) => this.logger.debug('Polled Comments'));
}
this.running = true;
if (this.streamSub !== undefined) {
this.logger.info('Bot Running');
await pEvent(this.streamSub, 'end');
} else if (this.streamComments !== undefined) {
this.logger.info('Bot Running');
await pEvent(this.streamComments, 'end');
} else {
this.logger.warn('No submission or comment checks to run! Bot will not run.');
return;
}
} catch (err) {
this.logger.error('Encountered unhandled error, manager is bailing out');
this.logger.error(err);
} finally {
this.running = false;
this.logger.info('Bot Stopped');
}
if (this.commentChecks.length > 0) {
const {
comments: {
limit = 10,
interval = 10000,
} = {}
} = this.pollOptions
this.streamComments = new CommentStream(this.client, {
subreddit: this.subreddit.display_name,
limit,
pollTime: interval,
});
this.streamComments.once('listing', () => this.commentsListedOnce = true);
this.streamComments.on('item', async (item) => {
if (!this.commentsListedOnce) {
return;
}
await this.runChecks('Comment', item)
});
//this.streamComments.on('listing', (_) => this.logger.debug('Polled Comments'));
}
if (this.streamSub !== undefined) {
this.logger.info('Bot Running');
await pEvent(this.streamSub, 'end');
} else if (this.streamComments !== undefined) {
this.logger.info('Bot Running');
await pEvent(this.streamComments, 'end');
} else {
this.logger.warn('No submission or comment checks to run! Bot will not run.');
return;
}
this.logger.info('Bot Stopped');
}
}

View File

@@ -13,26 +13,31 @@ import {mergeArr} from "../util";
import LoggedError from "../Utils/LoggedError";
import {SubredditCacheConfig} from "../Common/interfaces";
import {AuthorCriteria} from "../Rule";
import UserNotes from "./UserNotes";
export const WIKI_DESCRIM = 'wiki:';
export interface SubredditCacheOptions extends SubredditCacheConfig {
enabled: boolean;
logger?: Logger;
subreddit: Subreddit,
logger: Logger;
}
export class SubredditCache {
export class SubredditResources {
enabled: boolean;
authorTTL: number;
useSubredditAuthorCache: boolean;
wikiTTL: number;
protected authorTTL: number;
protected useSubredditAuthorCache: boolean;
protected wikiTTL: number;
name: string;
logger: Logger;
protected logger: Logger;
userNotes: UserNotes;
constructor(name: string, options?: SubredditCacheOptions) {
constructor(name: string, options: SubredditCacheOptions) {
const {
enabled = true,
authorTTL,
subreddit,
userNotesTTL = 60000,
wikiTTL = 300000, // 5 minutes
logger,
} = options || {};
@@ -46,6 +51,9 @@ export class SubredditCache {
this.authorTTL = authorTTL;
}
this.wikiTTL = wikiTTL;
this.userNotes = new UserNotes(enabled ? userNotesTTL : 0, subreddit, logger);
this.name = name;
if (logger === undefined) {
const alogger = winston.loggers.get('default')
@@ -61,7 +69,7 @@ export class SubredditCache {
if (useCache) {
const userName = user.name;
const hashObj: any = {...options, userName};
if(this.useSubredditAuthorCache) {
if (this.useSubredditAuthorCache) {
hashObj.subreddit = this.name;
}
hash = objectHash.sha1({...options, userName});
@@ -127,40 +135,46 @@ export class SubredditCache {
}
}
async testAuthorCriteria(item: (Comment|Submission), authorOpts: AuthorCriteria, include = true) {
async testAuthorCriteria(item: (Comment | Submission), authorOpts: AuthorCriteria, include = true) {
const useCache = this.enabled && this.authorTTL > 0;
let hash;
if(useCache) {
if (useCache) {
const hashObj = {itemId: item.id, ...authorOpts, include};
hash = `authorCrit-${objectHash.sha1(hashObj)}`;
const cachedAuthorTest = cache.get(hash);
if(null !== cachedAuthorTest) {
if (null !== cachedAuthorTest) {
this.logger.debug(`Cache Hit: Author Check on ${item.id}`);
return cachedAuthorTest;
}
}
const result = await testAuthorCriteria(item, authorOpts, include);
if(useCache) {
const result = await testAuthorCriteria(item, authorOpts, include, this.userNotes);
if (useCache) {
cache.put(hash, result, this.authorTTL);
}
return result;
}
}
class SubredditCacheManager {
caches: Map<string, SubredditCache> = new Map();
class SubredditResourcesManager {
resources: Map<string, SubredditResources> = new Map();
authorTTL: number = 10000;
enabled: boolean = true;
get(subName: string, initOptions?: SubredditCacheOptions): SubredditCache {
if (!this.caches.has(subName)) {
this.caches.set(subName, new SubredditCache(subName, initOptions))
get(subName: string): SubredditResources | undefined {
if (this.resources.has(subName)) {
return this.resources.get(subName) as SubredditResources;
}
return this.caches.get(subName) as SubredditCache;
return undefined;
}
set(subName: string, initOptions: SubredditCacheOptions): SubredditResources {
const resource = new SubredditResources(subName, initOptions);
this.resources.set(subName, resource);
return resource;
}
}
const manager = new SubredditCacheManager();
const manager = new SubredditResourcesManager();
export default manager;

264
src/Subreddit/UserNotes.ts Normal file
View File

@@ -0,0 +1,264 @@
import dayjs, {Dayjs} from "dayjs";
import {Comment, RedditUser, WikiPage} from "snoowrap";
import cache from 'memory-cache';
import {COMMENT_URL_ID, deflateUserNotes, inflateUserNotes, parseLinkIdentifier, SUBMISSION_URL_ID} from "../util";
import Subreddit from "snoowrap/dist/objects/Subreddit";
import {Logger} from "winston";
import LoggedError from "../Utils/LoggedError";
import Submission from "snoowrap/dist/objects/Submission";
import {RichContent} from "../Common/interfaces";
interface RawUserNotesPayload {
ver: number,
constants: UserNotesConstants,
blob: RawBlobPayload
}
interface RawBlobPayload {
[username: string]: RawUserNoteRoot
}
interface RawUserNoteRoot {
ns: RawNote[]
}
export interface RawNote {
/**
* Note Text
* */
n: string;
/**
* Unix epoch in seconds
* */
t: number;
/**
* Moderator index from constants.users
* */
m: number;
/**
* Link shorthand
* */
l: string;
/**
* type/color index from constants.warnings
* */
w: number;
}
export type UserNotesConstants = Pick<any, "users" | "warnings">;
export class UserNotes {
notesTTL: number;
subreddit: Subreddit;
wiki: WikiPage;
moderators?: RedditUser[];
logger: Logger;
identifier: string;
users: Map<string, UserNote[]> = new Map();
constructor(ttl: number, subreddit: Subreddit, logger: Logger) {
this.notesTTL = ttl;
this.subreddit = subreddit;
this.logger = logger;
this.wiki = subreddit.getWikiPage('usernotes');
this.identifier = `${this.subreddit.display_name}-usernotes`;
}
async getUserNotes(user: RedditUser): Promise<UserNote[]> {
let notes: UserNote[] | undefined = [];
if (this.users !== undefined) {
notes = this.users.get(user.name);
if (notes !== undefined) {
this.logger.debug('Returned cached notes');
return notes;
}
}
const payload = await this.retrieveData();
const rawNotes = payload.blob[user.name];
if (rawNotes !== undefined) {
if (this.moderators === undefined) {
this.moderators = await this.subreddit.getModerators();
}
const notes = rawNotes.ns.map(x => UserNote.fromRaw(x, payload.constants, this.moderators as RedditUser[]));
// sort in ascending order by time
notes.sort((a, b) => a.time.isBefore(b.time) ? -1 : 1);
if (this.notesTTL > 0) {
this.users.set(user.name, notes);
}
return notes;
} else {
return [];
}
}
async addUserNote(item: (Submission|Comment), type: string | number, text: string = ''): Promise<UserNote>
{
const payload = await this.retrieveData();
// idgaf
// @ts-ignore
const mod = await this.subreddit._r.getMe();
if(!payload.constants.users.includes(mod.name)) {
this.logger.info(`Mod ${mod.name} does not exist in UserNote constants, adding them`);
payload.constants.users.push(mod.name);
}
if(!payload.constants.warnings.find((x: string) => x === type)) {
this.logger.warn(`UserNote type '${type}' does not exist, adding it but make sure spelling and letter case is correct`);
payload.constants.warnings.push(type);
//throw new LoggedError(`UserNote type '${type}' does not exist. If you meant to use this please add it through Toolbox first.`);
}
const newNote = new UserNote(dayjs(), text, mod, type, `https://reddit.com${item.permalink}`);
if(payload.blob[item.author.name] === undefined) {
payload.blob[item.author.name] = {ns: []};
}
payload.blob[item.author.name].ns.push(newNote.toRaw(payload.constants));
await this.saveData(payload);
if(this.notesTTL > 0) {
const currNotes = this.users.get(item.author.name) || [];
currNotes.push(newNote);
this.users.set(item.author.name, currNotes);
}
return newNote;
}
async warningExists(type: string): Promise<boolean>
{
const payload = await this.retrieveData();
return payload.constants.warnings.some((x: string) => x === type);
}
async retrieveData(): Promise<RawUserNotesPayload> {
if (this.notesTTL > 0) {
const cachedPayload = cache.get(this.identifier);
if (cachedPayload !== null) {
return cachedPayload as RawUserNotesPayload;
}
}
try {
// @ts-ignore
this.wiki = await this.subreddit.getWikiPage('usernotes').fetch();
const wikiContent = this.wiki.content_md;
// TODO don't handle for versions lower than 6
const userNotes = JSON.parse(wikiContent);
userNotes.blob = inflateUserNotes(userNotes.blob);
if (this.notesTTL > 0) {
cache.put(`${this.subreddit.display_name}-usernotes`, userNotes, this.notesTTL, () => {
this.users = new Map();
});
}
return userNotes as RawUserNotesPayload;
} catch (err) {
const msg = `Could not read usernotes. Make sure at least one moderator has used toolbox and usernotes before.`;
this.logger.error(msg, err);
throw new LoggedError(msg);
}
}
async saveData(payload: RawUserNotesPayload): Promise<RawUserNotesPayload> {
const blob = deflateUserNotes(payload.blob);
const wikiPayload = {...payload, blob};
try {
// @ts-ignore
//this.wiki = await this.wiki.refresh();
// @ts-ignore
this.wiki = await this.subreddit.getWikiPage('usernotes').edit({text: JSON.stringify(wikiPayload), reason: 'ContextBot edited usernotes'});
if (this.notesTTL > 0) {
cache.put(this.identifier, payload, this.notesTTL, () => {
this.users = new Map();
});
}
return payload as RawUserNotesPayload;
} catch (err) {
const msg = `Could not edit usernotes. Make sure at least one moderator has used toolbox and usernotes before and that this account has editing permissions`;
this.logger.error(msg, err);
throw new LoggedError(msg);
}
}
}
export interface UserNoteJson extends RichContent {
/**
* User Note type key
* @examples ["spamwarn"]
* */
type: string,
}
export class UserNote {
//time: Dayjs;
// text?: string;
// moderator: RedditUser;
// noteTypeIndex: number;
// noteType: string | null;
// link: string;
constructor(public time: Dayjs, public text: string, public moderator: RedditUser, public noteType: string | number, public link: string) {
}
public toRaw(constants: UserNotesConstants): RawNote {
return {
t: this.time.unix(),
n: this.text,
m: constants.users.findIndex((x: string) => x === this.moderator.name),
w: typeof this.noteType === 'number' ? this.noteType : constants.warnings.findIndex((x: string) => x === this.noteType),
l: usernoteLinkShorthand(this.link)
}
}
public static fromRaw(obj: RawNote, constants: UserNotesConstants, mods: RedditUser[]) {
const mod = mods.find(x => x.name === constants.users[obj.m]);
if (mod === undefined) {
throw new Error('Could not find moderator for Usernote');
}
return new UserNote(dayjs.unix(obj.t), obj.n, mod, constants.warnings[obj.w] === null ? obj.w : constants.warnings[obj.w], usernoteLinkExpand(obj.l))
}
}
// https://github.com/toolbox-team/reddit-moderator-toolbox/wiki/Subreddit-Wikis%3A-usernotes#link-string-formats
export const usernoteLinkExpand = (link: string) => {
if (link.charAt(0) === 'l') {
const pieces = link.split(',');
if (pieces.length === 3) {
// it's a comment
return `https://www.reddit.com/comments/${pieces[1]}/_/${pieces[2]}`;
}
// its a submission
return `https://redd.it/${pieces[1]}`;
} else {
// its an old modmail thread
return `https://www.reddit.com/message/messages/${link.split(',')[1]}`;
}
}
export const usernoteLinkShorthand = (link: string) => {
const commentReg = parseLinkIdentifier([COMMENT_URL_ID]);
const submissionReg = parseLinkIdentifier([SUBMISSION_URL_ID]);
let commentId = commentReg(link);
let submissionId = submissionReg(link);
if (commentId !== undefined) {
commentId = commentReg(link);
return `l,${submissionId},${commentId}`;
} else if (submissionId !== undefined) {
return `l,${submissionId}`;
}
// aren't dealing with messages at this point so just store whole thing if we didn't get a shorthand
return link;
}
export default UserNotes;

View File

@@ -1,63 +1,67 @@
import commander, {InvalidOptionArgumentError} from "commander";
import {argParseInt, parseBool} from "../util";
export const getOptions = (): commander.Option[] => {
export const clientId = new commander.Option('-c, --clientId <id>', 'Client ID for your Reddit application (default: process.env.CLIENT_ID)')
.default(process.env.CLIENT_ID);
clientId.required = true;
export const clientSecret = new commander.Option('-e, --clientSecret <secret>', 'Client Secret for your Reddit application (default: process.env.CLIENT_SECRET)')
.default(process.env.CLIENT_SECRET);
clientSecret.required = true;
export const accessToken = new commander.Option('-a, --accessToken <token>', 'Access token retrieved from authenticating an account with your Reddit Application (default: process.env.ACCESS_TOKEN)')
.default(process.env.ACCESS_TOKEN);
accessToken.required = true;
export const refreshToken = new commander.Option('-r, --refreshToken <token>', 'Refresh token retrieved from authenticating an account with your Reddit Application (default: process.env.REFRESH_TOKEN)')
.default(process.env.REFRESH_TOKEN);
refreshToken.required = true;
export const subreddits = new commander.Option('-s, --subreddits <list...>', 'List of subreddits to run on. Bot will run on all subs it has access to if not defined')
.default(process.env.SUBREDDITS || [], 'process.env.SUBREDDITS (comma-seperated)');
export const logDir = new commander.Option('-d, --logDir <dir>', 'Absolute path to directory to store rotated logs in')
.default(process.env.LOG_DIR || `${process.cwd()}/logs`, 'process.env.LOG_DIR || process.cwd()/logs');
export const logLevel = new commander.Option('-l, --logLevel <level>', 'Log level')
.default(process.env.LOG_LEVEL || 'verbose', 'process.env.LOG_LEVEL || verbose');
export const wikiConfig = new commander.Option('-w, --wikiConfig <path>', 'Relative url to contextbot wiki page EX https://reddit.com/r/subreddit/wiki/<path>')
.default(process.env.WIKI_CONFIG || 'botconfig/contextbot', "process.env.WIKI_CONFIG || 'botconfig/contextbot'");
export const snooDebug = new commander.Option('--snooDebug', 'Set Snoowrap to debug')
.argParser(parseBool)
.default(process.env.SNOO_DEBUG || false, 'process.env.SNOO_DEBUG || false');
export const authorTTL = new commander.Option('--authorTTL <ms>', 'Set the TTL (ms) for the Author Activities shared cache')
.argParser(argParseInt)
.default(process.env.AUTHOR_TTL || 10000, 'process.env.AUTHOR_TTL || 10000');
export const heartbeat = new commander.Option('--heartbeat <s>', 'Interval, in seconds, between heartbeat logs. Set to 0 to disable')
.argParser(argParseInt)
//heartbeat.defaultValueDescription = 'process.env.HEARTBEAT || 300';
.default(process.env.HEARTBEAT || 300, 'process.env.HEARTBEAT || 300');
export const apiRemaining = new commander.Option('--apiLimitWarning <remaining>', 'When API limit remaining (600/10min) is lower than this value log statements for limit will be raised to WARN level')
.argParser(argParseInt)
.default(process.env.API_REMAINING || 250, 'process.env.API_REMAINING || 250');
export const dryRun = new commander.Option('--dryRun', 'Set dryRun=true for all checks/actions on all subreddits (overrides any existing)')
.argParser(parseBool)
.default(process.env.DRYRUN || false, 'process.env.DRYRUN || false');
export const disableCache = new commander.Option('--disableCache', 'Disable caching for all subreddits')
.argParser(parseBool)
.default(process.env.DISABLE_CACHE || false, 'process.env.DISABLE_CACHE || false');
export const checks = new commander.Option('-h, --checks <checkNames...>', 'An optional list of Checks, by name, that should be run. If none are specified all Checks for the Subreddit the Activity is in will be run');
export const limit = new commander.Option('--limit <limit>', 'Limit the number of unmoderated activities pulled for each subreddit')
.argParser(parseInt);
export const getUniversalOptions = (): commander.Option[] => {
let options = [];
const clientId = new commander.Option('-c, --clientId <id>', 'Client ID for your Reddit application (default: process.env.CLIENT_ID)')
.default(process.env.CLIENT_ID);
clientId.required = true;
const clientSecret = new commander.Option('-e, --clientSecret <secret>', 'Client Secret for your Reddit application (default: process.env.CLIENT_SECRET)')
.default(process.env.CLIENT_SECRET);
clientSecret.required = true;
const accessToken = new commander.Option('-a, --accessToken <token>', 'Access token retrieved from authenticating an account with your Reddit Application (default: process.env.ACCESS_TOKEN)')
.default(process.env.ACCESS_TOKEN);
accessToken.required = true;
const refreshToken = new commander.Option('-r, --refreshToken <token>', 'Refresh token retrieved from authenticating an account with your Reddit Application (default: process.env.REFRESH_TOKEN)')
.default(process.env.REFRESH_TOKEN);
refreshToken.required = true;
const subreddits = new commander.Option('-s, --subreddits <list...>', 'List of subreddits to run on. Bot will run on all subs it has access to if not defined')
.default(process.env.SUBREDDITS || [], 'process.env.SUBREDDITS (comma-seperated)');
const logDir = new commander.Option('-d, --logDir <dir>', 'Absolute path to directory to store rotated logs in')
.default(process.env.LOG_DIR || `${process.cwd()}/logs`, 'process.env.LOG_DIR || process.cwd()/logs');
const logLevel = new commander.Option('-l, --logLevel <level>', 'Log level')
.default(process.env.LOG_LEVEL || 'info', 'process.env.LOG_LEVEL || info');
const wikiConfig = new commander.Option('-w, --wikiConfig <path>', 'Relative url to contextbot wiki page EX https://reddit.com/r/subreddit/wiki/<path>')
.default(process.env.WIKI_CONFIG || 'botconfig/contextbot', "process.env.WIKI_CONFIG || 'botconfig/contextbot'");
const snooDebug = new commander.Option('--snooDebug', 'Set Snoowrap to debug')
.argParser(parseBool)
.default(process.env.SNOO_DEBUG || false, 'process.env.SNOO_DEBUG || false');
const authorTTL = new commander.Option('--authorTTL <ms>', 'Set the TTL (ms) for the Author Activities shared cache')
.argParser(argParseInt)
.default(process.env.AUTHOR_TTL || 10000, 'process.env.AUTHOR_TTL || 10000');
const heartbeat = new commander.Option('--heartbeat <s>', 'Interval, in seconds, between heartbeat logs. Set to 0 to disable')
.argParser(argParseInt)
//heartbeat.defaultValueDescription = 'process.env.HEARTBEAT || 300';
.default(process.env.HEARTBEAT || 300, 'process.env.HEARTBEAT || 300');
const apiRemaining = new commander.Option('--apiLimitWarning <remaining>', 'When API limit remaining (600/10min) is lower than this value log statements for limit will be raised to WARN level')
.argParser(argParseInt)
.default(process.env.API_REMAINING || 250, 'process.env.API_REMAINING || 250');
const dryRun = new commander.Option('--dryRun', 'Set dryRun=true for all checks/actions on all subreddits (overrides any existing)')
.argParser(parseBool)
.default(process.env.DRYRUN || false, 'process.env.DRYRUN || false');
const disableCache = new commander.Option('--disableCache', 'Disable caching for all subreddits')
.argParser(parseBool)
.default(process.env.DISABLE_CACHE || false, 'process.env.DISABLE_CACHE || false');
options.push(dryRun);
options = [

7
src/Utils/SimpleError.ts Normal file
View File

@@ -0,0 +1,7 @@
import ExtendableError from "es6-error";
class SimpleError extends ExtendableError {
}
export default SimpleError;

View File

@@ -4,9 +4,17 @@ import {Duration, DurationUnitsObjectType} from "dayjs/plugin/duration";
import dayjs, {Dayjs} from "dayjs";
import Mustache from "mustache";
import he from "he";
import {AuthorOptions, AuthorCriteria, RuleResult} from "../Rule";
import {ActivityWindowCriteria, ActivityWindowType} from "../Common/interfaces";
import {normalizeName, truncateStringToLength} from "../util";
import {AuthorCriteria, RuleResult, UserNoteCriteria} from "../Rule";
import {
ActivityWindowType,
CommentState,
DurationVal,
SubmissionState,
TypedActivityStates
} from "../Common/interfaces";
import {isActivityWindowCriteria, normalizeName, truncateStringToLength} from "../util";
import UserNotes from "../Subreddit/UserNotes";
import {Logger} from "winston";
export interface AuthorTypedActivitiesOptions extends AuthorActivitiesOptions {
type?: 'comment' | 'submission',
@@ -14,36 +22,61 @@ export interface AuthorTypedActivitiesOptions extends AuthorActivitiesOptions {
export interface AuthorActivitiesOptions {
window: ActivityWindowType | Duration
chunkSize?: number
chunkSize?: number,
}
export async function getAuthorActivities(user: RedditUser, options: AuthorTypedActivitiesOptions): Promise<Array<Submission | Comment>> {
const {chunkSize: cs = 100} = options;
const {
chunkSize: cs = 100,
window: optWindow
} = options;
let window: number | Dayjs,
chunkSize = Math.min(cs, 100);
if (typeof options.window !== 'number') {
let satisfiedCount: number | undefined,
satisfiedEndtime: Dayjs | undefined,
chunkSize = Math.min(cs, 100),
satisfy = 'any';
let durVal: DurationVal | undefined;
let duration: Duration | undefined;
if(isActivityWindowCriteria(optWindow)) {
const { satisfyOn = 'any', count, duration } = optWindow;
satisfiedCount = count;
durVal = duration;
satisfy = satisfyOn
} else if(typeof optWindow === 'number') {
satisfiedCount = optWindow;
} else {
durVal = optWindow as DurationVal;
}
// if count is less than max limit (100) go ahead and just get that many. may result in faster response time for low numbers
if(satisfiedCount !== undefined) {
chunkSize = Math.min(chunkSize, satisfiedCount);
}
if(durVal !== undefined) {
const endTime = dayjs();
let d;
if (dayjs.isDuration(options.window)) {
d = options.window;
} else {
if (!dayjs.isDuration(durVal)) {
// @ts-ignore
d = dayjs.duration(options.window);
duration = dayjs.duration(durVal);
}
if (!dayjs.isDuration(d)) {
if (!dayjs.isDuration(duration)) {
// TODO print object
throw new Error('window given was not a number, a valid ISO8601 duration, a Day.js duration, or well-formed Duration options');
}
window = endTime.subtract(d.asMilliseconds(), 'milliseconds');
} else {
window = options.window;
// use whichever is smaller so we only do one api request if window is smaller than default chunk size
chunkSize = Math.min(chunkSize, window);
satisfiedEndtime = endTime.subtract(duration.asMilliseconds(), 'milliseconds');
}
if(satisfiedCount === undefined && satisfiedEndtime === undefined) {
throw new Error('window value was not valid');
} else if(satisfy === 'all' && !(satisfiedCount !== undefined && satisfiedEndtime !== undefined)) {
// even though 'all' was requested we don't have two criteria so its really 'any' logic
satisfy = 'any';
}
let items: Array<Submission | Comment> = [];
let lastItemDate;
//let count = 1;
let listing;
switch (options.type) {
@@ -61,33 +94,58 @@ export async function getAuthorActivities(user: RedditUser, options: AuthorTyped
let offset = chunkSize;
while (!hitEnd) {
if (typeof window === 'number') {
hitEnd = listing.length >= window;
} else {
const listSlice = listing.slice(offset - chunkSize);
let countOk = false,
timeOk = false;
const truncatedItems = listSlice.filter((x) => {
const listSlice = listing.slice(offset - chunkSize)
if (satisfiedCount !== undefined && items.length + listSlice.length >= satisfiedCount) {
// satisfied count
if(satisfy === 'any') {
items = items.concat(listSlice).slice(0, satisfiedCount);
break;
}
countOk = true;
}
let truncatedItems: Array<Submission | Comment> = [];
if(satisfiedEndtime !== undefined) {
truncatedItems = listSlice.filter((x) => {
const utc = x.created_utc * 1000;
const itemDate = dayjs(utc);
// @ts-ignore
return window.isBefore(itemDate);
return satisfiedEndtime.isBefore(itemDate);
});
if(truncatedItems.length !== listSlice.length) {
hitEnd = true;
if (truncatedItems.length !== listSlice.length) {
if(satisfy === 'any') {
// satisfied duration
items = items.concat(truncatedItems);
break;
}
timeOk = true;
}
items = items.concat(truncatedItems);
}
if (!hitEnd) {
hitEnd = listing.isFinished;
// if we've satisfied everything take whichever is bigger
if(satisfy === 'all' && countOk && timeOk) {
if(satisfiedCount as number > items.length + truncatedItems.length) {
items = items.concat(listSlice).slice(0, satisfiedCount);
} else {
items = items.concat(truncatedItems);
}
break;
}
// if we got this far neither count nor time was satisfied (or both) so just add all items from listing and fetch more if possible
items = items.concat(listSlice);
hitEnd = listing.isFinished;
if (!hitEnd) {
offset += chunkSize;
listing = await listing.fetchMore({amount: chunkSize});
} else if(typeof window === 'number') {
items = listing.slice(0, window + 1);
}
}
// TODO truncate items to window size when duration
return Promise.resolve(items);
}
@@ -136,62 +194,111 @@ export const renderContent = async (content: string, data: (Submission | Comment
return he.decode(Mustache.render(content, {item: templateData, rules: normalizedRuleResults}));
}
export const testAuthorCriteria = async (item: (Comment|Submission), authorOpts: AuthorCriteria, include = true) => {
export const testAuthorCriteria = async (item: (Comment | Submission), authorOpts: AuthorCriteria, include = true, userNotes: UserNotes) => {
// @ts-ignore
const author: RedditUser = await item.author;
for(const k of Object.keys(authorOpts)) {
switch(k) {
case 'name':
const authPass = () => {
// @ts-ignore
for (const n of authorOpts[k]) {
if (n.toLowerCase() === author.name.toLowerCase()) {
return true;
}
}
return false;
}
if((include && !authPass) || (!include && authPass)) {
return false;
}
break;
case 'flairCssClass':
const css = await item.author_flair_css_class;
const cssPass = () => {
// @ts-ignore
for(const c of authorOpts[k]) {
if(c === css) {
return;
for (const k of Object.keys(authorOpts)) {
// @ts-ignore
if (authorOpts[k] !== undefined) {
switch (k) {
case 'name':
const authPass = () => {
// @ts-ignore
for (const n of authorOpts[k]) {
if (n.toLowerCase() === author.name.toLowerCase()) {
return true;
}
}
return false;
}
return false;
}
if((include && !cssPass) || (!include && cssPass)) {
return false;
}
break;
case 'flairText':
const text = await item.author_flair_text;
const textPass = () => {
// @ts-ignore
for(const c of authorOpts[k]) {
if(c === text) {
return
const authResult = authPass();
if ((include && !authResult) || (!include && authResult)) {
return false;
}
break;
case 'flairCssClass':
const css = await item.author_flair_css_class;
const cssPass = () => {
// @ts-ignore
for (const c of authorOpts[k]) {
if (c === css) {
return;
}
}
return false;
}
return false;
}
if((include && !textPass) || (!include && textPass)) {
return false;
}
break;
case 'isMod':
const mods: RedditUser[] = await item.subreddit.getModerators();
const isModerator = mods.some(x => x.name === item.author.name);
const modMatch = authorOpts.isMod === isModerator;
if((include && !modMatch) || (!include && !modMatch)) {
return false;
}
const cssResult = cssPass();
if ((include && !cssResult) || (!include && cssResult)) {
return false;
}
break;
case 'flairText':
const text = await item.author_flair_text;
const textPass = () => {
// @ts-ignore
for (const c of authorOpts[k]) {
if (c === text) {
return
}
}
return false;
};
const textResult = textPass();
if ((include && !textResult) || (!include && textResult)) {
return false;
}
break;
case 'isMod':
const mods: RedditUser[] = await item.subreddit.getModerators();
const isModerator = mods.some(x => x.name === item.author.name);
const modMatch = authorOpts.isMod === isModerator;
if ((include && !modMatch) || (!include && !modMatch)) {
return false;
}
break;
case 'userNotes':
const notes = await userNotes.getUserNotes(item.author);
const notePass = () => {
for (const noteCriteria of authorOpts[k] as UserNoteCriteria[]) {
const {count = 1, order = 'descending', search = 'current', type} = noteCriteria;
switch (search) {
case 'current':
if (notes.length > 0 && notes[notes.length - 1].noteType === type) {
return true;
}
break;
case 'consecutive':
let orderedNotes = notes;
if (order === 'descending') {
orderedNotes = [...notes];
orderedNotes.reverse();
}
let currCount = 0;
for (const note of orderedNotes) {
if (note.noteType === type) {
currCount++;
} else {
currCount = 0;
}
if (currCount >= count) {
return true;
}
}
break;
case 'total':
if (notes.filter(x => x.noteType === type).length >= count) {
return true;
}
}
}
return false;
}
const noteResult = notePass();
if ((include && !noteResult) || (!include && noteResult)) {
return false;
}
break;
}
}
}
return true;
@@ -260,3 +367,36 @@ export const getAttributionIdentifier = (sub: Submission, useParentMediaDomain =
return domain;
}
export const isItem = (item: Submission | Comment, stateCriteria: TypedActivityStates, logger: Logger): [boolean, SubmissionState|CommentState|undefined] => {
if (stateCriteria.length === 0) {
return [true, undefined];
}
const log = logger.child({leaf: 'Item Check'});
for (const crit of stateCriteria) {
const [pass, passCrit] = (() => {
for (const k of Object.keys(crit)) {
// @ts-ignore
if (crit[k] !== undefined) {
// @ts-ignore
if (item[k] !== undefined) {
// @ts-ignore
if (item[k] !== crit[k]) {
return [false, crit];
}
} else {
log.warn(`Tried to test for Item property '${k}' but it did not exist`);
}
}
}
log.verbose(`itemIs passed: ${JSON.stringify(crit)}`);
return [true, crit];
})() as [boolean, SubmissionState|CommentState|undefined];
if (pass) {
return [true, passCrit];
}
}
return [false, undefined];
}

View File

@@ -6,7 +6,7 @@ import dduration from 'dayjs/plugin/duration.js';
import relTime from 'dayjs/plugin/relativeTime.js';
import {Manager} from "./Subreddit/Manager";
import {Command} from 'commander';
import {getOptions} from "./Utils/CommandConfig";
import {checks, getUniversalOptions, limit} from "./Utils/CommandConfig";
import {App} from "./App";
import Submission from "snoowrap/dist/objects/Submission";
import {COMMENT_URL_ID, parseLinkIdentifier, SUBMISSION_URL_ID} from "./util";
@@ -19,7 +19,7 @@ const commentReg = parseLinkIdentifier([COMMENT_URL_ID]);
const submissionReg = parseLinkIdentifier([SUBMISSION_URL_ID]);
const program = new Command();
for (const o of getOptions()) {
for (const o of getUniversalOptions()) {
program.addOption(o);
}
@@ -41,7 +41,7 @@ for (const o of getOptions()) {
activityIdentifier: 'Either a permalink URL or the ID of the Comment or Submission',
type: `If activityIdentifier is not a permalink URL then the type of activity ('comment' or 'submission'). May also specify 'submission' type when using a permalink to a comment to get the Submission`,
})
.option('-h, --checks <checkNames...>', 'An optional list of Checks, by name, that should be run. If none are specified all Checks for the Subreddit the Activity is in will be run')
.addOption(checks)
.action(async (activityIdentifier, type, commandOptions = {}) => {
const {checks = []} = commandOptions;
const app = new App(program.opts());
@@ -93,6 +93,26 @@ for (const o of getOptions()) {
}
});
program.command('unmoderated <subreddits...>')
.description('Run checks on all unmoderated activity in the modqueue', {
subreddits: 'The list of subreddits to run on. If not specified will run on all subreddits the account has moderation access to.'
})
.addOption(checks)
.addOption(limit)
.action(async (subreddits = [], commandOptions = {}) => {
const {checks = [], limit = 100} = commandOptions;
const app = new App(program.opts());
await app.buildManagers(subreddits);
for(const manager of app.subManagers) {
const activities = await manager.subreddit.getUnmoderated({limit});
for(const a of activities.reverse()) {
await manager.runChecks(a instanceof Submission ? 'Submission' : 'Comment', a, checks);
}
}
});
await program.parseAsync();

View File

@@ -9,6 +9,12 @@ import Ajv from "ajv";
import {InvalidOptionArgumentError} from "commander";
import Submission from "snoowrap/dist/objects/Submission";
import {Comment} from "snoowrap";
import {inflateSync, deflateSync} from "zlib";
import pako from "pako";
import {ActivityWindowCriteria} from "./Common/interfaces";
import JSON5 from "json5";
import yaml, {JSON_SCHEMA} from "js-yaml";
import SimpleError from "./Utils/SimpleError";
dayjs.extend(utc);
dayjs.extend(dduration);
@@ -18,9 +24,22 @@ const {combine, printf, timestamp, label, splat, errors} = format;
const s = splat();
const SPLAT = Symbol.for('splat')
const errorsFormat = errors({stack: true});
//const errorsFormat = errors({stack: true});
const CWD = process.cwd();
// const errorAwareFormat = (info: any) => {
// if(info instanceof SimpleError) {
// return errors()(info);
// }
// }
const errorAwareFormat = {
transform: (info: any, opts: any) => {
// don't need to log stack trace if we know the error is just a simple message (we handled it)
const stack = !(info instanceof SimpleError) && !(info.message instanceof SimpleError);
return errors().transform(info, { stack });
}
}
export const truncateStringToLength = (length: number, truncStr = '...') => (str: string) => str.length > length ? `${str.slice(0, length - truncStr.length - 1)}${truncStr}` : str;
export const defaultFormat = printf(({
@@ -68,7 +87,8 @@ export const labelledFormat = (labelName = 'App') => {
),
l,
s,
errorsFormat,
errorAwareFormat,
//errorsFormat,
defaultFormat,
);
}
@@ -305,3 +325,74 @@ export function activityWindowText(activities: (Submission | Comment)[], suffix
export function normalizeName(val: string) {
return val.trim().replace(/\W+/g, '').toLowerCase()
}
// https://github.com/toolbox-team/reddit-moderator-toolbox/wiki/Subreddit-Wikis%3A-usernotes#working-with-the-blob
export const inflateUserNotes = (blob: string) => {
//const binaryData = Buffer.from(blob, 'base64').toString('binary');
//const str = pako.inflate(binaryData, {to: 'string'});
const buffer = Buffer.from(blob, 'base64');
const str = inflateSync(buffer).toString('utf-8');
// @ts-ignore
return JSON.parse(str);
}
// https://github.com/toolbox-team/reddit-moderator-toolbox/wiki/Subreddit-Wikis%3A-usernotes#working-with-the-blob
export const deflateUserNotes = (usersObject: object) => {
const jsonString = JSON.stringify(usersObject);
// Deflate/compress the string
//const binaryData = pako.deflate(jsonString);
const binaryData = deflateSync(jsonString);
// Convert binary data to a base64 string with a Buffer
const blob = Buffer.from(binaryData).toString('base64');
return blob;
}
export const isActivityWindowCriteria = (val: any): val is ActivityWindowCriteria => {
if (val !== null && typeof val === 'object') {
return (val.count !== undefined && typeof val.count === 'number') ||
// close enough
val.duration !== undefined;
}
return false;
}
export const parseFromJsonOrYamlToObject = (content: string): [object?, Error?, Error?] => {
let obj;
let jsonErr,
yamlErr;
try {
obj = JSON5.parse(content);
const oType = obj === null ? 'null' : typeof obj;
if (oType !== 'object') {
jsonErr = new SimpleError(`Parsing as json produced data of type '${oType}' (expected 'object')`);
obj = undefined;
}
} catch (err) {
jsonErr = err;
}
if (obj === undefined) {
try {
obj = yaml.load(content, {schema: JSON_SCHEMA, json: true});
const oType = obj === null ? 'null' : typeof obj;
if (oType !== 'object') {
yamlErr = new Error(`Parsing as yaml produced data of type '${oType}' (expected 'object')`);
obj = undefined;
}
} catch (err) {
yamlErr = err;
}
}
return [obj, jsonErr, yamlErr];
}
export const generateFooter = async (item: Submission | Comment) => {
const subName = await item.subreddit.display_name;
// TODO customize modmail message based on action being peformed
const modmailLink = `https://www.reddit.com/message/compose?to=%2Fr%2F${subName}&message=Reminder:%20If+you+are+messaging+about+a+post+removal+,+please+include+the%20post%20URL%20somewhere%20in%20the%20message.`
return `\r\n*****\r\nThis action was performed by [a bot.](https://www.reddit.com/r/ContextModBot/comments/o1dugk/introduction_to_contextmodbot_and_rcb/) Mention a moderator or [send a modmail](${modmailLink}) if you any ideas, questions , or concerns about this action.`
}