const fs = require("fs"); const uuidv4 = require("uuid/v4"); const crypto = require("crypto"); const request = require("request"); const express = require("express"); const app = express(); const baseURL = process.env.BASE_URL || "new.shadowfacts.net"; // const alreadyPosted = loadAlreadyPosted(); // function loadAlreadyPosted() { // const buffer = fs.readFileSync("data/already-posted.json"); // const data = buffer.toString(); // return JSON.parse(data); // } // function exitHandler() { // const data = JSON.stringify(alreadyPosted); // fs.writeFileSync("data/already-posted.json", data); // } // process.on("exit", exitHandler); // process.on("SIGINT", exitHandler); app.use(require("morgan")("dev")); app.get("/ap", (req, res) => { res.end("Hello, world!"); }); app.get("/ap/actor", (req, res) => { if (req.accepts(["application/activity+json", "application/ld+json"])) { res.sendFile("actor.json"); } else { res.redirect("/"); } }); // const port = process.env.PORT || 8083; // app.listen(port, () => { // console.log(`Listening on ${port}`); // }); function createMessage(object) { const uuid = uuidv4(); return { "@context": "https://www.w3.org/ns/activitystreams", "id": `https://${baseURL}/ap/${uuid}`, "type": "Create", "actor": `https://${baseURL}/ap/actor`, "object": object }; } function signAndSendMessage(message, targetDomain, inbox) { const inboxFragment = inbox.replace("https://" + targetDomain, ""); const privKey = fs.readFileSync("/Users/shadowfacts/Desktop/private.pem").toString(); const signer = crypto.createSign("sha256"); const date = new Date(); let stringToSign = `(request-target): post ${inboxFragment}\nhost: ${targetDomain}\ndate: ${date.toUTCString()}`; signer.update(stringToSign); signer.end(); const signature = signer.sign(privKey); const base64Signature = signature.toString("base64"); const header = `keyId="https://${baseURL}/ap/actor#main-key",headers="(request-target) host date",signature="${base64Signature}"`; request({ url: inbox, headers: { "Host": targetDomain, "Date": date.toUTCString(), "Signature": header, "Accept": "application/activity+json" }, method: "POST", json: true, body: message }, (err, res) => { console.log("Sent message to an inbox at", targetDomain); if (err) console.log("Error:", error, res); console.log("Response status code: ", res.statusCode) }); } // const message = createMessage({"@context":["https://www.w3.org/ns/activitystreams"],"type":"Article","id":"https://new.shadowfacts.net/meta/2019/reincarnation.json","published":"2019-01-02T23:02:42.000Z","inReplyTo":null,"url":"https://new.shadowfacts.net/meta/2019/reincarnation/","attributedTo":"https://new.shadowfacts.net/ap/actor","to":["https://www.w3.org/ns/activitystreams#Public"],"cc":[],"name":"Reincarnation","content":"
\n \"Futurama\n
A wise man once said that nothing really dies, it just comes back in a new form. Then he died.
\n
\n

Stand by for reincarnation.

\n\n

// TODO: write the actual blog post

\n"}); // console.log(message); // signAndSendMessage(message, "social.shadowfacts.net", "https://social.shadowfacts.net/users/shadowfacts/inbox");