kopia lustrzana https://github.com/badgen/badgen.net
feat(mastodon): add `username@instance` handler (#498)
rodzic
3aec694e48
commit
578ec7e285
|
@ -2,25 +2,52 @@ import got from '../libs/got'
|
||||||
import { millify } from '../libs/utils'
|
import { millify } from '../libs/utils'
|
||||||
import { createBadgenHandler, PathArgs } from '../libs/create-badgen-handler'
|
import { createBadgenHandler, PathArgs } from '../libs/create-badgen-handler'
|
||||||
|
|
||||||
|
const BRAND_COLOR = '3487CE'
|
||||||
|
|
||||||
export default createBadgenHandler({
|
export default createBadgenHandler({
|
||||||
title: 'Mastodon/Pleroma',
|
title: 'Mastodon/Pleroma',
|
||||||
examples: {
|
examples: {
|
||||||
'/mastodon/follow/1': 'followers (mastodon.social)',
|
'/mastodon/follow/Gargron@mastodon.social': 'followers',
|
||||||
'/mastodon/follow/1/mas.to': 'followers (other Mastodon instance)',
|
'/mastodon/follow/trumpet@mas.to': 'followers',
|
||||||
'/mastodon/follow/3/anime.website': 'followers (Pleroma instance)',
|
'/mastodon/follow/admin@cawfee.club': 'followers (Pleroma)',
|
||||||
},
|
},
|
||||||
handlers: {
|
handlers: {
|
||||||
'/mastodon/follow/:userId/:instance?': handler
|
'/mastodon/follow/:account<.+@.+>': accountHandler,
|
||||||
|
'/mastodon/follow/:user-id<\\d>/:instance?': userIdHandler
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
async function handler ({ userId, instance = 'mastodon.social' }: PathArgs) {
|
async function userIdHandler({ 'user-id': userId, instance = 'mastodon.social' }: PathArgs) {
|
||||||
const prefixUrl = `https://${instance}/api/v1/`
|
const info = await got(`https://${instance}/api/v1/accounts/${userId}`).json<any>()
|
||||||
const info = await got(`accounts/${userId}`, { prefixUrl }).json<any>()
|
const account = `${info.username}@${instance}`
|
||||||
|
|
||||||
return {
|
return {
|
||||||
subject: `follow @${info.username}@${instance}`,
|
subject: `follow @${account}`,
|
||||||
status: millify(info.followers_count),
|
status: millify(info.followers_count),
|
||||||
color: '3487CE'
|
color: BRAND_COLOR
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function accountHandler({ account }: PathArgs) {
|
||||||
|
const [username, instance] = account.split('@')
|
||||||
|
const { version } = await got(`https://${instance}/api/v1/instance`).json<any>()
|
||||||
|
const isPleroma = /\bPleroma\b/i.test(version)
|
||||||
|
if (isPleroma) return userIdHandler({ 'user-id': username, instance })
|
||||||
|
const resp = await got(`https://${instance}/@${username}.rss`)
|
||||||
|
const params = isFeed(resp) && parseFeed(resp.body, instance)
|
||||||
|
return params || {
|
||||||
|
subject: 'mastodon',
|
||||||
|
status: 'unknown',
|
||||||
|
color: 'grey'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function isFeed(response: import('got').Response) {
|
||||||
|
const contentType = response.headers['content-type'] || ''
|
||||||
|
return contentType.includes('application/rss+xml')
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseFeed(feed: string, instance: string) {
|
||||||
|
const reAvatarPath = /\/accounts\/avatars\/(\d{3})\/(\d{3})\/(\d{3})/
|
||||||
|
const userId = feed.match(reAvatarPath)?.slice(1).join('')
|
||||||
|
if (userId) return userIdHandler({ 'user-id': userId, instance })
|
||||||
|
}
|
||||||
|
|
Ładowanie…
Reference in New Issue