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