diff --git a/src/Mastodon.ts b/src/Mastodon.ts index 0794aca..d467997 100644 --- a/src/Mastodon.ts +++ b/src/Mastodon.ts @@ -23,6 +23,7 @@ export interface CreateStatusParamsBase { } export default class MastodonPoster { + public readonly hostname: string; /** * The actual instance, see https://www.npmjs.com/package/mastodon * @private @@ -30,7 +31,6 @@ export default class MastodonPoster { private readonly instance; private _dryrun: boolean; private _userInfoCache: Record = {} - public readonly hostname: string; private constructor(masto, dryrun: boolean, hostname: string) { this.instance = masto @@ -39,9 +39,9 @@ export default class MastodonPoster { } public static async construct(settings: LoginParams & { dryrun?: boolean }) { - return new MastodonPoster(await login(settings), settings.dryrun ?? false, + return new MastodonPoster(await login(settings), settings.dryrun ?? false, new URL(settings.url).hostname - ) + ) } public async writeMessage(text: string, options?: CreateStatusParamsBase): Promise<{ id: string }> { @@ -76,6 +76,9 @@ export default class MastodonPoster { 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(" ") ?? [] if (descrParts.indexOf("#nobot") >= 0 || descrParts.indexOf("#nomapcompletebot") >= 0) { return true @@ -104,16 +107,21 @@ export default class MastodonPoster { followingCount: number, statusesCount: number, fields: { name: string, value: string }[] - }> { + } | undefined> { if (this._userInfoCache[username]) { return this._userInfoCache[username] } - const acct = await this.instance.v1.accounts.lookup({ - acct: username, - }); - const info = await this.instance.v1.accounts.fetch(acct.id) - this._userInfoCache[username] = info - return info + try { + const acct = await this.instance.v1.accounts.lookup({ + acct: username, + }); + const info = await this.instance.v1.accounts.fetch(acct.id) + this._userInfoCache[username] = info + return info + } catch (e) { + console.error("Could not fetch user details for ", username) + return undefined + } } /** diff --git a/src/OsmUserInfo.ts b/src/OsmUserInfo.ts index 5d0c828..7778a5a 100644 --- a/src/OsmUserInfo.ts +++ b/src/OsmUserInfo.ts @@ -1,6 +1,7 @@ import Utils from "./Utils"; import * as fs from "fs"; import MastodonPoster from "./Mastodon"; +import {userInfo} from "os"; export interface UserInfo { "id": number, @@ -74,7 +75,10 @@ export default class OsmUserInfo { if(await mastodonApi.hasNoBot(username)){ return undefined } - let useraccount = (await mastodonApi.userInfoFor(username)).acct + let useraccount = (await mastodonApi.userInfoFor(username))?.acct + if(useraccount === undefined){ + useraccount = username + } if(!useraccount.startsWith("@")){ useraccount = "@"+useraccount } diff --git a/src/index.ts b/src/index.ts index 8f31c66..63f49f7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -38,16 +38,13 @@ export class Main { const start = Date.now() try { - for (const action of this._config.actions) { console.log("Running action", action) await this.runMapCompleteOverviewAction(poster, action) } - - const end = Date.now() - const timeNeeded = Math.floor((end - start) / 1000) } catch (e) { - console.error(e) + console.error("Caught top level exception: ", e) + console.log(e.stack) const end = Date.now() const timeNeeded = Math.floor((end - start) / 1000) await poster.writeMessage("@pietervdvn@en.osm.town Running MapComplete bot failed in " + timeNeeded + "seconds, the error is " + e, {