From 8796a589e81368560e0c7a1e9cb3814549b8b1c9 Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Tue, 18 Jun 2024 12:52:54 +0200 Subject: [PATCH] Fix broken nobot detection for gowin --- src/Config.ts | 4 +--- src/Mastodon.ts | 22 +++++++++++++--------- src/OsmUserInfo.ts | 32 ++++++++++++++++---------------- src/Utils.ts | 31 ++++++++++++++++++++++++++----- src/index.ts | 7 +++++-- 5 files changed, 61 insertions(+), 35 deletions(-) diff --git a/src/Config.ts b/src/Config.ts index b056ead..5370bb1 100644 --- a/src/Config.ts +++ b/src/Config.ts @@ -66,8 +66,6 @@ export default interface Config { mastodonAuth: LoginSettings & { /** IF set: prints to console instead of to Mastodon*/ dryrun?: boolean - }, + } actions: MapCompleteUsageOverview [] - - } \ No newline at end of file diff --git a/src/Mastodon.ts b/src/Mastodon.ts index 5638ba8..f7c4900 100644 --- a/src/Mastodon.ts +++ b/src/Mastodon.ts @@ -1,5 +1,6 @@ import {login, LoginParams} from 'masto'; import * as fs from "fs"; +import Utils from "./Utils"; export interface LoginSettings { url: string, @@ -48,24 +49,24 @@ export default class MastodonPoster { * Returns the length, counting a link as 23 characters * @param text */ - public static length23(text: string): number{ + public static length23(text: string): number { const splitted = text.split(" ") - + let total = 0; for (const piece of splitted) { - try{ + try { // This is a link, it counts for 23 characters // https://docs.joinmastodon.org/user/posting/#links new URL(piece) - total += 23 - }catch(e){ + total += 23 + } catch (e) { total += piece.length } } // add the spaces total += splitted.length - 1 return total - + } public async writeMessage(text: string, options?: CreateStatusParamsBase): Promise<{ id: string }> { @@ -85,11 +86,11 @@ export default class MastodonPoster { } if (this._dryrun) { - console.log("Dryrun enabled - not posting", options?.visibility ?? "public", `message (length ${text.length}, link23: ${MastodonPoster.length23(text)}): + console.log("Dryrun enabled - not posting", options?.visibility ?? "public", `message (length ${text.length}, link23: ${MastodonPoster.length23(text)}): ${text.split("\n").map(txt => " > " + txt).join("\n")}`) return {id: "some_id"} } - console.log("Uploading message", text.substring(0, 25)+"...", `(length ${text.length}, link23: ${MastodonPoster.length23(text)})`) + console.log("Uploading message", text.substring(0, 25) + "...", `(length ${text.length}, link23: ${MastodonPoster.length23(text)})`) console.log(text.split("\n").map(txt => " > " + txt).join("\n")) const statusUpdate = await this.instance.v1.statuses.create({ visibility: 'public', @@ -100,17 +101,20 @@ ${text.split("\n").map(txt => " > " + txt).join("\n")}`) return statusUpdate } + public async hasNoBot(username: string): Promise { const info = await this.userInfoFor(username) if (info === undefined) { 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) { + console.log("Found nobot in mastodon description for", username) return true } const nobot = info.fields.find(f => f.name === "nobot")?.value ?? "" if (nobot.toLowerCase() === "yes" || nobot.toLowerCase() === "true") { + console.log("Found nobot in mastodon fields for", username) return true } return false diff --git a/src/OsmUserInfo.ts b/src/OsmUserInfo.ts index 7778a5a..c46521e 100644 --- a/src/OsmUserInfo.ts +++ b/src/OsmUserInfo.ts @@ -28,7 +28,7 @@ export default class OsmUserInfo { osmBackend?: string, cacheDir?: string }) { - if(userId === undefined || userId === null || Number.isNaN(userId)){ + if (userId === undefined || userId === null || Number.isNaN(userId)) { throw new Error("Invalid userid: " + userId) } this._userId = userId; @@ -41,15 +41,15 @@ export default class OsmUserInfo { } } - + public async hasNoBotTag(): Promise<{ nobot: boolean, nomention: boolean - }>{ + }> { const description = (await this.getUserInfo()).description ?? "" const split = description.toLowerCase().replace(/-/g, "").split(" ") - const nobot = split.indexOf("#nobot") >=0 || split.indexOf("#nomapcompletebot") >= 0 - const nomention = split.indexOf("#nobotmention") >=0 || split.indexOf("#nomapcompletebotmention") >= 0 + const nobot = split.indexOf("#nobot") >= 0 || split.indexOf("#nomapcompletebot") >= 0 + const nomention = split.indexOf("#nobotmention") >= 0 || split.indexOf("#nomapcompletebotmention") >= 0 return {nobot, nomention} } @@ -60,7 +60,7 @@ export default class OsmUserInfo { */ public async GetMastodonUsername(mastodonApi: MastodonPoster): Promise { const {nomention} = await this.hasNoBotTag() - if(nomention){ + if (nomention) { return undefined } const mastodonLinks = await this.getMeLinks() @@ -68,22 +68,22 @@ export default class OsmUserInfo { if (mastodonLinks.length <= 0) { return undefined } - + const url = new URL(mastodonLinks[0]) - const username = url.pathname.substring(1) +(url.host === mastodonApi.hostname ? "" : "@" + url.host) - - if(await mastodonApi.hasNoBot(username)){ + const username = url.pathname.substring(1) + (url.host === mastodonApi.hostname ? "" : "@" + url.host) + + if (await mastodonApi.hasNoBot(username)) { return undefined } let useraccount = (await mastodonApi.userInfoFor(username))?.acct - if(useraccount === undefined){ + if (useraccount === undefined) { useraccount = username } - if(!useraccount.startsWith("@")){ - useraccount = "@"+useraccount + if (!useraccount.startsWith("@")) { + useraccount = "@" + useraccount } return useraccount - } + } public async getMeLinks(): Promise { const userdata = await this.getUserInfo() @@ -103,8 +103,8 @@ export default class OsmUserInfo { const cacheAgeInSeconds = (Date.now() - cacheCreatedTime.getTime()) / 1000 if (cacheAgeInSeconds > OsmUserInfo.max_cache_age_seconds) { console.log("Cache is old, unlinking...") - }else{ - + } else { + try { this._userData = JSON.parse(fs.readFileSync(this._cachingPath, "utf8")) return this._userData diff --git a/src/Utils.ts b/src/Utils.ts index 205fa67..6b89186 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -1,15 +1,16 @@ import https from "https"; import * as fs from "fs"; import {DOMParser} from '@xmldom/xmldom' +import * as fakedom from "fake-dom" export default class Utils { public static async DownloadJson(url, headers?: any): Promise { const data = await Utils.Download(url, headers) - try{ - - return JSON.parse(data.content) - }catch (e) { - console.log("Could not parse the result of ", url,": not a valid json:\n ",data.content) + try { + + return JSON.parse(data.content) + } catch (e) { + console.log("Could not parse the result of ", url, ": not a valid json:\n ", data.content) throw e } } @@ -105,4 +106,24 @@ export default class Utils { }); }); } + + public static stripHtmlToInnerText(htmlText: string): string; + public static stripHtmlToInnerText(htmlText: string | undefined): string | undefined; + + /** + * + * const input = "#nobot" + * 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 + } } \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index d8483d4..7c81c03 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,7 +10,7 @@ export class Main { private readonly _config: Config; - constructor(config: string | Config) { + constructor(config: string | Config, dryrun = false) { if (config === undefined) { console.log("Needs an argument: path of config file") throw "No path given" @@ -20,6 +20,9 @@ export class Main { } else { this._config = config; } + if (dryrun) { + this._config.mastodonAuth.dryrun = true + } 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"))