Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MBIN] Add subscribers badge #10270

Merged
merged 8 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions services/mbin/mbin.service.js
Original file line number Diff line number Diff line change
@@ -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: '[email protected]',
}),
},
},
}

static defaultBadgeData = { label: 'magazine', namedLogo: 'activitypub' }
chris-y marked this conversation as resolved.
Show resolved Hide resolved

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 })
}
}
43 changes: 43 additions & 0 deletions services/mbin/mbin.tester.js
Original file line number Diff line number Diff line change
@@ -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('/[email protected]')
.intercept(nock =>
nock('https://instance.tld/')
.get('/api/magazine/name/magazine')
.reply(
200,
JSON.stringify({
subscriptionsCount: 42,
}),
),
)
.expectBadge({
label: 'subscribe to [email protected]',
message: '42',
color: 'brightgreen',
})

t.create('unknown community')
.get('/[email protected]')
.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('/[email protected]')
.expectBadge({
label: 'subscribe to [email protected]',
message: isMetric,
color: 'brightgreen',
})
Loading