Skip to content

Commit

Permalink
Add invites and user 2
Browse files Browse the repository at this point in the history
  • Loading branch information
rogeriochaves committed Apr 4, 2020
1 parent eddd22f commit 962c00e
Show file tree
Hide file tree
Showing 12 changed files with 1,415 additions and 30 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.ejs
2 changes: 1 addition & 1 deletion app/lib/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const createWindow = () => {
},
});

win.loadURL("http://localhost:3000");
win.loadURL(`http://localhost:${process.env.EXPRESS_PORT || 3000}`);
};

app.whenReady().then(createWindow);
Expand Down
41 changes: 35 additions & 6 deletions app/lib/express.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
const express = require("express");
const app = express();
const port = 3000;
const port = process.env.EXPRESS_PORT || 3000;
const bodyParser = require("body-parser");
const pull = require("pull-stream");
const Client = require("ssb-client");
const ssbKeys = require("ssb-keys");
const ssbConfig = require("./ssb-config");

let ssbServer;
let ssbKeys;
Client((err, server) => {
let profile;

let homeFolder =
process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
let ssbSecret = ssbKeys.loadOrCreateSync(
`${homeFolder}/.${process.env.CONFIG_FOLDER || "ssb"}/secret`
);
Client(ssbSecret, ssbConfig, (err, server) => {
if (err) throw err;
ssbServer = server;
ssbServer.whoami((err, keys) => {
if (err) throw err;
ssbKeys = keys;
profile = keys;
});
console.log(ssbServer);
});

app.use(bodyParser.json());
Expand All @@ -24,10 +33,10 @@ app.use(express.static("public"));
app.get("/", (_req, res) => {
pull(
ssbServer.createFeedStream(),
pull.collect((err, msgs) => {
pull.collect((_err, msgs) => {
const posts = msgs.map((x) => x.value.content);

res.render("index", { posts, profile: ssbKeys });
res.render("index", { posts, profile });
})
);
});
Expand All @@ -38,6 +47,26 @@ app.post("/publish", (req, res) => {
});
});

app.get("/new_invite", (_req, res) => {
ssbServer.invite.create({ uses: 10 }, (err, invite) => {
if (err) throw err;
console.log("invite", invite);

res.render("new_invite", { invite });
});
});

app.post("/add_invite", (req, res) => {
const inviteCode = req.body.invite_code;

ssbServer.invite.accept(inviteCode, (err, result) => {
if (err) throw err;
console.log("result", result);

res.redirect("/");
});
});

const expressServer = app.listen(port, () =>
console.log(`Example app listening at http://localhost:${port}`)
);
Expand Down
20 changes: 20 additions & 0 deletions app/lib/ssb-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const configInject = require("ssb-config/inject");

module.exports = configInject(process.env.CONFIG_FOLDER || "ssb", {
connections: {
incoming: {
net: [
{
scope: "public",
host: "0.0.0.0",
external: ["lvh.me"],
transform: "shs",
port: process.env.SSB_PORT || 8008,
},
],
},
outgoing: {
net: [{ transform: "shs" }],
},
},
});
8 changes: 6 additions & 2 deletions app/lib/ssb.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
const Server = require("ssb-server");
const config = require("ssb-config");
const fs = require("fs");
const path = require("path");
const config = require("./ssb-config");

// add plugins
Server.use(require("ssb-master"))
.use(require("ssb-gossip"))
.use(require("ssb-replicate"))
.use(require("ssb-backlinks"));
.use(require("ssb-backlinks"))
.use(require("ssb-about"))
.use(require("ssb-contacts"))
.use(require("ssb-invite"))
.use(require("ssb-friends"));

const server = Server(config);
console.log("SSB server started at", config.port);
Expand Down
150 changes: 150 additions & 0 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "",
"main": "index.js",
"scripts": {
"start": "electron ."
"start": "electron .",
"start:user-2": "SSB_PORT=8009 EXPRESS_PORT=3001 CONFIG_FOLDER=ssb-user2 electron ."
},
"author": "",
"license": "ISC",
Expand All @@ -13,10 +14,14 @@
"ejs": "^3.0.2",
"express": "^4.17.1",
"pull-stream": "^3.6.14",
"ssb-about": "^2.0.1",
"ssb-backlinks": "^1.0.0",
"ssb-client": "^4.9.0",
"ssb-config": "^3.4.4",
"ssb-contacts": "0.0.2",
"ssb-friends": "^4.1.4",
"ssb-gossip": "^1.1.1",
"ssb-keys": "^7.2.2",
"ssb-master": "^1.0.3",
"ssb-replicate": "^1.3.2",
"ssb-server": "^15.2.0"
Expand Down
2 changes: 2 additions & 0 deletions app/views/_footer.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
</body>
</html>
Loading

0 comments on commit 962c00e

Please sign in to comment.