Compare commits

..

39 Commits

Author SHA1 Message Date
FoxxMD
661a0ae440 Merge branch 'edge' 2022-05-26 09:59:32 -04:00
FoxxMD
05f477b67d Merge branch 'edge' 2022-05-12 12:27:51 -04:00
Matt Foxx
1317a5916c Merge pull request #86 from wchristian/example_fix
trying to use names key in authorfilter causes config parse failure
2022-04-05 16:55:56 -04:00
Christian Walde
e9135ec1ef trying to use names key in authorfilter causes config parse failure 2022-04-05 13:49:41 +02:00
FoxxMD
e58a0f8f21 Merge branch 'edge' 2022-03-14 12:39:05 -04:00
FoxxMD
f7cebc013b Merge branch 'edge' 2022-03-08 09:48:06 -05:00
FoxxMD
ae8e11feb4 Merge branch 'edge' 2022-02-22 11:11:46 -05:00
FoxxMD
e07b8cc291 Merge branch 'edge' 2022-02-18 11:58:28 -05:00
FoxxMD
fc51928054 Merge branch 'edge' 2022-02-02 16:59:56 -05:00
FoxxMD
e2590e50f8 Merge branch 'edge' 2022-01-28 17:27:51 -05:00
FoxxMD
aaed0d3419 Merge branch 'edge' 2022-01-21 10:46:11 -05:00
FoxxMD
bc7eff8928 Merge branch 'edge' 2022-01-14 15:27:09 -05:00
FoxxMD
d6954533a0 Merge branch 'edge' 2022-01-10 12:32:14 -05:00
FoxxMD
ba53233640 Merge branch 'edge' 2022-01-07 09:31:14 -05:00
FoxxMD
1ac7ad4724 Merge branch 'edge' 2022-01-03 16:35:01 -05:00
FoxxMD
2a282a0d6f Merge branch 'edge' 2021-12-21 09:35:21 -05:00
FoxxMD
fd5a92758d Merge branch 'edge' 2021-11-28 19:43:20 -05:00
FoxxMD
39daa11f2d Merge branch 'edge' 2021-11-15 12:53:28 -05:00
FoxxMD
dac6541e28 Merge branch 'edge' 2021-11-01 16:12:43 -04:00
FoxxMD
97906281e6 Merge branch 'edge' 2021-11-01 14:55:10 -04:00
FoxxMD
487f13f704 Merge branch 'edge' 2021-10-12 11:56:51 -04:00
FoxxMD
631e21452c Merge branch 'edge' 2021-09-28 16:36:13 -04:00
FoxxMD
4f3685a1f5 Merge branch 'edge' 2021-09-21 15:18:38 -04:00
FoxxMD
d2d945db2c Merge branch 'edge' 2021-09-21 15:08:28 -04:00
FoxxMD
910f7f79ef Merge branch 'edge' 2021-09-20 10:54:32 -04:00
FoxxMD
a11b667d5e Merge branch 'edge' 2021-09-13 16:16:55 -04:00
FoxxMD
885e3fa765 Merge branch 'edge' 2021-08-26 16:04:01 -04:00
FoxxMD
465c3c9acf Merge branch 'edge' 2021-08-20 15:02:24 -04:00
FoxxMD
161251a943 Merge branch 'edge' 2021-08-05 14:40:06 -04:00
FoxxMD
ce4cb96d9a Merge branch 'edge' 2021-08-03 23:39:14 -04:00
FoxxMD
c317f95953 Merge branch 'edge' 2021-08-03 22:43:02 -04:00
FoxxMD
d0e0515990 Merge branch 'edge' 2021-08-02 15:44:57 -04:00
FoxxMD
cdddd8de48 Merge branch 'edge' 2021-07-30 18:17:38 -04:00
FoxxMD
f598215d88 Merge branch 'edge' 2021-07-30 14:46:51 -04:00
FoxxMD
0c7218571c Merge branch 'edge' 2021-07-29 13:25:16 -04:00
FoxxMD
acc7c49e0e Merge branch 'edge' 2021-07-29 11:27:42 -04:00
FoxxMD
01839512d5 Merge branch 'edge' 2021-07-29 11:14:33 -04:00
FoxxMD
4680640b0c Merge branch 'develop' 2021-07-28 16:58:36 -04:00
Matt Foxx
b813ebdd96 Create dockerhub.yml 2021-07-28 11:27:04 -04:00
203 changed files with 2841 additions and 34885 deletions

View File

@@ -8,12 +8,11 @@ coverage
.idea
*.bak
*.sqlite
*.sqlite*
*.json
*.json5
*.yaml
*.yml
*.env
# exceptions
!heroku.yml

1
.github/FUNDING.yml vendored
View File

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

View File

@@ -1,3 +0,0 @@
{
"ref": "refs/heads/edge"
}

View File

@@ -1,14 +1,4 @@
name: Publish Docker image to registries
# Builds image and tags based on the type of push event:
# * branch push -> tag is branch name IE context-mod:edge
# * release (tag) -> tag is release name IE context-mod:0.13.0
#
# Then pushes tagged images to multiple registries
#
# Based on
# https://github.com/docker/build-push-action/blob/master/docs/advanced/push-multi-registries.md
# https://github.com/docker/metadata-action
name: Publish Docker image to Dockerhub
on:
push:
@@ -23,12 +13,8 @@ on:
jobs:
push_to_registry:
name: Build and Push Docker image to registries
name: Push Docker image to Docker Hub
runs-on: ubuntu-latest
# https://docs.github.com/en/actions/security-guides/automatic-token-authentication#permissions-for-the-github_token
permissions:
packages: write
contents: read
steps:
- name: Check out the repo
uses: actions/checkout@v2
@@ -39,22 +25,12 @@ jobs:
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Login to GitHub Container Registry
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v3
with:
images: |
foxxmd/context-mod
ghcr.io/foxxmd/context-mod
images: foxxmd/context-mod
# generate Docker tags based on the following events/attributes
tags: |
type=raw,value=latest,enable=${{ endsWith(github.ref, 'master') }}
@@ -64,8 +40,7 @@ jobs:
latest=false
- name: Build and push Docker image
if: ${{ !env.ACT }}
uses: docker/build-push-action@v3
uses: docker/build-push-action@v2
with:
context: .
push: ${{ github.event_name != 'pull_request' }}

2
.gitignore vendored
View File

@@ -336,7 +336,6 @@ web_modules/
# dotenv environment variables file
.env
.env.test
*.env
# parcel-bundler cache (https://parceljs.org/)
.cache
@@ -392,7 +391,6 @@ dist
*.json5
!src/Schema/*.json
!.github/push-hook-sample.json
!docs/**/*.json5
!docs/**/*.yaml
!docs/**/*.json

2
.nvmrc
View File

@@ -1 +1 @@
16.18.0
16.14.2

View File

@@ -120,10 +120,6 @@ ENV NPM_CONFIG_LOGLEVEL debug
# can set database to use more performant better-sqlite3 since we control everything
ENV DB_DRIVER=better-sqlite3
# NODE_ARGS are expanded after `node` command in the entrypoint IE "node {NODE_ARGS} src/index.js run"
# by default enforce better memory mangement by limiting max long-lived GC space to 512MB
ENV NODE_ARGS="--max_old_space_size=512"
ARG webPort=8085
ENV PORT=$webPort
EXPOSE $PORT

View File

