diff --git a/src/Mastodon.ts b/src/Mastodon.ts index e21c8a6..0794aca 100644 --- a/src/Mastodon.ts +++ b/src/Mastodon.ts @@ -30,14 +30,18 @@ export default class MastodonPoster { private readonly instance; private _dryrun: boolean; private _userInfoCache: Record = {} + public readonly hostname: string; - private constructor(masto, dryrun: boolean) { + private constructor(masto, dryrun: boolean, hostname: string) { this.instance = masto this._dryrun = dryrun; + this.hostname = hostname } 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 }> { diff --git a/src/OsmUserInfo.ts b/src/OsmUserInfo.ts index 2a912b8..5d0c828 100644 --- a/src/OsmUserInfo.ts +++ b/src/OsmUserInfo.ts @@ -69,7 +69,7 @@ export default class OsmUserInfo { } const url = new URL(mastodonLinks[0]) - const username = url.pathname.substring(1) + "@" + url.host + const username = url.pathname.substring(1) +(url.host === mastodonApi.hostname ? "" : "@" + url.host) if(await mastodonApi.hasNoBot(username)){ return undefined diff --git a/src/Postbuilder.ts b/src/Postbuilder.ts index fa41b09..bc68ea1 100644 --- a/src/Postbuilder.ts +++ b/src/Postbuilder.ts @@ -229,6 +229,46 @@ export class Postbuilder { return result } + public async GroupTopcontributorsForTheme(theme: string, topContributors: { key: string; count: number }[], perContributor: Histogram, maxCount = 3): Promise<{ alreadyMentioned: Set; message: string }> { + const alreadyMentioned = new Set() + const etymologyContributors: { username: string }[] = [] + for (const topContributor of topContributors) { + const uid = topContributor.key + const changesetsMade = perContributor.get(uid) + if (changesetsMade.find(cs => cs.properties.theme !== theme)) { + continue + } + // This is an etymology-only contributor + alreadyMentioned.add(uid) + const userInfo = new OsmUserInfo(Number(uid), this._globalConfig) + const {nobot} = await userInfo.hasNoBotTag() + if (nobot) { + continue + } + const info = await userInfo.getUserInfo() + const username = await userInfo.GetMastodonUsername(this._poster) ?? info.display_name + etymologyContributors.push({username}) + } + let message: string + if(etymologyContributors.length <= 1){ + return { + alreadyMentioned: new Set(), + message: undefined + } + } + if (etymologyContributors.length <= 4) { + message = "- " + Utils.commasAnd(etymologyContributors.map(c => c.username)) + " contributed with ${theme}" + } else { + message = `- ${etymologyContributors.slice(0, 3).map(c => c.username).join(", ")} and ${etymologyContributors.length - 3} others contributed with thematic map ${theme}` + + } + + return { + alreadyMentioned, + message + } + } + public async buildMessage(date: string): Promise { const changesets = this._changesetsMade let lastPostId: string = undefined @@ -278,9 +318,24 @@ export class Postbuilder { `, ] + if (this._config.showTopContributors && topContributors.length > 0) { + + // We group contributors that only contributed to 'etymology' as they otherwise spam the first entry + + const { + alreadyMentioned, + message + } = await this.GroupTopcontributorsForTheme("etymology", topContributors, perContributor) + if(message?.length > 0){ + toSend.push(message) + } + for (const topContributor of topContributors) { const uid = topContributor.key + if (alreadyMentioned.has(uid)) { + continue + } const changesetsMade = perContributor.get(uid) try { const userInfo = new OsmUserInfo(Number(uid), this._globalConfig)