unescape content before report/comment

This commit is contained in:
FoxxMD
2021-06-11 10:36:37 -04:00
parent 1f439dc290
commit f6b3f02e05
5 changed files with 36 additions and 24 deletions

27
package-lock.json generated
View File

@@ -13,6 +13,7 @@
"dayjs": "^1.10.5",
"es6-error": "^4.1.1",
"fast-deep-equal": "^3.1.3",
"he": "^1.2.0",
"json5": "^2.2.0",
"memory-cache": "^0.2.0",
"mustache": "^4.2.0",
@@ -26,6 +27,7 @@
},
"devDependencies": {
"@tsconfig/node14": "^1.0.0",
"@types/he": "^1.1.1",
"@types/memory-cache": "^0.2.1",
"@types/minimist": "^1.2.1",
"@types/mustache": "^4.1.1",
@@ -129,6 +131,12 @@
"integrity": "sha512-/xUgezxxYePeXhg5S04hUjxG9JZi+rJTs1+4NwpYPfSaS7BeDa6tVJkH6lN9Cb6rl8d24Fi2uX0s0Ngg2JT6gg==",
"dev": true
},
"node_modules/@types/he": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/@types/he/-/he-1.1.1.tgz",
"integrity": "sha512-jpzrsR1ns0n3kyWt92QfOUQhIuJGQ9+QGa7M62rO6toe98woQjnsnzjdMtsQXCdvjjmqjS2ZBCC7xKw0cdzU+Q==",
"dev": true
},
"node_modules/@types/json-schema": {
"version": "7.0.7",
"resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.7.tgz",
@@ -838,6 +846,14 @@
"node": ">=4"
}
},
"node_modules/he": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz",
"integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==",
"bin": {
"he": "bin/he"
}
},
"node_modules/http-signature": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/http-signature/-/http-signature-1.2.0.tgz",
@@ -2254,6 +2270,12 @@
"integrity": "sha512-/xUgezxxYePeXhg5S04hUjxG9JZi+rJTs1+4NwpYPfSaS7BeDa6tVJkH6lN9Cb6rl8d24Fi2uX0s0Ngg2JT6gg==",
"dev": true
},
"@types/he": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/@types/he/-/he-1.1.1.tgz",
"integrity": "sha512-jpzrsR1ns0n3kyWt92QfOUQhIuJGQ9+QGa7M62rO6toe98woQjnsnzjdMtsQXCdvjjmqjS2ZBCC7xKw0cdzU+Q==",
"dev": true
},
"@types/json-schema": {
"version": "7.0.7",
"resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.7.tgz",
@@ -2850,6 +2872,11 @@
"integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=",
"dev": true
},
"he": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz",
"integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw=="
},
"http-signature": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/http-signature/-/http-signature-1.2.0.tgz",

View File

@@ -29,6 +29,7 @@
"dayjs": "^1.10.5",
"es6-error": "^4.1.1",
"fast-deep-equal": "^3.1.3",
"he": "^1.2.0",
"json5": "^2.2.0",
"memory-cache": "^0.2.0",
"mustache": "^4.2.0",
@@ -42,6 +43,7 @@
},
"devDependencies": {
"@tsconfig/node14": "^1.0.0",
"@types/he": "^1.1.1",
"@types/memory-cache": "^0.2.1",
"@types/minimist": "^1.2.1",
"@types/mustache": "^4.1.1",

View File

@@ -1,19 +1,12 @@
import Action, {ActionJson, ActionOptions} from "./index";
import {Comment} from "snoowrap";
import Submission from "snoowrap/dist/objects/Submission";
import dayjs, {Dayjs} from "dayjs";
import {renderContent} from "../Utils/SnoowrapUtils";
import {RichContent} from "../Common/interfaces";
import {RuleResult} from "../Rule";
import LoggedError from "../Utils/LoggedError";
export const WIKI_DESCRIM = 'wiki:';
export class CommentAction extends Action {
content: string;
hasWiki: boolean;
wiki?: string;
wikiFetched?: Dayjs;
lock: boolean = false;
sticky: boolean = false;
distinguish: boolean = false;
@@ -26,11 +19,7 @@ export class CommentAction extends Action {
sticky = false,
distinguish = false,
} = options;
this.hasWiki = content.trim().substring(0, WIKI_DESCRIM.length) === WIKI_DESCRIM;
this.content = content;
if (this.hasWiki) {
this.wiki = this.content.trim().substring(WIKI_DESCRIM.length);
}
this.lock = lock;
this.sticky = sticky;
this.distinguish = distinguish;
@@ -41,18 +30,10 @@ export class CommentAction extends Action {
}
async process(item: Comment | Submission, ruleResults: RuleResult[]): Promise<void> {
if (this.hasWiki && (this.wikiFetched === undefined || Math.abs(dayjs().diff(this.wikiFetched, 'minute')) > 5)) {
try {
const wiki = item.subreddit.getWikiPage(this.wiki as string);
this.content = await wiki.content_md;
this.wikiFetched = dayjs();
} catch (err) {
this.logger.error(`Could not read wiki page. Please ensure the page '${this.wiki}' exists and is readable`, err);
throw new LoggedError(`Could not read wiki page. Please ensure the page '${this.wiki}' exists and is readable`);
}
}
const content = await this.cache.getContent(this.content, item.subreddit);
const renderedContent = await renderContent(content, item, ruleResults);
// @ts-ignore
const reply: Comment = await item.reply(renderContent(this.content, item, ruleResults));
const reply: Comment = await item.reply(he.decode(renderedContent));
if (this.lock) {
if(item instanceof Submission) {
// @ts-ignore

View File

@@ -1,5 +1,6 @@
import {ActionJson, ActionConfig, ActionOptions} from "./index";
import Action from "./index";
import he from "he";
import Snoowrap, {Comment, Submission} from "snoowrap";
import {truncateStringToLength} from "../util";
import {renderContent} from "../Utils/SnoowrapUtils";
@@ -25,7 +26,7 @@ export class ReportAction extends Action {
async process(item: Comment | Submission, ruleResults: RuleResult[]): Promise<void> {
const content = await this.cache.getContent(this.content, item.subreddit);
const renderedContent = await renderContent(content, item, ruleResults);
const truncatedContent = reportTrunc(renderedContent);
const truncatedContent = he.decode(reportTrunc(renderedContent));
// @ts-ignore
await item.report({reason: truncatedContent});
}

View File

@@ -10,11 +10,12 @@ import {
import Subreddit from 'snoowrap/dist/objects/Subreddit';
import winston, {Logger} from "winston";
import {mergeArr} from "../util";
import {WIKI_DESCRIM} from "../Action/CommentAction";
import LoggedError from "../Utils/LoggedError";
import {SubredditCacheConfig} from "../Common/interfaces";
import {AuthorCriteria} from "../Rule";
export const WIKI_DESCRIM = 'wiki:';
export interface SubredditCacheOptions extends SubredditCacheConfig {
enabled: boolean;
logger?: Logger;