Add support for GNU Social
This commit is contained in:
parent
90585fcb68
commit
814b6d6a9b
1 changed files with 50 additions and 4 deletions
54
api/share.js
54
api/share.js
|
@ -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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Reference in a new issue