2020-04-04 15:32:27 +02:00
|
|
|
const express = require("express");
|
|
|
|
const app = express();
|
2020-04-04 17:15:43 +02:00
|
|
|
const port = process.env.EXPRESS_PORT || 3000;
|
2020-04-04 16:01:11 +02:00
|
|
|
const bodyParser = require("body-parser");
|
|
|
|
const Client = require("ssb-client");
|
2020-04-04 17:15:43 +02:00
|
|
|
const ssbKeys = require("ssb-keys");
|
|
|
|
const ssbConfig = require("./ssb-config");
|
2020-04-04 20:20:41 +02:00
|
|
|
const { promisify, asyncRouter } = require("./utils");
|
2020-04-05 13:29:05 +02:00
|
|
|
const queries = require("./queries");
|
2020-04-04 15:32:27 +02:00
|
|
|
|
2020-04-04 16:01:11 +02:00
|
|
|
let ssbServer;
|
2020-04-05 18:38:49 +02:00
|
|
|
let context = {};
|
2020-04-04 17:15:43 +02:00
|
|
|
|
|
|
|
let homeFolder =
|
|
|
|
process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
|
|
|
|
let ssbSecret = ssbKeys.loadOrCreateSync(
|
2020-04-04 20:20:41 +02:00
|
|
|
`${homeFolder}/.${process.env.CONFIG_FOLDER || "social"}/secret`
|
2020-04-04 17:15:43 +02:00
|
|
|
);
|
2020-04-04 20:20:41 +02:00
|
|
|
Client(ssbSecret, ssbConfig, async (err, server) => {
|
2020-04-04 16:01:11 +02:00
|
|
|
if (err) throw err;
|
2020-04-05 18:38:49 +02:00
|
|
|
const whoami = await promisify(server.whoami);
|
|
|
|
context.profile = await queries.getProfile(server, whoami.id);
|
2020-04-04 20:20:41 +02:00
|
|
|
|
2020-04-05 12:36:01 +02:00
|
|
|
console.log("nearby pubs", await promisify(server.peerInvites.getNearbyPubs));
|
|
|
|
console.log("getState", await promisify(server.deviceAddress.getState));
|
2020-04-04 20:20:41 +02:00
|
|
|
|
2020-04-05 12:36:01 +02:00
|
|
|
ssbServer = server;
|
2020-04-04 16:01:11 +02:00
|
|
|
});
|
2020-04-04 15:32:27 +02:00
|
|
|
|
2020-04-04 16:01:11 +02:00
|
|
|
app.use(bodyParser.json());
|
|
|
|
app.use(bodyParser.urlencoded({ extended: true }));
|
|
|
|
app.set("view engine", "ejs");
|
2020-04-04 15:32:27 +02:00
|
|
|
app.use(express.static("public"));
|
2020-04-05 18:38:49 +02:00
|
|
|
app.use((_req, res, next) => {
|
|
|
|
res.locals.profileUrl = (id, path = "") => {
|
|
|
|
if (id.includes("/")) {
|
|
|
|
return `/profile/${encodeURIComponent(id)}${path}`;
|
|
|
|
}
|
|
|
|
return `/profile/${id}${path}`;
|
|
|
|
};
|
|
|
|
res.locals.context = context;
|
|
|
|
next();
|
|
|
|
});
|
2020-04-04 15:32:27 +02:00
|
|
|
|
2020-04-04 20:20:41 +02:00
|
|
|
const router = asyncRouter(app);
|
|
|
|
|
2020-04-05 13:29:05 +02:00
|
|
|
router.get("/", async (_req, res) => {
|
2020-04-04 20:20:41 +02:00
|
|
|
if (!ssbServer) {
|
|
|
|
setTimeout(() => {
|
|
|
|
res.redirect("/");
|
|
|
|
}, 500);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-05 18:38:49 +02:00
|
|
|
if (!context.profile.name) {
|
2020-04-05 12:36:01 +02:00
|
|
|
res.redirect("/about");
|
|
|
|
}
|
|
|
|
|
2020-04-05 14:51:25 +02:00
|
|
|
const [posts, friends] = await Promise.all([
|
2020-04-05 18:38:49 +02:00
|
|
|
queries.getPosts(ssbServer, context.profile),
|
|
|
|
queries.getFriends(ssbServer, context.profile),
|
2020-04-05 13:29:05 +02:00
|
|
|
]);
|
2020-04-05 18:38:49 +02:00
|
|
|
res.render("index", { posts, friends });
|
|
|
|
});
|
|
|
|
|
|
|
|
router.get("/profile/:id", async (req, res) => {
|
|
|
|
const id = req.params.id;
|
|
|
|
|
|
|
|
if (id == context.profile.id) {
|
|
|
|
return res.redirect("/");
|
|
|
|
}
|
|
|
|
|
|
|
|
const profile = await queries.getProfile(ssbServer, id);
|
|
|
|
|
|
|
|
const [posts, friends] = await Promise.all([
|
|
|
|
queries.getPosts(ssbServer, profile),
|
|
|
|
queries.getFriends(ssbServer, profile),
|
|
|
|
]);
|
|
|
|
|
|
|
|
res.render("profile", { profile, posts, friends });
|
2020-04-04 15:32:27 +02:00
|
|
|
});
|
|
|
|
|
2020-04-04 20:20:41 +02:00
|
|
|
router.post("/publish", async (req, res) => {
|
2020-04-05 14:56:51 +02:00
|
|
|
await promisify(ssbServer.publish, {
|
|
|
|
type: "post",
|
|
|
|
text: req.body.message,
|
2020-04-05 18:38:49 +02:00
|
|
|
wall: context.profile.id,
|
2020-04-05 14:56:51 +02:00
|
|
|
});
|
2020-04-04 20:20:41 +02:00
|
|
|
|
|
|
|
res.redirect("/");
|
2020-04-04 15:32:27 +02:00
|
|
|
});
|
|
|
|
|
2020-04-05 18:38:49 +02:00
|
|
|
router.post("/profile/:id/publish", async (req, res) => {
|
|
|
|
const id = req.params.id;
|
|
|
|
|
|
|
|
await promisify(ssbServer.publish, {
|
|
|
|
type: "post",
|
|
|
|
text: req.body.message,
|
|
|
|
wall: id,
|
|
|
|
});
|
|
|
|
|
|
|
|
res.redirect("/profile/" + id);
|
|
|
|
});
|
|
|
|
|
2020-04-04 20:20:41 +02:00
|
|
|
router.get("/pubs", async (_req, res) => {
|
|
|
|
const invite = await promisify(ssbServer.invite.create, { uses: 10 });
|
|
|
|
const peers = await promisify(ssbServer.gossip.peers);
|
2020-04-04 17:15:43 +02:00
|
|
|
|
2020-04-05 18:38:49 +02:00
|
|
|
res.render("pubs", { invite, peers });
|
2020-04-04 17:15:43 +02:00
|
|
|
});
|
|
|
|
|
2020-04-04 20:20:41 +02:00
|
|
|
router.post("/pubs/add", async (req, res) => {
|
2020-04-04 17:15:43 +02:00
|
|
|
const inviteCode = req.body.invite_code;
|
|
|
|
|
2020-04-04 20:20:41 +02:00
|
|
|
await promisify(ssbServer.invite.accept, inviteCode);
|
2020-04-04 17:15:43 +02:00
|
|
|
|
2020-04-04 20:20:41 +02:00
|
|
|
res.redirect("/");
|
2020-04-04 17:15:43 +02:00
|
|
|
});
|
|
|
|
|
2020-04-05 12:36:01 +02:00
|
|
|
router.get("/about", (_req, res) => {
|
2020-04-05 18:38:49 +02:00
|
|
|
res.render("about");
|
2020-04-05 12:36:01 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
router.post("/about", async (req, res) => {
|
|
|
|
const name = req.body.name;
|
|
|
|
|
2020-04-05 18:38:49 +02:00
|
|
|
if (name != context.profile.name) {
|
2020-04-05 12:36:01 +02:00
|
|
|
await promisify(ssbServer.publish, {
|
|
|
|
type: "about",
|
2020-04-05 18:38:49 +02:00
|
|
|
about: context.profile.id,
|
2020-04-05 12:36:01 +02:00
|
|
|
name: name,
|
|
|
|
});
|
2020-04-05 18:38:49 +02:00
|
|
|
context.profile.name = name;
|
2020-04-05 12:36:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
res.redirect("/");
|
|
|
|
});
|
|
|
|
|
2020-04-05 13:29:05 +02:00
|
|
|
router.get("/debug", async (_req, res) => {
|
|
|
|
const entries = await queries.getAllEntries(ssbServer);
|
|
|
|
|
2020-04-05 18:38:49 +02:00
|
|
|
res.render("debug", { entries });
|
2020-04-05 13:29:05 +02:00
|
|
|
});
|
|
|
|
|
2020-04-05 14:51:25 +02:00
|
|
|
router.get("/search", async (req, res) => {
|
|
|
|
const query = req.query.query;
|
|
|
|
|
|
|
|
const people = await queries.searchPeople(ssbServer, query);
|
|
|
|
|
2020-04-05 18:38:49 +02:00
|
|
|
res.render("search", { people });
|
2020-04-05 14:51:25 +02:00
|
|
|
});
|
|
|
|
|
2020-04-04 16:01:11 +02:00
|
|
|
const expressServer = app.listen(port, () =>
|
2020-04-04 15:32:27 +02:00
|
|
|
console.log(`Example app listening at http://localhost:${port}`)
|
|
|
|
);
|
|
|
|
|
2020-04-04 16:01:11 +02:00
|
|
|
module.exports = expressServer;
|