Improve instance list
This now returns the list of instances of multiple Fediverse projects. Currently pretty slow because of the Mastodon and Pleroma being too big.
This commit is contained in:
parent
f5cbb082ed
commit
6d4eca0131
2 changed files with 51 additions and 5 deletions
14
src/constants.ts
Normal file
14
src/constants.ts
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
/**
|
||||||
|
* Enumeration of the supported fediverse projects.
|
||||||
|
*
|
||||||
|
* The values of this enum are used as the keys for the fediverse.observer API,
|
||||||
|
* as the icon names, etc.
|
||||||
|
*/
|
||||||
|
export enum FediverseProject {
|
||||||
|
Friendica = "friendica",
|
||||||
|
GNUSocial = "gnusocial",
|
||||||
|
Hubzilla = "hubzilla",
|
||||||
|
Mastodon = "mastodon",
|
||||||
|
Misskey = "misskey",
|
||||||
|
Pleroma = "pleroma",
|
||||||
|
}
|
|
@ -4,20 +4,52 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import type { APIRoute } from "astro";
|
import type { APIRoute } from "astro";
|
||||||
|
import { FediverseProject } from "../../constants";
|
||||||
|
|
||||||
interface MastodonServer {
|
interface ProjectInstance {
|
||||||
[key: string]: unknown;
|
|
||||||
domain: string;
|
domain: string;
|
||||||
|
score: number;
|
||||||
|
active_users_monthly: number;
|
||||||
|
status: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const PROJECTS = Object.values(FediverseProject);
|
||||||
|
|
||||||
|
export const fetchInstances = async (
|
||||||
|
projectId: string,
|
||||||
|
): Promise<ProjectInstance[]> => {
|
||||||
|
const response = await fetch("https://api.fediverse.observer/", {
|
||||||
|
headers: {
|
||||||
|
Accept: "*/*",
|
||||||
|
"Accept-Language": "en;q=1.0",
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
referrer: "https://api.fediverse.observer/",
|
||||||
|
body: `{"query":"{\\n nodes(softwarename: \\"${projectId}\\") {\\n domain\\n score\\n active_users_monthly\\n status\\n }\\n}\\n"}`,
|
||||||
|
method: "POST",
|
||||||
|
});
|
||||||
|
const json = await response.json();
|
||||||
|
const instances: ProjectInstance[] = json.data.nodes;
|
||||||
|
return instances.filter(
|
||||||
|
(instance) => instance.score > 0 && instance.status === 1,
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
export const get: APIRoute = async () => {
|
export const get: APIRoute = async () => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch("https://api.joinmastodon.org/servers");
|
const response = await Promise.all(
|
||||||
const instances = await response.json();
|
PROJECTS.map((projectId) => fetchInstances(projectId)),
|
||||||
|
);
|
||||||
|
const instances = response.flat();
|
||||||
|
instances.sort((a, b) => {
|
||||||
|
return b.active_users_monthly - a.active_users_monthly;
|
||||||
|
});
|
||||||
|
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify(
|
JSON.stringify(
|
||||||
instances.map((instance: MastodonServer) => instance.domain),
|
instances
|
||||||
|
.slice(0, 200)
|
||||||
|
.map((instance: ProjectInstance) => instance.domain),
|
||||||
),
|
),
|
||||||
{
|
{
|
||||||
headers: {
|
headers: {
|
||||||
|
|
Reference in a new issue