forked from MapComplete/MastodonBot
Group 'etymology' as they fill up the daily messages
This commit is contained in:
parent
5df7246db6
commit
931d4d9c68
3 changed files with 62 additions and 3 deletions
|
@ -30,14 +30,18 @@ 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) {
|
private constructor(masto, dryrun: boolean, hostname: string) {
|
||||||
this.instance = masto
|
this.instance = masto
|
||||||
this._dryrun = dryrun;
|
this._dryrun = dryrun;
|
||||||
|
this.hostname = hostname
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
public async writeMessage(text: string, options?: CreateStatusParamsBase): Promise<{ id: string }> {
|
public async writeMessage(text: string, options?: CreateStatusParamsBase): Promise<{ id: string }> {
|
||||||
|
|
|
@ -69,7 +69,7 @@ export default class OsmUserInfo {
|
||||||
}
|
}
|
||||||
|
|
||||||
const url = new URL(mastodonLinks[0])
|
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)){
|
if(await mastodonApi.hasNoBot(username)){
|
||||||
return undefined
|
return undefined
|
||||||
|
|
|
@ -229,6 +229,46 @@ export class Postbuilder {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async GroupTopcontributorsForTheme(theme: string, topContributors: { key: string; count: number }[], perContributor: Histogram<ChangeSetData>, maxCount = 3): Promise<{ alreadyMentioned: Set<string>; message: string }> {
|
||||||
|
const alreadyMentioned = new Set<string>()
|
||||||
|
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<string>(),
|
||||||
|
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<void> {
|
public async buildMessage(date: string): Promise<void> {
|
||||||
const changesets = this._changesetsMade
|
const changesets = this._changesetsMade
|
||||||
let lastPostId: string = undefined
|
let lastPostId: string = undefined
|
||||||
|
@ -278,9 +318,24 @@ export class Postbuilder {
|
||||||
`,
|
`,
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
if (this._config.showTopContributors && topContributors.length > 0) {
|
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) {
|
for (const topContributor of topContributors) {
|
||||||
const uid = topContributor.key
|
const uid = topContributor.key
|
||||||
|
if (alreadyMentioned.has(uid)) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
const changesetsMade = perContributor.get(uid)
|
const changesetsMade = perContributor.get(uid)
|
||||||
try {
|
try {
|
||||||
const userInfo = new OsmUserInfo(Number(uid), this._globalConfig)
|
const userInfo = new OsmUserInfo(Number(uid), this._globalConfig)
|
||||||
|
|
Loading…
Reference in a new issue