mirror of
https://github.com/FoxxMD/context-mod.git
synced 2026-01-14 07:57:57 -05:00
Compare commits
28 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a6e72dc79d | ||
|
|
962e44bf57 | ||
|
|
2189d92725 | ||
|
|
14711efeb3 | ||
|
|
774b41c2a3 | ||
|
|
4928b8b57a | ||
|
|
4b39794e2f | ||
|
|
c0ede0561c | ||
|
|
d7cea1f705 | ||
|
|
3e29d7eb9f | ||
|
|
48ea60f886 | ||
|
|
1897d96a8f | ||
|
|
1279975a8a | ||
|
|
7d0f7e8714 | ||
|
|
f6b3f02e05 | ||
|
|
1f439dc290 | ||
|
|
0a299308fb | ||
|
|
a84b39cc5a | ||
|
|
f64c6e0df5 | ||
|
|
250313b6a8 | ||
|
|
e4be9ed4e6 | ||
|
|
788af401b3 | ||
|
|
6bc74b383f | ||
|
|
51825a594d | ||
|
|
186d9ac4b7 | ||
|
|
ac02fdabfd | ||
|
|
0eeb204371 | ||
|
|
64a97ee048 |
2
.idea/redditcontextbot.iml
generated
2
.idea/redditcontextbot.iml
generated
@@ -5,7 +5,9 @@
|
||||
<excludeFolder url="file://$MODULE_DIR$/temp" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/tmp" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/src/logs" />
|
||||
</content>
|
||||
<content url="file://$MODULE_DIR$/node_modules" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
|
||||
102
README.md
102
README.md
@@ -24,6 +24,7 @@ Some feature highlights:
|
||||
* 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
|
||||
* Rules and Actions support named references so you write rules/actions once and reference them anywhere
|
||||
* User-configurable global/subreddit-level API caching
|
||||
* Docker container support
|
||||
|
||||
# Table of Contents
|
||||
@@ -31,6 +32,7 @@ Some feature highlights:
|
||||
* [How It Works](#how-it-works)
|
||||
* [Installation](#installation)
|
||||
* [Configuration](#configuration)
|
||||
* [Examples](#examples)
|
||||
* [Usage](#usage)
|
||||
|
||||
### How It Works
|
||||
@@ -85,10 +87,14 @@ docker run -e "CLIENT_ID=myId" ... foxxmd/reddit-context-bot
|
||||
|
||||
## Configuration
|
||||
|
||||
Context Bot's [configuration schema](/src/Schema/App.json) conforms to [JSON Schema](https://json-schema.org/) Draft 7.
|
||||
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/).
|
||||
|
||||
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.
|
||||
|
||||
### Examples
|
||||
|
||||
Read through the [Examples](/examples) section for a thorough introduction to all the **Rules**, in-depth concepts, and sample configuration files.
|
||||
|
||||
### Action Templating
|
||||
|
||||
Actions that can submit text (Report, Comment) will have their `content` values run through a [Mustache Template](https://mustache.github.io/). This means you can insert data generated by Rules into your text before the Action is performed.
|
||||
@@ -157,89 +163,37 @@ const content = "My content will render the property {{item.aProperty}} like thi
|
||||
```
|
||||
</details>
|
||||
|
||||
### Example Config
|
||||
|
||||
Below is a configuration fulfilling the example given at the start of this readme:
|
||||
|
||||
<details>
|
||||
<summary>Click to expand configuration</summary>
|
||||
|
||||
```json
|
||||
{
|
||||
"checks": [
|
||||
{
|
||||
"name": "repeatSpam",
|
||||
"kind": "submission",
|
||||
"rules": [
|
||||
{
|
||||
"kind": "repeatActivity",
|
||||
"gapAllowance": 2,
|
||||
"threshold": 10
|
||||
}
|
||||
],
|
||||
"actions": [
|
||||
{
|
||||
"kind": "remove"
|
||||
},
|
||||
{
|
||||
"kind": "comment",
|
||||
"content": "Thank you for your submission but we do not allow mass crossposting. Your submission has been removed",
|
||||
"distingish": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "selfPromoActivity",
|
||||
"kind": "submission",
|
||||
"rules": [
|
||||
{
|
||||
"kind": "recentActivity",
|
||||
"thresholds": [
|
||||
{
|
||||
"subreddits": [
|
||||
"YouTubeSubscribeBoost",
|
||||
"AdvertiseYourVideos"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"actions": [
|
||||
{
|
||||
"kind": "report",
|
||||
"content": "User posted link {{rules.recentactivity.totalCount}} times in {{rules.recentactivity.subCount}} SP subs: {{rules.recentactivity.summary}}"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
```
|
||||
</details>
|
||||
|
||||
## Usage
|
||||
|
||||
```
|
||||
Usage: index [options] [command]
|
||||
|
||||
Options:
|
||||
-c, --clientId <id> Client ID for your Reddit application (default: process.env.CLIENT_ID)
|
||||
-e, --clientSecret <secret> Client Secret for your Reddit application (default: process.env.CLIENT_SECRET)
|
||||
-a, --accessToken <token> Access token retrieved from authenticating an account with your Reddit Application (default: process.env.ACCESS_TOKEN)
|
||||
-r, --refreshToken <token> Refresh token retrieved from authenticating an account with your Reddit Application (default: process.env.REFRESH_TOKEN)
|
||||
-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 as comma-separated string)
|
||||
-d, --logDir <dir> Absolute path to directory to store rotated logs in (default: process.env.LOG_DIR || 'CWD/logs')
|
||||
-l, --logLevel <level> Log level (default: process.env.LOG_LEVEL || 'info')
|
||||
-w, --wikiConfig <path> Relative url to contextbot wiki page EX https://reddit.com/r/subreddit/wiki/<path> (default: process.env.WIKI_CONFIG || 'botconfig/contextbot')
|
||||
-n, --snooDebug Set Snoowrap to debug (default: process.env.SNOO_DEBUG || false)
|
||||
-h, --help display help for command
|
||||
-c, --clientId <id> Client ID for your Reddit application (default: process.env.CLIENT_ID)
|
||||
-e, --clientSecret <secret> Client Secret for your Reddit application (default: process.env.CLIENT_SECRET)
|
||||
-a, --accessToken <token> Access token retrieved from authenticating an account with your Reddit Application (default: process.env.ACCESS_TOKEN)
|
||||
-r, --refreshToken <token> Refresh token retrieved from authenticating an account with your Reddit Application (default: process.env.REFRESH_TOKEN)
|
||||
-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 (comma-seperated))
|
||||
-d, --logDir <dir> Absolute path to directory to store rotated logs in (default: process.env.LOG_DIR || process.cwd()/logs)
|
||||
-l, --logLevel <level> Log level (default: process.env.LOG_LEVEL || info)
|
||||
-w, --wikiConfig <path> Relative url to contextbot wiki page EX https://reddit.com/r/subreddit/wiki/<path> (default: process.env.WIKI_CONFIG || 'botconfig/contextbot')
|
||||
--snooDebug Set Snoowrap to debug (default: process.env.SNOO_DEBUG || false)
|
||||
--authorTTL <ms> Set the TTL (ms) for the Author Activities shared cache (default: process.env.AUTHOR_TTL || 10000)
|
||||
--heartbeat <s> Interval, in seconds, between heartbeat logs. Set to 0 to disable (default: process.env.HEARTBEAT || 300)
|
||||
--apiLimitWarning <remaining> When API limit remaining (600/10min) is lower than this value log statements for limit will be raised to WARN level (default: process.env.API_REMAINING || 250)
|
||||
--dryRun Set dryRun=true for all checks/actions on all subreddits (overrides any existing) (default: process.env.DRYRUN)
|
||||
--disableCache Disable caching for all subreddits (default: process.env.DISABLE_CACHE || false)
|
||||
-h, --help display help for command
|
||||
|
||||
Commands:
|
||||
run Runs bot normally (unattended)
|
||||
check <activityId> [type] [checkNames...] Run check(s) on a specific activity, then exits
|
||||
help [command] display help for command
|
||||
run Runs bot normally
|
||||
check [options] <activityIdentifier> [type] Run check(s) on a specific activity
|
||||
help [command] display help for command
|
||||
|
||||
```
|
||||
|
||||
### Logging
|
||||
|
||||
### Reddit App??
|
||||
|
||||
To use this bot you must do two things:
|
||||
|
||||
42
examples/README.md
Normal file
42
examples/README.md
Normal file
@@ -0,0 +1,42 @@
|
||||
# Examples
|
||||
|
||||
This directory contains example of valid, ready-to-go configurations for Context Bot for the purpose of:
|
||||
|
||||
* showcasing what the bot can do
|
||||
* providing best practices for writing your configuration
|
||||
* providing generally useful configurations **that can be used immediately** or as a jumping-off point for your configuration
|
||||
|
||||
### Creating Your Configuration
|
||||
|
||||
#### Get the raw contents of the configuration
|
||||
|
||||
* In a new tab open the github page for the configuration you want ([example](/examples/repeatActivity/crosspostSpamming.json5))
|
||||
* Click the **Raw** button...keep this tab open and move on to the next step
|
||||
|
||||
#### Edit your wiki configuration
|
||||
|
||||
* Visit the wiki page of the subreddit you want the bot to moderate
|
||||
* Using default bot settings this will be `https://old.reddit.com/r/YOURSUBERDDIT/wiki/botconfig/contextbot`
|
||||
* If the page does not exist create it, otherwise click **Edit**
|
||||
* Copy-paste the configuration into the wiki text box
|
||||
* In the previous tab you opened (for the configuration) **Select All** (Ctrl+A), then **Copy**
|
||||
* On the wiki page **Paste** into the text box
|
||||
* Save the edited wiki page
|
||||
* Ensure the wiki page visibility is restricted
|
||||
* On the wiki page click **settings** (**Page settings** in new reddit)
|
||||
* Check the box for **Only mods may edit and view** and then **save**
|
||||
|
||||
### Examples Overview
|
||||
|
||||
* Rules
|
||||
* [Attribution](/examples/attribution)
|
||||
* [Recent Activity](/examples/recentActivity)
|
||||
* [Repeat Activity](/examples/repeatActivity)
|
||||
* [History](/examples/history)
|
||||
* [Author](/examples/author)
|
||||
* [Advanced Concepts](/examples/advancedConcepts)
|
||||
* [Rule Sets](/examples/advancedConcepts/ruleSets.json5)
|
||||
* [Name Rules](/examples/advancedConcepts/ruleNameReuse.json5)
|
||||
* [Check Ordering](/examples/advancedConcepts)
|
||||
* Subreddit-ready examples
|
||||
* Coming soon...
|
||||
56
examples/advancedConcepts/README.md
Normal file
56
examples/advancedConcepts/README.md
Normal file
@@ -0,0 +1,56 @@
|
||||
### Named Rules
|
||||
|
||||
See [ruleNameReuse.json5](/examples/advancedConcepts/ruleNameReuse.json5)
|
||||
|
||||
### Check Order
|
||||
|
||||
Checks are run in the order they appear in your configuration, therefore you should place your highest requirement/severe action checks at the top and lowest requirement/moderate actions at the bottom.
|
||||
|
||||
This is so that if an Activity warrants a more serious reaction that Check is triggered first rather than having a lower requirement check with less severe actions triggered and causing all subsequent Checks to be skipped.
|
||||
|
||||
* Attribution >50% AND Repeat Activity 8x AND Recent Activity in 2 subs => remove submission + ban
|
||||
* Attribution >20% AND Repeat Activity 4x AND Recent Activity in 5 subs => remove submission + flair user restricted
|
||||
* Attribution >20% AND Repeat Activity 2x => remove submission
|
||||
* Attribution >20% AND History comments <30% => remove submission
|
||||
* Attribution >15% => report
|
||||
* Repeat Activity 2x => report
|
||||
* Recent Activity in 3 subs => report
|
||||
* Author not vetted => flair new user submission
|
||||
|
||||
### Rule Sets
|
||||
|
||||
The `rules` array on a `Checks` can contain both `Rule` objects and `RuleSet` objects.
|
||||
|
||||
A **Rule Set** is a "nested" set of `Rule` objects with a passing condition specified. These allow you to create more complex trigger behavior by combining multiple rules.
|
||||
|
||||
See **[ruleSets.json5](/examples/advancedConcepts/ruleSets.json5)** for a complete example as well as consulting the [schema](https://json-schema.app/view/%23%2Fdefinitions%2FRuleSetJson?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Freddit-context-bot%2Fmaster%2Fsrc%2FSchema%2FApp.json).
|
||||
|
||||
### Rule Order
|
||||
|
||||
The ordering of your Rules within a Check/RuleSet can have an impact on Check performance (speed) as well as API usage.
|
||||
|
||||
Consider these three rules:
|
||||
|
||||
* Rule A -- Recent Activity => 3 subreddits => last 15 submissions
|
||||
* Rule B -- Repeat Activity => last 3 days
|
||||
* Rule C -- Attribution => >10% => last 90 days or 300 submissions
|
||||
|
||||
The first two rules are lightweight in their requirements -- Rule A can be completed in 1 API call, Rule B potentially completed in 1 Api call.
|
||||
|
||||
However, depending on how active the Author is, Rule C will take *at least* 3 API calls just to get all activities (Reddit limit 100 items per call).
|
||||
|
||||
If the Check is using `AND` condition for its rules (default) then if either Rule A or Rule B fail then Rule C will never run. This means 3 API calls never made plus the time waiting for each to return.
|
||||
|
||||
**It is therefore advantageous to list your lightweight Rules first in each Check.**
|
||||
|
||||
### API Caching
|
||||
|
||||
Context bot implements some basic caching functionality for **Author Activities** and wiki pages (on Comment/Report Actions).
|
||||
|
||||
**Author Activities** are cached for a subreddit-configurable amount of time (10 seconds by default). A cached activities set can be re-used if the **window on a Rule is identical to the window on another Rule**.
|
||||
|
||||
This means that when possible you should re-use window values.
|
||||
|
||||
IE If you want to check an Author's Activities for a time range try to always use **7 Days** or always use **50 Items** for absolute counts.
|
||||
|
||||
Re-use will result in less API calls and faster Check times.
|
||||
79
examples/advancedConcepts/ruleNameReuse.json5
Normal file
79
examples/advancedConcepts/ruleNameReuse.json5
Normal file
@@ -0,0 +1,79 @@
|
||||
{
|
||||
"checks": [
|
||||
{
|
||||
"name": "Auto Remove SP Karma",
|
||||
"description": "Remove submission because author has self-promo >10% and posted in karma subs recently",
|
||||
"kind": "submission",
|
||||
"rules": [
|
||||
// named rules can be referenced at any point in the configuration (where they occur does not matter)
|
||||
// and can be used in any Check
|
||||
// Note: rules do not transfer between subreddit configurations
|
||||
"freekarmasub",
|
||||
{
|
||||
"name": "attr10all",
|
||||
"kind": "attribution",
|
||||
"criteria": [
|
||||
{
|
||||
"threshold": "10%",
|
||||
"window": {
|
||||
"days": 90
|
||||
}
|
||||
},
|
||||
{
|
||||
"threshold": "10%",
|
||||
"window": 100
|
||||
}
|
||||
],
|
||||
}
|
||||
],
|
||||
"actions": [
|
||||
{
|
||||
"kind": "remove"
|
||||
},
|
||||
{
|
||||
"kind": "comment",
|
||||
"content": "Your submission was removed because you are over reddit's threshold for self-promotion and recently posted this content in a karma sub"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Free Karma On Submission Alert",
|
||||
"description": "Check if author has posted this submission in 'freekarma' subreddits",
|
||||
"kind": "submission",
|
||||
"rules": [
|
||||
{
|
||||
// rules can be re-used throughout a configuration by referencing them by name
|
||||
//
|
||||
// The rule name itself can only contain spaces, hyphens and underscores
|
||||
// The value used to reference it will have all of these removed, and lower-cased
|
||||
//
|
||||
// so to reference this rule use the value 'freekarmasub'
|
||||
"name": "Free_Karma-SUB",
|
||||
"kind": "recentActivity",
|
||||
"lookAt": "submissions",
|
||||
"useSubmissionAsReference":true,
|
||||
"thresholds": [
|
||||
{
|
||||
"totalCount": 1,
|
||||
"subreddits": [
|
||||
"DeFreeKarma",
|
||||
"FreeKarma4U",
|
||||
"FreeKarma4You",
|
||||
"upvote"
|
||||
]
|
||||
}
|
||||
],
|
||||
"window": {
|
||||
"days": 7
|
||||
}
|
||||
}
|
||||
],
|
||||
"actions": [
|
||||
{
|
||||
"kind": "report",
|
||||
"content": "Submission posted {{rules.freekarmasub.totalCount}} times in karma {{rules.freekarmasub.subCount}} subs over {{rules.freekarmasub.window}}: {{rules.freekarmasub.subSummary}}"
|
||||
}
|
||||
]
|
||||
},
|
||||
]
|
||||
}
|
||||
99
examples/advancedConcepts/ruleSets.json5
Normal file
99
examples/advancedConcepts/ruleSets.json5
Normal file
@@ -0,0 +1,99 @@
|
||||
{
|
||||
"checks": [
|
||||
{
|
||||
"name": "Self Promo All or low comment",
|
||||
"description": "SP >10% of all activities or >10% of submissions with low comment engagement",
|
||||
"kind": "submission",
|
||||
"rules": [
|
||||
{
|
||||
// this attribution rule is looking at all activities
|
||||
//
|
||||
// we want want this one rule to trigger the check because >10% of all activity (submission AND comments) is a good requirement
|
||||
"name": "attr10all",
|
||||
"kind": "attribution",
|
||||
"criteria": [
|
||||
{
|
||||
"threshold": "10%",
|
||||
"window": {
|
||||
"days": 90
|
||||
}
|
||||
},
|
||||
{
|
||||
"threshold": "10%",
|
||||
"window": 100
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
// this is a **Rule Set**
|
||||
//
|
||||
// it is made up of "nested" rules with a pass condition (AND/OR)
|
||||
// if the nested rules pass the condition then the Rule Set triggers the Check
|
||||
//
|
||||
// AND = all nested rules must be triggered to make the Rule Set trigger
|
||||
// AND = any of the nested Rules will be the Rule Set trigger
|
||||
"condition": "AND",
|
||||
// in this check we use an Attribution >10% on ONLY submissions, which is a lower requirement then the above attribution rule
|
||||
// and combine it with a History rule looking for low comment engagement
|
||||
// to make a "higher" requirement Rule Set our of two low requirement Rules
|
||||
"rules": [
|
||||
{
|
||||
"name": "attr20sub",
|
||||
"kind": "attribution",
|
||||
"criteria": [
|
||||
{
|
||||
"threshold": "10%",
|
||||
"thresholdOn": "submissions",
|
||||
"window": {
|
||||
"days": 90
|
||||
}
|
||||
},
|
||||
{
|
||||
"threshold": "10%",
|
||||
"thresholdOn": "submissions",
|
||||
"window": 100
|
||||
}
|
||||
],
|
||||
"lookAt": "media"
|
||||
},
|
||||
{
|
||||
"name": "lowOrOpComm",
|
||||
"kind": "history",
|
||||
"criteriaJoin": "OR",
|
||||
"criteria": [
|
||||
{
|
||||
"window": {
|
||||
"days": 90
|
||||
},
|
||||
"comment": {
|
||||
"threshold": "50%",
|
||||
"condition": "<"
|
||||
}
|
||||
},
|
||||
{
|
||||
"window": {
|
||||
"days": 90
|
||||
},
|
||||
"comment": {
|
||||
"asOp": true,
|
||||
"threshold": "40%",
|
||||
"condition": ">"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"actions": [
|
||||
{
|
||||
"kind": "remove"
|
||||
},
|
||||
{
|
||||
"kind": "comment",
|
||||
"content": "Your submission was removed because you are over reddit's threshold for self-promotion or exhibit low comment engagement"
|
||||
}
|
||||
]
|
||||
},
|
||||
]
|
||||
}
|
||||
14
examples/attribution/README.md
Normal file
14
examples/attribution/README.md
Normal file
@@ -0,0 +1,14 @@
|
||||
# Attribution
|
||||
|
||||
The **Attribution** rule will aggregate an Author's content Attribution (youtube channels, twitter, website domains, etc.) and can check on their totals or percentages of all Activities over a time period:
|
||||
* Total # of attributions
|
||||
* As percentage of all Activity or only Submissions
|
||||
* Look at all domains or only media (youtube, vimeo, etc.)
|
||||
* Include self posts (by reddit domain) or not
|
||||
|
||||
Consult the [schema](https://json-schema.app/view/%23/%23%2Fdefinitions%2FCheckJson/%23%2Fdefinitions%2FAttributionJSONConfig?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Freddit-context-bot%2Fmaster%2Fsrc%2FSchema%2FApp.json) for a complete reference of the rule's properties.
|
||||
|
||||
### Examples
|
||||
|
||||
* [Self Promotion as percentage of all Activities](/examples/attribution/redditSelfPromoAll.json5) - Check if Author is submitting much more than they comment.
|
||||
* [Self Promotion as percentage of Submissions](/examples/attribution/redditSelfPromoSubmissionsOnly.json5) - Check if any of Author's aggregated submission origins are >10% of their submissions
|
||||
41
examples/attribution/redditSelfPromoAll.json5
Normal file
41
examples/attribution/redditSelfPromoAll.json5
Normal file
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"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 defaults to OR -- so either of these criteria will trigger the rule
|
||||
"criteria": [
|
||||
{
|
||||
// threshold can be a percent or an absolute number
|
||||
"threshold": "10%",
|
||||
// The default is "all" -- calculate percentage of entire history (submissions & comments)
|
||||
// "thresholdOn": "all",
|
||||
|
||||
// look at last 90 days of Author's activities (comments and submissions)
|
||||
"window": {
|
||||
"days": 90
|
||||
}
|
||||
},
|
||||
{
|
||||
"threshold": "10%",
|
||||
// look at Author's last 100 activities (comments and submissions)
|
||||
"window": 100
|
||||
}
|
||||
],
|
||||
}
|
||||
],
|
||||
"actions": [
|
||||
{
|
||||
"kind": "report",
|
||||
"content": "{{rules.attr10all.largestPercent}}% of {{rules.attr10all.activityTotal}} items over {{rules.attr10all.window}}"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
42
examples/attribution/redditSelfPromoSubmissionsOnly.json5
Normal file
42
examples/attribution/redditSelfPromoSubmissionsOnly.json5
Normal file
@@ -0,0 +1,42 @@
|
||||
{
|
||||
"checks": [
|
||||
{
|
||||
"name": "Self Promo Submissions",
|
||||
"description": "Check if any of Author's aggregated submission origins are >10% of their submissions",
|
||||
// check will run on a new submission in your subreddit and look at the Author of that submission
|
||||
"kind": "submission",
|
||||
"rules": [
|
||||
{
|
||||
"name": "attr10sub",
|
||||
"kind": "attribution",
|
||||
// criteria defaults to OR -- so either of these criteria will trigger the rule
|
||||
"criteria": [
|
||||
{
|
||||
// threshold can be a percent or an absolute number
|
||||
"threshold": "10%",
|
||||
// calculate percentage of submissions, rather than entire history (submissions & comments)
|
||||
"thresholdOn": "submissions",
|
||||
|
||||
// look at last 90 days of Author's activities (comments and submissions)
|
||||
"window": {
|
||||
"days": 90
|
||||
}
|
||||
},
|
||||
{
|
||||
"threshold": "10%",
|
||||
"thresholdOn": "submissions",
|
||||
// look at Author's last 100 activities (comments and submissions)
|
||||
"window": 100
|
||||
}
|
||||
],
|
||||
}
|
||||
],
|
||||
"actions": [
|
||||
{
|
||||
"kind": "report",
|
||||
"content": "{{rules.attr10sub.largestPercent}}% of {{rules.attr10sub.activityTotal}} items over {{rules.attr10sub.window}}"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
33
examples/author/README.md
Normal file
33
examples/author/README.md
Normal file
@@ -0,0 +1,33 @@
|
||||
# Author
|
||||
|
||||
## 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.
|
||||
|
||||
**AuthorCriteria** that can be checked:
|
||||
* name (u/userName)
|
||||
* author's subreddit flair text
|
||||
* author's subreddit flair css
|
||||
* author's subreddit mod status
|
||||
|
||||
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).
|
||||
|
||||
Consult the [schema](https://json-schema.app/view/%23%2Fdefinitions%2FAuthorRuleJSONConfig?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Freddit-context-bot%2Fmaster%2Fsrc%2FSchema%2FApp.json) for a complete reference of the rule's properties.
|
||||
|
||||
### Examples
|
||||
|
||||
* Basic examples
|
||||
* [Flair new user Submission](/examples/author/flairNewUserSubmission.json5) - If the Author does not have the `vet` flair then flair the Submission with `New User`
|
||||
* [Flair vetted user Submission](/examples/author/flairNewUserSubmission.json5) - If the Author does have the `vet` flair then flair the Submission with `Vetted`
|
||||
* Used with other Rules
|
||||
* [Ignore vetted user](/examples/author/flairNewUserSubmission.json5) - Short-circuit the Check if the Author has the `vet` flair
|
||||
|
||||
## 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.
|
||||
|
||||
**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.
|
||||
|
||||
### Examples
|
||||
|
||||
* [Skip recent activity check based on author](/examples/author/authorFilter.json5) - Skip a Recent Activity check for a set of subreddits if the Author of the Submission has any set of flairs.
|
||||
73
examples/author/authorFilter.json5
Normal file
73
examples/author/authorFilter.json5
Normal file
@@ -0,0 +1,73 @@
|
||||
{
|
||||
"checks": [
|
||||
{
|
||||
"name": "Karma/Meme Sub Activity",
|
||||
"description": "Report on karma sub activity or meme sub activity if user isn't a memelord",
|
||||
// check will run on a new submission in your subreddit and look at the Author of that submission
|
||||
"kind": "submission",
|
||||
"rules": [
|
||||
{
|
||||
"name": "freekarma",
|
||||
"kind": "recentActivity",
|
||||
"lookAt": "submissions",
|
||||
"thresholds": [
|
||||
{
|
||||
"totalCount": 1,
|
||||
"subreddits": [
|
||||
"DeFreeKarma",
|
||||
"FreeKarma4U",
|
||||
]
|
||||
}
|
||||
],
|
||||
"window": {
|
||||
"days": 7
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "noobmemer",
|
||||
"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": {
|
||||
// each property (include/exclude) can contain multiple AuthorCriteria
|
||||
// if any AuthorCriteria passes its test the Rule is skipped
|
||||
//
|
||||
// for an AuthorCriteria to pass all properties present on it must pass
|
||||
//
|
||||
// if "include" is present it will always run and exclude will be skipped
|
||||
// "include:" []
|
||||
"exclude": [
|
||||
// for this to pass the Author of the Submission must not have the flair "Supreme Memer" and have the name "user1" or "user2"
|
||||
{
|
||||
"flairText": ["Supreme Memer"],
|
||||
"names": ["user1","user2"]
|
||||
},
|
||||
{
|
||||
// for this to pass the Author of the Submission must not have the flair "Decent Memer"
|
||||
"flairText": ["Decent Memer"]
|
||||
}
|
||||
]
|
||||
},
|
||||
"lookAt": "submissions",
|
||||
"thresholds": [
|
||||
{
|
||||
"totalCount": 1,
|
||||
"subreddits": [
|
||||
"dankmemes",
|
||||
]
|
||||
}
|
||||
],
|
||||
"window": {
|
||||
"days": 7
|
||||
}
|
||||
}
|
||||
],
|
||||
"actions": [
|
||||
{
|
||||
"kind": "report",
|
||||
"content": "Author has posted in free karma sub, or in /r/dankmemes and does not have meme flair in this subreddit"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
29
examples/author/flairNewUserSubmission.json5
Normal file
29
examples/author/flairNewUserSubmission.json5
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"checks": [
|
||||
{
|
||||
"name": "Flair New User Sub",
|
||||
"description": "Flair submission as sketchy if user does not have vet flair",
|
||||
// check will run on a new submission in your subreddit and look at the Author of that submission
|
||||
"kind": "submission",
|
||||
"rules": [
|
||||
{
|
||||
"name": "newflair",
|
||||
"kind": "author",
|
||||
// rule will trigger if Author does not have "vet" flair text
|
||||
"exclude": [
|
||||
{
|
||||
"flairText": ["vet"]
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"actions": [
|
||||
{
|
||||
"kind": "flair",
|
||||
"text": "New User",
|
||||
"css": "orange"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
29
examples/author/flairVettedUserSubmission.json5
Normal file
29
examples/author/flairVettedUserSubmission.json5
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"checks": [
|
||||
{
|
||||
"name": "Flair Vetted User Submission",
|
||||
"description": "Flair submission as Approved if user has vet flair",
|
||||
// check will run on a new submission in your subreddit and look at the Author of that submission
|
||||
"kind": "submission",
|
||||
"rules": [
|
||||
{
|
||||
"name": "newflair",
|
||||
"kind": "author",
|
||||
// rule will trigger if Author has "vet" flair text
|
||||
"include": [
|
||||
{
|
||||
"flairText": ["vet"]
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"actions": [
|
||||
{
|
||||
"kind": "flair",
|
||||
"text": "Vetted",
|
||||
"css": "green"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
81
examples/author/ignoreVettedUser.json5
Normal file
81
examples/author/ignoreVettedUser.json5
Normal file
@@ -0,0 +1,81 @@
|
||||
{
|
||||
"checks": [
|
||||
{
|
||||
"name": "non-vetted karma/meme activity",
|
||||
"description": "Report if Author has SP and has recent karma/meme sub activity and isn't vetted",
|
||||
// check will run on a new submission in your subreddit and look at the Author of that submission
|
||||
"kind": "submission",
|
||||
"rules": [
|
||||
{
|
||||
// The Author Rule is best used in conjunction with other Rules --
|
||||
// instead of having to write an AuthorFilter for every Rule where you want to skip it based on Author criteria
|
||||
// you can write one Author Rule and make it fail on the required criteria
|
||||
// so that the check fails and Actions don't run
|
||||
"name": "nonvet",
|
||||
"kind": "author",
|
||||
"exclude": [
|
||||
{
|
||||
"flairText": ["vet"]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "attr10",
|
||||
"kind": "attribution",
|
||||
"criteria": [
|
||||
{
|
||||
"threshold": "10%",
|
||||
"window": {
|
||||
"days": 90
|
||||
}
|
||||
},
|
||||
{
|
||||
"threshold": "10%",
|
||||
"window": 100
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
"name": "freekarma",
|
||||
"kind": "recentActivity",
|
||||
"lookAt": "submissions",
|
||||
"thresholds": [
|
||||
{
|
||||
"totalCount": 1,
|
||||
"subreddits": [
|
||||
"DeFreeKarma",
|
||||
"FreeKarma4U",
|
||||
]
|
||||
}
|
||||
],
|
||||
"window": {
|
||||
"days": 7
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "memes",
|
||||
"kind": "recentActivity",
|
||||
"lookAt": "submissions",
|
||||
"thresholds": [
|
||||
{
|
||||
"totalCount": 3,
|
||||
"subreddits": [
|
||||
"dankmemes",
|
||||
]
|
||||
}
|
||||
],
|
||||
"window": {
|
||||
"days": 7
|
||||
}
|
||||
}
|
||||
],
|
||||
// will NOT run if the Author for this Submission has the flair "vet"
|
||||
"actions": [
|
||||
{
|
||||
"kind": "report",
|
||||
"content": "Author has posted in free karma or meme subs recently"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
13
examples/history/README.md
Normal file
13
examples/history/README.md
Normal file
@@ -0,0 +1,13 @@
|
||||
# History
|
||||
|
||||
The **History** rule can check an Author's submission/comment statistics over a time period:
|
||||
* Submission total or percentage of All Activity
|
||||
* Comment total or percentage of all Activity
|
||||
* Comments made as OP (commented in their own Submission) total or percentage of all Comments
|
||||
|
||||
Consult the [schema](https://json-schema.app/view/%23%2Fdefinitions%2FHistoryJSONConfig?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Freddit-context-bot%2Fmaster%2Fsrc%2FSchema%2FApp.json) for a complete reference of the rule's properties.
|
||||
|
||||
### Examples
|
||||
|
||||
* [Low Comment Engagement](/examples/history/lowEngagement.json5) - Check if Author is submitting much more than they comment.
|
||||
* [OP Comment Engagement](/examples/history/opOnlyEngagement.json5) - Check if Author is mostly engaging only in their own content
|
||||
35
examples/history/lowEngagement.json5
Normal file
35
examples/history/lowEngagement.json5
Normal file
@@ -0,0 +1,35 @@
|
||||
{
|
||||
"checks": [
|
||||
{
|
||||
"name": "Low Comment Engagement",
|
||||
"description": "Check if Author is submitting much more than they comment",
|
||||
// check will run on a new submission in your subreddit and look at the Author of that submission
|
||||
"kind": "submission",
|
||||
"rules": [
|
||||
{
|
||||
"name": "lowComm",
|
||||
"kind": "history",
|
||||
"criteria": [
|
||||
{
|
||||
// look at last 90 days of Author's activities
|
||||
"window": {
|
||||
"days": 90
|
||||
},
|
||||
// trigger if less than 30% of their activities in this time period are comments
|
||||
"comment": {
|
||||
"threshold": "30%",
|
||||
"condition": "<"
|
||||
}
|
||||
},
|
||||
]
|
||||
}
|
||||
],
|
||||
"actions": [
|
||||
{
|
||||
"kind": "report",
|
||||
"content": "Low engagement: comments were {{rules.lowcomm.commentPercent}} of {{rules.lowcomm.activityTotal}} over {{rules.lowcomm.window}}"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
36
examples/history/opOnlyEngagement.json5
Normal file
36
examples/history/opOnlyEngagement.json5
Normal file
@@ -0,0 +1,36 @@
|
||||
{
|
||||
"checks": [
|
||||
{
|
||||
"name": "Engaging Own Content Only",
|
||||
"description": "Check if Author is mostly engaging in their own content only",
|
||||
// check will run on a new submission in your subreddit and look at the Author of that submission
|
||||
"kind": "submission",
|
||||
"rules": [
|
||||
{
|
||||
"name": "opOnly",
|
||||
"kind": "history",
|
||||
"criteria": [
|
||||
{
|
||||
// look at last 90 days of Author's activities
|
||||
"window": {
|
||||
"days": 90
|
||||
},
|
||||
// trigger if less than 30% of their activities in this time period are comments
|
||||
"comment": {
|
||||
"asOp": true,
|
||||
"threshold": "60%",
|
||||
"condition": ">"
|
||||
}
|
||||
},
|
||||
]
|
||||
}
|
||||
],
|
||||
"actions": [
|
||||
{
|
||||
"kind": "report",
|
||||
"content": "Selfish OP: {{rules.oponly.opPercent}} of {{rules.oponly.commentTotal}} comments over {{rules.oponly.window}} are as OP"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
10
examples/recentActivity/README.md
Normal file
10
examples/recentActivity/README.md
Normal file
@@ -0,0 +1,10 @@
|
||||
# Recent Activity
|
||||
|
||||
The **Recent Activity** rule can check if an Author has made any Submissions/Comments in a list of defined Subreddits.
|
||||
|
||||
Consult the [schema](https://json-schema.app/view/%23%2Fdefinitions%2FRecentActivityRuleJSONConfig?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Freddit-context-bot%2Fmaster%2Fsrc%2FSchema%2FApp.json) for a complete reference of the rule's properties.
|
||||
|
||||
### Examples
|
||||
|
||||
* [Free Karma Subreddits](/examples/recentActivity/freeKarma.json5) - Check if the Author has recently posted in any "free karma" subreddits
|
||||
* [Submission in Free Karma Subreddits](/examples/recentActivity/freeKarmaOnSubmission.json5) - Check if the Author has posted the Submission this check is running on in any "free karma" subreddits recently
|
||||
41
examples/recentActivity/freeKarma.json5
Normal file
41
examples/recentActivity/freeKarma.json5
Normal file
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"checks": [
|
||||
{
|
||||
"name": "Free Karma Alert",
|
||||
"description": "Check if author has posted in 'freekarma' subreddits",
|
||||
// check will run on a new submission in your subreddit and look at the Author of that submission
|
||||
"kind": "submission",
|
||||
"rules": [
|
||||
{
|
||||
"name": "freekarma",
|
||||
"kind": "recentActivity",
|
||||
// when `lookAt` is not present this rule will look for submissions and comments
|
||||
// lookAt: "submissions"
|
||||
// lookAt: "comments"
|
||||
"thresholds": [
|
||||
{
|
||||
// for all subreddits, if the number of activities (sub/comment) is equal to or greater than 1 then the rule is triggered
|
||||
"totalCount": 1,
|
||||
"subreddits": [
|
||||
"DeFreeKarma",
|
||||
"FreeKarma4U",
|
||||
"FreeKarma4You",
|
||||
"upvote"
|
||||
]
|
||||
}
|
||||
],
|
||||
// will look at all of the Author's activities in the last 7 days
|
||||
"window": {
|
||||
"days": 7
|
||||
}
|
||||
}
|
||||
],
|
||||
"actions": [
|
||||
{
|
||||
"kind": "report",
|
||||
"content": "{{rules.freekarma.totalCount}} activities in karma {{rules.freekarma.subCount}} subs over {{rules.freekarma.window}}: {{rules.freekarma.subSummary}}"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
43
examples/recentActivity/freeKarmaOnSubmission.json5
Normal file
43
examples/recentActivity/freeKarmaOnSubmission.json5
Normal file
@@ -0,0 +1,43 @@
|
||||
{
|
||||
"checks": [
|
||||
{
|
||||
"name": "Free Karma On Submission Alert",
|
||||
"description": "Check if author has posted this submission in 'freekarma' subreddits",
|
||||
// check will run on a new submission in your subreddit and look at the Author of that submission
|
||||
"kind": "submission",
|
||||
"rules": [
|
||||
{
|
||||
"name": "freekarmasub",
|
||||
"kind": "recentActivity",
|
||||
// rule will only look at Author's submissions in these subreddits
|
||||
"lookAt": "submissions",
|
||||
// rule will only look at Author's submissions in these subreddits that have the same content (link) as the submission this event was made on
|
||||
// In simpler terms -- rule will only check to see if the same link the author just posted is also posted in these subreddits
|
||||
"useSubmissionAsReference":true,
|
||||
"thresholds": [
|
||||
{
|
||||
// for all subreddits, if the number of activities (sub/comment) is equal to or greater than 1 then the rule is triggered
|
||||
"totalCount": 1,
|
||||
"subreddits": [
|
||||
"DeFreeKarma",
|
||||
"FreeKarma4U",
|
||||
"FreeKarma4You",
|
||||
"upvote"
|
||||
]
|
||||
}
|
||||
],
|
||||
// look at all of the Author's submissions in the last 7 days
|
||||
"window": {
|
||||
"days": 7
|
||||
}
|
||||
}
|
||||
],
|
||||
"actions": [
|
||||
{
|
||||
"kind": "report",
|
||||
"content": "Submission posted {{rules.freekarmasub.totalCount}} times in karma {{rules.freekarmasub.subCount}} subs over {{rules.freekarmasub.window}}: {{rules.freekarmasub.subSummary}}"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
8
examples/repeatActivity/README.md
Normal file
8
examples/repeatActivity/README.md
Normal file
@@ -0,0 +1,8 @@
|
||||
# Repeat Activity
|
||||
|
||||
The **Repeat Activity** rule will check for patterns of repetition in an Author's Submission/Comment history. Consult the [schema](https://json-schema.app/view/%23%2Fdefinitions%2FRepeatActivityJSONConfig?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Freddit-context-bot%2Fmaster%2Fsrc%2FSchema%2FApp.json) for a complete reference of the rule's properties.
|
||||
|
||||
### Examples
|
||||
|
||||
* [Crosspost Spamming](/examples/repeatActivity/crosspostSpamming.json5) - Check if an Author is spamming their Submissions across multiple subreddits
|
||||
* [Burst-posting](/examples/repeatActivity/burstPosting.json5) - Check if Author is crossposting their Submissions in short bursts
|
||||
32
examples/repeatActivity/burstPosting.json5
Normal file
32
examples/repeatActivity/burstPosting.json5
Normal file
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"checks": [
|
||||
{
|
||||
"name": "Burstpost Spam",
|
||||
"description": "Check if Author is crossposting in short bursts",
|
||||
// check will run on a new submission in your subreddit and look at the Author of that submission
|
||||
"kind": "submission",
|
||||
"rules": [
|
||||
{
|
||||
"name": "burstpost",
|
||||
"kind": "repeatActivity",
|
||||
// will only look at Submissions in Author's history that contain the same content (link) as the Submission this check was initiated by
|
||||
"useSubmissionAsReference": true,
|
||||
// the number of non-repeat activities (submissions or comments) to ignore between repeat submissions
|
||||
"gapAllowance": 3,
|
||||
// if the Author has posted this Submission 6 times, ignoring 3 non-repeat activities between each repeat, then this rule will trigger
|
||||
"threshold": 6,
|
||||
// look at all of the Author's submissions in the last 7 days
|
||||
"window": {
|
||||
"days": 7
|
||||
}
|
||||
}
|
||||
],
|
||||
"actions": [
|
||||
{
|
||||
"kind": "report",
|
||||
"content": "Author has burst-posted this link {{rules.burstpost.largestRepeat}} times over {{rules.burstpost.window}}"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
30
examples/repeatActivity/crosspostSpamming.json5
Normal file
30
examples/repeatActivity/crosspostSpamming.json5
Normal file
@@ -0,0 +1,30 @@
|
||||
{
|
||||
"checks": [
|
||||
{
|
||||
"name": "Crosspost Spam",
|
||||
"description": "Check if Author is spamming Submissions across subreddits",
|
||||
// check will run on a new submission in your subreddit and look at the Author of that submission
|
||||
"kind": "submission",
|
||||
"rules": [
|
||||
{
|
||||
"name": "xpostspam",
|
||||
"kind": "repeatActivity",
|
||||
// will only look at Submissions in Author's history that contain the same content (link) as the Submission this check was initiated by
|
||||
"useSubmissionAsReference": true,
|
||||
// if the Author has posted this Submission 5 times consecutively then this rule will trigger
|
||||
"threshold": 5,
|
||||
// look at all of the Author's submissions in the last 7 days
|
||||
"window": {
|
||||
"days": 7
|
||||
}
|
||||
}
|
||||
],
|
||||
"actions": [
|
||||
{
|
||||
"kind": "report",
|
||||
"content": "Author has posted this link {{rules.xpostspam.largestRepeat}} times over {{rules.xpostspam.window}}"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
219
package-lock.json
generated
219
package-lock.json
generated
@@ -8,12 +8,16 @@
|
||||
"version": "1.0.0",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"ajv": "^6.12.6",
|
||||
"ajv": "^7.2.4",
|
||||
"commander": "^7.2.0",
|
||||
"dayjs": "^1.10.5",
|
||||
"es6-error": "^4.1.1",
|
||||
"fast-deep-equal": "^3.1.3",
|
||||
"he": "^1.2.0",
|
||||
"json5": "^2.2.0",
|
||||
"memory-cache": "^0.2.0",
|
||||
"mustache": "^4.2.0",
|
||||
"object-hash": "^2.2.0",
|
||||
"p-event": "^4.2.0",
|
||||
"safe-stable-stringify": "^1.1.1",
|
||||
"snoostorm": "^1.5.2",
|
||||
@@ -23,9 +27,12 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@tsconfig/node14": "^1.0.0",
|
||||
"@types/he": "^1.1.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",
|
||||
"ts-auto-guard": "*",
|
||||
"ts-json-schema-generator": "^0.93.0",
|
||||
"typescript-json-schema": "^0.50.1"
|
||||
@@ -107,9 +114,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/@tsconfig/node14": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/@tsconfig/node14/-/node14-1.0.0.tgz",
|
||||
"integrity": "sha512-RKkL8eTdPv6t5EHgFKIVQgsDapugbuOptNd9OOunN/HAkzmmTnZELx1kNCK0rSdUYGmiFMM3rRQMAWiyp023LQ==",
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/@tsconfig/node14/-/node14-1.0.1.tgz",
|
||||
"integrity": "sha512-509r2+yARFfHHE7T6Puu2jjkoycftovhXRqW328PDXTVGKihlb1P8Z9mMZH04ebyajfRY7dedfGynlrFHJUQCg==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/@types/command-line-args": {
|
||||
@@ -124,6 +131,12 @@
|
||||
"integrity": "sha512-/xUgezxxYePeXhg5S04hUjxG9JZi+rJTs1+4NwpYPfSaS7BeDa6tVJkH6lN9Cb6rl8d24Fi2uX0s0Ngg2JT6gg==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/@types/he": {
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://registry.npmjs.org/@types/he/-/he-1.1.1.tgz",
|
||||
"integrity": "sha512-jpzrsR1ns0n3kyWt92QfOUQhIuJGQ9+QGa7M62rO6toe98woQjnsnzjdMtsQXCdvjjmqjS2ZBCC7xKw0cdzU+Q==",
|
||||
"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",
|
||||
@@ -136,6 +149,12 @@
|
||||
"integrity": "sha512-bpcvu/MKHHeYX+qeEN8GE7DIravODWdACVA1ctevD8CN24RhPZIKMn9ntfAsrvLfSX3cR5RrBKAbYm9bGs0A+Q==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/@types/memory-cache": {
|
||||
"version": "0.2.1",
|
||||
"resolved": "https://registry.npmjs.org/@types/memory-cache/-/memory-cache-0.2.1.tgz",
|
||||
"integrity": "sha512-6rmz3bMqJNkj0HIN3AMhOfRN+JhnxxTULeBkwgilfnspjABtKG6ig8mbIzkOjtmiRw+WG1B56z+BO6htGz3IBw==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/@types/minimatch": {
|
||||
"version": "3.0.4",
|
||||
"resolved": "https://registry.npmjs.org/@types/minimatch/-/minimatch-3.0.4.tgz",
|
||||
@@ -155,9 +174,15 @@
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/@types/node": {
|
||||
"version": "15.12.1",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-15.12.1.tgz",
|
||||
"integrity": "sha512-zyxJM8I1c9q5sRMtVF+zdd13Jt6RU4r4qfhTd7lQubyThvLfx6yYekWSQjGCGV2Tkecgxnlpl/DNlb6Hg+dmEw==",
|
||||
"version": "15.12.2",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-15.12.2.tgz",
|
||||
"integrity": "sha512-zjQ69G564OCIWIOHSXyQEEDpdpGl+G348RAKY0XXy9Z5kU9Vzv1GMNnkar/ZJ8dzXB3COzD9Mo9NtRZ4xfgUww==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/@types/object-hash": {
|
||||
"version": "2.1.0",
|
||||
"resolved": "https://registry.npmjs.org/@types/object-hash/-/object-hash-2.1.0.tgz",
|
||||
"integrity": "sha512-RW3VRiuQIMo5PJ4Q1IwBtdLHL/t8ACpzUY40norN9ejE6CUBwKetmSxJnITJ0NlzN/ymF1nvPvlpvegtns7yOg==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/@types/strip-bom": {
|
||||
@@ -173,13 +198,13 @@
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/ajv": {
|
||||
"version": "6.12.6",
|
||||
"resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz",
|
||||
"integrity": "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==",
|
||||
"version": "7.2.4",
|
||||
"resolved": "https://registry.npmjs.org/ajv/-/ajv-7.2.4.tgz",
|
||||
"integrity": "sha512-nBeQgg/ZZA3u3SYxyaDvpvDtgZ/EZPF547ARgZBrG9Bhu1vKDwAIjtIf+sDtJUKa2zOcEbmRLBRSyMraS/Oy1A==",
|
||||
"dependencies": {
|
||||
"fast-deep-equal": "^3.1.1",
|
||||
"fast-json-stable-stringify": "^2.0.0",
|
||||
"json-schema-traverse": "^0.4.1",
|
||||
"json-schema-traverse": "^1.0.0",
|
||||
"require-from-string": "^2.0.2",
|
||||
"uri-js": "^4.2.2"
|
||||
},
|
||||
"funding": {
|
||||
@@ -787,6 +812,26 @@
|
||||
"node": ">=6"
|
||||
}
|
||||
},
|
||||
"node_modules/har-validator/node_modules/ajv": {
|
||||
"version": "6.12.6",
|
||||
"resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz",
|
||||
"integrity": "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==",
|
||||
"dependencies": {
|
||||
"fast-deep-equal": "^3.1.1",
|
||||
"fast-json-stable-stringify": "^2.0.0",
|
||||
"json-schema-traverse": "^0.4.1",
|
||||
"uri-js": "^4.2.2"
|
||||
},
|
||||
"funding": {
|
||||
"type": "github",
|
||||
"url": "https://github.com/sponsors/epoberezkin"
|
||||
}
|
||||
},
|
||||
"node_modules/har-validator/node_modules/json-schema-traverse": {
|
||||
"version": "0.4.1",
|
||||
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz",
|
||||
"integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg=="
|
||||
},
|
||||
"node_modules/harmony-reflect": {
|
||||
"version": "1.6.2",
|
||||
"resolved": "https://registry.npmjs.org/harmony-reflect/-/harmony-reflect-1.6.2.tgz",
|
||||
@@ -801,6 +846,14 @@
|
||||
"node": ">=4"
|
||||
}
|
||||
},
|
||||
"node_modules/he": {
|
||||
"version": "1.2.0",
|
||||
"resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz",
|
||||
"integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==",
|
||||
"bin": {
|
||||
"he": "bin/he"
|
||||
}
|
||||
},
|
||||
"node_modules/http-signature": {
|
||||
"version": "1.2.0",
|
||||
"resolved": "https://registry.npmjs.org/http-signature/-/http-signature-1.2.0.tgz",
|
||||
@@ -963,9 +1016,9 @@
|
||||
"integrity": "sha1-tIDIkuWaLwWVTOcnvT8qTogvnhM="
|
||||
},
|
||||
"node_modules/json-schema-traverse": {
|
||||
"version": "0.4.1",
|
||||
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz",
|
||||
"integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg=="
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz",
|
||||
"integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug=="
|
||||
},
|
||||
"node_modules/json-stable-stringify": {
|
||||
"version": "1.0.1",
|
||||
@@ -981,6 +1034,20 @@
|
||||
"resolved": "https://registry.npmjs.org/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz",
|
||||
"integrity": "sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus="
|
||||
},
|
||||
"node_modules/json5": {
|
||||
"version": "2.2.0",
|
||||
"resolved": "https://registry.npmjs.org/json5/-/json5-2.2.0.tgz",
|
||||
"integrity": "sha512-f+8cldu7X/y7RAJurMEJmdoKXGB/X550w2Nr3tTbezL6RwEE/iMcm+tZnXeoZtKuOq6ft8+CqzEkrIgx1fPoQA==",
|
||||
"dependencies": {
|
||||
"minimist": "^1.2.5"
|
||||
},
|
||||
"bin": {
|
||||
"json5": "lib/cli.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=6"
|
||||
}
|
||||
},
|
||||
"node_modules/jsonify": {
|
||||
"version": "0.0.0",
|
||||
"resolved": "https://registry.npmjs.org/jsonify/-/jsonify-0.0.0.tgz",
|
||||
@@ -1040,6 +1107,11 @@
|
||||
"integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/memory-cache": {
|
||||
"version": "0.2.0",
|
||||
"resolved": "https://registry.npmjs.org/memory-cache/-/memory-cache-0.2.0.tgz",
|
||||
"integrity": "sha1-eJCwHVLADI68nVM+H46xfjA0hxo="
|
||||
},
|
||||
"node_modules/merge2": {
|
||||
"version": "1.4.1",
|
||||
"resolved": "https://registry.npmjs.org/merge2/-/merge2-1.4.1.tgz",
|
||||
@@ -1093,6 +1165,11 @@
|
||||
"node": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/minimist": {
|
||||
"version": "1.2.5",
|
||||
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz",
|
||||
"integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw=="
|
||||
},
|
||||
"node_modules/mkdirp": {
|
||||
"version": "1.0.4",
|
||||
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz",
|
||||
@@ -1385,6 +1462,14 @@
|
||||
"node": ">=0.10.0"
|
||||
}
|
||||
},
|
||||
"node_modules/require-from-string": {
|
||||
"version": "2.0.2",
|
||||
"resolved": "https://registry.npmjs.org/require-from-string/-/require-from-string-2.0.2.tgz",
|
||||
"integrity": "sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw==",
|
||||
"engines": {
|
||||
"node": ">=0.10.0"
|
||||
}
|
||||
},
|
||||
"node_modules/reusify": {
|
||||
"version": "1.0.4",
|
||||
"resolved": "https://registry.npmjs.org/reusify/-/reusify-1.0.4.tgz",
|
||||
@@ -1814,9 +1899,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/typescript-json-schema/node_modules/@types/node": {
|
||||
"version": "14.17.2",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-14.17.2.tgz",
|
||||
"integrity": "sha512-sld7b/xmFum66AAKuz/rp/CUO8+98fMpyQ3SBfzzBNGMd/1iHBTAg9oyAvcYlAj46bpc74r91jSw2iFdnx29nw==",
|
||||
"version": "14.17.3",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-14.17.3.tgz",
|
||||
"integrity": "sha512-e6ZowgGJmTuXa3GyaPbTGxX17tnThl2aSSizrFthQ7m9uLGZBXiGhgE55cjRZTF5kjZvYn9EOPOMljdjwbflxw==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/typescript-json-schema/node_modules/typescript": {
|
||||
@@ -2168,9 +2253,9 @@
|
||||
}
|
||||
},
|
||||
"@tsconfig/node14": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/@tsconfig/node14/-/node14-1.0.0.tgz",
|
||||
"integrity": "sha512-RKkL8eTdPv6t5EHgFKIVQgsDapugbuOptNd9OOunN/HAkzmmTnZELx1kNCK0rSdUYGmiFMM3rRQMAWiyp023LQ==",
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/@tsconfig/node14/-/node14-1.0.1.tgz",
|
||||
"integrity": "sha512-509r2+yARFfHHE7T6Puu2jjkoycftovhXRqW328PDXTVGKihlb1P8Z9mMZH04ebyajfRY7dedfGynlrFHJUQCg==",
|
||||
"dev": true
|
||||
},
|
||||
"@types/command-line-args": {
|
||||
@@ -2185,6 +2270,12 @@
|
||||
"integrity": "sha512-/xUgezxxYePeXhg5S04hUjxG9JZi+rJTs1+4NwpYPfSaS7BeDa6tVJkH6lN9Cb6rl8d24Fi2uX0s0Ngg2JT6gg==",
|
||||
"dev": true
|
||||
},
|
||||
"@types/he": {
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://registry.npmjs.org/@types/he/-/he-1.1.1.tgz",
|
||||
"integrity": "sha512-jpzrsR1ns0n3kyWt92QfOUQhIuJGQ9+QGa7M62rO6toe98woQjnsnzjdMtsQXCdvjjmqjS2ZBCC7xKw0cdzU+Q==",
|
||||
"dev": true
|
||||
},
|
||||
"@types/json-schema": {
|
||||
"version": "7.0.7",
|
||||
"resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.7.tgz",
|
||||
@@ -2197,6 +2288,12 @@
|
||||
"integrity": "sha512-bpcvu/MKHHeYX+qeEN8GE7DIravODWdACVA1ctevD8CN24RhPZIKMn9ntfAsrvLfSX3cR5RrBKAbYm9bGs0A+Q==",
|
||||
"dev": true
|
||||
},
|
||||
"@types/memory-cache": {
|
||||
"version": "0.2.1",
|
||||
"resolved": "https://registry.npmjs.org/@types/memory-cache/-/memory-cache-0.2.1.tgz",
|
||||
"integrity": "sha512-6rmz3bMqJNkj0HIN3AMhOfRN+JhnxxTULeBkwgilfnspjABtKG6ig8mbIzkOjtmiRw+WG1B56z+BO6htGz3IBw==",
|
||||
"dev": true
|
||||
},
|
||||
"@types/minimatch": {
|
||||
"version": "3.0.4",
|
||||
"resolved": "https://registry.npmjs.org/@types/minimatch/-/minimatch-3.0.4.tgz",
|
||||
@@ -2216,9 +2313,15 @@
|
||||
"dev": true
|
||||
},
|
||||
"@types/node": {
|
||||
"version": "15.12.1",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-15.12.1.tgz",
|
||||
"integrity": "sha512-zyxJM8I1c9q5sRMtVF+zdd13Jt6RU4r4qfhTd7lQubyThvLfx6yYekWSQjGCGV2Tkecgxnlpl/DNlb6Hg+dmEw==",
|
||||
"version": "15.12.2",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-15.12.2.tgz",
|
||||
"integrity": "sha512-zjQ69G564OCIWIOHSXyQEEDpdpGl+G348RAKY0XXy9Z5kU9Vzv1GMNnkar/ZJ8dzXB3COzD9Mo9NtRZ4xfgUww==",
|
||||
"dev": true
|
||||
},
|
||||
"@types/object-hash": {
|
||||
"version": "2.1.0",
|
||||
"resolved": "https://registry.npmjs.org/@types/object-hash/-/object-hash-2.1.0.tgz",
|
||||
"integrity": "sha512-RW3VRiuQIMo5PJ4Q1IwBtdLHL/t8ACpzUY40norN9ejE6CUBwKetmSxJnITJ0NlzN/ymF1nvPvlpvegtns7yOg==",
|
||||
"dev": true
|
||||
},
|
||||
"@types/strip-bom": {
|
||||
@@ -2234,13 +2337,13 @@
|
||||
"dev": true
|
||||
},
|
||||
"ajv": {
|
||||
"version": "6.12.6",
|
||||
"resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz",
|
||||
"integrity": "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==",
|
||||
"version": "7.2.4",
|
||||
"resolved": "https://registry.npmjs.org/ajv/-/ajv-7.2.4.tgz",
|
||||
"integrity": "sha512-nBeQgg/ZZA3u3SYxyaDvpvDtgZ/EZPF547ARgZBrG9Bhu1vKDwAIjtIf+sDtJUKa2zOcEbmRLBRSyMraS/Oy1A==",
|
||||
"requires": {
|
||||
"fast-deep-equal": "^3.1.1",
|
||||
"fast-json-stable-stringify": "^2.0.0",
|
||||
"json-schema-traverse": "^0.4.1",
|
||||
"json-schema-traverse": "^1.0.0",
|
||||
"require-from-string": "^2.0.2",
|
||||
"uri-js": "^4.2.2"
|
||||
}
|
||||
},
|
||||
@@ -2738,6 +2841,24 @@
|
||||
"requires": {
|
||||
"ajv": "^6.12.3",
|
||||
"har-schema": "^2.0.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"ajv": {
|
||||
"version": "6.12.6",
|
||||
"resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz",
|
||||
"integrity": "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==",
|
||||
"requires": {
|
||||
"fast-deep-equal": "^3.1.1",
|
||||
"fast-json-stable-stringify": "^2.0.0",
|
||||
"json-schema-traverse": "^0.4.1",
|
||||
"uri-js": "^4.2.2"
|
||||
}
|
||||
},
|
||||
"json-schema-traverse": {
|
||||
"version": "0.4.1",
|
||||
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz",
|
||||
"integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg=="
|
||||
}
|
||||
}
|
||||
},
|
||||
"harmony-reflect": {
|
||||
@@ -2751,6 +2872,11 @@
|
||||
"integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=",
|
||||
"dev": true
|
||||
},
|
||||
"he": {
|
||||
"version": "1.2.0",
|
||||
"resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz",
|
||||
"integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw=="
|
||||
},
|
||||
"http-signature": {
|
||||
"version": "1.2.0",
|
||||
"resolved": "https://registry.npmjs.org/http-signature/-/http-signature-1.2.0.tgz",
|
||||
@@ -2879,9 +3005,9 @@
|
||||
"integrity": "sha1-tIDIkuWaLwWVTOcnvT8qTogvnhM="
|
||||
},
|
||||
"json-schema-traverse": {
|
||||
"version": "0.4.1",
|
||||
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz",
|
||||
"integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg=="
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz",
|
||||
"integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug=="
|
||||
},
|
||||
"json-stable-stringify": {
|
||||
"version": "1.0.1",
|
||||
@@ -2897,6 +3023,14 @@
|
||||
"resolved": "https://registry.npmjs.org/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz",
|
||||
"integrity": "sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus="
|
||||
},
|
||||
"json5": {
|
||||
"version": "2.2.0",
|
||||
"resolved": "https://registry.npmjs.org/json5/-/json5-2.2.0.tgz",
|
||||
"integrity": "sha512-f+8cldu7X/y7RAJurMEJmdoKXGB/X550w2Nr3tTbezL6RwEE/iMcm+tZnXeoZtKuOq6ft8+CqzEkrIgx1fPoQA==",
|
||||
"requires": {
|
||||
"minimist": "^1.2.5"
|
||||
}
|
||||
},
|
||||
"jsonify": {
|
||||
"version": "0.0.0",
|
||||
"resolved": "https://registry.npmjs.org/jsonify/-/jsonify-0.0.0.tgz",
|
||||
@@ -2953,6 +3087,11 @@
|
||||
"integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==",
|
||||
"dev": true
|
||||
},
|
||||
"memory-cache": {
|
||||
"version": "0.2.0",
|
||||
"resolved": "https://registry.npmjs.org/memory-cache/-/memory-cache-0.2.0.tgz",
|
||||
"integrity": "sha1-eJCwHVLADI68nVM+H46xfjA0hxo="
|
||||
},
|
||||
"merge2": {
|
||||
"version": "1.4.1",
|
||||
"resolved": "https://registry.npmjs.org/merge2/-/merge2-1.4.1.tgz",
|
||||
@@ -2991,6 +3130,11 @@
|
||||
"brace-expansion": "^1.1.7"
|
||||
}
|
||||
},
|
||||
"minimist": {
|
||||
"version": "1.2.5",
|
||||
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz",
|
||||
"integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw=="
|
||||
},
|
||||
"mkdirp": {
|
||||
"version": "1.0.4",
|
||||
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz",
|
||||
@@ -3192,6 +3336,11 @@
|
||||
"integrity": "sha1-jGStX9MNqxyXbiNE/+f3kqam30I=",
|
||||
"dev": true
|
||||
},
|
||||
"require-from-string": {
|
||||
"version": "2.0.2",
|
||||
"resolved": "https://registry.npmjs.org/require-from-string/-/require-from-string-2.0.2.tgz",
|
||||
"integrity": "sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw=="
|
||||
},
|
||||
"reusify": {
|
||||
"version": "1.0.4",
|
||||
"resolved": "https://registry.npmjs.org/reusify/-/reusify-1.0.4.tgz",
|
||||
@@ -3507,9 +3656,9 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@types/node": {
|
||||
"version": "14.17.2",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-14.17.2.tgz",
|
||||
"integrity": "sha512-sld7b/xmFum66AAKuz/rp/CUO8+98fMpyQ3SBfzzBNGMd/1iHBTAg9oyAvcYlAj46bpc74r91jSw2iFdnx29nw==",
|
||||
"version": "14.17.3",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-14.17.3.tgz",
|
||||
"integrity": "sha512-e6ZowgGJmTuXa3GyaPbTGxX17tnThl2aSSizrFthQ7m9uLGZBXiGhgE55cjRZTF5kjZvYn9EOPOMljdjwbflxw==",
|
||||
"dev": true
|
||||
},
|
||||
"typescript": {
|
||||
|
||||
17
package.json
17
package.json
@@ -9,10 +9,10 @@
|
||||
"start": "node server.js",
|
||||
"guard": "ts-auto-guard src/JsonConfig.ts",
|
||||
"schema": "npm run -s schema-app & npm run -s schema-ruleset & npm run -s schema-rule & npm run -s schema-action",
|
||||
"schema-app": "typescript-json-schema tsconfig.json JSONConfig --out src/Schema/App.json --required --tsNodeRegister --refs --propOrder",
|
||||
"schema-ruleset": "typescript-json-schema tsconfig.json RuleSetJson --out src/Schema/RuleSet.json --required --tsNodeRegister --refs --propOrder",
|
||||
"schema-rule": "typescript-json-schema tsconfig.json RuleJson --out src/Schema/Rule.json --required --tsNodeRegister --refs --propOrder",
|
||||
"schema-action": "typescript-json-schema tsconfig.json ActionJson --out src/Schema/Action.json --required --tsNodeRegister --refs --propOrder",
|
||||
"schema-app": "typescript-json-schema tsconfig.json JSONConfig --out src/Schema/App.json --required --tsNodeRegister --refs",
|
||||
"schema-ruleset": "typescript-json-schema tsconfig.json RuleSetJson --out src/Schema/RuleSet.json --required --tsNodeRegister --refs",
|
||||
"schema-rule": "typescript-json-schema tsconfig.json RuleJson --out src/Schema/Rule.json --required --tsNodeRegister --refs",
|
||||
"schema-action": "typescript-json-schema tsconfig.json ActionJson --out src/Schema/Action.json --required --tsNodeRegister --refs",
|
||||
"schemaNotWorking": "./node_modules/.bin/ts-json-schema-generator -f tsconfig.json -p src/JsonConfig.ts -t JSONConfig --out src/Schema/vegaSchema.json",
|
||||
"circular": "madge --circular --extensions ts src/index.ts",
|
||||
"circular-graph": "madge --image graph.svg --circular --extensions ts src/index.ts"
|
||||
@@ -24,12 +24,16 @@
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"ajv": "^6.12.6",
|
||||
"ajv": "^7.2.4",
|
||||
"commander": "^7.2.0",
|
||||
"dayjs": "^1.10.5",
|
||||
"es6-error": "^4.1.1",
|
||||
"fast-deep-equal": "^3.1.3",
|
||||
"he": "^1.2.0",
|
||||
"json5": "^2.2.0",
|
||||
"memory-cache": "^0.2.0",
|
||||
"mustache": "^4.2.0",
|
||||
"object-hash": "^2.2.0",
|
||||
"p-event": "^4.2.0",
|
||||
"safe-stable-stringify": "^1.1.1",
|
||||
"snoostorm": "^1.5.2",
|
||||
@@ -39,9 +43,12 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@tsconfig/node14": "^1.0.0",
|
||||
"@types/he": "^1.1.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",
|
||||
"ts-auto-guard": "*",
|
||||
"ts-json-schema-generator": "^0.93.0",
|
||||
"typescript-json-schema": "^0.50.1"
|
||||
|
||||
@@ -4,20 +4,21 @@ import {RemoveAction} from "./RemoveAction";
|
||||
import {ReportAction, ReportActionJson} from "./ReportAction";
|
||||
import {FlairAction, FlairActionJson} from "./SubmissionAction/FlairAction";
|
||||
import Action, {ActionJson} from "./index";
|
||||
import {Logger} from "winston";
|
||||
|
||||
export function actionFactory
|
||||
(config: ActionJson): Action {
|
||||
(config: ActionJson, logger: Logger, subredditName: string): Action {
|
||||
switch (config.kind) {
|
||||
case 'comment':
|
||||
return new CommentAction(config as CommentActionJson);
|
||||
return new CommentAction({...config as CommentActionJson, logger, subredditName});
|
||||
case 'lock':
|
||||
return new LockAction();
|
||||
return new LockAction({...config, logger, subredditName});
|
||||
case 'remove':
|
||||
return new RemoveAction();
|
||||
return new RemoveAction({...config, logger, subredditName});
|
||||
case 'report':
|
||||
return new ReportAction(config as ReportActionJson);
|
||||
return new ReportAction({...config as ReportActionJson, logger, subredditName});
|
||||
case 'flair':
|
||||
return new FlairAction(config as FlairActionJson);
|
||||
return new FlairAction({...config as FlairActionJson, logger, subredditName});
|
||||
default:
|
||||
throw new Error('rule "kind" was not recognized.');
|
||||
}
|
||||
|
||||
@@ -1,22 +1,15 @@
|
||||
import Action, {ActionJson, ActionConfig, ActionOptions} from "./index";
|
||||
import Snoowrap, {Comment} from "snoowrap";
|
||||
import Action, {ActionJson, ActionOptions} from "./index";
|
||||
import {Comment} from "snoowrap";
|
||||
import Submission from "snoowrap/dist/objects/Submission";
|
||||
import dayjs, {Dayjs} from "dayjs";
|
||||
import {renderContent} from "../Utils/SnoowrapUtils";
|
||||
import {RichContent} from "../Common/interfaces";
|
||||
import {RuleResult} from "../Rule";
|
||||
|
||||
export const WIKI_DESCRIM = 'wiki:';
|
||||
|
||||
export class CommentAction extends Action {
|
||||
content: string;
|
||||
hasWiki: boolean;
|
||||
wiki?: string;
|
||||
wikiFetched?: Dayjs;
|
||||
lock: boolean = false;
|
||||
sticky: boolean = false;
|
||||
distinguish: boolean = false;
|
||||
name?: string = 'Comment';
|
||||
|
||||
constructor(options: CommentActionOptions) {
|
||||
super(options);
|
||||
@@ -26,34 +19,33 @@ export class CommentAction extends Action {
|
||||
sticky = false,
|
||||
distinguish = false,
|
||||
} = options;
|
||||
this.hasWiki = content.trim().substring(0, WIKI_DESCRIM.length) === WIKI_DESCRIM;
|
||||
this.content = content;
|
||||
if (this.hasWiki) {
|
||||
this.wiki = this.content.trim().substring(WIKI_DESCRIM.length);
|
||||
}
|
||||
this.lock = lock;
|
||||
this.sticky = sticky;
|
||||
this.distinguish = distinguish;
|
||||
}
|
||||
|
||||
async handle(item: Comment | Submission, ruleResults: RuleResult[]): Promise<void> {
|
||||
if (this.hasWiki && (this.wikiFetched === undefined || Math.abs(dayjs().diff(this.wikiFetched, 'minute')) > 5)) {
|
||||
try {
|
||||
const wiki = item.subreddit.getWikiPage(this.wiki as string);
|
||||
this.content = await wiki.content_md;
|
||||
this.wikiFetched = dayjs();
|
||||
} catch (err) {
|
||||
this.logger.error(err);
|
||||
throw new Error(`Could not read wiki page. Please ensure the page '${this.wiki}' exists and is readable`);
|
||||
getKind() {
|
||||
return 'Comment';
|
||||
}
|
||||
|
||||
async process(item: Comment | Submission, ruleResults: RuleResult[]): Promise<void> {
|
||||
const content = await this.cache.getContent(this.content, item.subreddit);
|
||||
const renderedContent = await renderContent(content, item, ruleResults);
|
||||
this.logger.verbose(`Contents:\r\n${renderedContent}`);
|
||||
// @ts-ignore
|
||||
const reply: Comment = await item.reply(renderedContent);
|
||||
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');
|
||||
}
|
||||
}
|
||||
// @ts-ignore
|
||||
const reply: Comment = await item.reply(renderContent(this.content, item, ruleResults));
|
||||
if (this.lock && item instanceof Submission) {
|
||||
// @ts-ignore
|
||||
await item.lock();
|
||||
}
|
||||
if (this.distinguish) {
|
||||
if (this.distinguish && !this.dryRun) {
|
||||
// @ts-ignore
|
||||
await reply.distinguish({sticky: this.sticky});
|
||||
}
|
||||
|
||||
@@ -4,11 +4,18 @@ import Snoowrap, {Comment, Submission} from "snoowrap";
|
||||
import {RuleResult} from "../Rule";
|
||||
|
||||
export class LockAction extends Action {
|
||||
name?: string = 'Lock';
|
||||
async handle(item: Comment|Submission, ruleResults: RuleResult[]): Promise<void> {
|
||||
getKind() {
|
||||
return 'Lock';
|
||||
}
|
||||
|
||||
async process(item: Comment|Submission, ruleResults: RuleResult[]): Promise<void> {
|
||||
if (item instanceof Submission) {
|
||||
// @ts-ignore
|
||||
await item.lock();
|
||||
if(!this.dryRun) {
|
||||
// @ts-ignore
|
||||
await item.lock();
|
||||
}
|
||||
} else {
|
||||
this.logger.warn('Snoowrap does not support locking Comments');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,10 +4,15 @@ import Snoowrap, {Comment, Submission} from "snoowrap";
|
||||
import {RuleResult} from "../Rule";
|
||||
|
||||
export class RemoveAction extends Action {
|
||||
name?: string = 'Remove';
|
||||
async handle(item: Comment|Submission, ruleResults: RuleResult[]): Promise<void> {
|
||||
// @ts-ignore
|
||||
await item.remove();
|
||||
getKind() {
|
||||
return 'Remove';
|
||||
}
|
||||
|
||||
async process(item: Comment|Submission, ruleResults: RuleResult[]): Promise<void> {
|
||||
if(!this.dryRun) {
|
||||
// @ts-ignore
|
||||
await item.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,23 +7,30 @@ import {RuleResult} from "../Rule";
|
||||
|
||||
// https://www.reddit.com/dev/api/oauth#POST_api_report
|
||||
// denotes 100 characters maximum
|
||||
// const reportTrunc = truncateStringToLength(100);
|
||||
const reportTrunc = truncateStringToLength(100);
|
||||
// actually only applies to VISIBLE text on OLD reddit... on old reddit rest of text is visible on hover. on new reddit the whole thing displays (up to at least 400 characters)
|
||||
|
||||
export class ReportAction extends Action {
|
||||
content: string;
|
||||
name?: string = 'Report';
|
||||
|
||||
constructor(options: ReportActionOptions) {
|
||||
super(options);
|
||||
this.content = options.content || '';
|
||||
}
|
||||
|
||||
async handle(item: Comment | Submission, ruleResults: RuleResult[]): Promise<void> {
|
||||
const renderedContent = await renderContent(this.content, item, ruleResults);
|
||||
//const truncatedContent = reportTrunc(renderedContent);
|
||||
// @ts-ignore
|
||||
await item.report({reason: renderedContent});
|
||||
getKind() {
|
||||
return 'Report';
|
||||
}
|
||||
|
||||
async process(item: Comment | Submission, ruleResults: RuleResult[]): Promise<void> {
|
||||
const content = await this.cache.getContent(this.content, item.subreddit);
|
||||
const renderedContent = await renderContent(content, item, ruleResults);
|
||||
this.logger.verbose(`Contents:\r\n${renderedContent}`);
|
||||
const truncatedContent = reportTrunc(renderedContent);
|
||||
if(!this.dryRun) {
|
||||
// @ts-ignore
|
||||
await item.report({reason: truncatedContent});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
import {SubmissionActionConfig} from "./index";
|
||||
import Action, {ActionJson} from "../index";
|
||||
import Action, {ActionJson, ActionOptions} from "../index";
|
||||
import Snoowrap, {Comment, Submission} from "snoowrap";
|
||||
import {RuleResult} from "../../Rule";
|
||||
|
||||
export class FlairAction extends Action {
|
||||
text: string;
|
||||
css: string;
|
||||
name?: string = 'Flair';
|
||||
|
||||
constructor(options: FlairActionOptions) {
|
||||
super(options);
|
||||
@@ -17,10 +16,18 @@ export class FlairAction extends Action {
|
||||
this.css = options.css || '';
|
||||
}
|
||||
|
||||
async handle(item: Comment | Submission, ruleResults: RuleResult[]): Promise<void> {
|
||||
getKind() {
|
||||
return 'Flair';
|
||||
}
|
||||
|
||||
async process(item: Comment | Submission, ruleResults: RuleResult[]): Promise<void> {
|
||||
if (item instanceof Submission) {
|
||||
// @ts-ignore
|
||||
await item.assignFlair({text: this.text, cssClass: this.css})
|
||||
if(!this.dryRun) {
|
||||
// @ts-ignore
|
||||
await item.assignFlair({text: this.text, cssClass: this.css})
|
||||
}
|
||||
} else {
|
||||
this.logger.warn('Cannot flair Comment');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,7 +36,7 @@ export class FlairAction extends Action {
|
||||
* @minProperties 1
|
||||
* @additionalProperties false
|
||||
* */
|
||||
export interface FlairActionOptions extends SubmissionActionConfig {
|
||||
export interface FlairActionConfig extends SubmissionActionConfig {
|
||||
/**
|
||||
* The text of the flair to apply
|
||||
* */
|
||||
@@ -40,9 +47,13 @@ export interface FlairActionOptions extends SubmissionActionConfig {
|
||||
css?: string,
|
||||
}
|
||||
|
||||
export interface FlairActionOptions extends FlairActionConfig,ActionOptions {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Flair the Submission
|
||||
* */
|
||||
export interface FlairActionJson extends FlairActionOptions, ActionJson {
|
||||
export interface FlairActionJson extends FlairActionConfig, ActionJson {
|
||||
|
||||
}
|
||||
|
||||
@@ -1,36 +1,42 @@
|
||||
import Snoowrap, {Comment, Submission} from "snoowrap";
|
||||
import {Comment, Submission} from "snoowrap";
|
||||
import {Logger} from "winston";
|
||||
import {createLabelledLogger, loggerMetaShuffle} from "../util";
|
||||
import {RuleResult} from "../Rule";
|
||||
import CacheManager, {SubredditCache} from "../Subreddit/SubredditCache";
|
||||
|
||||
export abstract class Action {
|
||||
name?: string;
|
||||
logger: Logger;
|
||||
cache: SubredditCache;
|
||||
dryRun: boolean;
|
||||
|
||||
constructor(options: ActionOptions = {}) {
|
||||
constructor(options: ActionOptions) {
|
||||
const {
|
||||
name,
|
||||
loggerPrefix = '',
|
||||
name = this.getKind(),
|
||||
logger,
|
||||
subredditName,
|
||||
dryRun = false,
|
||||
} = options;
|
||||
if (name !== undefined) {
|
||||
this.name = name;
|
||||
}
|
||||
if (logger === undefined) {
|
||||
const prefix = `${loggerPrefix}|${this.name}`;
|
||||
this.logger = createLabelledLogger(prefix, prefix);
|
||||
} else {
|
||||
this.logger = logger.child(loggerMetaShuffle(logger, name || 'Action', undefined, {truncateLength: 100}));
|
||||
}
|
||||
|
||||
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]});
|
||||
}
|
||||
|
||||
abstract handle(item: Comment | Submission, ruleResults: RuleResult[]): Promise<void>;
|
||||
abstract getKind(): string;
|
||||
|
||||
async handle(item: Comment | Submission, ruleResults: RuleResult[]): Promise<void> {
|
||||
await this.process(item, ruleResults);
|
||||
this.logger.debug(`${this.dryRun ? 'DRYRUN - ' : ''}Done`);
|
||||
}
|
||||
|
||||
abstract process(item: Comment | Submission, ruleResults: RuleResult[]): Promise<void>;
|
||||
}
|
||||
|
||||
export interface ActionOptions {
|
||||
name?: string;
|
||||
logger?: Logger,
|
||||
loggerPrefix?: string,
|
||||
export interface ActionOptions extends ActionConfig {
|
||||
logger: Logger;
|
||||
subredditName: string;
|
||||
}
|
||||
|
||||
export interface ActionConfig {
|
||||
@@ -42,6 +48,12 @@ export interface ActionConfig {
|
||||
* @pattern ^[a-zA-Z]([\w -]*[\w])?$
|
||||
* */
|
||||
name?: string;
|
||||
/**
|
||||
* If `true` the Action will not make the API request to Reddit to perform its action.
|
||||
*
|
||||
* @default false
|
||||
* */
|
||||
dryRun?: boolean;
|
||||
}
|
||||
|
||||
export interface ActionJson extends ActionConfig {
|
||||
|
||||
86
src/App.ts
86
src/App.ts
@@ -1,13 +1,23 @@
|
||||
import Snoowrap from "snoowrap";
|
||||
import {Manager} from "./Subreddit/Manager";
|
||||
import winston, {Logger} from "winston";
|
||||
import {labelledFormat, loggerMetaShuffle} from "./util";
|
||||
import {argParseInt, labelledFormat, parseBool, 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";
|
||||
|
||||
const {transports} = winston;
|
||||
|
||||
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)]),
|
||||
}
|
||||
}
|
||||
|
||||
export class App {
|
||||
|
||||
client: Snoowrap;
|
||||
@@ -15,6 +25,9 @@ export class App {
|
||||
subManagers: Manager[] = [];
|
||||
logger: Logger;
|
||||
wikiLocation: string;
|
||||
dryRun?: true | undefined;
|
||||
heartbeatInterval: number;
|
||||
apiLimitWarning: number;
|
||||
|
||||
constructor(options: any = {}) {
|
||||
const {
|
||||
@@ -23,13 +36,24 @@ export class App {
|
||||
clientSecret,
|
||||
accessToken,
|
||||
refreshToken,
|
||||
logDir,
|
||||
logLevel,
|
||||
wikiConfig,
|
||||
snooDebug,
|
||||
logDir = process.env.LOG_DIR || `${process.cwd()}/logs`,
|
||||
logLevel = process.env.LOG_LEVEL || 'info',
|
||||
wikiConfig = process.env.WIKI_CONFIG || 'botconfig/contextbot',
|
||||
snooDebug = process.env.SNOO_DEBUG || false,
|
||||
dryRun = process.env.DRYRUN || false,
|
||||
heartbeat = process.env.HEARTBEAT || 300,
|
||||
apiLimitWarning = process.env.API_REMAINING || 250,
|
||||
version,
|
||||
authorTTL = process.env.AUTHOR_TTL || 10000,
|
||||
disableCache = process.env.DISABLE_CACHE || false,
|
||||
} = options;
|
||||
|
||||
CacheManager.authorTTL = argParseInt(authorTTL);
|
||||
CacheManager.enabled = !parseBool(disableCache);
|
||||
|
||||
this.dryRun = parseBool(dryRun) === true ? true : undefined;
|
||||
this.heartbeatInterval = argParseInt(heartbeat);
|
||||
this.apiLimitWarning = argParseInt(apiLimitWarning);
|
||||
this.wikiLocation = wikiConfig;
|
||||
|
||||
const consoleTransport = new transports.Console();
|
||||
@@ -75,6 +99,10 @@ export class App {
|
||||
|
||||
this.logger = winston.loggers.get('default');
|
||||
|
||||
if (this.dryRun) {
|
||||
this.logger.info('Running in DRYRUN mode');
|
||||
}
|
||||
|
||||
let subredditsArg = [];
|
||||
if (subreddits !== undefined) {
|
||||
if (Array.isArray(subreddits)) {
|
||||
@@ -93,38 +121,51 @@ export class App {
|
||||
accessToken,
|
||||
};
|
||||
|
||||
let shouldDebug = snooDebug === false && process.env.SNOO_DEBUG === 'true' ? true : snooDebug === true || snooDebug === 'true';
|
||||
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: this.logger.child(loggerMetaShuffle(this.logger, undefined, ['Snoowrap'])),
|
||||
logger: snooLogger,
|
||||
continueAfterRatelimitError: true,
|
||||
});
|
||||
}
|
||||
|
||||
async buildManagers(subreddits: string[] = []) {
|
||||
let availSubs = [];
|
||||
const name = await this.client.getMe().name;
|
||||
this.logger.info(`Reddit API Limit Remaining: ${this.client.ratelimitRemaining}`);
|
||||
this.logger.info(`Authenticated Account: /u/${name}`);
|
||||
for (const sub of await this.client.getModeratedSubreddits()) {
|
||||
// TODO don't know a way to check permissions yet
|
||||
availSubs.push(sub);
|
||||
}
|
||||
this.logger.info(`/u/${name} is a moderator of these subreddits: ${availSubs.map(x => x.display_name_prefixed).join(', ')}`);
|
||||
|
||||
let subsToRun = [];
|
||||
const subsToUse = subreddits.length > 0 ? subreddits : this.subreddits;
|
||||
if (subsToUse.length > 0) {
|
||||
this.logger.info(`User-defined subreddit constraints detected (CLI argument or environmental variable), will try to run on: ${subsToUse.join(', ')}`);
|
||||
for (const sub of subsToUse) {
|
||||
const asub = availSubs.find(x => x.display_name.toLowerCase() === sub.trim().toLowerCase())
|
||||
if (asub === undefined) {
|
||||
this.logger.error(`Will not run on ${sub} because is not modded by, or does not have appropriate permissions to mod with, for this client.`);
|
||||
this.logger.warn(`Will not run on ${sub} because is not modded by, or does not have appropriate permissions to mod with, for this client.`);
|
||||
} else {
|
||||
subsToRun.push(asub);
|
||||
// @ts-ignore
|
||||
const fetchedSub = await asub.fetch();
|
||||
subsToRun.push(fetchedSub);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// otherwise assume all moddable subs from client should be run on
|
||||
this.logger.info('No user-defined subreddit constraints detected, will try to run on all');
|
||||
subsToRun = availSubs;
|
||||
}
|
||||
|
||||
@@ -137,32 +178,47 @@ export class App {
|
||||
const wiki = sub.getWikiPage(this.wikiLocation);
|
||||
content = await wiki.content_md;
|
||||
} catch (err) {
|
||||
this.logger.error(`Could not read wiki configuration for ${sub.display_name}. Please ensure the page 'contextbot' exists and is readable -- error: ${err.message}`);
|
||||
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 = JSON.parse(content);
|
||||
json = JSON5.parse(content);
|
||||
|
||||
} catch (err) {
|
||||
this.logger.error(`Wiki page contents for ${sub.display_name} was not valid -- error: ${err.message}`);
|
||||
this.logger.error(`[${sub.display_name_prefixed}] Wiki page contents was not valid -- error: ${err.message}`);
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
subSchedule.push(new Manager(sub, this.client, this.logger, json));
|
||||
subSchedule.push(new Manager(sub, this.client, this.logger, json, {dryRun: this.dryRun}));
|
||||
} catch (err) {
|
||||
debugger;
|
||||
this.logger.error(`Config for ${sub.display_name} was not valid, will not run for this subreddit`, undefined, err);
|
||||
this.logger.error(`[${sub.display_name_prefixed}] Config was not valid`, undefined, 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async runManagers() {
|
||||
|
||||
for (const manager of this.subManagers) {
|
||||
manager.handle();
|
||||
}
|
||||
|
||||
if (this.heartbeatInterval !== 0) {
|
||||
this.heartbeat();
|
||||
}
|
||||
|
||||
const emitter = new EventEmitter();
|
||||
await pEvent(emitter, 'end');
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import {Logger} from "winston";
|
||||
import {Comment, Submission} from "snoowrap";
|
||||
import {actionFactory} from "../Action/ActionFactory";
|
||||
import {ruleFactory} from "../Rule/RuleFactory";
|
||||
import {createLabelledLogger, loggerMetaShuffle, mergeArr} from "../util";;
|
||||
import {createAjvFactory, mergeArr, ruleNamesFromResults} from "../util";
|
||||
import {JoinCondition, JoinOperands} from "../Common/interfaces";
|
||||
import * as RuleSchema from '../Schema/Rule.json';
|
||||
import * as RuleSetSchema from '../Schema/RuleSet.json';
|
||||
@@ -13,8 +13,6 @@ import * as ActionSchema from '../Schema/Action.json';
|
||||
import Ajv from 'ajv';
|
||||
import {ActionObjectJson, RuleJson, RuleObjectJson, ActionJson as ActionTypeJson} from "../Common/types";
|
||||
|
||||
const ajv = new Ajv();
|
||||
|
||||
export class Check implements ICheck {
|
||||
actions: Action[] = [];
|
||||
description?: string;
|
||||
@@ -22,6 +20,7 @@ export class Check implements ICheck {
|
||||
condition: JoinOperands;
|
||||
rules: Array<RuleSet | Rule> = [];
|
||||
logger: Logger;
|
||||
dryRun?: boolean;
|
||||
|
||||
constructor(options: CheckOptions) {
|
||||
const {
|
||||
@@ -30,18 +29,18 @@ export class Check implements ICheck {
|
||||
condition = 'AND',
|
||||
rules = [],
|
||||
actions = [],
|
||||
subredditName,
|
||||
dryRun,
|
||||
} = options;
|
||||
|
||||
if (options.logger !== undefined) {
|
||||
// @ts-ignore
|
||||
this.logger = options.logger.child(loggerMetaShuffle(options.logger, undefined, [`CHK ${name}`]), mergeArr);
|
||||
} else {
|
||||
this.logger = createLabelledLogger('Check');
|
||||
}
|
||||
this.logger = options.logger.child({labels: [`Check ${name}`]}, mergeArr);
|
||||
|
||||
const ajv = createAjvFactory(this.logger);
|
||||
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.condition = condition;
|
||||
this.dryRun = dryRun;
|
||||
for (const r of rules) {
|
||||
if (r instanceof Rule || r instanceof RuleSet) {
|
||||
this.rules.push(r);
|
||||
@@ -50,16 +49,13 @@ export class Check implements ICheck {
|
||||
let setErrors: any = [];
|
||||
let ruleErrors: any = [];
|
||||
if (valid) {
|
||||
// @ts-ignore
|
||||
r.logger = this.logger;
|
||||
this.rules.push(new RuleSet(r as RuleSetObjectJson));
|
||||
const ruleConfig = r as RuleSetObjectJson;
|
||||
this.rules.push(new RuleSet({...ruleConfig, logger: this.logger, subredditName}));
|
||||
} else {
|
||||
setErrors = ajv.errors;
|
||||
valid = ajv.validate(RuleSchema, r);
|
||||
if (valid) {
|
||||
// @ts-ignore
|
||||
r.logger = this.logger;
|
||||
this.rules.push(ruleFactory(r as RuleJSONConfig));
|
||||
this.rules.push(ruleFactory(r as RuleJSONConfig, this.logger, subredditName));
|
||||
} else {
|
||||
ruleErrors = ajv.errors;
|
||||
const leastErrorType = setErrors.length < ruleErrors ? 'RuleSet' : 'Rule';
|
||||
@@ -79,7 +75,8 @@ export class Check implements ICheck {
|
||||
} else {
|
||||
let valid = ajv.validate(ActionSchema, a);
|
||||
if (valid) {
|
||||
this.actions.push(actionFactory(a as ActionJson));
|
||||
const aj = a as ActionJson;
|
||||
this.actions.push(actionFactory({...aj, dryRun: this.dryRun || aj.dryRun}, this.logger, subredditName));
|
||||
// @ts-ignore
|
||||
a.logger = this.logger;
|
||||
} else {
|
||||
@@ -92,13 +89,11 @@ export class Check implements ICheck {
|
||||
}
|
||||
|
||||
async run(item: Submission | Comment, existingResults: RuleResult[] = []): Promise<[boolean, RuleResult[]]> {
|
||||
//this.logger.debug('Starting check');
|
||||
let allResults: RuleResult[] = [];
|
||||
let runOne = false;
|
||||
for (const r of this.rules) {
|
||||
const combinedResults = [...existingResults, ...allResults];
|
||||
const [passed, results] = await r.run(item, combinedResults);
|
||||
//allResults = allResults.concat(determineNewResults(combinedResults, results));
|
||||
allResults = allResults.concat(results);
|
||||
if (passed === null) {
|
||||
continue;
|
||||
@@ -106,22 +101,28 @@ export class Check implements ICheck {
|
||||
runOne = true;
|
||||
if (passed) {
|
||||
if (this.condition === 'OR') {
|
||||
this.logger.info(`✔️ => Rules (OR): ${ruleNamesFromResults(allResults)}`);
|
||||
return [true, allResults];
|
||||
}
|
||||
} else if (this.condition === 'AND') {
|
||||
this.logger.info(`❌ => Rules (AND): ${ruleNamesFromResults(allResults)}`);
|
||||
return [false, allResults];
|
||||
}
|
||||
}
|
||||
if (!runOne) {
|
||||
this.logger.info('❌ => All Rules skipped because of Author checks');
|
||||
return [false, allResults];
|
||||
}
|
||||
this.logger.info(`✔️ => Rules (AND) : ${ruleNamesFromResults(allResults)}`);
|
||||
return [true, allResults];
|
||||
}
|
||||
|
||||
async runActions(item: Submission | Comment, ruleResults: RuleResult[]): Promise<void> {
|
||||
this.logger.debug(`${this.dryRun ? 'DRYRUN - ' : ''}Running Actions`);
|
||||
for (const a of this.actions) {
|
||||
await a.handle(item, ruleResults);
|
||||
}
|
||||
this.logger.info(`${this.dryRun ? 'DRYRUN - ' : ''}Ran Actions`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,12 +136,20 @@ export interface ICheck extends JoinCondition {
|
||||
* */
|
||||
name: string,
|
||||
description?: string,
|
||||
|
||||
/**
|
||||
* Use this option to override the `dryRun` setting for all of its `Actions`
|
||||
*
|
||||
* @default undefined
|
||||
* */
|
||||
dryRun?: boolean;
|
||||
}
|
||||
|
||||
export interface CheckOptions extends ICheck {
|
||||
rules: Array<IRuleSet | IRule>
|
||||
actions: ActionConfig[]
|
||||
logger?: Logger
|
||||
logger: Logger
|
||||
subredditName: string
|
||||
}
|
||||
|
||||
export interface CheckJson extends ICheck {
|
||||
|
||||
@@ -171,15 +171,37 @@ export interface PollingOptions {
|
||||
}
|
||||
}
|
||||
|
||||
export interface SubredditCacheConfig {
|
||||
/**
|
||||
* Amount of time, in milliseconds, author activities (Comments/Submission) should be cached
|
||||
* */
|
||||
authorTTL?: number;
|
||||
wikiTTL?: number;
|
||||
}
|
||||
|
||||
export interface ManagerOptions {
|
||||
polling?: PollingOptions
|
||||
|
||||
caching?: false | SubredditCacheConfig
|
||||
|
||||
/**
|
||||
* If present, time in milliseconds between HEARTBEAT log statements with current api limit count. Nice to have to know things are still ticking if there is low activity
|
||||
* Use this option to override the `dryRun` setting for all `Checks`
|
||||
*
|
||||
* @default undefined
|
||||
* */
|
||||
heartbeatInterval?: number
|
||||
/**
|
||||
* When Reddit API limit remaining reaches this number context bot will start warning on every poll interval
|
||||
* @default 250
|
||||
* */
|
||||
apiLimitWarning?: number
|
||||
dryRun?: boolean;
|
||||
}
|
||||
|
||||
export interface ThresholdCriteria {
|
||||
/**
|
||||
* The number or percentage to trigger this criteria at
|
||||
*
|
||||
* * If `threshold` is a `number` then it is the absolute number of items to trigger at
|
||||
* * 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%
|
||||
* */
|
||||
threshold: number | string
|
||||
|
||||
condition: '>' | '>=' | '<' | '<='
|
||||
}
|
||||
|
||||
@@ -7,8 +7,9 @@ import {CommentActionJson} from "../Action/CommentAction";
|
||||
import {ReportActionJson} from "../Action/ReportAction";
|
||||
import {LockActionJson} from "../Action/LockAction";
|
||||
import {RemoveActionJson} from "../Action/RemoveAction";
|
||||
import {HistoryJSONConfig} from "../Rule/HistoryRule";
|
||||
|
||||
export type RuleJson = RecentActivityRuleJSONConfig | RepeatActivityJSONConfig | AuthorRuleJSONConfig | AttributionJSONConfig | string;
|
||||
export type RuleJson = RecentActivityRuleJSONConfig | RepeatActivityJSONConfig | AuthorRuleJSONConfig | AttributionJSONConfig | HistoryJSONConfig | string;
|
||||
export type RuleObjectJson = Exclude<RuleJson, string>
|
||||
|
||||
export type ActionJson = FlairActionJson | CommentActionJson | ReportActionJson | LockActionJson | RemoveActionJson | string;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import {Logger} from "winston";
|
||||
import {createLabelledLogger, loggerMetaShuffle, mergeArr} from "./util";
|
||||
import {createAjvFactory, mergeArr, normalizeName} from "./util";
|
||||
import {CommentCheck} from "./Check/CommentCheck";
|
||||
import {SubmissionCheck} from "./Check/SubmissionCheck";
|
||||
|
||||
@@ -14,75 +14,61 @@ import deepEqual from "fast-deep-equal";
|
||||
import {ActionJson, ActionObjectJson, RuleJson, RuleObjectJson} from "./Common/types";
|
||||
import {isActionJson} from "./Action";
|
||||
|
||||
const ajv = new Ajv();
|
||||
|
||||
export interface ConfigBuilderOptions {
|
||||
logger?: Logger,
|
||||
logger: Logger,
|
||||
}
|
||||
|
||||
export class ConfigBuilder {
|
||||
configLogger: Logger;
|
||||
logger: Logger;
|
||||
|
||||
constructor(options: ConfigBuilderOptions) {
|
||||
|
||||
if (options.logger !== undefined) {
|
||||
this.logger = options.logger.child(loggerMetaShuffle(options.logger, 'Config'), mergeArr);
|
||||
} else {
|
||||
this.logger = createLabelledLogger(`Config`, `Config`);
|
||||
}
|
||||
this.configLogger = options.logger.child({leaf: 'Config'}, mergeArr);
|
||||
this.logger = options.logger;
|
||||
}
|
||||
|
||||
buildFromJson(config: object): [Array<SubmissionCheck>,Array<CommentCheck>,ManagerOptions] {
|
||||
const commentChecks: Array<CommentCheck> = [];
|
||||
const subChecks: Array<SubmissionCheck> = [];
|
||||
|
||||
let namedRules: Map<string,RuleObjectJson> = new Map();
|
||||
let namedActions: Map<string,ActionObjectJson> = new Map();
|
||||
|
||||
validateJson(config: object): JSONConfig {
|
||||
const ajv = createAjvFactory(this.logger);
|
||||
const valid = ajv.validate(schema, config);
|
||||
let managerOptions: ManagerOptions = {};
|
||||
if(valid) {
|
||||
const validConfig = config as JSONConfig;
|
||||
const {checks = [], ...rest} = validConfig;
|
||||
for(const c of checks) {
|
||||
namedRules = extractNamedRules(c.rules, namedRules);
|
||||
namedActions = extractNamedActions(c.actions, namedActions);
|
||||
}
|
||||
|
||||
const structuredChecks: CheckStructuredJson[] = [];
|
||||
for(const c of checks) {
|
||||
const strongRules = insertNamedRules(c.rules, namedRules);
|
||||
const strongActions = insertNamedActions(c.actions, namedActions);
|
||||
const strongCheck = {...c, rules: strongRules, actions: strongActions} as CheckStructuredJson;
|
||||
structuredChecks.push(strongCheck);
|
||||
}
|
||||
|
||||
managerOptions = rest;
|
||||
for (const jCheck of structuredChecks) {
|
||||
if (jCheck.kind === 'comment') {
|
||||
commentChecks.push(new CommentCheck({...jCheck, logger: this.logger}));
|
||||
} else if (jCheck.kind === 'submission') {
|
||||
subChecks.push(new SubmissionCheck({...jCheck, logger: this.logger}));
|
||||
}
|
||||
}
|
||||
if (valid) {
|
||||
return config as JSONConfig;
|
||||
} else {
|
||||
this.logger.error('Json config was not valid. Please use schema to check validity.');
|
||||
if(Array.isArray(ajv.errors)) {
|
||||
for(const err of ajv.errors) {
|
||||
this.configLogger.error('Json config was not valid. Please use schema to check validity.');
|
||||
if (Array.isArray(ajv.errors)) {
|
||||
for (const err of ajv.errors) {
|
||||
let suffix = '';
|
||||
// @ts-ignore
|
||||
if(err.params.allowedValues !== undefined) {
|
||||
if (err.params.allowedValues !== undefined) {
|
||||
// @ts-ignore
|
||||
suffix = err.params.allowedValues.join(', ');
|
||||
suffix = ` [${suffix}]`;
|
||||
}
|
||||
this.logger.error(`${err.keyword}: ${err.schemaPath} => ${err.message}${suffix}`);
|
||||
this.configLogger.error(`${err.keyword}: ${err.schemaPath} => ${err.message}${suffix}`);
|
||||
}
|
||||
}
|
||||
throw new LoggedError();
|
||||
}
|
||||
}
|
||||
|
||||
return [subChecks, commentChecks, managerOptions];
|
||||
parseToStructured(config: JSONConfig): CheckStructuredJson[] {
|
||||
let namedRules: Map<string, RuleObjectJson> = new Map();
|
||||
let namedActions: Map<string, ActionObjectJson> = new Map();
|
||||
const {checks = []} = config;
|
||||
for (const c of checks) {
|
||||
namedRules = extractNamedRules(c.rules, namedRules);
|
||||
namedActions = extractNamedActions(c.actions, namedActions);
|
||||
}
|
||||
|
||||
const structuredChecks: CheckStructuredJson[] = [];
|
||||
for (const c of checks) {
|
||||
const strongRules = insertNamedRules(c.rules, namedRules);
|
||||
const strongActions = insertNamedActions(c.actions, namedActions);
|
||||
const strongCheck = {...c, rules: strongRules, actions: strongActions} as CheckStructuredJson;
|
||||
structuredChecks.push(strongCheck);
|
||||
}
|
||||
|
||||
return structuredChecks;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,7 +90,7 @@ export const extractNamedRules = (rules: Array<RuleSetJson | RuleJson>, namedRul
|
||||
}
|
||||
for (const rule of rulesToAdd) {
|
||||
const name = rule.name as string;
|
||||
const normalName = name.toLowerCase();
|
||||
const normalName = normalizeName(name);
|
||||
const {name: n, ...rest} = rule;
|
||||
const ruleNoName = {...rest};
|
||||
|
||||
@@ -146,7 +132,7 @@ export const insertNamedRules = (rules: Array<RuleSetJson | RuleJson>, namedRule
|
||||
|
||||
export const extractNamedActions = (actions: Array<ActionJson>, namedActions: Map<string, ActionObjectJson> = new Map()): Map<string, ActionObjectJson> => {
|
||||
for (const a of actions) {
|
||||
if(!(typeof a === 'string')) {
|
||||
if (!(typeof a === 'string')) {
|
||||
if (isActionJson(a) && a.name !== undefined) {
|
||||
const normalName = a.name.toLowerCase();
|
||||
const {name: n, ...rest} = a;
|
||||
@@ -175,7 +161,7 @@ export const insertNamedActions = (actions: Array<ActionJson>, namedActions: Map
|
||||
throw new Error(`No named Action with the name ${a} was found`);
|
||||
}
|
||||
strongActions.push(foundAction);
|
||||
}else {
|
||||
} else {
|
||||
strongActions.push(a);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 testAuthorCriteria(item, auth)) {
|
||||
if (await this.cache.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 testAuthorCriteria(item, auth, false)) {
|
||||
if (await this.cache.testAuthorCriteria(item, auth, false)) {
|
||||
return Promise.resolve([true, [this.getResult(true)]]);
|
||||
}
|
||||
}
|
||||
|
||||
295
src/Rule/HistoryRule.ts
Normal file
295
src/Rule/HistoryRule.ts
Normal file
@@ -0,0 +1,295 @@
|
||||
|
||||
import {ActivityWindowType, ThresholdCriteria} from "../Common/interfaces";
|
||||
import {Rule, RuleJSONConfig, RuleOptions, RuleResult} from "./index";
|
||||
import Submission from "snoowrap/dist/objects/Submission";
|
||||
import {getAuthorActivities} from "../Utils/SnoowrapUtils";
|
||||
import dayjs from "dayjs";
|
||||
import {comparisonTextOp, formatNumber, percentFromString} from "../util";
|
||||
|
||||
export interface CommentThresholdCriteria extends ThresholdCriteria {
|
||||
/**
|
||||
* If `true` then when threshold...
|
||||
*
|
||||
* * is `number` it will be number of comments where author is OP
|
||||
* * is `percent` it will be **percent of total comments where author is OP**
|
||||
* */
|
||||
asOp?: boolean
|
||||
}
|
||||
/**
|
||||
* If both `submission` and `comment` are defined then criteria will only trigger if BOTH thresholds are met
|
||||
* */
|
||||
export interface HistoryCriteria {
|
||||
|
||||
submission?: ThresholdCriteria
|
||||
comment?: CommentThresholdCriteria
|
||||
/**
|
||||
* Window defining Activities to consider (both Comment/Submission)
|
||||
*/
|
||||
window: ActivityWindowType
|
||||
|
||||
/**
|
||||
* The minimum number of activities that must exist from the `window` results for this criteria to run
|
||||
* @default 5
|
||||
* */
|
||||
minActivityCount?: number
|
||||
name?: string
|
||||
}
|
||||
|
||||
export class HistoryRule extends Rule {
|
||||
criteria: HistoryCriteria[];
|
||||
criteriaJoin: 'AND' | 'OR';
|
||||
include: string[];
|
||||
exclude: string[];
|
||||
|
||||
constructor(options: HistoryOptions) {
|
||||
super(options);
|
||||
const {
|
||||
criteria,
|
||||
criteriaJoin = 'OR',
|
||||
include = [],
|
||||
exclude = [],
|
||||
} = options || {};
|
||||
|
||||
this.criteria = criteria;
|
||||
this.criteriaJoin = criteriaJoin;
|
||||
if (this.criteria.length === 0) {
|
||||
throw new Error('Must provide at least one HistoryCriteria');
|
||||
}
|
||||
this.include = include.map(x => x.toLowerCase());
|
||||
this.exclude = exclude.map(x => x.toLowerCase());
|
||||
}
|
||||
|
||||
getKind(): string {
|
||||
return "History";
|
||||
}
|
||||
|
||||
protected getSpecificPremise(): object {
|
||||
return {
|
||||
criteria: this.criteria,
|
||||
include: this.include,
|
||||
exclude: this.exclude,
|
||||
}
|
||||
}
|
||||
|
||||
protected async process(item: Submission): Promise<[boolean, RuleResult[]]> {
|
||||
// TODO reuse activities between ActivityCriteria to reduce api calls
|
||||
|
||||
let criteriaResults = [];
|
||||
|
||||
for (const criteria of this.criteria) {
|
||||
|
||||
const {comment, window, submission, minActivityCount = 5} = criteria;
|
||||
|
||||
let activities = await this.cache.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());
|
||||
} else if (this.exclude.length > 0) {
|
||||
return !this.exclude.some(x => x === act.subreddit.display_name.toLowerCase())
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
if (activities.length < minActivityCount) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const activityTotal = activities.length;
|
||||
const {submissionTotal, commentTotal, opTotal} = activities.reduce((acc, act) => {
|
||||
if(act instanceof Submission) {
|
||||
return {...acc, submissionTotal: acc.submissionTotal + 1};
|
||||
}
|
||||
let a = {...acc, commentTotal: acc.commentTotal + 1};
|
||||
if(act.is_submitter) {
|
||||
a.opTotal = a.opTotal + 1;
|
||||
}
|
||||
return a;
|
||||
},{submissionTotal: 0, commentTotal: 0, opTotal: 0});
|
||||
|
||||
let commentTrigger = undefined;
|
||||
if(comment !== undefined) {
|
||||
const {threshold, condition, asOp = false} = comment;
|
||||
if(typeof threshold === 'string') {
|
||||
const per = percentFromString(threshold);
|
||||
if(asOp) {
|
||||
commentTrigger = comparisonTextOp(opTotal / commentTotal, condition, per);
|
||||
} else {
|
||||
commentTrigger = comparisonTextOp(commentTotal / activityTotal, condition, per);
|
||||
}
|
||||
} else {
|
||||
if(asOp) {
|
||||
commentTrigger = comparisonTextOp(opTotal, condition, threshold);
|
||||
} else {
|
||||
commentTrigger = comparisonTextOp(commentTotal, condition, threshold);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let submissionTrigger = undefined;
|
||||
if(submission !== undefined) {
|
||||
const {threshold, condition, } = submission;
|
||||
if(typeof threshold === 'string') {
|
||||
const per = percentFromString(threshold);
|
||||
submissionTrigger = comparisonTextOp(submissionTotal / activityTotal, condition, per);
|
||||
} else {
|
||||
submissionTrigger = comparisonTextOp(submissionTotal, condition, threshold);
|
||||
}
|
||||
}
|
||||
|
||||
const firstActivity = activities[0];
|
||||
const lastActivity = activities[activities.length - 1];
|
||||
|
||||
const activityTotalWindow = dayjs.duration(dayjs(firstActivity.created_utc * 1000).diff(dayjs(lastActivity.created_utc * 1000)));
|
||||
|
||||
criteriaResults.push({
|
||||
criteria,
|
||||
activityTotal,
|
||||
activityTotalWindow,
|
||||
submissionTotal,
|
||||
commentTotal,
|
||||
opTotal,
|
||||
triggered: submissionTrigger === true || commentTrigger === true
|
||||
});
|
||||
}
|
||||
|
||||
let criteriaMeta = false;
|
||||
if (this.criteriaJoin === 'OR') {
|
||||
criteriaMeta = criteriaResults.some(x => x.triggered);
|
||||
} else {
|
||||
criteriaMeta = criteriaResults.every(x => x.triggered);
|
||||
}
|
||||
|
||||
if (criteriaMeta) {
|
||||
// use first triggered criteria found
|
||||
const refCriteriaResults = criteriaResults.find(x => x.triggered);
|
||||
if (refCriteriaResults !== undefined) {
|
||||
const {
|
||||
activityTotal,
|
||||
activityTotalWindow,
|
||||
submissionTotal,
|
||||
commentTotal,
|
||||
opTotal,
|
||||
criteria: {
|
||||
comment: {
|
||||
threshold: cthresh,
|
||||
condition: ccond,
|
||||
asOp
|
||||
} = {},
|
||||
submission: {
|
||||
threshold: sthresh,
|
||||
condition: scond,
|
||||
} = {},
|
||||
window,
|
||||
},
|
||||
criteria,
|
||||
} = refCriteriaResults;
|
||||
|
||||
const data: any = {
|
||||
activityTotal,
|
||||
submissionTotal,
|
||||
commentTotal,
|
||||
opTotal,
|
||||
commentPercent: formatNumber((commentTotal/activityTotal)*100),
|
||||
submissionPercent: formatNumber((submissionTotal/activityTotal)*100),
|
||||
opPercent: formatNumber((opTotal/commentTotal)*100),
|
||||
criteria,
|
||||
window: typeof window === 'number' ? `${activityTotal} Items` : activityTotalWindow.humanize(true)
|
||||
|
||||
};
|
||||
|
||||
let thresholdSummary = [];
|
||||
let submissionSummary;
|
||||
let commentSummary;
|
||||
if(sthresh !== undefined) {
|
||||
const suffix = typeof sthresh === 'number' ? 'Items' : `(${formatNumber((submissionTotal/activityTotal)*100)}%) of ${activityTotal} Total`;
|
||||
submissionSummary = `Submissions (${submissionTotal}) were ${scond}${sthresh} ${suffix}`;
|
||||
data.submissionSummary = submissionSummary;
|
||||
thresholdSummary.push(submissionSummary);
|
||||
}
|
||||
if(cthresh !== undefined) {
|
||||
const totalType = asOp ? 'Comments' : 'Activities'
|
||||
const countType = asOp ? 'Comments as OP' : 'Comments';
|
||||
const suffix = typeof cthresh === 'number' ? 'Items' : `(${asOp ? formatNumber((opTotal/commentTotal)*100) : formatNumber((commentTotal/activityTotal)*100)}%) of ${activityTotal} Total ${totalType}`;
|
||||
commentSummary = `${countType} (${asOp ? opTotal : commentTotal}) were ${ccond}${cthresh} ${suffix}`;
|
||||
data.commentSummary = commentSummary;
|
||||
thresholdSummary.push(commentSummary);
|
||||
}
|
||||
|
||||
data.thresholdSummary = thresholdSummary.join(' and ');
|
||||
|
||||
const result = `${thresholdSummary} (${data.window})`;
|
||||
this.logger.verbose(result);
|
||||
return Promise.resolve([true, [this.getResult(true, {
|
||||
result,
|
||||
data,
|
||||
})]]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return Promise.resolve([false, [this.getResult(false)]]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default HistoryRule;
|
||||
|
||||
interface HistoryConfig {
|
||||
|
||||
/**
|
||||
* A list threshold-window values to test activities against.
|
||||
*
|
||||
* @minItems 1
|
||||
* */
|
||||
criteria: HistoryCriteria[]
|
||||
|
||||
/**
|
||||
* * If `OR` then any set of Criteria that pass will trigger the Rule
|
||||
* * If `AND` then all Criteria sets must pass to trigger the Rule
|
||||
* */
|
||||
criteriaJoin?: 'AND' | 'OR'
|
||||
|
||||
/**
|
||||
* Only include Submissions from this list of Subreddits.
|
||||
*
|
||||
* 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 1
|
||||
* */
|
||||
include?: string[],
|
||||
/**
|
||||
* Do not include Submissions from this list of Subreddits.
|
||||
*
|
||||
* 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 1
|
||||
* */
|
||||
exclude?: string[],
|
||||
}
|
||||
|
||||
export interface HistoryOptions extends HistoryConfig, RuleOptions {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* Available data for [Action templating](https://github.com/FoxxMD/reddit-context-bot#action-templating):
|
||||
*
|
||||
* ```
|
||||
* activityTotal => Total number of activities
|
||||
* submissionTotal => Total number of submissions
|
||||
* commentTotal => Total number of comments
|
||||
* opTotal => Total number of comments as OP
|
||||
* thresholdSummary => A text summary of the first Criteria triggered with totals/percentages
|
||||
* criteria => The ThresholdCriteria object
|
||||
* window => A text summary of the range of Activities considered (# of Items if number, time range if Duration)
|
||||
* ```
|
||||
* */
|
||||
export interface HistoryJSONConfig extends HistoryConfig, RuleJSONConfig {
|
||||
kind: 'history'
|
||||
}
|
||||
@@ -1,8 +1,7 @@
|
||||
import {Rule, RuleJSONConfig, RuleOptions, RulePremise, RuleResult} from "./index";
|
||||
import {Comment, VoteableContent} from "snoowrap";
|
||||
import Submission from "snoowrap/dist/objects/Submission";
|
||||
import {getAuthorActivities, getAuthorComments, getAuthorSubmissions} from "../Utils/SnoowrapUtils";
|
||||
import {parseUsableLinkIdentifier} from "../util";
|
||||
import {activityWindowText, parseUsableLinkIdentifier} from "../util";
|
||||
import {
|
||||
ActivityWindow,
|
||||
ActivityWindowCriteria,
|
||||
@@ -50,13 +49,13 @@ export class RecentActivityRule extends Rule {
|
||||
|
||||
switch (this.lookAt) {
|
||||
case 'comments':
|
||||
activities = await getAuthorComments(item.author, {window: this.window});
|
||||
activities = await this.cache.getAuthorComments(item.author, {window: this.window});
|
||||
break;
|
||||
case 'submissions':
|
||||
activities = await getAuthorSubmissions(item.author, {window: this.window});
|
||||
activities = await this.cache.getAuthorSubmissions(item.author, {window: this.window});
|
||||
break;
|
||||
default:
|
||||
activities = await getAuthorActivities(item.author, {window: this.window});
|
||||
activities = await this.cache.getAuthorActivities(item.author, {window: this.window});
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -64,9 +63,9 @@ export class RecentActivityRule extends Rule {
|
||||
let viableActivity = activities;
|
||||
if (this.useSubmissionAsReference) {
|
||||
if (!(item instanceof Submission)) {
|
||||
this.logger.debug('Cannot use post as reference because triggered item is not a Submission');
|
||||
this.logger.warn('Cannot use post as reference because triggered item is not a Submission');
|
||||
} else if (item.is_self) {
|
||||
this.logger.debug('Cannot use post as reference because triggered Submission is not a link type');
|
||||
this.logger.warn('Cannot use post as reference because triggered Submission is not a link type');
|
||||
} else {
|
||||
const usableUrl = parseLink(await item.url);
|
||||
viableActivity = viableActivity.filter((x) => {
|
||||
@@ -85,28 +84,69 @@ export class RecentActivityRule extends Rule {
|
||||
grouped[s] = (grouped[s] || []).concat(activity);
|
||||
return grouped;
|
||||
}, {} as Record<string, (Submission | Comment)[]>);
|
||||
const triggeredOn = [];
|
||||
let triggeredPerSub = [];
|
||||
let totalTriggeredOn;
|
||||
for (const triggerSet of this.thresholds) {
|
||||
const {count: threshold = 1, subreddits = []} = triggerSet;
|
||||
triggeredPerSub = [];
|
||||
let currCount = 0;
|
||||
let presentSubs = [];
|
||||
const {count: subCount, totalCount, subreddits = []} = triggerSet;
|
||||
for (const sub of subreddits) {
|
||||
const isub = sub.toLowerCase();
|
||||
const {[isub]: tSub = []} = groupedActivity;
|
||||
if (tSub.length >= threshold) {
|
||||
triggeredOn.push({subreddit: sub, count: tSub.length});
|
||||
if(tSub.length > 0) {
|
||||
currCount += tSub.length;
|
||||
presentSubs.push(sub);
|
||||
if (subCount !== undefined && tSub.length >= subCount) {
|
||||
triggeredPerSub.push({subreddit: sub, count: tSub.length, threshold: subCount});
|
||||
}
|
||||
}
|
||||
}
|
||||
if(totalCount !== undefined && currCount >= totalCount) {
|
||||
totalTriggeredOn = {subreddits: presentSubs, count: currCount, threshold: totalCount};
|
||||
}
|
||||
// if either trigger condition is hit end the iteration early
|
||||
if(triggeredPerSub.length > 0 || totalTriggeredOn !== undefined) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (triggeredOn.length > 0) {
|
||||
const friendlyText = triggeredOn.map(x => `${x.subreddit}(${x.count})`).join(', ');
|
||||
const friendly = `Triggered by: ${friendlyText}`;
|
||||
this.logger.debug(friendly);
|
||||
if (triggeredPerSub.length > 0 || totalTriggeredOn !== undefined) {
|
||||
let resultArr = [];
|
||||
const data: any = {};
|
||||
if(triggeredPerSub.length > 0) {
|
||||
data.perSubCount = triggeredPerSub.length;
|
||||
data.perSubTotal = triggeredPerSub.reduce((acc, x) => acc + x.count, 0);
|
||||
data.perSubSubredditsSummary = triggeredPerSub.map(x => x.subreddit).join(', ');
|
||||
data.perSubSummary = triggeredPerSub.map(x => `${x.subreddit}(${x.count})`).join(', ');
|
||||
data.perSubThreshold = triggeredPerSub[0].threshold;
|
||||
resultArr.push(`${triggeredPerSub.length} subs have >${triggeredPerSub[0].threshold} activities (${data.perSubTotal} Total)`);
|
||||
}
|
||||
if(totalTriggeredOn !== undefined) {
|
||||
data.totalCount = totalTriggeredOn.count;
|
||||
data.totalSubredditsCount = totalTriggeredOn.subreddits.length;
|
||||
data.totalSubredditsSummary = totalTriggeredOn.subreddits.join(', ')
|
||||
data.totalThreshold = totalTriggeredOn.threshold;
|
||||
data.totalSummary = `${data.totalCount} (>${totalTriggeredOn.threshold}) activities over ${totalTriggeredOn.subreddits.length} subreddits`;
|
||||
resultArr.push(data.totalSummary);
|
||||
}
|
||||
let summary;
|
||||
if(resultArr.length === 2) {
|
||||
// need a shortened summary
|
||||
summary = `${data.perSubCount} per-sub triggers (${data.perSubThreshold}) and ${data.totalCount} total (${data.totalThreshold})`
|
||||
} else {
|
||||
summary = resultArr[0];
|
||||
}
|
||||
const result = resultArr.join(' and ')
|
||||
this.logger.verbose(result);
|
||||
return Promise.resolve([true, [this.getResult(true, {
|
||||
result: friendly,
|
||||
result,
|
||||
data: {
|
||||
triggeredOn,
|
||||
summary: friendlyText,
|
||||
subCount: triggeredOn.length,
|
||||
totalCount: triggeredOn.reduce((cnt, data) => cnt + data.count, 0)
|
||||
window: typeof this.window === 'number' ? `${activities.length} Items` : activityWindowText(viableActivity),
|
||||
triggeredOn: triggeredPerSub,
|
||||
summary,
|
||||
subSummary: data.totalSubredditsSummary|| data.perSubSubredditsSummary,
|
||||
subCount: data.totalSubredditsCount || data.perSubCount,
|
||||
totalCount: data.totalCount || data.perSubTotal
|
||||
}
|
||||
})]]);
|
||||
}
|
||||
@@ -115,13 +155,23 @@ export class RecentActivityRule extends Rule {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* At least one count property must be present. If both are present then either can trigger the rule
|
||||
*
|
||||
* @minProperties 1
|
||||
* @additionalProperties false
|
||||
* */
|
||||
export interface SubThreshold extends SubredditCriteria {
|
||||
/**
|
||||
* The number of activities in each subreddit from the list that will trigger this rule
|
||||
* @default 1
|
||||
* @minimum 1
|
||||
* */
|
||||
count?: number,
|
||||
/**
|
||||
* The total number of activities across all listed subreddits that will trigger this rule
|
||||
* @minimum 1
|
||||
* */
|
||||
totalCount?: number
|
||||
}
|
||||
|
||||
interface RecentActivityConfig extends ActivityWindow, ReferenceSubmission {
|
||||
|
||||
@@ -3,18 +3,28 @@ import RepeatActivityRule, {RepeatActivityJSONConfig} from "./SubmissionRule/Rep
|
||||
import {Rule, RuleJSONConfig} from "./index";
|
||||
import AuthorRule, {AuthorRuleJSONConfig} from "./AuthorRule";
|
||||
import {AttributionJSONConfig, AttributionRule} from "./SubmissionRule/AttributionRule";
|
||||
import {Logger} from "winston";
|
||||
import HistoryRule, {HistoryJSONConfig} from "./HistoryRule";
|
||||
|
||||
export function ruleFactory
|
||||
(config: RuleJSONConfig): Rule {
|
||||
(config: RuleJSONConfig, logger: Logger, subredditName: string): Rule {
|
||||
let cfg;
|
||||
switch (config.kind) {
|
||||
case 'recentActivity':
|
||||
return new RecentActivityRule(config as RecentActivityRuleJSONConfig);
|
||||
cfg = config as RecentActivityRuleJSONConfig;
|
||||
return new RecentActivityRule({...cfg, logger, subredditName});
|
||||
case 'repeatActivity':
|
||||
return new RepeatActivityRule(config as RepeatActivityJSONConfig);
|
||||
cfg = config as RepeatActivityJSONConfig;
|
||||
return new RepeatActivityRule({...cfg, logger, subredditName});
|
||||
case 'author':
|
||||
return new AuthorRule(config as AuthorRuleJSONConfig);
|
||||
cfg = config as AuthorRuleJSONConfig;
|
||||
return new AuthorRule({...cfg, logger, subredditName});
|
||||
case 'attribution':
|
||||
return new AttributionRule(config as AttributionJSONConfig);
|
||||
cfg = config as AttributionJSONConfig;
|
||||
return new AttributionRule({...cfg, logger, subredditName});
|
||||
case 'history':
|
||||
cfg = config as HistoryJSONConfig;
|
||||
return new HistoryRule({...cfg, logger, subredditName});
|
||||
default:
|
||||
throw new Error('rule "kind" was not recognized.');
|
||||
}
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
import {IRule, Triggerable, Rule, RuleJSONConfig, RuleResult} from "./index";
|
||||
import {Comment, Submission} from "snoowrap";
|
||||
import {ruleFactory} from "./RuleFactory";
|
||||
import {createLabelledLogger, loggerMetaShuffle} from "../util";
|
||||
import {createAjvFactory, mergeArr} from "../util";
|
||||
import {Logger} from "winston";
|
||||
import {JoinCondition, JoinOperands} from "../Common/interfaces";
|
||||
import * as RuleSchema from '../Schema/Rule.json';
|
||||
import Ajv from 'ajv';
|
||||
import {RuleJson, RuleObjectJson} from "../Common/types";
|
||||
|
||||
const ajv = new Ajv();
|
||||
|
||||
export class RuleSet implements IRuleSet, Triggerable {
|
||||
rules: Rule[] = [];
|
||||
condition: JoinOperands;
|
||||
@@ -17,21 +15,16 @@ export class RuleSet implements IRuleSet, Triggerable {
|
||||
|
||||
constructor(options: RuleSetOptions) {
|
||||
const {logger, condition = 'AND', rules = []} = options;
|
||||
if (logger !== undefined) {
|
||||
this.logger = logger.child(loggerMetaShuffle(logger, 'Rule Set'));
|
||||
} else {
|
||||
this.logger = createLabelledLogger('Rule Set');
|
||||
}
|
||||
this.logger = logger.child({leaf: 'Rule Set'}, mergeArr);
|
||||
this.condition = condition;
|
||||
const ajv = createAjvFactory(this.logger);
|
||||
for (const r of rules) {
|
||||
if (r instanceof Rule) {
|
||||
this.rules.push(r);
|
||||
} else {
|
||||
const valid = ajv.validate(RuleSchema, r);
|
||||
if (valid) {
|
||||
// @ts-ignore
|
||||
r.logger = this.logger;
|
||||
this.rules.push(ruleFactory(r as RuleJSONConfig));
|
||||
this.rules.push(ruleFactory(r as RuleJSONConfig, logger, options.subredditName));
|
||||
} else {
|
||||
this.logger.warn('Could not build rule because of JSON errors', {}, {errors: ajv.errors, obj: r});
|
||||
}
|
||||
@@ -77,7 +70,8 @@ export interface IRuleSet extends JoinCondition {
|
||||
|
||||
export interface RuleSetOptions extends IRuleSet {
|
||||
rules: Array<IRule | RuleJSONConfig>,
|
||||
logger?: Logger
|
||||
logger: Logger
|
||||
subredditName: string
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import {SubmissionRule, SubmissionRuleJSONConfig} from "./index";
|
||||
import {ActivityWindow, ActivityWindowType, ReferenceSubmission} from "../../Common/interfaces";
|
||||
import {ActivityWindowType, ReferenceSubmission} from "../../Common/interfaces";
|
||||
import {RuleOptions, RuleResult} from "../index";
|
||||
import Submission from "snoowrap/dist/objects/Submission";
|
||||
import {getAttributionIdentifier, getAuthorActivities, getAuthorSubmissions} from "../../Utils/SnoowrapUtils";
|
||||
import {getAttributionIdentifier} from "../../Utils/SnoowrapUtils";
|
||||
import dayjs from "dayjs";
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ export interface AttributionCriteria {
|
||||
**/
|
||||
thresholdOn?: 'submissions' | 'all'
|
||||
/**
|
||||
* The minimum number of activities (activities defined in `includeInTotal`) that must exist for this criteria to run
|
||||
* The minimum number of activities that must exist for this criteria to run
|
||||
* @default 5
|
||||
* */
|
||||
minActivityCount?: number
|
||||
@@ -114,7 +114,7 @@ export class AttributionRule extends SubmissionRule {
|
||||
percentVal = Number.parseInt(threshold.replace('%', '')) / 100;
|
||||
}
|
||||
|
||||
let activities = thresholdOn === 'submissions' ? await getAuthorSubmissions(item.author, {window: window}) : await getAuthorActivities(item.author, {window: window});
|
||||
let activities = thresholdOn === 'submissions' ? await this.cache.getAuthorSubmissions(item.author, {window: window}) : await this.cache.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());
|
||||
@@ -235,7 +235,7 @@ export class AttributionRule extends SubmissionRule {
|
||||
}
|
||||
|
||||
const result = `${triggeredDomains.length} Attribution(s) met the threshold of ${threshold}, largest being ${largestCount} (${largestPercent}%) of ${activityTotal} Total -- window: ${data.window}`;
|
||||
|
||||
this.logger.verbose(result);
|
||||
return Promise.resolve([true, [this.getResult(true, {
|
||||
result,
|
||||
data,
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import {SubmissionRule, SubmissionRuleJSONConfig} from "./index";
|
||||
import {Rule, RuleOptions, RulePremise, RuleResult} from "../index";
|
||||
import {RuleOptions, RuleResult} from "../index";
|
||||
import {Comment} from "snoowrap";
|
||||
import {getAuthorActivities, getAuthorComments, getAuthorSubmissions} from "../../Utils/SnoowrapUtils";
|
||||
import {groupBy, parseUsableLinkIdentifier as linkParser, truncateStringToLength} from "../../util";
|
||||
import {activityWindowText, parseUsableLinkIdentifier as linkParser} from "../../util";
|
||||
import {ActivityWindow, ActivityWindowType, ReferenceSubmission} from "../../Common/interfaces";
|
||||
import Submission from "snoowrap/dist/objects/Submission";
|
||||
import dayjs from "dayjs";
|
||||
@@ -86,10 +85,10 @@ export class RepeatActivityRule extends SubmissionRule {
|
||||
let activities: (Submission | Comment)[] = [];
|
||||
switch (this.lookAt) {
|
||||
case 'submissions':
|
||||
activities = await getAuthorSubmissions(item.author, {window: this.window});
|
||||
activities = await this.cache.getAuthorSubmissions(item.author, {window: this.window});
|
||||
break;
|
||||
default:
|
||||
activities = await getAuthorActivities(item.author, {window: this.window});
|
||||
activities = await this.cache.getAuthorActivities(item.author, {window: this.window});
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -167,9 +166,11 @@ export class RepeatActivityRule extends SubmissionRule {
|
||||
if (triggeringSummaries.length > 0) {
|
||||
const largestRepeat = triggeringSummaries.reduce((acc, summ) => Math.max(summ.largestTrigger, acc), 0);
|
||||
const result = `${triggeringSummaries.length} of ${identifiersSummary.length} unique items repeated >=${this.threshold} (threshold) times, largest repeat: ${largestRepeat}`;
|
||||
this.logger.verbose(result);
|
||||
return Promise.resolve([true, [this.getResult(true, {
|
||||
result,
|
||||
data: {
|
||||
window: typeof this.window === 'number' ? `${activities.length} Items` : activityWindowText(activities),
|
||||
totalTriggeringSets: triggeringSummaries.length,
|
||||
largestRepeat,
|
||||
threshold: this.threshold,
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
import {Comment, RedditUser} from "snoowrap";
|
||||
import {Comment} from "snoowrap";
|
||||
import Submission from "snoowrap/dist/objects/Submission";
|
||||
import {Logger} from "winston";
|
||||
import {createLabelledLogger, findResultByPremise, loggerMetaShuffle, mergeArr} from "../util";
|
||||
import {findResultByPremise, mergeArr} from "../util";
|
||||
import {testAuthorCriteria} from "../Utils/SnoowrapUtils";
|
||||
import CacheManager, {SubredditCache} from "../Subreddit/SubredditCache";
|
||||
|
||||
export interface RuleOptions {
|
||||
name?: string;
|
||||
authors?: AuthorOptions;
|
||||
logger?: Logger
|
||||
loggerPrefix?: string
|
||||
logger: Logger
|
||||
subredditName: string;
|
||||
}
|
||||
|
||||
export interface RulePremise {
|
||||
@@ -35,18 +36,20 @@ export abstract class Rule implements IRule, Triggerable {
|
||||
name: string;
|
||||
logger: Logger
|
||||
authors: AuthorOptions;
|
||||
cache: SubredditCache;
|
||||
|
||||
constructor(options: RuleOptions) {
|
||||
const {
|
||||
name = this.getKind(),
|
||||
loggerPrefix = '',
|
||||
logger,
|
||||
authors: {
|
||||
include = [],
|
||||
exclude = [],
|
||||
} = {},
|
||||
subredditName,
|
||||
} = options;
|
||||
this.name = name;
|
||||
this.cache = CacheManager.get(subredditName);
|
||||
|
||||
this.authors = {
|
||||
exclude: exclude.map(x => new Author(x)),
|
||||
@@ -54,38 +57,31 @@ export abstract class Rule implements IRule, Triggerable {
|
||||
}
|
||||
|
||||
const ruleUniqueName = this.name === undefined ? this.getKind() : `${this.getKind()} - ${this.name}`;
|
||||
if (logger === undefined) {
|
||||
const prefix = `${loggerPrefix}|${ruleUniqueName}`;
|
||||
this.logger = createLabelledLogger(prefix, prefix);
|
||||
} else {
|
||||
this.logger = logger.child(loggerMetaShuffle(logger, undefined, [ruleUniqueName], {truncateLength: 100}));
|
||||
}
|
||||
this.logger = logger.child({labels: ['Rule',`${ruleUniqueName}`]}, mergeArr);
|
||||
}
|
||||
|
||||
async run(item: Comment | Submission, existingResults: RuleResult[] = []): Promise<[(boolean | null), RuleResult[]]> {
|
||||
this.logger = this.logger.child(loggerMetaShuffle(this.logger, `${item instanceof Submission ? 'SUB' : 'COMM'} ${item.id}`), mergeArr);
|
||||
this.logger.debug('Starting');
|
||||
const existingResult = findResultByPremise(this.getPremise(), existingResults);
|
||||
if (existingResult) {
|
||||
this.logger.debug('Returning existing result');
|
||||
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 testAuthorCriteria(item, auth)) {
|
||||
if (await this.cache.testAuthorCriteria(item, auth)) {
|
||||
return this.process(item);
|
||||
}
|
||||
}
|
||||
this.logger.debug('Inclusive author criteria not matched, rule running skipped');
|
||||
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'})]]);
|
||||
}
|
||||
if (this.authors.exclude !== undefined && this.authors.exclude.length > 0) {
|
||||
for (const auth of this.authors.exclude) {
|
||||
if (await testAuthorCriteria(item, auth, false)) {
|
||||
if (await this.cache.testAuthorCriteria(item, auth, false)) {
|
||||
return this.process(item);
|
||||
}
|
||||
}
|
||||
this.logger.debug('Exclusive author criteria not matched, rule running skipped');
|
||||
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 this.process(item);
|
||||
@@ -136,6 +132,7 @@ export class Author implements AuthorCriteria {
|
||||
* 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
|
||||
* */
|
||||
export interface AuthorOptions {
|
||||
/**
|
||||
@@ -199,6 +196,6 @@ export interface RuleJSONConfig extends IRule {
|
||||
/**
|
||||
* The kind of rule to run
|
||||
*/
|
||||
kind: 'recentActivity' | 'repeatActivity' | 'author' | 'attribution'
|
||||
kind: 'recentActivity' | 'repeatActivity' | 'author' | 'attribution' | 'history'
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"properties": {
|
||||
"dryRun": {
|
||||
"default": false,
|
||||
"description": "If `true` the Action will not make the API request to Reddit to perform its action.",
|
||||
"type": "boolean"
|
||||
},
|
||||
"kind": {
|
||||
"description": "The type of action that will be performed",
|
||||
"enum": [
|
||||
@@ -18,10 +23,6 @@
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"kind",
|
||||
"name"
|
||||
],
|
||||
"required": [
|
||||
"kind"
|
||||
],
|
||||
|
||||
@@ -28,17 +28,13 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"count",
|
||||
"duration"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"AttributionCriteria": {
|
||||
"properties": {
|
||||
"minActivityCount": {
|
||||
"default": 5,
|
||||
"description": "The minimum number of activities (activities defined in `includeInTotal`) that must exist for this criteria to run",
|
||||
"description": "The minimum number of activities that must exist for this criteria to run",
|
||||
"type": "number"
|
||||
},
|
||||
"name": {
|
||||
@@ -78,13 +74,6 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"threshold",
|
||||
"window",
|
||||
"thresholdOn",
|
||||
"minActivityCount",
|
||||
"name"
|
||||
],
|
||||
"required": [
|
||||
"threshold",
|
||||
"window"
|
||||
@@ -100,10 +89,10 @@
|
||||
"type": "boolean"
|
||||
},
|
||||
"authors": {
|
||||
"$ref": "#/definitions/AuthorOptions",
|
||||
"additionalProperties": false,
|
||||
"description": "If present then these Author criteria are checked before running the rule. If criteria fails then the rule is skipped.",
|
||||
"minProperties": 1
|
||||
"minProperties": 1,
|
||||
"type": "object"
|
||||
},
|
||||
"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```",
|
||||
@@ -177,19 +166,6 @@
|
||||
"type": "boolean"
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"kind",
|
||||
"criteria",
|
||||
"criteriaJoin",
|
||||
"include",
|
||||
"exclude",
|
||||
"lookAt",
|
||||
"aggregateMediaDomains",
|
||||
"includeSelf",
|
||||
"useSubmissionAsReference",
|
||||
"name",
|
||||
"authors"
|
||||
],
|
||||
"required": [
|
||||
"kind"
|
||||
],
|
||||
@@ -230,47 +206,15 @@
|
||||
"type": "array"
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"name",
|
||||
"flairCssClass",
|
||||
"flairText",
|
||||
"isMod"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"AuthorOptions": {
|
||||
"additionalProperties": false,
|
||||
"description": "If present then these Author criteria are checked before running the rule. If criteria fails then the rule is skipped.",
|
||||
"minProperties": 1,
|
||||
"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"
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"include",
|
||||
"exclude"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"AuthorRuleJSONConfig": {
|
||||
"properties": {
|
||||
"authors": {
|
||||
"$ref": "#/definitions/AuthorOptions",
|
||||
"additionalProperties": false,
|
||||
"description": "If present then these Author criteria are checked before running the rule. If criteria fails then the rule is skipped.",
|
||||
"minProperties": 1
|
||||
"minProperties": 1,
|
||||
"type": "object"
|
||||
},
|
||||
"exclude": {
|
||||
"description": "Only runs if include is not present. Will \"pass\" if any of set of the AuthorCriteria does not pass",
|
||||
@@ -299,13 +243,6 @@
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"kind",
|
||||
"include",
|
||||
"exclude",
|
||||
"name",
|
||||
"authors"
|
||||
],
|
||||
"required": [
|
||||
"exclude",
|
||||
"include",
|
||||
@@ -354,6 +291,11 @@
|
||||
"description": {
|
||||
"type": "string"
|
||||
},
|
||||
"dryRun": {
|
||||
"default": "undefined",
|
||||
"description": "Use this option to override the `dryRun` setting for all of its `Actions`",
|
||||
"type": "boolean"
|
||||
},
|
||||
"kind": {
|
||||
"description": "The type of event (new submission or new comment) this check should be run against",
|
||||
"enum": [
|
||||
@@ -383,6 +325,9 @@
|
||||
{
|
||||
"$ref": "#/definitions/AttributionJSONConfig"
|
||||
},
|
||||
{
|
||||
"$ref": "#/definitions/HistoryJSONConfig"
|
||||
},
|
||||
{
|
||||
"$ref": "#/definitions/RuleSetJson"
|
||||
},
|
||||
@@ -395,14 +340,6 @@
|
||||
"type": "array"
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"kind",
|
||||
"rules",
|
||||
"actions",
|
||||
"name",
|
||||
"description",
|
||||
"condition"
|
||||
],
|
||||
"required": [
|
||||
"actions",
|
||||
"kind",
|
||||
@@ -427,6 +364,11 @@
|
||||
"description": "Distinguish the comment after creation?",
|
||||
"type": "boolean"
|
||||
},
|
||||
"dryRun": {
|
||||
"default": false,
|
||||
"description": "If `true` the Action will not make the API request to Reddit to perform its action.",
|
||||
"type": "boolean"
|
||||
},
|
||||
"kind": {
|
||||
"description": "The type of action that will be performed",
|
||||
"enum": [
|
||||
@@ -452,20 +394,42 @@
|
||||
"type": "boolean"
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"lock",
|
||||
"sticky",
|
||||
"distinguish",
|
||||
"content",
|
||||
"kind",
|
||||
"name"
|
||||
],
|
||||
"required": [
|
||||
"content",
|
||||
"kind"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"CommentThresholdCriteria": {
|
||||
"properties": {
|
||||
"asOp": {
|
||||
"description": "If `true` then when threshold...\n\n* is `number` it will be number of comments where author is OP\n* is `percent` it will be **percent of total comments where author is OP**",
|
||||
"type": "boolean"
|
||||
},
|
||||
"condition": {
|
||||
"enum": [
|
||||
"<",
|
||||
"<=",
|
||||
">",
|
||||
">="
|
||||
],
|
||||
"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",
|
||||
"type": [
|
||||
"string",
|
||||
"number"
|
||||
]
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"condition",
|
||||
"threshold"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"DurationObject": {
|
||||
"additionalProperties": false,
|
||||
"description": "A Day.js duration object\n\nhttps://day.js.org/docs/en/durations/creating",
|
||||
@@ -493,15 +457,6 @@
|
||||
"type": "number"
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"seconds",
|
||||
"minutes",
|
||||
"hours",
|
||||
"days",
|
||||
"weeks",
|
||||
"months",
|
||||
"years"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"FlairActionJson": {
|
||||
@@ -511,6 +466,11 @@
|
||||
"description": "The text of the css class of the flair to apply",
|
||||
"type": "string"
|
||||
},
|
||||
"dryRun": {
|
||||
"default": false,
|
||||
"description": "If `true` the Action will not make the API request to Reddit to perform its action.",
|
||||
"type": "boolean"
|
||||
},
|
||||
"kind": {
|
||||
"description": "The type of action that will be performed",
|
||||
"enum": [
|
||||
@@ -532,13 +492,115 @@
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"text",
|
||||
"css",
|
||||
"name",
|
||||
"required": [
|
||||
"kind"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"HistoryCriteria": {
|
||||
"description": "If both `submission` and `comment` are defined then criteria will only trigger if BOTH thresholds are met",
|
||||
"properties": {
|
||||
"comment": {
|
||||
"$ref": "#/definitions/CommentThresholdCriteria"
|
||||
},
|
||||
"minActivityCount": {
|
||||
"default": 5,
|
||||
"description": "The minimum number of activities that must exist from the `window` results for this criteria to run",
|
||||
"type": "number"
|
||||
},
|
||||
"name": {
|
||||
"type": "string"
|
||||
},
|
||||
"submission": {
|
||||
"$ref": "#/definitions/ThresholdCriteria"
|
||||
},
|
||||
"window": {
|
||||
"anyOf": [
|
||||
{
|
||||
"$ref": "#/definitions/DurationObject"
|
||||
},
|
||||
{
|
||||
"$ref": "#/definitions/ActivityWindowCriteria"
|
||||
},
|
||||
{
|
||||
"type": [
|
||||
"string",
|
||||
"number"
|
||||
]
|
||||
}
|
||||
],
|
||||
"description": "Window defining Activities to consider (both Comment/Submission)"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"window"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"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,
|
||||
"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"
|
||||
},
|
||||
"criteria": {
|
||||
"description": "A list threshold-window values to test activities against.",
|
||||
"items": {
|
||||
"$ref": "#/definitions/HistoryCriteria"
|
||||
},
|
||||
"minItems": 1,
|
||||
"type": "array"
|
||||
},
|
||||
"criteriaJoin": {
|
||||
"description": "* If `OR` then any set of Criteria that pass will trigger the Rule\n* If `AND` then all Criteria sets must pass to trigger the Rule",
|
||||
"enum": [
|
||||
"AND",
|
||||
"OR"
|
||||
],
|
||||
"type": "string"
|
||||
},
|
||||
"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\"]",
|
||||
"examples": [
|
||||
"mealtimevideos",
|
||||
"askscience"
|
||||
],
|
||||
"items": {
|
||||
"type": "string"
|
||||
},
|
||||
"minItems": 1,
|
||||
"type": "array"
|
||||
},
|
||||
"include": {
|
||||
"description": "Only 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\"]",
|
||||
"examples": [
|
||||
"mealtimevideos",
|
||||
"askscience"
|
||||
],
|
||||
"items": {
|
||||
"type": "string"
|
||||
},
|
||||
"minItems": 1,
|
||||
"type": "array"
|
||||
},
|
||||
"kind": {
|
||||
"description": "The kind of rule to run",
|
||||
"enum": [
|
||||
"history"
|
||||
],
|
||||
"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.",
|
||||
"pattern": "^[a-zA-Z]([\\w -]*[\\w])?$",
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"criteria",
|
||||
"kind"
|
||||
],
|
||||
"type": "object"
|
||||
@@ -546,6 +608,11 @@
|
||||
"LockActionJson": {
|
||||
"description": "Lock the Activity",
|
||||
"properties": {
|
||||
"dryRun": {
|
||||
"default": false,
|
||||
"description": "If `true` the Action will not make the API request to Reddit to perform its action.",
|
||||
"type": "boolean"
|
||||
},
|
||||
"kind": {
|
||||
"description": "The type of action that will be performed",
|
||||
"enum": [
|
||||
@@ -563,10 +630,6 @@
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"name",
|
||||
"kind"
|
||||
],
|
||||
"required": [
|
||||
"kind"
|
||||
],
|
||||
@@ -589,10 +652,6 @@
|
||||
"type": "number"
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"limit",
|
||||
"interval"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"submissions": {
|
||||
@@ -609,27 +668,19 @@
|
||||
"type": "number"
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"limit",
|
||||
"interval"
|
||||
],
|
||||
"type": "object"
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"submissions",
|
||||
"comments"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"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": {
|
||||
"$ref": "#/definitions/AuthorOptions",
|
||||
"additionalProperties": false,
|
||||
"description": "If present then these Author criteria are checked before running the rule. If criteria fails then the rule is skipped.",
|
||||
"minProperties": 1
|
||||
"minProperties": 1,
|
||||
"type": "object"
|
||||
},
|
||||
"kind": {
|
||||
"description": "The kind of rule to run",
|
||||
@@ -701,15 +752,6 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"kind",
|
||||
"lookAt",
|
||||
"thresholds",
|
||||
"window",
|
||||
"useSubmissionAsReference",
|
||||
"name",
|
||||
"authors"
|
||||
],
|
||||
"required": [
|
||||
"kind",
|
||||
"thresholds"
|
||||
@@ -719,6 +761,11 @@
|
||||
"RemoveActionJson": {
|
||||
"description": "Remove the Activity",
|
||||
"properties": {
|
||||
"dryRun": {
|
||||
"default": false,
|
||||
"description": "If `true` the Action will not make the API request to Reddit to perform its action.",
|
||||
"type": "boolean"
|
||||
},
|
||||
"kind": {
|
||||
"description": "The type of action that will be performed",
|
||||
"enum": [
|
||||
@@ -736,10 +783,6 @@
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"name",
|
||||
"kind"
|
||||
],
|
||||
"required": [
|
||||
"kind"
|
||||
],
|
||||
@@ -749,10 +792,10 @@
|
||||
"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": {
|
||||
"$ref": "#/definitions/AuthorOptions",
|
||||
"additionalProperties": false,
|
||||
"description": "If present then these Author criteria are checked before running the rule. If criteria fails then the rule is skipped.",
|
||||
"minProperties": 1
|
||||
"minProperties": 1,
|
||||
"type": "object"
|
||||
},
|
||||
"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\"]",
|
||||
@@ -850,18 +893,6 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"kind",
|
||||
"threshold",
|
||||
"gapAllowance",
|
||||
"include",
|
||||
"exclude",
|
||||
"lookAt",
|
||||
"window",
|
||||
"useSubmissionAsReference",
|
||||
"name",
|
||||
"authors"
|
||||
],
|
||||
"required": [
|
||||
"kind"
|
||||
],
|
||||
@@ -874,6 +905,11 @@
|
||||
"description": "The text of the report. If longer than 100 characters will be truncated to \"[content]...\"",
|
||||
"type": "string"
|
||||
},
|
||||
"dryRun": {
|
||||
"default": false,
|
||||
"description": "If `true` the Action will not make the API request to Reddit to perform its action.",
|
||||
"type": "boolean"
|
||||
},
|
||||
"kind": {
|
||||
"description": "The type of action that will be performed",
|
||||
"enum": [
|
||||
@@ -891,11 +927,6 @@
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"content",
|
||||
"kind",
|
||||
"name"
|
||||
],
|
||||
"required": [
|
||||
"content",
|
||||
"kind"
|
||||
@@ -930,6 +961,9 @@
|
||||
{
|
||||
"$ref": "#/definitions/AttributionJSONConfig"
|
||||
},
|
||||
{
|
||||
"$ref": "#/definitions/HistoryJSONConfig"
|
||||
},
|
||||
{
|
||||
"type": "string"
|
||||
}
|
||||
@@ -939,19 +973,17 @@
|
||||
"type": "array"
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"rules",
|
||||
"condition"
|
||||
],
|
||||
"required": [
|
||||
"rules"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"SubThreshold": {
|
||||
"additionalProperties": false,
|
||||
"description": "At least one count property must be present. If both are present then either can trigger the rule",
|
||||
"minProperties": 1,
|
||||
"properties": {
|
||||
"count": {
|
||||
"default": 1,
|
||||
"description": "The number of activities in each subreddit from the list that will trigger this rule",
|
||||
"minimum": 1,
|
||||
"type": "number"
|
||||
@@ -967,23 +999,70 @@
|
||||
},
|
||||
"minItems": 1,
|
||||
"type": "array"
|
||||
},
|
||||
"totalCount": {
|
||||
"description": "The total number of activities across all listed subreddits that will trigger this rule",
|
||||
"minimum": 1,
|
||||
"type": "number"
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"count",
|
||||
"subreddits"
|
||||
],
|
||||
"required": [
|
||||
"subreddits"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"SubredditCacheConfig": {
|
||||
"properties": {
|
||||
"authorTTL": {
|
||||
"description": "Amount of time, in milliseconds, author activities (Comments/Submission) should be cached",
|
||||
"type": "number"
|
||||
},
|
||||
"wikiTTL": {
|
||||
"type": "number"
|
||||
}
|
||||
},
|
||||
"type": "object"
|
||||
},
|
||||
"ThresholdCriteria": {
|
||||
"properties": {
|
||||
"condition": {
|
||||
"enum": [
|
||||
"<",
|
||||
"<=",
|
||||
">",
|
||||
">="
|
||||
],
|
||||
"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",
|
||||
"type": [
|
||||
"string",
|
||||
"number"
|
||||
]
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"condition",
|
||||
"threshold"
|
||||
],
|
||||
"type": "object"
|
||||
}
|
||||
},
|
||||
"properties": {
|
||||
"apiLimitWarning": {
|
||||
"default": 250,
|
||||
"description": "When Reddit API limit remaining reaches this number context bot will start warning on every poll interval",
|
||||
"type": "number"
|
||||
"caching": {
|
||||
"anyOf": [
|
||||
{
|
||||
"$ref": "#/definitions/SubredditCacheConfig"
|
||||
},
|
||||
{
|
||||
"enum": [
|
||||
false
|
||||
],
|
||||
"type": "boolean"
|
||||
}
|
||||
]
|
||||
},
|
||||
"checks": {
|
||||
"description": "A list of all the checks that should be run for a subreddit.\n\nChecks are split into two lists -- submission or comment -- based on kind and run independently.\n\nChecks in each list are run in the order found in the configuration.\n\nWhen a check \"passes\", and actions are performed, then all subsequent checks are skipped.",
|
||||
@@ -993,21 +1072,16 @@
|
||||
"minItems": 1,
|
||||
"type": "array"
|
||||
},
|
||||
"heartbeatInterval": {
|
||||
"description": "If present, time in milliseconds between HEARTBEAT log statements with current api limit count. Nice to have to know things are still ticking if there is low activity",
|
||||
"type": "number"
|
||||
"dryRun": {
|
||||
"default": "undefined",
|
||||
"description": "Use this option to override the `dryRun` setting for all `Checks`",
|
||||
"type": "boolean"
|
||||
},
|
||||
"polling": {
|
||||
"$ref": "#/definitions/PollingOptions",
|
||||
"description": "You may specify polling options independently for submissions/comments"
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"checks",
|
||||
"polling",
|
||||
"heartbeatInterval",
|
||||
"apiLimitWarning"
|
||||
],
|
||||
"required": [
|
||||
"checks"
|
||||
],
|
||||
|
||||
@@ -13,6 +13,9 @@
|
||||
{
|
||||
"$ref": "#/definitions/AttributionJSONConfig"
|
||||
},
|
||||
{
|
||||
"$ref": "#/definitions/HistoryJSONConfig"
|
||||
},
|
||||
{
|
||||
"type": "string"
|
||||
}
|
||||
@@ -45,17 +48,13 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"count",
|
||||
"duration"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"AttributionCriteria": {
|
||||
"properties": {
|
||||
"minActivityCount": {
|
||||
"default": 5,
|
||||
"description": "The minimum number of activities (activities defined in `includeInTotal`) that must exist for this criteria to run",
|
||||
"description": "The minimum number of activities that must exist for this criteria to run",
|
||||
"type": "number"
|
||||
},
|
||||
"name": {
|
||||
@@ -95,13 +94,6 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"threshold",
|
||||
"window",
|
||||
"thresholdOn",
|
||||
"minActivityCount",
|
||||
"name"
|
||||
],
|
||||
"required": [
|
||||
"threshold",
|
||||
"window"
|
||||
@@ -117,10 +109,10 @@
|
||||
"type": "boolean"
|
||||
},
|
||||
"authors": {
|
||||
"$ref": "#/definitions/AuthorOptions",
|
||||
"additionalProperties": false,
|
||||
"description": "If present then these Author criteria are checked before running the rule. If criteria fails then the rule is skipped.",
|
||||
"minProperties": 1
|
||||
"minProperties": 1,
|
||||
"type": "object"
|
||||
},
|
||||
"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```",
|
||||
@@ -194,19 +186,6 @@
|
||||
"type": "boolean"
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"kind",
|
||||
"criteria",
|
||||
"criteriaJoin",
|
||||
"include",
|
||||
"exclude",
|
||||
"lookAt",
|
||||
"aggregateMediaDomains",
|
||||
"includeSelf",
|
||||
"useSubmissionAsReference",
|
||||
"name",
|
||||
"authors"
|
||||
],
|
||||
"required": [
|
||||
"kind"
|
||||
],
|
||||
@@ -247,47 +226,15 @@
|
||||
"type": "array"
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"name",
|
||||
"flairCssClass",
|
||||
"flairText",
|
||||
"isMod"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"AuthorOptions": {
|
||||
"additionalProperties": false,
|
||||
"description": "If present then these Author criteria are checked before running the rule. If criteria fails then the rule is skipped.",
|
||||
"minProperties": 1,
|
||||
"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"
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"include",
|
||||
"exclude"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"AuthorRuleJSONConfig": {
|
||||
"properties": {
|
||||
"authors": {
|
||||
"$ref": "#/definitions/AuthorOptions",
|
||||
"additionalProperties": false,
|
||||
"description": "If present then these Author criteria are checked before running the rule. If criteria fails then the rule is skipped.",
|
||||
"minProperties": 1
|
||||
"minProperties": 1,
|
||||
"type": "object"
|
||||
},
|
||||
"exclude": {
|
||||
"description": "Only runs if include is not present. Will \"pass\" if any of set of the AuthorCriteria does not pass",
|
||||
@@ -316,13 +263,6 @@
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"kind",
|
||||
"include",
|
||||
"exclude",
|
||||
"name",
|
||||
"authors"
|
||||
],
|
||||
"required": [
|
||||
"exclude",
|
||||
"include",
|
||||
@@ -330,6 +270,36 @@
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"CommentThresholdCriteria": {
|
||||
"properties": {
|
||||
"asOp": {
|
||||
"description": "If `true` then when threshold...\n\n* is `number` it will be number of comments where author is OP\n* is `percent` it will be **percent of total comments where author is OP**",
|
||||
"type": "boolean"
|
||||
},
|
||||
"condition": {
|
||||
"enum": [
|
||||
"<",
|
||||
"<=",
|
||||
">",
|
||||
">="
|
||||
],
|
||||
"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",
|
||||
"type": [
|
||||
"string",
|
||||
"number"
|
||||
]
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"condition",
|
||||
"threshold"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"DurationObject": {
|
||||
"additionalProperties": false,
|
||||
"description": "A Day.js duration object\n\nhttps://day.js.org/docs/en/durations/creating",
|
||||
@@ -357,14 +327,113 @@
|
||||
"type": "number"
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"seconds",
|
||||
"minutes",
|
||||
"hours",
|
||||
"days",
|
||||
"weeks",
|
||||
"months",
|
||||
"years"
|
||||
"type": "object"
|
||||
},
|
||||
"HistoryCriteria": {
|
||||
"description": "If both `submission` and `comment` are defined then criteria will only trigger if BOTH thresholds are met",
|
||||
"properties": {
|
||||
"comment": {
|
||||
"$ref": "#/definitions/CommentThresholdCriteria"
|
||||
},
|
||||
"minActivityCount": {
|
||||
"default": 5,
|
||||
"description": "The minimum number of activities that must exist from the `window` results for this criteria to run",
|
||||
"type": "number"
|
||||
},
|
||||
"name": {
|
||||
"type": "string"
|
||||
},
|
||||
"submission": {
|
||||
"$ref": "#/definitions/ThresholdCriteria"
|
||||
},
|
||||
"window": {
|
||||
"anyOf": [
|
||||
{
|
||||
"$ref": "#/definitions/DurationObject"
|
||||
},
|
||||
{
|
||||
"$ref": "#/definitions/ActivityWindowCriteria"
|
||||
},
|
||||
{
|
||||
"type": [
|
||||
"string",
|
||||
"number"
|
||||
]
|
||||
}
|
||||
],
|
||||
"description": "Window defining Activities to consider (both Comment/Submission)"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"window"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"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,
|
||||
"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"
|
||||
},
|
||||
"criteria": {
|
||||
"description": "A list threshold-window values to test activities against.",
|
||||
"items": {
|
||||
"$ref": "#/definitions/HistoryCriteria"
|
||||
},
|
||||
"minItems": 1,
|
||||
"type": "array"
|
||||
},
|
||||
"criteriaJoin": {
|
||||
"description": "* If `OR` then any set of Criteria that pass will trigger the Rule\n* If `AND` then all Criteria sets must pass to trigger the Rule",
|
||||
"enum": [
|
||||
"AND",
|
||||
"OR"
|
||||
],
|
||||
"type": "string"
|
||||
},
|
||||
"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\"]",
|
||||
"examples": [
|
||||
"mealtimevideos",
|
||||
"askscience"
|
||||
],
|
||||
"items": {
|
||||
"type": "string"
|
||||
},
|
||||
"minItems": 1,
|
||||
"type": "array"
|
||||
},
|
||||
"include": {
|
||||
"description": "Only 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\"]",
|
||||
"examples": [
|
||||
"mealtimevideos",
|
||||
"askscience"
|
||||
],
|
||||
"items": {
|
||||
"type": "string"
|
||||
},
|
||||
"minItems": 1,
|
||||
"type": "array"
|
||||
},
|
||||
"kind": {
|
||||
"description": "The kind of rule to run",
|
||||
"enum": [
|
||||
"history"
|
||||
],
|
||||
"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.",
|
||||
"pattern": "^[a-zA-Z]([\\w -]*[\\w])?$",
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"criteria",
|
||||
"kind"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
@@ -372,10 +441,10 @@
|
||||
"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": {
|
||||
"$ref": "#/definitions/AuthorOptions",
|
||||
"additionalProperties": false,
|
||||
"description": "If present then these Author criteria are checked before running the rule. If criteria fails then the rule is skipped.",
|
||||
"minProperties": 1
|
||||
"minProperties": 1,
|
||||
"type": "object"
|
||||
},
|
||||
"kind": {
|
||||
"description": "The kind of rule to run",
|
||||
@@ -447,15 +516,6 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"kind",
|
||||
"lookAt",
|
||||
"thresholds",
|
||||
"window",
|
||||
"useSubmissionAsReference",
|
||||
"name",
|
||||
"authors"
|
||||
],
|
||||
"required": [
|
||||
"kind",
|
||||
"thresholds"
|
||||
@@ -466,10 +526,10 @@
|
||||
"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": {
|
||||
"$ref": "#/definitions/AuthorOptions",
|
||||
"additionalProperties": false,
|
||||
"description": "If present then these Author criteria are checked before running the rule. If criteria fails then the rule is skipped.",
|
||||
"minProperties": 1
|
||||
"minProperties": 1,
|
||||
"type": "object"
|
||||
},
|
||||
"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\"]",
|
||||
@@ -567,27 +627,17 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"kind",
|
||||
"threshold",
|
||||
"gapAllowance",
|
||||
"include",
|
||||
"exclude",
|
||||
"lookAt",
|
||||
"window",
|
||||
"useSubmissionAsReference",
|
||||
"name",
|
||||
"authors"
|
||||
],
|
||||
"required": [
|
||||
"kind"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"SubThreshold": {
|
||||
"additionalProperties": false,
|
||||
"description": "At least one count property must be present. If both are present then either can trigger the rule",
|
||||
"minProperties": 1,
|
||||
"properties": {
|
||||
"count": {
|
||||
"default": 1,
|
||||
"description": "The number of activities in each subreddit from the list that will trigger this rule",
|
||||
"minimum": 1,
|
||||
"type": "number"
|
||||
@@ -603,16 +653,43 @@
|
||||
},
|
||||
"minItems": 1,
|
||||
"type": "array"
|
||||
},
|
||||
"totalCount": {
|
||||
"description": "The total number of activities across all listed subreddits that will trigger this rule",
|
||||
"minimum": 1,
|
||||
"type": "number"
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"count",
|
||||
"subreddits"
|
||||
],
|
||||
"required": [
|
||||
"subreddits"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"ThresholdCriteria": {
|
||||
"properties": {
|
||||
"condition": {
|
||||
"enum": [
|
||||
"<",
|
||||
"<=",
|
||||
">",
|
||||
">="
|
||||
],
|
||||
"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",
|
||||
"type": [
|
||||
"string",
|
||||
"number"
|
||||
]
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"condition",
|
||||
"threshold"
|
||||
],
|
||||
"type": "object"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,17 +28,13 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"count",
|
||||
"duration"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"AttributionCriteria": {
|
||||
"properties": {
|
||||
"minActivityCount": {
|
||||
"default": 5,
|
||||
"description": "The minimum number of activities (activities defined in `includeInTotal`) that must exist for this criteria to run",
|
||||
"description": "The minimum number of activities that must exist for this criteria to run",
|
||||
"type": "number"
|
||||
},
|
||||
"name": {
|
||||
@@ -78,13 +74,6 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"threshold",
|
||||
"window",
|
||||
"thresholdOn",
|
||||
"minActivityCount",
|
||||
"name"
|
||||
],
|
||||
"required": [
|
||||
"threshold",
|
||||
"window"
|
||||
@@ -100,10 +89,10 @@
|
||||
"type": "boolean"
|
||||
},
|
||||
"authors": {
|
||||
"$ref": "#/definitions/AuthorOptions",
|
||||
"additionalProperties": false,
|
||||
"description": "If present then these Author criteria are checked before running the rule. If criteria fails then the rule is skipped.",
|
||||
"minProperties": 1
|
||||
"minProperties": 1,
|
||||
"type": "object"
|
||||
},
|
||||
"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```",
|
||||
@@ -177,19 +166,6 @@
|
||||
"type": "boolean"
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"kind",
|
||||
"criteria",
|
||||
"criteriaJoin",
|
||||
"include",
|
||||
"exclude",
|
||||
"lookAt",
|
||||
"aggregateMediaDomains",
|
||||
"includeSelf",
|
||||
"useSubmissionAsReference",
|
||||
"name",
|
||||
"authors"
|
||||
],
|
||||
"required": [
|
||||
"kind"
|
||||
],
|
||||
@@ -230,47 +206,15 @@
|
||||
"type": "array"
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"name",
|
||||
"flairCssClass",
|
||||
"flairText",
|
||||
"isMod"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"AuthorOptions": {
|
||||
"additionalProperties": false,
|
||||
"description": "If present then these Author criteria are checked before running the rule. If criteria fails then the rule is skipped.",
|
||||
"minProperties": 1,
|
||||
"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"
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"include",
|
||||
"exclude"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"AuthorRuleJSONConfig": {
|
||||
"properties": {
|
||||
"authors": {
|
||||
"$ref": "#/definitions/AuthorOptions",
|
||||
"additionalProperties": false,
|
||||
"description": "If present then these Author criteria are checked before running the rule. If criteria fails then the rule is skipped.",
|
||||
"minProperties": 1
|
||||
"minProperties": 1,
|
||||
"type": "object"
|
||||
},
|
||||
"exclude": {
|
||||
"description": "Only runs if include is not present. Will \"pass\" if any of set of the AuthorCriteria does not pass",
|
||||
@@ -299,13 +243,6 @@
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"kind",
|
||||
"include",
|
||||
"exclude",
|
||||
"name",
|
||||
"authors"
|
||||
],
|
||||
"required": [
|
||||
"exclude",
|
||||
"include",
|
||||
@@ -313,6 +250,36 @@
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"CommentThresholdCriteria": {
|
||||
"properties": {
|
||||
"asOp": {
|
||||
"description": "If `true` then when threshold...\n\n* is `number` it will be number of comments where author is OP\n* is `percent` it will be **percent of total comments where author is OP**",
|
||||
"type": "boolean"
|
||||
},
|
||||
"condition": {
|
||||
"enum": [
|
||||
"<",
|
||||
"<=",
|
||||
">",
|
||||
">="
|
||||
],
|
||||
"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",
|
||||
"type": [
|
||||
"string",
|
||||
"number"
|
||||
]
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"condition",
|
||||
"threshold"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"DurationObject": {
|
||||
"additionalProperties": false,
|
||||
"description": "A Day.js duration object\n\nhttps://day.js.org/docs/en/durations/creating",
|
||||
@@ -340,14 +307,113 @@
|
||||
"type": "number"
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"seconds",
|
||||
"minutes",
|
||||
"hours",
|
||||
"days",
|
||||
"weeks",
|
||||
"months",
|
||||
"years"
|
||||
"type": "object"
|
||||
},
|
||||
"HistoryCriteria": {
|
||||
"description": "If both `submission` and `comment` are defined then criteria will only trigger if BOTH thresholds are met",
|
||||
"properties": {
|
||||
"comment": {
|
||||
"$ref": "#/definitions/CommentThresholdCriteria"
|
||||
},
|
||||
"minActivityCount": {
|
||||
"default": 5,
|
||||
"description": "The minimum number of activities that must exist from the `window` results for this criteria to run",
|
||||
"type": "number"
|
||||
},
|
||||
"name": {
|
||||
"type": "string"
|
||||
},
|
||||
"submission": {
|
||||
"$ref": "#/definitions/ThresholdCriteria"
|
||||
},
|
||||
"window": {
|
||||
"anyOf": [
|
||||
{
|
||||
"$ref": "#/definitions/DurationObject"
|
||||
},
|
||||
{
|
||||
"$ref": "#/definitions/ActivityWindowCriteria"
|
||||
},
|
||||
{
|
||||
"type": [
|
||||
"string",
|
||||
"number"
|
||||
]
|
||||
}
|
||||
],
|
||||
"description": "Window defining Activities to consider (both Comment/Submission)"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"window"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"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,
|
||||
"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"
|
||||
},
|
||||
"criteria": {
|
||||
"description": "A list threshold-window values to test activities against.",
|
||||
"items": {
|
||||
"$ref": "#/definitions/HistoryCriteria"
|
||||
},
|
||||
"minItems": 1,
|
||||
"type": "array"
|
||||
},
|
||||
"criteriaJoin": {
|
||||
"description": "* If `OR` then any set of Criteria that pass will trigger the Rule\n* If `AND` then all Criteria sets must pass to trigger the Rule",
|
||||
"enum": [
|
||||
"AND",
|
||||
"OR"
|
||||
],
|
||||
"type": "string"
|
||||
},
|
||||
"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\"]",
|
||||
"examples": [
|
||||
"mealtimevideos",
|
||||
"askscience"
|
||||
],
|
||||
"items": {
|
||||
"type": "string"
|
||||
},
|
||||
"minItems": 1,
|
||||
"type": "array"
|
||||
},
|
||||
"include": {
|
||||
"description": "Only 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\"]",
|
||||
"examples": [
|
||||
"mealtimevideos",
|
||||
"askscience"
|
||||
],
|
||||
"items": {
|
||||
"type": "string"
|
||||
},
|
||||
"minItems": 1,
|
||||
"type": "array"
|
||||
},
|
||||
"kind": {
|
||||
"description": "The kind of rule to run",
|
||||
"enum": [
|
||||
"history"
|
||||
],
|
||||
"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.",
|
||||
"pattern": "^[a-zA-Z]([\\w -]*[\\w])?$",
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"criteria",
|
||||
"kind"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
@@ -355,10 +421,10 @@
|
||||
"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": {
|
||||
"$ref": "#/definitions/AuthorOptions",
|
||||
"additionalProperties": false,
|
||||
"description": "If present then these Author criteria are checked before running the rule. If criteria fails then the rule is skipped.",
|
||||
"minProperties": 1
|
||||
"minProperties": 1,
|
||||
"type": "object"
|
||||
},
|
||||
"kind": {
|
||||
"description": "The kind of rule to run",
|
||||
@@ -430,15 +496,6 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"kind",
|
||||
"lookAt",
|
||||
"thresholds",
|
||||
"window",
|
||||
"useSubmissionAsReference",
|
||||
"name",
|
||||
"authors"
|
||||
],
|
||||
"required": [
|
||||
"kind",
|
||||
"thresholds"
|
||||
@@ -449,10 +506,10 @@
|
||||
"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": {
|
||||
"$ref": "#/definitions/AuthorOptions",
|
||||
"additionalProperties": false,
|
||||
"description": "If present then these Author criteria are checked before running the rule. If criteria fails then the rule is skipped.",
|
||||
"minProperties": 1
|
||||
"minProperties": 1,
|
||||
"type": "object"
|
||||
},
|
||||
"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\"]",
|
||||
@@ -550,27 +607,17 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"kind",
|
||||
"threshold",
|
||||
"gapAllowance",
|
||||
"include",
|
||||
"exclude",
|
||||
"lookAt",
|
||||
"window",
|
||||
"useSubmissionAsReference",
|
||||
"name",
|
||||
"authors"
|
||||
],
|
||||
"required": [
|
||||
"kind"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"SubThreshold": {
|
||||
"additionalProperties": false,
|
||||
"description": "At least one count property must be present. If both are present then either can trigger the rule",
|
||||
"minProperties": 1,
|
||||
"properties": {
|
||||
"count": {
|
||||
"default": 1,
|
||||
"description": "The number of activities in each subreddit from the list that will trigger this rule",
|
||||
"minimum": 1,
|
||||
"type": "number"
|
||||
@@ -586,16 +633,43 @@
|
||||
},
|
||||
"minItems": 1,
|
||||
"type": "array"
|
||||
},
|
||||
"totalCount": {
|
||||
"description": "The total number of activities across all listed subreddits that will trigger this rule",
|
||||
"minimum": 1,
|
||||
"type": "number"
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"count",
|
||||
"subreddits"
|
||||
],
|
||||
"required": [
|
||||
"subreddits"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"ThresholdCriteria": {
|
||||
"properties": {
|
||||
"condition": {
|
||||
"enum": [
|
||||
"<",
|
||||
"<=",
|
||||
">",
|
||||
">="
|
||||
],
|
||||
"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",
|
||||
"type": [
|
||||
"string",
|
||||
"number"
|
||||
]
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"condition",
|
||||
"threshold"
|
||||
],
|
||||
"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`)",
|
||||
@@ -625,6 +699,9 @@
|
||||
{
|
||||
"$ref": "#/definitions/AttributionJSONConfig"
|
||||
},
|
||||
{
|
||||
"$ref": "#/definitions/HistoryJSONConfig"
|
||||
},
|
||||
{
|
||||
"type": "string"
|
||||
}
|
||||
@@ -634,10 +711,6 @@
|
||||
"type": "array"
|
||||
}
|
||||
},
|
||||
"propertyOrder": [
|
||||
"rules",
|
||||
"condition"
|
||||
],
|
||||
"required": [
|
||||
"rules"
|
||||
],
|
||||
|
||||
@@ -4,7 +4,6 @@ import {SubmissionCheck} from "../Check/SubmissionCheck";
|
||||
import {CommentCheck} from "../Check/CommentCheck";
|
||||
import {
|
||||
determineNewResults,
|
||||
loggerMetaShuffle,
|
||||
mergeArr,
|
||||
} from "../util";
|
||||
import {CommentStream, SubmissionStream} from "snoostorm";
|
||||
@@ -15,6 +14,8 @@ import {ManagerOptions, PollingOptions} from "../Common/interfaces";
|
||||
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";
|
||||
|
||||
export class Manager {
|
||||
subreddit: Subreddit;
|
||||
@@ -28,27 +29,62 @@ export class Manager {
|
||||
streamSub?: SubmissionStream;
|
||||
commentsListedOnce = false;
|
||||
streamComments?: CommentStream;
|
||||
heartbeatInterval?: number;
|
||||
lastHeartbeat = dayjs();
|
||||
apiLimitWarning: number;
|
||||
dryRun?: boolean;
|
||||
|
||||
displayLabel: string;
|
||||
currentLabels?: string[];
|
||||
|
||||
getCurrentLabels = () => {
|
||||
return this.currentLabels;
|
||||
}
|
||||
|
||||
constructor(sub: Subreddit, client: Snoowrap, logger: Logger, sourceData: object, opts: ManagerOptions = {}) {
|
||||
this.logger = logger.child(loggerMetaShuffle(logger, undefined, [`r/${sub.display_name}`], {truncateLength: 40}), mergeArr);
|
||||
const displayLabel = `${sub.display_name_prefixed}`;
|
||||
this.displayLabel = displayLabel;
|
||||
this.currentLabels = [displayLabel];
|
||||
const getLabels = this.getCurrentLabels;
|
||||
// dynamic default meta for winston feasible using function getters
|
||||
// https://github.com/winstonjs/winston/issues/1626#issuecomment-531142958
|
||||
this.logger = logger.child({
|
||||
get labels() {
|
||||
return getLabels()
|
||||
}
|
||||
}, mergeArr);
|
||||
|
||||
const configBuilder = new ConfigBuilder({logger: this.logger});
|
||||
const [subChecks, commentChecks, configManagerOptions] = configBuilder.buildFromJson(sourceData);
|
||||
const {polling = {}, heartbeatInterval, apiLimitWarning = 250} = configManagerOptions || {};
|
||||
const validJson = configBuilder.validateJson(sourceData);
|
||||
const {checks, ...configManagerOpts} = validJson;
|
||||
const {polling = {}, caching, dryRun} = configManagerOpts || {};
|
||||
this.pollOptions = {...polling, ...opts.polling};
|
||||
this.heartbeatInterval = heartbeatInterval;
|
||||
this.apiLimitWarning = apiLimitWarning;
|
||||
this.subreddit = sub;
|
||||
this.client = client;
|
||||
for(const sub of subChecks) {
|
||||
this.logger.info(`Submission Check: ${sub.name}${sub.description !== undefined ? ` ${sub.description}` : ''}`);
|
||||
this.dryRun = opts.dryRun || dryRun;
|
||||
|
||||
const cacheConfig = caching === false ? {enabled: false, logger: this.logger} : {
|
||||
...caching,
|
||||
enabled: true,
|
||||
logger: this.logger
|
||||
};
|
||||
CacheManager.get(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};
|
||||
if (jCheck.kind === 'comment') {
|
||||
commentChecks.push(new CommentCheck(checkConfig));
|
||||
} else if (jCheck.kind === 'submission') {
|
||||
subChecks.push(new SubmissionCheck(checkConfig));
|
||||
}
|
||||
}
|
||||
|
||||
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}` : ''}`);
|
||||
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}`;
|
||||
@@ -64,51 +100,39 @@ export class Manager {
|
||||
const itemId = await item.id;
|
||||
let allRuleResults: RuleResult[] = [];
|
||||
const itemIdentifier = `${checkType} ${itemId}`;
|
||||
this.currentLabels = [this.displayLabel, itemIdentifier];
|
||||
const [peek, _] = await itemContentPeek(item);
|
||||
this.logger.info(`New Event: ${itemIdentifier} => ${peek}`);
|
||||
this.logger.info(`<EVENT> ${peek}`);
|
||||
|
||||
for (const check of checks) {
|
||||
if(checkNames.length > 0 && !checkNames.map(x => x.toLowerCase()).some(x => x === check.name.toLowerCase())) {
|
||||
this.logger.debug(`Check ${check} not in array of requested checks to run, skipping`);
|
||||
continue;
|
||||
}
|
||||
this.logger.debug(`[${itemIdentifier}] Running Check ${check.name}`);
|
||||
let triggered = false;
|
||||
let currentResults: RuleResult[] = [];
|
||||
try {
|
||||
const [checkTriggered, checkResults] = await check.run(item, allRuleResults);
|
||||
currentResults = checkResults;
|
||||
allRuleResults = allRuleResults.concat(determineNewResults(allRuleResults, checkResults));
|
||||
triggered = checkTriggered;
|
||||
const invokedRules = checkResults.map(x => x.name || x.premise.kind).join(' | ');
|
||||
if (checkTriggered) {
|
||||
this.logger.info(`[${itemIdentifier}] [CHK ${check.name}] Triggered with invoked Rules: ${invokedRules}`);
|
||||
} else {
|
||||
this.logger.debug(`[${itemIdentifier}] [CHK ${check.name}] WAS NOT triggered with invoked Rule(s): ${invokedRules}`);
|
||||
try {
|
||||
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;
|
||||
let currentResults: RuleResult[] = [];
|
||||
try {
|
||||
const [checkTriggered, checkResults] = await check.run(item, allRuleResults);
|
||||
currentResults = checkResults;
|
||||
allRuleResults = allRuleResults.concat(determineNewResults(allRuleResults, checkResults));
|
||||
triggered = checkTriggered;
|
||||
} catch (e) {
|
||||
this.logger.warn(`[Check ${check.name}] Failed with error: ${e.message}`, e);
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
this.logger.warn(`[${itemIdentifier}] [CHK ${check.name}] Failed with error: ${e.message}`, e);
|
||||
if (triggered) {
|
||||
await check.runActions(item, currentResults);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (triggered) {
|
||||
this.logger.debug(`[${itemIdentifier}] [CHK ${check.name}] Running actions`);
|
||||
// TODO give actions a name
|
||||
await check.runActions(item, currentResults);
|
||||
this.logger.info(`[${itemIdentifier}] [CHK ${check.name}] Ran actions`);
|
||||
break;
|
||||
} catch (err) {
|
||||
if (!(err instanceof LoggedError)) {
|
||||
this.logger.error('An unhandled error occurred while running checks', err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
heartbeat() {
|
||||
const apiRemaining = this.client.ratelimitRemaining;
|
||||
if(this.heartbeatInterval !== undefined && dayjs().diff(this.lastHeartbeat) >= this.heartbeatInterval) {
|
||||
this.logger.info(`HEARTBEAT -- Reddit API Rate Limit remaining: ${apiRemaining}`);
|
||||
this.lastHeartbeat = dayjs();
|
||||
}
|
||||
if(apiRemaining < this.apiLimitWarning) {
|
||||
this.logger.warn(`Reddit API rate limit remaining: ${apiRemaining} (Warning at ${this.apiLimitWarning})`);
|
||||
} finally {
|
||||
this.currentLabels = [this.displayLabel];
|
||||
this.logger.debug(`Reddit API Rate Limit remaining: ${this.client.ratelimitRemaining}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,8 +153,6 @@ export class Manager {
|
||||
|
||||
this.streamSub.once('listing', async (listing) => {
|
||||
this.subListedOnce = true;
|
||||
// for debugging
|
||||
// await this.runChecks('Submission', listing[0]);
|
||||
});
|
||||
this.streamSub.on('item', async (item) => {
|
||||
if (!this.subListedOnce) {
|
||||
@@ -138,7 +160,7 @@ export class Manager {
|
||||
}
|
||||
await this.runChecks('Submission', item)
|
||||
});
|
||||
this.streamSub.on('listing', (_) => this.heartbeat());
|
||||
//this.streamSub.on('listing', (_) => this.logger.debug('Polled Submissions'));
|
||||
}
|
||||
|
||||
if (this.commentChecks.length > 0) {
|
||||
@@ -160,15 +182,20 @@ export class Manager {
|
||||
}
|
||||
await this.runChecks('Comment', item)
|
||||
});
|
||||
this.streamComments.on('listing', (_) => this.heartbeat());
|
||||
//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!');
|
||||
this.logger.warn('No submission or comment checks to run! Bot will not run.');
|
||||
return;
|
||||
}
|
||||
|
||||
this.logger.info('Bot Stopped');
|
||||
}
|
||||
}
|
||||
|
||||
166
src/Subreddit/SubredditCache.ts
Normal file
166
src/Subreddit/SubredditCache.ts
Normal file
@@ -0,0 +1,166 @@
|
||||
import {RedditUser, Comment, Submission} from "snoowrap";
|
||||
import cache from 'memory-cache';
|
||||
import objectHash from 'object-hash';
|
||||
import {
|
||||
AuthorActivitiesOptions,
|
||||
AuthorTypedActivitiesOptions,
|
||||
getAuthorActivities,
|
||||
testAuthorCriteria
|
||||
} from "../Utils/SnoowrapUtils";
|
||||
import Subreddit from 'snoowrap/dist/objects/Subreddit';
|
||||
import winston, {Logger} from "winston";
|
||||
import {mergeArr} from "../util";
|
||||
import LoggedError from "../Utils/LoggedError";
|
||||
import {SubredditCacheConfig} from "../Common/interfaces";
|
||||
import {AuthorCriteria} from "../Rule";
|
||||
|
||||
export const WIKI_DESCRIM = 'wiki:';
|
||||
|
||||
export interface SubredditCacheOptions extends SubredditCacheConfig {
|
||||
enabled: boolean;
|
||||
logger?: Logger;
|
||||
}
|
||||
|
||||
export class SubredditCache {
|
||||
enabled: boolean;
|
||||
authorTTL: number;
|
||||
useSubredditAuthorCache: boolean;
|
||||
wikiTTL: number;
|
||||
name: string;
|
||||
logger: Logger;
|
||||
|
||||
constructor(name: string, options?: SubredditCacheOptions) {
|
||||
const {
|
||||
enabled = true,
|
||||
authorTTL,
|
||||
wikiTTL = 300000, // 5 minutes
|
||||
logger,
|
||||
} = options || {};
|
||||
|
||||
this.enabled = manager.enabled ? enabled : false;
|
||||
if (authorTTL === undefined) {
|
||||
this.useSubredditAuthorCache = false;
|
||||
this.authorTTL = manager.authorTTL;
|
||||
} else {
|
||||
this.useSubredditAuthorCache = true;
|
||||
this.authorTTL = authorTTL;
|
||||
}
|
||||
this.wikiTTL = wikiTTL;
|
||||
this.name = name;
|
||||
if (logger === undefined) {
|
||||
const alogger = winston.loggers.get('default')
|
||||
this.logger = alogger.child({labels: [this.name, 'Resource Cache']}, mergeArr);
|
||||
} else {
|
||||
this.logger = logger.child({labels: ['Resource Cache']}, mergeArr);
|
||||
}
|
||||
}
|
||||
|
||||
async getAuthorActivities(user: RedditUser, options: AuthorTypedActivitiesOptions): Promise<Array<Submission | Comment>> {
|
||||
const useCache = this.enabled && this.authorTTL > 0;
|
||||
let hash;
|
||||
if (useCache) {
|
||||
const userName = user.name;
|
||||
const hashObj: any = {...options, userName};
|
||||
if(this.useSubredditAuthorCache) {
|
||||
hashObj.subreddit = this.name;
|
||||
}
|
||||
hash = objectHash.sha1({...options, userName});
|
||||
|
||||
const cacheVal = cache.get(hash);
|
||||
if (null !== cacheVal) {
|
||||
this.logger.debug(`Cache Hit: ${userName} (${options.type || 'overview'})`);
|
||||
return cacheVal as Array<Submission | Comment>;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const items = await getAuthorActivities(user, options);
|
||||
|
||||
if (useCache) {
|
||||
cache.put(hash, items, this.authorTTL);
|
||||
}
|
||||
return Promise.resolve(items);
|
||||
}
|
||||
|
||||
async getAuthorComments(user: RedditUser, options: AuthorActivitiesOptions): Promise<Comment[]> {
|
||||
return await this.getAuthorActivities(user, {...options, type: 'comment'}) as unknown as Promise<Comment[]>;
|
||||
}
|
||||
|
||||
async getAuthorSubmissions(user: RedditUser, options: AuthorActivitiesOptions): Promise<Submission[]> {
|
||||
return await this.getAuthorActivities(user, {
|
||||
...options,
|
||||
type: 'submission'
|
||||
}) as unknown as Promise<Submission[]>;
|
||||
}
|
||||
|
||||
async getContent(val: string, subreddit: Subreddit): Promise<string> {
|
||||
const hasWiki = val.trim().substring(0, WIKI_DESCRIM.length) === WIKI_DESCRIM;
|
||||
if (!hasWiki) {
|
||||
return val;
|
||||
} else {
|
||||
const useCache = this.enabled && this.wikiTTL > 0;
|
||||
const wikiPath = val.trim().substring(WIKI_DESCRIM.length);
|
||||
|
||||
let hash = `${subreddit.display_name}-${wikiPath}`;
|
||||
if (useCache) {
|
||||
const cachedContent = cache.get(`${subreddit.display_name}-${wikiPath}`);
|
||||
if (cachedContent !== null) {
|
||||
this.logger.debug(`Cache Hit: ${wikiPath}`);
|
||||
return cachedContent;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const wikiPage = subreddit.getWikiPage(wikiPath);
|
||||
const wikiContent = await wikiPage.content_md;
|
||||
|
||||
if (useCache) {
|
||||
cache.put(hash, wikiContent, this.wikiTTL);
|
||||
}
|
||||
|
||||
return wikiContent;
|
||||
} catch (err) {
|
||||
const msg = `Could not read wiki page. Please ensure the page 'https://reddit.com${subreddit.display_name_prefixed}wiki/${wikiPath}' exists and is readable`;
|
||||
this.logger.error(msg, err);
|
||||
throw new LoggedError(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async testAuthorCriteria(item: (Comment|Submission), authorOpts: AuthorCriteria, include = true) {
|
||||
const useCache = this.enabled && this.authorTTL > 0;
|
||||
let hash;
|
||||
if(useCache) {
|
||||
const hashObj = {itemId: item.id, ...authorOpts, include};
|
||||
hash = `authorCrit-${objectHash.sha1(hashObj)}`;
|
||||
const cachedAuthorTest = cache.get(hash);
|
||||
if(null !== cachedAuthorTest) {
|
||||
this.logger.debug(`Cache Hit: Author Check on ${item.id}`);
|
||||
return cachedAuthorTest;
|
||||
}
|
||||
}
|
||||
|
||||
const result = await testAuthorCriteria(item, authorOpts, include);
|
||||
if(useCache) {
|
||||
cache.put(hash, result, this.authorTTL);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
class SubredditCacheManager {
|
||||
caches: Map<string, SubredditCache> = 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))
|
||||
}
|
||||
return this.caches.get(subName) as SubredditCache;
|
||||
}
|
||||
}
|
||||
|
||||
const manager = new SubredditCacheManager();
|
||||
|
||||
export default manager;
|
||||
@@ -1,27 +1,82 @@
|
||||
import {Option} from "commander";
|
||||
import commander, {InvalidOptionArgumentError} from "commander";
|
||||
import {argParseInt, parseBool} from "../util";
|
||||
|
||||
export const getOptions = () => {
|
||||
const options = [];
|
||||
export const getOptions = (): commander.Option[] => {
|
||||
let options = [];
|
||||
|
||||
const clientIdOption = new Option('-c, --clientId <id>', 'Client ID for your Reddit application').default(process.env.CLIENT_ID, 'process.env.CLIENT_ID');
|
||||
clientIdOption.required = true;
|
||||
options.push(clientIdOption);
|
||||
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 clientSecretOption = new Option('-e, --clientSecret <secret>', 'Client Secret for your Reddit application').default(process.env.CLIENT_SECRET, 'process.env.CLIENT_SECRET');
|
||||
clientSecretOption.required = true;
|
||||
options.push(clientSecretOption);
|
||||
const accessTokenOption = new Option('-a, --accessToken <token>', 'Access token retrieved from authenticating an account with your Reddit Application').default(process.env.ACCESS_TOKEN, 'process.env.ACCESS_TOKEN');
|
||||
accessTokenOption.required = true;
|
||||
options.push(accessTokenOption);
|
||||
const refreshTokenOption = new Option('-r, --refreshToken <token>', 'Refresh token retrieved from authenticating an account with your Reddit Application').default(process.env.REFRESH_TOKEN, 'process.env.REFRESH_TOKEN');
|
||||
refreshTokenOption.required = true;
|
||||
options.push(refreshTokenOption);
|
||||
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 = [
|
||||
clientId,
|
||||
clientSecret,
|
||||
accessToken,
|
||||
refreshToken,
|
||||
subreddits,
|
||||
logDir,
|
||||
logLevel,
|
||||
wikiConfig,
|
||||
snooDebug,
|
||||
authorTTL,
|
||||
heartbeat,
|
||||
apiRemaining,
|
||||
dryRun,
|
||||
disableCache
|
||||
]
|
||||
|
||||
options.push(new 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)'));
|
||||
options.push(new 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'));
|
||||
options.push(new Option('-l, --logLevel <level>', 'Log level').default(process.env.LOG_LEVEL, 'process.env.LOG_LEVEL'));
|
||||
options.push(new Option('-w, --wikiConfig <path>', 'Relative url to contextbot wiki page (from https://reddit.com/r/subreddit/wiki/<path>').default(process.env.WIKI_CONFIG || 'botconfig/contextbot', 'process.env.WIK_CONFIG || \'botconfig/contextbot\''));
|
||||
options.push(new Option('-n, --snooDebug', 'Set Snoowrap to debug').default(process.env.SNOO_DEBUG || false, 'process.env.SNOO_DEBUG || false'));
|
||||
|
||||
return options;
|
||||
}
|
||||
|
||||
@@ -3,9 +3,10 @@ import Submission from "snoowrap/dist/objects/Submission";
|
||||
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 {truncateStringToLength} from "../util";
|
||||
import {normalizeName, truncateStringToLength} from "../util";
|
||||
|
||||
export interface AuthorTypedActivitiesOptions extends AuthorActivitiesOptions {
|
||||
type?: 'comment' | 'submission',
|
||||
@@ -121,7 +122,7 @@ export const renderContent = async (content: string, data: (Submission | Comment
|
||||
} = ruleResult;
|
||||
// remove all non-alphanumeric characters (spaces, dashes, underscore) and set to lowercase
|
||||
// we will set this as the rule property name to make it easy to access results from mustache template
|
||||
const normalName = name.trim().replace(/\W+/g, '').toLowerCase()
|
||||
const normalName = normalizeName(name);
|
||||
return {
|
||||
...acc, [normalName]: {
|
||||
kind,
|
||||
@@ -132,7 +133,7 @@ export const renderContent = async (content: string, data: (Submission | Comment
|
||||
};
|
||||
}, {});
|
||||
|
||||
return Mustache.render(content, {item: templateData, rules: normalizedRuleResults});
|
||||
return he.decode(Mustache.render(content, {item: templateData, rules: normalizedRuleResults}));
|
||||
}
|
||||
|
||||
export const testAuthorCriteria = async (item: (Comment|Submission), authorOpts: AuthorCriteria, include = true) => {
|
||||
|
||||
@@ -104,7 +104,6 @@ for (const o of getOptions()) {
|
||||
logger.error('Reddit responded with a 403 insufficient_scope, did you choose the correct scopes?');
|
||||
}
|
||||
}
|
||||
debugger;
|
||||
console.log(err);
|
||||
}
|
||||
}());
|
||||
|
||||
166
src/util.ts
166
src/util.ts
@@ -5,6 +5,10 @@ import {RulePremise, RuleResult} from "./Rule";
|
||||
import deepEqual from "fast-deep-equal";
|
||||
import utc from 'dayjs/plugin/utc.js';
|
||||
import dduration from 'dayjs/plugin/duration.js';
|
||||
import Ajv from "ajv";
|
||||
import {InvalidOptionArgumentError} from "commander";
|
||||
import Submission from "snoowrap/dist/objects/Submission";
|
||||
import {Comment} from "snoowrap";
|
||||
|
||||
dayjs.extend(utc);
|
||||
dayjs.extend(dduration);
|
||||
@@ -19,22 +23,10 @@ const CWD = process.cwd();
|
||||
|
||||
export const truncateStringToLength = (length: number, truncStr = '...') => (str: string) => str.length > length ? `${str.slice(0, length - truncStr.length - 1)}${truncStr}` : str;
|
||||
|
||||
export const loggerMetaShuffle = (logger: Logger, newLeaf: (string | undefined | null) = null, extraLabels: string[] = [], {truncateLength = 50} = {}) => {
|
||||
const labelTrunc = truncateStringToLength(truncateLength);
|
||||
const {labels = [], leaf} = logger.defaultMeta || {};
|
||||
return {
|
||||
labels: labels.concat(extraLabels.map(x => labelTrunc(x))),
|
||||
leaf: newLeaf
|
||||
};
|
||||
}
|
||||
|
||||
let longestLabel = 3;
|
||||
// @ts-ignore
|
||||
export const defaultFormat = printf(({
|
||||
level,
|
||||
message,
|
||||
label = 'App',
|
||||
labels = [],
|
||||
labels = ['App'],
|
||||
leaf,
|
||||
itemId,
|
||||
timestamp,
|
||||
@@ -44,9 +36,6 @@ export const defaultFormat = printf(({
|
||||
...rest
|
||||
}) => {
|
||||
let stringifyValue = splatObj !== undefined ? jsonStringify(splatObj) : '';
|
||||
if (label.length > longestLabel) {
|
||||
longestLabel = label.length;
|
||||
}
|
||||
let msg = message;
|
||||
let stackMsg = '';
|
||||
if (stack !== undefined) {
|
||||
@@ -59,16 +48,11 @@ export const defaultFormat = printf(({
|
||||
stackMsg = `\n${cleanedStack}`;
|
||||
}
|
||||
|
||||
let labelContent = `[${label.padEnd(longestLabel)}]`;
|
||||
if (labels.length > 0 || (leaf !== null && leaf !== undefined)) {
|
||||
let nodes = labels;
|
||||
if (leaf !== null) {
|
||||
nodes.push(leaf);
|
||||
}
|
||||
//labelContent = `${labels.slice(0, labels.length).map((x: string) => `[${x}]`).join(' ')}`
|
||||
labelContent = `${nodes.map((x: string) => `[${x}]`).join(' ')}`;
|
||||
let nodes = labels;
|
||||
if (leaf !== null && leaf !== undefined) {
|
||||
nodes.push(leaf);
|
||||
}
|
||||
//let leafContent = leaf !== undefined ? ` (${leaf})` : '';
|
||||
const labelContent = `${nodes.map((x: string) => `[${x}]`).join(' ')}`;
|
||||
|
||||
return `${timestamp} ${level.padEnd(7)}: ${labelContent} ${msg}${stringifyValue !== '' ? ` ${stringifyValue}` : ''}${stackMsg}`;
|
||||
});
|
||||
@@ -89,19 +73,6 @@ export const labelledFormat = (labelName = 'App') => {
|
||||
);
|
||||
}
|
||||
|
||||
export const createLabelledLogger = (name = 'default', label = 'App') => {
|
||||
if (winston.loggers.has(name)) {
|
||||
return winston.loggers.get(name);
|
||||
}
|
||||
const def = winston.loggers.get('default');
|
||||
winston.loggers.add(name, {
|
||||
transports: def.transports,
|
||||
level: def.level,
|
||||
format: labelledFormat(label)
|
||||
});
|
||||
return winston.loggers.get(name);
|
||||
}
|
||||
|
||||
export interface groupByOptions {
|
||||
lowercase?: boolean
|
||||
}
|
||||
@@ -212,6 +183,125 @@ export const mergeArr = (objValue: [], srcValue: []): (any[] | undefined) => {
|
||||
}
|
||||
}
|
||||
|
||||
export const ruleNamesFromResults = (results: RuleResult[]) => {
|
||||
return results.map(x => x.name || x.premise.kind).join(' | ')
|
||||
}
|
||||
|
||||
export const createAjvFactory = (logger: Logger) => {
|
||||
return new Ajv({logger: logger, verbose: true, strict: "log", allowUnionTypes: true});
|
||||
}
|
||||
|
||||
export const comparisonTextOp = (val1: number, strOp: string, val2: number): boolean => {
|
||||
switch (strOp) {
|
||||
case '>':
|
||||
return val1 > val2;
|
||||
case '>=':
|
||||
return val1 >= val2;
|
||||
case '<':
|
||||
return val1 < val2;
|
||||
case '<=':
|
||||
return val1 <= val2;
|
||||
default:
|
||||
throw new Error(`${strOp} was not a recognized operator`);
|
||||
}
|
||||
}
|
||||
|
||||
export const percentFromString = (str: string): number => {
|
||||
const n = Number.parseInt(str.replace('%', ''));
|
||||
if(Number.isNaN(n)) {
|
||||
throw new Error(`${str} could not be parsed to a number`);
|
||||
}
|
||||
return n / 100;
|
||||
}
|
||||
|
||||
export const formatNumber = ( val: number|string, options: any = {} ) => {
|
||||
const {
|
||||
toFixed = 2,
|
||||
defaultVal = null,
|
||||
prefix = '',
|
||||
suffix = '',
|
||||
round = {
|
||||
type: 'round',
|
||||
enable: false,
|
||||
indicate: true,
|
||||
}
|
||||
} = options;
|
||||
let parsedVal = typeof val === 'number' ? val : Number.parseFloat( val );
|
||||
if(Number.isNaN( parsedVal )) {
|
||||
return defaultVal;
|
||||
}
|
||||
let prefixStr = prefix;
|
||||
const { enable = true, indicate = true, type = 'round' } = round;
|
||||
if(enable && !Number.isInteger(parsedVal)) {
|
||||
switch(type) {
|
||||
case 'round':
|
||||
parsedVal = Math.round(parsedVal);
|
||||
break;
|
||||
case 'ceil':
|
||||
parsedVal = Math.ceil(parsedVal);
|
||||
break;
|
||||
case 'floor':
|
||||
parsedVal = Math.floor(parsedVal);
|
||||
}
|
||||
if(indicate) {
|
||||
prefixStr = `~${prefix}`;
|
||||
}
|
||||
}
|
||||
const localeString = parsedVal.toLocaleString( undefined, {
|
||||
minimumFractionDigits: toFixed,
|
||||
maximumFractionDigits: toFixed,
|
||||
} );
|
||||
return `${prefixStr}${localeString}${suffix}`;
|
||||
};
|
||||
|
||||
export function argParseInt(value: any, prev: any = undefined): number {
|
||||
let usedVal = value;
|
||||
if (value === undefined || value === '') {
|
||||
usedVal = prev;
|
||||
}
|
||||
if(usedVal === undefined || usedVal === '') {
|
||||
return usedVal;
|
||||
}
|
||||
|
||||
if (typeof usedVal === 'string') {
|
||||
const parsedValue = parseInt(usedVal, 10);
|
||||
if (isNaN(parsedValue)) {
|
||||
throw new InvalidOptionArgumentError('Not a number.');
|
||||
}
|
||||
return parsedValue;
|
||||
} else if (typeof usedVal === 'number') {
|
||||
return usedVal;
|
||||
}
|
||||
throw new InvalidOptionArgumentError('Not a number.');
|
||||
}
|
||||
|
||||
export function parseBool(value: any, prev: any = false): boolean {
|
||||
let usedVal = value;
|
||||
if (value === undefined || value === '') {
|
||||
usedVal = prev;
|
||||
}
|
||||
if(usedVal === undefined || usedVal === '') {
|
||||
return false;
|
||||
}
|
||||
if (typeof usedVal === 'string') {
|
||||
return usedVal === 'true';
|
||||
} else if (typeof usedVal === 'boolean') {
|
||||
return usedVal;
|
||||
}
|
||||
throw new InvalidOptionArgumentError('Not a boolean value.');
|
||||
}
|
||||
|
||||
export function activityWindowText(activities: (Submission | Comment)[], suffix = false): (string | undefined) {
|
||||
if (activities.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
if (activities.length === 1) {
|
||||
return `1 Item`;
|
||||
}
|
||||
|
||||
return dayjs.duration(dayjs(activities[0].created_utc * 1000).diff(dayjs(activities[activities.length - 1].created_utc * 1000))).humanize(suffix);
|
||||
}
|
||||
|
||||
export function normalizeName(val: string) {
|
||||
return val.trim().replace(/\W+/g, '').toLowerCase()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user