@@ -30,9 +30,6 @@ Feature Highlights for **Moderators:**
* Event notification via Discord
* [**Web interface**](#web-ui-and-screenshots) for monitoring, administration, and oauth bot authentication
* [**Placeholders**](/docs/subreddit/actionTemplating.md) (like automoderator) can be configured via a wiki page or raw text and supports [mustache](https://mustache.github.io) templating
* [**Partial Configurations**](/docs/subreddit/components/README.md#partial-configurations) -- offload parts of your configuration to shared locations to consolidate logic between multiple subreddits
* [Guest Access](/docs/subreddit/README.md#guest-access) enables collaboration and easier setup by allowing temporary access
* [Toxic content prediction](/docs/subreddit/components/README.md#moderatehatespeechcom-predictions) using [moderatehatespeech.com](https://moderatehatespeech.com) machine learning model
Feature highlights for **Developers and Hosting (Operators):**
@@ -46,7 +43,6 @@ Feature highlights for **Developers and Hosting (Operators):**
* Historical statistics
* [Docker container support](/docs/operator/installation.md#docker-recommended)
* Easy, UI-based [OAuth authentication](/docs/operator/addingBot.md) for adding Bots and moderator dashboard
* Integration with [InfluxDB](https://www.influxdata.com) for detailed [time-series metrics](/docs/operator/database.md#influx) and a pre-built [Grafana](https://grafana.com) [dashboard](/docs/operator/database.md#grafana)
# Table of Contents
@@ -136,10 +132,6 @@ Moderator view/invite and authorization:
![Invite View](docs/images/oauth-invite.jpg)
A similar helper and invitation experience is available for adding **subreddits to an existing bot.**
![Subreddit Invite View](docs/images/subredditInvite.jpg)
### Configuration Editor
A built-in editor using [monaco-editor](https://microsoft.github.io/monaco-editor/) makes editing configurations easy:
@@ -153,13 +145,6 @@ A built-in editor using [monaco-editor](https://microsoft.github.io/monaco-edito
![Configuration View](docs/images/editor.jpg)
### [Grafana Dashboard](/docs/operator/database.md#grafana)
* Overall stats (active bots/subreddits, api calls, per second/hour/minute activity ingest)
* Over time graphs for events, per subreddit, and for individual rules/check/actions
![Grafana Dashboard](/docs/images/grafana.jpg)
## License
[MIT](/LICENSE)

View File

@@ -1,3 +0,0 @@
GITHUB_TOKEN=
DOCKERHUB_USERNAME=
DOCKER_PASSWORD=

View File

@@ -2,8 +2,6 @@
# used https://github.com/linuxserver/docker-plex as a template
# NODE_ARGS can be passed by ENV in docker command like "docker run foxxmd/context-mod -e NODE_ARGS=--optimize_for_size"
exec \
s6-setuidgid abc \
/usr/local/bin/node $NODE_ARGS /app/src/index.js run
/usr/local/bin/node /app/src/index.js run

View File

@@ -152,7 +152,6 @@ An **Action** is some action the bot can take against the checked Activity (comm
* For **Operator/Bot maintainers** see **[Operation Guide](/docs/operator/README.md)**
* For **Moderators**
* Start with the [Subreddit/Moderator docs](/docs/subreddit/README.md) or [Moderator Getting Started guide](/docs/subreddit/gettingStarted.md)
* Refer to the [Subreddit Components Documentation](/docs/subreddit/components) or the [subreddit-ready examples](/docs/subreddit/components/subredditReady)
* as well as the [schema](https://json-schema.app/view/%23?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Fcontext-mod%2Fmaster%2Fsrc%2FSchema%2FApp.json) which has
* fully annotated configuration data/structure

View File

@@ -1,17 +1,5 @@
TODO add more development sections...
# Developing/Testing Github Actions
Use [act](https://github.com/nektos/act) to run Github actions locally.
An example secrets file can be found in the project working directory at [act.env.example](act.env.example)
Modify [push-hook-sample.json](.github/push-hook-sample.json) to point to the local branch you want to run a `push` event trigger on, then run this command from the project working directory:
```bash
act -e .github/push-hook-sample.json --secret-file act.env
```
# Mocking Reddit API
Using [MockServer](https://www.mock-server.com/)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 133 KiB

After

Width:  |  Height:  |  Size: 125 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 183 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 137 KiB

After

Width:  |  Height:  |  Size: 148 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 158 KiB

After

Width:  |  Height:  |  Size: 226 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 141 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 225 KiB

After

Width:  |  Height:  |  Size: 479 KiB

View File

@@ -21,7 +21,7 @@ They are responsible for configuring the software at a high-level and managing a
# Overview
CM is composed of two applications that operate independently but are packaged together such that they act as one piece of software:
CM is composed of two applications that operate indepedently but are packaged together such that they act as one piece of software:
* **Server** -- Responsible for **running the bot(s)** and providing an API to retrieve information on and interact with them EX start/stop bot, reload config, retrieve operational status, etc.
* **Client** -- Responsible for serving the **web interface** and handling the bot oauth authentication flow between operators and subreddits/bots.

View File

@@ -136,8 +136,6 @@ You will need have this information available:
See the [**example minimum configuration** below.](#minimum-config)
This configuration can also be **generated** by CM if you start CM with **no configuration defined** and visit the web interface.
# Bots
Configured using the `bots` top-level property. Bot configuration can override and specify many more options than are available at the operator-level. Many of these can also set the defaults for each subreddit the bot runs:

View File

@@ -130,59 +130,3 @@ retention: 2000
runs:
...
```
# Influx
ContextMod supports writing detailed time-series data to [InfluxDB](https://www.influxdata.com/).
This data can be used to monitor the overall health, performance, and metrics for a ContextMod server. Currently, this data can **only be used by an Operator** as it requires access to the operator configuration and CM instance.
CM supports InfluxDB OSS > 2.3 or InfluxDB Cloud.
**Note:** This is an **advanced feature** and assumes you have enough technical knowledge to follow the documentation provided by each application to deploy and configure them. No support is guaranteed for installation, configuration, or use of Influx and Grafana.
## Supported Metrics
TBA
## Setup
### InfluxDB OSS
* Install [InfluxDB](https://docs.influxdata.com/influxdb/v2.3/install/)
* [Configure InfluxDB using the UI](https://docs.influxdata.com/influxdb/v2.3/install/#set-up-influxdb-through-the-ui)
* You will need **Username**, **Password**, **Organization Name**, and **Bucket Name** later for Grafana setup so make sure to record them somewhere
* [Create a Token](https://docs.influxdata.com/influxdb/v2.3/security/tokens/create-token/) with enough permissions to write/read to the bucket you configured
* After the token is created **view/copy the token** to clipboard by clicking the token name. You will need this for Grafana setup.
### ContextMod
Add the following block to the top-level of your operator configuration:
```yaml
influxConfig:
credentials:
url: 'http://localhost:8086' # URL to your influx DB instance
token: '9RtZ5YZ6bfEXAMPLENJsTSKg==' # token created in the previous step
org: MyOrg # organization created in the previous step
bucket: contextmod # name of the bucket created in the previous step
```
## Grafana
A pre-built dashboard for [Grafana](https://grafana.com) can be imported to display overall metrics/stats using InfluxDB data.
![Grafana Dashboard](/docs/images/grafana.jpg)
* Create a new Data Source using **InfluxDB** type
* Choose **Flux** for the **Query Language**
* Fill in the details for **URL**, **Basic Auth Details** and **InfluxDB Details** using the data you created in the [Influx Setup step](#influxdb-oss)
* Set **Min time interval** to `60s`
* Click **Save and test**
* Import Dashboard
* **Browse** the Dashboard pane
* Click **Import** and **upload** the [grafana dashboard json file](/docs/operator/grafana.json)
* Chose the data source you created from the **InfluxDB CM** dropdown
* Click **Import**
The dashboard can be filtered by **Bots** and **Subreddits** dropdowns at the top of the page to get more specific details.

View File

@@ -4,7 +4,9 @@ This getting started guide is for **Operators** -- that is, someone who wants to
* [Installation](#installation)
* [Create a Reddit Client](#create-a-reddit-client)
* [Start ContextMod](#start-contextmod)
* [Create a Minimum Configuration](#create-a-minimum-configuration)
* [Local Installation](#local-installation)
* [Docker Installation](#docker-installation)
* [Add a Bot to CM](#add-a-bot-to-cm)
* [Access The Dashboard](#access-the-dashboard)
* [What's Next?](#whats-next)
@@ -17,25 +19,29 @@ Follow the [installation](/docs/operator/installation.md) documentation. It is r
[Create a reddit client](/docs/operator/README.md#provisioning-a-reddit-client)
# Start ContextMod
# Create a Minimum Configuration
Start CM using the example command from your [installation](#installation) and visit http://localhost:8085
Using the information you received in the previous step [create a minimum file configuration](/docs/operator/configuration.md#minimum-configuration) save it as `config.yaml` somewhere.
The First Time Setup page will ask you to input:
# Start ContextMod With Configuration
* Client ID (from [Create a Reddit Client](#create-a-reddit-client))
* Client Secret (from [Create a Reddit Client](#create-a-reddit-client))
* Operator -- this is the username of your main Reddit account.
## Local Installation
**Write Config** and then restart CM. You have now created the [minimum configuration](/docs/operator/configuration.md#minimum-configuration) required to run CM.
If you [installed CM locally](/docs/installation.md#locally) move your configuration file `config.yaml` to the root of the project directory (where `package.json`) is located.
From the root directory run this command to start CM
```
node src/index.js run
```
## Docker Installation
If you [installed CM using Docker](/docs/installation.md#docker-recommended) make note of the directory you saved your minimum configuration to and substitute its full path for `host/path/folder` in the docker command show in the [docker install directions](/docs/operator/installation.md#docker-recommended)
# Add A Bot to CM
You should automatically be directed to the [Bot Invite Helper](/docs/operator/addingBot.md#cm-oauth-helper-recommended) used to authorize and add a Bot to your CM instance.
Follow the directions here and **create an Authorization Invite** at the bottom of the page.
Next, login to Reddit with the account you will be using as the Bot and then visit the **Authorization Invite** link you created. Follow the steps there to finish adding the Bot to your CM instance.
Once CM is up and running use the [CM OAuth Helper](/docs/operator/addingBot.md#cm-oauth-helper-recommended) to add authorize and add a Bot to your CM instance.
# Access The Dashboard
@@ -51,4 +57,4 @@ As an operator you should familiarize yourself with how the [operator configurat
If you are also the moderator of the subreddit the bot will be running you should check out the [moderator getting started guide.](/docs/subreddit/gettingStarted.md#setup-wiki-page)
You might also be interested in these [quick tips for using the web interface](/docs/webInterface.md). Additionally, on the dashboard click the **Help** button at the top of the page to get a guided tour of the dashboard.
You might also be interested in these [quick tips for using the web interface](/docs/webInterface.md)

File diff suppressed because it is too large Load Diff

View File

@@ -8,19 +8,15 @@ ContextMod can be run on almost any operating system but it is recommended to us
PROTIP: Using a container management tool like [Portainer.io CE](https://www.portainer.io/products/community-edition) will help with setup/configuration tremendously.
Images available from these registeries:
### [Dockerhub](https://hub.docker.com/r/foxxmd/context-mod)
* [Dockerhub](https://hub.docker.com/r/foxxmd/context-mod) - `docker.io/foxxmd/context-mod`
* [GHCR](https://github.com/foxxmd/context-mod/pkgs/container/context-mod) - `ghcr.io/foxxmd/context-mod`
An example of starting the container using the [minimum configuration](/docs/operator/configuration.md#minimum-config):
An example of starting the container using the [minimum configuration](/docs/operator/operatorConfiguration.md#minimum-config) with a [configuration file](/docs/operator/operatorConfiguration.md#defining-configuration-via-file):
* Bind the directory where your config file, logs, and database are located on your host machine into the container's default `DATA_DIR` by using `-v /host/path/folder:/config`
* Note: **You must do this** or else your configuration will be lost next time your container is updated.
* Expose the web interface using the container port `8085`
```
docker run -d -v /host/path/folder:/config -p 8085:8085 ghcr.io/foxxmd/context-mod:latest
docker run -d -v /host/path/folder:/config -p 8085:8085 foxxmd/context-mod
```
The location of `DATA_DIR` in the container can be changed by passing it as an environmental variable EX `-e "DATA_DIR=/home/abc/config`
@@ -37,7 +33,7 @@ To get the UID and GID for the current user run these commands from a terminal:
* `id -g` -- prints GID
```
docker run -d -v /host/path/folder:/config -p 8085:8085 -e PUID=1000 -e PGID=1000 ghcr.io/foxxmd/context-mod:latest
docker run -d -v /host/path/folder:/config -p 8085:8085 -e PUID=1000 -e PGID=1000 foxxmd/context-mod
```
## Locally
@@ -80,21 +76,3 @@ Be aware that Heroku's [free dyno plan](https://devcenter.heroku.com/articles/fr
* 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.
# Memory Management
Node exhibits [lazy GC cleanup](https://github.com/FoxxMD/context-mod/issues/90#issuecomment-1190384006) which can result in memory usage for long-running CM instances increasing to unreasonable levels. This problem does not seem to be an issue with CM itself but with Node's GC approach. The increase does not affect CM's performance and, for systems with less memory, the Node *should* limit memory usage based on total available.
In practice CM uses ~130MB for a single bot, single subreddit setup. Up to ~350MB for many (10+) bots or many (20+) subreddits.
If you need to reign in CM's memory usage for some reason this can be addressed by setting an upper limit for memory usage with `node` args by using either:
**--max_old_space_size=**
Value is megabytes. This sets an explicit limit on GC memory usage.
This is set by default in the [Docker](#docker-recommended) container using the env `NODE_ARGS` to `--max_old_space_size=512`. It can be disabled by overriding the ENV.
**--optimize_for_size**
Tells Node to optimize for (less) memory usage rather than some performance optimizations. This option is not memory size dependent. In practice performance does not seem to be affected and it reduces (but not entirely prevents) memory increases over long periods.

View File

@@ -1,95 +0,0 @@
This section is for **reddit moderators**. It covers how to use a CM bot for your subreddit.
If you are trying to run a ContextMod instance (the actual software) please refer to the [operator section](/docs/operator/README.md).
# Table of Contents
* [Overview](#overview)
* [Your Relationship to CM](#your-relationship-to-cm)
* [Operator](#operator)
* [Your Bot](#your-bot)
* [Getting Started](#getting-started)
* [Accessing The Bot](#accessing-the-bot)
* [Editing The Bot](#editing-the-bot)
* [Configuration](#configuration)
* [Guest Access](#guest-access)
# Overview
The Context Mod **software** can manage multiple **bots** (reddit accounts used as bots, like `/u/MyCMBot`). Each bot can manage (run) multiple **subreddits** which is determined by the subreddits the account is a moderator of.
You, the moderator of a subreddit a CM bot runs in, can access/manage the Bot using the CM software's [web interface](/docs/images/subredditStatus.jpg) and control its behavior using the [web editor.](/docs/images/editor.jpg)
## Your Relationship to CM
It is important to understand the relationship between you (the moderator), the bot, and the operator (the person running the CM software).
The easiest way to think about this is in relation to how you use Automoderator and interact with Reddit as a moderator. As an analogy:
### Operator
The operator is the person running the actual server/machine the Context Mod software is on.
They are best thought of as **Reddit:**
* Mostly hands-off when it comes to the bot and interacting with your subreddit
* You must interact with Reddit first before you can use automoderator (login, create a subreddit, etc...)
Unlike reddit, though, there is a greater level of trust required between you and the Operator because what you make the Bot do ultimately affects the Operator since they are the ones actually running your Bot and making API calls to reddit.
### Your Bot
Your bot is like an **invite-only version of Automoderator**:
* Unlike automoderator, you **must** interact with the Operator in order to get the bot working. It is not public for anyone to use.
* Like automoderator, you **must** create a [configuration](/docs/subreddit/components/README.md) for it do anything.
* The bot does not come pre-configured for you. It is a blank slate and requires user input to be useful.
* Also like automoderator, you are **entirely in control of the bot.**
* You can start, stop, and edit its behavior at any time without needing to communicate with the Operator.
* CM provides you _tools_, different ways the Bot can detect patterns in your subreddit/users as well as actions it can, and you can decide to use them however you want.
* Your bot is **only accessible to moderators of your subreddit.**
# Getting Started
The [Getting Started](/docs/subreddit/gettingStarted.md) guide lays out the steps needed to go from nothing to a working Bot. If you are a moderator new to Context Mod this is where you want to begin.
# Accessing The Bot
All bot management and editing is done through the [web interface.](/docs/images/subredditStatus.jpg) The URL used for accessing this interface is given to you by the **Operator** once they have agreed to host your bot/subreddit.
NOTE: This interface is **only access to moderators of your subreddit** and [guests.](#guest-access) You must login to the web interface **with your moderator account** in order to access it.
A **guided tour** that helps show how to manage the bot at a high-level is available on the web interface by clicking the **Help** button in the top-right of the page.
## Editing The Bot
Find the [editor in the web interface](/docs/webInterface.md#editingupdating-your-config) to access the built-in editor for the bot.
[The editor](/docs/images/editor.jpg) should be your all-in-one location for viewing and editing your bot's behavior. **It is equivalent to Automoderator's editor page.**
The editor features:
* syntax validation and highlighting
* configuration auto-complete and documentation (hover over properties)
* built-in validation using Microsoft Word "squiggly lines" indicators and an error list at the bottom of the window
* built-in saving (at the top of the window)
# Configuration
Use the [Configuration Reference](/docs/subreddit/components/README.md) to learn about all the different components available for building a CM configuration.
Additionally, refer to [How It Works](/docs/README.md#how-it-works) and [Core Concepts](/docs/README.md#concepts) to learn the basic of CM configuration.
After you have the basics under your belt you could use the [subreddit-reddit example configurations](/docs/subreddit/components/subredditReady) to familiarize yourself with a complete configuration and ways to use CM.
# Guest Access
CM supports **Guest Access**. Reddit users who are given Guest Access to your bot are allowed to access the web interface even though they are not moderators.
Additionally, they can edit the subreddit's config using the bot. If a Guest edits your config their username will be mentioned in the wiki page edit reason.
Guests can do everything a regular mod can except view/add/remove Guest. They can be removed at any time or set with an expiration date that their access is removed on.
**Guests are helpful if you are new to CM and know reddit users that can help you get started.**
[Add guests from the Subreddit tab in the main interface.](/docs/images/guests.jpg)

View File

@@ -1,150 +1,47 @@
Actions that can submit text (Report, Comment, UserNote, Message, Ban, Submission) will have their `content` values run through a [Mustache Template](https://mustache.github.io/). This means you can insert data generated by Rules into your text before the Action is performed.
Actions that can submit text (Report, Comment, UserNote) will have their `content` values run through a [Mustache Template](https://mustache.github.io/). This means you can insert data generated by Rules into your text before the Action is performed.
See here for a [cheatsheet](https://gist.github.com/FoxxMD/d365707cf99fdb526a504b8b833a5b78) and [here](https://www.tsmean.com/articles/mustache/the-ultimate-mustache-tutorial/) for a more thorough tutorial.
# Template Data
Some data can always be accessed at the top-level. Example
```
This action was run from {{manager}} in Check {{check}}.
The bot intro post is {{botLink}}
Message the moderators of this subreddit using this [compose link]({{modmailLink}})
```
| Name | Description | Example |
|---------------|---------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------|
| `manager` | The name of the subreddit the bot is running in | mealtimevideos |
| `check` | The name of the Check that was triggered | myCheck |
| `botLink` | A link to the bot introduction | https://www.reddit.com/r/ContextModBot/comments/otz396/introduction_to_contextmodbot |
| `modmailLink` | A link that opens reddit's DM compose with the subject line as the Activity being processed | https://www.reddit.com/message/compose?to=/r/mealtimevideos&message=https://www.reddit.com/r/ContextModBot/comments/otz396/introduction_to_contextmodbot |
## Activity Data
**Activity data can be accessed using the `item` variable.** Example
```
This activity is a {{item.kind}} with {{item.votes}} votes, created {{item.age}} ago.
```
Produces:
> This activity is a submission with 10 votes created 5 minutes ago.
### Common
All Actions with `content` have access to this data:
| Name | Description | Example |
|--------------|-----------------------------------------------------------------------------------------------------|----------------------------------------------------------|
| `kind` | The Activity type (submission or comment) | submission |
| `author` | Name of the Author of the Activity being processed | FoxxMD |
| `permalink` | URL to the Activity | https://reddit.com/r/mySuibreddit/comments/ab23f/my_post |
| `votes` | Number of upvotes | 69 |
| `age` | The age of the Activity in a [human friendly format](https://day.js.org/docs/en/durations/humanize) | 5 minutes |
| `subreddit` | The name of the subreddit the Activity is from | mealtimevideos |
| `id` | The `Reddit Thing` ID for the Activity | t3_0tin1 |
| `title` | As comments => the body of the comment. As Submission => title | Test post please ignore |
| `shortTitle` | The same as `title` but truncated to 15 characters | test post pleas... |
```json5
### Submissions
If the **Activity** is a Submission these additional properties are accessible:
| Name | Description | Example |
|---------------|-----------------------------------------------------------------|-------------------------|
| `upvoteRatio` | The upvote ratio | 100% |
| `nsfw` | If the submission is marked as NSFW | true |
| `spoiler` | If the submission is marked as a spoiler | true |
| `url` | If the submission was a link then this is the URL for that link | http://example.com |
| `title` | The title of the submission | Test post please ignore |
### Comments
If the **Activity** is a Comment these additional properties are accessible:
| Name | Description | Example |
|------|--------------------------------------------------------------|---------|
| `op` | If the Author is the OP of the Submission this comment is in | true |
### Moderator
If the **Activity** occurred in a Subreddit the Bot moderates these properties are accessible:
| Name | Description | Example |
|---------------|-------------------------------------|---------|
| `reports` | The number of reports recieved | 1 |
| `modReports` | The number of reports by moderators | 1 |
| `userReports` | The number of reports by users | 1 |
## Rule Data
### Summary
A summary of what rules were processed and which were triggered, with results, is available using the `ruleSummary` variable. Example:
{
item: {
kind: 'string', // the type of item (comment/submission)
author: 'string', // name of the item author (reddit user)
permalink: 'string', // a url to the item
url: 'string', // if the item is a Submission then its URL (external for link type submission, reddit link for self-posts)
title: 'string', // if the item is a Submission, then the title of the Submission,
botLink: 'string' // a link to the bot's FAQ
},
rules: {
// contains all rules that were run and are accessible using the name, lowercased, with all spaces/dashes/underscores removed
}
}
```
A summary of rules processed for this activity:
{{ruleSummary}}
```
Would produce:
> A summary of rules processed for this activity:
>
> * namedRegexRule - ✘
> * nameAttributionRule - ✓ - 1 Attribution(s) met the threshold of < 20%, with 1 (3%) of 32 Total -- window: 6 months
> * noXPost ✓ - ✓ 1 of 1 unique items repeated <= 3 times, largest repeat: 1
### Individual
Individual **Rules** can be accessed using the name of the rule, **lower-cased, with all spaces/dashes/underscores.** Example:
The properties of `rules` are accessible using the name, lower-cased, with all spaces/dashes/underscores. If no name is given `kind` is used as `name` Example:
```
Submission was repeated {{rules.noxpost.largestRepeat}} times
```
Produces
> Submission was repeated 7 times
## Action Data
### Summary
A summary of what actions have already been run **when the template is rendered** is available using the `actionSummary` variable. It is therefore important that the Action you want to produce the summary is run **after** any other Actions you want to get a summary for.
Example:
"rules": [
{
"name": "My Custom-Recent Activity Rule", // mycustomrecentactivityrule
"kind": "recentActivity"
},
{
// name = repeatsubmission
"kind": "repeatActivity",
}
]
```
A summary of actions processed for this activity, so far:
{{actionSummary}}
```
**To see what data is available for individual Rules [consult the schema](#configuration) for each Rule.**
Would produce:
> A summary of actions processed for this activity, so far:
>
> * approve - ✘ - Item is already approved??
> * lock - ✓
> * modnote - ✓ - (SOLID_CONTRIBUTOR) User is good
### Individual
Individual **Actions** can be accessed using the name of the action, **lower-cased, with all spaces/dashes/underscores.** Example:
```
User was banned for {{actions.exampleban.duration}} for {{actions.exampleban.reason}}
```
Produces
> User was banned for 4 days for toxic behavior
# Quick Templating Tutorial
#### Quick Templating Tutorial
As a quick example for how you will most likely be using templating -- wrapping a variable in curly brackets, `{{variable}}`, will cause the variable value to be rendered instead of the brackets:

View File

@@ -21,8 +21,6 @@ This list is not exhaustive. [For complete documentation on a subreddit's config
* [Author](#author)
* [Regex](#regex)
* [Repost](#repost)
* [Sentiment Analysis](#sentiment-analysis)
* [Toxic Content Prediction](#moderatehatespeechcom-predictions)
* [Rule Sets](#rule-sets)
* [Actions](#actions)
* [Named Actions](#named-actions)
@@ -30,7 +28,6 @@ This list is not exhaustive. [For complete documentation on a subreddit's config
* [List of Actions](#list-of-actions)
* [Approve](#approve)
* [Ban](#ban)
* [Submission](#submission)
* [Comment](#comment)
* [Contributor (Add/Remove)](#contributor)
* [Dispatch/Delay](#dispatch)
@@ -42,13 +39,10 @@ This list is not exhaustive. [For complete documentation on a subreddit's config
* [Message](#message)
* [Remove](#remove)
* [Report](#report)
* [Toolbox UserNote](#usernote)
* [Mod Note](#mod-note)
* [UserNote](#usernote)
* [Filters](#filters)
* [Filter Types](#filter-types)
* [Author Filter](#author-filter)
* [Mod Notes/Actions](#mod-actionsnotes-filter)
* [Toolbox UserNotes](#toolbox-usernotes-filter)
* [Item Filter](#item-filter)
* [Subreddit Filter](#subreddit-filter)
* [Named Filters](#named-filters)
@@ -69,7 +63,6 @@ This list is not exhaustive. [For complete documentation on a subreddit's config
* [Check Order](#check-order)
* [Rule Order](#rule-order)
* [Configuration Re-use and Caching](#configuration-re-use-and-caching)
* [Partial Configurations](#partial-configurations)
* [Subreddit-ready examples](#subreddit-ready-examples)
# Runs
@@ -372,18 +365,6 @@ The **Repost** rule is used to find reposts for both **Submissions** and **Comme
This rule is for searching **all of Reddit** for reposts, as opposed to just the history of 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/subreddit/components/repeatActivity) rule.
### Sentiment Analysis
[**Full Documentation**](/docs/subreddit/components/sentiment)
The **Sentiment Rule** is used to determine the overall emotional intent (negative, neutral, positive) of a Submission or Comment by analyzing the actual text content of the Activity.
### ModerateHateSpeech.com Predictions
[**Full Documentation**](/docs/subreddit/components/mhs)
ContextMod integrates with [moderatehatespeech.com](https://moderatehatespeech.com/) (MHS) [toxic content machine learning model](https://moderatehatespeech.com/framework/) through their API. This rule sends an Activity's content (title or body) to MHS which returns a prediction on whether the content is toxic and actionable by a moderator. Their model is [specifically trained for reddit content.](https://www.reddit.com/r/redditdev/comments/xdscbo/updated_bot_backed_by_moderationoriented_ml_for/)
# Rule Sets
The `rules` list on a `Check` can contain both `Rule` objects and `RuleSet` objects.
@@ -502,30 +483,11 @@ actions:
### Comment
Reply to an Activity with a comment. [Schema Documentation](https://json-schema.app/view/%23/%23%2Fdefinitions%2FSubmissionCheckJson/%23%2Fdefinitions%2FCommentActionJson?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Freddit-context-bot%2Fmaster%2Fsrc%2FSchema%2FApp.json)
Reply to the Activity being processed with a comment. [Schema Documentation](https://json-schema.app/view/%23/%23%2Fdefinitions%2FSubmissionCheckJson/%23%2Fdefinitions%2FCommentActionJson?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Freddit-context-bot%2Fmaster%2Fsrc%2FSchema%2FApp.json)
* If the Activity is a Submission the comment is a top-level reply
* If the Activity is a Comment the comment is a child reply
#### Templating
`content` can be [templated](#templating) and use [URL Tokens](#url-tokens)
#### Targets
Optionally, specify the Activity CM should reply to. **When not specified CM replies to the Activity being processed using `self`**
Valid values: `self`, `parent`, or a Reddit permalink.
`self` and `parent` are special targets that are relative to the Activity being processed:
* When the Activity being processed is a **Submission** => `parent` logs a warning and does nothing
* When the Activity being processed is a **Comment**
* `self` => reply to Comment
* `parent` => make a top-level Comment in the **Submission** the Comment belong to
If target is not self/parent then CM assumes the value is a **reddit permalink** and will attempt to make a Comment to that Activity
```yaml
actions:
- kind: comment
@@ -533,71 +495,7 @@ actions:
distinguish: boolean # distinguish as a mod
sticky: boolean # sticky comment
lock: boolean # lock the comment after creation
targets: string # 'self' or 'parent' or 'https://reddit.com/r/someSubreddit/21nfdi....'
```
### Submission
Create a Submission [Schema Documentation](https://json-schema.app/view/%23/%23%2Fdefinitions%2FSubmissionCheckJson/%23%2Fdefinitions%2FSubmissionActionJson?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Freddit-context-bot%2Fmaster%2Fsrc%2FSchema%2FApp.json)
The Submission type, Link or Self-Post, is determined based on the presence of `url` in the action's configuration.
```yaml
actions:
- kind: submission
title: string # required, the title of the submission. can be templated.
content: string # the body of the submission. can be templated
url: string # if specified the submission will be a Link Submission. can be templated
distinguish: boolean # distinguish as a mod
sticky: boolean # sticky comment
lock: boolean # lock the comment after creation
nsfw: boolean # mark submission as NSFW
spoiler: boolean # mark submission as a spoiler
flairId: string # flair template id for submission
flairText: string # flair text for submission
targets: string # 'self' or a subreddit name IE mealtimevideos
```
#### Templating
`content`,`url`, and `title` can be [templated](#templating) and use [URL Tokens](#url-tokens)
TIP: To create a Link Submission pointing to the Activity currently being processed use
```yaml
actions:
- kind: submission
url: {{item.permalink}}
# ...
```
#### Targets
Optionally, specify the Subreddit the Submission should be made in. **When not specified CM uses `self`**
Valid values: `self` or Subreddit Name
* `self` => (**Default**) Create Submission in the same Subreddit of the Activity being processed
* Subreddit Name => Create Submission in given subreddit IE `mealtimevideos`
* Your bot must be able to access and be able to post in the given subreddit
Example:
```yaml
actions:
- kind: comment
targets: mealtimevideos
```
To post to multiple subreddits use a list:
```yaml
actions:
- kind: comment
targets:
- self
- mealtimevideos
- anotherSubreddit
```
### Contributor
@@ -699,35 +597,31 @@ Some other things to note:
* If the `to` property is not specified then the message is sent to the Author of the Activity being processed
* `to` may be a **User** (u/aUser) or a **Subreddit** (r/aSubreddit)
* `to` **cannot** be a Subreddit when `asSubreddit: true` -- IE cannot send subreddit-to-subreddit messages
* TIP: `to` can be templated -- to send a message to the subreddit the Activity being processed is in use `'r/{{item.subreddit}}'`
* `content` and `title` can be [templated](#templating) and use [URL Tokens](#url-tokens)
* `content` can be [templated](#templating) and use [URL Tokens](#url-tokens)
```yaml
actions:
- kind: message
asSubreddit: true
content: 'A message sent as the subreddit' # can be templated
title: 'Title of the message' # can be templated
to: 'u/aUser' # do not specify 'to' in order default to sending to Author of Activity being processed. Can also be templated
content: 'A message sent as the subreddit'
title: 'Title of the message'
to: 'u/aUser' # do not specify 'to' in order default to sending to Author of Activity being processed
```
### Remove
Remove the Activity being processed. [Schema Documentation](https://json-schema.app/view/%23/%23%2Fdefinitions%2FSubmissionCheckJson/%23%2Fdefinitions%2FRemoveActionJson?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Freddit-context-bot%2Fedge%2Fsrc%2FSchema%2FApp.json)
* **note** can be [templated](#templating)
* **reasonId** IDs can be found in the [editor](/docs/webInterface.md) using the **Removal Reasons** popup
If neither note nor reasonId are included then no removal reason is added.
```yaml
actions:
- kind: remove
spam: false # optional, mark as spam on removal
note: 'a moderator-readable note' # optional, a note only visible to moderators (new reddit only)
reasonId: '2n0f4674-365e-46d2-8fc7-a337d85d5340' # optional, the ID of a removal reason to add to the removal action (new reddit only)
spam: boolean # optional, mark as spam on removal
```
#### What About Removal Reason?
Reddit does not support setting a removal reason through the API. Please complain in [r/modsupport](https://www.reddit.com/r/modsupport) or [r/redditdev](https://www.reddit.com/r/redditdev) to help get this added :)
### Report
Report the Activity being processed. [Schema Documentation](https://json-schema.app/view/%23/%23%2Fdefinitions%2FSubmissionCheckJson/%23%2Fdefinitions%2FReportActionJson?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Freddit-context-bot%2Fedge%2Fsrc%2FSchema%2FApp.json)
@@ -757,30 +651,6 @@ actions:
allowDuplicate: boolean # if false then the usernote will not be added if the same note appears for this activity
```
### Mod Note
[**Full Documentation**](/docs/subreddit/components/modActions/README.md#mod-note-action)
Add a [Mod Note](https://www.reddit.com/r/modnews/comments/t8vafc/announcing_mod_notes/) for the Author of the Activity.
* `type` must be one of the [valid note labels](https://www.reddit.com/dev/api#POST_api_mod_notes):
* BOT_BAN
* PERMA_BAN
* BAN
* ABUSE_WARNING
* SPAM_WARNING
* SPAM_WATCH
* SOLID_CONTRIBUTOR
* HELPFUL_USER
```yaml
actions:
- kind: modnote
type: SPAM_WATCH
content: 'a note only mods can see message' # optional
referenceActivity: boolean # if true the Note will be linked to the Activity being processed
```
# Filters
**Filters** are an additional channel for determining if an Event should be processed by ContextMod. They differ from **Rules** in several key ways:
@@ -862,14 +732,6 @@ There are two types of Filter. Both types have the same "shape" in the configura
Test the Author of an Activity. See [Schema documentation](https://json-schema.app/view/%23%2Fdefinitions%2FAuthorCriteria?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Freddit-context-bot%2Fedge%2Fsrc%2FSchema%2FApp.json) for all possible Author Criteria
#### Mod Actions/Notes Filter
See [Mod Actions/Notes](/docs/subreddit/components/modActions/README.md#mod-action-filter) documentation.
#### Toolbox UserNotes Filter
See [UserNotes](/docs/subreddit/components/userNotes/README.md) documentation
### Item Filter
Test for properties of an Activity:
@@ -1169,7 +1031,7 @@ If the Check is using `AND` condition for its rules (default) then if either Rul
**It is therefore advantageous to list your lightweight Rules first in each Check.**
## Configuration Re-use and Caching
### Configuration Re-use and Caching
ContextMod implements caching functionality for:
@@ -1193,116 +1055,6 @@ PROTIP: You can monitor the re-use of cache in the `Cache` section of your subre
[Learn more about how Caching works](/docs/operator/caching.md)
## Partial Configurations
ContextMod supports fetching parts of a configuration (a **Fragment**) from an external source. Fragments are an advanced feature and should only be used by users who are familiar with CM's configuration syntax and understand the risks/downsides associates with fragmenting a configuration.
**Fragments** are supported for:
* [Runs](#runs)
* [Checks](#checks)
* [Rules](#rules)
* [Actions](#actions)
### Should You Use Partial Configurations?
* **PROS**
* Consolidate shared configuration for many subreddits into one location
* Shared configuration can be updated independently of subreddits
* Allows sharing access to configuration outside of moderators of a specific subreddit or even reddit
* **CONS**
* Editor does not currently support viewing, editing, or updating Fragments. Only the Fragment URL is visible in a Subreddit's configuration
* No editor support for viewing obscures "complete view" of configuration and makes editor less useful for validation
* Currently, editor cannot validate individual Fragments. They must be copy-pasted "in place" within a normal configuration.
* Using external (non-wiki) sources means **you** are responsible for the security/access to the fragment
In general, Fragments should only be used to offload small, well-tested pieces of a configuration that can be shared between many subreddits. Examples:
* A regex Rule for spam links
* A Recent Activity Rule for reporting users from freekarma subreddits
### Usage
A Fragment may be either a special string or a Fragment object. The fetched Fragment can be either an object or an array of objects of the type of Fragment being replaced.
**String**
If value starts with `wiki:` then the proceeding value will be used to get a wiki page from the current subreddit
* EX `wiki:botconfig/myFragment` tries to get `https://reddit.com/r/currentSubreddit/wiki/botconfig/myFragment`
If the value starts with `wiki:` and ends with `|someValue` then `someValue` will be used as the base subreddit for the wiki page
* EX `wiki:myFragment/test|ContextModBot` tries to get `https://reddit.com/r/ContextModBot/wiki/myFragment/test`
If the value starts with `url:` then the value is fetched as an external url and expects raw text returned
* EX `url:https://pastebin.com/raw/38qfL7mL` tries to get the text response of `https://pastebin.com/raw/38qfL7mL`
**Object**
The object contains:
* `path` -- REQUIRED string following rules above
* `ttl` -- OPTIONAL, number of seconds to cache the URL result. Defaults to `WikiTTL`
#### Examples
**Replacing A Rule with a URL Fragment**
```yaml
runs:
- checks:
- name: Free Karma Alert
description: Check if author has posted in 'freekarma' subreddits
kind: submission
rules:
- 'url:https://gist.githubusercontent.com/FoxxMD/0e1ee1ab950ff4d1f0cd26172bae7f8f/raw/0ebfaca903e4a651827effac5775c8718fb6e1f2/fragmentRule.yaml'
- name: badSub
kind: recentActivity
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:
- MyBadSubreddit
window: 7 days
actions:
- kind: report
content: 'uses freekarma subreddits and bad subreddits'
```
**Replacing A Rule with a URL Fragment (Multiple)**
```yaml
runs:
- checks:
- name: Free Karma Alert
description: Check if author has posted in 'freekarma' subreddits
kind: submission
rules:
- 'url:https://gist.githubusercontent.com/FoxxMD/0e1ee1ab950ff4d1f0cd26172bae7f8f/raw/0ebfaca903e4a651827effac5775c8718fb6e1f2/fragmentRuleArray.yaml'
actions:
- kind: report
content: 'uses freekarma subreddits and bad subreddits'
```
**Replacing A Rule with a Wiki Fragment**
```yaml
runs:
- checks:
- name: Free Karma Alert
description: Check if author has posted in 'freekarma' subreddits
kind: submission
rules:
- 'wiki:freeKarmaFrag'
actions:
- kind: report
content: 'uses freekarma subreddits'
```
# Subreddit-Ready Examples
Refer to the [Subreddit-Ready Examples](/docs/subreddit/components/subredditReady) section to find ready-to-use configurations for common scenarios (spam, freekarma blocking, etc...). This is also a good place to familiarize yourself with what complete configurations look like.

View File

@@ -31,7 +31,7 @@
// 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
// OR = any of the nested Rules will be 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

View File

@@ -22,7 +22,7 @@ runs:
# 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
# OR = any of the nested Rules will be 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

View File

@@ -40,7 +40,7 @@
// 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"]
"name": ["user1","user2"]
},
{
// for this to pass the Author of the Submission must not have the flair "Decent Memer"

View File

@@ -30,7 +30,7 @@ runs:
# 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:
name:
- user1
- user2
# for this to pass the Author of the Submission must not have the flair "Decent Memer"

View File

@@ -5,45 +5,9 @@ The **History** rule can check an Author's submission/comment statistics over a
* Submission total or percentage of All Activity
* Comment total or percentage of all Activity
* Comments made as OP (commented in their own Submission) total or percentage of all Comments
* Ratio of activities against another window of activities
Consult the [schema](https://json-schema.app/view/%23%2Fdefinitions%2FHistoryJSONConfig?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Fcontext-mod%2Fmaster%2Fsrc%2FSchema%2FApp.json) for a complete reference of the rule's properties.
## Ratio
Use the `ratio` property in Criteria to test the [number of activities](/docs/subreddit/activitiesWindow.md) found in the parent criteria against the number of activities from _another_ [activity window](/docs/subreddit/activitiesWindow.md) defined in the ratio.
Example:
```yaml
- kind: history
criteria:
# "parent" criteria, returns all activities, in the last 100 from user's history, that occurred in r/mealtimevideos
- window:
count: 100
filterOn:
post:
subreddits:
include:
- mealtimevideos
ratio:
# "ratio" criteria, returns all activities, in the last 100 from user's history, that occurred in r/redditdev
window:
count: 100
filterOn:
post:
subreddits:
include:
- redditdev
# test (number of parent criteria activities) / (number of ratio critieria activities)
threshold: '> 1.2'
```
`threshold` may be a number or percentage `(number * 100)`
* EX `> 1.2` => There are 1.2 activities from parent criteria for every 1 ratio activities
* EX `<= 75%` => There are equal to or less than 0.75 activities from parent criteria for every 1 ratio activities
### Examples
* Low Comment Engagement [YAML](/docs/subreddit/componentscomponents/history/lowEngagement.yaml) | [JSON](/docs/subreddit/componentscomponents/history/lowEngagement.json5) - Check if Author is submitting much more than they comment.

View File

@@ -1,165 +0,0 @@
# Table of Contents
* [Overview](#overview)
* [MHS Predictions](#mhs-predictions)
* [Flagged](#flagged)
* [Confidence](#confidence)
* [Usage](#usage)
* [Minimal/Default Config](#minimaldefault-config)
* [Full Config](#full-config)
* [Historical Matching](#historical-matching)
* [Examples](#examples)
# Overview
[moderatehatespeech.com](https://moderatehatespeech.com/) (MHS) is a [non-profit initiative](https://moderatehatespeech.com/about/) to identify and fight toxic and hateful content online using programmatic technology such as machine learning models.
They offer a [toxic content prediction model](https://moderatehatespeech.com/framework/) specifically trained on and for [reddit content](https://www.reddit.com/r/redditdev/comments/xdscbo/updated_bot_backed_by_moderationoriented_ml_for/) as well as partnering [directly with subreddits.](https://moderatehatespeech.com/research/subreddit-program/).
Context Mod leverages their [API](https://moderatehatespeech.com/docs/) for toxic content predictions in the **MHS Rule.**
The **MHS Rule** sends an Activity's content (title or body) to MHS which returns a prediction on whether the content is toxic and actionable by a moderator.
## MHS Predictions
MHS's toxic content predictions return two indicators about the content it analyzed. Both are available as test conditions in ContextMod.
### Flagged
MHS returns a straight "Toxic or Normal" **flag** based on how it classifies the content.
Example
* `Normal` - "I love those pineapples"
* `Toxic` - "why are we having all these people from shithole countries coming here"
### Confidence
MHS returns how **confident** it is of the flag classification on a scale of 0 to 100.
Example
"why are we having all these people from shithole countries coming here"
* Flag = `Toxic`
* Confidence = `97.12` -> The model is 97% confident the content is `Toxic`
# Usage
**An MHS Api Key is required to use this Rule**. An API Key can be acquired, for free, by creating an account at [moderatehatespeech.com](https://moderatehatespeech.com).
The Key can be provided by the bot's Operator in the [bot config credentials](https://json-schema.app/view/%23/%23%2Fdefinitions%2FBotInstanceJsonConfig/%23%2Fdefinitions%2FBotCredentialsJsonConfig?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Fcontext-mod%2Fedge%2Fsrc%2FSchema%2FOperatorConfig.json) or in the subreddit's config in the top-level `credentials` property like this:
```yaml
credentials:
mhs:
apiKey: 'myMHSApiKey'
# the rest of your config below
polling:
# ...
runs:
# ...
```
### Minimal/Default Config
ContextMod provides a reasonable default configuration for the MHS Rule if you do not wish to configure it yourself. The default configuration will trigger the rule if the MHS prediction:
* flags as `toxic`
* with `90% or greater` confidence
Example
```yaml
rules:
- kind: mhs
# rest of your rules here...
```
### Full Config
| Property | Type | Description | Default |
|--------------|---------|-------------------------------------------------------------------------------------------|---------|
| `flagged` | boolean | Test whether content is flagged as toxic (true) or normal (false) | `true` |
| `confidence` | string | Comparison against a number 0 to 100 representing how confident MHS is in the prediction | `>= 90` |
| `testOn` | array | Which parts of the Activity to send to MHS. Options: `title` and/or `body` | `body` |
Example
```yaml
rules:
- kind: mhs
criteria:
flagged: true # triggers if MHs flags the content as toxic AND
confidence: '> 66' # MHS is 66% or more confident in its prediction
testOn: # send the body of the activity to the MHS prediction service
- body
```
#### Historical Matching
Like the [Sentiment](/docs/subreddit/components/sentiment#historical) and [Regex](/docs/subreddit/components/regex#historical) rules CM can also use MHS predictions to check content from the Author's history.
Example
```yaml
rules:
- kind: mhs
# ...same config as above but can include below...
historical:
mustMatchCurrent: true # if true then CM will not check author's history unless current Activity matches MHS prediction criteria
totalMatching: '> 1' # comparison for how many activities in history must match to trigger the rule
window: 10 # specify the range of activities to check in author's history
criteria: #... if specified, overrides parent-level criteria
```
# Examples
Report if MHS flags as toxic
```yaml
rules:
- kind: mhs
actions:
- kind: report
content: 'MHS flagged => {{rules.mhs.summary}}'
```
Report if MHS flags as toxic with 95% confidence
```yaml
rules:
- kind: mhs
confidence: '>= 95'
actions:
- kind: report
content: 'MHS flagged => {{rules.mhs.summary}}'
```
Report if MHS flags as toxic and at least 3 recent activities in last 10 from author's history are also toxic
```yaml
rules:
- kind: mhs
historical:
window: 10
mustMatchCurrent: true
totalMatching: '>= 3'
actions:
- kind: report
content: 'MHS flagged => {{rules.mhs.summary}}'
```
Approve if MHS flags as NOT toxic with 95% confidence
```yaml
rules:
- kind: mhs
confidence: '>= 95'
flagged: false
actions:
- kind: approve
```

View File

@@ -1,152 +0,0 @@
# Table of Contents
* [Overview](#overview)
* [Mod Note Action](#mod-note-action)
* [Mod Action Filter](#mod-action-filter)
* [API Usage](#api-usage)
* [When To Use?](#when-to-use)
* [Examples](#examples)
# Overview
[Mod Notes](https://www.reddit.com/r/modnews/comments/t8vafc/announcing_mod_notes/) is a feature for New Reddit that allow moderators to add short, categorizable notes to Users of their subreddit, optionally associating te note with a submission/comment the User made. They are inspired by [Toolbox User Notes](https://www.reddit.com/r/toolbox/wiki/docs/usernotes) which are also [supported by ContextMod.](/docs/subreddit/components/userNotes) Reddit's **Mod Notes** also combine [Moderation Log](https://mods.reddithelp.com/hc/en-us/articles/360022402312-Moderation-Log) actions (**Mod Actions**) for the selected User alongside moderator notes, enabling a full "overview" of moderator interactions with a User in their subreddit.
ContextMod supports adding **Mod Notes** to an Author using an [Action](/docs/subreddit/components/README.md#mod-note) and using **Mod Actions/Mod Notes** as a criteria in an [Author Filter](/docs/subreddit/components/README.md#author-filter)
# Mod Note Action
[**Schema Reference**](https://json-schema.app/view/%23%2Fdefinitions%2FModNoteActionJson?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Freddit-context-bot%2Fedge%2Fsrc%2FSchema%2FApp.json)
* `type` must be one of the [valid note labels](https://www.reddit.com/dev/api#POST_api_mod_notes):
* BOT_BAN
* PERMA_BAN
* BAN
* ABUSE_WARNING
* SPAM_WARNING
* SPAM_WATCH
* SOLID_CONTRIBUTOR
* HELPFUL_USER
```yaml
actions:
- kind: modnote
type: SPAM_WATCH
content: 'a note only mods can see message' # optional
referenceActivity: boolean # if true the Note will be linked to the Activity being processed
```
# Mod Action Filter
ContextMod can use **Mod Actions** (from moderation log) and **Mod Notes** in an [Author Filter](/docs/subreddit/components/README.md#author-filter).
## API Usage
Notes/Actions are **not** included in the data Reddit returns for either an Author or an Activity. This means that, in most cases, ContextMod is required to make **one additional API call to Reddit during Activity processing** if Notes/Actions as used as part of an **Author Filter**.
The impact of this additional call is greatest when the Author Filter is used as part of a **Comment Check** or running for **every Activity** such as part of a Run. Take this example:
No Mod Action filtering
* CM makes 1 api call to return new comments, find 10 new comments across 6 users
* Processing each comment, with no other filters, requires 0 additional calls
* At the end of processing 10 comments, CM has used a total of 1 api call.
Mod Action Filtering Used
* CM makes 1 api call to return new comments, find 10 new comments across 6 users
* Processing each comment, with a mod action filter, requires 1 additional api call per user
* At the end of processing 10 comments, CM has used a total of **7 api calls**
### When To Use?
In general,**do not** use Mod Actions in a Filter if:
* The filter is on a [**Comment** Check](/docs/subreddit/components/README.md#checks) and your subreddit has a high volume of Comments
* The filter is on a [Run](/docs/subreddit/components/README.md#runs) and your subreddit has a high volume of Activities
If you need Mod Notes-like functionality for a high volume subreddit consider using [Toolbox UserNotes](/docs/subreddit/components/userNotes) instead.
In general, **do** use Mod Actions in a Filter if:
* The filter is on a [**Submission** Check](/docs/subreddit/components/README.md#checks)
* The filter is part of an [Author **Rule**](/docs/subreddit/components/README.md#author) that is processed as **late as possible in the rule order for a Check**
* Your subreddit has a low volume of Activities (less than 100 combined submissions/comments in a 10 minute period, for example)
* The filter is on an Action
## Usage and Examples
Filter by Mod Actions/Notes on an Author Filter are done using the `modActions` property:
```yaml
age: '> 1 month'
# ...
modActions:
- ...
```
There two valid shapes for the Mod Action criteria: [ModLogCriteria](https://json-schema.app/view/%23%2Fdefinitions%2FModLogCriteria?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Freddit-context-bot%2Fedge%2Fsrc%2FSchema%2FApp.json) and [ModNoteCriteria](https://json-schema.app/view/%23%2Fdefinitions%2FModNoteCriteria?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Freddit-context-bot%2Fedge%2Fsrc%2FSchema%2FApp.json).
### ModLogCriteria
Used for filtering by **Moderation Log** actions *and/or general notes*.
* `activityType` -- Optional. If Mod Action is associated with an activity specify it here. A list or one of:
* `submission`
* `comment`
* `type` -- Optional. The type of Mod Log Action. A list or one of:
* `INVITE`
* `NOTE`
* `REMOVAL`
* `SPAM`
* `APPROVAL`
* `description` -- additional mod log details (string) to filter by -- not documented by reddit. Can be string or regex string-like `/.* test/i`
* `details` -- additional mod log details (string) to filter by -- not documented by reddit. Can be string or regex string-like `/.* test/i`
```yaml
activityType: submission
type:
- REMOVAL
- SPAM
search: total
count: '> 3 in 1 week'
```
### ModNoteCriteria
Inherits `activityType` from ModLogCriteria. If either of the below properties in included on the criteria then any other ModLogCriteria-specific properties are **ignored**.
* `note` -- the contents of the note to match against. Can be one of or a list of strings/regex string-like `/.* test/i`
* `noteType` -- If specified by the note, the note type (see [Mod Note Action](#mod-note-action) type). Can be one of or a list of strings/regex string-like `/.* test/i`
```yaml
noteType: SOLID_CONTRIBUTOR
search: total
count: '> 3 in 1 week'
```
### Examples
Author has more than 2 submission approvals in the last month
```yaml
type: APPROVAL
activityType: submission
search: total
count: '> 2 in 1 month'
```
Author has at least 1 BAN note
```yaml
noteType: BAN
search: total
count: '>= 1'
```
Author has at least 3 notes which include the words "self" and "promotion" in the last month
```yaml
note: '/self.*promo/i'
activityType: submission
search: total
count: '>= 3 in 1 month'
```

View File

@@ -1,183 +0,0 @@
# Table of Contents
* [Overview](#overview)
* [Pros And Cons](#pros-and-cons)
* [Technical Overview](#technical-overview)
* [Sentiment Values](#sentiment-values)
* [Usage](#usage)
* [Testing Sentiment Value](#testing-sentiment-value)
* [Numerical](#numerical)
* [Text](#text)
* [Sentiment Rule](#sentiment-rule)
* [Historical](#historical)
* [Examples](#examples)
# Overview
[Sentiment Analysis](https://monkeylearn.com/sentiment-analysis/) (SA) is a form of [Natural Language Processing](https://monkeylearn.com/natural-language-processing/) (NLP) used to extract the overall [sentiment](https://www.merriam-webster.com/dictionary/sentiment) (emotional intent) from a piece of text. Simply, SA is used to determine how positive or negative the emotion of a sentence is.
Examples:
* "I love how curly your hair is" -- very positive
* "The United States is over 200 years old" -- neutral
* "Frankly, your face is disgusting and I would hate to meet you" -- very negative
SA can be a powerful signal for determining the intent of a user's comment/submission. However, it should not be the **only** tool as it comes with both strengths and weaknesses.
## Pros and Cons
Pros
* In terms of Reddit API usage, SA is **free**. It requires no API calls and is computationally trivial.
* Extremely powerful signal for intent since it analyzes the actual text content of an activity
* Requires almost no setup to use
* Can be used as a substitute for regex/keyword matching when looking for hateful/toxic comments
* English language comprehension is very thorough
* Uses 3 independent algorithms to evaluate sentiment
* Understands common english slang, internet slang, and emojis
Cons
* Language limited -- only supported for English (most thorough), French, German, and Spanish
* Less accurate for small word count content (less than 4 words)
* Does not understand sarcasm/jokes
* Accuracy depends on use of common words
* Accuracy depends on clear intent
* Heavy nuance, obscure word choice, and hidden meanings are not understood
## Technical Overview
ContextMod attempts to identify the language of the content it is processing. Based on its confidence of the language it will use up to three different NLP libraries to extract sentiment:
* [NLP.js](https://github.com/axa-group/nlp.js/blob/master/docs/v3/sentiment-analysis.md) (english, french, german, and spanish)
* [vaderSentiment-js](https://github.com/vaderSentiment/vaderSentiment-js/) (english only)
* [wink-sentiment](https://github.com/winkjs/wink-sentiment) (english only)
The above libraries make use of these Sentiment Analysis algorithms:
* VADER https://github.com/cjhutto/vaderSentiment
* AFINN http://corpustext.com/reference/sentiment_afinn.html
* Senticon https://ieeexplore.ieee.org/document/8721408
* Pattern https://github.com/clips/pattern
* wink https://github.com/winkjs/wink-sentiment (modified AFINN with emojis)
Each library produces a normalized score: the sum of all the valence values for each recognized token in its lexicon, divided by the number of words/tokens.
ContextMod takes each normalized score and adjusts it to be between -1 and +1. It then adds finds the average of all normalized score to produce a final sentiment between -1 and +1.
# Sentiment Values
Each piece of content ContextMod analyses produces a score from -1 to +1 to represent the sentiment of that content
| Score | Sentiment |
|-------|--------------------|
| -1 | |
| -0.6 | Extremely Negative |
| -0.3 | Very Negative |
| -0.1 | Negative |
| 0 | Neutral |
| 0.1 | Positive |
| 0.3 | Very Positive |
| 0.6 | Extremely Positive |
| 1 | |
# Usage
## Testing Sentiment Value
Testing for sentiment in the Sentiment Rule is done using either a **text** or **numerical** comparison.
### Numerical
Similar to other numerical comparisons in CM -- use an equality operator and the number to test for:
* `> 0.1` -- sentiment is at least positive
* `<= -0.1` -- sentiment is not negative
Testing for *only* neutral sentiment should be done use a text comparison (below).
### Text
Use any of the **Sentiment** text values from the above table to form a test:
* `is very positive`
* `is neutral`
* `is extremely negative`
You may also use the `not` operator:
* `is not negative`
* `is not very negative`
* `is not neutral`
## Sentiment Rule
An example rule that tests the current comment/submission to see if it has negative sentiment:
```yaml
sentiment: 'is negative'
```
It's very simple :)
### Historical
You may also test the Sentiment of Activities from the user's history. (Note: this may use an API call to get history)
```yaml
sentiment: 'is negative'
historical:
window:
count: 50
mustMatchCurrent: true # optional, the initial activity being tested must test true ("is positive" must be true) before historical tests are run
sentimentVal: 'is very negative' # optional, if the sentiment test to use for historical content is different than the initial test
totalMatching: '> 3' # optional, a comparison for how many historical activities must match sentimentVal
```
# Examples
#### Check with Rules for recent problem subreddit activity and negative sentiment in comment
```yaml
name: Probably Toxic Comment
kind: comment
rules:
- kind: recentActivity
thresholds:
- aProblemSubreddit
- kind: sentiment
name: negsentiment
sentiment: 'is very negative'
actions:
- kind: report
content: 'Sentiment of {{rules.negsentiment.averageScore}} {{rules.negsentiment.sentimentTest}}'
```
#### Check with Rules for recent problem subreddit activity and negative sentiment in comment history from problem subreddits
```yaml
name: Toxic Comment With History
kind: comment
rules:
- kind: recentActivity
thresholds:
- aProblemSubreddit
- aSecondProblemSubreddit
- kind: sentiment
sentiment: 'is very negative'
historical:
sentimentVal: 'is negative'
mustMatchCurrent: true
totalMatching: '> 1'
window:
count: 100
filterOn:
post:
subreddits:
include:
- name:
- aProblemSubreddit
- aSecondProblemSubreddit
actions:
- kind: remove
```

View File

@@ -43,7 +43,7 @@
// remove this after confirming behavior is acceptable
{
"kind": "report",
"content": "Remove=> {{rules.freekarma.totalCount}} activities in freekarma subs"
"content": "Remove=> {{rules.newtube.totalCount}} activities in freekarma subs"
},
//
//

View File

@@ -25,7 +25,7 @@ runs:
actions:
- kind: report
enable: true
content: 'Remove=> {{rules.freekarma.totalCount}} activities in freekarma subs'
content: 'Remove=> {{rules.newtube.totalCount}} activities in freekarma subs'
- kind: remove
enable: true
- kind: comment

View File

@@ -6,16 +6,6 @@ Context Mod supports reading and writing [User Notes](https://www.reddit.com/r/t
[Click here for the Toolbox Quickstart Guide](https://www.reddit.com/r/toolbox/wiki/docs/quick_start)
Valid Note Types:
* `gooduser`
* `spamwatch`
* `spamwarn`
* `abusewarn`
* `ban`
* `permban`
* `botban`
## Filter
User Notes are an additional criteria on [AuthorCriteria](https://json-schema.app/view/%23%2Fdefinitions%2FAuthorCriteria?url=https%3A%2F%2Fraw.githubusercontent.com%2FFoxxMD%2Fcontext-mod%2Fmaster%2Fsrc%2FSchema%2FApp.json) that can be used alongside other Author properties for both [filtering rules and in the AuthorRule.](/docs/subreddit/components/author/)

View File

@@ -20,7 +20,6 @@
## Web Dashboard Tips
* Click the **Help** button at the top of the page to get a **guided tour of the dashboard**
* Use the [**Overview** section](/docs/images/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/images/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

397
package-lock.json generated
View File

@@ -1,20 +1,17 @@
{
"name": "redditcontextbot",
"version": "0.11.4",
"version": "0.5.1",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "redditcontextbot",
"version": "0.11.4",
"version": "0.5.1",
"hasInstallScript": true,
"license": "ISC",
"dependencies": {
"@awaitjs/express": "^0.8.0",
"@datasert/cronjs-matcher": "^1.2.0",
"@googleapis/youtube": "^2.0.0",
"@influxdata/influxdb-client": "^1.31.0",
"@influxdata/influxdb-client-apis": "^1.31.0",
"@nlpjs/core": "^4.23.4",
"@nlpjs/lang-de": "^4.23.4",
"@nlpjs/lang-en": "^4.23.4",
@@ -29,10 +26,10 @@
"autolinker": "^3.14.3",
"body-parser": "^1.19.0",
"cache-manager": "^3.4.4",
"cache-manager-redis-store": "^3.0.1",
"cache-manager-redis-store": "^2.0.0",
"commander": "^8.0.0",
"comment-json": "^4.1.1",
"connect-typeorm": "^2.0.0",
"connect-typeorm": "github:FoxxMD/connect-typeorm#typeormBump",
"cookie-parser": "^1.3.5",
"dayjs": "^1.10.5",
"deepmerge": "^4.2.2",
@@ -76,9 +73,8 @@
"string-similarity": "^4.0.4",
"tcp-port-used": "^1.0.2",
"triple-beam": "^1.3.0",
"typeorm": "^0.3.7",
"typeorm": "^0.3.4",
"typeorm-logger-adaptor": "^1.1.0",
"unique-names-generator": "^4.7.1",
"vader-sentiment": "^1.1.3",
"webhook-discord": "^3.7.7",
"wink-sentiment": "^5.0.2",
@@ -95,6 +91,7 @@
"@tsconfig/node14": "^1.0.0",
"@types/async": "^3.2.7",
"@types/cache-manager": "^3.4.2",
"@types/cache-manager-redis-store": "^2.0.0",
"@types/chai": "^4.3.0",
"@types/chai-as-promised": "^7.1.5",
"@types/cookie-parser": "^1.4.2",
@@ -137,7 +134,7 @@
"typescript-json-schema": "~0.53"
},
"engines": {
"node": ">=16.18.0"
"node": ">=16"
},
"optionalDependencies": {
"better-sqlite3": "^7.5.0",
@@ -656,20 +653,6 @@
"kuler": "^2.0.0"
}
},
"node_modules/@datasert/cronjs-matcher": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/@datasert/cronjs-matcher/-/cronjs-matcher-1.2.0.tgz",
"integrity": "sha512-ht6Vwwa3qssMn/9bphypjG/U8w0DV3GtTS2C6kbAy39rerQFTRzmml9xZNlot1K13gm9K/EEq3DLPEOsH++ICw==",
"dependencies": {
"@datasert/cronjs-parser": "^1.2.0",
"luxon": "^2.1.1"
}
},
"node_modules/@datasert/cronjs-parser": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/@datasert/cronjs-parser/-/cronjs-parser-1.2.0.tgz",
"integrity": "sha512-7kzYh7F5V3ElX+k3W9w6SKS6WdjqJQ2gIY1y0evldnjAwZxnFzR/Yu9Mv9OeDaCQX+mGAq2MvEnJbwu9oj3CXQ=="
},
"node_modules/@googleapis/youtube": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/@googleapis/youtube/-/youtube-2.0.0.tgz",
@@ -681,19 +664,6 @@
"node": ">=10.0.0"
}
},
"node_modules/@influxdata/influxdb-client": {
"version": "1.31.0",
"resolved": "https://registry.npmjs.org/@influxdata/influxdb-client/-/influxdb-client-1.31.0.tgz",
"integrity": "sha512-8DVT3ZB/VeCK5Nn+BxhgMrAMSTseQAEgV20AK+ZMO5Fcup9XWsA9L2zE+3eBFl0Y+lF3UeKiASkiKMQvws35GA=="
},
"node_modules/@influxdata/influxdb-client-apis": {
"version": "1.31.0",
"resolved": "https://registry.npmjs.org/@influxdata/influxdb-client-apis/-/influxdb-client-apis-1.31.0.tgz",
"integrity": "sha512-6ALGNLxtfffhICobOdj13Z6vj6gdQVOzVXPoPNd+w7V60zrbGhTqzXHV1KMZ/lzOb6YkRTRODbxz4W/b/7N5hg==",
"peerDependencies": {
"@influxdata/influxdb-client": "*"
}
},
"node_modules/@istanbuljs/load-nyc-config": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz",
@@ -978,59 +948,6 @@
"resolved": "https://registry.npmjs.org/@nlpjs/slot/-/slot-4.22.17.tgz",
"integrity": "sha512-cNYcxf9DKB+fnRa2NxT5wbWq5j57R1WCTXLWI/1Cyycr227IP7GN7qaD4RbkzotBFFB8wm63UHod9frzmuiXxg=="
},
"node_modules/@redis/bloom": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/@redis/bloom/-/bloom-1.0.2.tgz",
"integrity": "sha512-EBw7Ag1hPgFzdznK2PBblc1kdlj5B5Cw3XwI9/oG7tSn85/HKy3X9xHy/8tm/eNXJYHLXHJL/pkwBpFMVVefkw==",
"peerDependencies": {
"@redis/client": "^1.0.0"
}
},
"node_modules/@redis/client": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/@redis/client/-/client-1.3.0.tgz",
"integrity": "sha512-XCFV60nloXAefDsPnYMjHGtvbtHR8fV5Om8cQ0JYqTNbWcQo/4AryzJ2luRj4blveWazRK/j40gES8M7Cp6cfQ==",
"dependencies": {
"cluster-key-slot": "1.1.0",
"generic-pool": "3.8.2",
"yallist": "4.0.0"
},
"engines": {
"node": ">=14"
}
},
"node_modules/@redis/graph": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/@redis/graph/-/graph-1.0.1.tgz",
"integrity": "sha512-oDE4myMCJOCVKYMygEMWuriBgqlS5FqdWerikMoJxzmmTUErnTRRgmIDa2VcgytACZMFqpAOWDzops4DOlnkfQ==",
"peerDependencies": {
"@redis/client": "^1.0.0"
}
},
"node_modules/@redis/json": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/@redis/json/-/json-1.0.4.tgz",
"integrity": "sha512-LUZE2Gdrhg0Rx7AN+cZkb1e6HjoSKaeeW8rYnt89Tly13GBI5eP4CwDVr+MY8BAYfCg4/N15OUrtLoona9uSgw==",
"peerDependencies": {
"@redis/client": "^1.0.0"
}
},
"node_modules/@redis/search": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/@redis/search/-/search-1.1.0.tgz",
"integrity": "sha512-NyFZEVnxIJEybpy+YskjgOJRNsfTYqaPbK/Buv6W2kmFNaRk85JiqjJZA5QkRmWvGbyQYwoO5QfDi2wHskKrQQ==",
"peerDependencies": {
"@redis/client": "^1.0.0"
}
},
"node_modules/@redis/time-series": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/@redis/time-series/-/time-series-1.0.3.tgz",
"integrity": "sha512-OFp0q4SGrTH0Mruf6oFsHGea58u8vS/iI5+NpYdicaM+7BgqBZH8FFvNZ8rYYLrUO/QRqMq72NpXmxLVNcdmjA==",
"peerDependencies": {
"@redis/client": "^1.0.0"
}
},
"node_modules/@sindresorhus/is": {
"version": "4.6.0",
"resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-4.6.0.tgz",
@@ -1215,6 +1132,16 @@
"integrity": "sha512-71aBXoFYXZW4TnDHHH8gExw2lS28BZaWeKefgsiJI7QYZeJfUEbMKw6CQtzGjlYQcGIWwB76hcCrkVA3YHSvsw==",
"dev": true
},
"node_modules/@types/cache-manager-redis-store": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/@types/cache-manager-redis-store/-/cache-manager-redis-store-2.0.1.tgz",
"integrity": "sha512-8QuccvcPieh1xM/5kReE76SfdcIdEB0ePc+54ah/NBuK2eG+6O50SX4WKoJX81UxGdW3sh/WlDaDNqjnqxWNsA==",
"dev": true,
"dependencies": {
"@types/cache-manager": "*",
"@types/redis": "^2.8.0"
}
},
"node_modules/@types/cacheable-request": {
"version": "6.0.2",
"resolved": "https://registry.npmjs.org/@types/cacheable-request/-/cacheable-request-6.0.2.tgz",
@@ -1498,6 +1425,15 @@
"resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.4.tgz",
"integrity": "sha512-EEhsLsD6UsDM1yFhAvy0Cjr6VwmpMWqFBCb9w07wVugF7w9nfajxLuVmngTIpgS6svCnm6Vaw+MZhoDCKnOfsw=="
},
"node_modules/@types/redis": {
"version": "2.8.32",
"resolved": "https://registry.npmjs.org/@types/redis/-/redis-2.8.32.tgz",
"integrity": "sha512-7jkMKxcGq9p242exlbsVzuJb57KqHRhNl4dHoQu2Y5v9bCAbtIXXH0R3HleSQW4CTOqpHIYUW3t6tpUj4BVQ+w==",
"dev": true,
"dependencies": {
"@types/node": "*"
}
},
"node_modules/@types/responselike": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/@types/responselike/-/responselike-1.0.0.tgz",
@@ -2383,14 +2319,14 @@
}
},
"node_modules/cache-manager-redis-store": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/cache-manager-redis-store/-/cache-manager-redis-store-3.0.1.tgz",
"integrity": "sha512-o560kw+dFqusC9lQJhcm6L2F2fMKobJ5af+FoR2PdnMVdpQ3f3Bz6qzvObTGyvoazQJxjQNWgMQeChP4vRTuXQ==",
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/cache-manager-redis-store/-/cache-manager-redis-store-2.0.0.tgz",
"integrity": "sha512-bWLWlUg6nCYHiJLCCYxY2MgvwvKnvlWwrbuynrzpjEIhfArD2GC9LtutIHFEPeyGVQN6C+WEw+P3r+BFBwhswg==",
"dependencies": {
"redis": "^4.3.1"
"redis": "^3.0.2"
},
"engines": {
"node": ">= 16.18.0"
"node": ">= 8.3"
}
},
"node_modules/cacheable-lookup": {
@@ -2674,14 +2610,6 @@
"mimic-response": "^1.0.0"
}
},
"node_modules/cluster-key-slot": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/cluster-key-slot/-/cluster-key-slot-1.1.0.tgz",
"integrity": "sha512-2Nii8p3RwAPiFwsnZvukotvow2rIHM+yQ6ZcBXGHdniadkYGZYiGmkHJIbZPIV9nfv7m/U1IPMVVcAhoWFeklw==",
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/code-point-at": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/code-point-at/-/code-point-at-1.1.0.tgz",
@@ -2858,9 +2786,9 @@
}
},
"node_modules/connect-typeorm": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/connect-typeorm/-/connect-typeorm-2.0.0.tgz",
"integrity": "sha512-0OcbHJkNMTJjSrbcKGljr4PKgRq13Dds7zQq3+8oaf4syQTgGvGv9OgnXo2qg+Bljkh4aJNzIvW74QOVLn8zrw==",
"version": "1.2.0",
"resolved": "git+ssh://git@github.com/FoxxMD/connect-typeorm.git#a2f0a7225a1c218db0b919142076ce0774c8caa6",
"license": "MIT",
"dependencies": {
"@types/debug": "0.0.31",
"@types/express-session": "^1.15.5",
@@ -2868,7 +2796,7 @@
"express-session": "^1.15.6"
},
"peerDependencies": {
"typeorm": "^0.3.0"
"typeorm": "^0.3.4"
}
},
"node_modules/connect-typeorm/node_modules/debug": {
@@ -3251,6 +3179,14 @@
"integrity": "sha1-hMbhWbgZBP3KWaDvRM2HDTElD5o=",
"optional": true
},
"node_modules/denque": {
"version": "1.5.1",
"resolved": "https://registry.npmjs.org/denque/-/denque-1.5.1.tgz",
"integrity": "sha512-XwE+iZ4D6ZUB7mfYRMb5wByE8L74HCn30FBN7sWnXksWc1LO1bPDl67pBR9o/kC4z/xSNAwkMYcGgqDV3BE3Hw==",
"engines": {
"node": ">=0.10"
}
},
"node_modules/depd": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz",
@@ -4199,14 +4135,6 @@
"node": ">=10"
}
},
"node_modules/generic-pool": {
"version": "3.8.2",
"resolved": "https://registry.npmjs.org/generic-pool/-/generic-pool-3.8.2.tgz",
"integrity": "sha512-nGToKy6p3PAbYQ7p1UlWl6vSPwfwU6TMSWK7TTu+WUY4ZjyZQGniGGt2oNVvyNSpyZYSB43zMXVLcBm08MTMkg==",
"engines": {
"node": ">= 4"
}
},
"node_modules/gensync": {
"version": "1.0.0-beta.2",
"resolved": "https://registry.npmjs.org/gensync/-/gensync-1.0.0-beta.2.tgz",
@@ -5852,14 +5780,6 @@
"node": ">=10"
}
},
"node_modules/luxon": {
"version": "2.5.0",
"resolved": "https://registry.npmjs.org/luxon/-/luxon-2.5.0.tgz",
"integrity": "sha512-IDkEPB80Rb6gCAU+FEib0t4FeJ4uVOuX1CQ9GsvU3O+JAGIgu0J7sf1OarXKaKDygTZIoJyU6YdZzTFRu+YR0A==",
"engines": {
"node": ">=12"
}
},
"node_modules/make-dir": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/make-dir/-/make-dir-3.1.0.tgz",
@@ -7839,16 +7759,45 @@
}
},
"node_modules/redis": {
"version": "4.3.1",
"resolved": "https://registry.npmjs.org/redis/-/redis-4.3.1.tgz",
"integrity": "sha512-cM7yFU5CA6zyCF7N/+SSTcSJQSRMEKN0k0Whhu6J7n9mmXRoXugfWDBo5iOzGwABmsWKSwGPTU5J4Bxbl+0mrA==",
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/redis/-/redis-3.1.2.tgz",
"integrity": "sha512-grn5KoZLr/qrRQVwoSkmzdbw6pwF+/rwODtrOr6vuBRiR/f3rjSTGupbF90Zpqm2oenix8Do6RV7pYEkGwlKkw==",
"dependencies": {
"@redis/bloom": "1.0.2",
"@redis/client": "1.3.0",
"@redis/graph": "1.0.1",
"@redis/json": "1.0.4",
"@redis/search": "1.1.0",
"@redis/time-series": "1.0.3"
"denque": "^1.5.0",
"redis-commands": "^1.7.0",
"redis-errors": "^1.2.0",
"redis-parser": "^3.0.0"
},
"engines": {
"node": ">=10"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/node-redis"
}
},
"node_modules/redis-commands": {
"version": "1.7.0",
"resolved": "https://registry.npmjs.org/redis-commands/-/redis-commands-1.7.0.tgz",
"integrity": "sha512-nJWqw3bTFy21hX/CPKHth6sfhZbdiHP6bTawSgQBlKOVRG7EZkfHbbHwQJnrE4vsQf0CMNE+3gJ4Fmm16vdVlQ=="
},
"node_modules/redis-errors": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/redis-errors/-/redis-errors-1.2.0.tgz",
"integrity": "sha1-62LSrbFeTq9GEMBK/hUpOEJQq60=",
"engines": {
"node": ">=4"
}
},
"node_modules/redis-parser": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/redis-parser/-/redis-parser-3.0.0.tgz",
"integrity": "sha1-tm2CjNyv5rS4pCin3vTGvKwxyLQ=",
"dependencies": {
"redis-errors": "^1.0.0"
},
"engines": {
"node": ">=4"
}
},
"node_modules/reflect-metadata": {
@@ -9351,9 +9300,9 @@
}
},
"node_modules/typeorm": {
"version": "0.3.7",
"resolved": "https://registry.npmjs.org/typeorm/-/typeorm-0.3.7.tgz",
"integrity": "sha512-MsPJeP6Zuwfe64c++l80+VRqpGEGxf0CkztIEnehQ+CMmQPSHjOnFbFxwBuZ2jiLqZTjLk2ZqQdVF0RmvxNF3Q==",
"version": "0.3.6",
"resolved": "https://registry.npmjs.org/typeorm/-/typeorm-0.3.6.tgz",
"integrity": "sha512-DRqgfqcelMiGgWSMbBmVoJNFN2nPNA3EeY2gC324ndr2DZoGRTb9ILtp2oGVGnlA+cu5zgQ6it5oqKFNkte7Aw==",
"dependencies": {
"@sqltools/formatter": "^1.2.2",
"app-root-path": "^3.0.0",
@@ -9386,12 +9335,12 @@
},
"peerDependencies": {
"@google-cloud/spanner": "^5.18.0",
"@sap/hana-client": "^2.12.25",
"@sap/hana-client": "^2.11.14",
"better-sqlite3": "^7.1.2",
"hdb-pool": "^0.1.6",
"ioredis": "^5.0.4",
"ioredis": "^4.28.3",
"mongodb": "^3.6.0",
"mssql": "^7.3.0",
"mssql": "^6.3.1",
"mysql2": "^2.2.5",
"oracledb": "^5.1.0",
"pg": "^8.5.1",
@@ -9399,7 +9348,7 @@
"pg-query-stream": "^4.0.0",
"redis": "^3.1.1 || ^4.0.0",
"sql.js": "^1.4.0",
"sqlite3": "^5.0.3",
"sqlite3": "^5.0.2",
"ts-node": "^10.7.0",
"typeorm-aurora-data-api-driver": "^2.0.0"
},
@@ -9777,14 +9726,6 @@
"node": ">= 0.8.x"
}
},
"node_modules/unique-names-generator": {
"version": "4.7.1",
"resolved": "https://registry.npmjs.org/unique-names-generator/-/unique-names-generator-4.7.1.tgz",
"integrity": "sha512-lMx9dX+KRmG8sq6gulYYpKWZc9RlGsgBR6aoO8Qsm3qvkSJ+3rAymr+TnV8EDMrIrwuFJ4kruzMWM/OpYzPoow==",
"engines": {
"node": ">=8"
}
},
"node_modules/universalify": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz",
@@ -10847,20 +10788,6 @@
"kuler": "^2.0.0"
}
},
"@datasert/cronjs-matcher": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/@datasert/cronjs-matcher/-/cronjs-matcher-1.2.0.tgz",
"integrity": "sha512-ht6Vwwa3qssMn/9bphypjG/U8w0DV3GtTS2C6kbAy39rerQFTRzmml9xZNlot1K13gm9K/EEq3DLPEOsH++ICw==",
"requires": {
"@datasert/cronjs-parser": "^1.2.0",
"luxon": "^2.1.1"
}
},
"@datasert/cronjs-parser": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/@datasert/cronjs-parser/-/cronjs-parser-1.2.0.tgz",
"integrity": "sha512-7kzYh7F5V3ElX+k3W9w6SKS6WdjqJQ2gIY1y0evldnjAwZxnFzR/Yu9Mv9OeDaCQX+mGAq2MvEnJbwu9oj3CXQ=="
},
"@googleapis/youtube": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/@googleapis/youtube/-/youtube-2.0.0.tgz",
@@ -10869,17 +10796,6 @@
"googleapis-common": "^5.0.1"
}
},
"@influxdata/influxdb-client": {
"version": "1.31.0",
"resolved": "https://registry.npmjs.org/@influxdata/influxdb-client/-/influxdb-client-1.31.0.tgz",
"integrity": "sha512-8DVT3ZB/VeCK5Nn+BxhgMrAMSTseQAEgV20AK+ZMO5Fcup9XWsA9L2zE+3eBFl0Y+lF3UeKiASkiKMQvws35GA=="
},
"@influxdata/influxdb-client-apis": {
"version": "1.31.0",
"resolved": "https://registry.npmjs.org/@influxdata/influxdb-client-apis/-/influxdb-client-apis-1.31.0.tgz",
"integrity": "sha512-6ALGNLxtfffhICobOdj13Z6vj6gdQVOzVXPoPNd+w7V60zrbGhTqzXHV1KMZ/lzOb6YkRTRODbxz4W/b/7N5hg==",
"requires": {}
},
"@istanbuljs/load-nyc-config": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz",
@@ -11127,46 +11043,6 @@
"resolved": "https://registry.npmjs.org/@nlpjs/slot/-/slot-4.22.17.tgz",
"integrity": "sha512-cNYcxf9DKB+fnRa2NxT5wbWq5j57R1WCTXLWI/1Cyycr227IP7GN7qaD4RbkzotBFFB8wm63UHod9frzmuiXxg=="
},
"@redis/bloom": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/@redis/bloom/-/bloom-1.0.2.tgz",
"integrity": "sha512-EBw7Ag1hPgFzdznK2PBblc1kdlj5B5Cw3XwI9/oG7tSn85/HKy3X9xHy/8tm/eNXJYHLXHJL/pkwBpFMVVefkw==",
"requires": {}
},
"@redis/client": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/@redis/client/-/client-1.3.0.tgz",
"integrity": "sha512-XCFV60nloXAefDsPnYMjHGtvbtHR8fV5Om8cQ0JYqTNbWcQo/4AryzJ2luRj4blveWazRK/j40gES8M7Cp6cfQ==",
"requires": {
"cluster-key-slot": "1.1.0",
"generic-pool": "3.8.2",
"yallist": "4.0.0"
}
},
"@redis/graph": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/@redis/graph/-/graph-1.0.1.tgz",
"integrity": "sha512-oDE4myMCJOCVKYMygEMWuriBgqlS5FqdWerikMoJxzmmTUErnTRRgmIDa2VcgytACZMFqpAOWDzops4DOlnkfQ==",
"requires": {}
},
"@redis/json": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/@redis/json/-/json-1.0.4.tgz",
"integrity": "sha512-LUZE2Gdrhg0Rx7AN+cZkb1e6HjoSKaeeW8rYnt89Tly13GBI5eP4CwDVr+MY8BAYfCg4/N15OUrtLoona9uSgw==",
"requires": {}
},
"@redis/search": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/@redis/search/-/search-1.1.0.tgz",
"integrity": "sha512-NyFZEVnxIJEybpy+YskjgOJRNsfTYqaPbK/Buv6W2kmFNaRk85JiqjJZA5QkRmWvGbyQYwoO5QfDi2wHskKrQQ==",
"requires": {}
},
"@redis/time-series": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/@redis/time-series/-/time-series-1.0.3.tgz",
"integrity": "sha512-OFp0q4SGrTH0Mruf6oFsHGea58u8vS/iI5+NpYdicaM+7BgqBZH8FFvNZ8rYYLrUO/QRqMq72NpXmxLVNcdmjA==",
"requires": {}
},
"@sindresorhus/is": {
"version": "4.6.0",
"resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-4.6.0.tgz",
@@ -11266,6 +11142,16 @@
"integrity": "sha512-71aBXoFYXZW4TnDHHH8gExw2lS28BZaWeKefgsiJI7QYZeJfUEbMKw6CQtzGjlYQcGIWwB76hcCrkVA3YHSvsw==",
"dev": true
},
"@types/cache-manager-redis-store": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/@types/cache-manager-redis-store/-/cache-manager-redis-store-2.0.1.tgz",
"integrity": "sha512-8QuccvcPieh1xM/5kReE76SfdcIdEB0ePc+54ah/NBuK2eG+6O50SX4WKoJX81UxGdW3sh/WlDaDNqjnqxWNsA==",
"dev": true,
"requires": {
"@types/cache-manager": "*",
"@types/redis": "^2.8.0"
}
},
"@types/cacheable-request": {
"version": "6.0.2",
"resolved": "https://registry.npmjs.org/@types/cacheable-request/-/cacheable-request-6.0.2.tgz",
@@ -11549,6 +11435,15 @@
"resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.4.tgz",
"integrity": "sha512-EEhsLsD6UsDM1yFhAvy0Cjr6VwmpMWqFBCb9w07wVugF7w9nfajxLuVmngTIpgS6svCnm6Vaw+MZhoDCKnOfsw=="
},
"@types/redis": {
"version": "2.8.32",
"resolved": "https://registry.npmjs.org/@types/redis/-/redis-2.8.32.tgz",
"integrity": "sha512-7jkMKxcGq9p242exlbsVzuJb57KqHRhNl4dHoQu2Y5v9bCAbtIXXH0R3HleSQW4CTOqpHIYUW3t6tpUj4BVQ+w==",
"dev": true,
"requires": {
"@types/node": "*"
}
},
"@types/responselike": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/@types/responselike/-/responselike-1.0.0.tgz",
@@ -12286,11 +12181,11 @@
}
},
"cache-manager-redis-store": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/cache-manager-redis-store/-/cache-manager-redis-store-3.0.1.tgz",
"integrity": "sha512-o560kw+dFqusC9lQJhcm6L2F2fMKobJ5af+FoR2PdnMVdpQ3f3Bz6qzvObTGyvoazQJxjQNWgMQeChP4vRTuXQ==",
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/cache-manager-redis-store/-/cache-manager-redis-store-2.0.0.tgz",
"integrity": "sha512-bWLWlUg6nCYHiJLCCYxY2MgvwvKnvlWwrbuynrzpjEIhfArD2GC9LtutIHFEPeyGVQN6C+WEw+P3r+BFBwhswg==",
"requires": {
"redis": "^4.3.1"
"redis": "^3.0.2"
}
},
"cacheable-lookup": {
@@ -12503,11 +12398,6 @@
"mimic-response": "^1.0.0"
}
},
"cluster-key-slot": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/cluster-key-slot/-/cluster-key-slot-1.1.0.tgz",
"integrity": "sha512-2Nii8p3RwAPiFwsnZvukotvow2rIHM+yQ6ZcBXGHdniadkYGZYiGmkHJIbZPIV9nfv7m/U1IPMVVcAhoWFeklw=="
},
"code-point-at": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/code-point-at/-/code-point-at-1.1.0.tgz",
@@ -12659,9 +12549,8 @@
}
},
"connect-typeorm": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/connect-typeorm/-/connect-typeorm-2.0.0.tgz",
"integrity": "sha512-0OcbHJkNMTJjSrbcKGljr4PKgRq13Dds7zQq3+8oaf4syQTgGvGv9OgnXo2qg+Bljkh4aJNzIvW74QOVLn8zrw==",
"version": "git+ssh://git@github.com/FoxxMD/connect-typeorm.git#a2f0a7225a1c218db0b919142076ce0774c8caa6",
"from": "connect-typeorm@github:FoxxMD/connect-typeorm#typeormBump",
"requires": {
"@types/debug": "0.0.31",
"@types/express-session": "^1.15.5",
@@ -12971,6 +12860,11 @@
"integrity": "sha1-hMbhWbgZBP3KWaDvRM2HDTElD5o=",
"optional": true
},
"denque": {
"version": "1.5.1",
"resolved": "https://registry.npmjs.org/denque/-/denque-1.5.1.tgz",
"integrity": "sha512-XwE+iZ4D6ZUB7mfYRMb5wByE8L74HCn30FBN7sWnXksWc1LO1bPDl67pBR9o/kC4z/xSNAwkMYcGgqDV3BE3Hw=="
},
"depd": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz",
@@ -13708,11 +13602,6 @@
"json-bigint": "^1.0.0"
}
},
"generic-pool": {
"version": "3.8.2",
"resolved": "https://registry.npmjs.org/generic-pool/-/generic-pool-3.8.2.tgz",
"integrity": "sha512-nGToKy6p3PAbYQ7p1UlWl6vSPwfwU6TMSWK7TTu+WUY4ZjyZQGniGGt2oNVvyNSpyZYSB43zMXVLcBm08MTMkg=="
},
"gensync": {
"version": "1.0.0-beta.2",
"resolved": "https://registry.npmjs.org/gensync/-/gensync-1.0.0-beta.2.tgz",
@@ -14961,11 +14850,6 @@
"yallist": "^4.0.0"
}
},
"luxon": {
"version": "2.5.0",
"resolved": "https://registry.npmjs.org/luxon/-/luxon-2.5.0.tgz",
"integrity": "sha512-IDkEPB80Rb6gCAU+FEib0t4FeJ4uVOuX1CQ9GsvU3O+JAGIgu0J7sf1OarXKaKDygTZIoJyU6YdZzTFRu+YR0A=="
},
"make-dir": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/make-dir/-/make-dir-3.1.0.tgz",
@@ -16493,16 +16377,32 @@
}
},
"redis": {
"version": "4.3.1",
"resolved": "https://registry.npmjs.org/redis/-/redis-4.3.1.tgz",
"integrity": "sha512-cM7yFU5CA6zyCF7N/+SSTcSJQSRMEKN0k0Whhu6J7n9mmXRoXugfWDBo5iOzGwABmsWKSwGPTU5J4Bxbl+0mrA==",
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/redis/-/redis-3.1.2.tgz",
"integrity": "sha512-grn5KoZLr/qrRQVwoSkmzdbw6pwF+/rwODtrOr6vuBRiR/f3rjSTGupbF90Zpqm2oenix8Do6RV7pYEkGwlKkw==",
"requires": {
"@redis/bloom": "1.0.2",
"@redis/client": "1.3.0",
"@redis/graph": "1.0.1",
"@redis/json": "1.0.4",
"@redis/search": "1.1.0",
"@redis/time-series": "1.0.3"
"denque": "^1.5.0",
"redis-commands": "^1.7.0",
"redis-errors": "^1.2.0",
"redis-parser": "^3.0.0"
}
},
"redis-commands": {
"version": "1.7.0",
"resolved": "https://registry.npmjs.org/redis-commands/-/redis-commands-1.7.0.tgz",
"integrity": "sha512-nJWqw3bTFy21hX/CPKHth6sfhZbdiHP6bTawSgQBlKOVRG7EZkfHbbHwQJnrE4vsQf0CMNE+3gJ4Fmm16vdVlQ=="
},
"redis-errors": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/redis-errors/-/redis-errors-1.2.0.tgz",
"integrity": "sha1-62LSrbFeTq9GEMBK/hUpOEJQq60="
},
"redis-parser": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/redis-parser/-/redis-parser-3.0.0.tgz",
"integrity": "sha1-tm2CjNyv5rS4pCin3vTGvKwxyLQ=",
"requires": {
"redis-errors": "^1.0.0"
}
},
"reflect-metadata": {
@@ -17657,9 +17557,9 @@
}
},
"typeorm": {
"version": "0.3.7",
"resolved": "https://registry.npmjs.org/typeorm/-/typeorm-0.3.7.tgz",
"integrity": "sha512-MsPJeP6Zuwfe64c++l80+VRqpGEGxf0CkztIEnehQ+CMmQPSHjOnFbFxwBuZ2jiLqZTjLk2ZqQdVF0RmvxNF3Q==",
"version": "0.3.6",
"resolved": "https://registry.npmjs.org/typeorm/-/typeorm-0.3.6.tgz",
"integrity": "sha512-DRqgfqcelMiGgWSMbBmVoJNFN2nPNA3EeY2gC324ndr2DZoGRTb9ILtp2oGVGnlA+cu5zgQ6it5oqKFNkte7Aw==",
"requires": {
"@sqltools/formatter": "^1.2.2",
"app-root-path": "^3.0.0",
@@ -17910,11 +17810,6 @@
"resolved": "https://registry.npmjs.org/unicode/-/unicode-14.0.0.tgz",
"integrity": "sha512-BjinxTXkbm9Jomp/YBTMGusr4fxIG67fNGShHIRAL16Ur2GJTq2xvLi+sxuiJmInCmwqqev2BCFKyvbfp/yAkg=="
},
"unique-names-generator": {
"version": "4.7.1",
"resolved": "https://registry.npmjs.org/unique-names-generator/-/unique-names-generator-4.7.1.tgz",
"integrity": "sha512-lMx9dX+KRmG8sq6gulYYpKWZc9RlGsgBR6aoO8Qsm3qvkSJ+3rAymr+TnV8EDMrIrwuFJ4kruzMWM/OpYzPoow=="
},
"universalify": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz",

View File

@@ -1,6 +1,6 @@
{
"name": "redditcontextbot",
"version": "0.11.4",
"version": "0.5.1",
"description": "",
"main": "index.js",
"scripts": {
@@ -8,15 +8,13 @@
"build": "tsc && npm run bundle-front",
"bundle-front": "browserify src/Web/assets/browser.js | terser --compress --mangle > src/Web/assets/public/browserBundle.js",
"start": "node src/index.js run",
"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-check & npm run -s schema-run & npm run -s schema-config",
"schema-app": "typescript-json-schema tsconfig.json SubredditConfigData --out src/Schema/App.json --required --tsNodeRegister --refs --validationKeywords deprecationMessage",
"schema-ruleset": "typescript-json-schema tsconfig.json RuleSetConfigData --out src/Schema/RuleSet.json --required --tsNodeRegister --refs --validationKeywords deprecationMessage",
"schema-rule": "typescript-json-schema tsconfig.json RuleConfigData --out src/Schema/Rule.json --required --tsNodeRegister --refs --validationKeywords deprecationMessage",
"schema-check": "typescript-json-schema tsconfig.json ActivityCheckConfigValue --out src/Schema/Check.json --required --tsNodeRegister --refs --validationKeywords deprecationMessage",
"schema-run": "typescript-json-schema tsconfig.json RunConfigValue --out src/Schema/Run.json --required --tsNodeRegister --refs --validationKeywords deprecationMessage",
"schema-action": "typescript-json-schema tsconfig.json ActionConfigData --out src/Schema/Action.json --required --tsNodeRegister --refs --validationKeywords deprecationMessage",
"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 --validationKeywords deprecationMessage",
"schema-ruleset": "typescript-json-schema tsconfig.json RuleSetJson --out src/Schema/RuleSet.json --required --tsNodeRegister --refs --validationKeywords deprecationMessage",
"schema-rule": "typescript-json-schema tsconfig.json RuleJson --out src/Schema/Rule.json --required --tsNodeRegister --refs --validationKeywords deprecationMessage",
"schema-action": "typescript-json-schema tsconfig.json ActionJson --out src/Schema/Action.json --required --tsNodeRegister --refs --validationKeywords deprecationMessage",
"schema-config": "typescript-json-schema tsconfig.json OperatorJsonConfig --out src/Schema/OperatorConfig.json --required --tsNodeRegister --refs --validationKeywords deprecationMessage",
"schemaNotWorking": "./node_modules/.bin/ts-json-schema-generator -f tsconfig.json -p src/SubredditConfigData.ts -t JSONConfig --out src/Schema/vegaSchema.json",
"schemaNotWorking": "./node_modules/.bin/ts-json-schema-generator -f tsconfig.json -p src/JsonConfig.ts -t JSONConfig --out src/Schema/vegaSchema.json",
"circular": "madge --circular --extensions ts src/index.ts",
"circular-graph": "madge --image graph.svg --circular --extensions ts src/index.ts",
"postinstall": "patch-package",
@@ -24,17 +22,14 @@
"initMigration": "npm run typeorm -- migration:generate -t 1642180264563 -d ormconfig.js \"src/Common/Migrations/Database/init\""
},
"engines": {
"node": ">=16.18.0"
"node": ">=16"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@awaitjs/express": "^0.8.0",
"@datasert/cronjs-matcher": "^1.2.0",
"@googleapis/youtube": "^2.0.0",
"@influxdata/influxdb-client": "^1.31.0",
"@influxdata/influxdb-client-apis": "^1.31.0",
"@nlpjs/core": "^4.23.4",
"@nlpjs/lang-de": "^4.23.4",
"@nlpjs/lang-en": "^4.23.4",
@@ -49,10 +44,10 @@
"autolinker": "^3.14.3",
"body-parser": "^1.19.0",
"cache-manager": "^3.4.4",
"cache-manager-redis-store": "^3.0.1",
"cache-manager-redis-store": "^2.0.0",
"commander": "^8.0.0",
"comment-json": "^4.1.1",
"connect-typeorm": "^2.0.0",
"connect-typeorm": "github:FoxxMD/connect-typeorm#typeormBump",
"cookie-parser": "^1.3.5",
"dayjs": "^1.10.5",
"deepmerge": "^4.2.2",
@@ -96,9 +91,8 @@
"string-similarity": "^4.0.4",
"tcp-port-used": "^1.0.2",
"triple-beam": "^1.3.0",
"typeorm": "^0.3.7",
"typeorm": "^0.3.4",
"typeorm-logger-adaptor": "^1.1.0",
"unique-names-generator": "^4.7.1",
"vader-sentiment": "^1.1.3",
"webhook-discord": "^3.7.7",
"wink-sentiment": "^5.0.2",
@@ -115,6 +109,7 @@
"@tsconfig/node14": "^1.0.0",
"@types/async": "^3.2.7",
"@types/cache-manager": "^3.4.2",
"@types/cache-manager-redis-store": "^2.0.0",
"@types/chai": "^4.3.0",
"@types/chai-as-promised": "^7.1.5",
"@types/cookie-parser": "^1.4.2",

View File

@@ -3,7 +3,7 @@ import LockAction, {LockActionJson} from "./LockAction";
import {RemoveAction, RemoveActionJson} from "./RemoveAction";
import {ReportAction, ReportActionJson} from "./ReportAction";
import {FlairAction, FlairActionJson} from "./SubmissionAction/FlairAction";
import Action, {ActionJson, ActionRuntimeOptions, StructuredActionJson} from "./index";
import Action, {ActionJson, StructuredActionJson} from "./index";
import {Logger} from "winston";
import {UserNoteAction, UserNoteActionJson} from "./UserNoteAction";
import ApproveAction, {ApproveActionConfig} from "./ApproveAction";
@@ -17,42 +17,36 @@ import {DispatchAction, DispatchActionJson} from "./DispatchAction";
import {CancelDispatchAction, CancelDispatchActionJson} from "./CancelDispatchAction";
import ContributorAction, {ContributorActionJson} from "./ContributorAction";
import {StructuredFilter} from "../Common/Infrastructure/Filters/FilterShapes";
import {ModNoteAction, ModNoteActionJson} from "./ModNoteAction";
import {SubmissionAction, SubmissionActionJson} from "./SubmissionAction";
export function actionFactory
(config: StructuredActionJson, runtimeOptions: ActionRuntimeOptions): Action {
(config: StructuredActionJson, logger: Logger, subredditName: string, resources: SubredditResources, client: ExtendedSnoowrap, emitter: EventEmitter): Action {
switch (config.kind) {
case 'comment':
return new CommentAction({...config as StructuredFilter<CommentActionJson>, ...runtimeOptions});
case 'submission':
return new SubmissionAction({...config as StructuredFilter<SubmissionActionJson>, ...runtimeOptions});
return new CommentAction({...config as StructuredFilter<CommentActionJson>, logger, subredditName, resources, client, emitter});
case 'lock':
return new LockAction({...config as StructuredFilter<LockActionJson>, ...runtimeOptions});
return new LockAction({...config as StructuredFilter<LockActionJson>, logger, subredditName, resources, client, emitter});
case 'remove':
return new RemoveAction({...config as StructuredFilter<RemoveActionJson>, ...runtimeOptions});
return new RemoveAction({...config as StructuredFilter<RemoveActionJson>, logger, subredditName, resources, client, emitter});
case 'report':
return new ReportAction({...config as StructuredFilter<ReportActionJson>, ...runtimeOptions});
return new ReportAction({...config as StructuredFilter<ReportActionJson>, logger, subredditName, resources, client, emitter});
case 'flair':
return new FlairAction({...config as StructuredFilter<FlairActionJson>, ...runtimeOptions});
return new FlairAction({...config as StructuredFilter<FlairActionJson>, logger, subredditName, resources, client, emitter});
case 'userflair':
return new UserFlairAction({...config as StructuredFilter<UserFlairActionJson>, ...runtimeOptions});
return new UserFlairAction({...config as StructuredFilter<UserFlairActionJson>, logger, subredditName, resources, client, emitter});
case 'approve':
return new ApproveAction({...config as StructuredFilter<ApproveActionConfig>, ...runtimeOptions});
return new ApproveAction({...config as StructuredFilter<ApproveActionConfig>, logger, subredditName, resources, client, emitter});
case 'usernote':
return new UserNoteAction({...config as StructuredFilter<UserNoteActionJson>, ...runtimeOptions});
return new UserNoteAction({...config as StructuredFilter<UserNoteActionJson>, logger, subredditName, resources, client, emitter});
case 'ban':
return new BanAction({...config as StructuredFilter<BanActionJson>, ...runtimeOptions});
return new BanAction({...config as StructuredFilter<BanActionJson>, logger, subredditName, resources, client, emitter});
case 'message':
return new MessageAction({...config as StructuredFilter<MessageActionJson>, ...runtimeOptions});
return new MessageAction({...config as StructuredFilter<MessageActionJson>, logger, subredditName, resources, client, emitter});
case 'dispatch':
return new DispatchAction({...config as StructuredFilter<DispatchActionJson>, ...runtimeOptions});
return new DispatchAction({...config as StructuredFilter<DispatchActionJson>, logger, subredditName, resources, client, emitter});
case 'cancelDispatch':
return new CancelDispatchAction({...config as StructuredFilter<CancelDispatchActionJson>, ...runtimeOptions})
return new CancelDispatchAction({...config as StructuredFilter<CancelDispatchActionJson>, logger, subredditName, resources, client, emitter})
case 'contributor':
return new ContributorAction({...config as StructuredFilter<ContributorActionJson>, ...runtimeOptions})
case 'modnote':
return new ModNoteAction({...config as StructuredFilter<ModNoteActionJson>, ...runtimeOptions})
return new ContributorAction({...config as StructuredFilter<ContributorActionJson>, logger, subredditName, resources, client, emitter})
default:
throw new Error('rule "kind" was not recognized.');
}

View File

@@ -8,7 +8,6 @@ import {RuleResultEntity} from "../Common/Entities/RuleResultEntity";
import {runCheckOptions} from "../Subreddit/Manager";
import {ActionTarget, ActionTypes} from "../Common/Infrastructure/Atomic";
import {asComment, asSubmission} from "../util";
import {ActionResultEntity} from "../Common/Entities/ActionResultEntity";
export class ApproveAction extends Action {
@@ -27,7 +26,7 @@ export class ApproveAction extends Action {
this.targets = targets;
}
async process(item: Comment | Submission, ruleResults: RuleResultEntity[], actionResults: ActionResultEntity[], options: runCheckOptions): Promise<ActionProcessResult> {
async process(item: Comment | Submission, ruleResults: RuleResultEntity[], options: runCheckOptions): Promise<ActionProcessResult> {
const dryRun = this.getRuntimeAwareDryrun(options);
const touchedEntities = [];

View File

@@ -6,18 +6,6 @@ import {ActionProcessResult, Footer, RuleResult} from "../Common/interfaces";
import {RuleResultEntity} from "../Common/Entities/RuleResultEntity";
import {runCheckOptions} from "../Subreddit/Manager";
import {ActionTypes} from "../Common/Infrastructure/Atomic";
import {truncateStringToLength} from "../util";
import {ActionResultEntity} from "../Common/Entities/ActionResultEntity";
const truncate = truncateStringToLength(100);
const truncateLongMessage = truncateStringToLength(200);
const truncateIfNotUndefined = (val: string | undefined) => {
if(val === undefined) {
return undefined;
}
return truncate(val);
}
export class BanAction extends Action {
@@ -47,19 +35,17 @@ export class BanAction extends Action {
return 'ban';
}
async process(item: Comment | Submission, ruleResults: RuleResultEntity[], actionResults: ActionResultEntity[], options: runCheckOptions): Promise<ActionProcessResult> {
async process(item: Comment | Submission, ruleResults: RuleResultEntity[], options: runCheckOptions): Promise<ActionProcessResult> {
const dryRun = this.getRuntimeAwareDryrun(options);
const renderedBody = await this.renderContent(this.message, item, ruleResults, actionResults);
const renderedContent = renderedBody === undefined ? undefined : `${renderedBody}${await this.resources.renderFooter(item, this.footer)}`;
const renderedReason = truncateIfNotUndefined(await this.renderContent(this.reason, item, ruleResults, actionResults) as string);
const renderedNote = truncateIfNotUndefined(await this.renderContent(this.note, item, ruleResults, actionResults) as string);
const content = this.message === undefined ? undefined : await this.resources.getContent(this.message, item.subreddit);
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${truncateLongMessage(renderedContent)}` : renderedContent}`}`);
banPieces.push(`Reason: ${renderedReason || 'None'}`);
banPieces.push(`Note: ${renderedNote || 'None'}`);
banPieces.push(`Message: ${renderedContent === undefined ? 'None' : `${renderedContent.length > 100 ? `\r\n${renderedContent}` : renderedContent}`}`);
banPieces.push(`Reason: ${this.reason || 'None'}`);
banPieces.push(`Note: ${this.note || 'None'}`);
const durText = this.duration === undefined ? 'permanently' : `for ${this.duration} days`;
this.logger.info(`Banning ${item.author.name} ${durText}${this.reason !== undefined ? ` (${this.reason})` : ''}`);
this.logger.verbose(`\r\n${banPieces.join('\r\n')}`);
@@ -70,8 +56,8 @@ export class BanAction extends Action {
const bannedUser = await fetchedSub.banUser({
name: fetchedName,
banMessage: renderedContent === undefined ? undefined : renderedContent,
banReason: renderedReason,
banNote: renderedNote,
banReason: this.reason,
banNote: this.note,
duration: this.duration
});
touchedEntities.push(bannedUser);
@@ -79,14 +65,8 @@ export class BanAction extends Action {
return {
dryRun,
success: true,
result: `Banned ${item.author.name} ${durText}${renderedReason !== undefined ? ` (${renderedReason})` : ''}`,
touchedEntities,
data: {
message: renderedContent === undefined ? undefined : renderedContent,
reason: renderedReason,
note: renderedNote,
duration: durText
}
result: `Banned ${item.author.name} ${durText}${this.reason !== undefined ? ` (${this.reason})` : ''}`,
touchedEntities
};
}
@@ -117,10 +97,8 @@ export interface BanActionConfig extends ActionConfig, Footer {
* */
message?: string
/**
* Reason for ban. Can use Templating.
*
* If the length expands to more than 100 characters it will truncated with "..."
*
* Reason for ban.
* @maxLength 100
* @examples ["repeat spam"]
* */
reason?: string
@@ -132,10 +110,8 @@ export interface BanActionConfig extends ActionConfig, Footer {
* */
duration?: number
/**
* A mod note for this ban. Can use Templating.
*
* If the length expands to more than 100 characters it will truncated with "..."
*
* A mod note for this ban
* @maxLength 100
* @examples ["Sock puppet for u/AnotherUser"]
* */
note?: string

View File

@@ -12,7 +12,6 @@ import {isSubmission, parseDurationValToDuration} from "../util";
import {RuleResultEntity} from "../Common/Entities/RuleResultEntity";
import {runCheckOptions} from "../Subreddit/Manager";
import {ActionTarget, ActionTypes, InclusiveActionTarget} from "../Common/Infrastructure/Atomic";
import {ActionResultEntity} from "../Common/Entities/ActionResultEntity";
export class CancelDispatchAction extends Action {
identifiers?: (string | null)[];
@@ -36,7 +35,7 @@ export class CancelDispatchAction extends Action {
this.targets = !Array.isArray(target) ? [target] : target;
}
async process(item: Comment | Submission, ruleResults: RuleResultEntity[], actionResults: ActionResultEntity[], options: runCheckOptions): Promise<ActionProcessResult> {
async process(item: Comment | Submission, ruleResults: RuleResultEntity[], options: runCheckOptions): Promise<ActionProcessResult> {
// see note in DispatchAction about missing runtimeDryrun
const dryRun = this.dryRun;

View File

@@ -1,15 +1,12 @@
import Action, {ActionJson, ActionOptions} from "./index";
import {Comment, VoteableContent} from "snoowrap";
import {Comment} from "snoowrap";
import Submission from "snoowrap/dist/objects/Submission";
import {renderContent} from "../Utils/SnoowrapUtils";
import {ActionProcessResult, Footer, RequiredRichContent, RichContent, RuleResult} from "../Common/interfaces";
import {asComment, asSubmission, parseRedditThingsFromLink, truncateStringToLength} from "../util";
import {truncateStringToLength} from "../util";
import {RuleResultEntity} from "../Common/Entities/RuleResultEntity";
import {runCheckOptions} from "../Subreddit/Manager";
import {ActionTarget, ActionTypes, ArbitraryActionTarget} from "../Common/Infrastructure/Atomic";
import {CMError} from "../Utils/Errors";
import {SnoowrapActivity} from "../Common/Infrastructure/Reddit";
import {ActionResultEntity} from "../Common/Entities/ActionResultEntity";
import {ActionTypes} from "../Common/Infrastructure/Atomic";
export class CommentAction extends Action {
content: string;
@@ -17,7 +14,6 @@ export class CommentAction extends Action {
sticky: boolean = false;
distinguish: boolean = false;
footer?: false | string;
targets: ArbitraryActionTarget[]
constructor(options: CommentActionOptions) {
super(options);
@@ -27,139 +23,71 @@ export class CommentAction extends Action {
sticky = false,
distinguish = false,
footer,
targets = ['self']
} = options;
this.footer = footer;
this.content = content;
this.lock = lock;
this.sticky = sticky;
this.distinguish = distinguish;
if (!Array.isArray(targets)) {
this.targets = [targets];
} else {
this.targets = targets;
}
}
getKind(): ActionTypes {
return 'comment';
}
async process(item: Comment | Submission, ruleResults: RuleResultEntity[], actionResults: ActionResultEntity[], options: runCheckOptions): Promise<ActionProcessResult> {
async process(item: Comment | Submission, ruleResults: RuleResultEntity[], options: runCheckOptions): Promise<ActionProcessResult> {
const dryRun = this.getRuntimeAwareDryrun(options);
const body = await this.renderContent(this.content, item, ruleResults, actionResults) as string;
const content = await this.resources.getContent(this.content, item.subreddit);
const body = await renderContent(content, item, ruleResults, this.resources.userNotes);
const footer = await this.resources.renderFooter(item, this.footer);
const footer = await this.resources.generateFooter(item, this.footer);
const renderedContent = `${body}${footer}`;
this.logger.verbose(`Contents:\r\n${renderedContent.length > 100 ? `\r\n${renderedContent}` : renderedContent}`);
let allErrors = true;
const targetResults: string[] = [];
if(item.archived) {
this.logger.warn('Cannot comment because Item is archived');
return {
dryRun,
success: false,
result: 'Cannot comment because Item is archived'
};
}
const touchedEntities = [];
for (const target of this.targets) {
let targetItem = item;
let targetIdentifier = target;
if (target === 'parent') {
if (asSubmission(item)) {
const noParent = `[Parent] Submission ${item.name} does not have a parent`;
this.logger.warn(noParent);
targetResults.push(noParent);
continue;
}
targetItem = await this.resources.getActivity(this.client.getSubmission(item.link_id));
} else if (target !== 'self') {
const redditThings = parseRedditThingsFromLink(target);
let id = '';
try {
if (redditThings.comment !== undefined) {
id = redditThings.comment.id;
targetIdentifier = `Permalink Comment ${id}`
// @ts-ignore
await this.resources.getActivity(this.client.getSubmission(redditThings.submission.id));
targetItem = await this.resources.getActivity(this.client.getComment(redditThings.comment.id));
} else if (redditThings.submission !== undefined) {
id = redditThings.submission.id;
targetIdentifier = `Permalink Submission ${id}`
targetItem = await this.resources.getActivity(this.client.getSubmission(redditThings.submission.id));
} else {
targetResults.push(`[Permalink] Could not parse ${target} as a reddit permalink`);
continue;
}
} catch (err: any) {
targetResults.push(`[${targetIdentifier}] error occurred while fetching activity: ${err.message}`);
this.logger.warn(new CMError(`[${targetIdentifier}] error occurred while fetching activity`, {cause: err}));
continue;
}
}
if (targetItem.archived) {
const archived = `[${targetIdentifier}] Cannot comment because Item is archived`;
this.logger.warn(archived);
targetResults.push(archived);
continue;
}
let modifiers = [];
let reply: Comment;
if (!dryRun) {
// @ts-ignore
reply = await targetItem.reply(renderedContent);
// add to recent so we ignore activity when/if it is discovered by polling
await this.resources.setRecentSelf(reply);
touchedEntities.push(reply);
}
if (this.lock && targetItem.can_mod_post) {
if (!targetItem.can_mod_post) {
this.logger.warn(`[${targetIdentifier}] Cannot lock because bot is not a moderator`);
} else {
modifiers.push('Locked');
if (!dryRun) {
// snoopwrap typing issue, thinks comments can't be locked
// @ts-ignore
await reply.lock();
}
}
}
if (this.distinguish) {
if (!targetItem.can_mod_post) {
this.logger.warn(`[${targetIdentifier}] Cannot lock Distinguish/Sticky because bot is not a moderator`);
} else {
modifiers.push('Distinguished');
if (this.sticky) {
modifiers.push('Stickied');
}
if (!dryRun) {
// @ts-ignore
await reply.distinguish({sticky: this.sticky});
}
}
}
const modifierStr = modifiers.length === 0 ? '' : ` == ${modifiers.join(' | ')} == =>`;
let modifiers = [];
let reply: Comment;
if(!dryRun) {
// @ts-ignore
targetResults.push(`${targetIdentifier}${modifierStr} created Comment ${dryRun ? 'DRYRUN' : (reply as SnoowrapActivity).name}`)
allErrors = false;
reply = await item.reply(renderedContent);
// add to recent so we ignore activity when/if it is discovered by polling
await this.resources.setRecentSelf(reply);
touchedEntities.push(reply);
}
if (this.lock) {
modifiers.push('Locked');
if (!dryRun) {
// snoopwrap typing issue, thinks comments can't be locked
// @ts-ignore
await reply.lock();
}
}
if (this.distinguish && !dryRun) {
modifiers.push('Distinguished');
if(this.sticky) {
modifiers.push('Stickied');
}
if(!dryRun) {
// @ts-ignore
await reply.distinguish({sticky: this.sticky});
}
}
const modifierStr = modifiers.length === 0 ? '' : `[${modifiers.join(' | ')}]`;
return {
dryRun,
success: !allErrors,
result: `${targetResults.join('\n')}${truncateStringToLength(100)(body)}`,
success: true,
result: `${modifierStr}${truncateStringToLength(100)(body)}`,
touchedEntities,
data: {
body,
bodyShort: truncateStringToLength(100)(body),
comments: targetResults,
commentsFormatted: targetResults.map(x => `* ${x}`).join('\n')
}
};
}
@@ -169,8 +97,7 @@ export class CommentAction extends Action {
lock: this.lock,
sticky: this.sticky,
distinguish: this.distinguish,
footer: this.footer,
targets: this.targets,
footer: this.footer
}
}
}
@@ -188,21 +115,6 @@ export interface CommentActionConfig extends RequiredRichContent, Footer {
* Distinguish the comment after creation?
* */
distinguish?: boolean,
/**
* Specify where this comment should be made
*
* Valid values: 'self' | 'parent' | [reddit permalink]
*
* 'self' and 'parent' are special targets that are relative to the Activity being processed:
* * When Activity is Submission => 'parent' does nothing
* * When Activity is Comment
* * 'self' => reply to Activity
* * 'parent' => make a top-level comment in the Submission the Comment is in
*
* If target is not self/parent then CM assumes the value is a reddit permalink and will attempt to make a comment to that Activity
* */
targets?: ArbitraryActionTarget | ArbitraryActionTarget[]
}
export interface CommentActionOptions extends CommentActionConfig, ActionOptions {
@@ -212,5 +124,5 @@ export interface CommentActionOptions extends CommentActionConfig, ActionOptions
* Reply to the Activity. For a submission the reply will be a top-level comment.
* */
export interface CommentActionJson extends CommentActionConfig, ActionJson {
kind: 'comment'
kind: 'comment'
}

View File

@@ -7,7 +7,6 @@ import Comment from "snoowrap/dist/objects/Comment";
import {RuleResultEntity} from "../Common/Entities/RuleResultEntity";
import {runCheckOptions} from "../Subreddit/Manager";
import {ActionTarget, ActionTypes} from "../Common/Infrastructure/Atomic";
import {ActionResultEntity} from "../Common/Entities/ActionResultEntity";
export class ContributorAction extends Action {
@@ -26,7 +25,7 @@ export class ContributorAction extends Action {
this.actionType = action;
}
async process(item: Comment | Submission, ruleResults: RuleResultEntity[], actionResults: ActionResultEntity[], options: runCheckOptions): Promise<ActionProcessResult> {
async process(item: Comment | Submission, ruleResults: RuleResultEntity[], options: runCheckOptions): Promise<ActionProcessResult> {
const dryRun = this.getRuntimeAwareDryrun(options);
const contributors = await this.resources.getSubredditContributors();

View File

@@ -8,7 +8,6 @@ import {activityDispatchConfigToDispatch, isSubmission, parseDurationValToDurati
import {RuleResultEntity} from "../Common/Entities/RuleResultEntity";
import {runCheckOptions} from "../Subreddit/Manager";
import {ActionTarget, ActionTypes} from "../Common/Infrastructure/Atomic";
import {ActionResultEntity} from "../Common/Entities/ActionResultEntity";
export class DispatchAction extends Action {
dispatchData: ActivityDispatchConfig;
@@ -40,7 +39,7 @@ export class DispatchAction extends Action {
this.targets = !Array.isArray(target) ? [target] : target;
}
async process(item: Comment | Submission, ruleResults: RuleResultEntity[], actionResults: ActionResultEntity[], options: runCheckOptions): Promise<ActionProcessResult> {
async process(item: Comment | Submission, ruleResults: RuleResultEntity[], options: runCheckOptions): Promise<ActionProcessResult> {
// ignore runtimeDryrun here because "real run" isn't causing any reddit api calls to happen
// -- basically if bot is in dryrun this should still run since we want the "full effect" of the bot
// BUT if the action explicitly sets 'dryRun: true' then do not dispatch as they probably don't want to it actually going (intention?)

View File

@@ -5,14 +5,13 @@ import {ActionProcessResult, RuleResult} from "../Common/interfaces";
import {RuleResultEntity} from "../Common/Entities/RuleResultEntity";
import {runCheckOptions} from "../Subreddit/Manager";
import {ActionTypes} from "../Common/Infrastructure/Atomic";
import {ActionResultEntity} from "../Common/Entities/ActionResultEntity";
export class LockAction extends Action {
getKind(): ActionTypes {
return 'lock';
}
async process(item: Comment | Submission, ruleResults: RuleResultEntity[], actionResults: ActionResultEntity[], options: runCheckOptions): Promise<ActionProcessResult> {
async process(item: Comment | Submission, ruleResults: RuleResultEntity[], options: runCheckOptions): Promise<ActionProcessResult> {
const dryRun = this.getRuntimeAwareDryrun(options);
const touchedEntities = [];
//snoowrap typing issue, thinks comments can't be locked

View File

@@ -16,7 +16,6 @@ import {ErrorWithCause} from "pony-cause";
import {RuleResultEntity} from "../Common/Entities/RuleResultEntity";
import {runCheckOptions} from "../Subreddit/Manager";
import {ActionTypes} from "../Common/Infrastructure/Atomic";
import {ActionResultEntity} from "../Common/Entities/ActionResultEntity";
export class MessageAction extends Action {
content: string;
@@ -49,30 +48,27 @@ export class MessageAction extends Action {
return 'message';
}
async process(item: Comment | Submission, ruleResults: RuleResultEntity[], actionResults: ActionResultEntity[], options: runCheckOptions): Promise<ActionProcessResult> {
async process(item: Comment | Submission, ruleResults: RuleResultEntity[], options: runCheckOptions): Promise<ActionProcessResult> {
const dryRun = this.getRuntimeAwareDryrun(options);
const content = await this.resources.getContent(this.content);
const body = await renderContent(content, item, ruleResults, this.resources.userNotes);
const body = await this.renderContent(this.content, item, ruleResults, actionResults);
const titleTemplate = this.title ?? `Concerning your ${isSubmission(item) ? 'Submission' : 'Comment'}`;
const subject = await this.renderContent(titleTemplate, item, ruleResults, actionResults) as string;
const footer = await this.resources.renderFooter(item, this.footer);
const footer = await this.resources.generateFooter(item, this.footer);
const renderedContent = `${body}${footer}`;
let recipient = item.author.name;
if(this.to !== undefined) {
const renderedTo = await this.renderContent(this.to, item, ruleResults, actionResults) as string;
// parse to value
try {
const entityData = parseRedditEntity(renderedTo, 'user');
const entityData = parseRedditEntity(this.to, 'user');
if(entityData.type === 'user') {
recipient = entityData.name;
} else {
recipient = `/r/${entityData.name}`;
}
} catch (err: any) {
throw new ErrorWithCause(`'to' field for message was not in a valid format, given value after templating: ${renderedTo} -- See ${REDDIT_ENTITY_REGEX_URL} for valid examples`, {cause: err});
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}`);
@@ -84,7 +80,7 @@ export class MessageAction extends Action {
text: renderedContent,
// @ts-ignore
fromSubreddit: this.asSubreddit ? await item.subreddit.fetch() : undefined,
subject: subject,
subject: this.title || `Concerning your ${isSubmission(item) ? 'Submission' : 'Comment'}`,
};
const msgPreview = `\r\n
@@ -126,7 +122,7 @@ export interface MessageActionConfig extends RequiredRichContent, Footer {
asSubreddit: boolean
/**
* Entity to send message to. It can be templated.
* Entity to send message to.
*
* If not present Message be will sent to the Author of the Activity being checked.
*
@@ -138,9 +134,8 @@ export interface MessageActionConfig extends RequiredRichContent, Footer {
*
* **Note:** Reddit does not support sending a message AS a subreddit TO another subreddit
*
* **Tip:** To send a message to the subreddit of the Activity us `to: 'r/{{item.subreddit}}'`
*
* @examples ["aUserName","u/aUserName","r/aSubreddit", "r/{{item.subreddit}}"]
* @pattern ^\s*(\/[ru]\/|[ru]\/)*(\w+)*\s*$
* @examples ["aUserName","u/aUserName","r/aSubreddit"]
* */
to?: string

View File

@@ -1,134 +0,0 @@
import {ActionJson, ActionConfig, ActionOptions} from "./index";
import Action from "./index";
import {Comment} from "snoowrap";
import Submission from "snoowrap/dist/objects/Submission";
import {ActionProcessResult, RichContent} from "../Common/interfaces";
import {buildFilterCriteriaSummary, normalizeModActionCriteria, toModNoteLabel} from "../util";
import {RuleResultEntity} from "../Common/Entities/RuleResultEntity";
import {runCheckOptions} from "../Subreddit/Manager";
import {
ActionTypes,
ModUserNoteLabel,
} from "../Common/Infrastructure/Atomic";
import {ActionResultEntity} from "../Common/Entities/ActionResultEntity";
import {ModNoteCriteria} from "../Common/Infrastructure/Filters/FilterCriteria";
export class ModNoteAction extends Action {
content: string;
type?: string;
existingNoteCheck?: ModNoteCriteria
referenceActivity: boolean
constructor(options: ModNoteActionOptions) {
super(options);
const {type, content = '', existingNoteCheck = true, referenceActivity = true} = options;
this.type = type;
this.content = content;
this.referenceActivity = referenceActivity;
this.existingNoteCheck = typeof existingNoteCheck === 'boolean' ? this.generateModLogCriteriaFromDuplicateConvenience(existingNoteCheck) : normalizeModActionCriteria(existingNoteCheck);
}
getKind(): ActionTypes {
return 'modnote';
}
protected getSpecificPremise(): object {
return {
content: this.content,
type: this.type,
existingNoteCheck: this.existingNoteCheck,
referenceActivity: this.referenceActivity,
}
}
async process(item: Comment | Submission, ruleResults: RuleResultEntity[], actionResults: ActionResultEntity[], options: runCheckOptions): Promise<ActionProcessResult> {
const dryRun = this.getRuntimeAwareDryrun(options);
const modLabel = this.type !== undefined ? toModNoteLabel(this.type) : undefined;
const renderedContent = await this.renderContent(this.content, item, ruleResults, actionResults);
this.logger.verbose(`Note:\r\n(${this.type}) ${renderedContent}`);
let noteCheckPassed: boolean = true;
let noteCheckResult: undefined | string;
if(this.existingNoteCheck === undefined) {
// nothing to do!
noteCheckResult = 'existingNoteCheck=false so no existing note checks were performed.';
} else {
const noteCheckCriteriaResult = await this.resources.isAuthor(item, {
modActions: [this.existingNoteCheck]
});
noteCheckPassed = noteCheckCriteriaResult.passed;
const {details} = buildFilterCriteriaSummary(noteCheckCriteriaResult);
noteCheckResult = `${noteCheckPassed ? 'Existing note check condition succeeded' : 'Will not add note because existing note check condition failed'} -- ${details.join(' ')}`;
}
this.logger.info(noteCheckResult);
if (!noteCheckPassed) {
return {
dryRun,
success: false,
result: noteCheckResult
};
}
if (!dryRun) {
await this.resources.addModNote({
label: modLabel,
note: renderedContent,
activity: this.referenceActivity ? item : undefined,
subreddit: this.resources.subreddit,
user: item.author
});
}
return {
success: true,
dryRun,
result: `${modLabel !== undefined ? `(${modLabel})` : ''} ${renderedContent}`
}
}
generateModLogCriteriaFromDuplicateConvenience(val: boolean): ModNoteCriteria | undefined {
if(val) {
return {
noteType: this.type !== undefined ? [toModNoteLabel(this.type)] : undefined,
note: this.content !== '' ? [this.content] : undefined,
referencesCurrentActivity: this.referenceActivity ? true : undefined,
search: 'current',
count: '< 1'
}
}
return undefined;
}
}
export interface ModNoteActionConfig extends ActionConfig, RichContent {
/**
* Check if there is an existing Note matching some criteria before adding the Note.
*
* If this check passes then the Note is added. The value may be a boolean or ModNoteCriteria.
*
* Boolean convenience:
*
* * If `true` or undefined then CM generates a ModNoteCriteria that passes only if there is NO existing note matching note criteria
* * If `false` then no check is performed and Note is always added
*
* @examples [true]
* @default true
* */
existingNoteCheck?: boolean | ModNoteCriteria,
type?: ModUserNoteLabel
referenceActivity?: boolean
}
export interface ModNoteActionOptions extends Omit<ModNoteActionConfig, 'authorIs' | 'itemIs'>, ActionOptions {
}
/**
* Add a Toolbox User Note to the Author of this Activity
* */
export interface ModNoteActionJson extends ModNoteActionConfig, ActionJson {
kind: 'modnote'
}

View File

@@ -4,17 +4,13 @@ import Snoowrap, {Comment, Submission} from "snoowrap";
import {activityIsRemoved} from "../Utils/SnoowrapUtils";
import {ActionProcessResult, RuleResult} from "../Common/interfaces";
import dayjs from "dayjs";
import {isSubmission, truncateStringToLength} from "../util";
import {isSubmission} from "../util";
import {RuleResultEntity} from "../Common/Entities/RuleResultEntity";
import {runCheckOptions} from "../Subreddit/Manager";
import {ActionTypes} from "../Common/Infrastructure/Atomic";
import {ActionResultEntity} from "../Common/Entities/ActionResultEntity";
const truncate = truncateStringToLength(100);
export class RemoveAction extends Action {
spam: boolean;
note?: string;
reasonId?: string;
getKind(): ActionTypes {
return 'remove';
@@ -24,54 +20,21 @@ export class RemoveAction extends Action {
super(options);
const {
spam = false,
note,
reasonId,
} = options;
this.spam = spam;
this.note = note;
this.reasonId = reasonId;
}
async process(item: Comment | Submission, ruleResults: RuleResultEntity[], actionResults: ActionResultEntity[], options: runCheckOptions): Promise<ActionProcessResult> {
async process(item: Comment | Submission, ruleResults: RuleResultEntity[], options: runCheckOptions): Promise<ActionProcessResult> {
const dryRun = this.getRuntimeAwareDryrun(options);
const touchedEntities = [];
let removeSummary = [];
// issue with snoowrap typings, doesn't think prop exists on Submission
// @ts-ignore
if (activityIsRemoved(item)) {
this.logger.warn('It looks like this Item is already removed!');
}
if (this.spam) {
removeSummary.push('Marked as SPAM');
this.logger.verbose('Marking as spam on removal');
}
const renderedNote = await this.renderContent(this.note, item, ruleResults, actionResults);
let foundReasonId: string | undefined;
let foundReason: string | undefined;
if(this.reasonId !== undefined) {
const reason = await this.resources.getSubredditRemovalReasonById(this.reasonId);
if(reason === undefined) {
const reasonWarn = [`Could not find any Removal Reason with the ID ${this.reasonId}!`];
if(renderedNote === undefined) {
reasonWarn.push('Cannot add any Removal Reason because note is also empty!');
} else {
reasonWarn.push('Will add Removal Reason but only with note.');
}
this.logger.warn(reasonWarn.join(''));
} else {
foundReason = truncate(reason.title);
foundReasonId = reason.id;
removeSummary.push(`Reason: ${truncate(foundReason)} (${foundReasonId})`);
}
}
if(renderedNote !== undefined) {
removeSummary.push(`Note: ${truncate(renderedNote)}`);
}
this.logger.verbose(removeSummary.join(' | '));
if (!dryRun) {
// @ts-ignore
await item.remove({spam: this.spam});
@@ -81,18 +44,6 @@ export class RemoveAction extends Action {
// @ts-ignore
item.removed = true;
}
if(foundReasonId !== undefined || renderedNote !== undefined) {
await this.client.addRemovalReason(item, renderedNote, foundReasonId);
item.mod_reason_by = this.resources.botAccount as string;
if(renderedNote !== undefined) {
item.removal_reason = renderedNote;
}
if(foundReason !== undefined) {
item.mod_reason_title = foundReason;
}
}
await this.resources.resetCacheForItem(item);
touchedEntities.push(item);
}
@@ -100,8 +51,7 @@ export class RemoveAction extends Action {
return {
dryRun,
success: true,
touchedEntities,
result: removeSummary.join(' | ')
touchedEntities
}
}
@@ -116,22 +66,7 @@ export interface RemoveOptions extends Omit<RemoveActionConfig, 'authorIs' | 'it
}
export interface RemoveActionConfig extends ActionConfig {
/** (Optional) Mark Activity as spam */
spam?: boolean
/** (Optional) A mod-readable note added to the removal reason for this Activity. Can use Templating.
*
* This note (and removal reasons) are only visible on New Reddit
* */
note?: string
/** (Optional) The ID of the Removal Reason to use
*
* Removal reasons are only visible on New Reddit
*
* To find IDs for removal reasons check the "Removal Reasons" popup located in the CM dashboard config editor for your subreddit
*
* More info on Removal Reasons: https://mods.reddithelp.com/hc/en-us/articles/360010094892-Removal-Reasons
* */
reasonId?: string
}
/**

View File

@@ -7,7 +7,6 @@ import {ActionProcessResult, RichContent, RuleResult} from "../Common/interfaces
import {RuleResultEntity} from "../Common/Entities/RuleResultEntity";
import {runCheckOptions} from "../Subreddit/Manager";
import {ActionTypes} from "../Common/Infrastructure/Atomic";
import {ActionResultEntity} from "../Common/Entities/ActionResultEntity";
// https://www.reddit.com/dev/api/oauth#POST_api_report
// denotes 100 characters maximum
@@ -26,9 +25,10 @@ export class ReportAction extends Action {
return 'report';
}
async process(item: Comment | Submission, ruleResults: RuleResultEntity[], actionResults: ActionResultEntity[], options: runCheckOptions): Promise<ActionProcessResult> {
async process(item: Comment | Submission, ruleResults: RuleResultEntity[], options: runCheckOptions): Promise<ActionProcessResult> {
const dryRun = this.getRuntimeAwareDryrun(options);
const renderedContent = (await this.renderContent(this.content, item, ruleResults, actionResults) as string);
const content = await this.resources.getContent(this.content, item.subreddit);
const renderedContent = await renderContent(content, item, ruleResults, this.resources.userNotes);
this.logger.verbose(`Contents:\r\n${renderedContent}`);
const truncatedContent = reportTrunc(renderedContent);
const touchedEntities = [];

View File

@@ -1,329 +0,0 @@
import Action, {ActionJson, ActionOptions} from "./index";
import {Comment, SubmitLinkOptions, SubmitSelfPostOptions, VoteableContent} from "snoowrap";
import Submission from "snoowrap/dist/objects/Submission";
import {renderContent} from "../Utils/SnoowrapUtils";
import {ActionProcessResult, Footer, RequiredRichContent, RichContent, RuleResult} from "../Common/interfaces";
import {asComment, asSubmission, parseRedditEntity, parseRedditThingsFromLink, sleep, truncateStringToLength} from "../util";
import {RuleResultEntity} from "../Common/Entities/RuleResultEntity";
import {runCheckOptions} from "../Subreddit/Manager";
import {ActionTarget, ActionTypes, ArbitraryActionTarget} from "../Common/Infrastructure/Atomic";
import {CMError} from "../Utils/Errors";
import {SnoowrapActivity} from "../Common/Infrastructure/Reddit";
import Subreddit from "snoowrap/dist/objects/Subreddit";
import {ActionResultEntity} from "../Common/Entities/ActionResultEntity";
export class SubmissionAction extends Action {
content?: string;
lock: boolean = false;
sticky: boolean = false;
distinguish: boolean = false;
spoiler: boolean = false;
nsfw: boolean = false;
flairId?: string
flairText?: string
url?: string
title: string
footer?: false | string;
targets: ('self' | string)[]
constructor(options: SubmissionActionOptions) {
super(options);
const {
content,
lock = false,
sticky = false,
spoiler = false,
distinguish = false,
nsfw = false,
flairText,
flairId,
footer,
url,
title,
targets = ['self']
} = options;
this.footer = footer;
this.content = content;
this.lock = lock;
this.sticky = sticky;
if(this.sticky) {
this.distinguish = sticky;
} else {
this.distinguish = distinguish;
}
this.spoiler = spoiler;
this.nsfw = nsfw;
this.flairText = flairText;
this.flairId = flairId;
this.url = url;
this.title = title;
if (!Array.isArray(targets)) {
this.targets = [targets];
} else {
this.targets = targets;
}
}
getKind(): ActionTypes {
return 'submission';
}
async process(item: Comment | Submission, ruleResults: RuleResultEntity[], actionResults: ActionResultEntity[], options: runCheckOptions): Promise<ActionProcessResult> {
const dryRun = this.getRuntimeAwareDryrun(options);
const title = await this.renderContent(this.title, item, ruleResults, actionResults) as string;
this.logger.verbose(`Title: ${title}`);
const url = await this.renderContent(this.url, item, ruleResults, actionResults);
this.logger.verbose(`URL: ${url !== undefined ? url : '[No URL]'}`);
const body = await this.renderContent(this.content, item, ruleResults, actionResults);
let renderedContent: string | undefined = undefined;
if(body !== undefined) {
const footer = await this.resources.renderFooter(item, this.footer);
renderedContent = `${body}${footer}`;
this.logger.verbose(`Contents:\r\n${renderedContent.length > 100 ? `\r\n${renderedContent}` : renderedContent}`);
} else {
this.logger.verbose(`Contents: [No Body]`);
}
let allErrors = true;
const targetResults: string[] = [];
const touchedEntities = [];
let submittedOnce = false;
for (const targetVal of this.targets) {
//
if(submittedOnce) {
// delay submissions by 3 seconds (on previous successful call)
// to try to spread out load
await sleep(3000);
}
let target: Subreddit = item.subreddit;
let targetIdentifier = targetVal;
if (targetVal !== 'self') {
const subredditVal = parseRedditEntity(targetVal);
try {
target = await this.resources.getSubreddit(subredditVal.name);
targetIdentifier = `[Subreddit ${target.display_name}]`;
} catch (err: any) {
targetResults.push(`[${targetIdentifier}] error occurred while fetching subreddit: ${err.message}`);
if(!err.logged) {
this.logger.warn(new CMError(`[${targetIdentifier}] error occurred while fetching subreddit`, {cause: err}));
}
continue;
}
}
// TODO check if we can post in subreddit
let modifiers = [];
let post: Submission | undefined;
if (!dryRun) {
let opts: SubmitLinkOptions | SubmitSelfPostOptions;
let type: 'self' | 'link';
const genericOpts = {
title,
subredditName: target.display_name,
nsfw: this.nsfw,
spoiler: this.spoiler,
flairId: this.flairId,
flairText: this.flairText,
};
if(url !== undefined) {
type = 'link';
opts = {
...genericOpts,
url,
};
if(renderedContent !== undefined) {
// @ts-ignore
linkOpts.text = renderedContent;
}
} else {
type = 'self';
opts = {
...genericOpts,
text: renderedContent,
}
}
// @ts-ignore
post = await this.tryPost(type, target, opts);
await this.resources.setRecentSelf(post as Submission);
if(post !== undefined) {
touchedEntities.push(post);
}
}
if (this.lock) {
if (post !== undefined && !post.can_mod_post) {
this.logger.warn(`[${targetIdentifier}] Cannot lock because bot is not a moderator`);
} else {
modifiers.push('Locked');
if (!dryRun && post !== undefined) {
// snoopwrap typing issue, thinks comments can't be locked
// @ts-ignore
await post.lock();
}
}
}
if (this.distinguish) {
if (post !== undefined && !post.can_mod_post) {
this.logger.warn(`[${targetIdentifier}] Cannot Distinguish/Sticky because bot is not a moderator`);
} else {
modifiers.push('Distinguished');
if (this.sticky) {
modifiers.push('Stickied');
}
if (!dryRun && post !== undefined) {
// @ts-ignore
await post.distinguish({sticky: this.sticky});
}
}
}
const modifierStr = modifiers.length === 0 ? '' : ` == ${modifiers.join(' | ')} == =>`;
const targetSummary = `${targetIdentifier} ${modifierStr} created Submission ${dryRun ? 'DRYRUN' : (post as SnoowrapActivity).name}`;
// @ts-ignore
targetResults.push(targetSummary)
this.logger.verbose(targetSummary);
allErrors = false;
}
return {
dryRun,
success: !allErrors,
result: `${targetResults.join('\n')}${this.url !== undefined ? `\nURL: ${this.url}` : ''}${body !== undefined ? truncateStringToLength(100)(body) : ''}`,
touchedEntities,
data: {
body,
bodyShort: body !== undefined ? truncateStringToLength(100)(body) : '',
submissions: targetResults.map(x => `* ${x}`).join('\n')
}
};
}
// @ts-ignore
protected async tryPost(type: 'self' | 'link', target: Subreddit, data: SubmitLinkOptions | SubmitSelfPostOptions, maxAttempts = 2): Promise<Submission> {
let post: Submission | undefined;
let error: any;
for (let i = 0; i <= maxAttempts; i++) {
try {
if (type === 'self') {
// @ts-ignore
post = await target.submitSelfpost(data as SubmitSelfPostOptions);
} else {
// @ts-ignore
post = await target.submitLink(data as SubmitLinkOptions);
}
break;
} catch (e: any) {
if (e.message.includes('RATELIMIT')) {
// Looks like you've been doing that a lot. Take a break for 5 seconds before trying again
await sleep(5000);
error = e;
} else {
throw e;
}
}
}
if (error !== undefined) {
throw error;
}
// @ts-ignore
return post;
}
protected getSpecificPremise(): object {
return {
content: this.content,
lock: this.lock,
sticky: this.sticky,
spoiler: this.spoiler,
distinguish: this.distinguish,
nsfw: this.nsfw,
flairId: this.flairId,
flairText: this.flairText,
url: this.url,
text: this.content !== undefined ? truncateStringToLength(50)(this.content) : undefined,
footer: this.footer,
targets: this.targets,
}
}
}
export interface SubmissionActionConfig extends RichContent, Footer {
/**
* Lock the Submission after creation?
* */
lock?: boolean,
/**
* Sticky the Submission after creation?
* */
sticky?: boolean,
nsfw?: boolean
spoiler?: boolean
/**
* The title of this Submission.
*
* Templated the same as **content**
* */
title: string
/**
* If Submission should be a Link, the URL to use
*
* Templated the same as **content**
*
* PROTIP: To make a Link Submission pointing to the Activity being processed use `{{item.permalink}}` as the URL value
* */
url?: string
/**
* Flair template to apply to this Submission
* */
flairId?: string
/**
* Flair text to apply to this Submission
* */
flairText?: string
/**
* Distinguish as Mod after creation?
* */
distinguish?: boolean
/**
* Specify where this Submission should be made
*
* Valid values: 'self' | [subreddit]
*
* * 'self' -- DEFAULT. Post Submission to same subreddit of Activity being processed
* * [subreddit] -- The name of a subreddit to post Submission to. EX mealtimevideos
* */
targets?: ('self' | string) | ('self' | string)[]
}
export interface SubmissionActionOptions extends SubmissionActionConfig, ActionOptions {
}
/**
* Reply to the Activity. For a submission the reply will be a top-level comment.
* */
export interface SubmissionActionJson extends SubmissionActionConfig, ActionJson {
kind: 'submission'
}

View File

@@ -6,7 +6,6 @@ import Comment from 'snoowrap/dist/objects/Comment';
import {RuleResultEntity} from "../../Common/Entities/RuleResultEntity";
import {runCheckOptions} from "../../Subreddit/Manager";
import {ActionTypes} from "../../Common/Infrastructure/Atomic";
import {ActionResultEntity} from "../../Common/Entities/ActionResultEntity";
export class FlairAction extends Action {
text: string;
@@ -27,7 +26,7 @@ export class FlairAction extends Action {
return 'flair';
}
async process(item: Comment | Submission, ruleResults: RuleResultEntity[], actionResults: ActionResultEntity[], options: runCheckOptions): Promise<ActionProcessResult> {
async process(item: Comment | Submission, ruleResults: RuleResultEntity[], options: runCheckOptions): Promise<ActionProcessResult> {
const dryRun = this.getRuntimeAwareDryrun(options);
let flairParts = [];
if(this.text !== '') {

View File

@@ -4,7 +4,6 @@ import {ActionProcessResult, RuleResult} from '../Common/interfaces';
import {RuleResultEntity} from "../Common/Entities/RuleResultEntity";
import {runCheckOptions} from "../Subreddit/Manager";
import {ActionTypes} from "../Common/Infrastructure/Atomic";
import {ActionResultEntity} from "../Common/Entities/ActionResultEntity";
export class UserFlairAction extends Action {
text?: string;
@@ -23,7 +22,7 @@ export class UserFlairAction extends Action {
return 'userflair';
}
async process(item: Comment | Submission, ruleResults: RuleResultEntity[], actionResults: ActionResultEntity[], options: runCheckOptions): Promise<ActionProcessResult> {
async process(item: Comment | Submission, ruleResults: RuleResultEntity[], options: runCheckOptions): Promise<ActionProcessResult> {
const dryRun = this.getRuntimeAwareDryrun(options);
let flairParts = [];

View File

@@ -7,13 +7,12 @@ import Submission from "snoowrap/dist/objects/Submission";
import {ActionProcessResult, RuleResult} from "../Common/interfaces";
import {RuleResultEntity} from "../Common/Entities/RuleResultEntity";
import {runCheckOptions} from "../Subreddit/Manager";
import {ActionTypes, UserNoteType} from "../Common/Infrastructure/Atomic";
import {ActionResultEntity} from "../Common/Entities/ActionResultEntity";
import {ActionTypes} from "../Common/Infrastructure/Atomic";
export class UserNoteAction extends Action {
content: string;
type: UserNoteType;
type: string;
allowDuplicate: boolean;
constructor(options: UserNoteActionOptions) {
@@ -28,9 +27,10 @@ export class UserNoteAction extends Action {
return 'usernote';
}
async process(item: Comment | Submission, ruleResults: RuleResultEntity[], actionResults: ActionResultEntity[], options: runCheckOptions): Promise<ActionProcessResult> {
async process(item: Comment | Submission, ruleResults: RuleResultEntity[], options: runCheckOptions): Promise<ActionProcessResult> {
const dryRun = this.getRuntimeAwareDryrun(options);
const renderedContent = (await this.renderContent(this.content, item, ruleResults, actionResults) as string);
const content = await this.resources.getContent(this.content, item.subreddit);
const renderedContent = await renderContent(content, item, ruleResults, this.resources.userNotes);
this.logger.verbose(`Note:\r\n(${this.type}) ${renderedContent}`);
if (!this.allowDuplicate) {

View File

@@ -19,8 +19,6 @@ import {ActionResultEntity} from "../Common/Entities/ActionResultEntity";
import {FindOptionsWhere} from "typeorm/find-options/FindOptionsWhere";
import {ActionTypes} from "../Common/Infrastructure/Atomic";
import {RunnableBaseJson, RunnableBaseOptions, StructuredRunnableBase} from "../Common/Infrastructure/Runnable";
import { SubredditResources } from "../Subreddit/SubredditResources";
import {SnoowrapActivity} from "../Common/Infrastructure/Reddit";
export abstract class Action extends RunnableBase {
name?: string;
@@ -31,8 +29,6 @@ export abstract class Action extends RunnableBase {
managerEmitter: EventEmitter;
// actionEntity: ActionEntity | null = null;
actionPremiseEntity: ActionPremise | null = null;
checkName: string;
subredditName: string;
constructor(options: ActionOptions) {
super(options);
@@ -44,7 +40,6 @@ export abstract class Action extends RunnableBase {
subredditName,
dryRun = false,
emitter,
checkName,
} = options;
this.name = name;
@@ -53,8 +48,6 @@ export abstract class Action extends RunnableBase {
this.client = client;
this.logger = logger.child({labels: [`Action ${this.getActionUniqueName()}`]}, mergeArr);
this.managerEmitter = emitter;
this.checkName = checkName;
this.subredditName = subredditName;
}
abstract getKind(): ActionTypes;
@@ -119,7 +112,7 @@ export abstract class Action extends RunnableBase {
}
}
async handle(item: Comment | Submission, ruleResults: RuleResultEntity[], actionResults: ActionResultEntity[], options: runCheckOptions): Promise<ActionResultEntity> {
async handle(item: Comment | Submission, ruleResults: RuleResultEntity[], options: runCheckOptions): Promise<ActionResultEntity> {
const {dryRun: runtimeDryrun} = options;
const dryRun = runtimeDryrun || this.dryRun;
@@ -155,11 +148,10 @@ export abstract class Action extends RunnableBase {
actRes.runReason = runReason;
return actRes;
}
const results = await this.process(item, ruleResults, actionResults, options);
const results = await this.process(item, ruleResults, options);
actRes.success = results.success;
actRes.dryRun = results.dryRun;
actRes.result = results.result;
actRes.data = results.data;
actRes.touchedEntities = results.touchedEntities ?? [];
return actRes;
@@ -174,31 +166,20 @@ export abstract class Action extends RunnableBase {
}
}
abstract process(item: Comment | Submission, ruleResults: RuleResultEntity[], actionResults: ActionResultEntity[], options: runCheckOptions): Promise<ActionProcessResult>;
abstract process(item: Comment | Submission, ruleResults: RuleResultEntity[], options: runCheckOptions): Promise<ActionProcessResult>;
getRuntimeAwareDryrun(options: runCheckOptions): boolean {
const {dryRun: runtimeDryrun} = options;
return runtimeDryrun || this.dryRun;
}
async renderContent(template: string | undefined, item: SnoowrapActivity, ruleResults: RuleResultEntity[], actionResults: ActionResultEntity[]): Promise<string | undefined> {
if(template === undefined) {
return undefined;
}
return await this.resources.renderContent(template, item, ruleResults, actionResults, {manager: this.subredditName, check: this.checkName});
}
}
export interface ActionRuntimeOptions {
checkName: string
subredditName: string
export interface ActionOptions extends Omit<ActionConfig, 'authorIs' | 'itemIs'>, RunnableBaseOptions {
//logger: Logger;
subredditName: string;
//resources: SubredditResources;
client: ExtendedSnoowrap;
emitter: EventEmitter;
resources: SubredditResources;
logger: Logger;
}
export interface ActionOptions extends Omit<ActionConfig, 'authorIs' | 'itemIs'>, RunnableBaseOptions, ActionRuntimeOptions {
emitter: EventEmitter
}
export interface ActionConfig extends RunnableBaseJson {

View File

@@ -4,27 +4,24 @@ import {getLogger} from "./Utils/loggerFactory";
import {DatabaseMigrationOptions, OperatorConfig, OperatorConfigWithFileContext, OperatorFileConfig} from "./Common/interfaces";
import Bot from "./Bot";
import LoggedError from "./Utils/LoggedError";
import {generateRandomName, mergeArr, sleep} from "./util";
import {copyFile, open} from "fs/promises";
import {mergeArr, sleep} from "./util";
import {copyFile} from "fs/promises";
import {constants} from "fs";
import {Connection, DataSource, Repository} from "typeorm";
import {Connection} from "typeorm";
import {ErrorWithCause} from "pony-cause";
import {MigrationService} from "./Common/MigrationService";
import {Invokee} from "./Common/Infrastructure/Atomic";
import {DatabaseConfig} from "./Common/Infrastructure/Database";
import {InviteData} from "./Web/Common/interfaces";
import {BotInvite} from "./Common/Entities/BotInvite";
export class App {
bots: Bot[] = [];
logger: Logger;
dbLogger: Logger;
database: DataSource
database: Connection
startedAt: Dayjs = dayjs();
ranMigrations: boolean = false;
migrationBlocker?: string;
friendly?: string;
config: OperatorConfig;
@@ -33,7 +30,6 @@ export class App {
fileConfig: OperatorFileConfig;
migrationService: MigrationService;
inviteRepo: Repository<BotInvite>;
constructor(config: OperatorConfigWithFileContext) {
const {
@@ -53,8 +49,6 @@ export class App {
this.logger = getLogger(config.logging);
this.dbLogger = this.logger.child({labels: ['Database']}, mergeArr);
this.database = database;
this.inviteRepo = this.database.getRepository(BotInvite);
this.friendly = this.config.api.friendly;
this.logger.info(`Operators: ${name.length === 0 ? 'None Specified' : name.join(', ')}`)
@@ -120,8 +114,6 @@ export class App {
return;
}
await this.checkFriendlyName();
if(this.bots.length > 0) {
this.logger.info('Bots already exist, will stop and destroy these before building new ones.');
await this.destroy(causedBy);
@@ -141,7 +133,7 @@ export class App {
for (const b of this.bots) {
if (b.error === undefined) {
try {
await b.init();
await b.testClient();
await b.buildManagers();
await sleep(2000);
b.runManagers(causedBy).catch((err) => {
@@ -169,54 +161,4 @@ export class App {
await b.destroy(causedBy);
}
}
async checkFriendlyName() {
if(this.friendly === undefined) {
let randFriendly: string = generateRandomName();
this.logger.verbose(`No friendly name set for Server. Generated: ${randFriendly}`);
const exists = async (name: string) => {
const existing = await this.inviteRepo.findBy({instance: name});
return existing.length > 0;
}
while (await exists(randFriendly)) {
let oldFriendly = randFriendly;
randFriendly = generateRandomName();
this.logger.verbose(`${oldFriendly} already exists! Generated: ${randFriendly}`);
}
this.friendly = randFriendly;
this.fileConfig.document.setFriendlyName(this.friendly);
const handle = await open(this.fileConfig.document.location as string, 'w');
await handle.writeFile(this.fileConfig.document.toString());
await handle.close();
this.logger.verbose(`Wrote ${randFriendly} as friendly server name to config.`);
}
}
async getInviteById(id: string): Promise<BotInvite | undefined> {
const invite = await this.inviteRepo.findOne({where: {id, instance: this.friendly}});
if(invite === null) {
return undefined;
}
return invite;
}
async getInviteIds(): Promise<string[]> {
if(!this.ranMigrations) {
// not ready!
return [];
}
const invites = await this.inviteRepo.findBy({instance: this.friendly});
return invites.map(x => x.id);
}
async addInvite(data: InviteData): Promise<InviteData> {
return await this.inviteRepo.save(new BotInvite(data));
}
async deleteInvite(id: string): Promise<void> {
await this.inviteRepo.delete({ id });
}
}

View File

@@ -1,5 +1,4 @@
import Snoowrap, {Comment, ConfigOptions, RedditUser, Submission} from "snoowrap";
import {Subreddit} from "snoowrap/dist/objects"
import Snoowrap, {Comment, ConfigOptions, RedditUser, Submission, Subreddit} from "snoowrap";
import {Logger} from "winston";
import dayjs, {Dayjs} from "dayjs";
import {Duration} from "dayjs/plugin/duration";
@@ -14,13 +13,13 @@ import {
USER
} from "../Common/interfaces";
import {
createRetryHandler, symmetricalDifference,
createRetryHandler, difference,
formatNumber, getExceptionMessage, getUserAgent,
mergeArr,
parseBool,
parseDuration, parseMatchMessage, parseRedditEntity,
parseSubredditName, partition, RetryOptions,
sleep, intersect
parseSubredditName, RetryOptions,
sleep
} from "../util";
import {Manager} from "../Subreddit/Manager";
import {ExtendedSnoowrap, ProxiedSnoowrap} from "../Utils/SnoowrapClients";
@@ -28,15 +27,7 @@ import {CommentStream, ModQueueStream, SPoll, SubmissionStream, UnmoderatedStrea
import {BotResourcesManager} from "../Subreddit/SubredditResources";
import LoggedError from "../Utils/LoggedError";
import pEvent from "p-event";
import {
SimpleError,
isRateLimitError,
isRequestError,
isScopeError,
isStatusError,
CMError,
ISeriousError, definesSeriousError
} from "../Utils/Errors";
import {SimpleError, isRateLimitError, isRequestError, isScopeError, isStatusError, CMError} from "../Utils/Errors";
import {ErrorWithCause} from "pony-cause";
import {DataSource, Repository} from "typeorm";
import {Bot as BotEntity} from '../Common/Entities/Bot';
@@ -50,34 +41,19 @@ import {ManagerRunState} from "../Common/Entities/EntityRunState/ManagerRunState
import {Invokee, PollOn} from "../Common/Infrastructure/Atomic";
import {FilterCriteriaDefaults} from "../Common/Infrastructure/Filters/FilterShapes";
import {snooLogWrapper} from "../Utils/loggerFactory";
import {InfluxClient} from "../Common/Influx/InfluxClient";
import {Point} from "@influxdata/influxdb-client";
import {
BotInstanceFunctions, HydratedSubredditInviteData,
NormalizedManagerResponse,
SubredditInviteData,
SubredditInviteDataPersisted, SubredditOnboardingReadiness
} from "../Web/Common/interfaces";
import {AuthorEntity} from "../Common/Entities/AuthorEntity";
import {Guest, GuestEntityData} from "../Common/Entities/Guest/GuestInterfaces";
import {guestEntitiesToAll, guestEntityToApiGuest} from "../Common/Entities/Guest/GuestEntity";
import {SubredditInvite} from "../Common/Entities/SubredditInvite";
import {dayjsDTFormat} from "../Common/defaults";
class Bot implements BotInstanceFunctions {
class Bot {
client!: ExtendedSnoowrap;
logger!: Logger;
logs: LogInfo[] = [];
wikiLocation: string;
dryRun?: true | undefined;
inited: boolean = false;
running: boolean = false;
subreddits: string[];
excludeSubreddits: string[];
filterCriteriaDefaults?: FilterCriteriaDefaults
subManagers: Manager[] = [];
moderatedSubreddits: Subreddit[] = []
heartbeatInterval: number;
nextHeartbeat: Dayjs = dayjs();
heartBeating: boolean = false;
@@ -95,7 +71,6 @@ class Bot implements BotInstanceFunctions {
botName?: string;
botLink?: string;
botAccount?: string;
botUser?: RedditUser;
maxWorkers: number;
startedAt: Dayjs = dayjs();
sharedStreams: PollOn[] = [];
@@ -115,15 +90,9 @@ class Bot implements BotInstanceFunctions {
config: BotInstanceConfig;
influxClients: InfluxClient[] = [];
database: DataSource
invokeeRepo: Repository<InvokeeType>;
runTypeRepo: Repository<RunStateType>;
managerRepo: Repository<ManagerEntity>;
authorRepo: Repository<AuthorEntity>;
subredditInviteRepo: Repository<SubredditInvite>
botRepo: Repository<BotEntity>
botEntity!: BotEntity
getBotName = () => {
@@ -185,10 +154,6 @@ class Bot implements BotInstanceFunctions {
this.database = database;
this.invokeeRepo = this.database.getRepository(InvokeeType);
this.runTypeRepo = this.database.getRepository(RunStateType);
this.managerRepo = this.database.getRepository(ManagerEntity);
this.authorRepo = this.database.getRepository(AuthorEntity);
this.subredditInviteRepo = this.database.getRepository(SubredditInvite)
this.botRepo = this.database.getRepository(BotEntity)
this.config = config;
this.dryRun = parseBool(dryRun) === true ? true : undefined;
this.softLimit = softLimit;
@@ -212,11 +177,8 @@ class Bot implements BotInstanceFunctions {
this.logger.stream().on('log', (log: LogInfo) => {
if(log.bot !== undefined && log.bot === this.getBotName() && log.subreddit === undefined) {
this.logs.unshift(log);
if(this.logs.length > 300) {
// remove all elements starting from the 300th index (301st item)
this.logs.splice(300);
}
const combinedLogs = [log, ...this.logs];
this.logs = combinedLogs.slice(0, 301);
}
});
@@ -349,22 +311,33 @@ class Bot implements BotInstanceFunctions {
}
}
async init() {
if(this.inited) {
return;
}
let user: RedditUser;
async testClient(initial = true) {
try {
user = await this.testClient();
} catch(err: any) {
this.logger.error('An error occurred while trying to initialize the Reddit API Client which would prevent the Bot from running.');
throw err;
// @ts-ignore
await this.client.getMe();
this.logger.info('Test API call successful');
} 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.');
}
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;
}
}
async buildManagers(subreddits: string[] = []) {
let availSubs = [];
// @ts-ignore
const user = await this.client.getMe().fetch();
this.cacheManager.botName = user.name;
this.botUser = user;
this.botLink = `https://reddit.com/user/${user.name}`;
this.botAccount = `u/${user.name}`;
this.logger.info(`Reddit API Limit Remaining: ${this.client.ratelimitRemaining}`);
@@ -391,87 +364,35 @@ class Bot implements BotInstanceFunctions {
this.botEntity = b;
}
if(this.config.opInflux !== undefined) {
this.influxClients.push(this.config.opInflux.childClient(this.logger, {bot: user.name}));
if(this.config.influxConfig !== undefined) {
const iClient = new InfluxClient(this.config.influxConfig, this.logger, {bot: user.name});
await iClient.isReady();
this.influxClients.push(iClient);
}
}
this.inited = true;
}
// @ts-ignore
async testClient(initial = true) {
try {
// @ts-ignore
const user = await this.client.getMe().fetch();
this.logger.info('Test API call successful');
return user;
} 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.');
}
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;
}
}
async getModeratedSubreddits(refresh = false) {
if(this.moderatedSubreddits.length > 0 && !refresh) {
return this.moderatedSubreddits;
}
let subListing = await this.client.getModeratedSubreddits({count: 100});
while (!subListing.isFinished) {
while(!subListing.isFinished) {
subListing = await subListing.fetchMore({amount: 100});
}
const availSubs = subListing.filter(x => x.display_name !== `u_${this.botUser?.name}`);
this.moderatedSubreddits = availSubs;
return availSubs;
}
availSubs = subListing.filter(x => x.display_name !== `u_${user.name}`);
async buildManagers(subreddits: string[] = []) {
await this.init();
this.logger.verbose('Syncing subreddits to moderate with managers...');
const availSubs = await this.getModeratedSubreddits(true);
this.logger.verbose(`${this.botAccount} is a moderator of these subreddits: ${availSubs.map(x => x.display_name_prefixed).join(', ')}`);
this.logger.info(`u/${user.name} is a moderator of these subreddits: ${availSubs.map(x => x.display_name_prefixed).join(', ')}`);
let subsToRun: Subreddit[] = [];
const subsToUse = subreddits.length > 0 ? subreddits.map(parseSubredditName) : this.subreddits;
if (subsToUse.length > 0) {
this.logger.info(`Operator-specified subreddit constraints detected, will only use these: ${subsToUse.join(', ')}`);
const availSubsCI = availSubs.map(x => x.display_name.toLowerCase());
const [foundSubs, notFoundSubs] = partition(subsToUse, (aSub) => availSubsCI.includes(aSub.toLowerCase()));
if(notFoundSubs.length > 0) {
this.logger.warn(`Will not run some operator-specified subreddits because they are not modded by, or do not have appropriate mod permissions for, this bot: ${notFoundSubs.join(', ')}`);
}
for (const sub of foundSubs) {
this.logger.info(`Operator-defined subreddit constraints detected (CLI argument or environmental variable), will try to run on: ${subsToUse.join(', ')}`);
for (const sub of subsToUse) {
const asub = availSubs.find(x => x.display_name.toLowerCase() === sub.toLowerCase())
subsToRun.push(asub as Subreddit);
if (asub === undefined) {
this.logger.warn(`Will not run on ${sub} because is not modded by, or does not have appropriate permissions to mod with, for this client.`);
} else {
// @ts-ignore
const fetchedSub = await asub.fetch();
subsToRun.push(fetchedSub);
}
}
} else {
if(this.excludeSubreddits.length > 0) {
this.logger.info(`Will run on all moderated subreddits EXCEPT own profile and operator-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 operator-defined subreddit constraints detected, will run on all moderated subreddits EXCEPT own profile (${this.botAccount})`);
this.logger.info(`No user-defined subreddit constraints detected, will run on all moderated subreddits EXCEPT own profile (${this.botAccount})`);
subsToRun = availSubs;
}
}
@@ -494,66 +415,30 @@ class Bot implements BotInstanceFunctions {
return acc;
}
}, []);
const notMatched = symmetricalDifference(normalizedOverrideNames, subsToRunNames);
const notMatched = difference(normalizedOverrideNames, subsToRunNames);
if(notMatched.length > 0) {
this.logger.warn(`There are overrides defined for subreddits the bot is not running. Check your spelling! Overrides not matched: ${notMatched.join(', ')}`);
}
}
let subManagersChanged = false;
const subsToRunNames = subsToRun.map(x => x.display_name.toLowerCase());
// first stop and remove any managers with subreddits not in subsToRun
// -- this covers scenario where bot is running and mods of a subreddit de-mod the bot
// -- or where the include/exclude subs list changed from operator (not yet implemented)
if(this.subManagers.length > 0) {
let index = 0;
for(const manager of this.subManagers) {
if(!subsToRunNames.includes(manager.subreddit.display_name.toLowerCase())) {
subManagersChanged = true;
// determine if bot was de-modded
const deModded = !availSubs.some(x => x.display_name.toLowerCase() === manager.subreddit.display_name.toLowerCase());
this.logger.warn(`Stopping and removing manager for ${manager.subreddit.display_name.toLowerCase()} because it is ${deModded ? 'no longer moderated by this bot' : 'not in the list of subreddits to moderate'}`);
await manager.destroy('system', {reason: deModded ? 'No longer moderated by this bot' : 'Subreddit is not in moderated list'});
this.subManagers.splice(index, 1);
}
index++;
}
}
// then create any managers that don't already exist
// -- covers init scenario
// -- and in-situ adding subreddits IE bot is modded to a new subreddit while CM is running
const subsToInit: string[] = [];
// get configs for subs we want to run on and build/validate them
for (const sub of subsToRun) {
if(!this.subManagers.some(x => x.subreddit.display_name === sub.display_name)) {
subManagersChanged = true;
this.logger.info(`Manager for ${sub.display_name_prefixed} not found in loaded managers. Loading now...`);
subsToInit.push(sub.display_name);
try {
this.subManagers.push(await this.createManager(sub));
} catch (err: any) {
try {
this.subManagers.push(await this.createManager(sub));
} catch (err: any) {
}
}
}
for(const subName of subsToInit) {
for(const m of this.subManagers) {
try {
const m = this.subManagers.find(x => x.subreddit.display_name === subName);
await this.initManager(m as Manager);
await this.initManager(m);
} catch (err: any) {
}
}
if(!subManagersChanged) {
this.logger.verbose('All managers were already synced!');
} else {
this.parseSharedStreams();
}
return subManagersChanged;
this.parseSharedStreams();
}
parseSharedStreams() {
@@ -665,7 +550,7 @@ class Bot implements BotInstanceFunctions {
await manager.parseConfiguration('system', true, {suppressNotification: true, suppressChangeEvent: true});
} catch (err: any) {
if(err.logged !== true) {
const normalizedError = new ErrorWithCause(`Bot could not initialize manager`, {cause: err});
const normalizedError = new ErrorWithCause(`Bot could not initialize manager because config was not valid`, {cause: err});
// @ts-ignore
this.logger.error(normalizedError, {subreddit: manager.subreddit.display_name_prefixed});
} else {
@@ -674,7 +559,7 @@ class Bot implements BotInstanceFunctions {
}
}
async createManager(subVal: Subreddit): Promise<Manager> {
async createManager(sub: Subreddit): Promise<Manager> {
const {
flowControlDefaults: {
maxGotoDepth: botMaxDefault
@@ -685,15 +570,6 @@ class Bot implements BotInstanceFunctions {
} = {}
} = this.config;
let sub = subVal;
// make sure the subreddit is fully fetched
// @ts-ignore
if(subVal._hasFetched === false) {
// @ts-ignore
sub = await subVal.fetch();
}
const override = overrides.find(x => {
const configName = parseRedditEntity(x.name).name;
if(configName !== undefined) {
@@ -710,15 +586,15 @@ class Bot implements BotInstanceFunctions {
databaseConfig: {
retention = undefined
} = {},
wikiConfig = this.wikiLocation,
} = override || {};
const managerRepo = this.database.getRepository(ManagerEntity);
const subRepo = this.database.getRepository(SubredditEntity)
let subreddit = await subRepo.findOne({where: {id: sub.name}});
if(subreddit === null) {
subreddit = await subRepo.save(new SubredditEntity({id: sub.name, name: sub.display_name}))
}
let managerEntity = await this.managerRepo.findOne({
let managerEntity = await managerRepo.findOne({
where: {
bot: {
id: this.botEntity.id
@@ -727,15 +603,12 @@ class Bot implements BotInstanceFunctions {
id: subreddit.id
}
},
relations: {
guests: true
}
});
if(managerEntity === undefined || managerEntity === null) {
const invokee = await this.invokeeRepo.findOneBy({name: SYSTEM}) as InvokeeType;
const runType = await this.runTypeRepo.findOneBy({name: STOPPED}) as RunStateType;
managerEntity = await this.managerRepo.save(new ManagerEntity({
managerEntity = await managerRepo.save(new ManagerEntity({
name: sub.display_name,
bot: this.botEntity,
subreddit: subreddit as SubredditEntity,
@@ -743,15 +616,12 @@ class Bot implements BotInstanceFunctions {
eventsState: new EventsRunState({invokee, runType}),
managerState: new ManagerRunState({invokee, runType})
}));
this.logger.info(`Created new Manager (${managerEntity.id}) for ${subVal.display_name}`);
} else {
this.logger.info(`Found existing Manager (${managerEntity.id}) for ${subVal.display_name}`);
}
const manager = new Manager(sub, this.client, this.logger, this.cacheManager, {
dryRun: this.dryRun,
sharedStreams: this.sharedStreams,
wikiLocation: wikiConfig,
wikiLocation: this.wikiLocation,
botName: this.botName as string,
maxWorkers: this.maxWorkers,
filterCriteriaDefaults: this.filterCriteriaDefaults,
@@ -760,7 +630,6 @@ class Bot implements BotInstanceFunctions {
managerEntity: managerEntity as ManagerEntity,
statDefaults: (statDefaultsFromOverride ?? databaseStatisticsDefaults) as DatabaseStatisticsOperatorConfig,
retention,
influxClients: this.influxClients,
});
// all errors from managers will count towards bot-level retry count
manager.on('error', async (err) => await this.panicOnRetries(err));
@@ -793,50 +662,36 @@ class Bot implements BotInstanceFunctions {
}
async checkModInvites() {
this.logger.debug('Checking onboarding invites...');
const expired = this.botEntity.getSubredditInvites().filter(x => x.expiresAt !== undefined && x.expiresAt.isSameOrBefore(dayjs()));
for (const exp of expired) {
this.logger.debug(`Onboarding invite for ${exp.subreddit} expired at ${exp.expiresAt?.format(dayjsDTFormat)}`);
await this.deleteSubredditInvite(exp);
}
for (const subInvite of this.botEntity.getSubredditInvites()) {
if (subInvite.canAutomaticallyAccept()) {
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 {
await this.acceptModInvite(subInvite);
await this.deleteSubredditInvite(subInvite);
const manager = await 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(definesSeriousError(err) && !err.isSerious) {
this.logger.warn(err);
} else {
if (!(err instanceof LoggedError)) {
this.logger.error(err);
}
}
} else {
this.logger.debug(`Cannot try to automatically accept mod invite for ${subInvite.subreddit} because it has additional settings that require moderator approval`);
}
}
}
async acceptModInvite(invite: SubredditInvite) {
const {subreddit: name} = invite;
try {
// @ts-ignore
await this.client.getSubreddit(name).acceptModeratorInvite();
this.logger.info(`Accepted moderator invite for r/${name}!`);
} catch (err: any) {
if (err.message.includes('NO_INVITE_FOUND')) {
throw new SimpleError(`No pending moderation invite for r/${name} was found`, {isSerious: false});
} else if (isStatusError(err) && err.statusCode === 403) {
let msg = `Error occurred while checking r/${name} for a pending moderation invite.`;
if(!this.client.scope.includes('modself')) {
msg = `${msg} This bot must have the 'modself' oauth permission in order to accept invites.`;
} 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 {
msg = `${msg} If this subreddit is private it is likely no moderation invite exists.`;
this.logger.error(`Error occurred while checking r/${name} for a pending moderation invite. Error: ${err.message}`);
}
throw new CMError(msg, {cause: err})
} else {
throw new CMError(`Error occurred while checking r/${name} for a pending moderation invite.`, {cause: err});
}
}
}
@@ -887,15 +742,9 @@ class Bot implements BotInstanceFunctions {
async healthLoop() {
while (this.running) {
await sleep(5000);
const time = dayjs().valueOf()
await this.apiHealthCheck(time);
await this.guestModCleanup();
if (!this.running) {
break;
}
for(const m of this.subManagers) {
await m.writeHealthMetrics(time);
}
const now = dayjs();
if (now.isSameOrAfter(this.nextNannyCheck)) {
try {
@@ -908,17 +757,8 @@ class Bot implements BotInstanceFunctions {
}
if(now.isSameOrAfter(this.nextHeartbeat)) {
try {
// 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`);
}
await this.checkModInvites();
await this.buildManagers();
await this.heartbeat();
await this.checkModInvites();
} catch (err: any) {
this.logger.error(`Error occurred during heartbeat check: ${err.message}`);
}
@@ -930,73 +770,6 @@ class Bot implements BotInstanceFunctions {
this.emitter.emit('healthStopped');
}
getApiUsageSummary() {
const depletion = this.apiEstDepletion === undefined ? 'Not Calculated' : this.apiEstDepletion.humanize();
return`API Usage Rolling Avg: ${formatNumber(this.apiRollingAvg)}/s | Est Depletion: ${depletion} (${formatNumber(this.depletedInSecs, {toFixed: 0})} seconds)`;
}
async apiHealthCheck(time?: number) {
const rollingSample = this.apiSample.slice(0, 7)
rollingSample.unshift(this.client.ratelimitRemaining);
this.apiSample = rollingSample;
const diff = this.apiSample.reduceRight((acc: number[], curr, index) => {
if (this.apiSample[index + 1] !== undefined) {
const d = Math.abs(curr - this.apiSample[index + 1]);
if (d === 0) {
return [...acc, 0];
}
return [...acc, d / 10];
}
return acc;
}, []);
const diffTotal = diff.reduce((acc, curr) => acc + curr, 0);
if(diffTotal === 0 || diff.length === 0) {
this.apiRollingAvg = 0;
} else {
this.apiRollingAvg = diffTotal / diff.length; // api requests per second
}
this.depletedInSecs = this.apiRollingAvg === 0 ? Number.POSITIVE_INFINITY : this.client.ratelimitRemaining / this.apiRollingAvg; // number of seconds until current remaining limit is 0
// if depletion/api usage is 0 we need a sane value to use here for both displaying in logs as well as for api nanny. 10 years seems reasonable
this.apiEstDepletion = dayjs.duration((this.depletedInSecs === Number.POSITIVE_INFINITY ? {years: 10} : {seconds: this.depletedInSecs}));
if(this.influxClients.length > 0) {
const apiMeasure = new Point('apiHealth')
.intField('remaining', this.client.ratelimitRemaining)
.stringField('nannyMod', this.nannyMode ?? 'none');
if(time !== undefined) {
apiMeasure.timestamp(time);
}
if(this.apiSample.length > 1) {
const curr = this.apiSample[0];
const last = this.apiSample[1];
if(curr <= last) {
apiMeasure.intField('used', last - curr);
}
}
for(const iclient of this.influxClients) {
await iclient.writePoint(apiMeasure);
}
}
}
async guestModCleanup() {
const now = dayjs();
for(const m of this.subManagers) {
const expiredGuests = m.managerEntity.getGuests().filter(x => x.expiresAt.isBefore(now));
if(expiredGuests.length > 0) {
m.managerEntity.removeGuestById(expiredGuests.map(x => x.id));
m.logger.info(`Removed expired Guest Mods: ${expiredGuests.map(x => x.author.name).join(', ')}`);
await this.managerRepo.save(m.managerEntity);
}
}
}
async retentionCleanup() {
const now = dayjs();
if(now.isSameOrAfter(this.nextRetentionCheck)) {
@@ -1010,8 +783,15 @@ class Bot implements BotInstanceFunctions {
}
async heartbeat() {
this.logger.info(`HEARTBEAT -- ${this.getApiUsageSummary()}`);
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) {
@@ -1064,7 +844,6 @@ class Bot implements BotInstanceFunctions {
async runApiNanny() {
try {
this.logger.debug(this.getApiUsageSummary());
this.nextExpiration = dayjs(this.client.ratelimitExpiration);
const nowish = dayjs().add(10, 'second');
if (nowish.isAfter(this.nextExpiration)) {
@@ -1088,12 +867,30 @@ class Bot implements BotInstanceFunctions {
}
this.nextExpiration = dayjs(this.client.ratelimitExpiration);
}
const rollingSample = this.apiSample.slice(0, 7)
rollingSample.unshift(this.client.ratelimitRemaining);
this.apiSample = rollingSample;
const diff = this.apiSample.reduceRight((acc: number[], curr, index) => {
if (this.apiSample[index + 1] !== undefined) {
const d = Math.abs(curr - this.apiSample[index + 1]);
if (d === 0) {
return [...acc, 0];
}
return [...acc, d / 10];
}
return acc;
}, []);
this.apiRollingAvg = diff.reduce((acc, curr) => acc + curr, 0) / diff.length; // api requests per second
this.depletedInSecs = this.client.ratelimitRemaining / this.apiRollingAvg; // number of seconds until current remaining limit is 0
this.apiEstDepletion = dayjs.duration({seconds: this.depletedInSecs});
this.logger.debug(`API Usage Rolling Avg: ${formatNumber(this.apiRollingAvg)}/s | Est Depletion: ${this.apiEstDepletion.humanize()} (${formatNumber(this.depletedInSecs, {toFixed: 0})} seconds)`);
let hardLimitHit = false;
if (typeof this.hardLimit === 'string' && this.apiEstDepletion !== undefined) {
if (typeof this.hardLimit === 'string') {
const hardDur = parseDuration(this.hardLimit);
hardLimitHit = hardDur.asSeconds() > this.apiEstDepletion.asSeconds();
} else if(typeof this.hardLimit === 'number') {
} else {
hardLimitHit = this.hardLimit > this.client.ratelimitRemaining;
}
@@ -1102,6 +899,7 @@ class Bot implements BotInstanceFunctions {
return;
}
this.logger.info(`Detected HARD LIMIT of ${this.hardLimit} remaining`, {leaf: 'Api Nanny'});
this.logger.info(`API Remaining: ${this.client.ratelimitRemaining} | Usage Rolling Avg: ${this.apiRollingAvg}/s | Est Depletion: ${this.apiEstDepletion.humanize()} (${formatNumber(this.depletedInSecs, {toFixed: 0})} seconds)`, {leaf: 'Api Nanny'});
this.logger.info(`All subreddit event polling has been paused`, {leaf: 'Api Nanny'});
for (const m of this.subManagers) {
@@ -1118,10 +916,10 @@ class Bot implements BotInstanceFunctions {
}
let softLimitHit = false;
if (typeof this.softLimit === 'string' && this.apiEstDepletion !== undefined) {
if (typeof this.softLimit === 'string') {
const softDur = parseDuration(this.softLimit);
softLimitHit = softDur.asSeconds() > this.apiEstDepletion.asSeconds();
} else if(typeof this.softLimit === 'number') {
} else {
softLimitHit = this.softLimit > this.client.ratelimitRemaining;
}
@@ -1130,6 +928,7 @@ class Bot implements BotInstanceFunctions {
return;
}
this.logger.info(`Detected SOFT LIMIT of ${this.softLimit} remaining`, {leaf: 'Api Nanny'});
this.logger.info(`API Remaining: ${this.client.ratelimitRemaining} | Usage Rolling Avg: ${formatNumber(this.apiRollingAvg)}/s | Est Depletion: ${this.apiEstDepletion.humanize()} (${formatNumber(this.depletedInSecs, {toFixed: 0})} seconds)`, {leaf: 'Api Nanny'});
this.logger.info('Trying to detect heavy usage subreddits...', {leaf: 'Api Nanny'});
let threshold = 0.5;
let offenders = this.subManagers.filter(x => {
@@ -1187,251 +986,6 @@ class Bot implements BotInstanceFunctions {
throw err;
}
}
getManagerNames(): string[] {
return this.subManagers.map(x => x.displayLabel);
}
getSubreddits(normalized = true): string[] {
return normalized ? this.subManagers.map(x => parseRedditEntity(x.subreddit.display_name).name) : this.subManagers.map(x => x.subreddit.display_name);
}
getGuestManagers(user: string): NormalizedManagerResponse[] {
return this.subManagers.filter(x => x.managerEntity.getGuests().map(y => y.author.name).includes(user)).map(x => x.toNormalizedManager());
}
getGuestSubreddits(user: string): string[] {
return this.getGuestManagers(user).map(x => x.subredditNormal);
}
getAccessibleSubreddits(user: string, subreddits: string[] = []): string[] {
const normalSubs = subreddits.map(x => parseRedditEntity(x).name);
const moderatedSubs = intersect(normalSubs, this.getSubreddits());
const guestSubs = this.getGuestSubreddits(user);
return Array.from(new Set([...guestSubs, ...moderatedSubs]));
}
canUserAccessBot(user: string, subreddits: string[] = []) {
return this.getAccessibleSubreddits(user, subreddits).length > 0;
}
canUserAccessSubreddit(subreddit: string, user: string, subreddits: string[] = []): boolean {
return this.getAccessibleSubreddits(user, subreddits).includes(parseRedditEntity(subreddit).name);
}
async addGuest(userVal: string | string[], expiresAt: Dayjs, managerVal?: string | string[]) {
let managerNames: string[];
if(typeof managerVal === 'string') {
managerNames = [managerVal];
} else if(Array.isArray(managerVal)) {
managerNames = managerVal;
} else {
managerNames = this.subManagers.map(x => x.subreddit.display_name);
}
const cleanSubredditNames = managerNames.map(x => parseRedditEntity(x).name);
const userNames = typeof userVal === 'string' ? [userVal] : userVal;
const cleanUsers = userNames.map(x => parseRedditEntity(x.trim(), 'user').name);
const users: AuthorEntity[] = [];
for(const uName of cleanUsers) {
let user = await this.authorRepo.findOne({
where: {
name: uName,
}
});
if(user === null) {
users.push(await this.authorRepo.save(new AuthorEntity({name: uName})));
} else {
users.push(user);
}
}
const newGuestData = users.map(x => ({author: x, expiresAt})) as GuestEntityData[];
let newGuests = new Map<string, Guest[]>();
const updatedManagerEntities: ManagerEntity[] = [];
for(const m of this.subManagers) {
if(!cleanSubredditNames.includes(m.subreddit.display_name)) {
continue;
}
const filteredGuests = m.managerEntity.addGuest(newGuestData);
updatedManagerEntities.push(m.managerEntity);
newGuests.set(m.displayLabel, filteredGuests.map(x => guestEntityToApiGuest(x)));
m.logger.info(`Added ${cleanUsers.join(', ')} as Guest`);
}
await this.managerRepo.save(updatedManagerEntities);
return newGuests;
}
async removeGuest(userVal: string | string[], managerVal?: string | string[]) {
let managerNames: string[];
if(typeof managerVal === 'string') {
managerNames = [managerVal];
} else if(Array.isArray(managerVal)) {
managerNames = managerVal;
} else {
managerNames = this.subManagers.map(x => x.subreddit.display_name);
}
const cleanSubredditNames = managerNames.map(x => parseRedditEntity(x).name);
const userNames = typeof userVal === 'string' ? [userVal] : userVal;
const cleanUsers = userNames.map(x => parseRedditEntity(x.trim(), 'user').name);
let newGuests = new Map<string, Guest[]>();
const updatedManagerEntities: ManagerEntity[] = [];
for(const m of this.subManagers) {
if(!cleanSubredditNames.includes(m.subreddit.display_name)) {
continue;
}
const filteredGuests = m.managerEntity.removeGuestByUser(cleanUsers);
updatedManagerEntities.push(m.managerEntity);
newGuests.set(m.displayLabel, filteredGuests.map(x => guestEntityToApiGuest(x)));
m.logger.info(`Removed ${cleanUsers.join(', ')} from Guests`);
}
await this.managerRepo.save(updatedManagerEntities);
return newGuests;
}
async addSubredditInvite(data: HydratedSubredditInviteData){
let sub: Subreddit;
let name: string;
if (data.subreddit instanceof Subreddit) {
sub = data.subreddit;
name = sub.display_name;
} else {
try {
const maybeName = parseRedditEntity(data.subreddit);
name = maybeName.name;
} catch (e: any) {
throw new SimpleError(`Value '${data.subreddit}' is not a valid subreddit name`);
}
try {
const [exists, foundSub] = await this.client.subredditExists(name);
if (!exists) {
throw new SimpleError(`No subreddit with the name ${name} exists`);
}
if (foundSub !== undefined) {
name = foundSub.display_name;
}
} catch (e: any) {
throw e;
}
}
if((await this.subredditInviteRepo.findOneBy({subreddit: name}))) {
throw new CMError(`Invite for ${name} already exists`);
}
const invite = new SubredditInvite({
subreddit: name,
initialConfig: data.initialConfig,
guests: data.guests,
bot: this.botEntity
})
await this.subredditInviteRepo.save(invite);
this.botEntity.addSubredditInvite(invite);
return invite;
}
getSubredditInvites(): SubredditInviteDataPersisted[] {
if(this.botEntity !== undefined) {
return this.botEntity.getSubredditInvites().map(x => x.toSubredditInviteData());
}
this.logger.warn('No bot entity found');
return [];
}
getInvite(id: string): SubredditInvite | undefined {
if(this.botEntity !== undefined) {
return this.botEntity.getSubredditInvites().find(x => x.id === id);
}
this.logger.warn('No bot entity found');
return undefined;
}
getOnboardingReadiness(invite: SubredditInvite): SubredditOnboardingReadiness {
const hasManager = this.subManagers.some(x => x.subreddit.display_name.toLowerCase() === invite.subreddit.toLowerCase());
const isMod = this.moderatedSubreddits.some(x => x.display_name.toLowerCase() === invite.subreddit.toLowerCase());
return {
hasManager,
isMod
};
}
async finishOnboarding(invite: SubredditInvite) {
const readiness = this.getOnboardingReadiness(invite);
if (readiness.hasManager || readiness.isMod) {
this.logger.info(`Bot is already a mod of ${invite.subreddit}. Finishing onboarding early.`);
await this.deleteSubredditInvite(invite);
}
try {
await this.acceptModInvite(invite);
} catch (e: any) {
throw e;
}
try {
// rebuild managers to get new subreddit
await this.buildManagers();
const manager = this.subManagers.find(x => x.subreddit.display_name.toLowerCase() === invite.subreddit.toLowerCase());
if (manager === undefined) {
throw new CMError('Accepted moderator invitation but could not find manager after rebuilding??');
}
const {guests = [], initialConfig} = invite;
// add guests
if (guests.length > 0) {
await this.addGuest(guests, dayjs().add(1, 'day'), manager.subreddit.display_name);
}
// set initial config
if (initialConfig !== undefined) {
let data: string;
try {
const res = await manager.resources.getExternalResource(initialConfig);
data = res.val;
} catch (e: any) {
throw new CMError(`Accepted moderator invitation but error occurred while trying to fetch config from Initial Config value (${initialConfig})`, {cause: e});
}
try {
await manager.writeConfig(data, 'Generated by Initial Config during onboarding')
} catch (e: any) {
throw new CMError(`Accepted moderator invitation but error occurred while trying to set wiki config value from initial config (${initialConfig})`, {cause: e});
}
// it's ok if this fails because we've already done all the onboarding steps. user can still access the dashboard and all settings have been applied (even if they were invalid IE config)
manager.parseConfiguration('system', true).catch((err: any) => {
if(err.logged !== true) {
this.logger.error(err, {subreddit: manager.displayLabel});
}
})
}
} catch(e: any) {
throw e;
} finally {
await this.deleteSubredditInvite(invite);
}
}
async deleteSubredditInvite(val: string | SubredditInvite) {
let invite: SubredditInvite;
if(val instanceof SubredditInvite) {
invite = val;
} else {
const maybeInvite = this.botEntity.getSubredditInvites().find(x => x.subreddit === val);
if(maybeInvite === undefined) {
throw new CMError(`No invite for subreddit ${val} exists for this Bot`);
}
invite = maybeInvite;
}
await this.subredditInviteRepo.delete({id: invite.id});
this.botEntity.removeSubredditInvite(invite);
}
}
export default Bot;

View File

@@ -1,18 +1,19 @@
import {RuleSet, RuleSetConfigData, RuleSetConfigHydratedData, RuleSetConfigObject} from "../Rule/RuleSet";
import {Rule} from "../Rule";
import Action, {ActionConfig} from "../Action";
import {RuleSet, IRuleSet, RuleSetJson, RuleSetObjectJson, isRuleSetJSON} from "../Rule/RuleSet";
import {IRule, Rule, RuleJSONConfig} from "../Rule";
import Action, {ActionConfig, ActionJson, StructuredActionJson} from "../Action";
import {Logger} from "winston";
import {Comment, Submission} from "snoowrap";
import Snoowrap, {Comment, Submission} from "snoowrap";
import {actionFactory} from "../Action/ActionFactory";
import {ruleFactory} from "../Rule/RuleFactory";
import {
asPostBehaviorOptionConfig,
createAjvFactory,
FAIL,
isRuleSetResult,
boolToString,
createAjvFactory, determineNewResults,
FAIL, isRuleSetResult,
mergeArr,
PASS,
resultsSummary,
ruleNamesFromResults,
truncateStringToLength
} from "../util";
import {
@@ -21,17 +22,19 @@ import {
CheckSummary,
JoinCondition,
NotificationEventPayload,
PostBehavior,
PostBehaviorOptionConfigStrong,
PostBehaviorStrong,
RuleSetResult
PostBehavior, PostBehaviorOptionConfig, PostBehaviorOptionConfigStrong, PostBehaviorStrong,
RuleResult,
RuleSetResult, UserResultCache
} from "../Common/interfaces";
import * as RuleSchema from '../Schema/Rule.json';
import * as RuleSetSchema from '../Schema/RuleSet.json';
import * as ActionSchema from '../Schema/Action.json';
import {SubredditResources} from "../Subreddit/SubredditResources";
import {
ActionJson as ActionTypeJson
} from "../Common/types";
import {SubredditResources} from "../Subreddit/SubredditResources";
import {ExtendedSnoowrap} from '../Utils/SnoowrapClients';
import {ActionProcessingError, CheckProcessingError} from "../Utils/Errors";
import {ActionProcessingError, CheckProcessingError, isRateLimitError} from "../Utils/Errors";
import {ErrorWithCause, stackWithCauses} from "pony-cause";
import {runCheckOptions} from "../Subreddit/Manager";
import EventEmitter from "events";
@@ -44,28 +47,24 @@ import {RunnableBase} from "../Common/RunnableBase";
import {ActionResultEntity} from "../Common/Entities/ActionResultEntity";
import {RuleSetResultEntity} from "../Common/Entities/RuleSetResultEntity";
import {CheckToRuleResultEntity} from "../Common/Entities/RunnableAssociation/CheckToRuleResultEntity";
import {JoinOperands, PostBehaviorType, RecordOutputType, recordOutputTypes} from "../Common/Infrastructure/Atomic";
import {CommentState, SubmissionState,} from "../Common/Infrastructure/Filters/FilterCriteria";
import {
JoinOperands,
PostBehaviorType,
RecordOutputType,
recordOutputTypes
} from "../Common/Infrastructure/Atomic";
import {
MinimalOrFullFilter,
MinimalOrFullFilterJson
} from "../Common/Infrastructure/Filters/FilterShapes";
import {
CommentState,
SubmissionState,
} from "../Common/Infrastructure/Filters/FilterCriteria";
import {ActivityType} from "../Common/Infrastructure/Reddit";
import {
RunnableBaseJson,
RunnableBaseOptions,
StructuredRunnableBase,
TypedRunnableBaseData, TypedStructuredRunnableBase
} from "../Common/Infrastructure/Runnable";
import {
RuleConfigData, RuleConfigHydratedData,
RuleConfigObject,
StructuredRuleConfigObject,
StructuredRuleSetConfigObject
} from "../Common/Infrastructure/RuleShapes";
import {
ActionConfigData,
ActionConfigHydratedData,
ActionConfigObject,
StructuredActionObjectJson
} from "../Common/Infrastructure/ActionShapes";
import {IncludesData} from "../Common/Infrastructure/Includes";
import {RunnableBaseJson, RunnableBaseOptions, StructuredRunnableBase} from "../Common/Infrastructure/Runnable";
import {RuleJson, StructuredRuleObjectJson, StructuredRuleSetObjectJson} from "../Common/Infrastructure/RuleShapes";
import {ActionObjectJson, StructuredActionObjectJson} from "../Common/Infrastructure/ActionShapes";
const checkLogName = truncateStringToLength(25);
@@ -156,7 +155,7 @@ export abstract class Check extends RunnableBase implements Omit<ICheck, 'postTr
if(asPostBehaviorOptionConfig(postFail)) {
const {
behavior = 'next',
recordTo = ['influx']
recordTo = false
} = postFail;
let recordStrong: RecordOutputType[] = [];
if(typeof recordTo === 'boolean') {
@@ -175,7 +174,7 @@ export abstract class Check extends RunnableBase implements Omit<ICheck, 'postTr
} else {
this.postFail = {
behavior: postFail,
recordTo: ['influx']
recordTo: []
}
}
@@ -193,12 +192,12 @@ export abstract class Check extends RunnableBase implements Omit<ICheck, 'postTr
let ruleErrors: any = [];
if (valid) {
const ruleConfig = r;
this.rules.push(new RuleSet({...ruleConfig as StructuredRuleSetConfigObject, logger: this.logger, subredditName, resources: this.resources, client: this.client}));
this.rules.push(new RuleSet({...ruleConfig as StructuredRuleSetObjectJson, logger: this.logger, subredditName, resources: this.resources, client: this.client}));
} else {
setErrors = ajv.errors;
valid = ajv.validate(RuleSchema, r);
if (valid) {
this.rules.push(ruleFactory(r as StructuredRuleConfigObject, this.logger, subredditName, this.resources, this.client));
this.rules.push(ruleFactory(r as StructuredRuleObjectJson, this.logger, subredditName, this.resources, this.client));
} else {
ruleErrors = ajv.errors;
const leastErrorType = setErrors.length < ruleErrors ? 'RuleSet' : 'Rule';
@@ -222,14 +221,7 @@ export abstract class Check extends RunnableBase implements Omit<ICheck, 'postTr
this.actions.push(actionFactory({
...aj,
dryRun: this.dryRun || aj.dryRun
}, {
logger: this.logger,
subredditName,
resources: this.resources,
client: this.client,
emitter: this.emitter,
checkName: this.name
}));
}, this.logger, subredditName, this.resources, this.client, this.emitter));
// @ts-ignore
a.logger = this.logger;
} else {
@@ -442,7 +434,7 @@ export abstract class Check extends RunnableBase implements Omit<ICheck, 'postTr
checkSum.postBehavior = this.postFail.behavior;
}
behaviorT = checkResult.triggered ? 'Trigger' : 'Fail';
behaviorT = checkSum.triggered ? 'Trigger' : 'Fail';
switch (checkSum.postBehavior.toLowerCase()) {
case 'next':
@@ -571,7 +563,7 @@ export abstract class Check extends RunnableBase implements Omit<ICheck, 'postTr
const dr = dryRun || this.dryRun;
this.logger.debug(`${dr ? 'DRYRUN - ' : ''}Running Actions`);
for (const a of this.actions) {
const res = await a.handle(item, ruleResults, runActions, options);
const res = await a.handle(item, ruleResults, options);
runActions.push(res);
}
this.logger.info(`${dr ? 'DRYRUN - ' : ''}Ran Actions: ${runActions.map(x => x.premise.getFriendlyIdentifier()).join(' | ')}`);
@@ -613,7 +605,7 @@ export interface ICheck extends JoinCondition, PostBehavior, RunnableBaseJson {
}
export interface CheckOptions extends Omit<ICheck, 'authorIs' | 'itemIs'>, RunnableBaseOptions {
rules: Array<RuleConfigObject | RuleSetConfigObject>;
rules: Array<StructuredRuleSetObjectJson | StructuredRuleObjectJson>;
actions: ActionConfig[];
logger: Logger;
subredditName: string;
@@ -624,15 +616,7 @@ export interface CheckOptions extends Omit<ICheck, 'authorIs' | 'itemIs'>, Runna
emitter: EventEmitter
}
/*
* Can contain actions/rules as:
* - full objects
* - string to hydrate IE "url:fsdfd"
* - named string IE "namedRule"
*
* Also can contain itemIs/authorIs as full object or named filter
* */
export interface CheckConfigData extends ICheck, RunnableBaseJson {
export interface CheckJson extends ICheck {
/**
* The type of event (new submission or new comment) this check should be run against
* @examples ["submission", "comment"]
@@ -647,7 +631,7 @@ export interface CheckConfigData extends ICheck, RunnableBaseJson {
*
* **If `rules` is an empty array or not present then `actions` are performed immediately.**
* */
rules?: (RuleSetConfigData | RuleConfigData | string | IncludesData)[]
rules?: Array<RuleSetJson | RuleJson>
/**
* The `Actions` to run after the check is successfully triggered. ALL `Actions` will run in the order they are listed
*
@@ -655,7 +639,7 @@ export interface CheckConfigData extends ICheck, RunnableBaseJson {
*
* @examples [[{"kind": "comment", "content": "this is the content of the comment", "distinguish": true}, {"kind": "lock"}]]
* */
actions?: ActionConfigData[]
actions?: Array<ActionTypeJson>
/**
* If notifications are configured and this is `true` then an `eventActioned` event will be sent when this check is triggered.
@@ -667,49 +651,9 @@ export interface CheckConfigData extends ICheck, RunnableBaseJson {
cacheUserResult?: UserResultCacheOptions;
}
export interface SubmissionCheckConfigData extends CheckConfigData, TypedRunnableBaseData<SubmissionState> {
export interface SubmissionCheckJson extends CheckJson {
kind: 'submission'
}
export interface CommentCheckConfigData extends CheckConfigData, TypedRunnableBaseData<CommentState> {
kind: 'comment'
}
/*
* Can contain actions/rules as:
* - full objects
* - named string IE "namedRule"
*
* Also can contain itemIs/authorIs as full object or named filter
* */
export interface CheckConfigHydratedData extends CheckConfigData {
rules?: (RuleSetConfigHydratedData | RuleConfigHydratedData)[]
actions?: ActionConfigHydratedData[]
}
export interface SubmissionCheckConfigHydratedData extends CheckConfigHydratedData, TypedRunnableBaseData<SubmissionState> {
kind: 'submission'
}
export interface CommentCheckConfigHydratedData extends CheckConfigHydratedData, TypedRunnableBaseData<CommentState> {
kind: 'comment'
}
/*
* All actions/rules/filters should now be full objects
* */
export interface CheckConfigObject extends Omit<CheckConfigHydratedData, 'itemIs' | 'authorIs'>, StructuredRunnableBase {
rules: Array<RuleSetConfigObject | RuleConfigObject>
actions: Array<ActionConfigObject>
}
export interface SubmissionCheckConfigObject extends Omit<CheckConfigObject, 'itemIs' | 'author'>, TypedStructuredRunnableBase<SubmissionState> {
kind: 'submission'
}
export interface CommentCheckConfigObject extends Omit<CheckConfigObject, 'itemIs' | 'author'>, TypedStructuredRunnableBase<CommentState> {
kind: 'comment'
itemIs?: MinimalOrFullFilterJson<SubmissionState>
}
/**
@@ -747,18 +691,33 @@ export const userResultCacheDefault: Required<UserResultCacheOptions> = {
runActions: true,
}
export const asStructuredCommentCheckJson = (val: any): val is CommentCheckConfigObject => {
export interface CommentCheckJson extends CheckJson {
kind: 'comment'
itemIs?: MinimalOrFullFilterJson<CommentState>
}
export const asStructuredCommentCheckJson = (val: any): val is CommentCheckStructuredJson => {
return val.kind === 'comment';
}
export const asStructuredSubmissionCheckJson = (val: any): val is SubmissionCheckConfigObject => {
export const asStructuredSubmissionCheckJson = (val: any): val is SubmissionCheckStructuredJson => {
return val.kind === 'submission';
}
export type ActivityCheckConfigValue = string | IncludesData | SubmissionCheckConfigData | CommentCheckConfigData;
export type CheckStructuredJson = SubmissionCheckStructuredJson | CommentCheckStructuredJson;
// export interface CheckStructuredJson extends CheckJson {
// rules: Array<RuleSetObjectJson | RuleObjectJson>
// actions: Array<ActionObjectJson>
// }
export type ActivityCheckConfigData = Exclude<ActivityCheckConfigValue, IncludesData>;
export interface SubmissionCheckStructuredJson extends Omit<SubmissionCheckJson, 'authorIs' | 'itemIs' | 'rules'>, StructuredRunnableBase {
rules: Array<StructuredRuleSetObjectJson | StructuredRuleObjectJson>
actions: Array<ActionObjectJson>
itemIs?: MinimalOrFullFilter<SubmissionState>
}
export type ActivityCheckConfigHydratedData = SubmissionCheckConfigHydratedData | CommentCheckConfigHydratedData;
export type ActivityCheckObject = SubmissionCheckConfigObject | CommentCheckConfigObject;
export interface CommentCheckStructuredJson extends Omit<CommentCheckJson, 'authorIs' | 'itemIs' | 'rules'>, StructuredRunnableBase {
rules: Array<StructuredRuleSetObjectJson | StructuredRuleObjectJson>
actions: Array<ActionObjectJson>
itemIs?: MinimalOrFullFilter<CommentState>
}

View File

@@ -1,34 +0,0 @@
import {ActivitySourceData, ActivitySourceTypes} from "./Infrastructure/Atomic";
import {strToActivitySourceData} from "../util";
export class ActivitySource {
type: ActivitySourceTypes
identifier?: string
constructor(data: string | ActivitySourceData) {
if (typeof data === 'string') {
const {type, identifier} = strToActivitySourceData(data);
this.type = type;
this.identifier = identifier;
} else {
this.type = data.type;
this.identifier = data.identifier;
}
}
matches(desired: ActivitySource): boolean {
if(desired.type !== this.type) {
return false;
}
// if this source does not have an identifier (we have already matched type) then it is broad enough to match
if(this.identifier === undefined) {
return true;
}
// at this point we know this source has an identifier but desired DOES NOT so this source is more restrictive and does not match
if(desired.identifier === undefined) {
return false;
}
// otherwise sources match if identifiers are the same
return this.identifier.toLowerCase() === desired.identifier.toLowerCase();
}
}

View File

@@ -1,47 +0,0 @@
import {CacheOptions} from "../interfaces";
import cacheManager, {Cache} from "cache-manager";
import {redisStore} from "cache-manager-redis-store";
import {create as createMemoryStore} from "../../Utils/memoryStore";
import {CacheProvider} from "../Infrastructure/Atomic";
import {cacheOptDefaults} from "../defaults";
export const buildCacheOptionsFromProvider = (provider: CacheProvider | any): CacheOptions => {
if (typeof provider === 'string') {
return {
store: provider as CacheProvider,
...cacheOptDefaults
}
}
return {
store: 'memory',
...cacheOptDefaults,
...provider,
}
}
export const createCacheManager = async (options: CacheOptions): Promise<Cache> => {
const {store, max, ttl = 60, host = 'localhost', port, auth_pass, db, ...rest} = options;
switch (store) {
case 'none':
return cacheManager.caching({store: 'none', max, ttl});
case 'redis':
const rStore = await redisStore(
{
socket: {
host,
port
},
password: auth_pass,
database: db,
}
);
return cacheManager.caching({
store: rStore,
ttl,
...rest,
});
case 'memory':
default:
//return cacheManager.caching({store: 'memory', max, ttl});
return cacheManager.caching({store: {create: createMemoryStore}, max, ttl, shouldCloneBeforeSet: false});
}
}

View File

@@ -6,5 +6,4 @@ export interface ConfigToObjectOptions {
location?: string,
jsonDocFunc?: (content: string, location?: string) => AbstractConfigDocument<OperatorJsonConfig>,
yamlDocFunc?: (content: string, location?: string) => AbstractConfigDocument<YamlDocument>
allowArrays?: boolean
}

View File

@@ -19,7 +19,6 @@ export const parseFromJsonOrYamlToObject = (content: string, options?: ConfigToO
location,
jsonDocFunc = (content: string, location?: string) => new JsonConfigDocument(content, location),
yamlDocFunc = (content: string, location?: string) => new YamlConfigDocument(content, location),
allowArrays = false,
} = options || {};
try {

View File

@@ -1,14 +1,11 @@
import YamlConfigDocument from "../YamlConfigDocument";
import JsonConfigDocument from "../JsonConfigDocument";
import {YAMLMap, YAMLSeq, Pair, Scalar} from "yaml";
import {BotInstanceJsonConfig, OperatorJsonConfig, WebCredentials} from "../../interfaces";
import {YAMLMap, YAMLSeq} from "yaml";
import {BotInstanceJsonConfig, OperatorJsonConfig} from "../../interfaces";
import {assign} from 'comment-json';
export interface OperatorConfigDocumentInterface {
addBot(botData: BotInstanceJsonConfig): void;
setFriendlyName(name: string): void;
setWebCredentials(data: Required<WebCredentials>): void;
setOperator(name: string): void;
toJS(): OperatorJsonConfig;
}
@@ -18,12 +15,10 @@ export class YamlOperatorConfigDocument extends YamlConfigDocument implements Op
if (bots === undefined) {
this.parsed.add({key: 'bots', value: [botData]});
} else if (botData.name !== undefined) {
// granularly overwrite (merge) if we find an existing
// overwrite if we find an existing
const existingIndex = bots.items.findIndex(x => (x as YAMLMap).get('name') === botData.name);
if (existingIndex !== -1) {
const botObj = this.parsed.getIn(['bots', existingIndex]) as YAMLMap;
const mergedVal = mergeObjectToYaml(botData, botObj);
this.parsed.setIn(['bots', existingIndex], mergedVal);
this.parsed.setIn(['bots', existingIndex], botData);
} else {
this.parsed.addIn(['bots'], botData);
}
@@ -32,41 +27,11 @@ export class YamlOperatorConfigDocument extends YamlConfigDocument implements Op
}
}
setFriendlyName(name: string) {
this.parsed.addIn(['api', 'friendly'], name);
}
setWebCredentials(data: Required<WebCredentials>) {
this.parsed.addIn(['web', 'credentials'], data);
}
setOperator(name: string) {
this.parsed.addIn(['operator', 'name'], name);
}
toJS(): OperatorJsonConfig {
return super.toJS();
}
}
export const mergeObjectToYaml = (source: object, target: YAMLMap) => {
for (const [k, v] of Object.entries(source)) {
if (target.has(k)) {
const targetProp = target.get(k);
if (targetProp instanceof YAMLMap && typeof v === 'object') {
const merged = mergeObjectToYaml(v, targetProp);
target.set(k, merged)
} else {
// since target prop and value are not both objects don't bother merging, just overwrite (primitive or array)
target.set(k, v);
}
} else {
target.add({key: k, value: v});
}
}
return target;
}
export class JsonOperatorConfigDocument extends JsonConfigDocument implements OperatorConfigDocumentInterface {
addBot(botData: BotInstanceJsonConfig) {
if (this.parsed.bots === undefined) {
@@ -83,23 +48,6 @@ export class JsonOperatorConfigDocument extends JsonConfigDocument implements Op
}
}
setFriendlyName(name: string) {
const api = this.parsed.api || {};
this.parsed.api = {...api, friendly: name};
}
setWebCredentials(data: Required<WebCredentials>) {
const {
web = {},
} = this.parsed;
this.parsed.web = {...web, credentials: data};
}
setOperator(name: string) {
this.parsed.operator = { name };
}
toJS(): OperatorJsonConfig {
return super.toJS();
}

View File

@@ -56,11 +56,6 @@ export class ActionResultEntity extends TimeAwareRandomBaseEntity {
@JoinColumn({name: 'premiseId'})
premise!: ActionPremise;
/**
* Ephemeral -- only added during actual run time and used for action templating. Is not available after loading from DB.
* */
data?: any;
touchedEntities: (Submission | Comment | RedditUser | string)[] = []
set itemIs(data: ActivityStateFilterResult | IFilterResult<TypedActivityState> | undefined) {

View File

@@ -1,11 +1,11 @@
import {Entity, Column, ManyToOne, PrimaryColumn, OneToMany, Index} from "typeorm";
import {Entity, Column, PrimaryGeneratedColumn, ManyToOne, PrimaryColumn, OneToMany, OneToOne, Index} from "typeorm";
import {AuthorEntity} from "./AuthorEntity";
import {Subreddit} from "./Subreddit";
import {CMEvent} from "./CMEvent";
import Submission from "snoowrap/dist/objects/Submission";
import {Comment} from "snoowrap";
import {asComment, getActivityAuthorName, parseRedditFullname, redditThingTypeToPrefix} from "../../util";
import {activityReports, ActivityType, Report, SnoowrapActivity} from "../Infrastructure/Reddit";
import {ActivityReport} from "./ActivityReport";
import dayjs, {Dayjs} from "dayjs";
import {ActivityType} from "../Infrastructure/Reddit";
export interface ActivityEntityOptions {
id: string
@@ -15,7 +15,6 @@ export interface ActivityEntityOptions {
permalink: string
author: AuthorEntity
submission?: Activity
reports?: ActivityReport[]
}
@Entity()
@@ -70,9 +69,6 @@ export class Activity {
@OneToMany(type => Activity, obj => obj.submission, {nullable: true})
comments!: Activity[];
@OneToMany(type => ActivityReport, act => act.activity, {cascade: ['insert'], eager: true})
reports: ActivityReport[] | undefined
constructor(data?: ActivityEntityOptions) {
if(data !== undefined) {
this.type = data.type;
@@ -82,76 +78,10 @@ export class Activity {
this.permalink = data.permalink;
this.author = data.author;
this.submission = data.submission;
this.reports = data.reports !== undefined ? data.reports : undefined;
}
}
/**
* @param {SnoowrapActivity} activity
* @param {Dayjs|undefined} lastKnownStateTimestamp Override the last good state (useful when tracked through polling)
* */
syncReports(activity: SnoowrapActivity, lastKnownStateTimestamp?: Dayjs) {
if(activity.num_reports > 0 && (this.reports === undefined || activity.num_reports !== this.reports.length)) {
if(this.reports === undefined) {
this.reports = [];
}
const reports = activityReports(activity);
// match up existing reports
const usedReportEntities: string[] = [];
const unsyncedReports: Report[] = [];
for(const r of reports) {
const matchedEntity = this.reports.find(x => !usedReportEntities.includes(x.id) && x.matchReport(r));
if(matchedEntity !== undefined) {
usedReportEntities.push(matchedEntity.id);
} else {
// found an unsynced report
unsyncedReports.push(r);
}
}
// ideally we only have one report but it's possible (probable) there are more
//
// to simplify tracking over time we will spread out the "create time" for each report to be between NOW
// and the last recorded report, or if no reports then the create time of the activity
// -- the assumptions about tracking should be good enough for most users because:
// * default poll interval is 30 seconds so even if there are more than one reports in that time the resolution is high enough for accurate usage (most mods will use "> 1 report in 1 minute" or larger timescales)
// * for populating existing reports (CM has not been tracking since activity creation) we don't want to bunch up all reports at the timestamp which could create false positives,
// it's more likely that reports would be spread out than all occurring at the same time.
// TODO additionally, will allow users to specify minimum required granularity to use when filtering by reports over time
let lastRecordedTime = lastKnownStateTimestamp;
if(lastKnownStateTimestamp === undefined) {
lastRecordedTime = this.reports.length > 0 ?
// get the latest create date for existing reports
this.reports.reduce((acc, curr) => curr.createdAt.isAfter(acc) ? curr.createdAt : acc, dayjs('2000-1-1'))
// if no reports then use activity create date
: dayjs(activity.created_utc * 1000);
}
// find the amount of time between now and last good timestamp
const missingTimespan = dayjs.duration(dayjs().diff(lastRecordedTime));
const granularity = Math.floor(missingTimespan.asSeconds());
// each report will have its create date spaced out (mostly) equally between now and the last good timestamp
//
// if only one report stick it in exact middle
// if more than one than decrease span by 1/4 so that we don't end up having reports dead-on the last timestamp
const increment = Math.floor(unsyncedReports.length === 1 ? (granularity / 2) : ((granularity / 1.25) / unsyncedReports.length));
for(let i = 0; i < unsyncedReports.length; i++) {
const r = new ActivityReport({...unsyncedReports[i], activity: this, granularity});
r.createdAt = dayjs().subtract(increment * (i + 1), 'seconds');
this.reports.push(r);
}
return true;
}
return false;
}
static fromSnoowrapActivity(subreddit: Subreddit, activity: SnoowrapActivity, lastKnownStateTimestamp?: dayjs.Dayjs | undefined) {
static fromSnoowrapActivity(subreddit: Subreddit, activity: (Submission | Comment)) {
let submission: Activity | undefined;
let type: ActivityType = 'submission';
let content: string;
@@ -169,7 +99,7 @@ export class Activity {
const author = new AuthorEntity();
author.name = getActivityAuthorName(activity.author);
const entity = new Activity({
return new Activity({
id: activity.name,
subreddit,
type,
@@ -177,10 +107,6 @@ export class Activity {
permalink: activity.permalink,
author,
submission
});
entity.syncReports(activity, lastKnownStateTimestamp);
return entity;
})
}
}

View File

@@ -1,57 +0,0 @@
import {
Entity,
Column,
ManyToOne, JoinColumn, AfterLoad,
} from "typeorm";
import {Activity} from "./Activity";
import {ManagerEntity} from "./ManagerEntity";
import {TimeAwareRandomBaseEntity} from "./Base/TimeAwareRandomBaseEntity";
import {Report, ReportType} from "../Infrastructure/Reddit";
@Entity()
export class ActivityReport extends TimeAwareRandomBaseEntity {
@Column({nullable: false, length: 500})
reason!: string
@Column({nullable: false, length: 20})
type!: ReportType
@Column({nullable: true, length: 100})
author?: string
@Column("int", {nullable: false})
granularity: number = 0;
@ManyToOne(type => Activity, act => act.reports, {cascade: ['update']})
@JoinColumn({name: 'activityId'})
activity!: Activity;
@Column({nullable: false, name: 'activityId'})
activityId!: string
constructor(data?: Report & { activity: Activity, granularity: number }) {
super();
if (data !== undefined) {
this.reason = data.reason;
this.type = data.type;
this.author = data.author;
this.activity = data.activity;
this.activityId = data.activity.id;
this.granularity = data.granularity
}
}
matchReport(report: Report): boolean {
return this.reason === report.reason
&& this.type === report.type
&& this.author === report.author;
}
@AfterLoad()
convertPrimitives() {
if(this.author === null) {
this.author = undefined;
}
}
}

View File

@@ -12,10 +12,4 @@ export class AuthorEntity {
@OneToMany(type => Activity, act => act.author)
activities!: Activity[]
constructor(data?: any) {
if(data !== undefined) {
this.name = data.name;
}
}
}

View File

@@ -1,90 +1,13 @@
import {Entity, Column, PrimaryColumn, OneToMany, PrimaryGeneratedColumn} from "typeorm";
import {ManagerEntity} from "./ManagerEntity";
import {RandomIdBaseEntity} from "./Base/RandomIdBaseEntity";
import {BotGuestEntity, ManagerGuestEntity} from "./Guest/GuestEntity";
import {Guest, GuestEntityData, HasGuests} from "./Guest/GuestInterfaces";
import {SubredditInvite} from "./SubredditInvite";
@Entity()
export class Bot extends RandomIdBaseEntity implements HasGuests {
export class Bot extends RandomIdBaseEntity {
@Column("varchar", {length: 200})
name!: string;
@OneToMany(type => ManagerEntity, obj => obj.bot)
managers!: Promise<ManagerEntity[]>
@OneToMany(type => BotGuestEntity, obj => obj.guestOf, {eager: true, cascade: ['insert', 'remove', 'update']})
guests!: BotGuestEntity[]
@OneToMany(type => SubredditInvite, obj => obj.bot, {eager: true, cascade: ['insert', 'remove', 'update']})
subredditInvites!: SubredditInvite[]
getGuests() {
const g = this.guests;
if (g === undefined) {
return [];
}
//return g.map(x => ({id: x.id, name: x.author.name, expiresAt: x.expiresAt})) as Guest[];
return g;
}
addGuest(val: GuestEntityData | GuestEntityData[]) {
const reqGuests = Array.isArray(val) ? val : [val];
const guests = this.guests;
for (const g of reqGuests) {
const existing = guests.find(x => x.author.name.toLowerCase() === g.author.name.toLowerCase());
if (existing !== undefined) {
// update existing guest expiresAt
existing.expiresAt = g.expiresAt;
} else {
guests.push(new BotGuestEntity({...g, guestOf: this}));
}
}
this.guests = guests
return guests;
}
removeGuestById(val: string | string[]) {
const reqGuests = Array.isArray(val) ? val : [val];
const guests = this.guests;
const filteredGuests = guests.filter(x => reqGuests.includes(x.id));
this.guests = filteredGuests;
return filteredGuests;
}
removeGuestByUser(val: string | string[]) {
const reqGuests = (Array.isArray(val) ? val : [val]).map(x => x.trim().toLowerCase());
const guests = this.guests;
const filteredGuests = guests.filter(x => reqGuests.includes(x.author.name.toLowerCase()));
this.guests =filteredGuests;
return filteredGuests;
}
removeGuests() {
this.guests = []
return [];
}
getSubredditInvites(): SubredditInvite[] {
if(this.subredditInvites === undefined) {
return [];
}
return this.subredditInvites;
}
addSubredditInvite(invite: SubredditInvite) {
if(this.subredditInvites === undefined) {
this.subredditInvites = [];
}
this.subredditInvites.push(invite);
}
removeSubredditInvite(invite: SubredditInvite) {
if(this.subredditInvites === undefined) {
return;
}
const index = this.subredditInvites.findIndex(x => x.id === invite.id);
this.subredditInvites.splice(index, 1);
}
}

View File

@@ -21,7 +21,7 @@ import Submission from "snoowrap/dist/objects/Submission";
import Comment from "snoowrap/dist/objects/Comment";
import {ColumnDurationTransformer} from "./Transformers";
import { RedditUser } from "snoowrap/dist/objects";
import {ActivitySourceTypes, DurationVal, NonDispatchActivitySourceValue, onExistingFoundBehavior} from "../Infrastructure/Atomic";
import {ActivitySourceTypes, DurationVal, NonDispatchActivitySource, onExistingFoundBehavior} from "../Infrastructure/Atomic";
@Entity({name: 'DispatchedAction'})
export class DispatchedEntity extends TimeAwareRandomBaseEntity {
@@ -53,7 +53,7 @@ export class DispatchedEntity extends TimeAwareRandomBaseEntity {
identifier?: string
@Column("varchar", {nullable: true, length: 200})
cancelIfQueued?: boolean | NonDispatchActivitySourceValue | NonDispatchActivitySourceValue[]
cancelIfQueued?: boolean | NonDispatchActivitySource | NonDispatchActivitySource[]
@Column({nullable: true})
onExistingFound?: onExistingFoundBehavior
@@ -127,7 +127,7 @@ export class DispatchedEntity extends TimeAwareRandomBaseEntity {
} else if (cVal === 'false') {
this.cancelIfQueued = false;
} else if (cVal.includes('[')) {
this.cancelIfQueued = JSON.parse(cVal) as NonDispatchActivitySourceValue[];
this.cancelIfQueued = JSON.parse(cVal) as NonDispatchActivitySource[];
}
}
if(this.goto === null) {

View File

@@ -1,119 +0,0 @@
import {ChildEntity, Column, Entity, JoinColumn, ManyToOne, TableInheritance} from "typeorm";
import {AuthorEntity} from "../AuthorEntity";
import { ManagerEntity } from "../ManagerEntity";
import { Bot } from "../Bot";
import {TimeAwareRandomBaseEntity} from "../Base/TimeAwareRandomBaseEntity";
import dayjs, {Dayjs} from "dayjs";
import {Guest, GuestAll, GuestEntityData} from "./GuestInterfaces";
export interface GuestOptions<T extends ManagerEntity | Bot> extends GuestEntityData {
guestOf: T
}
@Entity({name: 'Guests'})
@TableInheritance({ column: { type: "varchar", name: "type" } })
export abstract class GuestEntity<T extends ManagerEntity | Bot> extends TimeAwareRandomBaseEntity {
@ManyToOne(type => AuthorEntity, undefined, {cascade: ['insert'], eager: true})
@JoinColumn({name: 'authorName'})
author!: AuthorEntity;
@Column({ name: 'expiresAt', nullable: true })
_expiresAt?: Date = new Date();
public get expiresAt(): Dayjs {
return dayjs(this._expiresAt);
}
public set expiresAt(d: Dayjs | undefined) {
if(d === undefined) {
this._expiresAt = d;
} else {
this._expiresAt = d.utc().toDate();
}
}
expiresAtTimestamp(): number | undefined {
if(this._expiresAt !== undefined) {
return this.expiresAt.valueOf();
}
return undefined;
}
protected constructor(data?: GuestOptions<T>) {
super();
if(data !== undefined) {
this.author = data.author;
this.expiresAt = data.expiresAt;
}
}
}
@ChildEntity('manager')
export class ManagerGuestEntity extends GuestEntity<ManagerEntity> {
type: string = 'manager';
@ManyToOne(type => ManagerEntity, act => act.guests, {nullable: false, orphanedRowAction: 'delete'})
@JoinColumn({name: 'guestOfId', referencedColumnName: 'id'})
guestOf!: ManagerEntity
constructor(data?: GuestOptions<ManagerEntity>) {
super(data);
if(data !== undefined) {
this.guestOf = data.guestOf;
}
}
}
@ChildEntity('bot')
export class BotGuestEntity extends GuestEntity<Bot> {
type: string = 'bot';
@ManyToOne(type => Bot, act => act.guests, {nullable: false, orphanedRowAction: 'delete'})
@JoinColumn({name: 'guestOfId', referencedColumnName: 'id'})
guestOf!: Bot
constructor(data?: GuestOptions<Bot>) {
super(data);
if(data !== undefined) {
this.guestOf = data.guestOf;
this.author = data.author;
}
}
}
export const guestEntityToApiGuest = (val: GuestEntity<any>): Guest => {
return {
id: val.id,
name: val.author.name,
expiresAt: val.expiresAtTimestamp(),
}
}
interface ContextualGuest extends Guest {
subreddit: string
}
export const guestEntitiesToAll = (val: Map<string, Guest[]>): GuestAll[] => {
const contextualGuests: ContextualGuest[] = Array.from(val.entries()).map(([sub, guests]) => guests.map(y => ({...y, subreddit: sub} as ContextualGuest))).flat(3);
const userMap = contextualGuests.reduce((acc, curr) => {
let u: GuestAll | undefined = acc.get(curr.name);
if (u === undefined) {
u = {name: curr.name, expiresAt: curr.expiresAt, subreddits: [curr.subreddit]};
} else {
if (!u.subreddits.includes(curr.subreddit)) {
u.subreddits.push(curr.subreddit);
}
if ((u.expiresAt === undefined && curr.expiresAt !== undefined) || (u.expiresAt !== undefined && curr.expiresAt !== undefined && curr.expiresAt < u.expiresAt)) {
u.expiresAt = curr.expiresAt;
}
}
acc.set(curr.name, u);
return acc;
}, new Map<string, GuestAll>());
return Array.from(userMap.values());
}

View File

@@ -1,28 +0,0 @@
import { Dayjs } from "dayjs"
import {AuthorEntity} from "../AuthorEntity";
export interface Guest {
id: string
name: string
expiresAt?: number
}
export interface GuestAll {
name: string
expiresAt?: number
subreddits: string[]
}
export interface GuestEntityData {
expiresAt?: Dayjs
author: AuthorEntity
}
export interface HasGuests {
getGuests: () => GuestEntityData[]
addGuest: (val: GuestEntityData | GuestEntityData[]) => GuestEntityData[]
removeGuestById: (val: string | string[]) => GuestEntityData[]
removeGuestByUser: (val: string | string[]) => GuestEntityData[]
removeGuests: () => GuestEntityData[]
}

View File

@@ -15,14 +15,12 @@ import {RunEntity} from "./RunEntity";
import {Bot} from "./Bot";
import {RandomIdBaseEntity} from "./Base/RandomIdBaseEntity";
import {ManagerRunState} from "./EntityRunState/ManagerRunState";
import {QueueRunState} from "./EntityRunState/QueueRunState";
import { QueueRunState } from "./EntityRunState/QueueRunState";
import {EventsRunState} from "./EntityRunState/EventsRunState";
import {RulePremise} from "./RulePremise";
import {ActionPremise} from "./ActionPremise";
import {RunningStateTypes} from "../../Subreddit/Manager";
import { RunningStateTypes } from "../../Subreddit/Manager";
import {EntityRunState} from "./EntityRunState/EntityRunState";
import {GuestEntity, ManagerGuestEntity} from "./Guest/GuestEntity";
import {Guest, GuestEntityData, HasGuests} from "./Guest/GuestInterfaces";
export interface ManagerEntityOptions {
name: string
@@ -38,12 +36,12 @@ export type RunningStateEntities = {
};
@Entity({name: 'Manager'})
export class ManagerEntity extends RandomIdBaseEntity implements RunningStateEntities, HasGuests {
export class ManagerEntity extends RandomIdBaseEntity implements RunningStateEntities {
@Column("varchar", {length: 200})
name!: string;
@ManyToOne(type => Bot, sub => sub.managers, {eager: true})
@ManyToOne(type => Bot, sub => sub.managers, {cascade: ['insert'], eager: true})
bot!: Bot;
@ManyToOne(type => Subreddit, sub => sub.activities, {cascade: ['insert'], eager: true})
@@ -58,15 +56,12 @@ export class ManagerEntity extends RandomIdBaseEntity implements RunningStateEnt
@OneToMany(type => ActionPremise, obj => obj.manager)
actions!: Promise<ActionPremise[]>
@OneToMany(type => CheckEntity, obj => obj.manager)
@OneToMany(type => CheckEntity, obj => obj.manager) // note: we will create author property in the Photo class below
checks!: Promise<CheckEntity[]>
@OneToMany(type => RunEntity, obj => obj.manager)
@OneToMany(type => RunEntity, obj => obj.manager) // note: we will create author property in the Photo class below
runs!: Promise<RunEntity[]>
@OneToMany(type => ManagerGuestEntity, obj => obj.guestOf, {eager: true, cascade: ['insert', 'remove', 'update']})
guests!: ManagerGuestEntity[]
@OneToOne(() => EventsRunState, {cascade: ['insert', 'update'], eager: true})
@JoinColumn()
eventsState!: EventsRunState
@@ -90,50 +85,4 @@ export class ManagerEntity extends RandomIdBaseEntity implements RunningStateEnt
this.managerState = data.managerState;
}
}
getGuests(): ManagerGuestEntity[] {
const g = this.guests;
if (g === undefined) {
return [];
}
//return g.map(x => ({id: x.id, name: x.author.name, expiresAt: x.expiresAt})) as Guest[];
return g;
}
addGuest(val: GuestEntityData | GuestEntityData[]) {
const reqGuests = Array.isArray(val) ? val : [val];
const guests = this.getGuests();
for (const g of reqGuests) {
const existing = guests.find(x => x.author.name.toLowerCase() === g.author.name.toLowerCase());
if (existing !== undefined) {
// update existing guest expiresAt
existing.expiresAt = g.expiresAt;
} else {
guests.push(new ManagerGuestEntity({...g, guestOf: this}));
}
}
this.guests = guests;
return guests;
}
removeGuestById(val: string | string[]) {
const reqGuests = Array.isArray(val) ? val : [val];
const guests = this.getGuests();
const filteredGuests = guests.filter(x => !reqGuests.includes(x.id));
this.guests = filteredGuests
return filteredGuests;
}
removeGuestByUser(val: string | string[]) {
const reqGuests = (Array.isArray(val) ? val : [val]).map(x => x.trim().toLowerCase());
const guests = this.getGuests();
const filteredGuests = guests.filter(x => !reqGuests.includes(x.author.name.toLowerCase()));
this.guests = filteredGuests;
return filteredGuests;
}
removeGuests() {
this.guests = [];
return [];
}
}

View File

@@ -1,99 +0,0 @@
import {AfterLoad, Column, Entity, JoinColumn, ManyToOne, PrimaryColumn} from "typeorm";
import {InviteData, SubredditInviteData, SubredditInviteDataPersisted} from "../../Web/Common/interfaces";
import dayjs, {Dayjs} from "dayjs";
import {TimeAwareRandomBaseEntity} from "./Base/TimeAwareRandomBaseEntity";
import {AuthorEntity} from "./AuthorEntity";
import {Bot} from "./Bot";
@Entity()
export class SubredditInvite extends TimeAwareRandomBaseEntity implements SubredditInviteData {
@PrimaryColumn("varchar", {length: 255})
subreddit!: string;
@Column("simple-json", {nullable: true})
guests?: string[]
@Column("text")
initialConfig?: string
@PrimaryColumn("varchar", {length: 200})
messageId?: string
@ManyToOne(type => Bot, bot => bot.subredditInvites, {nullable: false, orphanedRowAction: 'delete'})
@JoinColumn({name: 'botId', referencedColumnName: 'id'})
bot!: Bot;
@Column({name: 'expiresAt', nullable: true})
_expiresAt?: Date;
public get expiresAt(): Dayjs | undefined {
if (this._expiresAt === undefined) {
return undefined;
}
return dayjs(this._expiresAt);
}
public set expiresAt(d: Dayjs | undefined) {
if (d === undefined) {
this._expiresAt = d;
} else {
this._expiresAt = d.utc().toDate();
}
}
constructor(data?: SubredditInviteData & { expiresIn?: number, bot: Bot }) {
super();
if (data !== undefined) {
this.subreddit = data.subreddit;
this.initialConfig = data.initialConfig === null ? undefined : data.initialConfig;
this.guests = data.guests === null || data.guests === undefined ? [] : data.guests;
this.bot = data.bot;
if (data.expiresIn !== undefined && data.expiresIn !== 0) {
this.expiresAt = dayjs().add(data.expiresIn, 'seconds');
}
}
}
toSubredditInviteData(): SubredditInviteDataPersisted {
return {
id: this.id,
subreddit: this.subreddit,
initialConfig: this.getInitialConfig(),
guests: this.getGuests(),
expiresAt: this.expiresAt !== undefined ? this.expiresAt.unix() : undefined,
}
}
getGuests(): string[] {
if(this.guests === null || this.guests === undefined) {
return [];
}
return this.guests;
}
getInitialConfig(): string | undefined {
if(this.initialConfig === null) {
return undefined;
}
return this.initialConfig;
}
canAutomaticallyAccept() {
return this.getGuests().length === 0 && this.getInitialConfig() === undefined;
// TODO setup inbox checking to look for reply to messageId (eventually!)
}
@AfterLoad()
fixNullable() {
if(this.guests === null) {
this.guests = undefined;
}
if(this.initialConfig === null) {
this.initialConfig = undefined;
}
}
}

View File

@@ -1,17 +1,15 @@
import fetch from "node-fetch";
import {Submission} from "snoowrap/dist/objects";
import {URL} from "url";
import {absPercentDifference, getExtension, getSharpAsync, isValidImageURL} from "../util";
import {absPercentDifference, getSharpAsync, isValidImageURL} from "../util";
import {Sharp} from "sharp";
import {blockhashAndFlipped} from "./blockhash/blockhash";
import {CMError, SimpleError} from "../Utils/Errors";
import {FileHandle, open} from "fs/promises";
import {ImageHashCacheData} from "./Infrastructure/Atomic";
import {blockhash} from "./blockhash/blockhash";
import {SimpleError} from "../Utils/Errors";
export interface ImageDataOptions {
width?: number,
height?: number,
path: URL,
url: string,
variants?: ImageData[]
}
@@ -19,20 +17,19 @@ class ImageData {
width?: number
height?: number
path: URL
url: URL
variants: ImageData[] = []
preferredResolution?: [number, number]
sharpImg!: Sharp
hashResult?: string
hashResultFlipped?: string
hashResult!: string
actualResolution?: [number, number]
constructor(data: ImageDataOptions, aggressive = false) {
this.width = data.width;
this.height = data.height;
this.path = data.path;
if (!aggressive && !isValidImageURL(`${this.path.origin}${this.path.pathname}`)) {
throw new Error('Path did not end with a valid image extension');
this.url = new URL(data.url);
if (!aggressive && !isValidImageURL(`${this.url.origin}${this.url.pathname}`)) {
throw new Error('URL did not end with a valid image extension');
}
this.variants = data.variants || [];
}
@@ -42,90 +39,55 @@ class ImageData {
return await (await this.sharp()).clone().toFormat(format).toBuffer();
}
async hash(bits: number = 16, useVariantIfPossible = true): Promise<Required<ImageHashCacheData>> {
if (this.hashResult === undefined || this.hashResultFlipped === undefined) {
async hash(bits: number, useVariantIfPossible = true): Promise<string> {
if(this.hashResult === undefined) {
let ref: ImageData | undefined;
if (useVariantIfPossible && this.preferredResolution !== undefined) {
if(useVariantIfPossible && this.preferredResolution !== undefined) {
ref = this.getSimilarResolutionVariant(this.preferredResolution[0], this.preferredResolution[1]);
}
if (ref === undefined) {
if(ref === undefined) {
ref = this;
}
const [hash, hashFlipped] = await blockhashAndFlipped((await ref.sharp()).clone(), bits);
this.hashResult = hash;
this.hashResultFlipped = hashFlipped;
this.hashResult = await blockhash((await ref.sharp()).clone(), bits);
}
return {original: this.hashResult, flipped: this.hashResultFlipped};
return this.hashResult;
}
async sharp(): Promise<Sharp> {
if (this.sharpImg === undefined) {
let animated = false;
let getBuffer: () => Promise<Buffer>;
let fileHandle: FileHandle | undefined;
try {
if (this.path.protocol === 'file:') {
try {
animated = ['gif', 'webp'].includes(getExtension(this.path.pathname));
fileHandle = await open(this.path, 'r');
getBuffer = async () => await (fileHandle as FileHandle).readFile();
} catch (err: any) {
throw new CMError(`Unable to retrieve local file ${this.path.toString()}`, {cause: err});
const response = await fetch(this.url.toString())
if (response.ok) {
const ct = response.headers.get('Content-Type');
if (ct !== null && ct.includes('image')) {
const sFunc = await getSharpAsync();
// if image is animated then we want to extract the first frame and convert it to a regular image
// so we can compare two static images later (also because sharp can't use resize() on animated images)
if(['gif','webp'].some(x => ct.includes(x))) {
this.sharpImg = await sFunc(await (await sFunc(await response.buffer(), {pages: 1, animated: false})).png().toBuffer());
} else {
this.sharpImg = await sFunc(await response.buffer());
}
const meta = await this.sharpImg.metadata();
if (this.width === undefined || this.height === undefined) {
this.width = meta.width;
this.height = meta.height;
}
this.actualResolution = [meta.width as number, meta.height as number];
} else {
throw new SimpleError(`Content-Type for fetched URL ${this.url} did not contain "image"`);
}
} else {
try {
const response = await fetch(this.path.toString())
if (response.ok) {
const ct = response.headers.get('Content-Type');
if (ct !== null && ct.includes('image')) {
animated = ['gif', 'webp'].some(x => ct.includes(x));
getBuffer = async () => await response.buffer();
} else {
throw new SimpleError(`Content-Type for fetched URL ${this.path.toString()} did not contain "image"`);
}
} else {
throw new SimpleError(`Fetching ${this.path.toString()} => URL response was not OK: (${response.status})${response.statusText}`);
}
} catch (err: any) {
if (!(err instanceof SimpleError)) {
throw new CMError(`Error occurred while fetching response from URL ${this.path.toString()}`, {cause: err});
} else {
throw err;
}
}
throw new SimpleError(`URL response was not OK: (${response.status})${response.statusText}`);
}
} catch (err: any) {
throw new CMError('Unable to fetch image resource', {cause: err, isSerious: false});
}
try {
const sFunc = await getSharpAsync();
// if image is animated then we want to extract the first frame and convert it to a regular image
// so we can compare two static images later (also because sharp can't use resize() on animated images)
if (animated) {
this.sharpImg = await sFunc(await (await sFunc(await getBuffer(), {
pages: 1,
animated: false
}).trim().greyscale()).png().withMetadata().toBuffer());
if(!(err instanceof SimpleError)) {
throw new Error(`Error occurred while fetching response from URL: ${err.message}`);
} else {
this.sharpImg = await sFunc(await sFunc(await getBuffer()).trim().greyscale().withMetadata().toBuffer());
throw err;
}
if(fileHandle !== undefined) {
await fileHandle.close();
}
const meta = await this.sharpImg.metadata();
if (this.width === undefined || this.height === undefined) {
this.width = meta.width;
this.height = meta.height;
}
this.actualResolution = [meta.width as number, meta.height as number];
} catch (err: any) {
throw new CMError('Error occurred while converting image buffer to Sharp object', {cause: err});
}
}
return this.sharpImg;
@@ -145,8 +107,8 @@ class ImageData {
return this.width !== undefined && this.height !== undefined;
}
get basePath() {
return `${this.path.origin}${this.path.pathname}`;
get baseUrl() {
return `${this.url.origin}${this.url.pathname}`;
}
setPreferredResolutionByWidth(prefWidth: number) {
@@ -263,23 +225,10 @@ class ImageData {
return [refSharp, compareSharp, width, height];
}
toHashCache(): ImageHashCacheData {
return {
original: this.hashResult,
flipped: this.hashResultFlipped
}
}
setFromHashCache(data: ImageHashCacheData) {
const {original, flipped} = data;
this.hashResult = original;
this.hashResultFlipped = flipped;
}
static fromSubmission(sub: Submission, aggressive = false): ImageData {
const url = new URL(sub.url);
const data: any = {
path: url,
url,
};
let variants = [];
if (sub.preview !== undefined && sub.preview.enabled && sub.preview.images.length > 0) {
@@ -288,7 +237,7 @@ class ImageData {
data.width = ref.width;
data.height = ref.height;
variants = firstImg.resolutions.map(x => new ImageData({...x, path: new URL(x.url)}));
variants = firstImg.resolutions.map(x => new ImageData(x));
data.variants = variants;
}
return new ImageData(data, aggressive);

View File

@@ -1,189 +0,0 @@
import {InfluxConfig} from "./interfaces";
import {InfluxDB, Point, WriteApi, setLogger, DEFAULT_WriteOptions, ClientOptions, DEFAULT_RetryDelayStrategyOptions, Logger as InfluxLogger} from "@influxdata/influxdb-client";
import {HealthAPI} from "@influxdata/influxdb-client-apis";
import dayjs, {Dayjs} from "dayjs";
import {Logger} from "winston";
import {mergeArr} from "../../util";
import {CMError} from "../../Utils/Errors";
import {Agent} from 'http';
import {WriteOptions} from "@influxdata/influxdb-client/dist";
export interface InfluxClientConfig extends InfluxConfig {
client?: InfluxDB
ready?: boolean
}
/**
* Suppress non-error write failures
*
* These have not yet hit the max retry. On max retry failure Influx logs as ERROR.
* The non-error failures are super noisy in the log so suppress them UNLESS debug is turned on
*
* https://github.com/influxdata/influxdb-client-js/blob/master/packages/core/src/impl/WriteApiImpl.ts#L221
* */
const extendLogger = (logger: Logger, suppressWriteWarnings = true): InfluxLogger => {
return {
...logger,
error: (message: string, err?: any) => logger.error(message, err),
warn: (message: string, err?: any) => {
if(suppressWriteWarnings && !message.includes('Write to InfluxDB failed (attempt')) {
logger.warn(message, err);
} else if(!suppressWriteWarnings) {
logger.warn(message, err);
}
}
}
}
export class InfluxClient {
config: InfluxConfig;
client: InfluxDB;
write: WriteApi;
health: HealthAPI;
tags: Record<string, string>;
logger: Logger;
ready: boolean;
lastReadyAttempt: Dayjs | undefined;
constructor(config: InfluxClientConfig, logger: Logger, tags: Record<string, string> = {}) {
const {client, ready = false, ...rest} = config;
this.logger = logger.child({
labels: ['Influx']
}, mergeArr);
this.config = rest;
this.ready = ready;
if (client !== undefined) {
this.client = client;
} else {
this.client = InfluxClient.createClient(this.config);
setLogger(extendLogger(this.logger, !(rest.debug ?? false)));
}
this.write = this.client.getWriteApi(config.credentials.org, config.credentials.bucket, 'ms', InfluxClient.createWriteOptions(this.config, this.logger));
this.tags = tags;
this.write.useDefaultTags(tags);
this.health = new HealthAPI(this.client);
}
async isReady() {
if (this.ready) {
return true;
}
if (this.lastReadyAttempt === undefined || dayjs().diff(this.lastReadyAttempt, 's') >= 10) {
if (!(await this.testConnection())) {
this.logger.warn('Influx endpoint is not ready');
} else {
this.ready = true;
}
} else {
this.logger.debug(`Influx endpoint testing throttled. Waiting another ${10 - dayjs().diff(this.lastReadyAttempt, 's')} seconds`);
}
return this.ready;
}
async testConnection() {
try {
const result = await this.health.getHealth();
if (result.status === 'fail') {
return false;
}
return true;
} catch (e: any) {
this.logger.error(new CMError(`Testing health of Influx endpoint failed`, {cause: e, isSerious: false}));
return false;
}
}
async writePoint(data: Point | Point[]) {
if (await this.isReady()) {
if (Array.isArray(data)) {
this.write.writePoints(data);
} else {
this.write.writePoint(data);
}
}
}
async flush() {
if (await this.isReady()) {
try {
await this.write.flush(true);
} catch (e: any) {
this.logger.error(new CMError('Failed to flush data to Influx', {cause: e}));
}
}
}
static createClient(config: InfluxConfig): InfluxDB {
const {
credentials,
useKeepAliveAgent = true,
} = config;
const clientOptions: ClientOptions = {
url: credentials.url,
token: credentials.token,
writeOptions: InfluxClient.createWriteOptions(config),
}
if (useKeepAliveAgent) {
// reusing connection
// https://github.com/influxdata/influxdb-client-js/issues/393#issuecomment-985272866
const agent = new Agent({
keepAlive: true,
keepAliveMsecs: 20 * 1000, // 20 seconds keep alive
})
process.on('exit', () => agent.destroy())
clientOptions.transportOptions = {agent};
}
return new InfluxDB(clientOptions);
}
static createWriteOptions(config: InfluxConfig, logger?: Logger): Partial<WriteOptions> {
const {
writeOptions: {
defaultTags: userDefinedDefaultTags = {},
...restUserWriteOptions
} = {
batchSize: 500,
maxRetries: 5,
// 30 seconds
flushInterval: 30000
},
defaultTags: legacyDefaultTags = {},
debug = false,
} = config;
const allUserDefinedTags = {...legacyDefaultTags, ...userDefinedDefaultTags};
const writeOptions: Partial<WriteOptions> = {
...DEFAULT_WriteOptions,
...restUserWriteOptions,
defaultTags: allUserDefinedTags
}
if (debug && logger !== undefined) {
writeOptions.writeSuccess = (lines: Array<string>) => {
logger.debug(`Flushed ${lines.length} lines to server`);
};
writeOptions.writeRetrySkipped = (entry: { lines: Array<string>; expires: number }) => {
logger.warn(`Skipped flushing ${entry.lines.length} lines due to full buffer`);
}
}
return writeOptions;
}
childClient(logger: Logger, tags: Record<string, string> = {}) {
return new InfluxClient({
...this.config,
client: this.client,
ready: this.ready
}, logger, {...this.tags, ...tags});
}
}

View File

@@ -1,16 +0,0 @@
import {InfluxDB, WriteApi, WriteOptions} from "@influxdata/influxdb-client/dist";
export interface InfluxConfig {
credentials: InfluxCredentials
defaultTags?: Record<string, string>
writeOptions?: WriteOptions
useKeepAliveAgent?: boolean
debug?: boolean
}
export interface InfluxCredentials {
url: string
token: string
org: string
bucket: string
}

View File

@@ -1,8 +1,5 @@
import {StructuredRunnableBase} from "./Runnable";
import {ActionJson} from "../types";
import {IncludesData} from "./Includes";
export type ActionConfigData = ActionJson;
export type ActionConfigHydratedData = Exclude<ActionConfigData, IncludesData>;
export type ActionConfigObject = Exclude<ActionConfigHydratedData, string>;
export type StructuredActionObjectJson = Omit<ActionConfigObject, 'authorIs' | 'itemIs'> & StructuredRunnableBase
export type ActionObjectJson = Exclude<ActionJson, string>;
export type StructuredActionObjectJson = Omit<ActionObjectJson, 'authorIs' | 'itemIs'> & StructuredRunnableBase

View File

@@ -1,5 +1,3 @@
import {ActivityType} from "./Reddit";
/**
* A duration and how to compare it against a value
*
@@ -17,22 +15,6 @@ import {ActivityType} from "./Reddit";
export type DurationComparor = string;
/**
* A relative datetime description
*
* May be either:
*
* * day of the week (monday, tuesday, etc...)
* * cron expression IE `* * 15 *`
*
* See https://crontab.guru/ for generating expressions
*
* https://regexr.com/6u3cc
*
* @pattern ((?:(?:(?:(?:\d+,)+\d+|(?:\d+(?:\/|-|#)\d+)|\d+L?|\*(?:\/\d+)?|L(?:-\d+)?|\?|[A-Z]{3}(?:-[A-Z]{3})?) ?){5,7})$)|(mon|tues|wed|thurs|fri|sat|sun){1}
* */
export type RelativeDateTimeMatch = string;
/**
* A string containing a comparison operator and a value to compare against
*
@@ -41,7 +23,7 @@ export type RelativeDateTimeMatch = string;
* * EX `> 100` => greater than 100
* * EX `<= 75%` => less than or equal to 75%
*
* @pattern ^\s*(>|>=|<|<=)\s*((?:\d+)(?:(?:(?:.|,)\d+)+)?)\s*(%?)(.*)$
* @pattern ^\s*(>|>=|<|<=)\s*(\d+)\s*(%?)(.*)$
* */
export type CompareValueOrPercent = string;
export type StringOperator = '>' | '>=' | '<' | '<=';
@@ -166,18 +148,10 @@ export type RecordOutputOption = boolean | RecordOutputType | RecordOutputType[]
export type PostBehaviorType = 'next' | 'stop' | 'nextRun' | string;
export type onExistingFoundBehavior = 'replace' | 'skip' | 'ignore';
export type ActionTarget = 'self' | 'parent';
export type ArbitraryActionTarget = ActionTarget | string;
export type InclusiveActionTarget = ActionTarget | 'any';
export const SOURCE_POLL = 'poll';
export type SourcePollStr = 'poll';
export const SOURCE_DISPATCH = 'dispatch';
export type SourceDispatchStr = 'dispatch';
export const SOURCE_USER = 'user';
export type SourceUserStr = 'user';
export type DispatchSourceValue = SourceDispatchStr | `dispatch:${string}`;
export type NonDispatchActivitySourceValue = SourcePollStr | `poll:${PollOn}` | SourceUserStr | `user:${string}`;
export type ActivitySourceTypes = SourcePollStr | SourceDispatchStr | SourceUserStr; // TODO
export type DispatchSource = 'dispatch' | `dispatch:${string}`;
export type NonDispatchActivitySource = 'poll' | `poll:${PollOn}` | 'user' | `user:${string}`;
export type ActivitySourceTypes = 'poll' | 'dispatch' | 'user'; // TODO
// https://github.com/YousefED/typescript-json-schema/issues/426
// https://github.com/YousefED/typescript-json-schema/issues/425
// @pattern ^(((poll|dispatch)(:\w+)?)|user)$
@@ -195,17 +169,11 @@ export type ActivitySourceTypes = SourcePollStr | SourceDispatchStr | SourceUser
*
*
* */
export type ActivitySourceValue = NonDispatchActivitySourceValue | DispatchSourceValue;
export interface ActivitySourceData {
type: ActivitySourceTypes
identifier?: string
}
export type ActivitySource = NonDispatchActivitySource | DispatchSource;
export type ConfigFormat = 'json' | 'yaml';
export type ActionTypes =
'comment'
| 'submission'
| 'lock'
| 'remove'
| 'report'
@@ -217,8 +185,7 @@ export type ActionTypes =
| 'userflair'
| 'dispatch'
| 'cancelDispatch'
| 'contributor'
| 'modnote';
| 'contributor';
/**
* Test the calculated VADER sentiment (compound) score for an Activity using this comparison. Can be either a numerical or natural language
@@ -262,136 +229,3 @@ export type ActionTypes =
* @examples ["is negative", "> 0.2"]
* */
export type VaderSentimentComparison = string;
export type ModUserNoteLabel =
'BOT_BAN'
| 'PERMA_BAN'
| 'BAN'
| 'ABUSE_WARNING'
| 'SPAM_WARNING'
| 'SPAM_WATCH'
| 'SOLID_CONTRIBUTOR'
| 'HELPFUL_USER';
export const modUserNoteLabels = ['BOT_BAN', 'PERMA_BAN', 'BAN', 'ABUSE_WARNING', 'SPAM_WARNING', 'SPAM_WATCH', 'SOLID_CONTRIBUTOR', 'HELPFUL_USER'];
export type ModActionType =
'INVITE' |
'NOTE' |
'REMOVAL' |
'SPAM' |
'APPROVAL';
export type UserNoteType =
'gooduser' |
'spamwatch' |
'spamwarn' |
'abusewarn' |
'ban' |
'permban' |
'botban' |
string;
export const userNoteTypes = ['gooduser', 'spamwatch', 'spamwarn', 'abusewarn', 'ban', 'permban', 'botban'];
export type ConfigFragmentValidationFunc = (data: object, fetched: boolean) => boolean;
export interface WikiContext {
wiki: string
subreddit?: string
}
export interface ExternalUrlContext {
url: string
}
export interface UrlContext {
value: string
context: WikiContext | ExternalUrlContext
}
export interface ImageHashCacheData {
original?: string
flipped?: string
}
// https://www.reddit.com/message/compose?to=/r/mealtimevideos&message=https://www.reddit.com/r/ContextModBot/comments/otz396/introduction_to_contextmodbot
export interface BaseTemplateData {
botLink: string
modmailLink?: string
manager?: string
check?: string
//[key: string]: any
}
export interface ActivityTemplateData {
kind: ActivityType
author: string
votes: number
age: string
permalink: string
id: string
subreddit: string
title: string
shortTitle: string
}
export interface ModdedActivityTemplateData {
reports: number
modReports: number
userReports: number
}
export interface SubmissionTemplateData extends ActivityTemplateData, Partial<ModdedActivityTemplateData> {
nsfw: boolean
spoiler: boolean
op: boolean
upvoteRatio: string
url: string
}
export interface CommentTemplateData extends ActivityTemplateData, Partial<ModdedActivityTemplateData> {
op: boolean
}
export interface SubredditTemplateData {
subredditBreakdownFormatted: string
subredditBreakdown?: {
totalFormatted: string
submissionFormatted: string
commentFormatted: string
}
}
export interface RuleResultTemplateData {
kind: string
triggered: boolean
result: string
[key: string]: any
}
export interface ActionResultTemplateData {
kind: string
success: boolean
result: string
[key: string]: any
}
export interface ActionResultsTemplateData {
actionSummary: string
actions: {
[key: string]: ActionResultTemplateData
}
}
export interface RuleResultsTemplateData {
ruleSummary: string
rules: {
[key: string]: RuleResultTemplateData
}
}
export interface GenericContentTemplateData extends BaseTemplateData, Partial<RuleResultsTemplateData>, Partial<ActionResultsTemplateData> {
item?: (SubmissionTemplateData | CommentTemplateData)
}

View File

@@ -1,10 +1,6 @@
import {StringOperator} from "./Atomic";
import {Duration} from "dayjs/plugin/duration";
import InvalidRegexError from "../../Utils/InvalidRegexError";
import dayjs, {Dayjs, OpUnitType} from "dayjs";
import {CMError, SimpleError} from "../../Utils/Errors";
import {escapeRegex, parseDuration, parseDurationFromString, parseStringToRegex} from "../../util";
import {ReportType} from "./Reddit";
export interface DurationComparison {
operator: StringOperator,
@@ -16,10 +12,7 @@ export interface GenericComparison extends HasDisplayText {
value: number,
isPercent: boolean,
extra?: string,
groups?: Record<string, string>
displayText: string,
duration?: Duration
durationText?: string
}
export interface HasDisplayText {
@@ -35,178 +28,37 @@ export const asGenericComparison = (val: any): val is GenericComparison => {
return typeof val === 'object' && 'value' in val;
}
export const GENERIC_VALUE_COMPARISON = /^\s*(?<opStr>>|>=|<|<=)\s*(?<value>-?(?:\d+)(?:(?:(?:.|,)\d+)+)?)(?<extra>\s+.*)*$/
export const GENERIC_VALUE_COMPARISON_URL = 'https://regexr.com/6vama';
export const parseGenericValueComparison = (val: string, options?: {
requireDuration?: boolean,
reg?: RegExp
}): GenericComparison => {
const {
requireDuration = false,
reg = GENERIC_VALUE_COMPARISON,
} = options || {};
const matches = val.match(reg);
export const GENERIC_VALUE_COMPARISON = /^\s*(?<opStr>>|>=|<|<=)\s*(?<value>-?\d?\.?\d+)(?<extra>\s+.*)*$/
export const GENERIC_VALUE_COMPARISON_URL = 'https://regexr.com/60dq4';
export const parseGenericValueComparison = (val: string): GenericComparison => {
const matches = val.match(GENERIC_VALUE_COMPARISON);
if (matches === null) {
throw new InvalidRegexError(reg, val)
throw new InvalidRegexError(GENERIC_VALUE_COMPARISON, val, GENERIC_VALUE_COMPARISON_URL)
}
const groups = matches.groups as any;
let duration: Duration | undefined;
let durationText: string | undefined;
try {
const durationResult = parseDurationFromString(val, false);
if(durationResult.length > 1) {
throw new SimpleError(`Must only have one Duration value, found ${durationResult.length} in: ${val}`);
}
duration = durationResult[0].duration;
durationText = durationResult[0].original;
} catch (e) {
// if it returns an invalid regex just means they didn't
if (requireDuration || !(e instanceof InvalidRegexError)) {
throw e;
}
}
const displayParts = [`${groups.opStr} ${groups.value}`];
const hasPercent = typeof groups.percent === 'string' && groups.percent.trim() !== '';
if(hasPercent) {
displayParts.push('%');
}
const {
opStr,
value,
percent,
extra,
...rest
} = matches.groups || {};
const extraGroups: Record<string,string> = {};
let hasExtraGroups = false;
for(const [k,v] of Object.entries(rest)) {
if(typeof v === 'string' && v.trim() !== '') {
extraGroups[k] = v;
hasExtraGroups = true;
}
}
return {
operator: groups.opStr as StringOperator,
value: Number.parseFloat(groups.value),
isPercent: hasPercent,
isPercent: false,
extra: groups.extra,
groups: hasExtraGroups ? extraGroups : undefined,
displayText: displayParts.join(''),
duration,
durationText,
displayText: `${groups.opStr} ${groups.value}`
}
}
const GENERIC_VALUE_PERCENT_COMPARISON = /^\s*(?<opStr>>|>=|<|<=)\s*(?<value>(?:\d+)(?:(?:(?:.|,)\d+)+)?)\s*(?<percent>%)?(?<extra>.*)$/
const GENERIC_VALUE_PERCENT_COMPARISON_URL = 'https://regexr.com/6valr';
export const parseGenericValueOrPercentComparison = (val: string, options?: {requireDuration: boolean}): GenericComparison => {
return parseGenericValueComparison(val, {...(options ?? {}), reg: GENERIC_VALUE_PERCENT_COMPARISON});
}
/**
* Named groups: operator, time, unit
* */
const DURATION_COMPARISON_REGEX: RegExp = /^\s*(?<opStr>>|>=|<|<=)\s*(?<time>\d+)\s*(?<unit>days?|weeks?|months?|years?|hours?|minutes?|seconds?|milliseconds?)\s*$/;
const DURATION_COMPARISON_REGEX_URL = 'https://regexr.com/609n8';
export const parseDurationComparison = (val: string): DurationComparison => {
const result = parseGenericValueComparison(val, {requireDuration: true});
if(result.isPercent) {
throw new InvalidRegexError(DURATION_COMPARISON_REGEX, val, DURATION_COMPARISON_REGEX_URL, 'Duration comparison value cannot be a percentage');
}
if(result.value < 0) {
throw new InvalidRegexError(DURATION_COMPARISON_REGEX, val, DURATION_COMPARISON_REGEX_URL,'Duration value cannot be negative');
const GENERIC_VALUE_PERCENT_COMPARISON = /^\s*(?<opStr>>|>=|<|<=)\s*(?<value>\d+)\s*(?<percent>%?)(?<extra>.*)$/
const GENERIC_VALUE_PERCENT_COMPARISON_URL = 'https://regexr.com/60a16';
export const parseGenericValueOrPercentComparison = (val: string): GenericComparison => {
const matches = val.match(GENERIC_VALUE_PERCENT_COMPARISON);
if (matches === null) {
throw new InvalidRegexError(GENERIC_VALUE_PERCENT_COMPARISON, val, GENERIC_VALUE_PERCENT_COMPARISON_URL)
}
const groups = matches.groups as any;
return {
operator: result.operator as StringOperator,
duration: result.duration as Duration
operator: groups.opStr as StringOperator,
value: Number.parseFloat(groups.value),
isPercent: groups.percent !== '',
extra: groups.extra,
displayText: `${groups.opStr} ${groups.value}${groups.percent === undefined || groups.percent === '' ? '' : '%'}`
}
}
export const dateComparisonTextOp = (val1: Dayjs, strOp: StringOperator, val2: Dayjs, granularity?: OpUnitType): boolean => {
switch (strOp) {
case '>':
return val1.isBefore(val2, granularity);
case '>=':
return val1.isSameOrBefore(val2, granularity);
case '<':
return val1.isAfter(val2, granularity);
case '<=':
return val1.isSameOrAfter(val2, granularity);
default:
throw new Error(`${strOp} was not a recognized operator`);
}
}
export const compareDurationValue = (comp: DurationComparison, date: Dayjs) => {
const dateToCompare = dayjs().subtract(comp.duration.asSeconds(), 'seconds');
return dateComparisonTextOp(date, comp.operator, dateToCompare);
}
export const comparisonTextOp = (val1: number, strOp: string, val2: number): boolean => {
switch (strOp) {
case '>':
return val1 > val2;
case '>=':
return val1 >= val2;
case '<':
return val1 < val2;
case '<=':
return val1 <= val2;
default:
throw new Error(`${strOp} was not a recognized operator`);
}
}
export interface ReportComparison extends Omit<GenericComparison, 'groups'> {
reportType?: ReportType
reasonRegex?: RegExp
reasonMatch?: string
}
const REPORT_COMPARISON = /^\s*(?<opStr>>|>=|<|<=)\s*(?<value>\d+)(?<percent>\s*%)?(?:\s+(?<reportType>mods?|users?))?(?:\s+(?<reasonMatch>["'].*["']|\/.*\/))?.*(?<time>\d+)?\s*(?<unit>days?|weeks?|months?|years?|hours?|minutes?|seconds?|milliseconds?)?\s*$/i
const REPORT_REASON_LITERAL = /["'](.*)["']/i
export const parseReportComparison = (str: string): ReportComparison => {
const generic = parseGenericValueComparison(str, {reg: REPORT_COMPARISON});
const {
groups: {
reportType,
reasonMatch
} = {},
...rest
} = generic;
const result: ReportComparison = {...rest, reasonMatch};
if(reportType !== undefined) {
if(reportType.toLocaleLowerCase().includes('mod')) {
result.reportType = 'mod' as ReportType;
} else if (reportType.toLocaleLowerCase().includes('user')) {
result.reportType = 'user' as ReportType;
}
}
if(reasonMatch !== undefined) {
const literalMatch = reasonMatch.match(REPORT_REASON_LITERAL);
if(literalMatch !== null) {
const cleanLiteralMatch = `/.*${escapeRegex(literalMatch[1].trim())}.*/`;
result.reasonRegex = parseStringToRegex(cleanLiteralMatch, 'i');
if(result.reasonRegex === undefined) {
throw new CMError(`Could not convert reason match value to Regex: ${cleanLiteralMatch}`, {isSerious: false})
}
} else {
result.reasonRegex = parseStringToRegex(reasonMatch, 'i');
if(result.reasonRegex === undefined) {
throw new CMError(`Could not convert reason match value to Regex: ${reasonMatch}`, {isSerious: false})
}
}
}
return result;
}

View File

@@ -1,15 +1,4 @@
import {
CompareValue,
CompareValueOrPercent,
DurationComparor,
ModeratorNameCriteria,
ModeratorNames, ModActionType,
ModUserNoteLabel, RelativeDateTimeMatch
} from "../Atomic";
import {ActivityType, MaybeActivityType} from "../Reddit";
import {GenericComparison, parseGenericValueComparison} from "../Comparisons";
import {parseStringToRegexOrLiteralSearch} from "../../../util";
import { Submission, Comment } from "snoowrap";
import {CompareValue, CompareValueOrPercent, DurationComparor, ModeratorNameCriteria, ModeratorNames} from "../Atomic";
/**
* Different attributes a `Subreddit` can be in. Only include a property if you want to check it.
@@ -66,40 +55,42 @@ export const defaultStrongSubredditCriteriaOptions = {
export type FilterCriteriaDefaultBehavior = 'replace' | 'merge';
export interface UserSubredditHistoryCriteria {
export interface UserNoteCriteria {
/**
* User Note type key to search for
* @examples ["spamwarn"]
* */
type: string;
/**
* Number of occurrences of this type. Ignored if `search` is `current`
*
* A string containing a comparison operator and/or a value to compare number of occurrences against
*
* The syntax is `(< OR > OR <= OR >=) <number>[percent sign] [in timeRange] [ascending|descending]`
*
* If `timeRange` is given then only notes/mod actions that occur between timeRange and NOW will be returned. `timeRange` is ignored if search is `current`
* The syntax is `(< OR > OR <= OR >=) <number>[percent sign] [ascending|descending]`
*
* @examples [">= 1"]
* @default ">= 1"
* @pattern ^\s*(?<opStr>>|>=|<|<=)\s*(?<value>\d+)\s*(?<percent>%?)\s*(?<duration>in\s+\d+\s*(days?|weeks?|months?|years?|hours?|minutes?|seconds?|milliseconds?))?\s*(?<extra>asc.*|desc.*)*$
* @pattern ^\s*(?<opStr>>|>=|<|<=)\s*(?<value>\d+)\s*(?<percent>%?)\s*(?<extra>asc.*|desc.*)*$
* */
count?: string;
/**
* How to test the Toolbox Notes or Mod Actions for this Author:
* How to test the notes for this Author:
*
* ### current
*
* Only the most recent note is checked for criteria
* Only the most recent note is checked for `type`
*
* ### total
*
* `count` comparison of mod actions/notes must be found within all history
* The `count` comparison of `type` must be found within all notes
*
* * EX `count: > 3` => Must have more than 3 notes of `type`, total
* * EX `count: <= 25%` => Must have 25% or less of notes of `type`, total
* * EX: `count: > 3 in 1 week` => Must have more than 3 notes within the last week
*
* ### consecutive
*
* The `count` **number** of mod actions/notes must be found in a row.
* The `count` **number** of `type` notes must be found in a row.
*
* You may also specify the time-based order in which to search the notes by specifying `ascending (asc)` or `descending (desc)` in the `count` value. Default is `descending`
*
@@ -113,129 +104,7 @@ export interface UserSubredditHistoryCriteria {
search?: 'current' | 'consecutive' | 'total'
}
export interface UserNoteCriteria extends UserSubredditHistoryCriteria {
/**
* User Note type key to search for
* @examples ["spamwarn"]
* */
type: string;
}
export interface ModActionCriteria extends UserSubredditHistoryCriteria {
type?: ModActionType | ModActionType[]
activityType?: MaybeActivityType | MaybeActivityType[]
referencesCurrentActivity?: boolean
}
export interface FullModActionCriteria extends Omit<ModActionCriteria, 'count'> {
type?: ModActionType[]
count?: GenericComparison
activityType?: MaybeActivityType[]
}
export interface ModNoteCriteria extends ModActionCriteria {
noteType?: ModUserNoteLabel | ModUserNoteLabel[]
note?: string | string[]
}
export interface FullModNoteCriteria extends FullModActionCriteria, Omit<ModNoteCriteria, 'note' | 'count' | 'type' | 'activityType'> {
noteType?: ModUserNoteLabel[]
note?: RegExp[]
}
const arrayableModNoteProps = ['activityType','noteType','note'];
export const asModNoteCriteria = (val: any): val is ModNoteCriteria => {
return val !== null && typeof val === 'object' && ('noteType' in val || 'note' in val);
}
export const toFullModNoteCriteria = (val: ModNoteCriteria): FullModNoteCriteria => {
const result = Object.entries(val).reduce((acc: FullModNoteCriteria, curr) => {
const [k,v] = curr;
if(v === undefined) {
return acc;
}
const rawVal = arrayableModNoteProps.includes(k) && !Array.isArray(v) ? [v] : v;
switch(k) {
case 'search':
acc.search = rawVal;
break;
case 'count':
acc.count = parseGenericValueComparison(rawVal);
break;
case 'activityType':
case 'noteType':
case 'referencesCurrentActivity':
acc[k] = rawVal;
break;
case 'note':
acc[k] = rawVal.map((x: string) => parseStringToRegexOrLiteralSearch(x))
}
return acc;
}, {});
result.type = ['NOTE'];
return result;
}
export interface ModLogCriteria extends ModActionCriteria {
action?: string | string[]
details?: string | string[]
description?: string | string[]
}
export interface FullModLogCriteria extends FullModActionCriteria, Omit<ModLogCriteria, 'action' | 'details' | 'description' | 'count' | 'type' | 'activityType'> {
action?: RegExp[]
details?: RegExp[]
description?: RegExp[]
}
const arrayableModLogProps = ['type','activityType','action','description','details', 'type'];
export const asModLogCriteria = (val: any): val is ModLogCriteria => {
return val !== null && typeof val === 'object' && !asModNoteCriteria(val) && ('action' in val || 'details' in val || 'description' in val || 'activityType' in val || 'search' in val || 'count' in val || 'type' in val);
}
export const toFullModLogCriteria = (val: ModLogCriteria): FullModLogCriteria => {
return Object.entries(val).reduce((acc: FullModLogCriteria, curr) => {
const [k,v] = curr;
if(v === undefined) {
return acc;
}
const rawVal = arrayableModLogProps.includes(k) && !Array.isArray(v) ? [v] : v;
switch(k) {
case 'search':
acc.search = rawVal;
break;
case 'count':
acc.count = parseGenericValueComparison(rawVal);
break;
case 'activityType':
case 'type':
case 'referencesCurrentActivity':
acc[k as keyof FullModLogCriteria] = rawVal;
break;
case 'action':
case 'description':
case 'details':
acc[k as keyof FullModLogCriteria] = rawVal.map((x: string) => parseStringToRegexOrLiteralSearch(x))
}
return acc;
}, {});
}
export const authorCriteriaProperties = ['name', 'flairCssClass', 'flairText', 'flairTemplate', 'isMod', 'userNotes', 'modActions', 'age', 'linkKarma', 'commentKarma', 'totalKarma', 'verified', 'shadowBanned', 'description', 'isContributor'];
export const authorCriteriaProperties = ['name', 'flairCssClass', 'flairText', 'flairTemplate', 'isMod', 'userNotes', 'age', 'linkKarma', 'commentKarma', 'totalKarma', 'verified', 'shadowBanned', 'description', 'isContributor'];
/**
* Criteria with which to test against the author of an Activity. The outcome of the test is based on:
@@ -249,11 +118,10 @@ export const authorCriteriaProperties = ['name', 'flairCssClass', 'flairText', '
* */
export interface AuthorCriteria {
/**
* A list of reddit usernames (case-insensitive) or regular expressions to match against. Do not include the "u/" prefix
*
* A list of reddit usernames (case-insensitive) to match against. Do not include the "u/" prefix
*
* EX to match against /u/FoxxMD and /u/AnotherUser use ["FoxxMD","AnotherUser"]
* @examples ["FoxxMD","AnotherUser", "/.*Foxx.\/*i"]
* @examples ["FoxxMD","AnotherUser"]
* */
name?: string[],
/**
@@ -291,8 +159,6 @@ export interface AuthorCriteria {
* */
userNotes?: UserNoteCriteria[]
modActions?: (ModNoteCriteria | ModLogCriteria)[]
/**
* Test the age of the Author's account (when it was created) against this comparison
*
@@ -362,35 +228,7 @@ export interface AuthorCriteria {
* Is the author an approved user (contributor)?
* */
isContributor?: boolean
}
/**
* When testing AuthorCriteria test properties in order of likelihood to require an API call to complete
* */
export const orderedAuthorCriteriaProps: (keyof AuthorCriteria)[] = [
'name', // never needs an api call, returned/cached with activity info
// none of these normally need api calls unless activity is a skeleton generated by CM (not normal)
// all are part of cached activity data
'flairCssClass',
'flairText',
'flairTemplate',
// usernotes are cached longer than author by default (5 min vs 60 seconds)
'userNotes',
// requires fetching/getting cached author.
// If fetching and user is shadowbanned none of the individual author data below will be retrievable either so always do this first
'shadowBanned',
// individual props require fetching/getting cached
'age',
'linkKarma',
'commentKarma',
'totalKarma',
'verified',
'description',
'isMod', // requires fetching mods for subreddit
'isContributor', // requires fetching contributors for subreddit
'modActions', // requires fetching mod notes/actions for author (shortest cache TTL)
];
} // properties calculated/derived by CM -- not provided as plain values by reddit
export interface ActivityState {
/**
* * true/false => test whether Activity is removed or not
@@ -410,56 +248,21 @@ export interface ActivityState {
approved?: boolean | ModeratorNames | ModeratorNames[] | ModeratorNameCriteria
score?: CompareValue
/**
* A string containing a comparison operator, a value to compare against, an (optional) report type filter, an (optional) qualifier for report reason, and an (optional) time constraint
* A string containing a comparison operator and a value to compare against
*
* The syntax is `(< OR > OR <= OR >=) number[%] [type] [reasonQualifier] [timeValue] [timeUnit]`
*
* If only comparison and number is given then defaults to TOTAL reports on an Activity.
* The syntax is `(< OR > OR <= OR >=) <number>`
*
* * EX `> 2` => greater than 2 total reports
*
* Type (optional) determines which type of reports to look at:
* Defaults to TOTAL reports on an Activity. Suffix the value with the report type to check that type:
*
* * `mod` -- mod reports
* * EX `> 3 mod` => greater than 3 mod reports
* * `user` -- user reports
* * EX `> 3 user` => greater than 3 user reports
* * EX `> 3 mod` => greater than 3 mod reports
* * EX `>= 1 user` => greater than 1 user report
*
* Report reason qualifiers can be:
*
* * enclosed double or single quotes -- report reason contains
* * EX `> 1 "misinformation" => greater than 1 report with reason containing "misinformation"
* * enclosed in backslashes -- match regex
* * EX `> 1 \harassment towards .*\` => greater than 1 report with reason matching regex \harassment towards .*\
*
* Type and reason qualifiers can be used together:
*
* EX `> 2 user "misinformation" => greater than 2 user reports with reasons containing "misinformation"
*
* The time constraint filter reports created between NOW and [timeConstraint] in the past:
*
* * `> 3 in 30 minutes` => more than 3 reports created between NOW and 30 minutes ago
* * `> 2 user "misinformation" in 2 hours` => more than 2 user reports containing "misinformation" created between NOW and 2 hours ago
*
* @pattern ^\s*(>|>=|<|<=)\s*(\d+)(\s*%)?(\s+(?:mods?|users?))?(\s+(?:["'].*["']|\/.*\/))?.*(\d+)?\s*(days?|weeks?|months?|years?|hours?|minutes?|seconds?|milliseconds?)?\s*$
* @pattern ^\s*(>|>=|<|<=)\s*(\d+)\s*(%?)(.*)$
* */
reports?: string
reports?: CompareValue
age?: DurationComparor
/**
* A relative datetime description to match the date the Activity was created
*
* May be either:
*
* * day of the week (monday, tuesday, etc...)
* * cron expression IE `* * 15 *`
*
* See https://crontab.guru/ for generating expressions
*
* https://regexr.com/6u3cc
*
* */
createdOn?: RelativeDateTimeMatch | RelativeDateTimeMatch[]
/**
* Test whether the activity is present in dispatched/delayed activities
*
@@ -474,7 +277,7 @@ export interface ActivityState {
dispatched?: boolean | string | string[]
// cant use ActivitySource | ActivitySource[] here because of issues with generating json schema, see ActivitySource comments
// can use ActivitySource | ActivitySource[] here because of issues with generating json schema, see ActivitySource comments
/**
* Test where the current activity was sourced from.
*
@@ -489,33 +292,6 @@ export interface ActivityState {
*
* */
source?: string | string[]
/**
* * If `true` then passes if ANY flair
* * If `false` then passes if NO flair
* * If string or list of strings then text is matched, case-insensitive. String may also be a regular expression enclosed in forward slashes.
* */
authorFlairText?: boolean | string | string[]
/**
* * If `true` then passes if ANY flair
* * If `false` then passes if NO flair
* * If string or list of strings then template id is matched, case-insensitive. String may also be a regular expression enclosed in forward slashes.
* */
authorFlairTemplateId?: boolean | string | string[]
/**
* * If `true` then passes if ANY class
* * If `false` then passes if NO class
* * If string or list of strings then class is matched, case-insensitive. String may also be a regular expression enclosed in forward slashes.
* */
authorFlairCssClass?: boolean | string | string[]
/**
* * If `true` then passes if ANY color
* * If `false` then passes if NO color
* * If string or list of strings then color is matched, case-insensitive, without #. String may also be a regular expression enclosed in forward slashes.
* */
authorFlairBackgroundColor?: boolean | string | string[]
}
/**
@@ -538,22 +314,13 @@ export interface SubmissionState extends ActivityState {
/**
* * If `true` then passes if flair has ANY text
* * If `false` then passes if flair has NO text
* * If string or list of strings then text is matched, case-insensitive. String may also be a regular expression enclosed in forward slashes.
* */
link_flair_text?: boolean | string | string[]
/**
* * If `true` then passes if flair has ANY css
* * If `false` then passes if flair has NO css
* * If string or list of strings then class is matched, case-insensitive. String may also be a regular expression enclosed in forward slashes.
* */
link_flair_css_class?: boolean | string | string[]
/**
* * If `true` then passes if ANY color
* * If `false` then passes if NO color
* * If string or list of strings then color is matched, case-insensitive, without #. String may also be a regular expression enclosed in forward slashes.
* */
link_flair_background_color?: boolean | string | string[]
/**
* * If `true` then passes if there is ANY flair template id
* * If `false` then passes if there is NO flair template id
@@ -563,28 +330,6 @@ export interface SubmissionState extends ActivityState {
* Is the submission a reddit-hosted image or video?
* */
isRedditMediaDomain?: boolean
/**
* Compare the upvote ratio for this Submission, expressed as a whole number
*
* Can be either a comparison string or a number. If a number then CM assumes upvote ratio must be greater than or equal to this.
*
* Example:
*
* * `< 90` => less than 90% upvoted
* * 45 => greater than or equal to 45% upvoted
* */
upvoteRatio?: number | CompareValue
}
export const cmToSnoowrapActivityMap: Record<string, keyof (Submission & Comment)> = {
authorFlairText: 'author_flair_text',
flairText: 'author_flair_text',
authorFlairTemplateId: 'author_flair_template_id',
authorFlairCssClass: 'author_flair_css_class',
authorFlairBackgroundColor: 'author_flair_background_color',
flairTemplate: 'link_flair_template_id',
flairCssClass: 'author_flair_css_class',
}
export const cmActivityProperties = ['submissionState', 'score', 'reports', 'removed', 'deleted', 'filtered', 'age', 'title'];

View File

@@ -1,45 +0,0 @@
import {ConfigFormat} from "./Atomic";
export interface IncludesData {
/**
* The special-form path to the config fragment to retrieve.
*
* If value starts with `wiki:` then the proceeding value will be used to get a wiki page from the current subreddit
*
* * EX `wiki:botconfig/mybot` tries to get `https://reddit.com/r/currentSubreddit/wiki/botconfig/mybot`
*
* If the value starts with `wiki:` and ends with `|someValue` then `someValue` will be used as the base subreddit for the wiki page
*
* * EX `wiki:replytemplates/test|ContextModBot` tries to get `https://reddit.com/r/ContextModBot/wiki/replytemplates/test`
*
* If the value starts with `url:` then the value is fetched as an external url and expects raw text returned
*
* * EX `url:https://pastebin.com/raw/38qfL7mL` tries to get the text response of `https://pastebin.com/raw/38qfL7mL`
* */
path: string
/**
* An unused hint about the content type. Not implemented yet
* */
type?: ConfigFormat
/**
* Control caching for the config fragment.
*
* If not specified the value for `wikiTTL` will be used
*
* * If true then value is cached forever
* * If false then value is never cached
* * If a number then the number of seconds to cache value
* * If 'response' then CM will attempt to use Cache-Control or Expires headers from the response to determine how long to cache the value
* */
ttl?: number | boolean | 'response'
}
export type IncludesUrl = `url:${string}`;
export type IncludesWiki = `wiki:${string}`;
export type IncludesString = IncludesUrl | IncludesWiki;
export type IncludesType = string | IncludesData;
export const asIncludesData = (val: any): val is IncludesData => {
return val !== null && typeof val === 'object' && 'path' in val;
}

View File

@@ -1,7 +1,6 @@
import {Comment, Submission} from "snoowrap/dist/objects";
export type ActivityType = 'submission' | 'comment';
export type MaybeActivityType = ActivityType | false;
export type FullNameTypes = ActivityType | 'user' | 'subreddit' | 'message';
export interface RedditThing {
@@ -31,76 +30,3 @@ export interface CachedFetchedActivitiesResult {
export interface FetchedActivitiesResult extends CachedFetchedActivitiesResult {
post: SnoowrapActivity[]
}
export type ReportType = 'mod' | 'user';
export interface Report {
reason: string
type: ReportType
author?: string
snoozed: boolean
canSnooze: boolean
}
export type RawRedditUserReport = [
string, // reason
number, // number of reports with this reason
boolean, // is report snoozed
boolean // can the reports be snoozed
];
export type RawRedditModReport = [string, string];
export const activityReports = (activity: SnoowrapActivity): Report[] => {
const reports: Report[] = [];
for(const r of (activity.user_reports as unknown as RawRedditUserReport[])) {
const report = {
reason: r[0],
type: 'user' as ReportType,
snoozed: r[2],
canSnooze: r[3]
};
for(let i = 0; i < r[1]; i++) {
reports.push(report);
}
}
for(const r of (activity.mod_reports as unknown as RawRedditModReport[])) {
reports.push({
reason: r[0],
type: 'mod' as ReportType,
author: r[1],
snoozed: false,
canSnooze: false
})
}
return reports;
}
export interface RawSubredditRemovalReasonData {
data: {
[key: string]: SubredditRemovalReason
},
order: [string]
}
export interface SubredditRemovalReason {
message: string
id: string,
title: string
}
export interface SubredditActivityAbsoluteBreakdown {
count: number
name: string
}
export interface SubredditActivityBreakdown extends SubredditActivityAbsoluteBreakdown {
percent: number
}
export interface SubredditActivityBreakdownByType {
total: SubredditActivityBreakdown[]
submission: SubredditActivityBreakdown[]
comment: SubredditActivityBreakdown[]
}

View File

@@ -1,12 +1,10 @@
import {StructuredRunnableBase} from "./Runnable";
import {RuleSetConfigObject} from "../../Rule/RuleSet";
import {RuleSetObjectJson} from "../../Rule/RuleSet";
import {RuleObjectJsonTypes} from "../types";
import {IncludesData} from "./Includes";
export type RuleConfigData = RuleObjectJsonTypes | string | IncludesData;
export type RuleConfigHydratedData = Exclude<RuleConfigData, IncludesData>
export type RuleConfigObject = Exclude<RuleConfigHydratedData, string>
export type StructuredRuleConfigObject = Omit<RuleConfigObject, 'authorIs' | 'itemIs'> & StructuredRunnableBase
export type StructuredRuleSetConfigObject = Omit<RuleSetConfigObject, 'rules'> & {
rules: StructuredRuleConfigObject[]
export type RuleJson = RuleObjectJsonTypes | string;
export type RuleObjectJson = Exclude<RuleJson, string>
export type StructuredRuleObjectJson = Omit<RuleObjectJson, 'authorIs' | 'itemIs'> & StructuredRunnableBase
export type StructuredRuleSetObjectJson = Omit<RuleSetObjectJson, 'rules'> & {
rules: StructuredRuleObjectJson[]
}

View File

@@ -10,16 +10,11 @@ export interface RunnableBaseOptions extends Omit<RunnableBaseJson, 'itemIs' | '
authorIs?: MinimalOrFullFilter<AuthorCriteria>
}
export interface StructuredRunnableBase extends RunnableBaseJson {
export interface StructuredRunnableBase {
itemIs?: MinimalOrFullFilter<TypedActivityState>
authorIs?: MinimalOrFullFilter<AuthorCriteria>
}
export interface TypedStructuredRunnableBase<T> extends TypedRunnableBaseData<T> {
itemIs?: MinimalOrFullFilter<T>
authorIs?: MinimalOrFullFilter<AuthorCriteria>
}
export interface RunnableBaseJson {
/**
* A list of criteria to test the state of the `Activity` against before running the check.
@@ -36,10 +31,3 @@ export interface RunnableBaseJson {
* */
authorIs?: MinimalOrFullFilterJson<AuthorCriteria>
}
export interface TypedRunnableBaseData<T extends TypedActivityState> extends RunnableBaseJson {
/**
* If present then these Author criteria are checked before running the Check. If criteria fails then the Check will fail.
* */
authorIs?: MinimalOrFullFilterJson<AuthorCriteria>
}

View File

@@ -5,12 +5,12 @@ import {SentimentIntensityAnalyzer} from 'vader-sentiment';
import wink from 'wink-sentiment';
import {SnoowrapActivity} from "./Infrastructure/Reddit";
import {
asGenericComparison, comparisonTextOp,
asGenericComparison,
GenericComparison,
parseGenericValueComparison,
RangedComparison
} from "./Infrastructure/Comparisons";
import {asSubmission, between, formatNumber} from "../util";
import {asSubmission, between, comparisonTextOp, formatNumber} from "../util";
import {CMError, MaybeSeriousErrorWithCause} from "../Utils/Errors";
import InvalidRegexError from "../Utils/InvalidRegexError";
import {StringOperator} from "./Infrastructure/Atomic";

View File

@@ -5,7 +5,6 @@ import {DatabaseMigrationOptions} from "./interfaces";
import {copyFile} from "fs/promises";
import {constants} from "fs";
import {ErrorWithCause} from "pony-cause";
import {CMError} from "../Utils/Errors";
export interface ExistingTable {
table: Table
@@ -119,10 +118,9 @@ export class MigrationService {
try {
await this.backupDatabase();
continueBCBackedup = true;
} catch (err: any) {
if(!(err instanceof CMError) || !err.logged) {
this.dbLogger.error(err, {leaf: 'Backup'});
}
} catch (err) {
// @ts-ignore
this.dbLogger.error(err, {leaf: 'Backup'});
}
} else {
this.dbLogger.info('Configuration DID NOT specify migrations may be executed if automated backup is successful. Will not try to create a backup.');
@@ -156,34 +154,25 @@ YOU SHOULD BACKUP YOUR EXISTING DATABASE BEFORE CONTINUING WITH MIGRATIONS.`);
async backupDatabase() {
try {
let location: string | undefined;
const canBackup = ['sqljs','better-sqlite3'].includes(this.database.options.type);
if(canBackup) {
if(this.database.options.type === 'sqljs') {
location = this.database.options.location === ':memory:' ? undefined : this.database.options.location;
} else {
location = this.database.options.database === ':memory:' || (typeof this.database.options.database !== 'string') ? undefined : this.database.options.database;
}
}
if (canBackup && location !== undefined) {
if (this.database.options.type === 'sqljs' && this.database.options.location !== undefined) {
try {
const ts = Date.now();
const backupLocation = `${location}.${ts}.bak`
const backupLocation = `${this.database.options.location}.${ts}.bak`
this.dbLogger.info(`Detected sqljs (sqlite) database. Will try to make a backup at ${backupLocation}`, {leaf: 'Backup'});
await copyFile(location, backupLocation, constants.COPYFILE_EXCL);
await copyFile(this.database.options.location, backupLocation, constants.COPYFILE_EXCL);
this.dbLogger.info('Successfully created backup!', {leaf: 'Backup'});
} catch (err: any) {
throw new ErrorWithCause('Cannot make an automated backup of your configured database.', {cause: err});
}
} else {
let msg = 'Cannot make an automated backup of your configured database.';
if (!canBackup) {
msg += ' Only SQlite (sqljs or better-sqlite3 database type) is implemented for automated backups right now, sorry :( You will need to manually backup your database.';
if (this.database.options.type !== 'sqljs') {
msg += ' Only SQlite (sqljs database type) is implemented for automated backups right now, sorry :( You will need to manually backup your database.';
} else {
// TODO don't throw for this??
msg += ' Database location is not defined (probably in-memory).';
}
throw new CMError(msg, {logged: true});
throw new Error(msg);
}
} catch (e: any) {
this.dbLogger.error(e, {leaf: 'Backup'});

View File

@@ -1,92 +0,0 @@
import {QueryRunner, TableIndex} from "typeorm";
/**
* Boilerplate for creating generic index
* */
export const index = (prefix: string, columns: string[], unique = true) => new TableIndex({
name: `IDX_${unique ? 'UN_' : ''}${prefix}_${columns.join('-')}`,
columnNames: columns,
isUnique: unique,
});
/**
* Create index on id column
* */
export const idIndex = (prefix: string, unique: boolean) => index(prefix, ['id'], unique);
/**
* Boilerplate primary key column for random ID
* */
export const randomIdColumn = () => ({
name: 'id',
type: 'varchar',
length: '20',
isPrimary: true,
isUnique: true,
});
/**
* Create a time data column based on database type
* */
export const timeAtColumn = (columnName: string, dbType: string, nullable?: boolean) => {
const dbSpecifics = dbType === 'postgres' ? {
type: 'timestamptz'
} : {
type: 'datetime',
// required to get millisecond precision on mysql/mariadb
// https://mariadb.com/kb/en/datetime/
// https://dev.mysql.com/doc/refman/8.0/en/fractional-seconds.html
length: '3',
}
return {
name: columnName,
isNullable: nullable ?? false,
...dbSpecifics
}
}
export const createdAtColumn = (type: string) => timeAtColumn('createdAt', type);
const updatedAtColumn = (type: string) => timeAtColumn('updatedAt', type);
const createdUpdatedAtColumns = (type: string) => [
timeAtColumn('createdAt', type),
timeAtColumn('updatedAt', type)
];
export const createdAtIndex = (prefix: string) => index(prefix, ['createdAt'], false);
const updatedAtIndex = (prefix: string) => index(prefix, ['updatedAt'], false);
const createdUpdatedAtIndices = (prefix: string) => {
return [
createdAtIndex(prefix),
updatedAtIndex(prefix)
]
}
/**
* Boilerplate for filter (itemIs, authorIs) FK column -- uses FK is filter ID
* */
const filterColumn = (name: string) => ({
name,
type: 'varchar',
length: '20',
isNullable: true
});
const authorIsColumn = () => filterColumn('authorIs');
const itemIsColumn = () => filterColumn('itemIs');
export const filterColumns = () => ([authorIsColumn(), itemIsColumn()]);
const authorIsIndex = (prefix: string) => index(prefix, ['authorIs']);
const itemIsIndex = (prefix: string) => index(prefix, ['itemIs']);
export const filterIndices = (prefix: string) => {
return [
authorIsIndex(prefix),
itemIsIndex(prefix)
]
}
export const tableHasData = async (runner: QueryRunner, name: string): Promise<boolean | null> => {
const countRes = await runner.query(`select count(*) from ${name}`);
let hasRows = null;
if (Array.isArray(countRes) && countRes[0] !== null) {
const {
'count(*)': count
} = countRes[0] || {};
hasRows = count !== 0;
}
return hasRows;
}

View File

@@ -1,12 +1,86 @@
import {MigrationInterface, QueryRunner, Table, TableIndex} from "typeorm";
import {
createdAtColumn,
createdAtIndex,
filterColumns,
filterIndices,
randomIdColumn,
timeAtColumn
} from "../MigrationUtil";
import {MigrationInterface, QueryRunner, Table, TableIndex, TableColumn, TableForeignKey} from "typeorm";
const randomIdColumn = () => ({
name: 'id',
type: 'varchar',
length: '20',
isPrimary: true,
isUnique: true,
});
const timeAtColumn = (columnName: string, dbType: string, nullable?: boolean) => {
const dbSpecifics = dbType === 'postgres' ? {
type: 'timestamptz'
} : {
type: 'datetime',
// required to get millisecond precision on mysql/mariadb
// https://mariadb.com/kb/en/datetime/
// https://dev.mysql.com/doc/refman/8.0/en/fractional-seconds.html
length: '3',
}
return {
name: columnName,
isNullable: nullable ?? false,
...dbSpecifics
}
}
const createdAtColumn = (type: string) => timeAtColumn('createdAt', type);
const updatedAtColumn = (type: string) => timeAtColumn('updatedAt', type);
const createdUpdatedAtColumns = (type: string) => [
timeAtColumn('createdAt', type),
timeAtColumn('updatedAt', type)
];
const createdAtIndex = (prefix: string) => new TableIndex({
name: `IDX_${prefix}_createdAt`,
columnNames: ['createdAt']
});
const updatedAtIndex = (prefix: string) => new TableIndex({
name: `IDX_${prefix}_updatedAt`,
columnNames: ['updatedAt']
})
const createdUpdatedAtIndices = (prefix: string) => {
return [
createdAtIndex(prefix),
updatedAtIndex(prefix)
]
}
const filterColumn = (name: string) => ({
name,
type: 'varchar',
length: '20',
isNullable: true
});
const authorIsColumn = () => filterColumn('authorIs');
const itemIsColumn = () => filterColumn('itemIs');
const filterColumns = () => ([authorIsColumn(), itemIsColumn()]);
const authorIsIndex = (prefix: string) => new TableIndex({
name: `IDX_${prefix}_authorIs`,
columnNames: ['authorIs'],
isUnique: true,
});
const itemIsIndex = (prefix: string) => new TableIndex({
name: `IDX_${prefix}_itemIs`,
columnNames: ['itemIs'],
isUnique: true
});
const filterIndices = (prefix: string) => {
return [
authorIsIndex(prefix),
itemIsIndex(prefix)
]
}
export class initApi1642180264563 implements MigrationInterface {
name = 'initApi1642180264563'

View File

@@ -1,90 +0,0 @@
import {MigrationInterface, QueryRunner, Table} from "typeorm"
import {idIndex, index} from "../MigrationUtil";
export class indexes1653586738904 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
queryRunner.connection.logger.logSchemaBuild('Starting Index Add/Update Migration');
queryRunner.connection.logger.logSchemaBuild('IF YOU HAVE A LARGE DATABASE THIS MAY TAKE SEVERAL MINUTES! DO NOT STOP CONTEXTMOD WHILE MIGRATION IS IN PROGRESS!');
// unique ids due to random id
const uniqueIdTableNames = [
'Manager',
'CMEvent',
'FilterResult',
'FilterCriteriaResult',
'RunnableResult',
'RulePremise',
'RuleResult',
'RuleSetResult',
'ActionPremise',
'ActionResult',
'CheckResult',
'RunResult'
];
for (const tableName of uniqueIdTableNames) {
const cmTable = await queryRunner.getTable(tableName);
await queryRunner.createIndex(cmTable as Table, idIndex(tableName, true));
}
// additional indexes
const actSource = await queryRunner.getTable('ActivitySource');
await queryRunner.createIndex(actSource as Table, idIndex('ActivitySource', false));
const event = await queryRunner.getTable('CMEvent');
await queryRunner.createIndices(event as Table, [index('CMEvent', ['activity_id'], false)]);
// FilterCriteriaResult criteriaId filterResultId
const fcrTable = await queryRunner.getTable('FilterCriteriaResult');
await queryRunner.createIndices(fcrTable as Table, [
index('FilterCriteriaResult', ['criteriaId'], false),
index('FilterCriteriaResult', ['filterResultId'], false)
]);
// FilterCriteria id
const fcTable = await queryRunner.getTable('FilterCriteria');
await queryRunner.createIndices(fcTable as Table, [
idIndex('FilterCriteriaResult', false),
]);
// RunnableResult resultId runnableId
const rrTable = await queryRunner.getTable('RunnableResult');
await queryRunner.createIndices(rrTable as Table, [
index('RunnableResult', ['resultId'], false),
index('RunnableResult', ['runnableId'], false)
]);
// ActionResult checkResultId premiseId
const arTable = await queryRunner.getTable('ActionResult');
await queryRunner.createIndices(arTable as Table, [
index('ActionResult', ['checkResultId'], false),
index('ActionResult', ['premiseId'], false)
]);
// CheckResult runId
const crTable = await queryRunner.getTable('CheckResult');
await queryRunner.createIndices(crTable as Table, [
index('CheckResult', ['runId'], false),
]);
// RunResult eventId
const runResTable = await queryRunner.getTable('RunResult');
await queryRunner.createIndices(runResTable as Table, [
index('RunResult', ['eventId'], false),
]);
}
public async down(queryRunner: QueryRunner): Promise<void> {
}
}

View File

@@ -1,62 +0,0 @@
import {MigrationInterface, QueryRunner, Table, TableIndex} from "typeorm"
import {createdAtColumn, createdAtIndex, idIndex, index, randomIdColumn} from "../MigrationUtil";
export class reportTracking1657632517934 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const dbType = queryRunner.connection.driver.options.type;
await queryRunner.createTable(
new Table({
name: 'ActivityReport',
columns: [
randomIdColumn(),
{
name: 'activityId',
type: 'varchar',
length: '20',
isNullable: false
},
{
name: 'reason',
type: 'varchar',
length: '500',
isNullable: false
},
{
name: 'type',
type: 'varchar',
length: '200',
isNullable: false
},
{
name: 'author',
type: 'varchar',
length: '100',
isNullable: true
},
{
name: 'granularity',
type: 'int',
isNullable: false
},
createdAtColumn(dbType),
],
indices: [
idIndex('ActivityReport', true),
index('ActivityReport', ['activityId'], false),
index('ActivityReportReason', ['reason'], false),
createdAtIndex('report'),
]
}),
true,
true,
true
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
}
}

View File

@@ -1,50 +0,0 @@
import {MigrationInterface, QueryRunner, Table} from "typeorm"
import {createdAtColumn, createdAtIndex, idIndex, index, randomIdColumn, timeAtColumn} from "../MigrationUtil";
export class Guests1658930394548 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const dbType = queryRunner.connection.driver.options.type;
await queryRunner.createTable(
new Table({
name: 'Guests',
columns: [
randomIdColumn(),
{
name: 'authorName',
type: 'varchar',
length: '200',
isNullable: false,
},
{
name: 'type',
type: 'varchar',
isNullable: false,
length: '50'
},
{
name: 'guestOfId',
type: 'varchar',
length: '20',
isNullable: true
},
timeAtColumn('expiresAt', dbType, true),
createdAtColumn(dbType),
],
indices: [
idIndex('Guests', true),
createdAtIndex('guests'),
index('guest', ['expiresAt'], false)
]
}),
true,
true,
true
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
}
}

View File

@@ -1,145 +0,0 @@
import {MigrationInterface, QueryRunner, Table, TableColumn} from "typeorm"
import {createdAtColumn, createdAtIndex, idIndex, index, randomIdColumn, tableHasData, timeAtColumn} from "../MigrationUtil";
export class invites1660228987769 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const dbType = queryRunner.connection.driver.options.type;
await queryRunner.createTable(
new Table({
name: 'SubredditInvite',
columns: [
{
name: 'id',
type: 'varchar',
length: '255',
isPrimary: true,
},
{
name: 'botId',
type: 'varchar',
length: '20',
isNullable: false
},
{
name: 'subreddit',
type: 'varchar',
length: '255',
isNullable: false
},
{
name: 'guests',
type: 'text',
isNullable: true
},
{
name: 'initialConfig',
type: 'text',
isNullable: true
},
createdAtColumn(dbType),
timeAtColumn('expiresAt', dbType, true)
],
}),
true,
true,
true
);
if (await queryRunner.hasTable('Invite')) {
await queryRunner.renameTable('Invite', 'BotInvite');
const table = await queryRunner.getTable('BotInvite') as Table;
await queryRunner.addColumns(table, [
new TableColumn({
name: 'initialConfig',
type: 'text',
isNullable: true
}),
new TableColumn({
name: 'guests',
type: 'text',
isNullable: true
})
]);
queryRunner.connection.logger.logSchemaBuild(`Table 'Invite' has been renamed 'BotInvite'. If there are existing rows on this table they will need to be recreated.`);
} else {
await queryRunner.createTable(
new Table({
name: 'BotInvite',
columns: [
{
name: 'id',
type: 'varchar',
length: '255',
isPrimary: true,
},
{
name: 'clientId',
type: 'varchar',
length: '255',
},
{
name: 'clientSecret',
type: 'varchar',
length: '255',
},
{
name: 'redirectUri',
type: 'text',
},
{
name: 'creator',
type: 'varchar',
length: '255',
},
{
name: 'permissions',
type: 'text'
},
{
name: 'instance',
type: 'varchar',
length: '255',
isNullable: true
},
{
name: 'overwrite',
type: 'boolean',
isNullable: true,
},
{
name: 'subreddits',
type: 'text',
isNullable: true
},
{
name: 'guests',
type: 'text',
isNullable: true
},
{
name: 'initialConfig',
type: 'text',
isNullable: true
},
createdAtColumn(dbType),
timeAtColumn('expiresAt', dbType, true)
],
}),
true,
true,
true
);
}
}
public async down(queryRunner: QueryRunner): Promise<void> {
}
}

View File

@@ -1,15 +0,0 @@
import { MigrationInterface, QueryRunner } from "typeorm"
import {ActionType} from "../../../Entities/ActionType";
export class submission1661183583080 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.manager.getRepository(ActionType).save([
new ActionType('submission')
]);
}
public async down(queryRunner: QueryRunner): Promise<void> {
}
}

View File

@@ -1,22 +0,0 @@
import {MigrationInterface, QueryRunner, Table, TableColumn} from "typeorm"
export class subredditInvite1663001719622 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const table = await queryRunner.getTable('SubredditInvite') as Table;
await queryRunner.addColumns(table, [
new TableColumn( {
name: 'messageId',
type: 'varchar',
length: '200',
isUnique: true,
isNullable: true
}),
]);
}
public async down(queryRunner: QueryRunner): Promise<void> {
}
}

View File

@@ -1,15 +0,0 @@
import { MigrationInterface, QueryRunner } from "typeorm"
import {RuleType} from "../../../Entities/RuleType";
export class mhs1663609045418 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.manager.getRepository(RuleType).save([
new RuleType('mhs'),
]);
}
public async down(queryRunner: QueryRunner): Promise<void> {
}
}

View File

@@ -1,33 +0,0 @@
import {MigrationInterface, QueryRunner} from "typeorm"
import {tableHasData} from "../MigrationUtil";
export class removeInvites1660588028346 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const dbType = queryRunner.connection.driver.options.type;
if (dbType === 'sqljs' && await queryRunner.hasTable('Invite')) {
// const countRes = await queryRunner.query('select count(*) from Invite');
// let hasNoRows = null;
// if (Array.isArray(countRes) && countRes[0] !== null) {
// const {
// 'count(*)': count
// } = countRes[0] || {};
// hasNoRows = count === 0;
// }
const hasRows = await tableHasData(queryRunner, 'Invite');
if (hasRows === false) {
await queryRunner.dropTable('Invite');
} else {
let prefix = hasRows === null ? `Could not determine if SQL.js 'web' database had the table 'Invite' --` : `SQL.js 'web' database had the table 'Invite' and it is not empty --`
queryRunner.connection.logger.logSchemaBuild(`${prefix} This table is being replaced by 'BotInvite' table in 'app' database. If you have existing invites you will need to recreate them.`);
}
}
}
public async down(queryRunner: QueryRunner): Promise<void> {
}
}

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