1
0
mirror of https://github.com/badges/shields.git synced 2025-04-18 19:44:04 +03:00

[MBIN] Add subscribers badge (#10270)

* Add Mbin support

* Add tests for Mbin

* Update services/mbin/mbin.service.js

Remove extra space from description

Co-authored-by: Pierre-Yves Bigourdan <10694593+PyvesB@users.noreply.github.com>

* Remove test for bad server or connection

* Removed timeout

* Run not found test against live server

* Use isMetric for test

---------

Co-authored-by: Pierre-Yves Bigourdan <10694593+PyvesB@users.noreply.github.com>
This commit is contained in:
Chris Young 2024-06-23 22:25:29 +01:00 committed by GitHub
parent 0c17d9d44d
commit 92718ff98b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 113 additions and 0 deletions

View File

@ -0,0 +1,70 @@
import Joi from 'joi'
import { metric } from '../text-formatters.js'
import { BaseJsonService, InvalidParameter, pathParams } from '../index.js'
const schema = Joi.object({
subscriptionsCount: Joi.number().required(),
}).required()
export default class Mbin extends BaseJsonService {
static category = 'social'
static route = {
base: 'mbin',
pattern: ':magazine',
}
static openApi = {
'/mbin/{magazine}': {
get: {
summary: 'Mbin',
description:
'Mbin is a fork of Kbin, a content aggregator for the Fediverse.',
parameters: pathParams({
name: 'magazine',
description: 'The magazine to query. This is CASE SENSITIVE.',
example: 'teletext@fedia.io',
}),
},
},
}
static defaultBadgeData = { label: 'magazine', namedLogo: 'activitypub' }
static render({ magazine, members }) {
return {
label: `subscribe to ${magazine}`,
message: metric(members),
style: 'social',
color: 'brightgreen',
}
}
async fetch({ magazine }) {
const splitAlias = magazine.split('@')
// The magazine will be in the format of 'magazine@server'
if (splitAlias.length !== 2) {
throw new InvalidParameter({
prettyMessage: 'invalid magazine',
})
}
const mag = splitAlias[0]
const host = splitAlias[1]
const data = await this._requestJson({
url: `https://${host}/api/magazine/name/${mag}`,
schema,
httpErrors: {
404: 'magazine not found',
},
})
return data.subscriptionsCount
}
async handle({ magazine }) {
const members = await this.fetch({ magazine })
return this.constructor.render({ magazine, members })
}
}

View File

@ -0,0 +1,43 @@
import { isMetric } from '../test-validators.js'
import { createServiceTester } from '../tester.js'
export const t = await createServiceTester()
t.create('get magazine subscribers')
.get('/magazine@instance.tld.json')
.intercept(nock =>
nock('https://instance.tld/')
.get('/api/magazine/name/magazine')
.reply(
200,
JSON.stringify({
subscriptionsCount: 42,
}),
),
)
.expectBadge({
label: 'subscribe to magazine@instance.tld',
message: '42',
color: 'brightgreen',
})
t.create('unknown community')
.get('/01J12N2ETYG3W5B6G8Y11F5EXG@fedia.io.json')
.expectBadge({
label: 'magazine',
message: 'magazine not found',
color: 'red',
})
t.create('invalid magazine').get('/magazine.invalid.json').expectBadge({
label: 'magazine',
message: 'invalid magazine',
color: 'red',
})
t.create('test on real mbin magazine for API compliance')
.get('/teletext@fedia.io.json')
.expectBadge({
label: 'subscribe to teletext@fedia.io',
message: isMetric,
color: 'brightgreen',
})