Compare commits

..

169 Commits

Author SHA1 Message Date
FoxxMD
e07b8cc291 Merge branch 'edge' 2022-02-18 11:58:28 -05:00
FoxxMD
80fabeac54 fix(usernote): Fix adding new note to user note cache AFTER clearing cache
* Fixes an issue where the cached notes for a user only contain the last added note instead of all notes + new
* Also reduced api calls by caching moderator adding new note instead of calling each time
2022-02-18 09:54:18 -05:00
FoxxMD
c001be9abf feat(ui): Add reddit status indicator with link 2022-02-17 16:14:36 -05:00
FoxxMD
639a542fb2 fix(ui): Fix default values for scopes and permissions when not available 2022-02-17 13:53:06 -05:00
FoxxMD
9299258de0 feat(ui): Add moderator permissions list to subreddit overview 2022-02-17 13:37:42 -05:00
FoxxMD
59f8ac6dd4 feat(ui): Add oauth scopes list to bot overview
Visible when user is an operator
2022-02-17 13:29:37 -05:00
FoxxMD
f16155bb1f fix(flair): Fix snoowrap function used for assigning flair template id 2022-02-17 13:17:17 -05:00
FoxxMD
e2d2f73bb3 feat: Add log warning when user has no access 2022-02-15 11:00:09 -05:00
FoxxMD
9ca5d6c8c2 fix: Fix config builder to supply more defaults for a minimal configuration
* Provide a default redirect uri
* Don't add default bot instance if no credentials were specified
2022-02-14 12:07:54 -05:00
FoxxMD
d8f673bd26 fix(remove): Only warn if item looks removed, on remove action
If the item is not actually removed (it's hard to tell from reddit api) we don't want to prematurely end remove action. Just warn and try to remove anyway
2022-02-14 09:31:50 -05:00
FoxxMD
7e2068d82a fix(author): Ensure automoderator is always detected as a moderator for author isMod test 2022-02-14 09:30:54 -05:00
FoxxMD
176611dbf3 docs: Add web interface and config onboarding 2022-02-11 23:40:28 -05:00
FoxxMD
3d99406f33 Merge branch 'persistActions' into edge 2022-02-09 17:09:24 -05:00
FoxxMD
ab355977ba fix(approve): Fix approval check target 2022-02-09 16:41:39 -05:00
FoxxMD
8667fcdef3 fix(stats): Correctly initialize all time historical stats from cache when stat is empty 2022-02-09 13:10:05 -05:00
FoxxMD
ec20445772 refactor(ui): Use checkmark symbol that matches x symbol (no emojis) 2022-02-09 13:09:39 -05:00
FoxxMD
0293928a99 feat(cache): Implement cache key manipulation based on key pattern
* Implement glob pattern or regex as argument
* Implement scan search for redis for efficiency otherwise iterate keys using generic function
* Implement cache reset based on passed item from action -- reset item crit for activities, author crit for users, and overwrite any cached activity
2022-02-08 13:01:09 -05:00
FoxxMD
b56d6dbe7c fix(actions): Only include successfully run actions in notification summary 2022-02-07 22:21:22 -05:00
FoxxMD
42d269e28d feat(actions): Mutate activities during actions for immediate use and ensure cache is synced 2022-02-07 16:21:43 -05:00
FoxxMD
8f60a1da53 feat(regex): Add option to stop rule early if current activity does not match
In order to prevent history from being pulled (and using api) if user indicates current activity must also match
2022-02-07 15:15:50 -05:00
FoxxMD
f511be7c33 fix(usernote): Throw error with cause when usernote fails instead of logging quietly
* Makes error cause easier to see in stack and fixes error now logging during action failure
* Use error with cause for logging action error for clearer stack
2022-02-07 12:41:10 -05:00
FoxxMD
ebb426e696 feat(filter): Add isRedditMediaDomain submission state criteria 2022-02-07 10:36:56 -05:00
FoxxMD
fc51928054 Merge branch 'edge' 2022-02-02 16:59:56 -05:00
FoxxMD
c07276a3be fix(logging): Fix typo in error transform 2022-02-01 13:13:27 -05:00
FoxxMD
4a2297f5cd docs: Add github sponsor link 2022-02-01 12:01:34 -05:00
FoxxMD
f8967d55c4 feat(repeat): Use newer text comparison technique to improve repeat detection
* Use same technique as repost rule which has high accuracy and let false-positives
* Implement ability to see similarity score, case sensitivity, and text transformations
2022-01-31 14:08:21 -05:00
FoxxMD
e2590e50f8 Merge branch 'edge' 2022-01-28 17:27:51 -05:00
FoxxMD
7e8745d226 fix(polling): Fix shared polling behavior for nanny mode changes
* On hard limit stop shared streams
* On nanny mode turned off restart any stopped shared streams
2022-01-27 16:49:03 -05:00
FoxxMD
e2efc85833 fix(polling): Fix running state not changed on error
* Set running to false when error is caught. Was not caught on last stream refactor which changed polling behavior to end if any error is caught rather than waiting for external source to clear interval
* Add debugging/error messages on polling start/stop
2022-01-27 16:47:43 -05:00
FoxxMD
41038b9bcd feat(logging): Implement richer errors everywhere
* Use ErrorWithCause so we can get and print a chain of error causes
* Make reddit error response in stack trace more readable by replacing them with a "translated" parent response and add them as the cause
* Properly handle error formatting for winston by looking at shape of log object for error rather than testing instanceof (see comments in errorAwareFormat)
* Fix formatting in web interface for log lines with white-space pre css and properly splitting timestamp from rest of the message
2022-01-27 16:27:03 -05:00
FoxxMD
9fe8c9568c refactor: Move SimpleError into main Errors module 2022-01-27 11:48:23 -05:00
FoxxMD
9614f7a209 refactor(logging): Implement snoowrap errors "the right way" and implement consolidated logging function
* Implement declaration file for snoowrap errors so they can be imported directly
* Implement logging function to handle boilerplate for known error responses (reddit HTTP response, rate limit, etc.)
2022-01-27 11:43:39 -05:00
FoxxMD
8dbaaf6798 fix(logging): Defaults for log file dir 2022-01-26 12:28:56 -05:00
FoxxMD
c14ad6cb76 feat(logging): Implement separate logging options for each transport type
* Add properties for file, console, and stream in logging object of operator config
* Each property inherits a (useful) subset of winston transport options
2022-01-26 12:09:03 -05:00
FoxxMD
adda280dd3 fix(logging): Fix parsing log dir
* Correct else condition to use log dir when value is not true
* Set level to 'debug' on init logger if no value is provided to help with debugging
2022-01-26 10:27:01 -05:00
FoxxMD
15fd47bdb4 fix(polling): Correct typings for stream getter and check isFinished for Listing 2022-01-26 10:11:06 -05:00
FoxxMD
78b6d8b7b6 feat(polling): Add debug messages when streams are stopped 2022-01-26 10:00:09 -05:00
FoxxMD
61bc63ccc5 fix(polling): Emit config change event to bot only after manager has rebuilt polling 2022-01-26 09:50:30 -05:00
FoxxMD
05df8b7fe2 fix(polling): Use manager eventState to control shared stream callback rather than removing callback when events are stopped
Should prevent edge cases where shared streams are re-parsed while managers are stopped (hard limit) and then removed due to there being no callbacks
2022-01-25 18:07:15 -05:00
FoxxMD
3cb7dffb90 fix(polling): Prevent endless loop when trying to enforce continuity on a stream with no items returned 2022-01-25 09:25:59 -05:00
FoxxMD
d0aafc34b9 feat(remove): Add option to mark activity as spam 2022-01-21 13:03:05 -05:00
FoxxMD
d2e1b5019f chore: Update packages 2022-01-21 13:02:31 -05:00
FoxxMD
aaed0d3419 Merge branch 'edge' 2022-01-21 10:46:11 -05:00
FoxxMD
2a77c71645 fix(usernotes): Fix wiki entity handling to avoid unhandled rejection
Since snoowrap's WikiPage isn't a "real" object setting it as a property on the class means if it rejects the whole application crashes. Fix this by building wiki proxy every time we need it before awaiting promise for edit/retrieval so that promise scope is bound to the function we are in (that has try-catch)
2022-01-20 14:10:39 -05:00
FoxxMD
780e5c185e refactor(author filter): Strongly structure comparison/matching data for more consistent manipulation and output
* Use interface for comparison results at both criteria property level and criteria level
* Implement summary functions to build string results of comparisons
* Output all comparisons to debug and provide summaries to verbose (when applicable)
2022-01-20 14:08:54 -05:00
FoxxMD
38e2a4e69a fix(filter): Missing return on flair failure comparison 2022-01-19 15:49:44 -05:00
FoxxMD
7e0c34b6a3 fix(userflair): Fix wrong assignment for css 2022-01-19 13:10:11 -05:00
FoxxMD
e3ceb90d6f fix(filter): Fix default excludeCondition type
* Expected (prior) behavior is that all exclude criteria must pass, not just one
* Fix missing AND condition logic when all conditions pass
2022-01-19 13:09:45 -05:00
FoxxMD
6977e3bcdf feat(author): Add flair template criteria for author/submission
* Add filtering by flairTemplate id for author/submission
* Refactor flair properties for author/submission to accept string or array of strings
2022-01-19 12:48:58 -05:00
FoxxMD
f382cddc2a fix(filter): Change array merging behavior for authorIs defaults to be more sane
* Don't just overwrite (duh)
* Drop any default filters that include object keys that are also present in user-defined filters -- this way user-defined always takes precedence on merge
2022-01-19 11:52:18 -05:00
FoxxMD
99a5642bdf fix(ui): Change time formatting from 12 to 24 hour 2022-01-18 16:49:07 -05:00
FoxxMD
174d832ab0 docs: Pretty up readme header 2022-01-18 16:08:40 -05:00
FoxxMD
3ee7586fe2 fix(approve): Fix touched entity 2022-01-18 13:37:56 -05:00
FoxxMD
e2c724b4ae feat(approve): Implement approving parent submission of comment 2022-01-18 13:37:22 -05:00
FoxxMD
d581f19a36 feat(logs): Use log objects in api to improve parsing client-side
* Add options for /logs endpoint to stream objects instead of strings
* Always return log objects from /status endpoint -- fixes bug where all bots/subreddits got lines from logs that had newlines
* Return context-aware, formatted log lines to client to reduce line length IE if returning to botA -> subA then do not need to include labels for botA,subA #40
* Shorten timestamp to just time and wrap full timestamp in tooltip #40
* Emit log objects to client to reduce parsing complexity (don't have to regex for bot/subreddit name)
2022-01-18 12:59:59 -05:00
FoxxMD
48dea24bea feat: Improve first-run display in ui and add system view
* Fix bugs in UI when bot does not have a name (configured incorrectly)
* Implement instance system log view for operators
2022-01-18 10:38:39 -05:00
FoxxMD
5fc2a693a0 fix(config): Fix empty yaml config document initialization 2022-01-18 00:06:52 -05:00
FoxxMD
7be0722140 fix(bot): Fix limit rate expiration getter when there is no client initialized 2022-01-18 00:06:24 -05:00
FoxxMD
6ab9fe4bf4 feat(config): Implement persisting bots from invite process to application and config
* write to config when bot is added
* replace/add based on existing bot
* implement specify instance from instances user is operator of
* implement specify subreddits to run on using comma-separated list
* rewrite invite flow ending to be more clear on results and next steps
2022-01-17 17:47:27 -05:00
FoxxMD
5811af0342 feat(config): Refactor config parsing to preserve comments and enable writing
* use node-comment and yaml@next to keep comment information intact
* store ast/source version of parsed config for operator
* implement generic yaml/json operator config classes to keep everything organized and simplify marshalling source to js/string
* refactor file parsing and json/yaml parsing to have better single responsibility
2022-01-17 15:51:43 -05:00
FoxxMD
ed2924264a feat(util): Better check for file/dir permissions 2022-01-17 11:18:23 -05:00
FoxxMD
e9394ccf2e refactor(tooling): Ignore sqlite files 2022-01-17 09:52:18 -05:00
FoxxMD
dec72f95c6 docs: Add discord invite link 2022-01-14 16:42:01 -05:00
FoxxMD
bc7eff8928 Merge branch 'edge' 2022-01-14 15:27:09 -05:00
FoxxMD
80c11b2c7f refactor(filter): Consolidate authorIs logic and add additional control to exclude logic
* Add excludeCondition to control how exclude sets are tested (and/or)
* Refactor authorIs logic from check/rule/action into standalone function (DRY)
* Simplify filter defaults -- don't need to specify automoderator since it is always a mod
2022-01-14 10:51:29 -05:00
FoxxMD
e6a2a86828 feat(config): Implement default filter criteria behavior
* Add default behavior config to operator and manager config
* Implement configurable behavior when filter is present on check
* Add defaults to exclude mods and automoderator from checks
2022-01-13 16:46:32 -05:00
FoxxMD
96749be571 refactor(polling): Simplify and cleanup all polling logic
* Remove unused clearProcessing code
* Use same data structures (Map) for storing polling objects in both Manager and Bot to reduce cognitive load and re-use some logic
* Rename "mod" streams to "shared" streams
* Implement detection and updating of polling when manager config changes
* Implement detection and updating of shared streams on manager config update
* Use shared retry handler for manager polling to better handle general reddit api issues (all polling stops faster)
* Move initial polling buffer into polling object (instead of in manager) for better logic encapsulation and add debug logging for it
* Add more debug logging for manager/bot poll building
2022-01-13 11:39:16 -05:00
FoxxMD
6b7e8e7749 feat(polling): Implement shared streams for all polling sources
* Refactor polling config to use new 'shared' string list of polling sources and deprecate 'sharedMod' property
* Refactor how shared sources are built to look for shared intention in manager polling options before creating
* Implement continuity check for comment/submission polling to ensure no activities are missed
* Add debug logging to polling
2022-01-12 15:47:43 -05:00
FoxxMD
43b29432a2 refactor(auth): Refactor auth data structures to consolidate logic
* Add abstract user class with auth methods with implementations for client/server
* Refactor client/server logic to use class methods instead of inline auth checks

Closes #71
2022-01-12 09:57:38 -05:00
FoxxMD
ff84946068 feat(regex): Experimental support for parsing regex expressions from fetched URL
* Support fetching from reddit wiki
* Support fetching from raw URL
* Support parsing and fetching from gist, github blob, and regexr (very experimental)
2022-01-11 14:05:57 -05:00
FoxxMD
7cdde99864 fix(recent): Potential fix for reddit ACID issues on history retrieval 2022-01-11 13:00:51 -05:00
FoxxMD
8eee1fe2e1 fix(recent): Remove code that should have been deleted during refactor
Refactored recent to use batch subreddit testing but forgot to remove old, individual subreddit testing, code so activities were being counted twice
2022-01-11 10:15:16 -05:00
FoxxMD
6fc09864f6 fix: Don't delete property from object
Object passed by ref, duh
2022-01-11 10:13:48 -05:00
FoxxMD
1510980ce3 fix(util): Ensure provided state description is reattached to strong sub state 2022-01-11 10:13:14 -05:00
FoxxMD
56005f0f28 fix(bot): Fix own profile detection when building managers 2022-01-11 09:52:44 -05:00
FoxxMD
03b655515c fix(server): Fix logs not persisting for managers
* Change manager acquisition so all managers belong to a bot before they start logging so all logs are captured correctly
* Fix log capture logic that prevented all subreddits from being populated
2022-01-11 09:45:25 -05:00
FoxxMD
edd874f356 fix(server): Correctly filter bots and managers on auth on server 2022-01-11 09:15:52 -05:00
FoxxMD
7f13debe3b fix(client): Make sure all moderated subreddits are fetched 2022-01-10 16:17:24 -05:00
Matt Foxx
1565bdbf1a Merge pull request #67 from rysie/feature/dry-run-buttons
Run/Dry run buttons
2022-01-10 14:54:42 -05:00
FoxxMD
ec4cee8c77 refactor(ui): Fix and simplify button logic
* Fix url query selector to constrain to sub
* Use shared class between run buttons to simplify class modification and click event
2022-01-10 14:54:17 -05:00
FoxxMD
d6954533a0 Merge branch 'edge' 2022-01-10 12:32:14 -05:00
Matt Foxx
04b8762926 Merge pull request #68 from rysie/feature/flair-docs
feat(docs): User flair and submission flair docs
2022-01-10 12:31:56 -05:00
FoxxMD
dcc5f87c30 refactor(docs): Clean up flair docs
* Fix regex escaped characters
* Use authorIs
* make flair action type usage more clear
2022-01-10 12:22:16 -05:00
FoxxMD
66d9c0b2a7 fix(auth): Fix bug allowing any valid moderator to see all instances 2022-01-10 10:27:13 -05:00
FoxxMD
00e7cad423 fix(auth): Logout bot after auth flow is complete 2022-01-10 10:26:45 -05:00
Marcin Macinski
bc541d00d4 feat(docs): User flair and submission flair docs 2022-01-08 00:02:37 +01:00
Marcin Macinski
c5b27628b0 feat(ui): Run/Dry run buttons 2022-01-07 23:32:12 +01:00
FoxxMD
ba53233640 Merge branch 'edge' 2022-01-07 09:31:14 -05:00
Matt Foxx
ede86d285b Merge pull request #62 from rysie/user-flair-action
UserFlairAction added
2022-01-06 14:55:27 -05:00
FoxxMD
52f6aabb69 feat: Prevent bot from running on reports/comments it just created
Cache reported items or new comments made by bot for a short time (default to twice polling interval, 1 minute) to prevent bot from running on things it did itself
2022-01-06 14:54:17 -05:00
FoxxMD
18175f3662 feat(item filter): Support checking for different report types: total, user, mod 2022-01-06 13:13:10 -05:00
FoxxMD
68a272d305 fix(ui): Fix subreddit intersection check for bot related routes
Remove any prefixed r/ from a bot's subreddits when checking intersection with user subreddits
2022-01-06 12:29:19 -05:00
FoxxMD
3dac91fafc fix(recent): Fix default behavior for submissionReference based on activity type
Eliminates noisy logging when it's not specified but activity is comment
2022-01-06 12:09:04 -05:00
FoxxMD
e5bb8c2a38 fix(bot): Reduce retries for more aggressive fallback on reddit api issues
* Reduce retry for snoowrap to 2 since we do our own error handling in-app and 2 is enough for the occasional, non-systemic blip
* Reduce manager retries
2022-01-05 20:46:54 -05:00
FoxxMD
61e0baf3fd feat(recent): Add combined karma to template variables 2022-01-05 17:08:15 -05:00
FoxxMD
37e9d1fcc2 fix(polling): Fix set timeout args 2022-01-05 14:28:19 -05:00
FoxxMD
5e70ca1cb6 fix: Fix and improve code related to stopping bots when reddit api is not OK
* Fix polling timeout to actually stop on error by simplifying timeout and waiting until response is OK to recreate next timeout call
* Use "unexpected exception" retry count for all non well-known "reddit blip" responses in retry handler rather than failing immediately AND log this distinction
* Fix managers not emitting errors from checks
* Fix bot not awaiting retry handler on manager error emit
* Increase nanny loop delay on error to reduce api pressure when there are many bots running
* (unrelated) set bot as running before starting managers so UI is available earlier
2022-01-05 12:58:17 -05:00
FoxxMD
7f7ed18927 refactor(server): return app earlier so UI is available earlier
Bot init can finish asynchronously without any negative affect to server/client. Returning earlier means we can access server info earlier in startup
2022-01-05 12:50:55 -05:00
FoxxMD
efed3381fd feat(config): Allow top-level operator snoowrap config 2022-01-05 10:39:43 -05:00
FoxxMD
5ac5d65a28 refactor(userflair): Fix dryrun usage and add unflair functionality
* Can flair user on comment/submission
* fix dryrun if-else block (maybe a debugging artifact?)
* allow all properties to be undefined/null/empty and use as intention to unflair user
2022-01-03 21:02:21 -05:00
FoxxMD
1ac7ad4724 Merge branch 'edge' 2022-01-03 16:35:01 -05:00
FoxxMD
0ae74fdce1 docs: Add missing config box screenshot 2022-01-03 13:42:35 -05:00
FoxxMD
845173822c docs(onboarding): Update moderator getting started to reflect new editor capabilities 2022-01-03 13:40:17 -05:00
FoxxMD
edb3036957 feat(editor): Resolve raw url from github blob url 2022-01-03 13:31:54 -05:00
FoxxMD
3790f0e061 feat(editor): Implement wiki page creation
If page is not created yet detect it and require more perms for re-authorization
2022-01-03 13:23:31 -05:00
FoxxMD
e3e4e4abff feat(editor): Resolve raw gist url from gist id 2022-01-03 11:50:23 -05:00
FoxxMD
fd9b83437b feat(editor): Use manager format to infer format for editor
* Provide wiki format in manager data to status api
* Remove additional View link and instead infer format for querystring from manager data
2022-01-03 11:17:04 -05:00
FoxxMD
05694f115c fix(editor): Fix how initial editor value is set
For some reason providing the data directly to a new model doesn't trigger validation and also had some other weird effects. Instead, using an empty string as initial value and then set model value to data afterwards -- which fixes everything b/c idk why.
2022-01-03 11:15:29 -05:00
FoxxMD
70ee157198 feat(config): Make manager aware of config format
* Default format to yaml
* Add detected config format as property to manager
* When neither format is valid use starting character to (naively) detect json or not
* Reduce config error noise by only show one format error based on likely type and print other to debug
2022-01-03 10:58:02 -05:00
FoxxMD
bbb4ec3c2d fix: Fix missing hyphen character on regex for parsing reddit entity name
Reddit users can have hyphens in their names. Slight tradeoff for allowing hyphens for subreddit names (they are non existent) to allow all valid reddit user names is worth it.

Found and corrected by @prometheus-22
2021-12-31 00:04:25 -05:00
FoxxMD
acb72551ec fix(bot): Add missing invite check in healthloop 2021-12-30 23:09:09 -05:00
FoxxMD
bf6affe592 feat(bot): Add basic programmatic moderator invitation acceptance
* Store subreddits to try to accept invites from in bot's default cache
* Handle invitation scenarios (none, modself missing, accepted) and starting manager after invitation
* Implement basic invitation acceptance list control in UI for bot operators
2021-12-30 22:52:37 -05:00
FoxxMD
8c2cb02a46 feat(bot): Add modself oauth permission
Used for accepting moderator invitations
2021-12-30 20:25:51 -05:00
FoxxMD
73e2af2100 feat(manager): Improve wiki page creation
* Only try page creation if response error is a 404
* Improve permission error descriptions
* Only create if it can also set page permissions to improve security
2021-12-30 19:08:47 -05:00
FoxxMD
ba4c4af5a7 Update invite view 2021-12-30 18:00:11 -05:00
FoxxMD
9ad21ee2dd feat(bot): Add ability to create non-existing wiki page and change visibility 2021-12-25 21:13:55 -05:00
FoxxMD
b32c4f213c docs(yaml): Finish adding yaml examples
#61
2021-12-25 16:42:44 -05:00
FoxxMD
7e01c8d1f8 docs(yaml): add yaml examples fo activities window
#61
2021-12-25 16:28:58 -05:00
FoxxMD
aee158ecc9 docs(yaml): add attribution and advanced concepts yaml examples
#61
2021-12-25 16:18:13 -05:00
FoxxMD
8cd2243c2d docs(yaml): add history and author yaml examples
#61
2021-12-25 16:07:46 -05:00
FoxxMD
4969789532 docs(yaml): add recentActivity yaml examples
#61
2021-12-25 15:53:54 -05:00
FoxxMD
1dcfdc14d1 docs(yaml): add regex yaml examples
#61
2021-12-25 15:23:34 -05:00
FoxxMD
f1c9b64f64 docs(yaml): Add more examples
#61
2021-12-23 21:35:18 -05:00
FoxxMD
2e5a61566b docs(yaml): create yaml versions of subreddit ready examples
#61
2021-12-23 12:34:50 -05:00
Matt Foxx
85761fa662 Merge pull request #65 from rysie/edge-submission-flair-fix
Flair action fix: import + assigning flair by flair_template_id
2021-12-23 09:33:34 -05:00
Marcin Macinski
0b1a6bd77b Flair action fix: import + assigning flair by flair_template_id 2021-12-23 12:34:08 +01:00
Marcin Macinski
51e299ca99 Merge branch 'edge' into user-flair-action 2021-12-22 01:13:33 +01:00
Marcin Macinski
7696f3c2ff UserFlairAction added 2021-12-22 00:45:59 +01:00
FoxxMD
1c9ed41e70 feat(ui): Implement basic wiki editing capabilities for editor
* Use stored scope to determine if user can save
* Only show save action if loaded from a subreddit
* Implement re-authorization flow through popup window and sockets to update status in editor
* Implement wiki location endpoint for server and wiki save endpoint for client
2021-12-21 16:30:57 -05:00
FoxxMD
2d67f9f57d refactor(ui): Migrate all editor usage to monaco-yaml base
* monaco-yaml can also do json validation since its just normal monaco
* simplifies config.ejs greatly not having to maintain two different monaco implementation, at the expense of a larger project
2021-12-21 14:22:26 -05:00
FoxxMD
975bcb6ad7 feat(ui): Enable additional scopes usage
* Store scopes in user auth object/sessions
* Implement requesting additional scopes through login route
2021-12-21 12:30:33 -05:00
FoxxMD
2a282a0d6f Merge branch 'edge' 2021-12-21 09:35:21 -05:00
FoxxMD
0d087521a7 feat(docs): Specify yaml can be validated in editor 2021-12-21 09:35:06 -05:00
FoxxMD
fb5fc961cc feat(docs): Add youtube credentials requirements for repost rule 2021-12-21 09:33:24 -05:00
FoxxMD
c04b305881 feat(docs): Add link-spamming regex config 2021-12-21 09:22:35 -05:00
FoxxMD
5c5e9a26aa refactor(ui): Unify editor look-and-feel
* Use named entry files for webpack so we can access them from config.ejs
* Move some of the validation logic to config.ejs so it can be used by json editor as well
* Use same controls for loading files/schemas/urls for both editors
* Add validation errors box for both editors
2021-12-20 23:30:41 -05:00
Matt Foxx
477d1a10ae Merge pull request #60 from rysie/edge
Support setting user flair by flair_template_id
2021-12-20 18:37:36 -05:00
Marcin Macinski
bbee92699c Support setting user flair by flair_template_id 2021-12-21 00:10:40 +01:00
FoxxMD
7f09043cdf fix(ui): Add missing yaml asset files
Need to unignore them...
2021-12-20 15:56:36 -05:00
FoxxMD
768a199c40 feat(ui): Add quick-n-dirty YAML editor
Using one-off project cm-yaml http://github.com/foxxmd/cm-yaml to run a static version of monaco-yaml inside CM
2021-12-20 15:42:37 -05:00
FoxxMD
6e4b0c7719 feat(regex): Add match sample to rule results so it can be used in content template 2021-12-20 12:33:40 -05:00
FoxxMD
89b21e6073 fix(repost): Use average of all algos to get high score
Ensures no anomalous scores dominate
2021-12-18 18:44:52 -05:00
FoxxMD
da611c5894 fix(repost): Fix max weighted score and formatting 2021-12-18 18:22:38 -05:00
FoxxMD
2c90a260c0 fix(stream): Remove processing clear behavior for polling to try to fix weird polling behavior 2021-12-18 18:08:00 -05:00
FoxxMD
f081598da6 feat(repost): Improve text matching and default values
* Use 3 different matching algorithms using the highest score out of the three
* Weight score based on length of the sentence
* Increase minimum number of words to 3
* Enforce min word count on external (youtube) comments
2021-12-18 17:42:43 -05:00
FoxxMD
55f45163a4 feat(repost): Fix some bugs and improve result data
* Wrong hash check
* Add closest match summary and percent for use in content
2021-12-18 14:54:01 -05:00
FoxxMD
e4dfa9dde3 refactor(docker): reduce image size with best practices
* use --no-cache so repository index isn't included in image
* use FROM instructions to build project with dev dependencies, then copy built project to new stage and install production-only libraries
* properly ignore docs and node_modules with dockerignore
2021-12-17 15:10:22 -05:00
FoxxMD
0e395792db refactor: Remove unusused ts-auto-guard package
Has @ts-morph as a dependency which is a huge package
2021-12-17 14:36:03 -05:00
FoxxMD
dcbeb784e8 refactor: Remove set-random-interval
* depended on and always downloaded an entire, older typescript version (even with production install) -- was not necessary for one function
* refactor project to use newer TS version (specify any type for catch blocks to fix compiler errors)
2021-12-17 14:25:48 -05:00
FoxxMD
aeaeb6ce27 refactor(docker): Remove monaco editor local dependency to reduce image size
Wasn't necessary to have it installed locally (can use CDN) and is not a part of the core experience
2021-12-17 14:00:54 -05:00
FoxxMD
d6a29c5914 fix(bot): Actually get all moderated subreddits
Previously was only getting first page of moderated subreddits (listing). Make sure to get all pages.
2021-12-17 13:24:09 -05:00
FoxxMD
c1224121d4 feat(subreddit): Implement user profile filtering for SubredditState
* Add convenience property for filtering subreddits by user profile prefix (u_)
* Do some smart property comparison to check if SubredditState name is regex or not -- use additional user prof check if it is so we don't clobber existing name regex

Closes #56
2021-12-17 13:01:47 -05:00
FoxxMD
9790e681ea docs(regex): Add subreddit-ready example for removing discord link spam 2021-12-16 16:27:06 -05:00
FoxxMD
a48a850c98 Update winston repository package format/information 2021-12-16 14:58:43 -05:00
FoxxMD
b8369a9e9f Package bump for security
https://github.com/advisories/GHSA-93q8-gq69-wqmw
2021-12-14 12:06:48 -05:00
FoxxMD
0c31bdf25e refactor(repost): Improve repost criteria configuration and add documentation
* Simplify usage b/w comment-submission by removing "criteria" in main criteria property -- comment checks can just use additional properties
* Consolidated occurrence count/time into one property to allow and/or operands on both conditions (more powerful!)
* Added documentation describing repost configuration
* Added repost configuration examples
2021-12-07 16:30:05 -05:00
FoxxMD
4b14e581dd fix: Add more staggering on heartbeat check
Always stagger subreddit checks
2021-12-06 17:31:00 -05:00
FoxxMD
b2846efd2b fix: Improve falloff behavior when reddit api error are encountered
* Turn off snoowrap request queuing
* Aggregate errors from all managers at bot-level and force all to stop (and clear queue/polling) if a small threshold is met
* Add activity refresh on check into try-catch so delayed activities in queue don't cause a loop if they fail due to api issues
2021-12-06 17:16:04 -05:00
FoxxMD
a787e4515b feat(repost): Implement occurrence count and time comparisons
* User-defined set of comparisons for testing how many reposts were found
* User-defined set of comparisons for testing when a slice/all of reposts were created
2021-12-06 13:28:02 -05:00
FoxxMD
f63e2a0ec4 Merge branch 'topComments' into edge 2021-12-01 17:34:51 -05:00
FoxxMD
9d0e098db1 feat(rule): Implement repost rule
* checks for both submission/comment
* search by submission title, url, dups/crossposts, and external (just youtube for now)
* user-defined string sameness for all search facets
* user-defined case-sensitivity and regex-based transformations for activity/repost item values
* cache comment checks
* implemented youtube client for retrieving video comments
2021-12-01 17:34:25 -05:00
FoxxMD
181390f0eb fix: Correct error description for reading a config file
Was using copy-pasted statement from wiki error which was confusing
2021-12-01 09:33:03 -05:00
FoxxMD
a8c7b1dac9 Interim implementation of repost rule 2021-11-30 20:42:40 -05:00
FoxxMD
fd5a92758d Merge branch 'edge' 2021-11-28 19:43:20 -05:00
FoxxMD
027199d788 docs(heroku): Add information on dyno usage 2021-11-15 17:27:42 -05:00
FoxxMD
2a9f01b928 fix(heroku): Add OPERATOR to heroku deploy template 2021-11-15 17:26:34 -05:00
FoxxMD
cf54502f0d fix(heroku): Add redirect uri env to heroku deploy template 2021-11-15 17:26:34 -05:00
FoxxMD
2a3663ccc9 refactor(heroku): heroku-specific docker change
* Add image for web and worker
* Refactor port usage to conform to heroku's usage
2021-11-15 17:26:34 -05:00
FoxxMD
dc2eeffcb5 fix(bot): Remove own profile "sub" from default moderated subreddits to run on
The user profile for the bot account is returning as a moderated "subreddit" EX (/u/ContextMotBot) but its not useable by CM because it does not have a wiki and users don't use this as a sub. Filter it out of moderated subreddits when using the default behavior.
2021-11-15 13:19:21 -05:00
253 changed files with 13840 additions and 4372 deletions

View File

@@ -1,8 +1,8 @@
node_modules
Dockerfile
.dockerignore
.gitignore
.git
src/logs
/docs
.github
/docs/
/node_modules/

2
.github/FUNDING.yml vendored Normal file
View File

@@ -0,0 +1,2 @@
github: [FoxxMD]
custom: ["bitcoincash:qqmpsh365r8n9jhp4p8ks7f7qdr7203cws4kmkmr8q"]

10
.gitignore vendored
View File

@@ -381,4 +381,14 @@ dist
.pnp.*
**/src/**/*.js
!src/Web/assets/public/yaml/*
**/src/**/*.map
/**/*.sqlite
/**/*.bak
*.yaml
*.json5
!src/Schema/*.json
!docs/**/*.json5
!docs/**/*.yaml
!docs/**/*.json

View File

@@ -1,15 +1,17 @@
FROM node:16-alpine3.14
FROM node:16-alpine3.14 as base
ENV TZ=Etc/GMT
# vips required to run sharp library for image comparison
RUN echo "http://dl-4.alpinelinux.org/alpine/v3.14/community" >> /etc/apk/repositories \
&& apk --update add vips
&& apk --no-cache add vips
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
WORKDIR /usr/app
FROM base as build
COPY package*.json ./
COPY tsconfig.json .
@@ -17,7 +19,13 @@ RUN npm install
ADD . /usr/app
RUN npm run build
RUN npm run build && rm -rf node_modules
FROM base as app
COPY --from=build /usr/app /usr/app
RUN npm install --production
ENV NPM_CONFIG_LOGLEVEL debug

View File

@@ -1,6 +1,7 @@
[![Latest Release](https://img.shields.io/github/v/release/foxxmd/context-mod)](https://github.com/FoxxMD/context-mod/releases)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Docker Pulls](https://img.shields.io/docker/pulls/foxxmd/context-mod)](https://hub.docker.com/r/foxxmd/context-mod)
# ContextMod [![Latest Release](https://img.shields.io/github/v/release/foxxmd/context-mod)](https://github.com/FoxxMD/context-mod/releases) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![Docker Pulls](https://img.shields.io/docker/pulls/foxxmd/context-mod)](https://hub.docker.com/r/foxxmd/context-mod)
<img src="/docs/logo.png" align="right"
alt="ContextMod logo" width="180" height="176">
**Context Mod** (CM) is an event-based, [reddit](https://reddit.com) moderation bot built on top of [snoowrap](https://github.com/not-an-aardvark/snoowrap) and written in [typescript](https://www.typescriptlang.org/).
@@ -19,14 +20,15 @@ Some feature highlights:
* Default/no configuration runs "All In One" behavior
* Additional configuration allows web interface to connect to multiple servers
* Each server instance can run multiple reddit accounts as bots
* **Per-subreddit configuration** is handled by JSON stored in the subreddit wiki
* Any text-based actions (comment, submission, message, usernotes, ban, etc...) can be configured via a wiki page or raw text in JSON and support [mustache](https://mustache.github.io) [templating](/docs/actionTemplating.md)
* **Per-subreddit configuration** is handled by YAML (**like automoderator!**) or JSON stored in the subreddit wiki
* Any text-based actions (comment, submission, message, usernotes, ban, etc...) can be configured via a wiki page or raw text and supports [mustache](https://mustache.github.io) [templating](/docs/actionTemplating.md)
* 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.
* Support Activity skipping based on:
* Author criteria (name, css flair/text, age, karma, moderator status, and [Toolbox User Notes](https://www.reddit.com/r/toolbox/wiki/docs/usernotes))
* Activity state (removed, locked, distinguished, etc.)
* Rules and Actions support named references (write once, reference anywhere)
* [**Image Comparisons**](/docs/imageComparison.md) via fingerprinting and/or pixel differences
* [**Repost detection**](/docs/examples/repost) with support for external services (youtube, etc...)
* Global/subreddit-level **API caching**
* Support for [Toolbox User Notes](https://www.reddit.com/r/toolbox/wiki/docs/usernotes) as criteria or Actions (writing notes)
* Docker container support
@@ -84,7 +86,7 @@ See the [Moderator's Getting Started Guide](/docs/gettingStartedMod.md)
## Configuration and Documentation
Context Bot's configuration can be written in JSON, [JSON5](https://json5.org/) or YAML. Its schema conforms to [JSON Schema Draft 7](https://json-schema.org/). Additionally, many **operator** settings can be passed via command line or environmental variables.
Context Bot's configuration can be written in YAML (like automoderator) or [JSON5](https://json5.org/). Its schema conforms to [JSON Schema Draft 7](https://json-schema.org/). Additionally, many **operator** settings can be passed via command line or environmental variables.
* For **operators** (running the bot instance) see the [Operator Configuration](/docs/operatorConfiguration.md) guide
* For **moderators** consult the [app schema and examples folder](/docs/#configuration-and-usage)
@@ -125,7 +127,7 @@ Moderator view/invite and authorization:
A built-in editor using [monaco-editor](https://microsoft.github.io/monaco-editor/) makes editing configurations easy:
* Automatic JSON syntax validation and formatting
* Automatic JSON or YAML syntax validation and formatting
* Automatic Schema (subreddit or operator) validation
* All properties are annotated via hover popups
* Unauthenticated view via `yourdomain.com/config`

View File

@@ -17,12 +17,22 @@
"REFRESH_TOKEN": {
"description": "Refresh token retrieved from authenticating an account with your Reddit Application",
"value": "",
"required": true
"required": false
},
"ACCESS_TOKEN": {
"description": "Access token retrieved from authenticating an account with your Reddit Application",
"value": "",
"required": true
"required": false
},
"REDIRECT_URI": {
"description": "Redirect URI you specified when creating your Reddit Application. Required if you want to use the web interface. In the provided example replace 'your-heroku-app-name' with the name of your HEROKU app.",
"value": "https://your-heroku-6app-name.herokuapp.com/callback",
"required": false
},
"OPERATOR": {
"description": "Your reddit username WITHOUT any prefixes EXAMPLE /u/FoxxMD => FoxxMD. Specified user will be recognized as an admin.",
"value": "",
"required": false
},
"WIKI_CONFIG": {
"description": "Relative url to contextbot wiki page EX https://reddit.com/r/subreddit/wiki/<path>",

View File

@@ -102,6 +102,7 @@ Find detailed descriptions of all the Rules, with examples, below:
* [History](/docs/examples/history)
* [Author](/docs/examples/author)
* [Regex](/docs/examples/regex)
* [Repost](/docs/examples/repost)
### Rule Set
@@ -119,6 +120,15 @@ It consists of:
* **rules** -- The **Rules** for the Rule Set.
Example
YAML
```yaml
condition: AND
# rules are an array
rules:
- aRule
```
JSON
```json5
{
"condition": "AND",

View File

@@ -17,7 +17,28 @@ Examples of all of the above
<details>
```yaml
# count, last 100 activities
window: 100
# duration, last 10 days
window: 10 days
# duration object, last 2 months and 5 days
window:
months: 2
days: 5
# iso 8601 string, last 15 minutes
window: PT15M
# ActivityWindowCriteria, last 100 activities or 6 weeks of activities (whichever is found first)
window:
count: 100
duration: 6 weeks
```
```json5
// count, last 100 activities
{
"window": 100
@@ -49,6 +70,7 @@ Examples of all of the above
}
}
```
</details>
## Types of Ranges
@@ -95,6 +117,7 @@ If you need to specify multiple units of time for your duration you can instead
Example
JSON
```json
{
"days": 4,
@@ -102,6 +125,13 @@ Example
"minutes": 20
}
```
YAML
```yaml
window:
days: 4
hours: 6
minutes: 20
```
##### An ISO 8601 duration string
@@ -119,6 +149,7 @@ This is an object that lets you specify more granular conditions for your range.
The full object looks like this:
JSON
```json
{
"count": 100,
@@ -130,6 +161,19 @@ The full object looks like this:
}
}
```
YAML
```yaml
window:
count: 100
duration: 10 days
satisfyOn: any
subreddits:
include:
- mealtimevideos
- pooptimevideos
exclude:
- videos
```
### Specifying Range
@@ -142,7 +186,9 @@ If both range properties are specified then the value `satisfyOn` determines how
If **any** then Activities will be retrieved until one of the range properties is met, **whichever occurs first.**
Example
Example
JSON
```json
{
"count": 80,
@@ -150,6 +196,13 @@ Example
"satisfyOn": "any"
}
```
YAML
```yaml
window:
count: 80
duration: 90 days
satisfyOn: any
```
Activities are retrieved in chunks of 100 (or `count`, whichever is smaller)
* If 90 days of activities returns only 40 activities => returns 40 activities
@@ -160,6 +213,8 @@ Activities are retrieved in chunks of 100 (or `count`, whichever is smaller)
If **all** then both ranges must be satisfied. Effectively, whichever range produces the most Activities will be the one that is used.
Example
JSON
```json
{
"count": 100,
@@ -167,6 +222,13 @@ Example
"satisfyOn": "all"
}
```
YAML
```yaml
window:
count: 100
duration: 90 days
satisfyOn: all
```
Activities are retrieved in chunks of 100 (or `count`, whichever is smaller)
* If at 90 days of activities => 40 activities retrieved
@@ -187,6 +249,8 @@ You may filter retrieved Activities using an array of subreddits.
Use **include** to specify which subreddits should be included from results
Example where only activities from /r/mealtimevideos and /r/modsupport will be returned
JSON
```json
{
"count": 100,
@@ -196,7 +260,17 @@ Example where only activities from /r/mealtimevideos and /r/modsupport will be r
"include": ["mealtimevideos","modsupport"]
}
}
```
YAML
```yaml
window:
count: 100
duruation: 90 days
satisfyOn: any
subreddits:
include:
- mealtimevideos
- modsupport
```
#### Exclude
@@ -204,6 +278,8 @@ Example where only activities from /r/mealtimevideos and /r/modsupport will be r
Use **exclude** to specify which subreddits should NOT be in the results
Example where activities from /r/mealtimevideos and /r/modsupport will not be returned in results
JSON
```json
{
"count": 100,
@@ -214,4 +290,15 @@ Example where activities from /r/mealtimevideos and /r/modsupport will not be re
}
}
```
YAML
```yaml
window:
count: 100
duruation: 90 days
satisfyOn: any
subreddits:
exclude:
- mealtimevideos
- modsupport
```
**Note:** `exclude` will be ignored if `include` is also present.

View File

@@ -17,6 +17,8 @@ This directory contains example of valid, ready-to-go configurations for Context
* [History](/docs/examples/history)
* [Author](/docs/examples/author)
* [Regex](/docs/examples/regex)
* [Repost](/docs/examples/repost)
* [Author and post flairs](/docs/examples/onlyfansFlair)
* [Toolbox User Notes](/docs/examples/userNotes)
* [Advanced Concepts](/docs/examples/advancedConcepts)
* [Rule Sets](/docs/examples/advancedConcepts/ruleSets.json5)

View File

@@ -1,6 +1,6 @@
### Named Rules
See [ruleNameReuse.json5](/docs/examples/advancedConcepts/ruleNameReuse.json5)
See **Rule Name Reuse Examples [YAML](/docs/examples/advancedConcepts/ruleNameReuse.yaml) | [JSON](/docs/examples/advancedConcepts/ruleNameReuse.json5)**
### Check Order
@@ -23,7 +23,7 @@ The `rules` array on a `Checks` can contain both `Rule` objects and `RuleSet` ob
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](/docs/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%2Fcontext-mod%2Fmaster%2Fsrc%2FSchema%2FApp.json).
See **ruleSets [YAML](/docs/examples/advancedConcepts/ruleSets.yaml) | [JSON](/docs/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%2Fcontext-mod%2Fmaster%2Fsrc%2FSchema%2FApp.json).
### Rule Order

View File

@@ -0,0 +1,52 @@
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: 90 days
- 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:
- threshold: '>= 1'
subreddits:
- DeFreeKarma
- FreeKarma4U
- FreeKarma4You
- upvote
window: 7 days
actions:
- kind: report
content: >-
Submission posted {{rules.freekarmasub.totalCount}} times in karma
{{rules.freekarmasub.subCount}} subs over
{{rules.freekarmasub.window}}: {{rules.freekarmasub.subSummary}}

View File

@@ -0,0 +1,53 @@
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: 90 days
- 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: 90 days
- threshold: '> 10%'
thresholdOn: submissions
window: 100
lookAt: media
- name: lowOrOpComm
kind: history
criteriaJoin: OR
criteria:
- window: 90 days
comment: < 50%
- window: 90 days
comment: '> 40% OP'
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

View File

@@ -10,5 +10,5 @@ Consult the [schema](https://json-schema.app/view/%23/%23%2Fdefinitions%2FCheckJ
### Examples
* [Self Promotion as percentage of all Activities](/docs/examples/attribution/redditSelfPromoAll.json5) - Check if Author is submitting much more than they comment.
* [Self Promotion as percentage of Submissions](/docs/examplesm/attribution/redditSelfPromoSubmissionsOnly.json5) - Check if any of Author's aggregated submission origins are >10% of their submissions
* Self Promotion as percentage of all Activities [YAML](/docs/examples/attribution/redditSelfPromoAll.yaml) | [JSON](/docs/examples/attribution/redditSelfPromoAll.json5) - Check if Author is submitting much more than they comment.
* Self Promotion as percentage of Submissions [YAML](/docs/examples/attribution/redditSelfPromoSubmissionsOnly.yaml) | [JSON](/docs/examplesm/attribution/redditSelfPromoSubmissionsOnly.json5) - Check if any of Author's aggregated submission origins are >10% of their submissions

View File

@@ -0,0 +1,27 @@
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: '> 10%' # threshold can be a percent or an absolute number
# 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: 90 days
- 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}}

View File

@@ -0,0 +1,24 @@
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: '> 10%' # threshold can be a percent or an absolute number
thresholdOn: submissions # calculate percentage of submissions, rather than entire history (submissions & comments)
window: 90 days # look at last 90 days of Author's activities (comments and submissions)
- threshold: '> 10%'
thresholdOn: submissions
window: 100 # look at Author's last 100 activities (comments and submissions)
actions:
- kind: report
content: >-
{{rules.attr10sub.largestPercent}}% of
{{rules.attr10sub.activityTotal}} items over
{{rules.attr10sub.window}}

View File

@@ -18,10 +18,10 @@ Consult the [schema](https://json-schema.app/view/%23%2Fdefinitions%2FAuthorRule
### Examples
* Basic examples
* [Flair new user Submission](/docs/examples/author/flairNewUserSubmission.json5) - If the Author does not have the `vet` flair then flair the Submission with `New User`
* [Flair vetted user Submission](/docs/examples/author/flairNewUserSubmission.json5) - If the Author does have the `vet` flair then flair the Submission with `Vetted`
* Flair new user Submission [YAML](/docs/examples/author/flairNewUserSubmission.yaml) | [JSON](/docs/examples/author/flairNewUserSubmission.json5) - If the Author does not have the `vet` flair then flair the Submission with `New User`
* Flair vetted user Submission [YAML](/docs/examples/author/flairNewUserSubmission.yaml) | [JSON](/docs/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](/docs/examples/author/flairNewUserSubmission.json5) - Short-circuit the Check if the Author has the `vet` flair
* Ignore vetted user [YAML](/docs/examples/author/flairNewUserSubmission.yaml) | [JSON](/docs/examples/author/flairNewUserSubmission.json5) - Short-circuit the Check if the Author has the `vet` flair
## Filter
@@ -35,4 +35,4 @@ All **Rules** and **Checks** have an optional `authorIs` property that takes an
### Examples
* [Skip recent activity check based on author](/docs/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.
* Skip recent activity check based on author [YAML](/docs/examples/author/authorFilter.yaml) | [JSON](/docs/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.

View File

@@ -0,0 +1,48 @@
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:
- threshold: '>= 1'
subreddits:
- DeFreeKarma
- FreeKarma4U
window: 7 days
- 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
authorIs:
# 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:
- threshold: '>= 1'
subreddits:
- dankmemes
window: 7 days
actions:
- kind: report
content: >-
Author has posted in free karma sub, or in /r/dankmemes and does not
have meme flair in this subreddit

View File

@@ -0,0 +1,16 @@
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

View File

@@ -0,0 +1,16 @@
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

View File

@@ -0,0 +1,45 @@
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: 90 days
- threshold: '> 10%'
window: 100
- name: freekarma
kind: recentActivity
lookAt: submissions
thresholds:
- threshold: '>= 1'
subreddits:
- DeFreeKarma
- FreeKarma4U
window: 7 days
- name: memes
kind: recentActivity
lookAt: submissions
thresholds:
- threshold: '>= 3'
subreddits:
- dankmemes
window: 7 days
# 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

View File

@@ -9,5 +9,5 @@ Consult the [schema](https://json-schema.app/view/%23%2Fdefinitions%2FHistoryJSO
### Examples
* [Low Comment Engagement](/docs/examples/history/lowEngagement.json5) - Check if Author is submitting much more than they comment.
* [OP Comment Engagement](/docs/examples/history/opOnlyEngagement.json5) - Check if Author is mostly engaging only in their own content
* Low Comment Engagement [YAML](/docs/examples/history/lowEngagement.yaml) | [JSON](/docs/examples/history/lowEngagement.json5) - Check if Author is submitting much more than they comment.
* OP Comment Engagement [YAML](/docs/examples/history/opOnlyEngagement.yaml) | [JSON](/docs/examples/history/opOnlyEngagement.json5) - Check if Author is mostly engaging only in their own content

View File

@@ -0,0 +1,21 @@
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:
- comment: '< 30%'
window:
# get author's last 90 days of activities or 100 activities, whichever is less
duration: 90 days
count: 100
# trigger if less than 30% of their activities in this time period are comments
actions:
- kind: report
content: >-
Low engagement: comments were {{rules.lowcomm.commentPercent}} of
{{rules.lowcomm.activityTotal}} over {{rules.lowcomm.window}}

View File

@@ -0,0 +1,22 @@
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:
# trigger if more than 60% of their activities in this time period are comments as OP
- comment: '> 60% OP'
window:
# get author's last 90 days of activities or 100 activities, whichever is less
duration: 90 days
count: 100
actions:
- kind: report
content: >-
Selfish OP: {{rules.oponly.opPercent}} of
{{rules.oponly.commentTotal}} comments over {{rules.oponly.window}}
are as OP

View File

@@ -0,0 +1,9 @@
# Flair users and submissions
Flair users and submissions based on certain keywords from submitter's profile.
Consult [User Flair schema](https://json-schema.app/view/%23%2Fdefinitions%2FUserFlairActionJson?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Fcontext-mod%2Fmaster%2Fsrc%2FSchema%2FApp.json) and [Submission Flair schema](https://json-schema.app/view/%23%2Fdefinitions%2FFlairActionJson?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Fcontext-mod%2Fmaster%2Fsrc%2FSchema%2FApp.json) for a complete reference of the rule's properties.
### Examples
* OnlyFans submissions [YAML](/docs/examples/onlyFansFlair/onlyFansFlair.yaml) | [JSON](/docs/examples/onlyfansFlair/onlyfansFlair.json5) - Check whether submitter has typical OF keywords in their profile and flair both author + submission accordingly.

View File

@@ -0,0 +1,68 @@
{
"checks": [
{
"name": "Flair OF submitters",
"description": "Flair submission as OF if user does not have Verified flair and has certain keywords in their profile",
"kind": "submission",
"authorIs": {
"exclude": [
{
"flairCssClass": ["verified"]
}
]
},
"rules": [
{
"name": "OnlyFans strings in description",
"kind": "author",
"include": [
{
"description": [
"/(cashapp|allmylinks|linktr|onlyfans\\.com)/i",
"/(see|check|my|view) (out|of|onlyfans|kik|skype|insta|ig|profile|links)/i",
"my links",
"$"
]
}
]
}
],
"actions": [
{
"name": "Set OnlyFans user flair",
"kind": "userflair",
"flair_template_id": "put-your-onlyfans-user-flair-id-here"
},
{
"name":"Set OF Creator SUBMISSION flair",
"kind": "flair",
"flair_template_id": "put-your-onlyfans-post-flair-id-here"
}
]
},
{
"name": "Flair posts of OF submitters",
"description": "Flair submission as OnlyFans if submitter has OnlyFans userflair (override post flair set by submitter)",
"kind": "submission",
"rules": [
{
"name": "Include OF submitters",
"kind": "author",
"include": [
{
"flairCssClass": ["onlyfans"]
}
]
}
],
"actions": [
{
"name":"Set OF Creator SUBMISSION flair",
"kind": "flair",
"flair_template_id": "put-your-onlyfans-post-flair-id-here"
}
]
}
]
}

View File

@@ -0,0 +1,38 @@
checks:
- name: Flair OF submitters
description: Flair submission as OF if user does not have Verified flair and has
certain keywords in their profile
kind: submission
authorIs:
exclude:
- flairCssClass:
- verified
rules:
- name: OnlyFans strings in description
kind: author
include:
- description:
- '/(cashapp|allmylinks|linktr|onlyfans\.com)/i'
- '/(see|check|my|view) (out|of|onlyfans|kik|skype|insta|ig|profile|links)/i'
- my links
- "$"
actions:
- name: Set OnlyFans user flair
kind: userflair
flair_template_id: put-your-onlyfans-user-flair-id-here
- name: Set OF Creator SUBMISSION flair
kind: flair
flair_template_id: put-your-onlyfans-post-flair-id-here
- name: Flair posts of OF submitters
description: Flair submission as OnlyFans if submitter has OnlyFans userflair (override post flair set by submitter)
kind: submission
rules:
- name: Include OF submitters
kind: author
include:
- flairCssClass:
- onlyfans
actions:
- name: Set OF Creator SUBMISSION flair
kind: flair
flair_template_id: put-your-onlyfans-post-flair-id-here

View File

@@ -6,5 +6,5 @@ Consult the [schema](https://json-schema.app/view/%23%2Fdefinitions%2FRecentActi
### Examples
* [Free Karma Subreddits](/docs/examples/recentActivity/freeKarma.json5) - Check if the Author has recently posted in any "free karma" subreddits
* [Submission in Free Karma Subreddits](/docs/examples/recentActivity/freeKarmaOnSubmission.json5) - Check if the Author has posted the Submission this check is running on in any "free karma" subreddits recently
* Free Karma Subreddits [YAML](/docs/examples/recentActivity/freeKarma.yaml) | [JSON](/docs/examples/recentActivity/freeKarma.json5) - Check if the Author has recently posted in any "free karma" subreddits
* Submission in Free Karma Subreddits [YAML](/docs/examples/recentActivity/freeKarmaOnSubmission.yaml) | [JSON](/docs/examples/recentActivity/freeKarmaOnSubmission.json5) - Check if the Author has posted the Submission this check is running on in any "free karma" subreddits recently

View File

@@ -0,0 +1,27 @@
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: comments
useSubmissionAsReference: false
thresholds:
# if the number of activities (sub/comment) found CUMULATIVELY in the subreddits listed is
# equal to or greater than 1 then the rule is triggered
- threshold: '>= 1'
subreddits:
- DeFreeKarma
- FreeKarma4U
- FreeKarma4You
- upvote
window: 7 days
actions:
- kind: report
content: >-
{{rules.freekarma.totalCount}} activities in karma
{{rules.freekarma.subCount}} subs over {{rules.freekarma.window}}:
{{rules.freekarma.subSummary}}

View File

@@ -0,0 +1,26 @@
checks:
- name: Free Karma On Submission Alert
description: Check if author has posted this submission in 'freekarma' subreddits
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:
- threshold: '>= 1'
subreddits:
- DeFreeKarma
- FreeKarma4U
- FreeKarma4You
- upvote
window: 7 days
actions:
- kind: report
content: >-
Submission posted {{rules.freekarmasub.totalCount}} times in karma
{{rules.freekarmasub.subCount}} subs over
{{rules.freekarmasub.window}}: {{rules.freekarmasub.subSummary}}

View File

@@ -11,10 +11,12 @@ Which can then be used in conjunction with a [`window`](https://github.com/FoxxM
### Examples
* [Trigger if regex matches against the current activity](/docs/examples/regex/matchAnyCurrentActivity.json5)
* [Trigger if regex matches 5 times against the current activity](/docs/examples/regex/matchThresholdCurrentActivity.json5)
* [Trigger if regex matches against any part of a Submission](/docs/examples/regex/matchSubmissionParts.json5)
* [Trigger if regex matches any of Author's last 10 activities](/docs/examples/regex/matchHistoryActivity.json5)
* [Trigger if regex matches at least 3 of Author's last 10 activities](/docs/examples/regex/matchActivityThresholdHistory.json5)
* [Trigger if there are 5 regex matches in the Author's last 10 activities](/docs/examples/regex/matchTotalHistoryActivity.json5)
* [Trigger if there are 5 regex matches in the Author's last 10 comments](/docs/examples/regex/matchSubsetHistoryActivity.json5)
* Trigger if regex matches against the current activity - [YAML](/docs/examples/regex/matchAnyCurrentActivity.yaml) | [JSON](/docs/examples/regex/matchAnyCurrentActivity.json5)
* Trigger if regex matches 5 times against the current activity - [YAML](/docs/examples/regex/matchThresholdCurrentActivity.yaml) | [JSON](/docs/examples/regex/matchThresholdCurrentActivity.json5)
* Trigger if regex matches against any part of a Submission - [YAML](/docs/examples/regex/matchSubmissionParts.yaml) | [JSON](/docs/examples/regex/matchSubmissionParts.json5)
* Trigger if regex matches any of Author's last 10 activities - [YAML](/docs/examples/regex/matchHistoryActivity.yaml) | [JSON](/docs/examples/regex/matchHistoryActivity.json5)
* Trigger if regex matches at least 3 of Author's last 10 activities - [YAML](/docs/examples/regex/matchActivityThresholdHistory.json5) | [JSON](/docs/examples/regex/matchActivityThresholdHistory.json5)
* Trigger if there are 5 regex matches in the Author's last 10 activities - [YAML](/docs/examples/regex/matchTotalHistoryActivity.yaml) | [JSON](/docs/examples/regex/matchTotalHistoryActivity.json5)
* Trigger if there are 5 regex matches in the Author's last 10 comments - [YAML](/docs/examples/regex/matchSubsetHistoryActivity.yaml) | [JSON](/docs/examples/regex/matchSubsetHistoryActivity.json5)
* Remove comments that are spamming discord links - [YAML](/docs/examples/regex/removeDiscordSpam.yaml) | [JSON](/docs/examples/regex/removeDiscordSpam.json5)
* Differs from just using automod because this config can allow one-off/organic links from users who DO NOT spam discord links but will still remove the comment if the user is spamming them

View File

@@ -0,0 +1,13 @@
name: swear
kind: regex
criteria:
# triggers if more than 3 activities in the last 10 match the regex
- regex: '/fuck|shit|damn/'
# this differs from "totalMatchThreshold"
#
# activityMatchThreshold => # of activities from window must match regex
# totalMatchThreshold => # of matches across all activities from window must match regex
activityMatchThreshold: '> 3'
# if `window` is specified it tells the rule to check the current activity as well as the activities returned from `window`
# learn more about `window` here https://github.com/FoxxMD/context-mod/blob/master/docs/activitiesWindow.md
window: 10

View File

@@ -0,0 +1,6 @@
name: swear
kind: regex
criteria:
- regex: '/fuck|shit|damn/'
# if "matchThreshold" is not specified it defaults to this -- default behavior is to trigger if there are any matches
#matchThreshold: "> 0"

View File

@@ -0,0 +1,8 @@
name: swear
kind: regex
criteria:
# triggers if any activity in the last 10 (including current activity) match the regex
- regex: '/fuck|shit|damn/'
# if `window` is specified it tells the rule to check the current activity as well as the activities returned from `window`
# learn more about `window` here https://github.com/FoxxMD/context-mod/blob/master/docs/activitiesWindow.md
window: 10

View File

@@ -0,0 +1,11 @@
name: swear
kind: regex
criteria:
- regex: '/fuck|shit|damn/'
# triggers if the current activity has more than 0 matches
# if the activity is a submission then matches against title, body, and url
# if "testOn" is not provided then `title, body` are the defaults
testOn:
- title
- body
- url

View File

@@ -0,0 +1,16 @@
name: swear
kind: regex
criteria:
# triggers if there are more than 5 regex matches in the last 10 activities (comments only)
- regex: '/fuck|shit|damn/'
# this differs from "activityMatchThreshold"
#
# activityMatchThreshold => # of activities from window must match regex
# totalMatchThreshold => # of matches across all activities from window must match regex
totalMatchThreshold: '> 5'
# if `window` is specified it tells the rule to check the current activity as well as the activities returned from `window`
# learn more about `window` here https://github.com/FoxxMD/context-mod/blob/master/docs/activitiesWindow.md
window: 10
# determines which activities from window to consider
# defaults to "all" (submissions and comments)
lookAt: comments

View File

@@ -0,0 +1,6 @@
name: swear
kind: regex
criteria:
- regex: '/fuck|shit|damn/'
# triggers if current activity has greater than 5 matches
matchThreshold: '> 5'

View File

@@ -0,0 +1,13 @@
name: swear
kind: regex
criteria:
# triggers if there are more than 5 regex matches in the last 10 activities (comments or submission)
- regex: '/fuck|shit|damn/'
# this differs from "activityMatchThreshold"
#
# activityMatchThreshold => # of activities from window must match regex
# totalMatchThreshold => # of matches across all activities from window must match regex
totalMatchThreshold: '> 5'
# if `window` is specified it tells the rule to check the current activity as well as the activities returned from `window`
# learn more about `window` here https://github.com/FoxxMD/context-mod/blob/master/docs/activitiesWindow.md
window: 10

View File

@@ -0,0 +1,73 @@
{
"checks": [
{
"name": "remove discord spam",
"notifyOnTrigger": true,
"description": "remove comments from users who are spamming discord links",
"kind": "comment",
"authorIs": {
"exclude": [
{
"isMod": true
}
]
},
"itemIs": [
{
"removed": false,
"approved": false,
}
],
"condition": "OR",
"rules": [
{
// set to false if you want to allow comments with a discord link ONLY IF
// the author doesn't have a history of spamming discord links
// -- basically allows one-off/organic discord links
"enable": true,
"name": "linkOnlySpam",
"kind": "regex",
"criteria": [
{
"name": "only link",
"regex": "/^.*(discord\\.gg\\/[\\w\\d]+)$/i",
}
]
},
{
"condition": "AND",
"rules": [
{
"name": "linkAnywhereSpam",
"kind": "regex",
"criteria": [
{
"name": "contains link anywhere",
"regex": "/^.*(discord\\.gg\\/[\\w\\d]+).*$/i",
}
]
},
{
"name": "linkAnywhereHistoricalSpam",
"kind": "regex",
"criteria": [
{
"name": "contains links anywhere historically",
"regex": "/^.*(discord\\.gg\\/[\\w\\d]+).*$/i",
"totalMatchThreshold": ">= 3",
"lookAt": "comments",
"window": 10
}
]
}
]
}
],
"actions": [
{
"kind": "remove"
}
]
}
]
}

View File

@@ -0,0 +1,36 @@
checks:
- name: remove discord spam
notifyOnTrigger: true
description: remove comments from users who are spamming discord links
kind: comment
authorIs:
exclude:
- isMod: true
itemIs:
- removed: false
approved: false
condition: OR
rules:
- enable: true
name: linkOnlySpam
kind: regex
criteria:
- name: only link
regex: '/^.*(discord\.gg\/[\w\d]+)$/i'
- condition: AND
rules:
- name: linkAnywhereSpam
kind: regex
criteria:
- name: contains link anywhere
regex: '/^.*(discord\.gg\/[\w\d]+).*$/i'
- name: linkAnywhereHistoricalSpam
kind: regex
criteria:
- name: contains links anywhere historically
regex: '/^.*(discord\.gg\/[\w\d]+).*$/i'
totalMatchThreshold: '>= 3'
lookAt: comments
window: 10
actions:
- kind: remove

View File

@@ -45,5 +45,5 @@ With only `gapAllowance: 2` this rule **would trigger** because the the 1 and 2
## Examples
* [Crosspost Spamming](/docs/examples/repeatActivity/crosspostSpamming.json5) - Check if an Author is spamming their Submissions across multiple subreddits
* [Burst-posting](/docs/examples/repeatActivity/burstPosting.json5) - Check if Author is crossposting their Submissions in short bursts
* Crosspost Spamming [JSON](/docs/examples/repeatActivity/crosspostSpamming.json5) | [YAML](/docs/examples/repeatActivity/crosspostSpamming.yaml) - Check if an Author is spamming their Submissions across multiple subreddits
* Burst-posting [JSON](/docs/examples/repeatActivity/burstPosting.json5) | [YAML](/docs/examples/repeatActivity/burstPosting.yaml) - Check if Author is crossposting their Submissions in short bursts

View File

@@ -0,0 +1,23 @@
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 or 100 submissions
window:
duration: 7 days
count: 100
actions:
- kind: report
content: >-
Author has burst-posted this link {{rules.burstpost.largestRepeat}}
times over {{rules.burstpost.window}}

View File

@@ -0,0 +1,19 @@
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: 7 days
actions:
- kind: report
content: >-
Author has posted this link {{rules.xpostspam.largestRepeat}} times
over {{rules.xpostspam.window}}

View File

@@ -0,0 +1,927 @@
The **Repost** rule is used to find reposts for both **Submissions** and **Comments**, depending on what type of **Check** it is used on.
Note: This rule is for searching **all of Reddit** for reposts, as opposed to just the Author of the Activity being checked. If you only want to check for reposts by the Author of the Activity being checked you should use the [Repeat Activity](/docs/examples/repeatActivity) rule.
# TLDR
Out of the box CM generates a repost rule with sensible default behavior without any configuration. You do not need to configure any of below options (facets, modifiers, criteria) yourself in order to have a working repost rule. Default behavior is as follows...
* When looking for Submission reposts CM will find any Submissions with
* a very similar title
* or independent of title...
* any crossposts/duplicates
* any submissions with the exact URL
* When looking for Comment reposts CM will do the above AND THEN
* compare the top 50 most-upvoted comments from the top 10 most-upvoted Submissions against the comment being checked
* compare any items found from external source (Youtube comments, etc...) against the comment being checked
# Configuration
## Search Facets
ContextMod has several ways to search for reposts -- all of which look at different elements of a Submission in order to find repost candidates. You can define any/all of these **Search Facets** you want to use to search Reddit inside the configuration for the Repost Rule in the `searchOn` property.
### Usage
Facets are specified in the `searchOn` array property within the rule's configuration.
**String**
Specify one or more types of facets as a string to use their default configurations
<details>
YAML
```yaml
kind: repost
criteria:
- searchOn:
- title
- url
- crossposts
```
JSON
```json5
{
"kind": "repost",
"criteria": [
{
// ...
"searchOn": ["title", "url", "crossposts"],
// ....
}
]
}
```
</details>
**Object**
**string** and object configurations can be mixed
<details>
```yaml
kind: repost
criteria:
- searchOn:
- title
- kind: url
matchScore: 90
- external
```
```json5
{
"kind": "repost",
"criteria": [
{
// ...
"searchOn": [
"title",
{
"kind": "url",
// could also specify multiple types to use the same config for all
//"kind": ["url", "duplicates"]
"matchScore": 90,
//...
},
"external"
],
// ....
}
]
}
```
</details>
### Facet Types
* **title** -- search reddit for Submissions with a similar title
* **url** -- search reddit for Submissions with the same URL
* **duplicates** -- get all Submissions **reddit has identified** as duplicates that are **NOT** crossposts
* these are found under *View discussions in other communities* (new reddit) or *other discussions* (old reddit) on the Submission
* **crossposts** -- get all Submissions where the current Submission is the source of an **official** crosspost
* this differs from duplicates in that crossposts use reddit's built-in crosspost functionality, respect subreddit crosspost rules, and link back to the original Submission
* **external** -- get items from the Submission's link source that may be reposted (currently implemented for **Comment Checks** only)
* When the Submission link is for...
* **Youtube** -- get top comments on video by replies/like count
* **NOTE:** An **API Key** for the [Youtube Data API](https://developers.google.com/youtube/v3) must be provided for this facet to work. This can be provided by the operator alongside [bot credentials](/docs/operatorConfiguration.md) or in the top-level `credentials` property for a [subreddit configuration.](https://json-schema.app/view/%23?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Freddit-context-bot%2Fmaster%2Fsrc%2FSchema%2FApp.json)
### Facet Modifiers
For all **Facets**, except for **external**, there are options that be configured to determine if the found Submissions is a "valid" repost IE filtering. These options can be configured **per facet**.
* **matchScore** -- The percentage, as a whole number, of a repost title that must match the title being checked in order to consider both a match
* **minWordCount** -- The minimum number of words a title must have
* **caseSensitive** -- If the match comparison should be case-sensitive (defaults to `false`)
Additionally, the current Activity's title and/or each repost's title can be transformed before matching:
* **transformations** -- An array of SearchAndReplace objects used to transform the repost's title
* **transformationsActivity** -- An array of SearchAndReplace objects used to transform the current Activity's title
#### Modifier Defaults
To make facets easier to use without configuration sensible defaults are applied to each when no other configuration is defined...
* **title**
* `matchScore: 85` -- The candidate repost's title must be at least 85% similar to the current Activity's title
* `minWordCount: 2` -- The candidate repost's title must have at least 2 words
For `url`,`duplicates`, and `crossposts` the only default is `matchScore: 0` because the assumption is you want to treat any actual dups/x-posts or exact URLs as reposts, regardless of their title.
## Additional Criteria Properties
A **criteria** object may also specify some additional tests to run against the reposts found from searching.
### For Submissions and Comments
#### Occurrences
Define a set of criteria to test against the **number of reposts**, **time reposts were created**, or both.
##### Count
<details>
```yaml
kind: repost
criteria:
- searchOn:
- title
- url
- crossposts
occurrences:
criteria:
- count:
condition: AND
test:
- '> 3'
- <= 5
```
```json5
{
"kind": "repost",
"criteria": [
{
// ...
"searchOn": ["title", "url", "crossposts"],
"occurrences": {
"criteria": [
{
// passes if BOTH tests are true
"count": {
"condition": "AND", // default is AND
"test": [
"> 3", // TRUE if there are GREATER THAN 3 reposts found
"<= 5" // TRUE if there are LESS THAN OR EQUAL TO 5 reposts found
]
}
}
],
}
}
]
}
```
</details>
##### Time
Define a test or array of tests to run against **when reposts were created**
<details>
```yaml
kind: repost
criteria:
- searchOn:
- title
- url
- crossposts
occurrences:
criteria:
- time:
condition: AND
test:
- testOn: all
condition: '> 3 months'
```
```json5
{
"kind": "repost",
"criteria": [
{
// ...
"searchOn": [
"title",
"url",
"crossposts"
],
"occurrences": {
"criteria": [
{
time: {
// how to test array of comparisons. AND => all must pass, OR => any must pass
"condition": "AND",
"test": [
{
// which of the found reposts to test the time comparison on
//
// "all" => ALL reposts must pass time comparison
// "any" => ANY repost must pass time comparison
// "newest" => The newest (closest in time to now) repost must pass time comparison
// "oldest" => The oldest (furthest in time from now) repost must pass time comparison
//
"testOn": "all",
// Tested items must be OLDER THAN 3 months
"condition": "> 3 months"
}
]
}
}
]
},
}
]
}
```
</details>
### For Comments
When the rule is run in a **Comment Check** you may specify text comparisons (like those found in Search Facets) to run on the contents of the repost comments *against* the contents of the comment being checked.
* **matchScore** -- The percentage, as a whole number, of a repost comment that must match the comment being checked in order to consider both a match (defaults to 85% IE `85`)
* **minWordCount** -- The minimum number of words a comment must have
* **caseSensitive** -- If the match comparison should be case-sensitive (defaults to `false`)
# Examples
Examples of a *full* CM configuration, including the Repost Rule, in various scenarios. In each scenario the parts of the configuration that affect the rule are indicated.
## Submissions
When the Repost Rule is run on a **Submission Check** IE the activity being checked is a Submission.
### Default Behavior (No configuration)
This is the same behavior described in the [TLDR](#TLDR) section above -- find any submissions with:
* a very similar title (85% or more the same)
* or ignoring title...
* any crossposts/duplicates
* any submissions with the exact URL
<details>
```yaml
polling:
- unmoderated
checks:
- name: subRepost
description: Check if submission has been reposted
kind: submission
condition: AND
rules:
- kind: repost
actions:
- kind: report
content: This submission was reposted
```
```json5
{
"polling": [
"unmoderated"
],
"checks": [
{
"name": "subRepost",
"description": "Check if submission has been reposted",
// kind specifies this check is for SUBMISSIONS
"kind": "submission",
"condition": "AND",
"rules": [
// repost rule configuration is below
//
{
"kind": "repost"
},
//
// repost rule configuration is above
],
"actions": [
{
"kind": "report",
"content": "This submission was reposted"
}
]
}
]
}
```
</details>
### Search by Title Only
Find any submissions with:
* a very similar title (85% or more the same)
<details>
```yaml
polling:
- unmoderated
checks:
- name: subRepost
description: Check if submission has been reposted
kind: submission
condition: AND
rules:
- kind: repost
criteria:
- searchOn:
- title
actions:
- kind: report
content: This submission was reposted
```
```json5
{
"polling": [
"unmoderated"
],
"checks": [
{
"name": "subRepost",
"description": "Check if submission has been reposted",
// kind specifies this check is for SUBMISSIONS
"kind": "submission",
"condition": "AND",
"rules": [
// repost rule configuration is below
//
{
"kind": "repost",
"criteria": [
{
// specify only title to search on
"searchOn": [
"title" // uses default configuration since only string is specified
]
}
]
},
//
// repost rule configuration is above
],
"actions": [
{
"kind": "report",
"content": "This submission was reposted"
}
]
}
]
}
```
</details>
### Search by Title only and specify similarity percentage
* a very similar title (95% or more the same)
<details>
```yaml
polling:
- unmoderated
checks:
- name: subRepost
description: Check if submission has been reposted
kind: submission
condition: AND
rules:
- kind: repost
criteria:
- searchOn:
- kind: title
matchScore: '95'
actions:
- kind: report
content: This submission was reposted
```
```json5
{
"polling": [
"unmoderated"
],
"checks": [
{
"name": "subRepost",
"description": "Check if submission has been reposted",
// kind specifies this check is for SUBMISSIONS
"kind": "submission",
"condition": "AND",
"rules": [
// repost rule configuration is below
//
{
"kind": "repost",
"criteria": [
{
// specify only title to search on
"searchOn": [
{
"kind": "title",
// titles must be 95% or more similar
"matchScore": "95"
}
]
}
]
},
//
// repost rule configuration is above
],
"actions": [
{
"kind": "report",
"content": "This submission was reposted"
}
]
}
]
}
```
</details>
### Search by Title, specify similarity percentage, AND any duplicates
<details>
```yaml
polling:
- unmoderated
checks:
- name: subRepost
description: Check if submission has been reposted
kind: submission
condition: AND
rules:
- kind: repost
criteria:
- searchOn:
- duplicates
- kind: title
matchScore: '95'
actions:
- kind: report
content: This submission was reposted
```
```json5
{
"polling": [
"unmoderated"
],
"checks": [
{
"name": "subRepost",
"description": "Check if submission has been reposted",
// kind specifies this check is for SUBMISSIONS
"kind": "submission",
"condition": "AND",
"rules": [
// repost rule configuration is below
//
{
"kind": "repost",
"criteria": [
{
"searchOn": [
// look for duplicates (NON crossposts) using default configuration
"duplicates",
// search by title
{
"kind": "title",
// titles must be 95% or more similar
"matchScore": "95"
}
]
}
]
},
//
// repost rule configuration is above
],
"actions": [
{
"kind": "report",
"content": "This submission was reposted"
}
]
}
]
}
```
</details>
### Approve Submission if not reposted in the last month, by title
<details>
```yaml
polling:
- unmoderated
checks:
- name: subRepost
description: Check there are no reposts with same title in the last month
kind: submission
condition: AND
rules:
- kind: repost
criteria:
- searchOn:
- title
occurrences:
condition: OR
criteria:
- count:
test:
- < 1
- time:
test:
- testOn: newest
condition: '> 1 month'
actions:
- kind: approve
```
```json5
{
"polling": [
"unmoderated"
],
"checks": [
{
"name": "subRepost",
"description": "Check there are no reposts with same title in the last month",
// kind specifies this check is for SUBMISSIONS
"kind": "submission",
"condition": "AND",
"rules": [
// repost rule configuration is below
//
{
"kind": "repost",
"criteria": [
{
"searchOn": [
"title"
],
"occurrences": {
// if EITHER criteria is TRUE then it "passes"
"condition": "OR",
"criteria": [
// first criteria:
// TRUE if there are LESS THAN 1 reposts (no reposts found)
{
"count": {
"test": ["< 1"]
}
},
// second criteria:
// TRUE if the newest repost is older than one month
{
"time": {
"test": [
{
"testOn": "newest",
"condition": "> 1 month"
}
]
}
}
]
},
}
]
},
//
// repost rule configuration is above
],
"actions": [
{
// approve this post since we know it is not a repost of anything within the last month
"kind": "approve",
}
]
}
]
}
```
</details>
## Comments
### Default Behavior (No configuration)
This is the same behavior described in the [TLDR](#TLDR) section above -- find any submissions with:
* a very similar title (85% or more the same)
* or ignoring title...
* any crossposts/duplicates
* any submissions with the exact URL
* If comment being checked is on a Submission for Youtube then get top 50 comments on youtube video as well...
AND THEN
* sort submissions by votes
* take top 20 (upvoted) comments from top 10 (upvoted) submissions
* sort comments by votes, take top 50 + top 50 external items
FINALLY
* filter all gathered comments by default `matchScore: 85` to find very similar matches
* rules is triggered if any are found
<details>
```yaml
polling:
- newComm
checks:
- name: commRepost
description: Check if comment has been reposted
kind: common
condition: AND
rules:
- kind: repost
actions:
- kind: report
content: This comment was reposted
```
```json5
{
"polling": [
"newComm"
],
"checks": [
{
"name": "commRepost",
"description": "Check if comment has been reposted",
// kind specifies this check is for COMMENTS
"kind": "common",
"condition": "AND",
"rules": [
// repost rule configuration is below
//
{
"kind": "repost"
},
//
// repost rule configuration is above
],
"actions": [
{
"kind": "report",
"content": "This comment was reposted"
}
]
}
]
}
```
</details>
### Search by external (youtube) comments only
<details>
```yaml
polling:
- newComm
checks:
- name: commRepost
description: Check if comment has been reposted from youtube
kind: comment
condition: AND
rules:
- kind: repost
criteria:
- searchOn:
- external
actions:
- kind: report
content: This comment was reposted from youtube
```
```json5
{
"polling": [
"newComm"
],
"checks": [
{
"name": "commRepost",
"description": "Check if comment has been reposted from youtube",
// kind specifies this check is for SUBMISSIONS
"kind": "comment",
"condition": "AND",
"rules": [
// repost rule configuration is below
//
{
"kind": "repost",
"criteria": [
{
// specify only external (youtube) to search on
"searchOn": [
"external"
]
}
]
},
//
// repost rule configuration is above
],
"actions": [
{
"kind": "report",
"content": "This comment was reposted from youtube"
}
]
}
]
}
```
</details>
### Search by external (youtube) comments only, with higher comment match percentage
<details>
```yaml
polling:
- newComm
checks:
- name: commRepost
description: Check if comment has been reposted from youtube
kind: comment
condition: AND
rules:
- kind: repost
criteria:
- searchOn:
- external
matchScore: 95
actions:
- kind: report
content: This comment was reposted from youtube
```
```json5
{
"polling": [
"newComm"
],
"checks": [
{
"name": "commRepost",
"description": "Check if comment has been reposted from youtube",
// kind specifies this check is for SUBMISSIONS
"kind": "comment",
"condition": "AND",
"rules": [
// repost rule configuration is below
//
{
"kind": "repost",
"criteria": [
{
// specify only external (youtube) to search on
"searchOn": [
"external"
],
"matchScore": 95 // matchScore for comments is on criteria instead of searchOn config...
},
]
},
//
// repost rule configuration is above
],
"actions": [
{
"kind": "report",
"content": "This comment was reposted from youtube"
}
]
}
]
}
```
</details>
### Search by external (youtube) comments and submission URL, with higher comment match percentage
<details>
```yaml
polling:
- newComm
checks:
- name: commRepost
description: Check if comment has been reposted
kind: comment
condition: AND
rules:
- kind: repost
criteria:
- searchOn:
- external
- url
matchScore: 95
actions:
- kind: report
content: >-
This comment was reposted from youtube or from submission with the
same URL
```
```json5
{
"polling": [
"newComm"
],
"checks": [
{
"name": "commRepost",
"description": "Check if comment has been reposted",
// kind specifies this check is for SUBMISSIONS
"kind": "comment",
"condition": "AND",
"rules": [
// repost rule configuration is below
//
{
"kind": "repost",
"criteria": [
{
// specify only external (youtube) to search on
"searchOn": [
"external",
// can specify any/all submission search facets to acquire comments from
"url"
],
"matchScore": 95 // matchScore for comments is on criteria instead of searchOn config...
},
]
},
//
// repost rule configuration is above
],
"actions": [
{
"kind": "report",
"content": "This comment was reposted from youtube or from submission with the same URL"
}
]
}
]
}
```
</details>

View File

@@ -11,21 +11,31 @@ All actions for these configurations are non-destructive in that:
**You will have to remove the `report` action and `dryRun` settings yourself.** This is to ensure that you understand the behavior the bot will be performing. If you are unsure of this you should leave them in place until you are certain the behavior the bot is performing is acceptable.
**YAML** is the same format as **automoderator**
## Submission-based Behavior
### [Remove submissions from users who have used 'freekarma' subs to bypass karma checks](/docs/examples/subredditReady/freekarma.json5)
### Remove submissions from users who have used 'freekarma' subs to bypass karma checks
[YAML](/docs/examples/subredditReady/freekarma.yaml) | [JSON](/docs/examples/subredditReady/freekarma.json5)
If the user has any activity (comment/submission) in known freekarma subreddits in the past (50 activities or 6 months) then remove the submission.
### [Remove submissions from users who have crossposted the same submission 4 or more times](/docs/examples/subredditReady/crosspostSpam.json5)
### Remove submissions from users who have crossposted the same submission 4 or more times
[YAML](/docs/examples/subredditReady/crosspostSpam.yaml) | [JSON](/docs/examples/subredditReady/crosspostSpam.yaml)
If the user has crossposted the same submission in the past (50 activities or 6 months) 4 or more times in a row then remove the submission.
### [Remove submissions from users who have crossposted or used 'freekarma' subs](/docs/examples/subredditReady/freeKarmaOrCrosspostSpam.json5)
### Remove submissions from users who have crossposted or used 'freekarma' subs
[YAML](/docs/examples/subredditReady/freeKarmaOrCrosspostSpam.yaml) | [JSON](/docs/examples/subredditReady/freeKarmaOrCrosspostSpam.json5)
Will remove submission if either of the above two behaviors is detected
### [Remove link submissions where the user's history is comprised of 10% or more of the same link](/docs/examples/subredditReady/selfPromo.json5)
### Remove link submissions where the user's history is comprised of 10% or more of the same link
[YAML](/docs/examples/subredditReady/selfPromo.yaml) | [JSON](/docs/examples/subredditReady/selfPromo.json5)
If the link origin (youtube author, twitter author, etc. or regular domain for non-media links)
@@ -36,6 +46,33 @@ then remove the submission
## Comment-based behavior
### [Remove comment if the user has posted the same comment 4 or more times in a row](/docs/examples/subredditReady/commentSpam.json5)
### Remove comment if the user has posted the same comment 4 or more times in a row
[YAML](/docs/examples/subredditReady/commentSpam.yaml) | [JSON](/docs/examples/subredditReady/commentSpam.json5)
If the user made the same comment (with some fuzzy matching) 4 or more times in a row in the past (50 activities or 6 months) then remove the comment.
### Remove comment if it is discord invite link spam
[YAML](/docs/examples/subredditReady/discordSpam.yaml) | [JSON](/docs/examples/subredditReady/discordSpam.json5)
This rule goes a step further than automod can by being more discretionary about how it handles this type of spam.
* Remove the comment and **ban a user** if:
* Comment being checked contains **only** a discord link (no other text) AND
* Discord links appear **anywhere** in three or more of the last 10 comments the Author has made
otherwise...
* Remove the comment if:
* Comment being checked contains **only** a discord link (no other text) OR
* Comment contains a discord link **anywhere** AND
* Discord links appear **anywhere** in three or more of the last 10 comments the Author has made
Using these checks ContextMod can more easily distinguish between these use cases for a user commenting with a discord link:
* actual spammers who only spam a discord link
* users who may comment with a link but have context for it either in the current comment or in their history
* users who many comment with a link but it's a one-off event (no other links historically)
Additionally, you could modify both/either of these checks to not remove one-off discord link comments but still remove if the user has a historical trend for spamming links

View File

@@ -0,0 +1,25 @@
polling:
- newComm
checks:
# Stop users who spam the same comment many times
- name: low xp comment spam
description: X-posted comment >=4x
kind: comment
condition: AND
rules:
- name: xPostLow
kind: repeatActivity
# number of "non-repeat" comments allowed between "repeat comments"
gapAllowance: 2
# greater or more than 4 repeat comments triggers this rule
threshold: '>= 4'
# retrieve either last 50 comments or 6 months' of history, whichever is less
window:
count: 50
duration: 6 months
actions:
- kind: report
enable: true
content: 'Remove => Posted same comment {{rules.xpostlow.largestRepeat}}x times'
- kind: remove
enable: true

View File

@@ -0,0 +1,48 @@
polling:
- unmoderated
checks:
# stop users who post low-effort, crossposted spam submissions
#
# Remove a SUBMISSION if the user has crossposted it at least 4 times in recent history AND
# less than 50% of their activity is comments OR more than 40% of those comments are as OP (in the own submissions)
- name: low xp spam and engagement
description: X-posted 4x and low comment engagement
kind: submission
itemIs:
- removed: false
condition: AND
rules:
- name: xPostLow
kind: repeatActivity
gapAllowance: 2
threshold: '>= 4'
window:
count: 50
duration: 6 months
- name: lowOrOpComm
kind: history
criteriaJoin: OR
criteria:
- window:
count: 100
duration: 6 months
comment: < 50%
- window:
count: 100
duration: 6 months
comment: '> 40% OP'
actions:
- kind: report
enable: true
content: >-
Remove=>{{rules.xpostlow.largestRepeat}} X-P =>
{{rules.loworopcomm.thresholdSummary}}
- kind: remove
enable: true
- kind: comment
enable: true
content: >-
Your submission has been removed because you cross-posted it
{{rules.xpostlow.largestRepeat}} times and you have very low
engagement outside of making submissions
distinguish: true

View File

@@ -0,0 +1,75 @@
{
"polling": ["newComm"],
"checks": [
{
"name": "ban discord only spammer",
"description": "ban a user who spams only a discord link many times historically",
"kind": "comment",
"condition": "AND",
"rules": [
"linkOnlySpam",
"linkAnywhereHistoricalSpam",
],
"actions": [
{
"kind": "remove"
},
{
"kind": "ban",
"content": "spamming discord links"
}
]
},
{
"name": "remove discord spam",
"description": "remove comments from users who only link to discord or mention discord link many times historically",
"kind": "comment",
"condition": "OR",
"rules": [
{
"name": "linkOnlySpam",
"kind": "regex",
"criteria": [
{
"name": "only link",
"regex": "/^.*(discord\\.gg\\/[\\w\\d]+)$/i",
}
]
},
{
"condition": "AND",
"rules": [
{
"name": "linkAnywhereSpam",
"kind": "regex",
"criteria": [
{
"name": "contains link anywhere",
"regex": "/^.*(discord\\.gg\\/[\\w\\d]+).*$/i",
}
]
},
{
"name": "linkAnywhereHistoricalSpam",
"kind": "regex",
"criteria": [
{
"name": "contains links anywhere historically",
"regex": "/^.*(discord\\.gg\\/[\\w\\d]+).*$/i",
"totalMatchThreshold": ">= 3",
"lookAt": "comments",
"window": 10
}
]
}
]
}
],
"actions": [
{
"kind": "remove"
}
]
}
]
}

View File

@@ -0,0 +1,46 @@
polling:
- newComm
checks:
- name: ban discord only spammer
description: ban a user who spams only a discord link many times historically
kind: comment
condition: AND
rules:
- linkOnlySpam
- linkAnywhereHistoricalSpam
actions:
- kind: remove
- kind: ban
content: spamming discord links
- name: remove discord spam
description: >-
remove comments from users who only link to discord or mention discord
link many times historically
kind: comment
condition: OR
rules:
- name: linkOnlySpam
kind: regex
criteria:
- name: only link
# single quotes are required to escape special characters
regex: '/^.*(discord\.gg\/[\w\d]+)$/i'
- condition: AND
rules:
- name: linkAnywhereSpam
kind: regex
criteria:
- name: contains link anywhere
# single quotes are required to escape special characters
regex: '/^.*(discord\.gg\/[\w\d]+).*$/i'
- name: linkAnywhereHistoricalSpam
kind: regex
criteria:
- name: contains links anywhere historically
# single quotes are required to escape special characters
regex: '/^.*(discord\.gg\/[\w\d]+).*$/i'
totalMatchThreshold: '>= 3'
lookAt: comments
window: 10
actions:
- kind: remove

View File

@@ -0,0 +1,84 @@
polling:
- unmoderated
checks:
# stop users who post low-effort, crossposted spam submissions
#
# Remove a SUBMISSION if the user has crossposted it at least 4 times in recent history AND
# less than 50% of their activity is comments OR more than 40% of those comments are as OP (in the own submissions)
- name: remove on low xp spam and engagement
description: X-posted 4x and low comment engagement
kind: submission
itemIs:
- removed: false
condition: AND
rules:
- name: xPostLow
kind: repeatActivity
gapAllowance: 2
threshold: '>= 4'
window:
count: 50
duration: 6 months
- name: lowOrOpComm
kind: history
criteriaJoin: OR
criteria:
- window:
count: 100
duration: 6 months
comment: < 50%
- window:
count: 100
duration: 6 months
comment: '> 40% OP'
actions:
- kind: report
enable: true
content: >-
Remove=>{{rules.xpostlow.largestRepeat}} X-P =>
{{rules.loworopcomm.thresholdSummary}}
- kind: remove
enable: false
- kind: comment
enable: true
content: >-
Your submission has been removed because you cross-posted it
{{rules.xpostlow.largestRepeat}} times and you have very low
engagement outside of making submissions
distinguish: true
dryRun: true
# Remove submissions from users who have recent activity in freekarma subs within the last 50 activities or 6 months (whichever is less)
- name: freekarma removal
description: Remove submission if user has used freekarma sub recently
kind: submission
itemIs:
- removed: false
condition: AND
rules:
- name: freekarma
kind: recentActivity
window:
count: 50
duration: 6 months
useSubmissionAsReference: false
thresholds:
- subreddits:
- FreeKarma4U
- FreeKarma4You
- KarmaStore
- promote
- shamelessplug
- upvote
actions:
- kind: report
enable: true
content: 'Remove=> {{rules.newtube.totalCount}} activities in freekarma subs'
- kind: remove
enable: false
- kind: comment
enable: true
content: >-
Your submission has been removed because you have recent activity in
'freekarma' subs
distinguish: true
dryRun: true

View File

@@ -0,0 +1,35 @@
polling:
- unmoderated
checks:
# Remove submissions from users who have recent activity in freekarma subs within the last 50 activities or 6 months (whichever is less)
- name: freekarma removal
description: Remove submission if user has used freekarma sub recently
kind: submission
itemIs:
- removed: false
condition: AND
rules:
- name: freekarma
kind: recentActivity
window:
count: 50
duration: 6 months
useSubmissionAsReference: false
thresholds:
- subreddits:
- FreeKarma4U
- FreeKarma4You
- KarmaStore
- upvote
actions:
- kind: report
enable: true
content: 'Remove=> {{rules.newtube.totalCount}} activities in freekarma subs'
- kind: remove
enable: true
- kind: comment
enable: false
content: >-
Your submission has been removed because you have recent activity in
'freekarma' subs
distinguish: true

View File

@@ -0,0 +1,71 @@
polling:
- unmoderated
checks:
#
# Stop users who make link submissions with a self-promotional agenda (with reddit's suggested 10% rule)
# https://www.reddit.com/wiki/selfpromotion#wiki_guidelines_for_self-promotion_on_reddit
#
# Remove a SUBMISSION if the link comprises more than or equal to 10% of users history (100 activities or 6 months) OR
#
# if link comprises 10% of submission history (100 activities or 6 months)
# AND less than 50% of their activity is comments OR more than 40% of those comments are as OP (in the own submissions)
#
- name: Self-promo all AND low engagement
description: Self-promo is >10% for all or just sub and low comment engagement
kind: submission
condition: OR
rules:
- name: attr
kind: attribution
criteria:
- threshold: '>= 10%'
window:
count: 100
duration: 6 months
domains:
- 'AGG:SELF'
- condition: AND
rules:
- name: attrsub
kind: attribution
criteria:
- threshold: '>= 10%'
thresholdOn: submissions
window:
count: 100
duration: 6 months
domains:
- 'AGG:SELF'
- name: lowOrOpComm
kind: history
criteriaJoin: OR
criteria:
- window:
count: 100
duration: 6 months
comment: < 50%
- window:
count: 100
duration: 6 months
comment: '> 40% OP'
actions:
- kind: report
enable: true
content: >-
{{rules.attr.largestPercent}}{{rules.attrsub.largestPercent}} of
{{rules.attr.activityTotal}}{{rules.attrsub.activityTotal}} items
({{rules.attr.window}}{{rules.attrsub.window}}){{#rules.loworopcomm.thresholdSummary}}
=>
{{rules.loworopcomm.thresholdSummary}}{{/rules.loworopcomm.thresholdSummary}}
- kind: remove
enable: false
- kind: comment
enable: true
content: >-
Your submission has been removed it comprises 10% or more of your
recent history
({{rules.attr.largestPercent}}{{rules.attrsub.largestPercent}}). This
is against [reddit's self promotional
guidelines.](https://www.reddit.com/wiki/selfpromotion#wiki_guidelines_for_self-promotion_on_reddit)
distinguish: true
dryRun: true

View File

@@ -14,7 +14,7 @@ Consult the [schema](https://json-schema.app/view/%23%2Fdefinitions%2FUserNoteCr
### Examples
* [Do not tag user with Good User note](/docs/examples/userNotes/usernoteFilter.json5)
* Do not tag user with Good User note [JSON](/docs/examples/userNotes/usernoteFilter.json5) | [YAML](/docs/examples/userNotes/usernoteFilter.yaml)
## Action
@@ -23,4 +23,4 @@ A User Note can also be added to the Author of a Submission or Comment with the
### Examples
* [Add note on user doing self promotion](/docs/examples/userNotes/usernoteSP.json5)
* Add note on user doing self promotion [JSON](/docs/examples/userNotes/usernoteSP.json5) | [YAML](/docs/examples/userNotes/usernoteSP.yaml)

View File

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

View File

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

View File

@@ -14,8 +14,8 @@ This getting started guide is for **reddit moderators** -- that is, someone who
Before continuing with this guide you should first make sure you understand how a ContextMod works. Please review this documentation:
* [How It Works](/docs#how-it-works)
* [Core Concepts](/docs#concepts)
* [How It Works](/docs/README.md#how-it-works)
* [Core Concepts](/docs/README.md#concepts)
# Choose A Bot
@@ -36,15 +36,16 @@ If the Operator has communicated that **you should add a bot they control as a m
___
Ensure that you are in communication with the **operator** for this bot. The bot **will not automatically accept a moderator invitation,** it must be manually done by the bot operator. This is an intentional barrier to ensure moderators and the operator are familiar with their respective needs and have some form of trust.
Ensure that you are in communication with the **operator** of this bot. The bot **will only accept a moderator invitation if your subreddit has been whitelisted by the operator.** This is an intentional barrier to ensure moderators and the operator are familiar with their respective needs and have some form of trust.
Now invite the bot to moderate your subreddit. The bot should have at least these permissions:
* Manage Users
* Manage Posts and Comments
* Manage Flair
Additionally, the bot must have the **Manage Wiki Pages** permission if you plan to use [Toolbox User Notes](https://www.reddit.com/r/toolbox/wiki/docs/usernotes). If you are not planning on using this feature and do not want the bot to have this permission then you **must** ensure the bot has visibility to the configuration wiki page (detailed below).
* Manage Wiki Pages
* Required to read the moderator-only visible wiki page used to configure the bot
* Required to read/write to [Toolbox User Notes](https://www.reddit.com/r/toolbox/wiki/docs/usernotes)
## Bring Your Own Bot (BYOB)
@@ -60,7 +61,7 @@ If the operator has communicated that **they want to use a bot you control** thi
**Cons:**
* More setup required for both moderators and operators
* You must have access to the credentials for the reddit account (bot)
___
@@ -72,15 +73,28 @@ Review the information shown on the invite link webpage and then follow the dire
# Configuring the Bot
The bot's behavior is defined using a configuration, like automoderator, that is stored in the **wiki** of each subreddit it moderates.
The default location for this page is at `https://old.reddit.com/r/YOURSUBERDDIT/wiki/botconfig/contextbot`
## Setup wiki page
The bot automatically tries to create its configuration wiki page. You can find the result of this in the log for your subreddit in the web interface.
If this fails for some reason you can create the wiki page through the web interface by navigating to your subreddit's tab, opening the [built-in editor (click **View**)](/docs/screenshots/configBox.png), and following the directions in **Create configuration for...** link found there.
If neither of the above approaches work, or you do not wish to use the web interface, expand the section below for directions on how to manually setup the wiki page:
<details>
* Visit the wiki page of the subreddit you want the bot to moderate
* The default location the bot checks for a configuration is at `https://old.reddit.com/r/YOURSUBERDDIT/wiki/botconfig/contextbot`
* If the page does not exist create it
* 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**
* Alternatively, if you did not give the bot the **Manage Wiki Pages** permission then add it to the **allow users to edit page** setting
</details>
## Procure a configuration
@@ -94,25 +108,46 @@ Visit the [Examples](https://github.com/FoxxMD/context-mod/tree/master/docs/exam
After you have found a configuration to use as a starting point:
* In a new tab open the github page for the configuration you want ([example](/docs/examples/repeatActivity/crosspostSpamming.json5))
* Click the **Raw** button, then select all and copy all of the text to your clipboard.
* Copy the URL for the configuration file EX `https://github.com/FoxxMD/context-mod/blob/master/docs/examples/subredditReady/freekarma.json5` and either:
* (Easiest) **Load** it into your [subreddit's built-in editor](#using-the-built-in-editor) and **Save**
* or on the file's page, click the **Raw** button, select all and copy to your clipboard, and [manually save to your wiki page](#manually-saving)
### Build Your Own Config
Additionally, you can use [this schema editor](https://json-schema.app/view/%23?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Fcontext-mod%2Fmaster%2Fsrc%2FSchema%2FApp.json) to build your configuration. The editor features a ton of handy features:
CM comes equipped with a [configuration explorer](https://json-schema.app/view/%23?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Fcontext-mod%2Fmaster%2Fsrc%2FSchema%2FApp.json) to help you see all available options, with descriptions and examples, that can be used in your configuration.
* fully annotated configuration data/structure
* generated examples in json/yaml
* built-in editor that automatically validates your config
To create or edit a configuration you should use **CM's buit-in editor** which features:
* syntax validation and formatting
* full configuration validation with error highlighting, hints, and fixes
* hover over properties to see documentation and examples
PROTIP: Find an example config to use as a starting point and then build on it using the editor.
To use the editor either:
* [use your subreddit's built-in editor](#using-the-built-in-editor)
* or use the public editor at https://cm.foxxmd.dev/config
PROTIP: Find an [example config](#using-an-example-config) to use as a starting point and then build on it using the editor.
## Saving Your Configuration
* Open the wiki page you created in the [previous step](#setup-wiki-page) and click **edit**
### Using the built-in Editor
In the web interface each subreddit's tab has access to the built-in editor. Use this built-in editor to automatically create, load, or save the configuration for that subreddit's wiki.
* Visit the tab for the subreddit you want to edit the configuration of
* Open the [built-in editor by click **View**](/docs/screenshots/configBox.png)
* Edit your configuration
* Follow the directions on the **Save to r/..** link found at the top of the editor to automatically save your configuration
### Manually Saving
<details>
* Open the wiki page you created in the [wiki setup step](#setup-wiki-page) and click **edit**
* Copy-paste your configuration into the wiki text box
* Save the edited wiki page
</details>
___
The bot automatically checks for new configurations on your wiki page every 5 minutes. If your operator has the web interface accessible you may login there and force the config to update on your subreddit.

View File

@@ -50,6 +50,18 @@ tsc -p .
### [Heroku Quick Deploy](https://heroku.com/about)
[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://dashboard.heroku.com/new?template=https://github.com/FoxxMD/context-mod)
This template provides a **web** and **worker** dyno for heroku.
* **Web** -- Will run the bot **and** the web interface for ContextMod.
* **Worker** -- Will run **just** the bot.
Be aware that Heroku's [free dyno plan](https://devcenter.heroku.com/articles/free-dyno-hours#dyno-sleeping) enacts some limits:
* A **Web** dyno will go to sleep (pause) after 30 minutes without web activity -- so your bot will ALSO go to sleep at this time
* The **Worker** dyno **will not** go to sleep but you will NOT be able to access the web interface. You can, however, still see how Cm is running by reading the logs for the dyno.
If you want to use a free dyno it is recommended you perform first-time setup (bot authentication and configuration, testing, etc...) with the **Web** dyno, then SWITCH to a **Worker** dyno so it can run 24/7.
# Bot Authentication
Next you need to create a bot and authenticate it with Reddit. Follow the [bot authentication guide](/docs/botAuthentication.md) to complete this step.

View File

@@ -7,6 +7,7 @@ ContextMod supports comparing image content, for the purpose of detecting duplic
To enable comparisons reference the example below (at the top-level of your rule) and configure as needed:
JSON
```json5
{
"name": "ruleWithImageDetection",
@@ -31,9 +32,18 @@ To enable comparisons reference the example below (at the top-level of your rule
},
//
// And above ^^^
...
//...
}
```
YAML
```yaml
name: ruleWithImageDetection
kind: recentActivity
enable: true
threshold: 5
fetchBehavior: extension
```
**Perceptual Hashing** (`hash`) and **Pixel Comparisons** (`pixel`) may be used at the same time. Refer to the documentation below to see how they interact.
@@ -114,10 +124,12 @@ To further configure hashing refer to this code block:
// the higher the bits the more accurate the comparison
//
// NOTE: Hashes of different sizes (bits) cannot be compared. If you are caching hashes make sure all rules where results may be shared use the same bit count to ensure hashes can be compared. Otherwise hashes will be recomputed.
"bits": 32, // default is 32 if not defined
"bits": 32,
// default is 32 if not defined
//
// number of seconds to cache an image hash
"ttl": 60, // default is 60 if not defined
"ttl": 60,
// default is 60 if not defined
//
// "High Confidence" Threshold
// If the difference in comparison is equal to or less than this number the images are considered the same and pixel comparison WILL NOT occur
@@ -139,8 +151,22 @@ To further configure hashing refer to this code block:
//
// And above ^^^
//"pixel": {...}
},
}
//...
}
```
YAML
```yaml
name: ruleWithImageDetectionAndConfiguredHashing
kind: recentActivity
imageDetection:
enable: true
hash:
enable: true
bits: 32
ttl: 60
hardThreshold: 5
softThreshold: 0
```
## Pixel Comparison
@@ -184,18 +210,28 @@ To configure pixel comparisons refer to this code block:
```json5
{
"name": "ruleWithImageDetectionAndPixelEnabled",
"kind": "recentActivity",
"imageDetection": {
//"hash": {...}
"pixel": {
// enable or disable pixel comparisons (disabled by default)
"enable": true,
// if the comparison difference percentage is equal to or less than this value the images are considered the same
//
// if not defined the value from imageDetection.threshold will be used
"threshold": 5
}
},
//...
"name": "ruleWithImageDetectionAndPixelEnabled",
"kind": "recentActivity",
"imageDetection": {
//"hash": {...}
"pixel": {
// enable or disable pixel comparisons (disabled by default)
"enable": true,
// if the comparison difference percentage is equal to or less than this value the images are considered the same
//
// if not defined the value from imageDetection.threshold will be used
"threshold": 5
}
},
//...
}
```
YAML
```yaml
name: ruleWithImageDetectionAndPixelEnabled
kind: recentActivity
imageDetection:
pixel:
enable: true
threshold: 5
```

BIN
docs/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

View File

@@ -121,6 +121,16 @@ Below are examples of the minimum required config to run the application using a
Using **FILE**
<details>
YAML
```yaml
bots:
- credentials:
clientId: f4b4df1c7b2
clientSecret: 34v5q1c56ub
refreshToken: 34_f1w1v4
accessToken: p75_1c467b2
```
JSON
```json5
{
"bots": [
@@ -175,6 +185,11 @@ An example of using multiple configuration levels together IE all are provided t
}
}
```
YAML
```yaml
logging:
level: debug
```
</details>
@@ -220,6 +235,30 @@ See the [Architecture Docs](/docs/serverClientArchitecture.md) for more informat
<details>
YAML
```yaml
bots:
- credentials:
clientId: f4b4df1c7b2
clientSecret: 34v5q1c56ub
refreshToken: 34_f1w1v4
accessToken: p75_1c467b2
web:
credentials:
clientId: f4b4df1c7b2
clientSecret: 34v5q1c56ub
redirectUri: 'http://localhost:8085/callback'
clients:
# server application running on this same CM instance
- host: 'localhost:8095'
secret: localSecret
# a server application running somewhere else
- host: 'mySecondContextMod.com:8095'
secret: anotherSecret
api:
secret: localSecret
```
JSON
```json5
{
"bots": [
@@ -289,3 +328,14 @@ A caching object in the json configuration:
}
}
```
YAML
```yaml
provider:
store: memory
ttl: 60
max: 500
host: localhost
port: 6379
auth_pass: null
db: 0
```

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 75 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 93 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 84 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB

BIN
docs/screenshots/logs.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 133 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

30
docs/webInterface.md Normal file
View File

@@ -0,0 +1,30 @@
## Editing/Updating Your Config
* Open the editor for your subreddit
* In the web dashboard \-> r/YourSubreddit \-> Config -> **View** [(here)](/docs/screenshots/config/config.jpg)
* Follow the directions on the [link at the top of the window](/docs/screenshots/config/save.png) to enable config editing using your moderator account
* After enabling editing just click "save" at any time to save your config
* After you have added/edited your config the bot will detect changes within 5 minutes or you can manually trigger it by clicking **Update**
## General Config (Editor) Tips
* The editor will automatically validate your [syntax (formatting)](/docs/screenshots/config/syntax.png) and [config correctness](/docs/screenshots/config/correctness.png) (property names, required properties, etc.)
* These show up as squiggly lines like in Microsoft Word and as a [list at the bottom of the editor](/docs/screenshots/config/errors.png)
* In your config all **Checks** and **Actions** have two properties that control how they behave:
* [**Enable**](/docs/screenshots/config/enable.png) (defaults to `enable: true`) -- Determines if the check or action is run, at all
* **Dryrun** (defaults to `dryRun: false`) -- When `true` the check or action will run but any **Actions** that may be triggered will "pretend" to execute but not actually talk to the Reddit API.
* Use `dryRun` to test your config without the bot making any changes on reddit
* When starting out with a new config it is recommended running the bot with remove/ban actions **disabled**
* Use `report` actions to get reports in your modqueue from the bot that describe what it detected and what it would do about it
* Once the bot is behaving as desired (no false positives or weird behavior) destructive actions can be enabled or turned off of dryrun
## Web Dashboard Tips
* Use the [**Overview** section](/docs/screenshots/botOperations.png) to control the bot at a high-level
* You can **manually run** the bot on any activity (comment/submission) by pasting its permalink into the [input field below the Overview section](/docs/screenshots/runInput.png) and hitting one of the **run buttons**
* **Dry run** will make the bot run on the activity but it will only **pretend** to run actions, if triggered. This is super useful for testing your config without consequences
* **Run** will do everything
* All of the bot's activity is shown in real-time in the [log section](/docs/screenshots/logs.png)
* This will output the results of all run checks/rules and any actions that run
* You can view summaries of all activities that triggered a check (had actions run) by clicking on [Actioned Events](/docs/screenshots/actionsEvents.png)
* This includes activities run with dry run

29
heroku.Dockerfile Normal file
View File

@@ -0,0 +1,29 @@
FROM node:16-alpine3.14
ENV TZ=Etc/GMT
# vips required to run sharp library for image comparison
RUN echo "http://dl-4.alpinelinux.org/alpine/v3.14/community" >> /etc/apk/repositories \
&& apk --update add vips
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
WORKDIR /usr/app
COPY package*.json ./
COPY tsconfig.json .
RUN npm install
ADD . /usr/app
RUN npm run build
ENV NPM_CONFIG_LOGLEVEL debug
ARG log_dir=/home/node/logs
RUN mkdir -p $log_dir
VOLUME $log_dir
ENV LOG_DIR=$log_dir
CMD [ "node", "src/index.js", "run", "all", "--port $PORT"]

View File

@@ -1,3 +1,4 @@
build:
docker:
worker: Dockerfile
web: heroku.Dockerfile
worker: heroku.Dockerfile

4445
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -7,7 +7,6 @@
"test": "echo \"Error: no tests installed\" && exit 1",
"build": "tsc",
"start": "node src/index.js run",
"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 & npm run -s schema-config",
"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",
@@ -26,14 +25,17 @@
"license": "ISC",
"dependencies": {
"@awaitjs/express": "^0.8.0",
"@googleapis/youtube": "^2.0.0",
"@stdlib/regexp-regexp": "^0.0.6",
"ajv": "^7.2.4",
"ansi-regex": ">=5.0.1",
"async": "^3.2.0",
"autolinker": "^3.14.3",
"body-parser": "^1.19.0",
"cache-manager": "^3.4.4",
"cache-manager-redis-store": "^2.0.0",
"commander": "^8.0.0",
"comment-json": "^4.1.1",
"cookie-parser": "^1.3.5",
"dayjs": "^1.10.5",
"deepmerge": "^4.2.2",
@@ -47,17 +49,16 @@
"express-socket.io-session": "^1.3.5",
"fast-deep-equal": "^3.1.3",
"fuse.js": "^6.4.6",
"globrex": "^0.1.2",
"got": "^11.8.2",
"he": "^1.2.0",
"http-proxy": "^1.18.1",
"image-size": "^1.0.0",
"js-yaml": "^4.1.0",
"json5": "^2.2.0",
"jsonwebtoken": "^8.5.1",
"leven": "^3.1.0",
"lodash": "^4.17.21",
"lru-cache": "^6.0.0",
"monaco-editor": "^0.27.0",
"mustache": "^4.2.0",
"node-fetch": "^2.6.1",
"normalize-url": "^6.1.0",
@@ -68,20 +69,22 @@
"passport-custom": "^1.1.1",
"passport-jwt": "^4.0.0",
"pixelmatch": "^5.2.1",
"pony-cause": "^1.1.1",
"pretty-print-json": "^1.0.3",
"safe-stable-stringify": "^1.1.1",
"set-random-interval": "^1.1.0",
"snoostorm": "^1.5.2",
"snoowrap": "^1.23.0",
"socket.io": "^4.1.3",
"string-similarity": "^4.0.4",
"tcp-port-used": "^1.0.2",
"triple-beam": "^1.3.0",
"typescript": "^4.3.4",
"webhook-discord": "^3.7.7",
"winston": "FoxxMD/winston#fbab8de969ecee578981c77846156c7f43b5f01e",
"winston": "github:FoxxMD/winston#fbab8de969ecee578981c77846156c7f43b5f01e",
"winston-daily-rotate-file": "^4.5.5",
"winston-duplex": "^0.1.1",
"winston-transport": "^4.4.0",
"yaml": "2.0.0-10",
"zlib": "^1.0.5"
},
"devDependencies": {
@@ -93,6 +96,7 @@
"@types/express": "^4.17.13",
"@types/express-session": "^1.17.4",
"@types/express-socket.io-session": "^1.3.6",
"@types/globrex": "^0.1.1",
"@types/he": "^1.1.1",
"@types/http-proxy": "^1.17.7",
"@types/js-yaml": "^4.0.1",
@@ -108,11 +112,12 @@
"@types/passport-jwt": "^3.0.6",
"@types/pixelmatch": "^5.2.4",
"@types/sharp": "^0.29.2",
"@types/string-similarity": "^4.0.0",
"@types/tcp-port-used": "^1.0.0",
"@types/triple-beam": "^1.3.2",
"ts-auto-guard": "*",
"ts-essentials": "^9.1.2",
"ts-json-schema-generator": "^0.93.0",
"typescript-json-schema": "^0.50.1"
"typescript-json-schema": "~0.53"
},
"optionalDependencies": {
"sharp": "^0.29.1"

View File

@@ -10,10 +10,11 @@ import ApproveAction, {ApproveActionConfig} from "./ApproveAction";
import BanAction, {BanActionJson} from "./BanAction";
import {MessageAction, MessageActionJson} from "./MessageAction";
import {SubredditResources} from "../Subreddit/SubredditResources";
import Snoowrap from "snoowrap";
import {UserFlairAction, UserFlairActionJson} from './UserFlairAction';
import {ExtendedSnoowrap} from '../Utils/SnoowrapClients';
export function actionFactory
(config: ActionJson, logger: Logger, subredditName: string, resources: SubredditResources, client: Snoowrap): Action {
(config: ActionJson, logger: Logger, subredditName: string, resources: SubredditResources, client: ExtendedSnoowrap): Action {
switch (config.kind) {
case 'comment':
return new CommentAction({...config as CommentActionJson, logger, subredditName, resources, client});
@@ -25,6 +26,8 @@ export function actionFactory
return new ReportAction({...config as ReportActionJson, logger, subredditName, resources, client});
case 'flair':
return new FlairAction({...config as FlairActionJson, logger, subredditName, resources, client});
case 'userflair':
return new UserFlairAction({...config as UserFlairActionJson, logger, subredditName, resources, client});
case 'approve':
return new ApproveAction({...config as ApproveActionConfig, logger, subredditName, resources, client});
case 'usernote':

View File

@@ -1,39 +1,94 @@
import {ActionJson, ActionConfig} from "./index";
import {ActionJson, ActionConfig, ActionOptions} from "./index";
import Action from "./index";
import Snoowrap, {Comment, Submission} from "snoowrap";
import Snoowrap from "snoowrap";
import {RuleResult} from "../Rule";
import {ActionProcessResult} from "../Common/interfaces";
import Submission from "snoowrap/dist/objects/Submission";
import Comment from "snoowrap/dist/objects/Comment";
export class ApproveAction extends Action {
targets: ApproveTarget[]
getKind() {
return 'Approve';
}
constructor(options: ApproveOptions) {
super(options);
const {
targets = ['self']
} = options;
this.targets = targets;
}
async process(item: Comment | Submission, ruleResults: RuleResult[], runtimeDryrun?: boolean): Promise<ActionProcessResult> {
const dryRun = runtimeDryrun || this.dryRun;
//snoowrap typing issue, thinks comments can't be locked
// @ts-ignore
if (item.approved) {
this.logger.warn('Item is already approved');
return {
dryRun,
success: false,
result: 'Item is already approved'
const touchedEntities = [];
const realTargets = item instanceof Submission ? ['self'] : this.targets;
for(const target of realTargets) {
let targetItem = item;
if(target !== 'self' && item instanceof Comment) {
targetItem = await this.resources.getActivity(this.client.getSubmission(item.link_id));
}
// @ts-ignore
if (targetItem.approved) {
const msg = `${target === 'self' ? 'Item' : 'Comment\'s parent Submission'} is already approved`;
this.logger.warn(msg);
return {
dryRun,
success: false,
result: msg
}
}
if (!dryRun) {
// make sure we have an actual item and not just a plain object from cache
if(target !== 'self' && !(targetItem instanceof Submission)) {
// @ts-ignore
targetItem = await this.client.getSubmission((item as Comment).link_id).fetch();
}
// @ts-ignore
touchedEntities.push(await targetItem.approve());
if(target === 'self') {
// @ts-ignore
item.approved = true;
await this.resources.resetCacheForItem(item);
} else if(await this.resources.hasActivity(targetItem)) {
// @ts-ignore
targetItem.approved = true;
await this.resources.resetCacheForItem(targetItem);
}
}
}
if (!dryRun) {
// @ts-ignore
await item.approve();
}
return {
dryRun,
success: true,
touchedEntities
}
}
}
export interface ApproveActionConfig extends ActionConfig {
export type ApproveTarget = 'self' | 'parent';
export interface ApproveOptions extends ApproveActionConfig, ActionOptions {}
export interface ApproveActionConfig extends ActionConfig {
/**
* Specify which Activities to approve
*
* This setting is only applicable if the Activity being acted on is a **comment**. On a **submission** the setting does nothing
*
* * self => approve activity being checked (comment)
* * parent => approve parent (submission) of activity being checked (comment)
* */
targets?: ApproveTarget[]
}
/**

View File

@@ -39,6 +39,7 @@ export class BanAction extends Action {
const renderedBody = content === undefined ? undefined : await renderContent(content, item, ruleResults, this.resources.userNotes);
const renderedContent = renderedBody === undefined ? undefined : `${renderedBody}${await this.resources.generateFooter(item, this.footer)}`;
const touchedEntities = [];
let banPieces = [];
banPieces.push(`Message: ${renderedContent === undefined ? 'None' : `${renderedContent.length > 100 ? `\r\n${renderedContent}` : renderedContent}`}`);
banPieces.push(`Reason: ${this.reason || 'None'}`);
@@ -50,18 +51,20 @@ export class BanAction extends Action {
// @ts-ignore
const fetchedSub = await item.subreddit.fetch();
const fetchedName = await item.author.name;
await fetchedSub.banUser({
const bannedUser = await fetchedSub.banUser({
name: fetchedName,
banMessage: renderedContent === undefined ? undefined : renderedContent,
banReason: this.reason,
banNote: this.note,
duration: this.duration
});
touchedEntities.push(bannedUser);
}
return {
dryRun,
success: true,
result: `Banned ${item.author.name} ${durText}${this.reason !== undefined ? ` (${this.reason})` : ''}`
result: `Banned ${item.author.name} ${durText}${this.reason !== undefined ? ` (${this.reason})` : ''}`,
touchedEntities
};
}
}

View File

@@ -51,16 +51,19 @@ export class CommentAction extends Action {
result: 'Cannot comment because Item is archived'
};
}
const touchedEntities = [];
let reply: Comment;
if(!dryRun) {
// @ts-ignore
reply = await item.reply(renderedContent);
touchedEntities.push(reply);
}
if (this.lock) {
if (!dryRun) {
// snoopwrap typing issue, thinks comments can't be locked
// @ts-ignore
await item.lock();
touchedEntities.push(item);
}
}
if (this.distinguish && !dryRun) {
@@ -78,7 +81,8 @@ export class CommentAction extends Action {
return {
dryRun,
success: true,
result: `${modifierStr}${this.lock ? ' - Locked Author\'s Activity - ' : ''}${truncateStringToLength(100)(body)}`
result: `${modifierStr}${this.lock ? ' - Locked Author\'s Activity - ' : ''}${truncateStringToLength(100)(body)}`,
touchedEntities,
};
}
}

View File

@@ -11,6 +11,7 @@ export class LockAction extends Action {
async process(item: Comment | Submission, ruleResults: RuleResult[], runtimeDryrun?: boolean): Promise<ActionProcessResult> {
const dryRun = runtimeDryrun || this.dryRun;
const touchedEntities = [];
//snoowrap typing issue, thinks comments can't be locked
// @ts-ignore
if (item.locked) {
@@ -25,10 +26,15 @@ export class LockAction extends Action {
//snoowrap typing issue, thinks comments can't be locked
// @ts-ignore
await item.lock();
// @ts-ignore
item.locked = true;
await this.resources.resetCacheForItem(item);
touchedEntities.push(item);
}
return {
dryRun,
success: true
success: true,
touchedEntities
}
}
}

View File

@@ -12,7 +12,8 @@ import {
REDDIT_ENTITY_REGEX_URL,
truncateStringToLength
} from "../util";
import SimpleError from "../Utils/SimpleError";
import {SimpleError} from "../Utils/Errors";
import {ErrorWithCause} from "pony-cause";
export class MessageAction extends Action {
content: string;
@@ -58,17 +59,14 @@ export class MessageAction extends Action {
if(this.to !== undefined) {
// parse to value
try {
const entityData = parseRedditEntity(this.to);
const entityData = parseRedditEntity(this.to, 'user');
if(entityData.type === 'user') {
recipient = entityData.name;
} else {
recipient = `/r/${entityData.name}`;
}
} catch (err) {
this.logger.error(`'to' field for message was not in a valid format. See ${REDDIT_ENTITY_REGEX_URL} for valid examples`);
this.logger.error(err);
err.logged = true;
throw err;
} catch (err: any) {
throw new ErrorWithCause(`'to' field for message was not in a valid format. See ${REDDIT_ENTITY_REGEX_URL} for valid examples`, {cause: err});
}
if(recipient.includes('/r/') && this.asSubreddit) {
throw new SimpleError(`Cannot send a message as a subreddit to another subreddit. Requested recipient: ${recipient}`);

View File

@@ -1,45 +1,69 @@
import {ActionJson, ActionConfig} from "./index";
import {ActionJson, ActionConfig, ActionOptions} from "./index";
import Action from "./index";
import Snoowrap, {Comment, Submission} from "snoowrap";
import {RuleResult} from "../Rule";
import {activityIsRemoved} from "../Utils/SnoowrapUtils";
import {ActionProcessResult} from "../Common/interfaces";
import dayjs from "dayjs";
import {isSubmission} from "../util";
export class RemoveAction extends Action {
spam: boolean;
getKind() {
return 'Remove';
}
constructor(options: RemoveOptions) {
super(options);
const {
spam = false,
} = options;
this.spam = spam;
}
async process(item: Comment | Submission, ruleResults: RuleResult[], runtimeDryrun?: boolean): Promise<ActionProcessResult> {
const dryRun = runtimeDryrun || this.dryRun;
const touchedEntities = [];
// issue with snoowrap typings, doesn't think prop exists on Submission
// @ts-ignore
if (activityIsRemoved(item)) {
return {
dryRun,
success: false,
result: 'Item is already removed',
}
this.logger.warn('It looks like this Item is already removed!');
}
if (this.spam) {
this.logger.verbose('Marking as spam on removal');
}
if (!dryRun) {
// @ts-ignore
await item.remove();
await item.remove({spam: this.spam});
item.banned_at_utc = dayjs().unix();
item.spam = this.spam;
if(!isSubmission(item)) {
// @ts-ignore
item.removed = true;
}
await this.resources.resetCacheForItem(item);
touchedEntities.push(item);
}
return {
dryRun,
success: true,
touchedEntities
}
}
}
export interface RemoveActionConfig extends ActionConfig {
export interface RemoveOptions extends RemoveActionConfig, ActionOptions {
}
export interface RemoveActionConfig extends ActionConfig {
spam?: boolean
}
/**
* Remove the Activity
* */
export interface RemoveActionJson extends RemoveActionConfig, ActionJson {
kind: 'remove'
kind: 'remove'
}

View File

@@ -29,15 +29,21 @@ export class ReportAction extends Action {
const renderedContent = await renderContent(content, item, ruleResults, this.resources.userNotes);
this.logger.verbose(`Contents:\r\n${renderedContent}`);
const truncatedContent = reportTrunc(renderedContent);
const touchedEntities = [];
if(!dryRun) {
// @ts-ignore
await item.report({reason: truncatedContent});
// due to reddit not updating this in response (maybe)?? just increment stale activity
item.num_reports++;
await this.resources.resetCacheForItem(item);
touchedEntities.push(item);
}
return {
dryRun,
success: true,
result: truncatedContent
result: truncatedContent,
touchedEntities
};
}
}

View File

@@ -1,20 +1,23 @@
import {SubmissionActionConfig} from "./index";
import Action, {ActionJson, ActionOptions} from "../index";
import Snoowrap, {Comment, Submission} from "snoowrap";
import {RuleResult} from "../../Rule";
import {ActionProcessResult} from "../../Common/interfaces";
import Submission from 'snoowrap/dist/objects/Submission';
import Comment from 'snoowrap/dist/objects/Comment';
export class FlairAction extends Action {
text: string;
css: string;
flair_template_id: string;
constructor(options: FlairActionOptions) {
super(options);
if (options.text === undefined && options.css === undefined) {
throw new Error('Must define either text or css on FlairAction');
if (options.text === undefined && options.css === undefined && options.flair_template_id === undefined) {
throw new Error('Must define either text+css or flair_template_id on FlairAction');
}
this.text = options.text || '';
this.css = options.css || '';
this.flair_template_id = options.flair_template_id || '';
}
getKind() {
@@ -30,12 +33,26 @@ export class FlairAction extends Action {
if(this.css !== '') {
flairParts.push(`CSS: ${this.css}`);
}
if(this.flair_template_id !== '') {
flairParts.push(`Template: ${this.flair_template_id}`);
}
const flairSummary = flairParts.length === 0 ? 'No flair (unflaired)' : flairParts.join(' | ');
this.logger.verbose(flairSummary);
if (item instanceof Submission) {
if(!this.dryRun) {
// @ts-ignore
await item.assignFlair({text: this.text, cssClass: this.css})
if (this.flair_template_id) {
// typings are wrong for this function, flair_template_id should be accepted
// assignFlair uses /api/flair (mod endpoint)
// selectFlair uses /api/selectflair (self endpoint for user to choose their own flair for submission)
// @ts-ignore
await item.assignFlair({flair_template_id: this.flair_template_id}).then(() => {});
item.link_flair_template_id = this.flair_template_id;
} else {
await item.assignFlair({text: this.text, cssClass: this.css}).then(() => {});
item.link_flair_css_class = this.css;
item.link_flair_text = this.text;
}
await this.resources.resetCacheForItem(item);
}
} else {
this.logger.warn('Cannot flair Comment');
@@ -60,12 +77,16 @@ export class FlairAction extends Action {
export interface FlairActionConfig extends SubmissionActionConfig {
/**
* The text of the flair to apply
* */
* */
text?: string,
/**
* The text of the css class of the flair to apply
* */
css?: string,
/**
* Flair template ID to assign
* */
flair_template_id?: string,
}
export interface FlairActionOptions extends FlairActionConfig,ActionOptions {
@@ -76,5 +97,5 @@ export interface FlairActionOptions extends FlairActionConfig,ActionOptions {
* Flair the Submission
* */
export interface FlairActionJson extends FlairActionConfig, ActionJson {
kind: 'flair'
kind: 'flair'
}

View File

@@ -0,0 +1,117 @@
import Action, {ActionConfig, ActionJson, ActionOptions} from './index';
import {Comment, RedditUser, Submission} from 'snoowrap';
import {RuleResult} from '../Rule';
import {ActionProcessResult} from '../Common/interfaces';
export class UserFlairAction extends Action {
text?: string;
css?: string;
flair_template_id?: string;
constructor(options: UserFlairActionOptions) {
super(options);
this.text = options.text === null || options.text === '' ? undefined : options.text;
this.css = options.css === null || options.css === '' ? undefined : options.css;
this.flair_template_id = options.flair_template_id === null || options.flair_template_id === '' ? undefined : options.flair_template_id;
}
getKind() {
return 'User Flair';
}
async process(item: Comment | Submission, ruleResults: RuleResult[], runtimeDryrun?: boolean): Promise<ActionProcessResult> {
const dryRun = runtimeDryrun || this.dryRun;
let flairParts = [];
if (this.flair_template_id !== undefined) {
flairParts.push(`Flair template ID: ${this.flair_template_id}`)
if(this.text !== undefined || this.css !== undefined) {
this.logger.warn('Text/CSS properties will be ignored since a flair template is specified');
}
} else {
if (this.text !== undefined) {
flairParts.push(`Text: ${this.text}`);
}
if (this.css !== undefined) {
flairParts.push(`CSS: ${this.css}`);
}
}
const flairSummary = flairParts.length === 0 ? 'Unflair user' : flairParts.join(' | ');
this.logger.verbose(flairSummary);
if (!this.dryRun) {
if (this.flair_template_id !== undefined) {
try {
// @ts-ignore
await this.client.assignUserFlairByTemplateId({
subredditName: item.subreddit.display_name,
flairTemplateId: this.flair_template_id,
username: item.author.name,
});
item.author_flair_template_id = this.flair_template_id
} catch (err: any) {
this.logger.error('Either the flair template ID is incorrect or you do not have permission to access it.');
throw err;
}
} else if (this.text === undefined && this.css === undefined) {
// @ts-ignore
await item.subreddit.deleteUserFlair(item.author.name);
item.author_flair_css_class = null;
item.author_flair_text = null;
item.author_flair_template_id = null;
} else {
// @ts-ignore
await item.author.assignFlair({
subredditName: item.subreddit.display_name,
cssClass: this.css,
text: this.text,
});
item.author_flair_text = this.text ?? null;
item.author_flair_css_class = this.css ?? null;
}
await this.resources.resetCacheForItem(item);
await this.resources.resetCacheForItem(item.author);
}
return {
dryRun,
success: true,
result: flairSummary,
}
}
}
/**
* Flair the Author of an Activity
*
* Leave all properties blank or null to remove a User's existing flair
* */
export interface UserFlairActionConfig extends ActionConfig {
/**
* The text of the flair to apply
* */
text?: string,
/**
* The text of the css class of the flair to apply
* */
css?: string,
/**
* Flair template to pick.
*
* **Note:** If this template is used text/css are ignored
* */
flair_template_id?: string;
}
export interface UserFlairActionOptions extends UserFlairActionConfig, ActionOptions {
}
/**
* Flair the Submission
* */
export interface UserFlairActionJson extends UserFlairActionConfig, ActionJson {
kind: 'userflair'
}

View File

@@ -1,17 +1,19 @@
import Snoowrap, {Comment, Submission} from "snoowrap";
import {Comment, Submission} from "snoowrap";
import {Logger} from "winston";
import {RuleResult} from "../Rule";
import {SubredditResources} from "../Subreddit/SubredditResources";
import {checkAuthorFilter, SubredditResources} from "../Subreddit/SubredditResources";
import {ActionProcessResult, ActionResult, ChecksActivityState, TypedActivityStates} from "../Common/interfaces";
import Author, {AuthorOptions} from "../Author/Author";
import {mergeArr} from "../util";
import LoggedError from "../Utils/LoggedError";
import {ExtendedSnoowrap} from '../Utils/SnoowrapClients';
import {ErrorWithCause} from "pony-cause";
export abstract class Action {
name?: string;
logger: Logger;
resources: SubredditResources;
client: Snoowrap
client: ExtendedSnoowrap;
authorIs: AuthorOptions;
itemIs: TypedActivityStates;
dryRun: boolean;
@@ -27,6 +29,7 @@ export abstract class Action {
subredditName,
dryRun = false,
authorIs: {
excludeCondition = 'OR',
include = [],
exclude = [],
} = {},
@@ -41,6 +44,7 @@ export abstract class Action {
this.logger = logger.child({labels: [`Action ${this.getActionUniqueName()}`]}, mergeArr);
this.authorIs = {
excludeCondition,
exclude: exclude.map(x => new Author(x)),
include: include.map(x => new Author(x)),
}
@@ -71,36 +75,20 @@ export abstract class Action {
actRes.runReason = `Activity did not pass 'itemIs' test, Action not run`;
return actRes;
}
if (this.authorIs.include !== undefined && this.authorIs.include.length > 0) {
for (const auth of this.authorIs.include) {
if (await this.resources.testAuthorCriteria(item, auth)) {
actRes.run = true;
const results = await this.process(item, ruleResults, runtimeDryrun);
return {...actRes, ...results};
}
}
this.logger.verbose('Inclusive author criteria not matched, Action not run');
actRes.runReason = 'Inclusive author criteria not matched';
return actRes;
} else if (this.authorIs.exclude !== undefined && this.authorIs.exclude.length > 0) {
for (const auth of this.authorIs.exclude) {
if (await this.resources.testAuthorCriteria(item, auth, false)) {
actRes.run = true;
const results = await this.process(item, ruleResults, runtimeDryrun);
return {...actRes, ...results};
}
}
this.logger.verbose('Exclusive author criteria not matched, Action not run');
actRes.runReason = 'Exclusive author criteria not matched';
const [authFilterResult, authFilterType] = await checkAuthorFilter(item, this.authorIs, this.resources, this.logger);
if(!authFilterResult) {
this.logger.verbose(`${authFilterType} author criteria not matched, Action not run`);
actRes.runReason = `${authFilterType} author criteria not matched`;
return actRes;
}
actRes.run = true;
const results = await this.process(item, ruleResults, runtimeDryrun);
return {...actRes, ...results};
} catch (err) {
} catch (err: any) {
if(!(err instanceof LoggedError)) {
this.logger.error(`Encountered error while running`, err);
const actionError = new ErrorWithCause('Action did not run successfully due to unexpected error', {cause: err});
this.logger.error(actionError);
}
actRes.success = false;
actRes.result = err.message;
@@ -114,8 +102,8 @@ export abstract class Action {
export interface ActionOptions extends ActionConfig {
logger: Logger;
subredditName: string;
resources: SubredditResources
client: Snoowrap
resources: SubredditResources;
client: ExtendedSnoowrap;
}
export interface ActionConfig extends ChecksActivityState {
@@ -162,7 +150,7 @@ export interface ActionJson extends ActionConfig {
/**
* The type of action that will be performed
*/
kind: 'comment' | 'lock' | 'remove' | 'report' | 'approve' | 'ban' | 'flair' | 'usernote' | 'message'
kind: 'comment' | 'lock' | 'remove' | 'report' | 'approve' | 'ban' | 'flair' | 'usernote' | 'message' | 'userflair'
}
export const isActionJson = (obj: object): obj is ActionJson => {

View File

@@ -1,7 +1,7 @@
import winston, {Logger} from "winston";
import dayjs, {Dayjs} from "dayjs";
import {getLogger} from "./Utils/loggerFactory";
import {Invokee, OperatorConfig} from "./Common/interfaces";
import {Invokee, OperatorConfig, OperatorConfigWithFileContext, OperatorFileConfig} from "./Common/interfaces";
import Bot from "./Bot";
import LoggedError from "./Utils/LoggedError";
import {sleep} from "./util";
@@ -14,7 +14,10 @@ export class App {
error: any;
constructor(config: OperatorConfig) {
config: OperatorConfig;
fileConfig: OperatorFileConfig;
constructor(config: OperatorConfigWithFileContext) {
const {
operator: {
name,
@@ -23,6 +26,11 @@ export class App {
bots = [],
} = config;
const {fileConfig, ...rest} = config;
this.config = rest;
this.fileConfig = fileConfig;
this.logger = getLogger(config.logging);
this.logger.info(`Operators: ${name.length === 0 ? 'None Specified' : name.join(', ')}`)
@@ -74,7 +82,7 @@ export class App {
this.logger.error(err);
}
});
} catch (err) {
} catch (err: any) {
if (b.error === undefined) {
b.error = err.message;
}

View File

@@ -1,5 +1,5 @@
import {UserNoteCriteria} from "../Rule";
import {CompareValue, CompareValueOrPercent, DurationComparor} from "../Common/interfaces";
import {CompareValue, CompareValueOrPercent, DurationComparor, JoinOperands} from "../Common/interfaces";
import {parseStringToRegex} from "../util";
/**
@@ -12,7 +12,17 @@ export interface AuthorOptions {
* */
include?: AuthorCriteria[];
/**
* Only runs if `include` is not present. Will "pass" if any of set of the AuthorCriteria **does not** pass
* * OR => if ANY exclude condition "does not" pass then the exclude test passes
* * AND => if ALL exclude conditions "do not" pass then the exclude test passes
*
* Defaults to OR
* @default OR
* */
excludeCondition?: JoinOperands
/**
* Only runs if `include` is not present. Each AuthorCriteria is comprised of conditions that the Author being checked must "not" pass. See excludeCondition for set behavior
*
* EX: `isMod: true, name: Automoderator` => Will pass if the Author IS NOT a mod and IS NOT named Automoderator
* */
exclude?: AuthorCriteria[];
}
@@ -36,15 +46,20 @@ export interface AuthorCriteria {
* */
name?: string[],
/**
* A list of (user) flair css class values from the subreddit to match against
* A (user) flair css class (or list of) from the subreddit to match against
* @examples ["red"]
* */
flairCssClass?: string[],
flairCssClass?: string | string[],
/**
* A list of (user) flair text values from the subreddit to match against
* A (user) flair text value (or list of) from the subreddit to match against
* @examples ["Approved"]
* */
flairText?: string[],
flairText?: string | string[],
/**
* A (user) flair template id (or list of) from the subreddit to match against
* */
flairTemplate?: string | string[]
/**
* Is the author a moderator?
* */
@@ -136,8 +151,12 @@ export class Author implements AuthorCriteria {
constructor(options: AuthorCriteria) {
this.name = options.name;
this.flairCssClass = options.flairCssClass;
this.flairText = options.flairText;
if(options.flairCssClass !== undefined) {
this.flairCssClass = typeof options.flairCssClass === 'string' ? [options.flairCssClass] : options.flairCssClass;
}
if(options.flairText !== undefined) {
this.flairText = typeof options.flairText === 'string' ? [options.flairText] : options.flairText;
}
this.isMod = options.isMod;
this.userNotes = options.userNotes;
this.age = options.age;

View File

@@ -3,23 +3,35 @@ import {Logger} from "winston";
import dayjs, {Dayjs} from "dayjs";
import {Duration} from "dayjs/plugin/duration";
import EventEmitter from "events";
import {BotInstanceConfig, Invokee, PAUSED, RUNNING, STOPPED, SYSTEM, USER} from "../Common/interfaces";
import {
BotInstanceConfig,
FilterCriteriaDefaults,
Invokee,
PAUSED,
PollOn,
RUNNING,
STOPPED,
SYSTEM,
USER
} from "../Common/interfaces";
import {
createRetryHandler,
formatNumber,
formatNumber, getExceptionMessage,
mergeArr,
parseBool,
parseDuration,
parseSubredditName,
parseDuration, parseMatchMessage,
parseSubredditName, RetryOptions,
sleep,
snooLogWrapper
} from "../util";
import {Manager} from "../Subreddit/Manager";
import {ExtendedSnoowrap, ProxiedSnoowrap} from "../Utils/SnoowrapClients";
import {ModQueueStream, UnmoderatedStream} from "../Subreddit/Streams";
import {CommentStream, ModQueueStream, SPoll, SubmissionStream, UnmoderatedStream} from "../Subreddit/Streams";
import {BotResourcesManager} from "../Subreddit/SubredditResources";
import LoggedError from "../Utils/LoggedError";
import pEvent from "p-event";
import {SimpleError, isRateLimitError, isRequestError, isScopeError, isStatusError, CMError} from "../Utils/Errors";
import {ErrorWithCause} from "pony-cause";
class Bot {
@@ -31,6 +43,7 @@ class Bot {
running: boolean = false;
subreddits: string[];
excludeSubreddits: string[];
filterCriteriaDefaults?: FilterCriteriaDefaults
subManagers: Manager[] = [];
heartbeatInterval: number;
nextHeartbeat: Dayjs = dayjs();
@@ -41,16 +54,20 @@ class Bot {
nannyMode?: 'soft' | 'hard';
nannyRunning: boolean = false;
nextNannyCheck: Dayjs = dayjs().add(10, 'second');
sharedStreamRetryHandler: Function;
nannyRetryHandler: Function;
managerRetryHandler: Function;
nextExpiration: Dayjs = dayjs();
botName?: string;
botLink?: string;
botAccount?: string;
maxWorkers: number;
startedAt: Dayjs = dayjs();
sharedModqueue: boolean = false;
sharedStreams: PollOn[] = [];
streamListedOnce: string[] = [];
stagger: number;
apiSample: number[] = [];
apiRollingAvg: number = 0;
apiEstDepletion?: Duration;
@@ -73,6 +90,7 @@ class Bot {
const {
notifications,
name,
filterCriteriaDefaults,
subreddits: {
names = [],
exclude = [],
@@ -81,18 +99,20 @@ class Bot {
heartbeatInterval,
},
credentials: {
clientId,
clientSecret,
refreshToken,
accessToken,
reddit: {
clientId,
clientSecret,
refreshToken,
accessToken,
},
},
snoowrap: {
proxy,
debug,
},
polling: {
sharedMod,
stagger,
shared = [],
stagger = 2000,
},
queue: {
maxWorkers,
@@ -116,7 +136,8 @@ class Bot {
this.hardLimit = hardLimit;
this.wikiLocation = wikiConfig;
this.heartbeatInterval = heartbeatInterval;
this.sharedModqueue = sharedMod;
this.filterCriteriaDefaults = filterCriteriaDefaults;
this.sharedStreams = shared;
if(name !== undefined) {
this.botName = name;
}
@@ -171,64 +192,23 @@ class Bot {
this.client = proxy === undefined ? new ExtendedSnoowrap(creds) : new ProxiedSnoowrap({...creds, proxy});
this.client.config({
warnings: true,
maxRetryAttempts: 5,
maxRetryAttempts: 2,
debug,
logger: snooLogWrapper(this.logger.child({labels: ['Snoowrap']}, mergeArr)),
continueAfterRatelimitError: true,
continueAfterRatelimitError: false,
});
} catch (err) {
} catch (err: any) {
if(this.error === undefined) {
this.error = err.message;
this.logger.error(err);
}
}
const retryHandler = createRetryHandler({maxRequestRetry: 8, maxOtherRetry: 1}, this.logger);
this.sharedStreamRetryHandler = createRetryHandler({maxRequestRetry: 8, maxOtherRetry: 2}, this.logger);
this.nannyRetryHandler = createRetryHandler({maxRequestRetry: 5, maxOtherRetry: 1}, this.logger);
this.managerRetryHandler = createRetryHandler({maxRequestRetry: 8, maxOtherRetry: 8, waitOnRetry: false, clearRetryCountAfter: 2}, this.logger);
const modStreamErrorListener = (name: string) => async (err: any) => {
this.logger.error('Polling error occurred', err);
const shouldRetry = await retryHandler(err);
if(shouldRetry) {
defaultUnmoderatedStream.startInterval();
} else {
for(const m of this.subManagers) {
if(m.modStreamCallbacks.size > 0) {
m.notificationManager.handle('runStateChanged', `${name.toUpperCase()} Polling Stopped`, 'Encountered too many errors from Reddit while polling. Will try to restart on next heartbeat.');
}
}
this.logger.error(`Mod stream ${name.toUpperCase()} encountered too many errors while polling. Will try to restart on next heartbeat.`);
}
}
const modStreamListingListener = (name: string) => async (listing: (Comment|Submission)[]) => {
// dole out in order they were received
if(!this.streamListedOnce.includes(name)) {
this.streamListedOnce.push(name);
return;
}
for(const i of listing) {
const foundManager = this.subManagers.find(x => x.subreddit.display_name === i.subreddit.display_name && x.modStreamCallbacks.get(name) !== undefined);
if(foundManager !== undefined) {
foundManager.modStreamCallbacks.get(name)(i);
if(stagger !== undefined) {
await sleep(stagger);
}
}
}
}
const defaultUnmoderatedStream = new UnmoderatedStream(this.client, {subreddit: 'mod', limit: 100, clearProcessed: { size: 100, retain: 100 }});
// @ts-ignore
defaultUnmoderatedStream.on('error', modStreamErrorListener('unmoderated'));
defaultUnmoderatedStream.on('listing', modStreamListingListener('unmoderated'));
const defaultModqueueStream = new ModQueueStream(this.client, {subreddit: 'mod', limit: 100, clearProcessed: { size: 100, retain: 100 }});
// @ts-ignore
defaultModqueueStream.on('error', modStreamErrorListener('modqueue'));
defaultModqueueStream.on('listing', modStreamListingListener('modqueue'));
this.cacheManager.modStreams.set('unmoderated', defaultUnmoderatedStream);
this.cacheManager.modStreams.set('modqueue', defaultModqueueStream);
this.stagger = stagger ?? 2000;
process.on('uncaughtException', (e) => {
this.error = e;
@@ -253,33 +233,62 @@ class Bot {
});
}
createSharedStreamErrorListener = (name: string) => async (err: any) => {
const shouldRetry = await this.sharedStreamRetryHandler(err);
if(shouldRetry) {
(this.cacheManager.modStreams.get(name) as SPoll<any>).startInterval(false, 'Within retry limits');
} else {
for(const m of this.subManagers) {
if(m.sharedStreamCallbacks.size > 0) {
m.notificationManager.handle('runStateChanged', `${name.toUpperCase()} Polling Stopped`, 'Encountered too many errors from Reddit while polling. Will try to restart on next heartbeat.');
}
}
this.logger.error(`Mod stream ${name.toUpperCase()} encountered too many errors while polling. Will try to restart on next heartbeat.`);
}
}
createSharedStreamListingListener = (name: string) => async (listing: (Comment|Submission)[]) => {
// dole out in order they were received
if(!this.streamListedOnce.includes(name)) {
this.streamListedOnce.push(name);
return;
}
for(const i of listing) {
const foundManager = this.subManagers.find(x => x.subreddit.display_name === i.subreddit.display_name && x.sharedStreamCallbacks.get(name) !== undefined && x.eventsState.state === RUNNING);
if(foundManager !== undefined) {
foundManager.sharedStreamCallbacks.get(name)(i);
if(this.stagger !== undefined) {
await sleep(this.stagger);
}
}
}
}
async onTerminate(reason = 'The application was shutdown') {
for(const m of this.subManagers) {
await m.notificationManager.handle('runStateChanged', 'Application Shutdown', reason);
}
}
async testClient() {
async testClient(initial = true) {
try {
// @ts-ignore
await this.client.getMe();
this.logger.info('Test API call successful');
} catch (err) {
this.logger.error('An error occurred while trying to initialize the Reddit API Client which would prevent the entire application from running.');
if(err.name === 'StatusCodeError') {
const authHeader = err.response.headers['www-authenticate'];
if (authHeader !== undefined && authHeader.includes('insufficient_scope')) {
this.logger.error('Reddit responded with a 403 insufficient_scope. Please ensure you have chosen the correct scopes when authorizing your account.');
} else if(err.statusCode === 401) {
this.logger.error('It is likely a credential is missing or incorrect. Check clientId, clientSecret, refreshToken, and accessToken');
}
this.logger.error(`Error Message: ${err.message}`);
} else {
this.logger.error(err);
} catch (err: any) {
if (initial) {
this.logger.error('An error occurred while trying to initialize the Reddit API Client which would prevent the entire application from running.');
}
this.error = `Error occurred while testing Reddit API client: ${err.message}`;
err.logged = true;
throw err;
const hint = getExceptionMessage(err, {
401: 'Likely a credential is missing or incorrect. Check clientId, clientSecret, refreshToken, and accessToken',
400: 'Credentials may have been invalidated manually or by reddit due to behavior',
});
let msg = `Error occurred while testing Reddit API client${hint !== undefined ? `: ${hint}` : ''}`;
this.error = msg;
const clientError = new CMError(msg, {cause: err});
clientError.logged = true;
this.logger.error(clientError);
throw clientError;
}
}
@@ -298,10 +307,12 @@ class Bot {
}
this.logger.info(`Bot Name${botNameFromConfig ? ' (from config)' : ''}: ${this.botName}`);
for (const sub of await this.client.getModeratedSubreddits()) {
// TODO don't know a way to check permissions yet
availSubs.push(sub);
let subListing = await this.client.getModeratedSubreddits({count: 100});
while(!subListing.isFinished) {
subListing = await subListing.fetchMore({amount: 100});
}
availSubs = subListing.filter(x => x.display_name !== `u_${user.name}`);
this.logger.info(`u/${user.name} is a moderator of these subreddits: ${availSubs.map(x => x.display_name_prefixed).join(', ')}`);
let subsToRun: Subreddit[] = [];
@@ -320,30 +331,177 @@ class Bot {
}
} else {
if(this.excludeSubreddits.length > 0) {
this.logger.info(`Will run on all moderated subreddits but user-defined excluded: ${this.excludeSubreddits.join(', ')}`);
this.logger.info(`Will run on all moderated subreddits but own profile and user-defined excluded: ${this.excludeSubreddits.join(', ')}`);
const normalExcludes = this.excludeSubreddits.map(x => x.toLowerCase());
subsToRun = availSubs.filter(x => !normalExcludes.includes(x.display_name.toLowerCase()));
} else {
this.logger.info('No user-defined subreddit constraints detected, will run on all moderated subreddits');
this.logger.info(`No user-defined subreddit constraints detected, will run on all moderated subreddits EXCEPT own profile (${this.botAccount})`);
subsToRun = availSubs;
}
}
let subSchedule: Manager[] = [];
// get configs for subs we want to run on and build/validate them
for (const sub of subsToRun) {
const manager = new Manager(sub, this.client, this.logger, this.cacheManager, {dryRun: this.dryRun, sharedModqueue: this.sharedModqueue, wikiLocation: this.wikiLocation, botName: this.botName, maxWorkers: this.maxWorkers});
try {
await manager.parseConfiguration('system', true, {suppressNotification: true});
} catch (err) {
if (!(err instanceof LoggedError)) {
this.logger.error(`Config was not valid:`, {subreddit: sub.display_name_prefixed});
this.logger.error(err, {subreddit: sub.display_name_prefixed});
}
this.subManagers.push(this.createManager(sub));
} catch (err: any) {
}
}
for(const m of this.subManagers) {
try {
await this.initManager(m);
} catch (err: any) {
}
}
this.parseSharedStreams();
}
parseSharedStreams() {
const sharedCommentsSubreddits = !this.sharedStreams.includes('newComm') ? [] : this.subManagers.filter(x => x.isPollingShared('newComm')).map(x => x.subreddit.display_name);
if (sharedCommentsSubreddits.length > 0) {
const stream = this.cacheManager.modStreams.get('newComm');
if (stream === undefined || stream.subreddit !== sharedCommentsSubreddits.join('+')) {
let processed;
if (stream !== undefined) {
this.logger.info('Restarting SHARED COMMENT STREAM due to a subreddit config change');
stream.end('Replacing with a new stream with updated subreddits');
processed = stream.processed;
}
if (sharedCommentsSubreddits.length > 100) {
this.logger.warn(`SHARED COMMENT STREAM => Reddit can only combine 100 subreddits for getting new Comments but this bot has ${sharedCommentsSubreddits.length}`);
}
const defaultCommentStream = new CommentStream(this.client, {
subreddit: sharedCommentsSubreddits.join('+'),
limit: 100,
enforceContinuity: true,
logger: this.logger,
processed,
label: 'Shared Polling'
});
// @ts-ignore
defaultCommentStream.on('error', this.createSharedStreamErrorListener('newComm'));
defaultCommentStream.on('listing', this.createSharedStreamListingListener('newComm'));
this.cacheManager.modStreams.set('newComm', defaultCommentStream);
}
} else {
const stream = this.cacheManager.modStreams.get('newComm');
if (stream !== undefined) {
stream.end('Determined no managers are listening on shared stream parsing');
}
}
const sharedSubmissionsSubreddits = !this.sharedStreams.includes('newSub') ? [] : this.subManagers.filter(x => x.isPollingShared('newSub')).map(x => x.subreddit.display_name);
if (sharedSubmissionsSubreddits.length > 0) {
const stream = this.cacheManager.modStreams.get('newSub');
if (stream === undefined || stream.subreddit !== sharedSubmissionsSubreddits.join('+')) {
let processed;
if (stream !== undefined) {
this.logger.info('Restarting SHARED SUBMISSION STREAM due to a subreddit config change');
stream.end('Replacing with a new stream with updated subreddits');
processed = stream.processed;
}
if (sharedSubmissionsSubreddits.length > 100) {
this.logger.warn(`SHARED SUBMISSION STREAM => Reddit can only combine 100 subreddits for getting new Submissions but this bot has ${sharedSubmissionsSubreddits.length}`);
}
const defaultSubStream = new SubmissionStream(this.client, {
subreddit: sharedSubmissionsSubreddits.join('+'),
limit: 100,
enforceContinuity: true,
logger: this.logger,
processed,
label: 'Shared Polling'
});
// @ts-ignore
defaultSubStream.on('error', this.createSharedStreamErrorListener('newSub'));
defaultSubStream.on('listing', this.createSharedStreamListingListener('newSub'));
this.cacheManager.modStreams.set('newSub', defaultSubStream);
}
} else {
const stream = this.cacheManager.modStreams.get('newSub');
if (stream !== undefined) {
stream.end('Determined no managers are listening on shared stream parsing');
}
}
const isUnmoderatedShared = !this.sharedStreams.includes('unmoderated') ? false : this.subManagers.some(x => x.isPollingShared('unmoderated'));
const unmoderatedstream = this.cacheManager.modStreams.get('unmoderated');
if (isUnmoderatedShared && unmoderatedstream === undefined) {
const defaultUnmoderatedStream = new UnmoderatedStream(this.client, {
subreddit: 'mod',
limit: 100,
logger: this.logger,
label: 'Shared Polling'
});
// @ts-ignore
defaultUnmoderatedStream.on('error', this.createSharedStreamErrorListener('unmoderated'));
defaultUnmoderatedStream.on('listing', this.createSharedStreamListingListener('unmoderated'));
this.cacheManager.modStreams.set('unmoderated', defaultUnmoderatedStream);
} else if (!isUnmoderatedShared && unmoderatedstream !== undefined) {
unmoderatedstream.end('Determined no managers are listening on shared stream parsing');
}
const isModqueueShared = !this.sharedStreams.includes('modqueue') ? false : this.subManagers.some(x => x.isPollingShared('modqueue'));
const modqueuestream = this.cacheManager.modStreams.get('modqueue');
if (isModqueueShared && modqueuestream === undefined) {
const defaultModqueueStream = new ModQueueStream(this.client, {
subreddit: 'mod',
limit: 100,
logger: this.logger,
label: 'Shared Polling'
});
// @ts-ignore
defaultModqueueStream.on('error', this.createSharedStreamErrorListener('modqueue'));
defaultModqueueStream.on('listing', this.createSharedStreamListingListener('modqueue'));
this.cacheManager.modStreams.set('modqueue', defaultModqueueStream);
} else if (isModqueueShared && modqueuestream !== undefined) {
modqueuestream.end('Determined no managers are listening on shared stream parsing');
}
}
async initManager(manager: Manager) {
try {
await manager.parseConfiguration('system', true, {suppressNotification: true, suppressChangeEvent: true});
} catch (err: any) {
if(err.logged !== true) {
const normalizedError = new ErrorWithCause(`Bot could not start manager because config was not valid`, {cause: err});
// @ts-ignore
this.logger.error(normalizedError, {subreddit: manager.subreddit.display_name_prefixed});
} else {
this.logger.error('Bot could not start manager because config was not valid', {subreddit: manager.subreddit.display_name_prefixed});
}
}
}
createManager(sub: Subreddit): Manager {
const manager = new Manager(sub, this.client, this.logger, this.cacheManager, {
dryRun: this.dryRun,
sharedStreams: this.sharedStreams,
wikiLocation: this.wikiLocation,
botName: this.botName as string,
maxWorkers: this.maxWorkers,
filterCriteriaDefaults: this.filterCriteriaDefaults,
});
// all errors from managers will count towards bot-level retry count
manager.on('error', async (err) => await this.panicOnRetries(err));
manager.on('configChange', async () => {
this.parseSharedStreams();
await this.runSharedStreams(false);
});
return manager;
}
// if the cumulative errors exceeds configured threshold then stop ALL managers as there is most likely something very bad happening
async panicOnRetries(err: any) {
if(!await this.managerRetryHandler(err)) {
this.logger.warn('Bot detected too many errors from managers within a short time. Stopping all managers and will try to restart on next heartbeat.');
for(const m of this.subManagers) {
await m.stop('system',{reason: 'Bot detected too many errors from all managers. Stopping all manager as a failsafe.'});
}
subSchedule.push(manager);
}
this.subManagers = subSchedule;
}
async destroy(causedBy: Invokee) {
@@ -357,14 +515,49 @@ class Bot {
this.logger.info('Bot is stopped.');
}
async runModStreams(notify = false) {
async checkModInvites() {
const subs: string[] = await this.cacheManager.getPendingSubredditInvites();
for (const name of subs) {
try {
// @ts-ignore
await this.client.getSubreddit(name).acceptModeratorInvite();
this.logger.info(`Accepted moderator invite for r/${name}!`);
await this.cacheManager.deletePendingSubredditInvite(name);
// @ts-ignore
const sub = await this.client.getSubreddit(name);
this.logger.info(`Attempting to add manager for r/${name}`);
try {
const manager = this.createManager(sub);
this.logger.info(`Starting manager for r/${name}`);
this.subManagers.push(manager);
await this.initManager(manager);
await manager.start('system', {reason: 'Caused by creation due to moderator invite'});
await this.runSharedStreams();
} catch (err: any) {
if (!(err instanceof LoggedError)) {
this.logger.error(err);
}
}
} catch (err: any) {
if (err.message.includes('NO_INVITE_FOUND')) {
this.logger.warn(`No pending moderation invite for r/${name} was found`);
} else if (isStatusError(err) && err.statusCode === 403) {
this.logger.error(`Error occurred while checking r/${name} for a pending moderation invite. It is likely that this bot does not have the 'modself' oauth permission. Error: ${err.message}`);
} else {
this.logger.error(`Error occurred while checking r/${name} for a pending moderation invite. Error: ${err.message}`);
}
}
}
}
async runSharedStreams(notify = false) {
for(const [k,v] of this.cacheManager.modStreams) {
if(!v.running && this.subManagers.some(x => x.modStreamCallbacks.get(k) !== undefined)) {
if(!v.running && this.subManagers.some(x => x.sharedStreamCallbacks.get(k) !== undefined)) {
v.startInterval();
this.logger.info(`Starting default ${k.toUpperCase()} mod stream`);
this.logger.info(`Starting ${k.toUpperCase()} shared polling`);
if(notify) {
for(const m of this.subManagers) {
if(m.modStreamCallbacks.size > 0) {
if(m.sharedStreamCallbacks.size > 0) {
await m.notificationManager.handle('runStateChanged', `${k.toUpperCase()} Polling Started`, 'Polling was successfully restarted on heartbeat.');
}
}
@@ -375,6 +568,8 @@ class Bot {
}
async runManagers(causedBy: Invokee = 'system') {
this.running = true;
if(this.subManagers.every(x => !x.validConfigLoaded)) {
this.logger.warn('All managers have invalid configs!');
this.error = 'All managers have invalid configs';
@@ -382,15 +577,15 @@ class Bot {
for (const manager of this.subManagers) {
if (manager.validConfigLoaded && manager.botState.state !== RUNNING) {
await manager.start(causedBy, {reason: 'Caused by application startup'});
await sleep(2000);
await sleep(this.stagger);
}
}
await this.runModStreams();
await this.runSharedStreams();
this.running = true;
this.nextNannyCheck = dayjs().add(10, 'second');
this.nextHeartbeat = dayjs().add(this.heartbeatInterval, 'second');
await this.checkModInvites();
await this.healthLoop();
}
@@ -404,15 +599,16 @@ class Bot {
try {
await this.runApiNanny();
this.nextNannyCheck = dayjs().add(10, 'second');
} catch (err) {
this.logger.info('Delaying next nanny check for 1 minute due to emitted error');
this.nextNannyCheck = dayjs().add(120, 'second');
} catch (err: any) {
this.logger.info('Delaying next nanny check for 4 minutes due to emitted error');
this.nextNannyCheck = dayjs().add(240, 'second');
}
}
if(dayjs().isSameOrAfter(this.nextHeartbeat)) {
try {
await this.heartbeat();
} catch (err) {
await this.checkModInvites();
} catch (err: any) {
this.logger.error(`Error occurred during heartbeat check: ${err.message}`);
}
this.nextHeartbeat = dayjs().add(this.heartbeatInterval, 'second');
@@ -424,20 +620,39 @@ class Bot {
async heartbeat() {
const heartbeat = `HEARTBEAT -- API Remaining: ${this.client.ratelimitRemaining} | Usage Rolling Avg: ~${formatNumber(this.apiRollingAvg)}/s | Est Depletion: ${this.apiEstDepletion === undefined ? 'N/A' : this.apiEstDepletion.humanize()} (${formatNumber(this.depletedInSecs, {toFixed: 0})} seconds)`
this.logger.info(heartbeat);
// run sanity check to see if there is a service issue
try {
await this.testClient(false);
} catch (err: any) {
throw new SimpleError(`Something isn't right! This could be a Reddit API issue (service is down? buggy??) or an issue with the Bot account. Will not run heartbeat operations and will wait until next heartbeat (${dayjs.duration(this.nextHeartbeat.diff(dayjs())).humanize()}) to try again`);
}
let startedAny = false;
for (const s of this.subManagers) {
if(s.botState.state === STOPPED && s.botState.causedBy === USER) {
this.logger.debug('Skipping config check/restart on heartbeat due to previously being stopped by user', {subreddit: s.displayLabel});
continue;
}
try {
// ensure calls to wiki page are also staggered so we aren't hitting api hard when bot has a ton of subreddits to check
await sleep(this.stagger);
const newConfig = await s.parseConfiguration();
if(newConfig || (s.queueState.state !== RUNNING && s.queueState.causedBy === SYSTEM))
{
await s.startQueue('system', {reason: newConfig ? 'Config updated on heartbeat triggered reload' : 'Heartbeat detected non-running queue'});
}
if(newConfig || (s.eventsState.state !== RUNNING && s.eventsState.causedBy === SYSTEM))
{
await s.startEvents('system', {reason: newConfig ? 'Config updated on heartbeat triggered reload' : 'Heartbeat detected non-running events'});
const willStart = newConfig || (s.queueState.state !== RUNNING && s.queueState.causedBy === SYSTEM) || (s.eventsState.state !== RUNNING && s.eventsState.causedBy === SYSTEM);
if(willStart) {
// stagger restart
if (startedAny) {
await sleep(this.stagger);
}
startedAny = true;
if(newConfig || (s.queueState.state !== RUNNING && s.queueState.causedBy === SYSTEM))
{
await s.startQueue('system', {reason: newConfig ? 'Config updated on heartbeat triggered reload' : 'Heartbeat detected non-running queue'});
}
if(newConfig || (s.eventsState.state !== RUNNING && s.eventsState.causedBy === SYSTEM))
{
await s.startEvents('system', {reason: newConfig ? 'Config updated on heartbeat triggered reload' : 'Heartbeat detected non-running events'});
}
}
if(s.botState.state !== RUNNING && s.eventsState.state === RUNNING && s.queueState.state === RUNNING) {
s.botState = {
@@ -445,10 +660,12 @@ class Bot {
causedBy: 'system',
}
}
} catch (err) {
this.logger.info('Stopping event polling to prevent activity processing queue from backing up. Will be restarted when config update succeeds.')
await s.stopEvents('system', {reason: 'Invalid config will cause events to pile up in queue. Will be restarted when config update succeeds (next heartbeat).'});
if(!(err instanceof LoggedError)) {
} catch (err: any) {
if(s.eventsState.state === RUNNING) {
this.logger.info('Stopping event polling to prevent activity processing queue from backing up. Will be restarted when config update succeeds.')
await s.stopEvents('system', {reason: 'Invalid config will cause events to pile up in queue. Will be restarted when config update succeeds (next heartbeat).'});
}
if(err.logged !== true) {
this.logger.error(err, {subreddit: s.displayLabel});
}
if(this.nextHeartbeat !== undefined) {
@@ -456,7 +673,7 @@ class Bot {
}
}
}
await this.runModStreams(true);
await this.runSharedStreams(true);
}
async runApiNanny() {
@@ -472,7 +689,10 @@ class Bot {
// @ts-ignore
await this.client.getMe();
shouldRetry = false;
} catch (err) {
} catch (err: any) {
if(isRateLimitError(err)) {
throw err;
}
shouldRetry = await this.nannyRetryHandler(err);
if (!shouldRetry) {
throw err;
@@ -521,6 +741,10 @@ class Bot {
m.notificationManager.handle('runStateChanged', 'Hard Limit Triggered', `Hard Limit of ${this.hardLimit} hit (API Remaining: ${this.client.ratelimitRemaining}). Subreddit event polling has been paused.`, 'system', 'warn');
}
for(const [k,v] of this.cacheManager.modStreams) {
v.end('Hard limit cutoff');
}
this.nannyMode = 'hard';
return;
}
@@ -587,10 +811,11 @@ class Bot {
await m.startEvents('system', {reason: 'API Nanny has been turned off due to better API conditions'});
}
}
await this.runSharedStreams(true);
this.nannyMode = undefined;
}
} catch (err) {
} catch (err: any) {
this.logger.error(`Error occurred during nanny loop: ${err.message}`);
throw err;
}

View File

@@ -28,8 +28,11 @@ import * as RuleSchema from '../Schema/Rule.json';
import * as RuleSetSchema from '../Schema/RuleSet.json';
import * as ActionSchema from '../Schema/Action.json';
import {ActionObjectJson, RuleJson, RuleObjectJson, ActionJson as ActionTypeJson} from "../Common/types";
import {SubredditResources} from "../Subreddit/SubredditResources";
import {Author, AuthorCriteria, AuthorOptions} from "../Author/Author";
import {checkAuthorFilter, SubredditResources} from "../Subreddit/SubredditResources";
import {Author, AuthorCriteria, AuthorOptions} from '..';
import {ExtendedSnoowrap} from '../Utils/SnoowrapClients';
import {isRateLimitError} from "../Utils/Errors";
import {ErrorWithCause} from "pony-cause";
const checkLogName = truncateStringToLength(25);
@@ -42,15 +45,12 @@ export abstract class Check implements ICheck {
rules: Array<RuleSet | Rule> = [];
logger: Logger;
itemIs: TypedActivityStates;
authorIs: {
include: AuthorCriteria[],
exclude: AuthorCriteria[]
};
authorIs: AuthorOptions;
cacheUserResult: Required<UserResultCacheOptions>;
dryRun?: boolean;
notifyOnTrigger: boolean;
resources: SubredditResources;
client: Snoowrap;
client: ExtendedSnoowrap;
constructor(options: CheckOptions) {
const {
@@ -68,6 +68,7 @@ export abstract class Check implements ICheck {
itemIs = [],
authorIs: {
include = [],
excludeCondition,
exclude = [],
} = {},
dryRun,
@@ -88,6 +89,7 @@ export abstract class Check implements ICheck {
this.condition = condition;
this.itemIs = itemIs;
this.authorIs = {
excludeCondition,
exclude: exclude.map(x => new Author(x)),
include: include.map(x => new Author(x)),
}
@@ -158,7 +160,7 @@ export abstract class Check implements ICheck {
runStats.push(`${this.actions.length} Actions`);
// not sure if this should be info or verbose
this.logger.info(`=${this.enabled ? 'Enabled' : 'Disabled'}= ${type.toUpperCase()} (${this.condition})${this.notifyOnTrigger ? ' ||Notify on Trigger|| ' : ''} => ${runStats.join(' | ')}${this.description !== undefined ? ` => ${this.description}` : ''}`);
if (this.rules.length === 0 && this.itemIs.length === 0 && this.authorIs.exclude.length === 0 && this.authorIs.include.length === 0) {
if (this.rules.length === 0 && this.itemIs.length === 0 && this.authorIs.exclude?.length === 0 && this.authorIs.include?.length === 0) {
this.logger.warn('No rules, item tests, or author test found -- this check will ALWAYS PASS!');
}
let ruleSetIndex = 1;
@@ -201,30 +203,9 @@ export abstract class Check implements ICheck {
this.logger.verbose(`${FAIL} => Item did not pass 'itemIs' test`);
return [false, allRuleResults];
}
let authorPass = null;
if (this.authorIs.include !== undefined && this.authorIs.include.length > 0) {
for (const auth of this.authorIs.include) {
if (await this.resources.testAuthorCriteria(item, auth)) {
authorPass = true;
break;
}
}
if (!authorPass) {
this.logger.verbose(`${FAIL} => Inclusive author criteria not matched`);
return Promise.resolve([false, allRuleResults]);
}
}
if (authorPass === null && this.authorIs.exclude !== undefined && this.authorIs.exclude.length > 0) {
for (const auth of this.authorIs.exclude) {
if (await this.resources.testAuthorCriteria(item, auth, false)) {
authorPass = true;
break;
}
}
if (!authorPass) {
this.logger.verbose(`${FAIL} => Exclusive author criteria not matched`);
return Promise.resolve([false, allRuleResults]);
}
const [authFilterResult, authFilterType] = await checkAuthorFilter(item, this.authorIs, this.resources, this.logger);
if(!authFilterResult) {
return Promise.resolve([false, allRuleResults]);
}
if (this.rules.length === 0) {
@@ -268,10 +249,8 @@ export abstract class Check implements ICheck {
// otherwise AND and did not return already so all passed
this.logger.info(`${PASS} => Rules: ${resultsSummary(allResults, this.condition)}`);
return [true, allRuleResults];
} catch (e) {
e.logged = true;
this.logger.warn(`Running rules failed due to uncaught exception`, e);
throw e;
} catch (e: any) {
throw new ErrorWithCause('Running rules failed due to error', {cause: e});
}
}
@@ -345,13 +324,13 @@ export interface ICheck extends JoinCondition, ChecksActivityState {
}
export interface CheckOptions extends ICheck {
rules: Array<IRuleSet | IRule>
actions: ActionConfig[]
logger: Logger
subredditName: string
notifyOnTrigger?: boolean
resources: SubredditResources
client: Snoowrap
rules: Array<IRuleSet | IRule>;
actions: ActionConfig[];
logger: Logger;
subredditName: string;
notifyOnTrigger?: boolean;
resources: SubredditResources;
client: ExtendedSnoowrap;
cacheUserResult?: UserResultCacheOptions;
}

View File

@@ -0,0 +1,27 @@
import {ConfigFormat} from "../types";
export interface ConfigDocumentInterface<DocumentType> {
format: ConfigFormat;
parsed: DocumentType
//parsingError: Error | string;
raw: string;
location?: string;
toString(): string;
toJS(): object;
}
abstract class AbstractConfigDocument<DocumentType> implements ConfigDocumentInterface<DocumentType> {
public abstract format: ConfigFormat;
public abstract parsed: DocumentType;
//public abstract parsingError: Error | string;
constructor(public raw: string, public location?: string) {
}
public abstract toString(): string;
public abstract toJS(): object;
}
export default AbstractConfigDocument;

View File

@@ -0,0 +1,30 @@
import AbstractConfigDocument from "./AbstractConfigDocument";
import {stringify, parse} from 'comment-json';
import JSON5 from 'json5';
import {ConfigFormat} from "../types";
import {OperatorJsonConfig} from "../interfaces";
class JsonConfigDocument extends AbstractConfigDocument<OperatorJsonConfig> {
public parsed: OperatorJsonConfig;
protected cleanParsed: OperatorJsonConfig;
public format: ConfigFormat;
public constructor(raw: string, location?: string) {
super(raw, location);
this.parsed = parse(raw);
this.cleanParsed = JSON5.parse(raw);
this.format = 'json';
}
public toJS(): OperatorJsonConfig {
return this.cleanParsed;
}
public toString(): string {
return stringify(this.parsed, null, 1);
}
}
export default JsonConfigDocument;

View File

@@ -0,0 +1,54 @@
import YamlConfigDocument from "../YamlConfigDocument";
import JsonConfigDocument from "../JsonConfigDocument";
import {YAMLMap, YAMLSeq} from "yaml";
import {BotInstanceJsonConfig, OperatorJsonConfig} from "../../interfaces";
import {assign} from 'comment-json';
export interface OperatorConfigDocumentInterface {
addBot(botData: BotInstanceJsonConfig): void;
toJS(): OperatorJsonConfig;
}
export class YamlOperatorConfigDocument extends YamlConfigDocument implements OperatorConfigDocumentInterface {
addBot(botData: BotInstanceJsonConfig) {
const bots = this.parsed.get('bots') as YAMLSeq;
if (bots === undefined) {
this.parsed.add({key: 'bots', value: [botData]});
} else if (botData.name !== undefined) {
// overwrite if we find an existing
const existingIndex = bots.items.findIndex(x => (x as YAMLMap).get('name') === botData.name);
if (existingIndex !== -1) {
this.parsed.setIn(['bots', existingIndex], botData);
} else {
this.parsed.addIn(['bots'], botData);
}
} else {
this.parsed.addIn(['bots'], botData);
}
}
toJS(): OperatorJsonConfig {
return super.toJS();
}
}
export class JsonOperatorConfigDocument extends JsonConfigDocument implements OperatorConfigDocumentInterface {
addBot(botData: BotInstanceJsonConfig) {
if (this.parsed.bots === undefined) {
this.parsed.bots = [botData];
} else if (botData.name !== undefined) {
const existingIndex = this.parsed.bots.findIndex(x => x.name === botData.name);
if (existingIndex !== -1) {
this.parsed.bots[existingIndex] = assign(this.parsed.bots[existingIndex], botData);
} else {
this.parsed.bots.push(botData);
}
} else {
this.parsed.bots.push(botData);
}
}
toJS(): OperatorJsonConfig {
return super.toJS();
}
}

View File

@@ -0,0 +1,24 @@
import AbstractConfigDocument from "./AbstractConfigDocument";
import {Document, parseDocument} from 'yaml';
import {ConfigFormat} from "../types";
class YamlConfigDocument extends AbstractConfigDocument<Document> {
public parsed: Document;
public format: ConfigFormat;
public constructor(raw: string, location?: string) {
super(raw, location);
this.parsed = parseDocument(raw);
this.format = 'yaml';
}
public toJS(): object {
return this.parsed.toJS();
}
public toString(): string {
return this.parsed.toString();
}
}
export default YamlConfigDocument;

View File

@@ -3,9 +3,9 @@ import {Submission} from "snoowrap/dist/objects";
import {URL} from "url";
import {absPercentDifference, getSharpAsync, isValidImageURL} from "../util";
import sizeOf from "image-size";
import SimpleError from "../Utils/SimpleError";
import {Sharp} from "sharp";
import {blockhash} from "./blockhash/blockhash";
import {SimpleError} from "../Utils/Errors";
export interface ImageDataOptions {
width?: number,
@@ -83,7 +83,7 @@ class ImageData {
}
} catch (err) {
} catch (err: any) {
if(!(err instanceof SimpleError)) {
throw new Error(`Error occurred while fetching response from URL: ${err.message}`);
} else {

View File

@@ -1,7 +1,7 @@
import {HistoricalStats} from "./interfaces";
import {HistoricalStats, FilterCriteriaDefaults} from "./interfaces";
export const cacheOptDefaults = {ttl: 60, max: 500, checkPeriod: 600};
export const cacheTTLDefaults = {authorTTL: 60, userNotesTTL: 300, wikiTTL: 300, submissionTTL: 60, commentTTL: 60, filterCriteriaTTL: 60, subredditTTL: 600};
export const cacheTTLDefaults = {authorTTL: 60, userNotesTTL: 300, wikiTTL: 300, submissionTTL: 60, commentTTL: 60, filterCriteriaTTL: 60, subredditTTL: 600, selfTTL: 60};
export const historicalDefaults: HistoricalStats = {
eventsCheckedTotal: 0,
eventsActionedTotal: 0,
@@ -29,3 +29,13 @@ export const createHistoricalDefaults = (): HistoricalStats => {
actionsRun: new Map(),
};
}
export const filterCriteriaDefault: FilterCriteriaDefaults = {
authorIs: {
exclude: [
{
isMod: true
}
]
}
}

View File

@@ -5,6 +5,17 @@ import Poll from "snoostorm/out/util/Poll";
import Snoowrap from "snoowrap";
import {RuleResult} from "../Rule";
import {IncomingMessage} from "http";
import Submission from "snoowrap/dist/objects/Submission";
import Comment from "snoowrap/dist/objects/Comment";
import RedditUser from "snoowrap/dist/objects/RedditUser";
import {AuthorCriteria, AuthorOptions} from "../Author/Author";
import {ConfigFormat} from "./types";
import AbstractConfigDocument, {ConfigDocumentInterface} from "./Config/AbstractConfigDocument";
import {Document as YamlDocument} from 'yaml';
import {JsonOperatorConfigDocument, YamlOperatorConfigDocument} from "./Config/Operator";
import {ConsoleTransportOptions} from "winston/lib/winston/transports";
import {DailyRotateFileTransportOptions} from "winston-daily-rotate-file";
import {DuplexTransportOptions} from "winston-duplex/dist/DuplexTransport";
/**
* An ISO 8601 Duration
@@ -486,38 +497,6 @@ export type PollOn = 'unmoderated' | 'modqueue' | 'newSub' | 'newComm';
export interface PollingOptionsStrong extends PollingOptions {
limit: number,
interval: number,
clearProcessed: ClearProcessedOptions
}
/**
* For very long-running, high-volume subreddits clearing the list of processed activities helps manage memory bloat
*
* All of these options have default values based on the limit and/or interval set for polling options on each subreddit stream. They only need to modified if the defaults are not sufficient.
*
* If both `after` and `size` are defined whichever is hit first will trigger the list to clear. `after` will be reset after ever clear.
* */
export interface ClearProcessedOptions {
/**
* An interval the processed list should be cleared after.
*
* * EX `9 days`
* * EX `3 months`
* * EX `5 minutes`
* @pattern ^\s*(?<time>\d+)\s*(?<unit>days?|weeks?|months?|years?|hours?|minutes?|seconds?|milliseconds?)\s*$
* */
after?: string,
/**
* Number of activities found in processed list after which the list should be cleared.
*
* Defaults to the `limit` value from `PollingOptions`
* */
size?: number,
/**
* The number of activities to retain in processed list after clearing.
*
* Defaults to `limit` value from `PollingOptions`
* */
retain?: number,
}
export interface PollingDefaults {
@@ -591,8 +570,6 @@ export interface PollingOptions extends PollingDefaults {
*
* */
pollOn: 'unmoderated' | 'modqueue' | 'newSub' | 'newComm'
clearProcessed?: ClearProcessedOptions
}
export interface TTLConfig {
@@ -670,6 +647,24 @@ export interface TTLConfig {
* @default 60
* */
filterCriteriaTTL?: number | boolean;
/**
* Amount of time, in seconds, an Activity that the bot has acted on or created will be ignored if found during polling
*
* This is useful to prevent the bot from checking Activities it *just* worked on or a product of the checks. Examples:
*
* * Ignore comments created through an Action
* * Ignore Activity polled from modqueue that the bot just reported
*
* This value should be at least as long as the longest polling interval for modqueue/newComm
*
* * If `0` or `true` will cache indefinitely (not recommended)
* * If `false` will not cache
*
* @examples [50]
* @default 50
* */
selfTTL?: number | boolean
}
export interface CacheConfig extends TTLConfig {
@@ -832,6 +827,15 @@ export interface ManagerOptions {
nickname?: string
notifications?: NotificationConfig
credentials?: ThirdPartyCredentialsJsonConfig
/**
* Set the default filter criteria for all checks. If this property is specified it will override any defaults passed from the bot's config
*
* Default behavior is to exclude all mods and automoderator from checks
* */
filterCriteriaDefaults?: FilterCriteriaDefaults
}
/**
@@ -907,6 +911,20 @@ export interface ActivityState {
distinguished?: boolean
approved?: boolean
score?: CompareValue
/**
* A string containing a comparison operator and a value to compare against
*
* The syntax is `(< OR > OR <= OR >=) <number>`
*
* * EX `> 2` => greater than 2 total reports
*
* Defaults to TOTAL reports on an Activity. Suffix the value with the report type to check that type:
*
* * EX `> 3 mod` => greater than 3 mod reports
* * EX `>= 1 user` => greater than 1 user report
*
* @pattern ^\s*(>|>=|<|<=)\s*(\d+)\s*(%?)(.*)$
* */
reports?: CompareValue
age?: DurationComparor
}
@@ -928,8 +946,13 @@ export interface SubmissionState extends ActivityState {
* */
title?: string
link_flair_text?: string
link_flair_css_class?: string
link_flair_text?: string | string[]
link_flair_css_class?: string | string[]
flairTemplate?: string | string[]
/**
* Is the submission a reddit-hosted image or video?
* */
isRedditMediaDomain?: boolean
}
// properties calculated/derived by CM -- not provided as plain values by reddit
@@ -948,6 +971,14 @@ export interface CommentState extends ActivityState {
* A list of SubmissionState attributes to test the Submission this comment is in
* */
submissionState?: SubmissionState[]
/**
* The (nested) level of a comment.
*
* * 0 mean the comment is at top-level (replying to submission)
* * non-zero, Nth value means the comment has N parent comments
* */
depth?: DurationComparor
}
/**
@@ -979,6 +1010,8 @@ export interface SubredditState {
* A friendly description of what this State is trying to parse
* */
stateDescription?: string
isUserProfile?: boolean
}
export interface StrongSubredditState extends SubredditState {
@@ -1006,6 +1039,28 @@ export const STOPPED = 'stopped';
export const RUNNING = 'running';
export const PAUSED = 'paused';
export interface SearchAndReplaceRegExp {
/**
* The search value to test for
*
* Can be a normal string (converted to a case-sensitive literal) or a valid regular expression
*
* EX `["find this string", "/some string*\/ig"]`
*
* @examples ["find this string", "/some string*\/ig"]
* */
search: string
/**
* The replacement string/value to use when search is found
*
* This can be a literal string like `'replace with this`, an empty string to remove the search value (`''`), or a special regex value
*
* See replacement here for more information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
* */
replace: string
}
export interface NamedGroup {
[name: string]: string
}
@@ -1023,6 +1078,92 @@ export interface RegExResult {
}
type LogLevel = "error" | "warn" | "info" | "verbose" | "debug";
export type LogConsoleOptions = Pick<ConsoleTransportOptions, 'silent' | 'eol' | 'stderrLevels' | 'consoleWarnLevels'> & {
level?: LogLevel
}
export type LogFileOptions = Omit<DailyRotateFileTransportOptions, 'stream' | 'handleRejections' | 'options' | 'handleExceptions' | 'format' | 'log' | 'logv' | 'close' | 'dirname'> & {
level?: LogLevel
/**
* The absolute path to a directory where rotating log files should be stored.
*
* * If not present or `null` or `false` no log files will be created
* * If `true` logs will be stored at `[working directory]/logs`
*
* * ENV => `LOG_DIR`
* * ARG => `--logDir [dir]`
*
* @examples ["/var/log/contextmod"]
* */
dirname?: string | boolean | null
}
// export type StrongFileOptions = LogFileOptions & {
// dirname?: string
// }
export type LogStreamOptions = Omit<DuplexTransportOptions, 'name' | 'stream' | 'handleRejections' | 'handleExceptions' | 'format' | 'log' | 'logv' | 'close'> & {
level?: LogLevel
}
export interface LoggingOptions {
/**
* The minimum log level to output. The log level set will output logs at its level **and all levels above it:**
*
* * `error`
* * `warn`
* * `info`
* * `verbose`
* * `debug`
*
* Note: `verbose` will display *a lot* of information on the status/result of run rules/checks/actions etc. which is very useful for testing configurations. Once your bot is stable changing the level to `info` will reduce log noise.
*
* * ENV => `LOG_LEVEL`
* * ARG => `--logLevel <level>`
*
* @default "verbose"
* @examples ["verbose"]
* */
level?: LogLevel,
/**
* **DEPRECATED** - Use `file.dirname` instead
* The absolute path to a directory where rotating log files should be stored.
*
* * If not present or `null` or `false` no log files will be created
* * If `true` logs will be stored at `[working directory]/logs`
*
* * ENV => `LOG_DIR`
* * ARG => `--logDir [dir]`
*
* @examples ["/var/log/contextmod"]
* @deprecated
* @see logging.file.dirname
* */
path?: string | boolean | null
/**
* Options for Rotating File logging
* */
file?: LogFileOptions
/**
* Options for logging to api/web
* */
stream?: LogStreamOptions
/**
* Options for logging to console
* */
console?: LogConsoleOptions
}
export type StrongLoggingOptions = Required<Pick<LoggingOptions, 'stream' | 'console' | 'file'>> & {
level?: LogLevel
};
export type LoggerFactoryOptions = StrongLoggingOptions & {
additionalTransports?: any[]
defaultLabel?: string
}
/**
* Available cache providers
* */
@@ -1038,6 +1179,7 @@ export type StrongCache = {
submissionTTL: number | boolean,
commentTTL: number | boolean,
subredditTTL: number | boolean,
selfTTL: number | boolean,
filterCriteriaTTL: number | boolean,
provider: CacheOptions
actionedEventsMax?: number,
@@ -1150,6 +1292,7 @@ export interface Notifier {
export interface ManagerStateChangeOption {
reason?: string
suppressNotification?: boolean
suppressChangeEvent?: boolean
}
/**
@@ -1255,6 +1398,53 @@ export interface WebCredentials {
redirectUri?: string,
}
export interface SnoowrapOptions {
/**
* Proxy all requests to Reddit's API through this endpoint
*
* * ENV => `PROXY`
* * ARG => `--proxy <proxyEndpoint>`
*
* @examples ["http://localhost:4443"]
* */
proxy?: string,
/**
* Manually set the debug status for snoowrap
*
* When snoowrap has `debug: true` it will log the http status response of reddit api requests to at the `debug` level
*
* * Set to `true` to always output
* * Set to `false` to never output
*
* If not present or `null` will be set based on `logLevel`
*
* * ENV => `SNOO_DEBUG`
* * ARG => `--snooDebug`
* */
debug?: boolean,
}
export type FilterCriteriaDefaultBehavior = 'replace' | 'merge';
export interface FilterCriteriaDefaults {
itemIs?: TypedActivityStates
/**
* Determine how itemIs defaults behave when itemIs is present on the check
*
* * merge => adds defaults to check's itemIs
* * replace => check itemIs will replace defaults (no defaults used)
* */
itemIsBehavior?: FilterCriteriaDefaultBehavior
/**
* Determine how authorIs defaults behave when authorIs is present on the check
*
* * merge => merges defaults with check's authorIs
* * replace => check authorIs will replace defaults (no defaults used)
* */
authorIs?: AuthorOptions
authorIsBehavior?: FilterCriteriaDefaultBehavior
}
/**
* The configuration for an **individual reddit account** ContextMod will run as a bot.
*
@@ -1264,7 +1454,7 @@ export interface WebCredentials {
*
* */
export interface BotInstanceJsonConfig {
credentials?: RedditCredentials
credentials?: BotCredentialsJsonConfig | RedditCredentials
/*
* The name to display for the bot. If not specified will use the name of the reddit account IE `u/TheBotName`
* */
@@ -1275,33 +1465,20 @@ export interface BotInstanceJsonConfig {
notifications?: NotificationConfig
/**
* Settings to control some [Snoowrap](https://github.com/not-an-aardvark/snoowrap) behavior
* Settings to control some [Snoowrap](https://github.com/not-an-aardvark/snoowrap) behavior.
*
* Overrides any defaults provided at top-level operator config.
*
* Set to an empty object to "ignore" any top-level config
* */
snoowrap?: {
/**
* Proxy all requests to Reddit's API through this endpoint
*
* * ENV => `PROXY`
* * ARG => `--proxy <proxyEndpoint>`
*
* @examples ["http://localhost:4443"]
* */
proxy?: string,
/**
* Manually set the debug status for snoowrap
*
* When snoowrap has `debug: true` it will log the http status response of reddit api requests to at the `debug` level
*
* * Set to `true` to always output
* * Set to `false` to never output
*
* If not present or `null` will be set based on `logLevel`
*
* * ENV => `SNOO_DEBUG`
* * ARG => `--snooDebug`
* */
debug?: boolean,
}
snoowrap?: SnoowrapOptions
/**
* Define the default behavior for all filter criteria on all checks in all subreddits
*
* Defaults to exclude mods and automoderator from checks
* */
filterCriteriaDefaults?: FilterCriteriaDefaults
/**
* Settings related to bot behavior for subreddits it is managing
@@ -1370,18 +1547,31 @@ export interface BotInstanceJsonConfig {
* */
polling?: PollingDefaults & {
/**
* If set to `true` all subreddits polling unmoderated/modqueue with default polling settings will share a request to "r/mod"
* otherwise each subreddit will poll its own mod view
* DEPRECATED: See `shared`
*
* Using the ENV or ARG will sett `unmoderated` and `modqueue` on `shared`
*
* * ENV => `SHARE_MOD`
* * ARG => `--shareMod`
*
* @default false
* @deprecated
* */
sharedMod?: boolean,
/**
* If sharing a mod stream stagger pushing relevant Activities to individual subreddits.
* Set which polling sources should be shared among subreddits using default polling settings for that source
*
* * For `unmoderated and `modqueue` the bot will poll on **r/mod** for new activities
* * For `newSub` and `newComm` all subreddits sharing the source will be combined to poll like **r/subreddit1+subreddit2/new**
*
* If set to `true` all polling sources will be shared, otherwise specify which sourcs should be shared as a list
*
* */
shared?: PollOn[] | true,
/**
* If sharing a stream staggers pushing relevant Activities to individual subreddits.
*
* Useful when running many subreddits and rules are potentially cpu/memory/traffic heavy -- allows spreading out load
* */
@@ -1484,38 +1674,7 @@ export interface OperatorJsonConfig {
/**
* Settings to configure global logging defaults
* */
logging?: {
/**
* The minimum log level to output. The log level set will output logs at its level **and all levels above it:**
*
* * `error`
* * `warn`
* * `info`
* * `verbose`
* * `debug`
*
* Note: `verbose` will display *a lot* of information on the status/result of run rules/checks/actions etc. which is very useful for testing configurations. Once your bot is stable changing the level to `info` will reduce log noise.
*
* * ENV => `LOG_LEVEL`
* * ARG => `--logLevel <level>`
*
* @default "verbose"
* @examples ["verbose"]
* */
level?: LogLevel,
/**
* The absolute path to a directory where rotating log files should be stored.
*
* * If not present or `null` no log files will be created
* * If `true` logs will be stored at `[working directory]/logs`
*
* * ENV => `LOG_DIR`
* * ARG => `--logDir [dir]`
*
* @examples ["/var/log/contextmod"]
* */
path?: string,
},
logging?: LoggingOptions,
/**
* Settings to configure the default caching behavior globally
@@ -1524,6 +1683,11 @@ export interface OperatorJsonConfig {
* */
caching?: OperatorCacheConfig
/**
* Set global snoowrap options as well as default snoowrap config for all bots that don't specify their own
* */
snoowrap?: SnoowrapOptions
bots?: BotInstanceJsonConfig[]
/**
@@ -1644,6 +1808,8 @@ export interface OperatorJsonConfig {
* */
friendly?: string,
}
credentials?: ThirdPartyCredentialsJsonConfig
}
export interface RequiredOperatorRedditCredentials extends RedditCredentials {
@@ -1657,8 +1823,23 @@ export interface RequiredWebRedditCredentials extends RedditCredentials {
redirectUri: string
}
export interface ThirdPartyCredentialsJsonConfig {
youtube?: {
apiKey: string
}
[key: string]: any
}
export interface BotCredentialsJsonConfig extends ThirdPartyCredentialsJsonConfig {
reddit: RedditCredentials
}
export interface BotCredentialsConfig extends ThirdPartyCredentialsJsonConfig {
reddit: RequiredOperatorRedditCredentials
}
export interface BotInstanceConfig extends BotInstanceJsonConfig {
credentials: RequiredOperatorRedditCredentials
credentials: BotCredentialsJsonConfig
snoowrap: {
proxy?: string,
debug?: boolean,
@@ -1671,7 +1852,7 @@ export interface BotInstanceConfig extends BotInstanceJsonConfig {
heartbeatInterval: number,
},
polling: {
sharedMod: boolean,
shared: PollOn[],
stagger?: number,
limit: number,
interval: number,
@@ -1693,10 +1874,7 @@ export interface OperatorConfig extends OperatorJsonConfig {
display?: string,
},
notifications?: NotificationConfig
logging: {
level: LogLevel,
path?: string,
},
logging: StrongLoggingOptions,
caching: StrongCache,
web: {
port: number,
@@ -1720,6 +1898,16 @@ export interface OperatorConfig extends OperatorJsonConfig {
friendly?: string,
}
bots: BotInstanceConfig[]
credentials: ThirdPartyCredentialsJsonConfig
}
export interface OperatorFileConfig {
document: YamlOperatorConfigDocument | JsonOperatorConfigDocument
isWriteable?: boolean
}
export interface OperatorConfigWithFileContext extends OperatorConfig {
fileConfig: OperatorFileConfig
}
//export type OperatorConfig = Required<OperatorJsonConfig>;
@@ -1749,20 +1937,18 @@ export interface LogInfo {
bot?: string
}
export interface ActionResult {
export interface ActionResult extends ActionProcessResult {
kind: string,
name: string,
run: boolean,
runReason?: string,
dryRun: boolean,
success: boolean,
result?: string,
}
export interface ActionProcessResult {
success: boolean,
dryRun: boolean,
result?: string
touchedEntities?: (Submission | Comment | RedditUser | string)[]
}
export interface ActionedEvent {
@@ -1791,14 +1977,6 @@ export interface RedditEntity {
type: RedditEntityType
}
export interface StatusCodeError extends Error {
name: 'StatusCodeError',
statusCode: number,
message: string,
response: IncomingMessage,
error: Error
}
export interface HistoricalStatsDisplay extends HistoricalStats {
checksRunTotal: number
checksFromCacheTotal: number
@@ -1878,3 +2056,104 @@ export interface HistoricalStatUpdateData {
rulesCachedTotal: number
rulesTriggered: string[] | string
}
export type SearchFacetType = 'title' | 'url' | 'duplicates' | 'crossposts' | 'external';
export interface RepostItem {
value: string
createdOn?: number
source: string
sourceUrl?: string
score?: number
id: string
itemType: string
acquisitionType: SearchFacetType | 'comment'
sourceObj?: any
reqSameness?: number
}
export interface RepostItemResult extends RepostItem {
sameness: number
}
export interface StringComparisonOptions {
lengthWeight?: number,
transforms?: ((str: string) => string)[]
}
export interface FilterCriteriaPropertyResult<T> {
property: keyof T
expected: (string | boolean | number)[]
found?: string | boolean | number | null
passed?: null | boolean
reason?: string
behavior: FilterBehavior
}
export interface FilterCriteriaResult<T> {
behavior: FilterBehavior
criteria: T//AuthorCriteria | TypedActivityStates
propertyResults: FilterCriteriaPropertyResult<T>[]
passed: boolean
}
export type FilterBehavior = 'include' | 'exclude'
export interface FilterResult<T> {
criteriaResults: FilterCriteriaResult<T>[]
join: JoinOperands
passed: boolean
}
export interface TextTransformOptions {
/**
* A set of search-and-replace operations to perform on text values before performing a match. Transformations are performed in the order they are defined.
*
* * If `transformationsActivity` IS NOT defined then these transformations will be performed on BOTH the activity text (submission title or comment) AND the repost candidate text
* * If `transformationsActivity` IS defined then these transformations are only performed on repost candidate text
* */
transformations?: SearchAndReplaceRegExp[]
/**
* Specify a separate set of transformations for the activity text (submission title or comment)
*
* To perform no transformations when `transformations` is defined set this to an empty array (`[]`)
* */
transformationsActivity?: SearchAndReplaceRegExp[]
}
export interface TextMatchOptions {
/**
* The percentage, as a whole number, of a repost title/comment that must match the title/comment being checked in order to consider both a match
*
* Note: Setting to 0 will make every candidate considered a match -- useful if you want to match if the URL has been reposted anywhere
*
* Defaults to `85` (85%)
*
* @default 85
* @example [85]
* */
matchScore?: number
/**
* The minimum number of words in the activity being checked for which this rule will run on
*
* If the word count is below the minimum the rule fails
*
* Defaults to 2
*
* @default 2
* @example [2]
* */
minWordCount?: number
/**
* Should text matching be case sensitive?
*
* Defaults to false
*
* @default false
* @example [false]
**/
caseSensitive?: boolean
}

View File

@@ -3,6 +3,7 @@ import {RepeatActivityJSONConfig} from "../Rule/RepeatActivityRule";
import {AuthorRuleJSONConfig} from "../Rule/AuthorRule";
import {AttributionJSONConfig} from "../Rule/AttributionRule";
import {FlairActionJson} from "../Action/SubmissionAction/FlairAction";
import {UserFlairActionJson} from "../Action/UserFlairAction";
import {CommentActionJson} from "../Action/CommentAction";
import {ReportActionJson} from "../Action/ReportAction";
import {LockActionJson} from "../Action/LockAction";
@@ -13,9 +14,19 @@ import {ApproveActionJson} from "../Action/ApproveAction";
import {BanActionJson} from "../Action/BanAction";
import {RegexRuleJSONConfig} from "../Rule/RegexRule";
import {MessageActionJson} from "../Action/MessageAction";
import {RepostRuleJSONConfig} from "../Rule/RepostRule";
export type RuleJson = RecentActivityRuleJSONConfig | RepeatActivityJSONConfig | AuthorRuleJSONConfig | AttributionJSONConfig | HistoryJSONConfig | RegexRuleJSONConfig | string;
export type RuleJson = RecentActivityRuleJSONConfig | RepeatActivityJSONConfig | AuthorRuleJSONConfig | AttributionJSONConfig | HistoryJSONConfig | RegexRuleJSONConfig | RepostRuleJSONConfig | string;
export type RuleObjectJson = Exclude<RuleJson, string>
export type ActionJson = CommentActionJson | FlairActionJson | ReportActionJson | LockActionJson | RemoveActionJson | ApproveActionJson | BanActionJson | UserNoteActionJson | MessageActionJson | string;
export type ActionJson = CommentActionJson | FlairActionJson | ReportActionJson | LockActionJson | RemoveActionJson | ApproveActionJson | BanActionJson | UserNoteActionJson | MessageActionJson | UserFlairActionJson | string;
export type ActionObjectJson = Exclude<ActionJson, string>;
// borrowed from https://github.com/jabacchetta/set-random-interval/blob/master/src/index.ts
export type SetRandomInterval = (
intervalFunction: () => void,
minDelay: number,
maxDelay: number,
) => { clear: () => void };
export type ConfigFormat = 'json' | 'yaml';

26
src/Common/typings/support.d.ts vendored Normal file
View File

@@ -0,0 +1,26 @@
declare module 'snoowrap/dist/errors' {
export interface InvalidUserError extends Error {
}
export interface NoCredentialsError extends Error {
}
export interface InvalidMethodCallError extends Error {
}
export interface RequestError extends Error {
statusCode: number,
response: http.IncomingMessage
error: Error
}
export interface StatusCodeError extends RequestError {
name: 'StatusCodeError',
}
export interface RateLimitError extends RequestError {
name: 'RateLimitError',
}
}

Some files were not shown because too many files have changed in this diff Show More