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

Renew [Mastodon] docs and improve parameter handling #10789

Merged
merged 4 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
25 changes: 13 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,27 @@ 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' })
if (domain.startsWith('https://')) {
domain = domain.substring(8)
} else if (domain.startsWith('http://')) {
domain = domain.substring(7)
}
cyb3rko marked this conversation as resolved.
Show resolved Hide resolved
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
Loading