Skip to content

Commit

Permalink
Add Support for [Nostr] Followers (#9870)
Browse files Browse the repository at this point in the history
* add nostr followers with tests

* fix broken test

* fix http error handler

* rename route from nostr to nostr-band

* remove unnecessary test cases

* edit test expectation

* validate data schema ensuring exact one key

* remove unavailable named logo

* migrate examples to openApi

* Update services/nostr-band/nostr-band-followers.tester.js

* remove unused import

---------

Co-authored-by: chris48s <[email protected]>
Co-authored-by: chris48s <[email protected]>
  • Loading branch information
3 people authored Jan 11, 2024
1 parent bfcbaea commit 4a55c3e
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
67 changes: 67 additions & 0 deletions services/nostr-band/nostr-band-followers.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import Joi from 'joi'
import { metric } from '../text-formatters.js'
import { BaseJsonService, pathParams } from '../index.js'

const npubSchema = Joi.object({
followers_pubkey_count: Joi.number().required(),
}).required()

const mainSchema = Joi.object({
stats: Joi.object()
.pattern(Joi.string(), npubSchema)
.min(1)
.max(1)
.required(),
}).required()

export default class NostrBandFollowers extends BaseJsonService {
static category = 'social'

static route = {
base: 'nostr-band/followers',
pattern: ':npub',
}

static openApi = {
'/nostr-band/followers/{npub}': {
get: {
summary: 'Nostr.band Followers',
description:
'Returns the number of followers for a Nostr pubkey using the Nostr.band API.',
parameters: pathParams({
name: 'npub',
description: 'Nostr pubkey in (npub1...) format or hex.',
example:
'npub18c556t7n8xa3df2q82rwxejfglw5przds7sqvefylzjh8tjne28qld0we7',
}),
},
},
}

static defaultBadgeData = { label: 'followers' }

static render({ followers }) {
return {
message: metric(followers),
style: 'social',
}
}

async fetch({ npub }) {
const data = await this._requestJson({
url: `https://api.nostr.band/v0/stats/profile/${npub}`,
schema: mainSchema,
httpErrors: {
400: 'invalid pubkey',
},
})
const stats = data.stats
const firstKey = Object.keys(stats)[0]
return stats[firstKey].followers_pubkey_count
}

async handle({ npub }) {
const followers = await this.fetch({ npub })
return this.constructor.render({ followers })
}
}
15 changes: 15 additions & 0 deletions services/nostr-band/nostr-band-followers.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { isMetric } from '../test-validators.js'
import { createServiceTester } from '../tester.js'
export const t = await createServiceTester()

t.create('fetch: valid npub')
.get('/npub18c556t7n8xa3df2q82rwxejfglw5przds7sqvefylzjh8tjne28qld0we7.json')
.expectBadge({
label: 'followers',
message: isMetric,
})

t.create('fetch: invalid npub').get('/invalidnpub.json').expectBadge({
label: 'followers',
message: 'invalid pubkey',
})

0 comments on commit 4a55c3e

Please sign in to comment.