forked from MapComplete/MastodonBot
Gracefully handle failing user lookups
This commit is contained in:
parent
59c7e47f49
commit
65921d2d11
3 changed files with 25 additions and 16 deletions
|
@ -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<string, any> = {}
|
||||
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<boolean> {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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, {
|
||||
|
|
Loading…
Reference in a new issue