Fetch posts and contacts, mapping with author name

This commit is contained in:
Rogerio Chaves 2020-04-05 12:36:01 +02:00
parent 5ef7cf4c4a
commit 3653424dd8
No known key found for this signature in database
GPG Key ID: E6AF5440509B1D94
5 changed files with 112 additions and 27 deletions

View File

@ -27,7 +27,7 @@ app.on("window-all-closed", () => {
app.on("activate", () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) {
if (BrowserWindow.getAllWindows().length == 0) {
createWindow();
}
});

View File

@ -18,16 +18,16 @@ let ssbSecret = ssbKeys.loadOrCreateSync(
);
Client(ssbSecret, ssbConfig, async (err, server) => {
if (err) throw err;
profile = await promisify(server.whoami);
profile.name = await promisify(server.about.latestValue, {
key: "name",
dest: profile.id,
});
console.log("nearby pubs", await promisify(server.peerInvites.getNearbyPubs));
console.log("getState", await promisify(server.deviceAddress.getState));
ssbServer = server;
profile = await promisify(ssbServer.whoami);
console.log(
"nearby pubs",
await promisify(ssbServer.peerInvites.getNearbyPubs)
);
console.log("getState", await promisify(ssbServer.deviceAddress.getState));
console.log("ssbServer", ssbServer);
});
app.use(bodyParser.json());
@ -45,12 +45,58 @@ router.get("/", (_req, res) => {
return;
}
pull(
ssbServer.query.read({ limit: 10, reverse: true }),
pull.collect((_err, msgs) => {
const posts = msgs.map((x) => x.value.content);
if (!profile.name) {
res.redirect("/about");
}
res.render("index", { posts, profile });
const getAuthorName = (data, callback) => {
let promises = [];
const authorNamePromise = promisify(ssbServer.about.latestValue, {
key: "name",
dest: data.value.author,
});
promises.push(authorNamePromise);
if (data.value.content.type == "contact") {
const contactNamePromise = promisify(ssbServer.about.latestValue, {
key: "name",
dest: data.value.content.contact,
});
promises.push(contactNamePromise);
}
Promise.all(promises)
.then(([authorName, contactName]) => {
data.value.authorName = authorName;
if (contactName) {
data.value.content.contactName = contactName;
}
callback(null, data);
})
.catch((err) => callback(err, null));
};
pull(
ssbServer.query.read({
reverse: true,
query: [
{
$filter: {
value: {
content: { type: { $in: ["post", "contact"] } },
},
},
},
],
limit: 500,
}),
pull.asyncMap(getAuthorName),
pull.collect((_err, msgs) => {
const entries = msgs.map((x) => x.value);
res.render("index", { entries, profile });
})
);
});
@ -65,7 +111,7 @@ router.get("/pubs", async (_req, res) => {
const invite = await promisify(ssbServer.invite.create, { uses: 10 });
const peers = await promisify(ssbServer.gossip.peers);
res.render("pubs", { invite, peers });
res.render("pubs", { invite, peers, profile });
});
router.post("/pubs/add", async (req, res) => {
@ -76,6 +122,25 @@ router.post("/pubs/add", async (req, res) => {
res.redirect("/");
});
router.get("/about", (_req, res) => {
res.render("about", { profile });
});
router.post("/about", async (req, res) => {
const name = req.body.name;
if (name != profile.name) {
await promisify(ssbServer.publish, {
type: "about",
about: profile.id,
name: name,
});
profile.name = name;
}
res.redirect("/");
});
const expressServer = app.listen(port, () =>
console.log(`Example app listening at http://localhost:${port}`)
);

View File

@ -6,9 +6,15 @@
<title>Social</title>
</head>
<body>
<header>
<nav>
<a href="/">Home</a>
<a href="/pubs">Pubs</a>
</nav>
</header>
<% if (profile.name) { %>
<header>
<div style="float: right">
<%= profile.name %> (<%= profile.id.slice(0, 8) %>)
</div>
<nav>
<a href="/">Home</a>
<a href="/about">About me</a>
<a href="/pubs">Pubs</a>
</nav>
</header>
<% } %>

12
app/views/about.ejs Normal file
View File

@ -0,0 +1,12 @@
<%- include('_header') %>
<h1>About me</h1>
<form action="about" method="POST">
<p>
Name: <input type="text" name="name" value="<%= profile.name %>">
</p>
<input type="submit" value="Save">
</form>
<%- include('_footer') %>

View File

@ -4,11 +4,13 @@
<textarea name="message"></textarea>
<input type="submit" value="Send" />
</form>
<p>
Whoami: <%= profile.id %>
</p>
<% posts.map(post => { %>
<div><%= post.text %></div>
<% entries.map(entry => { %>
<% if (entry.content.type == "post") { %>
<div><%= entry.authorName %>: <%= entry.content.text %></div>
<% } else if (entry.content.type == "contact") { %>
<div><%= entry.authorName %> followed <%= entry.content.contactName %></div>
<% } %>
<% }) %>
<%- include('_footer') %>