Skip to content

Commit

Permalink
Renew [Mastodon] docs and improve parameter handling (#10789)
Browse files Browse the repository at this point in the history
* refactor: handle parameter as domain and not url

* docs: update Mastodon documentation

* test: adapt Mastodon tests to changes

* style: replace substring expressions with RegEx

Co-authored-by: chris48s <[email protected]>

---------

Co-authored-by: chris48s <[email protected]>
  • Loading branch information
cyb3rko and chris48s authored Jan 5, 2025
1 parent 1b00489 commit 41d072e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
21 changes: 9 additions & 12 deletions services/mastodon/mastodon-follow.service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Joi from 'joi'
import { metric } from '../text-formatters.js'
import { optionalUrl, nonNegativeInteger } from '../validators.js'
import { nonNegativeInteger } from '../validators.js'
import { BaseJsonService, NotFound, pathParam, queryParam } from '../index.js'

const schema = Joi.object({
Expand All @@ -9,15 +9,11 @@ const schema = Joi.object({
})

const queryParamSchema = Joi.object({
domain: optionalUrl,
domain: Joi.string().optional(),
}).required()

const description = `
To find your user id, you can use [this tool](https://prouser123.me/misc/mastodon-userid-lookup.html).
Alternatively you can make a request to \`https://your.mastodon.server/.well-known/webfinger?resource=acct:<user>@<domain>\`
Failing that, you can also visit your profile page, where your user ID will be in the header in a tag like this: \`<link href='https://your.mastodon.server/api/salmon/<your-user-id>' rel='salmon'>\`
To find your user id, you can make a request to \`https://your.mastodon.server/api/v1/accounts/lookup?acct=yourusername\`.
`

export default class MastodonFollow extends BaseJsonService {
Expand All @@ -41,7 +37,7 @@ export default class MastodonFollow extends BaseJsonService {
}),
queryParam({
name: 'domain',
example: 'https://mastodon.social',
example: 'mastodon.social',
}),
],
},
Expand All @@ -58,22 +54,23 @@ export default class MastodonFollow extends BaseJsonService {
message: metric(followers),
style: 'social',
link: [
`${domain}/users/${username}`,
`${domain}/users/${username}/followers`,
`https://${domain}/users/${username}`,
`https://${domain}/users/${username}/followers`,
],
}
}

async fetch({ id, domain }) {
return this._requestJson({
schema,
url: `${domain}/api/v1/accounts/${id}/`,
url: `https://${domain}/api/v1/accounts/${id}/`,
})
}

async handle({ id }, { domain = 'https://mastodon.social' }) {
async handle({ id }, { domain = 'mastodon.social' }) {
if (isNaN(id))
throw new NotFound({ prettyMessage: 'invalid user id format' })
domain = domain.replace(/^https?:\/\//, '')
const data = await this.fetch({ id, domain })
return this.constructor.render({
username: data.username,
Expand Down
11 changes: 11 additions & 0 deletions services/mastodon/mastodon-follow.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ t.create('Followers - default domain - invalid user ID (id not in use)')
})

t.create('Followers - alternate domain')
.get('/2214.json?domain=mastodon.xyz')
.expectBadge({
label: 'follow @PhotonQyv',
message: isMetric,
link: [
'https://mastodon.xyz/users/PhotonQyv',
'https://mastodon.xyz/users/PhotonQyv/followers',
],
})

t.create('Followers - alternate domain legacy')
.get('/2214.json?domain=https%3A%2F%2Fmastodon.xyz')
.expectBadge({
label: 'follow @PhotonQyv',
Expand Down

0 comments on commit 41d072e

Please sign in to comment.