-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Support for [Nostr] Followers (#9870)
* 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
1 parent
bfcbaea
commit 4a55c3e
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}) |