forked from MapComplete/MastodonBot
Fix broken nobot detection for gowin
This commit is contained in:
parent
61482d19cc
commit
8796a589e8
5 changed files with 61 additions and 35 deletions
|
@ -66,8 +66,6 @@ export default interface Config {
|
|||
mastodonAuth: LoginSettings & {
|
||||
/** IF set: prints to console instead of to Mastodon*/
|
||||
dryrun?: boolean
|
||||
},
|
||||
}
|
||||
actions: MapCompleteUsageOverview []
|
||||
|
||||
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
import {login, LoginParams} from 'masto';
|
||||
import * as fs from "fs";
|
||||
import Utils from "./Utils";
|
||||
|
||||
export interface LoginSettings {
|
||||
url: string,
|
||||
|
@ -48,17 +49,17 @@ 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){
|
||||
} catch (e) {
|
||||
total += piece.length
|
||||
}
|
||||
}
|
||||
|
@ -89,7 +90,7 @@ export default class MastodonPoster {
|
|||
${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<boolean> {
|
||||
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
|
||||
|
|
|
@ -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;
|
||||
|
@ -45,11 +45,11 @@ 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<string | undefined> {
|
||||
const {nomention} = await this.hasNoBotTag()
|
||||
if(nomention){
|
||||
if (nomention) {
|
||||
return undefined
|
||||
}
|
||||
const mastodonLinks = await this.getMeLinks()
|
||||
|
@ -70,17 +70,17 @@ export default class OsmUserInfo {
|
|||
}
|
||||
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
|
@ -103,7 +103,7 @@ 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"))
|
||||
|
|
29
src/Utils.ts
29
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<any> {
|
||||
const data = await Utils.Download(url, headers)
|
||||
try{
|
||||
try {
|
||||
|
||||
return JSON.parse(data.content)
|
||||
}catch (e) {
|
||||
console.log("Could not parse the result of ", url,": not a valid json:\n ",data.content)
|
||||
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 = "#<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
|
||||
}
|
||||
}
|
|
@ -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"))
|
||||
|
|
Loading…
Reference in a new issue