Compare commits

..

20 Commits

Author SHA1 Message Date
FoxxMD
fc51928054 Merge branch 'edge' 2022-02-02 16:59:56 -05:00
FoxxMD
c07276a3be fix(logging): Fix typo in error transform 2022-02-01 13:13:27 -05:00
FoxxMD
4a2297f5cd docs: Add github sponsor link 2022-02-01 12:01:34 -05:00
FoxxMD
f8967d55c4 feat(repeat): Use newer text comparison technique to improve repeat detection
* Use same technique as repost rule which has high accuracy and let false-positives
* Implement ability to see similarity score, case sensitivity, and text transformations
2022-01-31 14:08:21 -05:00
FoxxMD
e2590e50f8 Merge branch 'edge' 2022-01-28 17:27:51 -05:00
FoxxMD
7e8745d226 fix(polling): Fix shared polling behavior for nanny mode changes
* On hard limit stop shared streams
* On nanny mode turned off restart any stopped shared streams
2022-01-27 16:49:03 -05:00
FoxxMD
e2efc85833 fix(polling): Fix running state not changed on error
* Set running to false when error is caught. Was not caught on last stream refactor which changed polling behavior to end if any error is caught rather than waiting for external source to clear interval
* Add debugging/error messages on polling start/stop
2022-01-27 16:47:43 -05:00
FoxxMD
41038b9bcd feat(logging): Implement richer errors everywhere
* Use ErrorWithCause so we can get and print a chain of error causes
* Make reddit error response in stack trace more readable by replacing them with a "translated" parent response and add them as the cause
* Properly handle error formatting for winston by looking at shape of log object for error rather than testing instanceof (see comments in errorAwareFormat)
* Fix formatting in web interface for log lines with white-space pre css and properly splitting timestamp from rest of the message
2022-01-27 16:27:03 -05:00
FoxxMD
9fe8c9568c refactor: Move SimpleError into main Errors module 2022-01-27 11:48:23 -05:00
FoxxMD
9614f7a209 refactor(logging): Implement snoowrap errors "the right way" and implement consolidated logging function
* Implement declaration file for snoowrap errors so they can be imported directly
* Implement logging function to handle boilerplate for known error responses (reddit HTTP response, rate limit, etc.)
2022-01-27 11:43:39 -05:00
FoxxMD
8dbaaf6798 fix(logging): Defaults for log file dir 2022-01-26 12:28:56 -05:00
FoxxMD
c14ad6cb76 feat(logging): Implement separate logging options for each transport type
* Add properties for file, console, and stream in logging object of operator config
* Each property inherits a (useful) subset of winston transport options
2022-01-26 12:09:03 -05:00
FoxxMD
adda280dd3 fix(logging): Fix parsing log dir
* Correct else condition to use log dir when value is not true
* Set level to 'debug' on init logger if no value is provided to help with debugging
2022-01-26 10:27:01 -05:00
FoxxMD
15fd47bdb4 fix(polling): Correct typings for stream getter and check isFinished for Listing 2022-01-26 10:11:06 -05:00
FoxxMD
78b6d8b7b6 feat(polling): Add debug messages when streams are stopped 2022-01-26 10:00:09 -05:00
FoxxMD
61bc63ccc5 fix(polling): Emit config change event to bot only after manager has rebuilt polling 2022-01-26 09:50:30 -05:00
FoxxMD
05df8b7fe2 fix(polling): Use manager eventState to control shared stream callback rather than removing callback when events are stopped
Should prevent edge cases where shared streams are re-parsed while managers are stopped (hard limit) and then removed due to there being no callbacks
2022-01-25 18:07:15 -05:00
FoxxMD
3cb7dffb90 fix(polling): Prevent endless loop when trying to enforce continuity on a stream with no items returned 2022-01-25 09:25:59 -05:00
FoxxMD
d0aafc34b9 feat(remove): Add option to mark activity as spam 2022-01-21 13:03:05 -05:00
FoxxMD
d2e1b5019f chore: Update packages 2022-01-21 13:02:31 -05:00
30 changed files with 1367 additions and 655 deletions

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

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

