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,
@ -48,24 +49,24 @@ export default class MastodonPoster {
* Returns the length, counting a link as 23 characters * Returns the length, counting a link as 23 characters
* @param text * @param text
*/ */
public static length23(text: string): number{ public static length23(text: string): number {
const splitted = text.split(" ") const splitted = text.split(" ")
let total = 0; let total = 0;
for (const piece of splitted) { for (const piece of splitted) {
try{ try {
// This is a link, it counts for 23 characters // This is a link, it counts for 23 characters
// https://docs.joinmastodon.org/user/posting/#links // https://docs.joinmastodon.org/user/posting/#links
new URL(piece) new URL(piece)
total += 23 total += 23
}catch(e){ } catch (e) {
total += piece.length total += piece.length
} }
} }
// add the spaces // add the spaces
total += splitted.length - 1 total += splitted.length - 1
return total return total
} }
public async writeMessage(text: string, options?: CreateStatusParamsBase): Promise<{ id: string }> { public async writeMessage(text: string, options?: CreateStatusParamsBase): Promise<{ id: string }> {
@ -85,11 +86,11 @@ export default class MastodonPoster {
} }
if (this._dryrun) { 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")}`) ${text.split("\n").map(txt => " > " + txt).join("\n")}`)
return {id: "some_id"} 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")) console.log(text.split("\n").map(txt => " > " + txt).join("\n"))
const statusUpdate = await this.instance.v1.statuses.create({ const statusUpdate = await this.instance.v1.statuses.create({
visibility: 'public', visibility: 'public',
@ -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

@ -28,7 +28,7 @@ export default class OsmUserInfo {
osmBackend?: string, osmBackend?: string,
cacheDir?: 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) throw new Error("Invalid userid: " + userId)
} }
this._userId = userId; this._userId = userId;
@ -41,15 +41,15 @@ export default class OsmUserInfo {
} }
} }
public async hasNoBotTag(): Promise<{ public async hasNoBotTag(): Promise<{
nobot: boolean, nobot: boolean,
nomention: boolean nomention: boolean
}>{ }> {
const description = (await this.getUserInfo()).description ?? "" const description = (await this.getUserInfo()).description ?? ""
const split = description.toLowerCase().replace(/-/g, "").split(" ") const split = description.toLowerCase().replace(/-/g, "").split(" ")
const nobot = split.indexOf("#nobot") >=0 || split.indexOf("#nomapcompletebot") >= 0 const nobot = split.indexOf("#nobot") >= 0 || split.indexOf("#nomapcompletebot") >= 0
const nomention = split.indexOf("#nobotmention") >=0 || split.indexOf("#nomapcompletebotmention") >= 0 const nomention = split.indexOf("#nobotmention") >= 0 || split.indexOf("#nomapcompletebotmention") >= 0
return {nobot, nomention} return {nobot, nomention}
} }
@ -60,7 +60,7 @@ export default class OsmUserInfo {
*/ */
public async GetMastodonUsername(mastodonApi: MastodonPoster): Promise<string | undefined> { public async GetMastodonUsername(mastodonApi: MastodonPoster): Promise<string | undefined> {
const {nomention} = await this.hasNoBotTag() const {nomention} = await this.hasNoBotTag()
if(nomention){ if (nomention) {
return undefined return undefined
} }
const mastodonLinks = await this.getMeLinks() const mastodonLinks = await this.getMeLinks()
@ -68,22 +68,22 @@ export default class OsmUserInfo {
if (mastodonLinks.length <= 0) { if (mastodonLinks.length <= 0) {
return undefined return undefined
} }
const url = new URL(mastodonLinks[0]) const url = new URL(mastodonLinks[0])
const username = url.pathname.substring(1) +(url.host === mastodonApi.hostname ? "" : "@" + url.host) const username = url.pathname.substring(1) + (url.host === mastodonApi.hostname ? "" : "@" + url.host)
if(await mastodonApi.hasNoBot(username)){ if (await mastodonApi.hasNoBot(username)) {
return undefined return undefined
} }
let useraccount = (await mastodonApi.userInfoFor(username))?.acct let useraccount = (await mastodonApi.userInfoFor(username))?.acct
if(useraccount === undefined){ if (useraccount === undefined) {
useraccount = username useraccount = username
} }
if(!useraccount.startsWith("@")){ if (!useraccount.startsWith("@")) {
useraccount = "@"+useraccount useraccount = "@" + useraccount
} }
return useraccount return useraccount
} }
public async getMeLinks(): Promise<string[]> { public async getMeLinks(): Promise<string[]> {
const userdata = await this.getUserInfo() const userdata = await this.getUserInfo()
@ -103,8 +103,8 @@ export default class OsmUserInfo {
const cacheAgeInSeconds = (Date.now() - cacheCreatedTime.getTime()) / 1000 const cacheAgeInSeconds = (Date.now() - cacheCreatedTime.getTime()) / 1000
if (cacheAgeInSeconds > OsmUserInfo.max_cache_age_seconds) { if (cacheAgeInSeconds > OsmUserInfo.max_cache_age_seconds) {
console.log("Cache is old, unlinking...") console.log("Cache is old, unlinking...")
}else{ } else {
try { try {
this._userData = JSON.parse(fs.readFileSync(this._cachingPath, "utf8")) this._userData = JSON.parse(fs.readFileSync(this._cachingPath, "utf8"))
return this._userData return this._userData

View file

@ -1,15 +1,16 @@
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> {
const data = await Utils.Download(url, headers) const data = await Utils.Download(url, headers)
try{ try {
return JSON.parse(data.content) return JSON.parse(data.content)
}catch (e) { } catch (e) {
console.log("Could not parse the result of ", url,": not a valid json:\n ",data.content) console.log("Could not parse the result of ", url, ": not a valid json:\n ", data.content)
throw e 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 = "#<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"))