Fix broken nobot detection for gowin

This commit is contained in:
Pieter Vander Vennet 2024-06-18 12:52:54 +02:00
parent 61482d19cc
commit 8796a589e8
5 changed files with 61 additions and 35 deletions

View file

@ -66,8 +66,6 @@ export default interface Config {
mastodonAuth: LoginSettings & { mastodonAuth: LoginSettings & {
/** IF set: prints to console instead of to Mastodon*/ /** IF set: prints to console instead of to Mastodon*/
dryrun?: boolean dryrun?: boolean
}, }
actions: MapCompleteUsageOverview [] actions: MapCompleteUsageOverview []
} }

View file

@ -1,5 +1,6 @@
import {login, LoginParams} from 'masto'; import {login, LoginParams} from 'masto';
import * as fs from "fs"; import * as fs from "fs";
import Utils from "./Utils";
export interface LoginSettings { export interface LoginSettings {
url: string, url: string,
@ -100,17 +101,20 @@ ${text.split("\n").map(txt => " > " + txt).join("\n")}`)
return statusUpdate return statusUpdate
} }
public async hasNoBot(username: string): Promise<boolean> { public async hasNoBot(username: string): Promise<boolean> {
const info = await this.userInfoFor(username) const info = await this.userInfoFor(username)
if (info === undefined) { if (info === undefined) {
return false return false
} }
const descrParts = info.note?.replace(/-/g, "")?.toLowerCase()?.split(" ") ?? [] const descrParts = Utils.stripHtmlToInnerText(info.note)?.replace(/-/g, "")?.toLowerCase() ?? ""
if (descrParts.indexOf("#nobot") >= 0 || descrParts.indexOf("#nomapcompletebot") >= 0) { if (descrParts.indexOf("#nobot") >= 0 || descrParts.indexOf("#nomapcompletebot") >= 0) {
console.log("Found nobot in mastodon description for", username)
return true return true
} }
const nobot = info.fields.find(f => f.name === "nobot")?.value ?? "" const nobot = info.fields.find(f => f.name === "nobot")?.value ?? ""
if (nobot.toLowerCase() === "yes" || nobot.toLowerCase() === "true") { if (nobot.toLowerCase() === "yes" || nobot.toLowerCase() === "true") {
console.log("Found nobot in mastodon fields for", username)
return true return true
} }
return false return false

View file

@ -1,6 +1,7 @@
import https from "https"; import https from "https";
import * as fs from "fs"; import * as fs from "fs";
import {DOMParser} from '@xmldom/xmldom' import {DOMParser} from '@xmldom/xmldom'
import * as fakedom from "fake-dom"
export default class Utils { export default class Utils {
public static async DownloadJson(url, headers?: any): Promise<any> { public static async DownloadJson(url, headers?: any): Promise<any> {
@ -105,4 +106,24 @@ export default class Utils {
}); });
}); });
} }
public static stripHtmlToInnerText(htmlText: string): string;
public static stripHtmlToInnerText(htmlText: string | undefined): string | undefined;
/**
*
* const input = "#<span>nobot</span>"
* Utils.stripHtmlToInnerText(input) // => "#nobot"
*/
public static stripHtmlToInnerText(htmlText: string | undefined): string | undefined {
if (fakedom === undefined || window === undefined) {
throw "FakeDom not initialized"
}
if (htmlText === undefined) {
return undefined
}
const el = document.createElement("div")
el.innerHTML = htmlText
return el.textContent
}
} }

View file

@ -10,7 +10,7 @@ export class Main {
private readonly _config: Config; private readonly _config: Config;
constructor(config: string | Config) { constructor(config: string | Config, dryrun = false) {
if (config === undefined) { if (config === undefined) {
console.log("Needs an argument: path of config file") console.log("Needs an argument: path of config file")
throw "No path given" throw "No path given"
@ -20,6 +20,9 @@ export class Main {
} else { } else {
this._config = config; this._config = config;
} }
if (dryrun) {
this._config.mastodonAuth.dryrun = true
}
this._config.osmBackend ??= "https://www.openstreetmap.org" this._config.osmBackend ??= "https://www.openstreetmap.org"
} }
@ -100,4 +103,4 @@ export class Main {
} }
new Main(process.argv[2]).main().then(_ => console.log("All done")) new Main(process.argv[2], process.argv[3] !== undefined).main().then(_ => console.log("All done"))