677
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -68,6 +68,7 @@
"passport-custom": "^1.1.1",
"passport-jwt": "^4.0.0",
"pixelmatch": "^5.2.1",
"pony-cause": "^1.1.1",
"pretty-print-json": "^1.0.3",
"safe-stable-stringify": "^1.1.1",
"snoostorm": "^1.5.2",
@@ -114,7 +115,7 @@
"@types/triple-beam": "^1.3.2",
"ts-essentials": "^9.1.2",
"ts-json-schema-generator": "^0.93.0",
"typescript-json-schema": "^0.50.1"
"typescript-json-schema": "~0.53"
},
"optionalDependencies": {
"sharp": "^0.29.1"

View File

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

View File

@@ -1,4 +1,4 @@
import {ActionJson, ActionConfig} from "./index";
import {ActionJson, ActionConfig, ActionOptions} from "./index";
import Action from "./index";
import Snoowrap, {Comment, Submission} from "snoowrap";
import {RuleResult} from "../Rule";
@@ -6,10 +6,20 @@ import {activityIsRemoved} from "../Utils/SnoowrapUtils";
import {ActionProcessResult} from "../Common/interfaces";
export class RemoveAction extends Action {
spam: boolean;
getKind() {
return 'Remove';
}
constructor(options: RemoveOptions) {
super(options);
const {
spam = false,
} = options;
this.spam = spam;
}
async process(item: Comment | Submission, ruleResults: RuleResult[], runtimeDryrun?: boolean): Promise<ActionProcessResult> {
const dryRun = runtimeDryrun || this.dryRun;
const touchedEntities = [];
@@ -22,9 +32,12 @@ export class RemoveAction extends Action {
result: 'Item is already removed',
}
}
if (this.spam) {
this.logger.verbose('Marking as spam on removal');
}
if (!dryRun) {
// @ts-ignore
await item.remove();
await item.remove({spam: this.spam});
touchedEntities.push(item);
}
@@ -36,13 +49,16 @@ export class RemoveAction extends Action {
}
}
export interface RemoveActionConfig extends ActionConfig {
export interface RemoveOptions extends RemoveActionConfig, ActionOptions {
}
export interface RemoveActionConfig extends ActionConfig {
spam?: boolean
}
/**
* Remove the Activity
* */
export interface RemoveActionJson extends RemoveActionConfig, ActionJson {
kind: 'remove'
kind: 'remove'
}

View File

@@ -16,10 +16,10 @@ import {
} from "../Common/interfaces";
import {
createRetryHandler,
formatNumber,
formatNumber, getExceptionMessage,
mergeArr,
parseBool,
parseDuration,
parseDuration, parseMatchMessage,
parseSubredditName, RetryOptions,
sleep,
snooLogWrapper
@@ -30,8 +30,8 @@ import {CommentStream, ModQueueStream, SPoll, SubmissionStream, UnmoderatedStrea
import {BotResourcesManager} from "../Subreddit/SubredditResources";
import LoggedError from "../Utils/LoggedError";
import pEvent from "p-event";
import SimpleError from "../Utils/SimpleError";
import {isRateLimitError, isStatusError} from "../Utils/Errors";
import {SimpleError, isRateLimitError, isRequestError, isScopeError, isStatusError, CMError} from "../Utils/Errors";
import {ErrorWithCause} from "pony-cause";
class Bot {
@@ -234,10 +234,9 @@ class Bot {
}
createSharedStreamErrorListener = (name: string) => async (err: any) => {
this.logger.error(`Polling error occurred on stream ${name.toUpperCase()}`, err);
const shouldRetry = await this.sharedStreamRetryHandler(err);
if(shouldRetry) {
(this.cacheManager.modStreams.get(name) as SPoll<any>).startInterval(false);
(this.cacheManager.modStreams.get(name) as SPoll<any>).startInterval(false, 'Within retry limits');
} else {
for(const m of this.subManagers) {
if(m.sharedStreamCallbacks.size > 0) {
@@ -255,7 +254,7 @@ class Bot {
return;
}
for(const i of listing) {
const foundManager = this.subManagers.find(x => x.subreddit.display_name === i.subreddit.display_name && x.sharedStreamCallbacks.get(name) !== undefined);
const foundManager = this.subManagers.find(x => x.subreddit.display_name === i.subreddit.display_name && x.sharedStreamCallbacks.get(name) !== undefined && x.eventsState.state === RUNNING);
if(foundManager !== undefined) {
foundManager.sharedStreamCallbacks.get(name)(i);
if(this.stagger !== undefined) {
@@ -280,22 +279,16 @@ class Bot {
if (initial) {
this.logger.error('An error occurred while trying to initialize the Reddit API Client which would prevent the entire application from running.');
}
if (err.name === 'StatusCodeError') {
const authHeader = err.response.headers['www-authenticate'];
if (authHeader !== undefined && authHeader.includes('insufficient_scope')) {
this.logger.error('Reddit responded with a 403 insufficient_scope. Please ensure you have chosen the correct scopes when authorizing your account.');
} else if (err.statusCode === 401) {
this.logger.error('It is likely a credential is missing or incorrect. Check clientId, clientSecret, refreshToken, and accessToken');
} else if(err.statusCode === 400) {
this.logger.error('Credentials may have been invalidated due to prior behavior. The error message may contain more information.');
}
this.logger.error(`Error Message: ${err.message}`);
} else {
this.logger.error(err);
}
this.error = `Error occurred while testing Reddit API client: ${err.message}`;
err.logged = true;
throw err;
const hint = getExceptionMessage(err, {
401: 'Likely a credential is missing or incorrect. Check clientId, clientSecret, refreshToken, and accessToken',
400: 'Credentials may have been invalidated manually or by reddit due to behavior',
});
let msg = `Error occurred while testing Reddit API client${hint !== undefined ? `: ${hint}` : ''}`;
this.error = msg;
const clientError = new CMError(msg, {cause: err});
clientError.logged = true;
this.logger.error(clientError);
throw clientError;
}
}
@@ -375,7 +368,7 @@ class Bot {
let processed;
if (stream !== undefined) {
this.logger.info('Restarting SHARED COMMENT STREAM due to a subreddit config change');
stream.end();
stream.end('Replacing with a new stream with updated subreddits');
processed = stream.processed;
}
if (sharedCommentsSubreddits.length > 100) {
@@ -397,7 +390,7 @@ class Bot {
} else {
const stream = this.cacheManager.modStreams.get('newComm');
if (stream !== undefined) {
stream.end();
stream.end('Determined no managers are listening on shared stream parsing');
}
}
@@ -408,7 +401,7 @@ class Bot {
let processed;
if (stream !== undefined) {
this.logger.info('Restarting SHARED SUBMISSION STREAM due to a subreddit config change');
stream.end();
stream.end('Replacing with a new stream with updated subreddits');
processed = stream.processed;
}
if (sharedSubmissionsSubreddits.length > 100) {
@@ -430,7 +423,7 @@ class Bot {
} else {
const stream = this.cacheManager.modStreams.get('newSub');
if (stream !== undefined) {
stream.end();
stream.end('Determined no managers are listening on shared stream parsing');
}
}
@@ -448,7 +441,7 @@ class Bot {
defaultUnmoderatedStream.on('listing', this.createSharedStreamListingListener('unmoderated'));
this.cacheManager.modStreams.set('unmoderated', defaultUnmoderatedStream);
} else if (!isUnmoderatedShared && unmoderatedstream !== undefined) {
unmoderatedstream.end();
unmoderatedstream.end('Determined no managers are listening on shared stream parsing');
}
const isModqueueShared = !this.sharedStreams.includes('modqueue') ? false : this.subManagers.some(x => x.isPollingShared('modqueue'));
@@ -465,7 +458,7 @@ class Bot {
defaultModqueueStream.on('listing', this.createSharedStreamListingListener('modqueue'));
this.cacheManager.modStreams.set('modqueue', defaultModqueueStream);
} else if (isModqueueShared && modqueuestream !== undefined) {
modqueuestream.end();
modqueuestream.end('Determined no managers are listening on shared stream parsing');
}
}
@@ -473,10 +466,12 @@ class Bot {
try {
await manager.parseConfiguration('system', true, {suppressNotification: true, suppressChangeEvent: true});
} catch (err: any) {
if (!(err instanceof LoggedError)) {
this.logger.error(`Config was not valid:`, {subreddit: manager.subreddit.display_name_prefixed});
this.logger.error(err, {subreddit: manager.subreddit.display_name_prefixed});
err.logged = true;
if(err.logged !== true) {
const normalizedError = new ErrorWithCause(`Bot could not start manager because config was not valid`, {cause: err});
// @ts-ignore
this.logger.error(normalizedError, {subreddit: manager.subreddit.display_name_prefixed});
} else {
this.logger.error('Bot could not start manager because config was not valid', {subreddit: manager.subreddit.display_name_prefixed});
}
}
}
@@ -666,9 +661,11 @@ class Bot {
}
}
} catch (err: any) {
this.logger.info('Stopping event polling to prevent activity processing queue from backing up. Will be restarted when config update succeeds.')
await s.stopEvents('system', {reason: 'Invalid config will cause events to pile up in queue. Will be restarted when config update succeeds (next heartbeat).'});
if(!(err instanceof LoggedError)) {
if(s.eventsState.state === RUNNING) {
this.logger.info('Stopping event polling to prevent activity processing queue from backing up. Will be restarted when config update succeeds.')
await s.stopEvents('system', {reason: 'Invalid config will cause events to pile up in queue. Will be restarted when config update succeeds (next heartbeat).'});
}
if(err.logged !== true) {
this.logger.error(err, {subreddit: s.displayLabel});
}
if(this.nextHeartbeat !== undefined) {
@@ -744,6 +741,10 @@ class Bot {
m.notificationManager.handle('runStateChanged', 'Hard Limit Triggered', `Hard Limit of ${this.hardLimit} hit (API Remaining: ${this.client.ratelimitRemaining}). Subreddit event polling has been paused.`, 'system', 'warn');
}
for(const [k,v] of this.cacheManager.modStreams) {
v.end('Hard limit cutoff');
}
this.nannyMode = 'hard';
return;
}
@@ -810,6 +811,7 @@ class Bot {
await m.startEvents('system', {reason: 'API Nanny has been turned off due to better API conditions'});
}
}
await this.runSharedStreams(true);
this.nannyMode = undefined;
}

View File

@@ -31,6 +31,8 @@ import {ActionObjectJson, RuleJson, RuleObjectJson, ActionJson as ActionTypeJson
import {checkAuthorFilter, SubredditResources} from "../Subreddit/SubredditResources";
import {Author, AuthorCriteria, AuthorOptions} from '..';
import {ExtendedSnoowrap} from '../Utils/SnoowrapClients';
import {isRateLimitError} from "../Utils/Errors";
import {ErrorWithCause} from "pony-cause";
const checkLogName = truncateStringToLength(25);
@@ -248,9 +250,7 @@ export abstract class Check implements ICheck {
this.logger.info(`${PASS} => Rules: ${resultsSummary(allResults, this.condition)}`);
return [true, allRuleResults];
} catch (e: any) {
e.logged = true;
this.logger.warn(`Running rules failed due to uncaught exception`, e);
throw e;
throw new ErrorWithCause('Running rules failed due to error', {cause: e});
}
}

View File

@@ -3,9 +3,9 @@ import {Submission} from "snoowrap/dist/objects";
import {URL} from "url";
import {absPercentDifference, getSharpAsync, isValidImageURL} from "../util";
import sizeOf from "image-size";
import SimpleError from "../Utils/SimpleError";
import {Sharp} from "sharp";
import {blockhash} from "./blockhash/blockhash";
import {SimpleError} from "../Utils/Errors";
export interface ImageDataOptions {
width?: number,

View File

@@ -13,6 +13,9 @@ import {ConfigFormat} from "./types";
import AbstractConfigDocument, {ConfigDocumentInterface} from "./Config/AbstractConfigDocument";
import {Document as YamlDocument} from 'yaml';
import {JsonOperatorConfigDocument, YamlOperatorConfigDocument} from "./Config/Operator";
import {ConsoleTransportOptions} from "winston/lib/winston/transports";
import {DailyRotateFileTransportOptions} from "winston-daily-rotate-file";
import {DuplexTransportOptions} from "winston-duplex/dist/DuplexTransport";
/**
* An ISO 8601 Duration
@@ -1071,6 +1074,92 @@ export interface RegExResult {
}
type LogLevel = "error" | "warn" | "info" | "verbose" | "debug";
export type LogConsoleOptions = Pick<ConsoleTransportOptions, 'silent' | 'eol' | 'stderrLevels' | 'consoleWarnLevels'> & {
level?: LogLevel
}
export type LogFileOptions = Omit<DailyRotateFileTransportOptions, 'stream' | 'handleRejections' | 'options' | 'handleExceptions' | 'format' | 'log' | 'logv' | 'close' | 'dirname'> & {
level?: LogLevel
/**
* The absolute path to a directory where rotating log files should be stored.
*
* * If not present or `null` or `false` no log files will be created
* * If `true` logs will be stored at `[working directory]/logs`
*
* * ENV => `LOG_DIR`
* * ARG => `--logDir [dir]`
*
* @examples ["/var/log/contextmod"]
* */
dirname?: string | boolean | null
}
// export type StrongFileOptions = LogFileOptions & {
// dirname?: string
// }
export type LogStreamOptions = Omit<DuplexTransportOptions, 'name' | 'stream' | 'handleRejections' | 'handleExceptions' | 'format' | 'log' | 'logv' | 'close'> & {
level?: LogLevel
}
export interface LoggingOptions {
/**
* The minimum log level to output. The log level set will output logs at its level **and all levels above it:**
*
* * `error`
* * `warn`
* * `info`
* * `verbose`
* * `debug`
*
* Note: `verbose` will display *a lot* of information on the status/result of run rules/checks/actions etc. which is very useful for testing configurations. Once your bot is stable changing the level to `info` will reduce log noise.
*
* * ENV => `LOG_LEVEL`
* * ARG => `--logLevel <level>`
*
* @default "verbose"
* @examples ["verbose"]
* */
level?: LogLevel,
/**
* **DEPRECATED** - Use `file.dirname` instead
* The absolute path to a directory where rotating log files should be stored.
*
* * If not present or `null` or `false` no log files will be created
* * If `true` logs will be stored at `[working directory]/logs`
*
* * ENV => `LOG_DIR`
* * ARG => `--logDir [dir]`
*
* @examples ["/var/log/contextmod"]
* @deprecated
* @see logging.file.dirname
* */
path?: string | boolean | null
/**
* Options for Rotating File logging
* */
file?: LogFileOptions
/**
* Options for logging to api/web
* */
stream?: LogStreamOptions
/**
* Options for logging to console
* */
console?: LogConsoleOptions
}
export type StrongLoggingOptions = Required<Pick<LoggingOptions, 'stream' | 'console' | 'file'>> & {
level?: LogLevel
};
export type LoggerFactoryOptions = StrongLoggingOptions & {
additionalTransports?: any[]
defaultLabel?: string
}
/**
* Available cache providers
* */
@@ -1581,38 +1670,7 @@ export interface OperatorJsonConfig {
/**
* Settings to configure global logging defaults
* */
logging?: {
/**
* The minimum log level to output. The log level set will output logs at its level **and all levels above it:**
*
* * `error`
* * `warn`
* * `info`
* * `verbose`
* * `debug`
*
* Note: `verbose` will display *a lot* of information on the status/result of run rules/checks/actions etc. which is very useful for testing configurations. Once your bot is stable changing the level to `info` will reduce log noise.
*
* * ENV => `LOG_LEVEL`
* * ARG => `--logLevel <level>`
*
* @default "verbose"
* @examples ["verbose"]
* */
level?: LogLevel,
/**
* The absolute path to a directory where rotating log files should be stored.
*
* * If not present or `null` no log files will be created
* * If `true` logs will be stored at `[working directory]/logs`
*
* * ENV => `LOG_DIR`
* * ARG => `--logDir [dir]`
*
* @examples ["/var/log/contextmod"]
* */
path?: string,
},
logging?: LoggingOptions,
/**
* Settings to configure the default caching behavior globally
@@ -1812,10 +1870,7 @@ export interface OperatorConfig extends OperatorJsonConfig {
display?: string,
},
notifications?: NotificationConfig
logging: {
level: LogLevel,
path?: string,
},
logging: StrongLoggingOptions,
caching: StrongCache,
web: {
port: number,
@@ -1918,22 +1973,6 @@ export interface RedditEntity {
type: RedditEntityType
}
export interface StatusCodeError extends Error {
name: 'StatusCodeError',
statusCode: number,
message: string,
response: IncomingMessage,
error: Error
}
export interface RequestError extends Error {
name: 'RequestError',
statusCode: number,
message: string,
response: IncomingMessage,
error: Error
}
export interface HistoricalStatsDisplay extends HistoricalStats {
checksRunTotal: number
checksFromCacheTotal: number
@@ -2061,3 +2100,56 @@ export interface FilterResult<T> {
join: JoinOperands
passed: boolean
}
export interface TextTransformOptions {
/**
* A set of search-and-replace operations to perform on text values before performing a match. Transformations are performed in the order they are defined.
*
* * If `transformationsActivity` IS NOT defined then these transformations will be performed on BOTH the activity text (submission title or comment) AND the repost candidate text
* * If `transformationsActivity` IS defined then these transformations are only performed on repost candidate text
* */
transformations?: SearchAndReplaceRegExp[]
/**
* Specify a separate set of transformations for the activity text (submission title or comment)
*
* To perform no transformations when `transformations` is defined set this to an empty array (`[]`)
* */
transformationsActivity?: SearchAndReplaceRegExp[]
}
export interface TextMatchOptions {
/**
* The percentage, as a whole number, of a repost title/comment that must match the title/comment being checked in order to consider both a match
*
* Note: Setting to 0 will make every candidate considered a match -- useful if you want to match if the URL has been reposted anywhere
*
* Defaults to `85` (85%)
*
* @default 85
* @example [85]
* */
matchScore?: number
/**
* The minimum number of words in the activity being checked for which this rule will run on
*
* If the word count is below the minimum the rule fails
*
* Defaults to 2
*
* @default 2
* @example [2]
* */
minWordCount?: number
/**
* Should text matching be case sensitive?
*
* Defaults to false
*
* @default false
* @example [false]
**/
caseSensitive?: boolean
}

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

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

View File

@@ -55,9 +55,10 @@ import {
OperatorConfigDocumentInterface,
YamlOperatorConfigDocument
} from "./Common/Config/Operator";
import SimpleError from "./Utils/SimpleError";
import {ConfigDocumentInterface} from "./Common/Config/AbstractConfigDocument";
import {Document as YamlDocument} from "yaml";
import {SimpleError} from "./Utils/Errors";
import {ErrorWithCause} from "pony-cause";
export interface ConfigBuilderOptions {
logger: Logger,
@@ -373,7 +374,16 @@ export const parseOpConfigFromArgs = (args: any): OperatorJsonConfig => {
},
logging: {
level: logLevel,
path: logDir === true ? `${process.cwd()}/logs` : undefined,
file: {
level: logLevel,
dirName: logDir,
},
stream: {
level: logLevel,
},
console: {
level: logLevel,
}
},
caching: {
provider: caching,
@@ -457,9 +467,17 @@ export const parseOpConfigFromEnv = (): OperatorJsonConfig => {
display: process.env.OPERATOR_DISPLAY
},
logging: {
// @ts-ignore
level: process.env.LOG_LEVEL,
path: process.env.LOG_DIR === 'true' ? `${process.cwd()}/logs` : undefined,
file: {
level: process.env.LOG_LEVEL,
dirname: process.env.LOG_DIR,
},
stream: {
level: process.env.LOG_LEVEL,
},
console: {
level: process.env.LOG_LEVEL,
}
},
caching: {
provider: {
@@ -501,11 +519,25 @@ export const parseOpConfigFromEnv = (): OperatorJsonConfig => {
// json config
// args from cli
export const parseOperatorConfigFromSources = async (args: any): Promise<[OperatorJsonConfig, OperatorFileConfig]> => {
const {logLevel = process.env.LOG_LEVEL, logDir = process.env.LOG_DIR || false} = args || {};
const {logLevel = process.env.LOG_LEVEL ?? 'debug', logDir = process.env.LOG_DIR} = args || {};
const envPath = process.env.OPERATOR_ENV;
const initLoggerOptions = {
level: logLevel,
console: {
level: logLevel
},
file: {
level: logLevel,
dirname: logDir,
},
stream: {
level: logLevel
}
}
// create a pre config logger to help with debugging
const initLogger = getLogger({logLevel, logDir: logDir === true ? `${process.cwd()}/logs` : logDir}, 'init');
// default to debug if nothing is provided
const initLogger = getLogger(initLoggerOptions, 'init');
try {
const vars = await GetEnvVars({
@@ -556,9 +588,7 @@ export const parseOperatorConfigFromSources = async (args: any): Promise<[Operat
fileConfigFormat = err.extension
}
} else {
initLogger.error('Cannot continue app startup because operator config file exists but was not parseable.');
err.logged = true;
throw err;
throw new ErrorWithCause('Cannot continue app startup because operator config file exists but was not parseable.', {cause: err});
}
}
const [format, doc, jsonErr, yamlErr] = parseFromJsonOrYamlToObject(rawConfig, {
@@ -591,7 +621,15 @@ export const parseOperatorConfigFromSources = async (args: any): Promise<[Operat
try {
configFromFile = validateJson(configDoc.toJS(), operatorSchema, initLogger) as OperatorJsonConfig;
const {bots = []} = configFromFile || {};
const {
bots = [],
logging: {
path = undefined
} = {}
} = configFromFile || {};
if(path !== undefined) {
initLogger.warn(`'path' property in top-level 'logging' object is DEPRECATED and will be removed in next minor version. Use 'logging.file.dirname' instead`);
}
for (const b of bots) {
const {
polling: {
@@ -651,6 +689,9 @@ export const buildOperatorConfigWithDefaults = (data: OperatorJsonConfig): Opera
logging: {
level = 'verbose',
path,
file = {},
console = {},
stream = {},
} = {},
caching: opCache,
web: {
@@ -726,6 +767,12 @@ export const buildOperatorConfigWithDefaults = (data: OperatorJsonConfig): Opera
const defaultOperators = typeof name === 'string' ? [name] : name;
const {
dirname = path,
...fileRest
} = file;
const config: OperatorConfig = {
mode,
operator: {
@@ -734,7 +781,19 @@ export const buildOperatorConfigWithDefaults = (data: OperatorJsonConfig): Opera
},
logging: {
level,
path
file: {
level: level,
dirname,
...fileRest,
},
stream: {
level: level,
...stream,
},
console: {
level: level,
...console,
}
},
caching: cache,
web: {

View File

@@ -14,8 +14,8 @@ import {
PASS
} from "../util";
import { Comment } from "snoowrap/dist/objects";
import SimpleError from "../Utils/SimpleError";
import as from "async";
import {SimpleError} from "../Utils/Errors";
export interface AttributionCriteria {

View File

@@ -11,7 +11,7 @@ import {
ActivityWindowType, JoinOperands,
} from "../Common/interfaces";
import dayjs from 'dayjs';
import SimpleError from "../Utils/SimpleError";
import {SimpleError} from "../Utils/Errors";
export interface RegexCriteria {
/**

View File

@@ -1,17 +1,28 @@
import {Rule, RuleJSONConfig, RuleOptions, RuleResult} from "./index";
import {Comment} from "snoowrap";
import {
activityWindowText, asSubmission,
comparisonTextOp, FAIL, getActivitySubredditName, isExternalUrlSubmission, isRedditMedia,
parseGenericValueComparison, parseSubredditName,
parseUsableLinkIdentifier as linkParser, PASS, subredditStateIsNameOnly, toStrongSubredditState
activityWindowText,
asSubmission,
comparisonTextOp,
FAIL,
getActivitySubredditName,
isExternalUrlSubmission,
isRedditMedia,
parseGenericValueComparison,
parseSubredditName,
parseUsableLinkIdentifier as linkParser,
PASS,
searchAndReplace,
stringSameness,
subredditStateIsNameOnly,
toStrongSubredditState
} from "../util";
import {
ActivityWindow,
ActivityWindowType,
ReferenceSubmission,
ReferenceSubmission, SearchAndReplaceRegExp,
StrongSubredditState,
SubredditState
SubredditState, TextMatchOptions, TextTransformOptions
} from "../Common/interfaces";
import Submission from "snoowrap/dist/objects/Submission";
import dayjs from "dayjs";
@@ -29,27 +40,6 @@ interface RepeatActivityReducer {
allSets: RepeatActivityData[]
}
const getActivityIdentifier = (activity: (Submission | Comment), length = 200) => {
let identifier: string;
if (asSubmission(activity)) {
if (activity.is_self) {
identifier = `${activity.title}${activity.selftext.slice(0, length)}`;
} else if(isRedditMedia(activity)) {
identifier = activity.title;
} else {
identifier = parseUsableLinkIdentifier(activity.url) as string;
}
} else {
identifier = activity.body.slice(0, length);
}
return identifier;
}
const fuzzyOptions = {
includeScore: true,
distance: 15
};
export class RepeatActivityRule extends Rule {
threshold: string;
window: ActivityWindowType;
@@ -62,6 +52,9 @@ export class RepeatActivityRule extends Rule {
activityFilterFunc: (x: Submission|Comment) => Promise<boolean> = async (x) => true;
keepRemoved: boolean;
minWordCount: number;
transformations: SearchAndReplaceRegExp[]
caseSensitive: boolean
matchScore: number
constructor(options: RepeatActivityOptions) {
super(options);
@@ -75,7 +68,13 @@ export class RepeatActivityRule extends Rule {
include = [],
exclude = [],
keepRemoved = false,
transformations = [],
caseSensitive = true,
matchScore = 85,
} = options;
this.matchScore = matchScore;
this.transformations = transformations;
this.caseSensitive = caseSensitive;
this.minWordCount = minWordCount;
this.keepRemoved = keepRemoved;
this.threshold = threshold;
@@ -136,6 +135,37 @@ export class RepeatActivityRule extends Rule {
}
}
getActivityIdentifier(activity: (Submission | Comment), length = 200, transform = true) {
let identifier: string;
if (asSubmission(activity)) {
if (activity.is_self) {
identifier = `${activity.title}${activity.selftext.slice(0, length)}`;
} else if(isRedditMedia(activity)) {
identifier = activity.title;
} else {
identifier = parseUsableLinkIdentifier(activity.url) as string;
}
} else {
identifier = activity.body.slice(0, length);
}
if(!transform) {
return identifier;
}
// apply any transforms
if (this.transformations.length > 0) {
identifier = searchAndReplace(identifier, this.transformations);
}
// perform after transformations so as not to mess up regex's depending on case
if(!this.caseSensitive) {
identifier = identifier.toLowerCase();
}
return identifier;
}
async process(item: Submission|Comment): Promise<[boolean, RuleResult]> {
let referenceUrl;
if(asSubmission(item) && this.useSubmissionAsReference) {
@@ -162,9 +192,10 @@ export class RepeatActivityRule extends Rule {
const acc = await accProm;
const {openSets = [], allSets = []} = acc;
let identifier = getActivityIdentifier(activity);
let identifier = this.getActivityIdentifier(activity);
const isUrl = isExternalUrlSubmission(activity);
let fu = new Fuse([identifier], !isUrl ? fuzzyOptions : {...fuzzyOptions, distance: 5});
//let fu = new Fuse([identifier], !isUrl ? fuzzyOptions : {...fuzzyOptions, distance: 5});
const validSub = await this.activityFilterFunc(activity);
let minMet = identifier.length >= this.minWordCount;
@@ -174,12 +205,15 @@ export class RepeatActivityRule extends Rule {
let currIdentifierInOpen = false;
const bufferedActivities = this.gapAllowance === undefined || this.gapAllowance === 0 ? [] : activities.slice(Math.max(0, index - this.gapAllowance), Math.max(0, index));
for (const o of openSets) {
const res = fu.search(o.identifier);
const match = res.length > 0;
if (match && validSub && minMet) {
const strMatchResults = stringSameness(o.identifier, identifier);
if (strMatchResults.highScoreWeighted >= this.matchScore && minMet) {
updatedOpenSets.push({...o, sets: [...o.sets, activity]});
currIdentifierInOpen = true;
} else if (bufferedActivities.some(x => fu.search(getActivityIdentifier(x)).length > 0) && validSub && minMet) {
} else if (bufferedActivities.some(x => {
let buffIdentifier = this.getActivityIdentifier(x);
const buffMatch = stringSameness(identifier, buffIdentifier);
return buffMatch.highScoreWeighted >= this.matchScore;
}) && validSub && minMet) {
updatedOpenSets.push(o);
} else if(!currIdentifierInOpen && !isUrl) {
updatedAllSets.push(o);
@@ -193,15 +227,18 @@ export class RepeatActivityRule extends Rule {
// could be that a spammer is using different URLs for each submission but similar submission titles so search by title as well
const sub = activity as Submission;
identifier = sub.title;
fu = new Fuse([identifier], !isUrl ? fuzzyOptions : {...fuzzyOptions, distance: 5});
//fu = new Fuse([identifier], !isUrl ? fuzzyOptions : {...fuzzyOptions, distance: 5});
minMet = identifier.length >= this.minWordCount;
for (const o of openSets) {
const res = fu.search(o.identifier);
const match = res.length > 0;
if (match && validSub && minMet) {
const strMatchResults = stringSameness(o.identifier, identifier);
if (strMatchResults.highScoreWeighted >= this.matchScore && minMet) {
updatedOpenSets.push({...o, sets: [...o.sets, activity]});
currIdentifierInOpen = true;
} else if (bufferedActivities.some(x => fu.search(getActivityIdentifier(x)).length > 0) && validSub && minMet && !updatedOpenSets.includes(o)) {
} else if (bufferedActivities.some(x => {
let buffIdentifier = this.getActivityIdentifier(x);
const buffMatch = stringSameness(identifier, buffIdentifier);
return buffMatch.highScoreWeighted >= this.matchScore;
}) && validSub && minMet && !updatedOpenSets.includes(o)) {
updatedOpenSets.push(o);
} else if(!updatedAllSets.includes(o)) {
updatedAllSets.push(o);
@@ -232,7 +269,7 @@ export class RepeatActivityRule extends Rule {
let applicableGroupedActivities = identifierGroupedActivities;
if (this.useSubmissionAsReference) {
applicableGroupedActivities = new Map();
let identifier = getActivityIdentifier(item);
let identifier = this.getActivityIdentifier(item);
let referenceSubmissions = identifierGroupedActivities.get(identifier);
if(referenceSubmissions === undefined && isExternalUrlSubmission(item)) {
// if external url sub then try by title
@@ -240,7 +277,7 @@ export class RepeatActivityRule extends Rule {
referenceSubmissions = identifierGroupedActivities.get(identifier);
if(referenceSubmissions === undefined) {
// didn't get by title so go back to url since that's the default
identifier = getActivityIdentifier(item);
identifier = this.getActivityIdentifier(item);
}
}
@@ -265,7 +302,7 @@ export class RepeatActivityRule extends Rule {
};
for (let set of value) {
const test = comparisonTextOp(set.length, operator, thresholdValue);
const md = set.map((x: (Comment | Submission)) => `[${asSubmission(x) ? x.title : getActivityIdentifier(x, 50)}](https://reddit.com${x.permalink}) in ${x.subreddit_name_prefixed} on ${dayjs(x.created_utc * 1000).utc().format()}`);
const md = set.map((x: (Comment | Submission)) => `[${asSubmission(x) ? x.title : this.getActivityIdentifier(x, 50)}](https://reddit.com${x.permalink}) in ${x.subreddit_name_prefixed} on ${dayjs(x.created_utc * 1000).utc().format()}`);
summaryData.sets.push(set);
summaryData.largestTrigger = Math.max(summaryData.largestTrigger, set.length);
@@ -325,7 +362,7 @@ interface SummaryData {
triggeringSetsMarkdown: string[]
}
interface RepeatActivityConfig extends ActivityWindow, ReferenceSubmission {
interface RepeatActivityConfig extends ActivityWindow, ReferenceSubmission, TextMatchOptions {
/**
* The number of repeat submissions that will trigger the rule
* @default ">= 5"
@@ -383,18 +420,9 @@ interface RepeatActivityConfig extends ActivityWindow, ReferenceSubmission {
keepRemoved?: boolean
/**
* For activities that are text-based this is the minimum number of words required for the activity to be considered for a repeat
*
* EX if `minimumWordCount=5` and a comment is `what about you` then it is ignored because `3 is less than 5`
*
* **For self-text submissions** -- title + body text
*
* **For comments* -- body text
*
* @default 1
* @example [1]
* A set of search-and-replace operations to perform on text values before performing a match. Transformations are performed in the order they are defined.
* */
minWordCount?: number,
transformations?: SearchAndReplaceRegExp[]
}
export interface RepeatActivityOptions extends RepeatActivityConfig, RuleOptions {

View File

@@ -18,7 +18,7 @@ import {
RepostItem,
RepostItemResult,
SearchAndReplaceRegExp,
SearchFacetType,
SearchFacetType, TextMatchOptions, TextTransformOptions,
} from "../Common/interfaces";
import objectHash from "object-hash";
import {getActivities, getAttributionIdentifier} from "../Utils/SnoowrapUtils";
@@ -30,59 +30,6 @@ import {rest} from "lodash";
const parseYtIdentifier = parseUsableLinkIdentifier();
export interface TextMatchOptions {
/**
* The percentage, as a whole number, of a repost title/comment that must match the title/comment being checked in order to consider both a match
*
* Note: Setting to 0 will make every candidate considered a match -- useful if you want to match if the URL has been reposted anywhere
*
* Defaults to `85` (85%)
*
* @default 85
* @example [85]
* */
matchScore?: number
/**
* The minimum number of words in the activity being checked for which this rule will run on
*
* If the word count is below the minimum the rule fails
*
* Defaults to 2
*
* @default 2
* @example [2]
* */
minWordCount?: number
/**
* Should text matching be case sensitive?
*
* Defaults to false
*
* @default false
* @example [false]
**/
caseSensitive?: boolean
}
export interface TextTransformOptions {
/**
* A set of search-and-replace operations to perform on text values before performing a match. Transformations are performed in the order they are defined.
*
* * If `transformationsActivity` IS NOT defined then these transformations will be performed on BOTH the activity text (submission title or comment) AND the repost candidate text
* * If `transformationsActivity` IS defined then these transformations are only performed on repost candidate text
* */
transformations?: SearchAndReplaceRegExp[]
/**
* Specify a separate set of transformations for the activity text (submission title or comment)
*
* To perform no transformations when `transformations` is defined set this to an empty array (`[]`)
* */
transformationsActivity?: SearchAndReplaceRegExp[]
}
export interface SearchFacetJSONConfig extends TextMatchOptions, TextTransformOptions, ActivityWindow {
kind: SearchFacetType | SearchFacetType[]
}

View File

@@ -246,8 +246,7 @@
"default": "undefined",
"description": "This list determines which categories of domains should be aggregated on. All aggregated domains will be tested against `threshold`\n\n* If `media` is included then aggregate author's submission history which reddit recognizes as media (youtube, vimeo, etc.)\n* If `redditMedia` is included then aggregate on author's submissions history which are media hosted on reddit: galleries, videos, and images (i.redd.it / v.redd.it)\n* If `self` is included then aggregate on author's submission history which are self-post (`self.[subreddit]`) or domain is `reddit.com`\n* If `link` is included then aggregate author's submission history which is external links and not recognized as `media` by reddit\n\nIf nothing is specified or list is empty (default) rule will only aggregate on `link` and `media` (ignores reddit-hosted content and self-posts)",
"examples": [
[
]
[]
],
"items": {
"enum": [
@@ -280,8 +279,7 @@
},
"domains": {
"default": [
[
]
[]
],
"description": "A list of domains whose Activities will be tested against `threshold`.\n\nThe values are tested as partial strings so you do not need to include full URLs, just the part that matters.\n\nEX `[\"youtube\"]` will match submissions with the domain `https://youtube.com/c/aChannel`\nEX `[\"youtube.com/c/bChannel\"]` will NOT match submissions with the domain `https://youtube.com/c/aChannel`\n\nIf you wish to aggregate on self-posts for a subreddit use the syntax `self.[subreddit]` EX `self.AskReddit`\n\n**If this Rule is part of a Check for a Submission and you wish to aggregate on the domain of the Submission use the special string `AGG:SELF`**\n\nIf nothing is specified or list is empty (default) aggregate using `aggregateOn`",
"items": {
@@ -963,8 +961,7 @@
"type": "object"
},
"CacheOptions": {
"additionalProperties": {
},
"additionalProperties": {},
"description": "Configure granular settings for a cache provider with this object",
"properties": {
"auth_pass": {
@@ -2638,6 +2635,9 @@
],
"pattern": "^[a-zA-Z]([\\w -]*[\\w])?$",
"type": "string"
},
"spam": {
"type": "boolean"
}
},
"required": [
@@ -2667,6 +2667,11 @@
}
]
},
"caseSensitive": {
"default": false,
"description": "Should text matching be case sensitive?\n\nDefaults to false",
"type": "boolean"
},
"exclude": {
"description": "If present, activities will be counted only if they are **NOT** found in this list of Subreddits\n\nEach value in the list can be either:\n\n * string (name of subreddit)\n * regular expression to run on the subreddit name\n * `SubredditState`\n\nEX `[\"mealtimevideos\",\"askscience\", \"/onlyfans*\\/i\", {\"over18\": true}]`",
"examples": [
@@ -2757,9 +2762,14 @@
],
"type": "string"
},
"matchScore": {
"default": 85,
"description": "The percentage, as a whole number, of a repost title/comment that must match the title/comment being checked in order to consider both a match\n\nNote: Setting to 0 will make every candidate considered a match -- useful if you want to match if the URL has been reposted anywhere\n\nDefaults to `85` (85%)",
"type": "number"
},
"minWordCount": {
"default": 1,
"description": "For activities that are text-based this is the minimum number of words required for the activity to be considered for a repeat\n\nEX if `minimumWordCount=5` and a comment is `what about you` then it is ignored because `3 is less than 5`\n\n**For self-text submissions** -- title + body text\n\n**For comments* -- body text",
"default": 2,
"description": "The minimum number of words in the activity being checked for which this rule will run on\n\nIf the word count is below the minimum the rule fails\n\nDefaults to 2",
"type": "number"
},
"name": {
@@ -2775,6 +2785,13 @@
"description": "The number of repeat submissions that will trigger the rule",
"type": "string"
},
"transformations": {
"description": "A set of search-and-replace operations to perform on text values before performing a match. Transformations are performed in the order they are defined.",
"items": {
"$ref": "#/definitions/SearchAndReplaceRegExp"
},
"type": "array"
},
"useSubmissionAsReference": {
"default": true,
"description": "If activity is a Submission and is a link (not self-post) then only look at Submissions that contain this link, otherwise consider all activities.",
@@ -3601,8 +3618,7 @@
"type": "object"
},
"ThirdPartyCredentialsJsonConfig": {
"additionalProperties": {
},
"additionalProperties": {},
"properties": {
"youtube": {
"properties": {

View File

@@ -397,8 +397,7 @@
"type": "object"
},
"CacheOptions": {
"additionalProperties": {
},
"additionalProperties": {},
"description": "Configure granular settings for a cache provider with this object",
"properties": {
"auth_pass": {
@@ -607,6 +606,117 @@
},
"type": "object"
},
"LoggingOptions": {
"properties": {
"console": {
"allOf": [
{
"$ref": "#/definitions/Pick<Transports.ConsoleTransportOptions,\"silent\"|\"eol\"|\"stderrLevels\"|\"consoleWarnLevels\">"
},
{
"properties": {
"level": {
"enum": [
"debug",
"error",
"info",
"verbose",
"warn"
],
"type": "string"
}
},
"type": "object"
}
],
"description": "Options for logging to console"
},
"file": {
"allOf": [
{
"$ref": "#/definitions/Omit<DailyRotateFileTransportOptions,\"stream\"|\"dirname\"|\"options\"|\"handleRejections\"|\"format\"|\"handleExceptions\"|\"log\"|\"logv\"|\"close\">"
},
{
"properties": {
"dirname": {
"description": "The absolute path to a directory where rotating log files should be stored.\n\n* If not present or `null` or `false` no log files will be created\n* If `true` logs will be stored at `[working directory]/logs`\n\n* ENV => `LOG_DIR`\n* ARG => `--logDir [dir]`",
"examples": [
"/var/log/contextmod"
],
"type": [
"null",
"string",
"boolean"
]
},
"level": {
"enum": [
"debug",
"error",
"info",
"verbose",
"warn"
],
"type": "string"
}
},
"type": "object"
}
],
"description": "Options for Rotating File logging"
},
"level": {
"default": "verbose",
"description": "The minimum log level to output. The log level set will output logs at its level **and all levels above it:**\n\n * `error`\n * `warn`\n * `info`\n * `verbose`\n * `debug`\n\n Note: `verbose` will display *a lot* of information on the status/result of run rules/checks/actions etc. which is very useful for testing configurations. Once your bot is stable changing the level to `info` will reduce log noise.\n\n * ENV => `LOG_LEVEL`\n * ARG => `--logLevel <level>`",
"enum": [
"debug",
"error",
"info",
"verbose",
"warn"
],
"examples": [
"verbose"
],
"type": "string"
},
"path": {
"description": "**DEPRECATED** - Use `file.dirname` instead\nThe absolute path to a directory where rotating log files should be stored.\n\n* If not present or `null` or `false` no log files will be created\n* If `true` logs will be stored at `[working directory]/logs`\n\n* ENV => `LOG_DIR`\n* ARG => `--logDir [dir]`",
"examples": [
"/var/log/contextmod"
],
"type": [
"null",
"string",
"boolean"
]
},
"stream": {
"allOf": [
{
"$ref": "#/definitions/Omit<DuplexTransportOptions,\"name\"|\"stream\"|\"handleRejections\"|\"format\"|\"handleExceptions\"|\"log\"|\"logv\"|\"close\">"
},
{
"properties": {
"level": {
"enum": [
"debug",
"error",
"info",
"verbose",
"warn"
],
"type": "string"
}
},
"type": "object"
}
],
"description": "Options for logging to api/web"
}
},
"type": "object"
},
"NotificationConfig": {
"properties": {
"events": {
@@ -672,6 +782,90 @@
],
"type": "object"
},
"Omit<DailyRotateFileTransportOptions,\"stream\"|\"dirname\"|\"options\"|\"handleRejections\"|\"format\"|\"handleExceptions\"|\"log\"|\"logv\"|\"close\">": {
"properties": {
"auditFile": {
"description": "A string representing the name of the name of the audit file. (default: './hash-audit.json')",
"type": "string"
},
"createSymlink": {
"description": "Create a tailable symlink to the current active log file. (default: false)",
"type": "boolean"
},
"datePattern": {
"description": "A string representing the moment.js date format to be used for rotating. The meta characters used in this string will dictate the frequency of the file rotation. For example, if your datePattern is simply 'HH' you will end up with 24 log files that are picked up and appended to every day. (default 'YYYY-MM-DD')",
"type": "string"
},
"eol": {
"type": "string"
},
"extension": {
"description": "A string representing an extension to be added to the filename, if not included in the filename property. (default: '')",
"type": "string"
},
"filename": {
"description": "Filename to be used to log to. This filename can include the %DATE% placeholder which will include the formatted datePattern at that point in the filename. (default: 'winston.log.%DATE%)",
"type": "string"
},
"frequency": {
"description": "A string representing the frequency of rotation. (default: 'custom')",
"type": "string"
},
"json": {
"type": "boolean"
},
"level": {
"type": "string"
},
"maxFiles": {
"description": "Maximum number of logs to keep. If not set, no logs will be removed. This can be a number of files or number of days. If using days, add 'd' as the suffix. (default: null)",
"type": [
"string",
"number"
]
},
"maxSize": {
"description": "Maximum size of the file after which it will rotate. This can be a number of bytes, or units of kb, mb, and gb. If using the units, add 'k', 'm', or 'g' as the suffix. The units need to directly follow the number. (default: null)",
"type": [
"string",
"number"
]
},
"silent": {
"type": "boolean"
},
"symlinkName": {
"description": "The name of the tailable symlink. (default: 'current.log')",
"type": "string"
},
"utc": {
"description": "A boolean whether or not to generate file name from \"datePattern\" in UTC format. (default: false)",
"type": "boolean"
},
"zippedArchive": {
"description": "A boolean to define whether or not to gzip archived log files. (default 'false')",
"type": "boolean"
}
},
"type": "object"
},
"Omit<DuplexTransportOptions,\"name\"|\"stream\"|\"handleRejections\"|\"format\"|\"handleExceptions\"|\"log\"|\"logv\"|\"close\">": {
"properties": {
"dump": {
"type": "boolean"
},
"eol": {
"type": "string"
},
"level": {
"type": "string"
},
"silent": {
"type": "boolean"
}
},
"type": "object"
},
"OperatorCacheConfig": {
"properties": {
"actionedEventsDefault": {
@@ -791,6 +985,29 @@
},
"type": "object"
},
"Pick<Transports.ConsoleTransportOptions,\"silent\"|\"eol\"|\"stderrLevels\"|\"consoleWarnLevels\">": {
"properties": {
"consoleWarnLevels": {
"items": {
"type": "string"
},
"type": "array"
},
"eol": {
"type": "string"
},
"silent": {
"type": "boolean"
},
"stderrLevels": {
"items": {
"type": "string"
},
"type": "array"
}
},
"type": "object"
},
"PollingDefaults": {
"properties": {
"delayUntil": {
@@ -983,8 +1200,7 @@
"type": "object"
},
"ThirdPartyCredentialsJsonConfig": {
"additionalProperties": {
},
"additionalProperties": {},
"properties": {
"youtube": {
"properties": {
@@ -1110,32 +1326,8 @@
"$ref": "#/definitions/ThirdPartyCredentialsJsonConfig"
},
"logging": {
"description": "Settings to configure global logging defaults",
"properties": {
"level": {
"default": "verbose",
"description": "The minimum log level to output. The log level set will output logs at its level **and all levels above it:**\n\n * `error`\n * `warn`\n * `info`\n * `verbose`\n * `debug`\n\n Note: `verbose` will display *a lot* of information on the status/result of run rules/checks/actions etc. which is very useful for testing configurations. Once your bot is stable changing the level to `info` will reduce log noise.\n\n * ENV => `LOG_LEVEL`\n * ARG => `--logLevel <level>`",
"enum": [
"debug",
"error",
"info",
"verbose",
"warn"
],
"examples": [
"verbose"
],
"type": "string"
},
"path": {
"description": "The absolute path to a directory where rotating log files should be stored.\n\n* If not present or `null` no log files will be created\n* If `true` logs will be stored at `[working directory]/logs`\n\n* ENV => `LOG_DIR`\n* ARG => `--logDir [dir]`",
"examples": [
"/var/log/contextmod"
],
"type": "string"
}
},
"type": "object"
"$ref": "#/definitions/LoggingOptions",
"description": "Settings to configure global logging defaults"
},
"mode": {
"default": "all",

View File

@@ -184,8 +184,7 @@
"default": "undefined",
"description": "This list determines which categories of domains should be aggregated on. All aggregated domains will be tested against `threshold`\n\n* If `media` is included then aggregate author's submission history which reddit recognizes as media (youtube, vimeo, etc.)\n* If `redditMedia` is included then aggregate on author's submissions history which are media hosted on reddit: galleries, videos, and images (i.redd.it / v.redd.it)\n* If `self` is included then aggregate on author's submission history which are self-post (`self.[subreddit]`) or domain is `reddit.com`\n* If `link` is included then aggregate author's submission history which is external links and not recognized as `media` by reddit\n\nIf nothing is specified or list is empty (default) rule will only aggregate on `link` and `media` (ignores reddit-hosted content and self-posts)",
"examples": [
[
]
[]
],
"items": {
"enum": [
@@ -218,8 +217,7 @@
},
"domains": {
"default": [
[
]
[]
],
"description": "A list of domains whose Activities will be tested against `threshold`.\n\nThe values are tested as partial strings so you do not need to include full URLs, just the part that matters.\n\nEX `[\"youtube\"]` will match submissions with the domain `https://youtube.com/c/aChannel`\nEX `[\"youtube.com/c/bChannel\"]` will NOT match submissions with the domain `https://youtube.com/c/aChannel`\n\nIf you wish to aggregate on self-posts for a subreddit use the syntax `self.[subreddit]` EX `self.AskReddit`\n\n**If this Rule is part of a Check for a Submission and you wish to aggregate on the domain of the Submission use the special string `AGG:SELF`**\n\nIf nothing is specified or list is empty (default) aggregate using `aggregateOn`",
"items": {
@@ -1462,6 +1460,11 @@
}
]
},
"caseSensitive": {
"default": false,
"description": "Should text matching be case sensitive?\n\nDefaults to false",
"type": "boolean"
},
"exclude": {
"description": "If present, activities will be counted only if they are **NOT** found in this list of Subreddits\n\nEach value in the list can be either:\n\n * string (name of subreddit)\n * regular expression to run on the subreddit name\n * `SubredditState`\n\nEX `[\"mealtimevideos\",\"askscience\", \"/onlyfans*\\/i\", {\"over18\": true}]`",
"examples": [
@@ -1552,9 +1555,14 @@
],
"type": "string"
},
"matchScore": {
"default": 85,
"description": "The percentage, as a whole number, of a repost title/comment that must match the title/comment being checked in order to consider both a match\n\nNote: Setting to 0 will make every candidate considered a match -- useful if you want to match if the URL has been reposted anywhere\n\nDefaults to `85` (85%)",
"type": "number"
},
"minWordCount": {
"default": 1,
"description": "For activities that are text-based this is the minimum number of words required for the activity to be considered for a repeat\n\nEX if `minimumWordCount=5` and a comment is `what about you` then it is ignored because `3 is less than 5`\n\n**For self-text submissions** -- title + body text\n\n**For comments* -- body text",
"default": 2,
"description": "The minimum number of words in the activity being checked for which this rule will run on\n\nIf the word count is below the minimum the rule fails\n\nDefaults to 2",
"type": "number"
},
"name": {
@@ -1570,6 +1578,13 @@
"description": "The number of repeat submissions that will trigger the rule",
"type": "string"
},
"transformations": {
"description": "A set of search-and-replace operations to perform on text values before performing a match. Transformations are performed in the order they are defined.",
"items": {
"$ref": "#/definitions/SearchAndReplaceRegExp"
},
"type": "array"
},
"useSubmissionAsReference": {
"default": true,
"description": "If activity is a Submission and is a link (not self-post) then only look at Submissions that contain this link, otherwise consider all activities.",

View File

@@ -158,8 +158,7 @@
"default": "undefined",
"description": "This list determines which categories of domains should be aggregated on. All aggregated domains will be tested against `threshold`\n\n* If `media` is included then aggregate author's submission history which reddit recognizes as media (youtube, vimeo, etc.)\n* If `redditMedia` is included then aggregate on author's submissions history which are media hosted on reddit: galleries, videos, and images (i.redd.it / v.redd.it)\n* If `self` is included then aggregate on author's submission history which are self-post (`self.[subreddit]`) or domain is `reddit.com`\n* If `link` is included then aggregate author's submission history which is external links and not recognized as `media` by reddit\n\nIf nothing is specified or list is empty (default) rule will only aggregate on `link` and `media` (ignores reddit-hosted content and self-posts)",
"examples": [
[
]
[]
],
"items": {
"enum": [
@@ -192,8 +191,7 @@
},
"domains": {
"default": [
[
]
[]
],
"description": "A list of domains whose Activities will be tested against `threshold`.\n\nThe values are tested as partial strings so you do not need to include full URLs, just the part that matters.\n\nEX `[\"youtube\"]` will match submissions with the domain `https://youtube.com/c/aChannel`\nEX `[\"youtube.com/c/bChannel\"]` will NOT match submissions with the domain `https://youtube.com/c/aChannel`\n\nIf you wish to aggregate on self-posts for a subreddit use the syntax `self.[subreddit]` EX `self.AskReddit`\n\n**If this Rule is part of a Check for a Submission and you wish to aggregate on the domain of the Submission use the special string `AGG:SELF`**\n\nIf nothing is specified or list is empty (default) aggregate using `aggregateOn`",
"items": {
@@ -1436,6 +1434,11 @@
}
]
},
"caseSensitive": {
"default": false,
"description": "Should text matching be case sensitive?\n\nDefaults to false",
"type": "boolean"
},
"exclude": {
"description": "If present, activities will be counted only if they are **NOT** found in this list of Subreddits\n\nEach value in the list can be either:\n\n * string (name of subreddit)\n * regular expression to run on the subreddit name\n * `SubredditState`\n\nEX `[\"mealtimevideos\",\"askscience\", \"/onlyfans*\\/i\", {\"over18\": true}]`",
"examples": [
@@ -1526,9 +1529,14 @@
],
"type": "string"
},
"matchScore": {
"default": 85,
"description": "The percentage, as a whole number, of a repost title/comment that must match the title/comment being checked in order to consider both a match\n\nNote: Setting to 0 will make every candidate considered a match -- useful if you want to match if the URL has been reposted anywhere\n\nDefaults to `85` (85%)",
"type": "number"
},
"minWordCount": {
"default": 1,
"description": "For activities that are text-based this is the minimum number of words required for the activity to be considered for a repeat\n\nEX if `minimumWordCount=5` and a comment is `what about you` then it is ignored because `3 is less than 5`\n\n**For self-text submissions** -- title + body text\n\n**For comments* -- body text",
"default": 2,
"description": "The minimum number of words in the activity being checked for which this rule will run on\n\nIf the word count is below the minimum the rule fails\n\nDefaults to 2",
"type": "number"
},
"name": {
@@ -1544,6 +1552,13 @@
"description": "The number of repeat submissions that will trigger the rule",
"type": "string"
},
"transformations": {
"description": "A set of search-and-replace operations to perform on text values before performing a match. Transformations are performed in the order they are defined.",
"items": {
"$ref": "#/definitions/SearchAndReplaceRegExp"
},
"type": "array"
},
"useSubmissionAsReference": {
"default": true,
"description": "If activity is a Submission and is a link (not self-post) then only look at Submissions that contain this link, otherwise consider all activities.",

View File

@@ -48,7 +48,8 @@ import {CheckStructuredJson} from "../Check";
import NotificationManager from "../Notification/NotificationManager";
import {createHistoricalDefaults, historicalDefaults} from "../Common/defaults";
import {ExtendedSnoowrap} from "../Utils/SnoowrapClients";
import {isRateLimitError, isStatusError} from "../Utils/Errors";
import {CMError, isRateLimitError, isStatusError} from "../Utils/Errors";
import {ErrorWithCause} from "pony-cause";
export interface RunningState {
state: RunState,
@@ -449,9 +450,6 @@ export class Manager extends EventEmitter {
this.logger.info(checkSummary);
}
this.validConfigLoaded = true;
if(!suppressChangeEvent) {
this.emit('configChange');
}
if(this.eventsState.state === RUNNING) {
// need to update polling, potentially
await this.buildPolling();
@@ -462,6 +460,9 @@ export class Manager extends EventEmitter {
}
}
}
if(!suppressChangeEvent) {
this.emit('configChange');
}
} catch (err: any) {
this.validConfigLoaded = false;
throw err;
@@ -484,21 +485,21 @@ export class Manager extends EventEmitter {
if(isStatusError(err) && err.statusCode === 404) {
// see if we can create the page
if (!this.client.scope.includes('wikiedit')) {
throw new Error(`Page does not exist and could not be created because Bot does not have oauth permission 'wikiedit'`);
throw new ErrorWithCause(`Page does not exist and could not be created because Bot does not have oauth permission 'wikiedit'`, {cause: err});
}
const modPermissions = await this.getModPermissions();
if (!modPermissions.includes('all') && !modPermissions.includes('wiki')) {
throw new Error(`Page does not exist and could not be created because Bot not have mod permissions for creating wiki pages. Must have 'all' or 'wiki'`);
throw new ErrorWithCause(`Page does not exist and could not be created because Bot not have mod permissions for creating wiki pages. Must have 'all' or 'wiki'`, {cause: err});
}
if(!this.client.scope.includes('modwiki')) {
throw new Error(`Bot COULD create wiki config page but WILL NOT because it does not have the oauth permissions 'modwiki' which is required to set page visibility and editing permissions. Safety first!`);
throw new ErrorWithCause(`Bot COULD create wiki config page but WILL NOT because it does not have the oauth permissions 'modwiki' which is required to set page visibility and editing permissions. Safety first!`, {cause: err});
}
// @ts-ignore
wiki = await this.subreddit.getWikiPage(this.wikiLocation).edit({
text: '',
reason: 'Empty configuration created for ContextMod'
});
this.logger.info(`Wiki page at ${this.wikiLocation} did not exist, but bot created it!`);
this.logger.info(`Wiki page at ${this.wikiLocation} did not exist so bot created it!`);
// 0 = use subreddit wiki permissions
// 1 = only approved wiki contributors
@@ -542,11 +543,10 @@ export class Manager extends EventEmitter {
} catch (err: any) {
let hint = '';
if(isStatusError(err) && err.statusCode === 403) {
hint = `\r\nHINT: Either the page is restricted to mods only and the bot's reddit account does have the mod permission 'all' or 'wiki' OR the bot does not have the 'wikiread' oauth permission`;
hint = ` -- HINT: Either the page is restricted to mods only and the bot's reddit account does have the mod permission 'all' or 'wiki' OR the bot does not have the 'wikiread' oauth permission`;
}
const msg = `Could not read wiki configuration. Please ensure the page https://reddit.com${this.subreddit.url}wiki/${this.wikiLocation} exists and is readable${hint} -- error: ${err.message}`;
this.logger.error(msg);
throw new ConfigParseError(msg);
const msg = `Could not read wiki configuration. Please ensure the page https://reddit.com${this.subreddit.url}wiki/${this.wikiLocation} exists and is readable${hint}`;
throw new ErrorWithCause(msg, {cause: err});
}
if (sourceData.replace('\r\n', '').trim() === '') {
@@ -580,8 +580,12 @@ export class Manager extends EventEmitter {
return true;
} catch (err: any) {
const error = new ErrorWithCause('Failed to parse subreddit configuration', {cause: err});
// @ts-ignore
//error.logged = true;
this.logger.error(error);
this.validConfigLoaded = false;
throw err;
throw error;
}
}
@@ -906,9 +910,9 @@ export class Manager extends EventEmitter {
}
if(!this.sharedStreamCallbacks.has(source)) {
stream.once('listing', this.noChecksWarning(source));
this.sharedStreamCallbacks.set(source, onItem);
this.logger.debug(`${removedOwn ? 'Stopped own polling and replace with ' : 'Set '}listener on shared polling ${source}`);
}
this.sharedStreamCallbacks.set(source, onItem);
} else {
let ownPollingMsgParts: string[] = [];
let removedShared = false;
@@ -937,14 +941,9 @@ export class Manager extends EventEmitter {
this.emit('error', err);
if (isRateLimitError(err)) {
this.logger.error('Encountered rate limit while polling! Bot is all out of requests :( Stopping subreddit queue and polling.');
await this.stop();
}
this.logger.error('Polling error occurred', err);
const shouldRetry = await this.pollingRetryHandler(err);
if (shouldRetry) {
stream.startInterval(false);
stream.startInterval(false, 'Within retry limits');
} else {
this.logger.warn('Stopping subreddit processing/polling due to too many errors');
await this.stop();
@@ -1140,7 +1139,6 @@ export class Manager extends EventEmitter {
s.end();
}
this.streams = new Map();
this.sharedStreamCallbacks = new Map();
this.startedAt = undefined;
this.logger.info(`Events STOPPED by ${causedBy}`);
this.eventsState = {

View File

@@ -1,10 +1,11 @@
import {Poll, SnooStormOptions} from "snoostorm"
import Snoowrap from "snoowrap";
import Snoowrap, {Listing} from "snoowrap";
import {EventEmitter} from "events";
import {PollConfiguration} from "snoostorm/out/util/Poll";
import {DEFAULT_POLLING_INTERVAL} from "../Common/interfaces";
import {mergeArr, parseDuration, random} from "../util";
import { Logger } from "winston";
import {ErrorWithCause} from "pony-cause";
type Awaitable<T> = Promise<T> | T;
@@ -18,11 +19,12 @@ interface RCBPollingOptions<T> extends SnooStormOptions {
}
interface RCBPollConfiguration<T> extends PollConfiguration<T>,RCBPollingOptions<T> {
get: () => Promise<Listing<T>>
}
export class SPoll<T extends object> extends Poll<T> {
identifier: keyof T;
getter: () => Awaitable<T[]>;
getter: () => Promise<Listing<T>>;
frequency;
running: boolean = false;
// intention of newStart is to make polling behavior such that only "new" items AFTER polling has started get emitted
@@ -82,6 +84,10 @@ export class SPoll<T extends object> extends Poll<T> {
// @ts-ignore
batch = await batch.fetchMore({amount: 100});
}
if(batch.length === 0 || batch.isFinished) {
// if nothing is returned we don't want to end up in an endless loop!
anyAlreadySeen = true;
}
for (const item of batch) {
const id = item[self.identifier];
if (self.processed.has(id)) {
@@ -99,7 +105,7 @@ export class SPoll<T extends object> extends Poll<T> {
}
page++;
}
const newItemMsg = `Found ${newItems.length} new items`;
const newItemMsg = `Found ${newItems.length} new items out of ${batch.length} returned`;
if(self.newStart) {
self.logger.debug(`${newItemMsg} but will ignore all on first start.`);
self.emit("listing", []);
@@ -113,6 +119,8 @@ export class SPoll<T extends object> extends Poll<T> {
// if everything succeeded then create a new timeout
self.createInterval();
} catch (err: any) {
self.running = false;
self.logger.error(new ErrorWithCause('Polling Interval stopped due to error encountered', {cause: err}));
self.emit('error', err);
}
}
@@ -120,15 +128,22 @@ export class SPoll<T extends object> extends Poll<T> {
}
// allow controlling newStart state
startInterval = (newStartState?: boolean) => {
startInterval = (newStartState?: boolean, msg?: string) => {
this.running = true;
if(newStartState !== undefined) {
this.newStart = newStartState;
}
const startMsg = `Polling Interval Started${msg !== undefined ? `: ${msg}` : ''}`;
this.logger.debug(startMsg)
this.createInterval();
}
end = () => {
end = (reason?: string) => {
let msg ='Stopping Polling Interval';
if(reason !== undefined) {
msg += `: ${reason}`;
}
this.logger.debug(msg);
this.running = false;
this.newStart = true;
super.end();

View File

@@ -1,22 +1,39 @@
import {StatusCodeError, RequestError} from "../Common/interfaces";
import {RateLimitError, RequestError, StatusCodeError} from 'snoowrap/dist/errors';
import ExtendableError from "es6-error";
import {ErrorWithCause} from "pony-cause";
export const isRateLimitError = (err: any) => {
return typeof err === 'object' && err.name === 'RateLimitError';
export const isRateLimitError = (err: any): err is RateLimitError => {
return isRequestError(err) && err.name === 'RateLimitError';
}
export const isScopeError = (err: any): boolean => {
if(typeof err === 'object' && err.name === 'StatusCodeError' && err.response !== undefined) {
if(isStatusError(err)) {
const authHeader = err.response.headers['www-authenticate'];
return authHeader !== undefined && authHeader.includes('insufficient_scope');
}
return false;
}
export const getScopeError = (err: any): string | undefined => {
if(isScopeError(err)) {
return err.response.headers['www-authenticate'];
}
return undefined;
}
export const isStatusError = (err: any): err is StatusCodeError => {
return typeof err === 'object' && err.name === 'StatusCodeError' && err.response !== undefined;
return isRequestError(err) && err.name === 'StatusCodeError';
}
export const isRequestError = (err: any): err is RequestError => {
return typeof err === 'object' && err.name === 'RequestError' && err.response !== undefined;
return typeof err === 'object' && err.response !== undefined;
}
export class SimpleError extends ExtendableError {
}
export class CMError extends ErrorWithCause {
logged: boolean = false;
}

View File

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

View File

@@ -29,10 +29,9 @@ import {
import UserNotes from "../Subreddit/UserNotes";
import {Logger} from "winston";
import InvalidRegexError from "./InvalidRegexError";
import SimpleError from "./SimpleError";
import {AuthorCriteria} from "../Author/Author";
import {URL} from "url";
import {isStatusError} from "./Errors";
import {SimpleError, isStatusError} from "./Errors";
import {Dictionary, ElementOf, SafeDictionary} from "ts-essentials";
export const BOT_LINK = 'https://www.reddit.com/r/ContextModBot/comments/otz396/introduction_to_contextmodbot';

View File

@@ -1,19 +1,28 @@
import {labelledFormat, logLevels} from "../util";
import winston, {Logger} from "winston";
import {DuplexTransport} from "winston-duplex";
import {LoggerFactoryOptions} from "../Common/interfaces";
import process from "process";
import path from "path";
const {transports} = winston;
export const getLogger = (options: any, name = 'app'): Logger => {
export const getLogger = (options: LoggerFactoryOptions, name = 'app'): Logger => {
if(!winston.loggers.has(name)) {
const {
path,
level,
additionalTransports = [],
defaultLabel = 'App',
file: {
dirname,
...fileRest
},
console,
stream
} = options || {};
const consoleTransport = new transports.Console({
...console,
handleExceptions: true,
handleRejections: true,
});
@@ -28,21 +37,39 @@ export const getLogger = (options: any, name = 'app'): Logger => {
objectMode: true,
},
name: 'duplex',
dump: false,
handleExceptions: true,
handleRejections: true,
...stream,
dump: false,
}),
...additionalTransports,
];
if (path !== undefined && path !== '') {
if (dirname !== undefined && dirname !== '' && dirname !== null) {
let realDir: string | undefined;
if(typeof dirname === 'boolean') {
if(!dirname) {
realDir = undefined;
} else {
realDir = path.resolve(__dirname, '../../logs')
}
} else if(dirname === 'true') {
realDir = path.resolve(__dirname, '../../logs')
} else if(dirname === 'false') {
realDir = undefined;
} else {
realDir = dirname;
}
const rotateTransport = new winston.transports.DailyRotateFile({
dirname: path,
createSymlink: true,
symlinkName: 'contextBot-current.log',
filename: 'contextBot-%DATE%.log',
datePattern: 'YYYY-MM-DD',
maxSize: '5m',
dirname: realDir,
...fileRest,
handleExceptions: true,
handleRejections: true,
});

View File

@@ -24,7 +24,6 @@ import EventEmitter from "events";
import stream, {Readable, Writable, Transform} from "stream";
import winston from "winston";
import tcpUsed from "tcp-port-used";
import SimpleError from "../../Utils/SimpleError";
import http from "http";
import jwt from 'jsonwebtoken';
import {Server as SocketServer} from "socket.io";
@@ -49,6 +48,8 @@ import {ExtendedSnoowrap} from "../../Utils/SnoowrapClients";
import ClientUser from "../Common/User/ClientUser";
import {BotStatusResponse} from "../Common/interfaces";
import {TransformableInfo} from "logform";
import {SimpleError} from "../../Utils/Errors";
import {ErrorWithCause} from "pony-cause";
const emitter = new EventEmitter();
@@ -630,9 +631,7 @@ const webClient = async (options: OperatorConfig) => {
server = await app.listen(port);
io = new SocketServer(server);
} catch (err: any) {
logger.error('Error occurred while initializing web or socket.io server', err);
err.logged = true;
throw err;
throw new ErrorWithCause('[Web] Error occurred while initializing web or socket.io server', {cause: err});
}
logger.info(`Web UI started: http://localhost:${port}`, {label: ['Web']});

View File

@@ -18,7 +18,6 @@ import {getLogger} from "../../Utils/loggerFactory";
import LoggedError from "../../Utils/LoggedError";
import {Invokee, LogInfo, OperatorConfigWithFileContext} from "../../Common/interfaces";
import http from "http";
import SimpleError from "../../Utils/SimpleError";
import {heartbeat} from "./routes/authenticated/applicationRoutes";
import logs from "./routes/authenticated/user/logs";
import status from './routes/authenticated/user/status';
@@ -30,6 +29,8 @@ import Bot from "../../Bot";
import addBot from "./routes/authenticated/user/addBot";
import dayjs from "dayjs";
import ServerUser from "../Common/User/ServerUser";
import {SimpleError} from "../../Utils/Errors";
import {ErrorWithCause} from "pony-cause";
const server = addAsync(express());
server.use(bodyParser.json());
@@ -116,9 +117,7 @@ const rcbServer = async function (options: OperatorConfigWithFileContext) {
httpServer = await server.listen(port);
io = new SocketServer(httpServer);
} catch (err: any) {
logger.error('Error occurred while initializing web or socket.io server', err);
err.logged = true;
throw err;
throw new ErrorWithCause('[Server] Error occurred while initializing web or socket.io server', {cause: err});
}
logger.info(`API started => localhost:${port}`);

View File

@@ -205,9 +205,6 @@ const program = new Command();
} catch (err: any) {
if (!err.logged && !(err instanceof LoggedError)) {
const logger = winston.loggers.has('app') ? winston.loggers.get('app') : winston.loggers.get('init');
if(isScopeError(err)) {
logger.error('Reddit responded with a 403 insufficient_scope which means the bot is lacking necessary OAUTH scopes to perform general actions.');
}
logger.error(err);
}
process.kill(process.pid, 'SIGTERM');

View File

@@ -29,13 +29,12 @@ import {
RedditEntityType,
RegExResult, RepostItem, RepostItemResult,
ResourceStats, SearchAndReplaceRegExp,
StatusCodeError, StringComparisonOptions,
StringComparisonOptions,
StringOperator,
StrongSubredditState,
SubredditState
} from "./Common/interfaces";
import { Document as YamlDocument } from 'yaml'
import SimpleError from "./Utils/SimpleError";
import InvalidRegexError from "./Utils/InvalidRegexError";
import {constants, promises} from "fs";
import {cacheOptDefaults} from "./Common/defaults";
@@ -44,24 +43,24 @@ import redisStore from "cache-manager-redis-store";
import crypto from "crypto";
import Autolinker from 'autolinker';
import {create as createMemoryStore} from './Utils/memoryStore';
import {MESSAGE} from "triple-beam";
import {MESSAGE, LEVEL} from "triple-beam";
import {RedditUser} from "snoowrap/dist/objects";
import reRegExp from '@stdlib/regexp-regexp';
import fetch, {Response} from "node-fetch";
import { URL } from "url";
import ImageData from "./Common/ImageData";
import {Sharp, SharpOptions} from "sharp";
// @ts-ignore
import {blockhashData, hammingDistance} from 'blockhash';
import {ErrorWithCause, stackWithCauses} from "pony-cause";
import {ConfigFormat, SetRandomInterval} from "./Common/types";
import stringSimilarity from 'string-similarity';
import calculateCosineSimilarity from "./Utils/StringMatching/CosineSimilarity";
import levenSimilarity from "./Utils/StringMatching/levenSimilarity";
import {isRequestError, isStatusError} from "./Utils/Errors";
import {SimpleError, isRateLimitError, isRequestError, isScopeError, isStatusError, CMError} from "./Utils/Errors";
import {parse} from "path";
import JsonConfigDocument from "./Common/Config/JsonConfigDocument";
import YamlConfigDocument from "./Common/Config/YamlConfigDocument";
import AbstractConfigDocument, {ConfigDocumentInterface} from "./Common/Config/AbstractConfigDocument";
import LoggedError from "./Utils/LoggedError";
//import {ResembleSingleCallbackComparisonResult} from "resemblejs";
@@ -90,35 +89,77 @@ const CWD = process.cwd();
// }
// }
const errorAwareFormat = {
transform: (info: any, opts: any) => {
// don't need to log stack trace if we know the error is just a simple message (we handled it)
const stack = !(info instanceof SimpleError) && !(info.message instanceof SimpleError);
const {name, response, message, stack: errStack, error, statusCode} = info;
if(name === 'StatusCodeError' && response !== undefined && response.headers !== undefined && response.headers['content-type'].includes('html')) {
// reddit returns html even when we specify raw_json in the querystring (via snoowrap)
// which means the html gets set as the message for the error AND gets added to the stack as the message
// and we end up with a h u g e log statement full of noisy html >:(
transform: (einfo: any, {stack = true}: any = {}) => {
const errorSample = error.slice(0, 10);
const messageBeforeIndex = message.indexOf(errorSample);
let newMessage = `Status Error ${statusCode} from Reddit`;
if(messageBeforeIndex > 0) {
newMessage = `${message.slice(0, messageBeforeIndex)} - ${newMessage}`;
}
let cleanStack = errStack;
// because winston logger.child() re-assigns its input to an object ALWAYS the object we recieve here will never actually be of type Error
const includeStack = stack && (!isProbablyError(einfo, 'simpleerror') && !isProbablyError(einfo.message, 'simpleerror'));
// try to get just stacktrace by finding beginning of what we assume is the actual trace
if(errStack) {
cleanStack = `${newMessage}\n${errStack.slice(errStack.indexOf('at new StatusCodeError'))}`;
}
// now put it all together so its nice and clean
info.message = newMessage;
info.stack = cleanStack;
if (!isProbablyError(einfo.message) && !isProbablyError(einfo)) {
return einfo;
}
return errors().transform(info, { stack });
let info: any = {};
if (isProbablyError(einfo)) {
const tinfo = transformError(einfo);
info = Object.assign({}, tinfo, {
// @ts-ignore
level: einfo.level,
// @ts-ignore
[LEVEL]: einfo[LEVEL] || einfo.level,
message: tinfo.message,
// @ts-ignore
[MESSAGE]: tinfo[MESSAGE] || tinfo.message
});
if(includeStack) {
// so we have to create a dummy error and re-assign all error properties from our info object to it so we can get a proper stack trace
const dummyErr = new ErrorWithCause('');
for(const k in tinfo) {
if(dummyErr.hasOwnProperty(k) || k === 'cause') {
// @ts-ignore
dummyErr[k] = tinfo[k];
}
}
// @ts-ignore
info.stack = stackWithCauses(dummyErr);
}
} else {
const err = transformError(einfo.message);
info = Object.assign(einfo, err);
// @ts-ignore
info.message = err.message;
// @ts-ignore
info[MESSAGE] = err.message;
if(includeStack) {
const dummyErr = new ErrorWithCause('');
for(const k in err) {
if(dummyErr.hasOwnProperty(k) || k === 'cause') {
// @ts-ignore
dummyErr[k] = info[k];
}
}
// @ts-ignore
info.stack = stackWithCauses(dummyErr);
}
}
// remove redundant message from stack and make stack causes easier to read
if(info.stack !== undefined) {
let cleanedStack = info.stack.replace(info.message, '');
cleanedStack = `${cleanedStack}`;
cleanedStack = cleanedStack.replaceAll('caused by:', '\ncaused by:');
info.stack = cleanedStack;
}
return info;
}
}
const isProbablyError = (val: any, errName = 'error') => {
return typeof val === 'object' && val.name !== undefined && val.name.toLowerCase().includes(errName);
}
export const PASS = '✔';
export const FAIL = '✘';
@@ -900,6 +941,11 @@ export const createRetryHandler = (opts: RetryOptions, logger: Logger) => {
lastErrorAt = dayjs();
if(isRateLimitError(err)) {
logger.error('Will not retry because error was due to ratelimit exhaustion');
return false;
}
const redditApiError = isRequestError(err) || isStatusError(err);
if(redditApiError) {
@@ -937,6 +983,119 @@ export const createRetryHandler = (opts: RetryOptions, logger: Logger) => {
}
}
type StringReturn = (err:any) => string;
export interface LogMatch {
[key: string | number]: string | StringReturn
}
export interface logExceptionOptions {
context?: string
logIfNotMatched?: boolean
logStackTrace?: boolean
match?: LogMatch
}
export const parseMatchMessage = (err: any, match: LogMatch, matchTypes: (string | number)[], defaultMatch: string): [string, boolean] => {
for(const m of matchTypes) {
if(match[m] !== undefined) {
if(typeof match[m] === 'string') {
return [match[m] as string, true];
}
return [(match[m] as Function)(err), true];
}
}
return [defaultMatch, false];
}
export const getExceptionMessage = (err: any, match: LogMatch = {}): string | undefined => {
let matched = false,
matchMsg;
if (isRequestError(err)) {
if (isRateLimitError(err)) {
([matchMsg, matched] = parseMatchMessage(err, match, ['ratelimit', err.statusCode], 'Ratelimit Exhausted'));
} else if (isScopeError(err)) {
([matchMsg, matched] = parseMatchMessage(err, match, ['scope', err.statusCode], 'Missing OAUTH scope required for this request'));
} else {
([matchMsg, matched] = parseMatchMessage(err, match, [err.statusCode], err.message));
}
} else {
([matchMsg, matched] = parseMatchMessage(err, match, ['any'], err.message));
}
if (matched) {
return matchMsg;
}
}
const _transformError = (err: Error, seen: Set<Error>, matchOptions?: LogMatch) => {
if (!err || !isProbablyError(err)) {
return '';
}
if (seen.has(err)) {
return err;
}
// @ts-ignore
let mOpts = err.matchOptions ?? matchOptions;
if (isRequestError(err)) {
const errMsgParts = [`Reddit responded with a NOT OK status (${err.statusCode})`];
if (err.response.headers['content-type'].includes('html')) {
// reddit returns html even when we specify raw_json in the querystring (via snoowrap)
// which means the html gets set as the message for the error AND gets added to the stack as the message
// and we end up with a h u g e log statement full of noisy html >:(
const {error, statusCode, message, stack: errStack} = err;
const errorSample = (error as unknown as string).slice(0, 10);
const messageBeforeIndex = message.indexOf(errorSample);
let newMessage = `Status Error ${statusCode} from Reddit`;
if (messageBeforeIndex > 0) {
newMessage = `${message.slice(0, messageBeforeIndex)} - ${newMessage}`;
}
let cleanStack = errStack;
// try to get just stacktrace by finding beginning of what we assume is the actual trace
if (errStack) {
cleanStack = `${newMessage}\n${errStack.slice(errStack.indexOf('at new StatusCodeError'))}`;
}
// now put it all together so its nice and clean
err.message = newMessage;
err.stack = cleanStack;
}
const msg = getExceptionMessage(err, mOpts);
if (msg !== undefined) {
errMsgParts.push(msg);
}
// we don't care about stack trace for this error because we know where it came from so truncate to two lines for now...maybe remove all together later
if(err.stack !== undefined) {
err.stack = err.stack.split('\n').slice(0, 2).join('\n');
}
const normalizedError = new ErrorWithCause(errMsgParts.join(' => '), {cause: err});
normalizedError.stack = normalizedError.message;
return normalizedError;
}
// @ts-ignore
const cause = err.cause as unknown;
if (cause !== undefined && cause instanceof Error) {
// @ts-ignore
err.cause = _transformError(cause, seen, mOpts);
}
return err;
}
export const transformError = (err: Error): any => _transformError(err, new Set());
const LABELS_REGEX: RegExp = /(\[.+?])*/g;
export const parseLabels = (log: string): string[] => {
return Array.from(log.matchAll(LABELS_REGEX), m => m[0]).map(x => x.substring(1, x.length - 1));
@@ -1000,12 +1159,14 @@ export const formatLogLineToHtml = (log: string | LogInfo, timestamp?: string) =
.replace(/(\s*verbose\s*):/i, '<span class="error purple">$1</span>:')
.replaceAll('\n', '<br />');
//.replace(HYPERLINK_REGEX, '<a target="_blank" href="$&">$&</a>');
let line = `<div class="logLine">${logContent}</div>`
let line = '';
if(timestamp !== undefined) {
line = line.replace(timestamp, (match) => {
return formattedTime(dayjs(match).format('HH:mm:ss z'), match);
});
const timeStampReplacement = formattedTime(dayjs(timestamp).format('HH:mm:ss z'), timestamp);
const splitLine = logContent.split(timestamp);
line = `<div class="logLine">${splitLine[0]}${timeStampReplacement}<span style="white-space: pre-wrap">${splitLine[1]}</span></div>`;
} else {
line = `<div style="white-space: pre-wrap" class="logLine">${logContent}</div>`
}
return line;
}

View File

@@ -5,7 +5,8 @@
"resolveJsonModule": true,
"typeRoots": [
"./node_modules/@types",
"./src/Web/types"
"./src/Web/types",
"./src/Common/typings"
]
},
// "compilerOptions": {