fedicrawl/application/src/Fediverse/Providers/Peertube/retrieveFollowers.ts

47 wiersze
1.2 KiB
TypeScript
Czysty Zwykły widok Historia

2021-12-23 14:14:06 +00:00
import { assertSuccessJsonResponse } from '../../assertSuccessJsonResponse'
import { z } from 'zod'
2022-11-29 13:11:27 +00:00
import getTimeoutMilliseconds from '../../getTimeoutMilliseconds.js'
import { NodeProviderMethod } from '../NodeProviderMethod'
import { NoMoreNodesError } from '../NoMoreNodesError'
2021-12-23 14:14:06 +00:00
const limit = 100
const schema = z.object({
total: z.number(),
data: z.array(
z.object({
follower: z.object({
host: z.string()
}),
following: z.object({
host: z.string()
})
})
)
})
2022-11-22 15:37:11 +00:00
export const retrieveFollowers: NodeProviderMethod = async (domain, page, robotsTxt) => {
const response = await robotsTxt.getIfAllowed(
2022-11-29 13:11:27 +00:00
new URL(`https://${domain}/api/v1/server/followers`),
2022-09-18 11:32:25 +00:00
{
params: {
count: limit,
sort: 'createdAt',
start: page * limit
},
2022-11-29 13:11:27 +00:00
timeout: getTimeoutMilliseconds(domain)
2022-09-18 11:32:25 +00:00
}
)
2021-12-23 14:14:06 +00:00
assertSuccessJsonResponse(response)
const responseData = schema.parse(response.data)
const hosts = new Set<string>()
2022-09-18 11:32:25 +00:00
responseData.data.forEach((item) => {
2021-12-23 14:14:06 +00:00
hosts.add(item.follower.host)
hosts.add(item.following.host)
})
if (hosts.size === 0) {
throw new NoMoreNodesError('follower')
2021-12-23 14:14:06 +00:00
}
return Array.from(hosts)
}