Add support for GNU Social

This commit is contained in:
Nikita Karamov 2023-03-16 18:59:43 +01:00
parent 90585fcb68
commit 814b6d6a9b
No known key found for this signature in database
GPG key ID: 41D6F71EE78E77CD

View file

@ -19,6 +19,45 @@
*/ */
import http from "http"; import http from "http";
import https from "https";
const pathsMap = {
mastodon: {
checkUrl: "/api/v1/instance",
postUrl: "share",
textParam: "text",
},
gnuSocial: {
checkUrl: "/api/statusnet/version.xml",
postUrl: "/notice/new",
textParam: "status_textarea",
},
};
const queryUrl = (url, service) => {
return new Promise((resolve, reject) => {
const get = url.protocol === "https:" ? https.get : http.get;
get(url, ({ statusCode }) => {
if (statusCode === 200) {
console.debug(url.href, "is", service);
resolve(service);
} else {
reject(url);
}
}).on("error", (error) => {
reject(error);
});
});
};
const detectService = async (instanceURL) => {
const checkPromises = Object.entries(pathsMap).map(
([service, { checkUrl }]) =>
queryUrl(new URL(checkUrl, instanceURL), service)
);
return await Promise.any(checkPromises);
};
const requestListener = async (request, response) => { const requestListener = async (request, response) => {
if (request.method !== "POST") { if (request.method !== "POST") {
@ -37,10 +76,17 @@ const requestListener = async (request, response) => {
const instanceURL = const instanceURL =
requestBody.get("instance") || "https://mastodon.social/"; requestBody.get("instance") || "https://mastodon.social/";
const finalURL = new URL("share", instanceURL); detectService(instanceURL)
finalURL.search = new URLSearchParams({ postText }).toString(); .then((service) => {
const publishUrl = new URL(pathsMap[service].postUrl, instanceURL);
response.writeHead(303, { Location: finalURL.toString() }).end(); publishUrl.search = new URLSearchParams([
[pathsMap[service].textParam, postText],
]);
response.writeHead(303, { Location: publishUrl.toString() }).end();
})
.catch((error) => {
response.writeHead(400).end(error);
});
}); });
}; };