Refactor ssbServer, which is actually a client, to be a singleton
This commit is contained in:
parent
98d6e27d2c
commit
c63823964d
|
@ -1,10 +1,8 @@
|
|||
const ssb = require("./ssb-client");
|
||||
const express = require("express");
|
||||
const app = express();
|
||||
const port = process.env.PORT || 3000;
|
||||
const bodyParser = require("body-parser");
|
||||
const Client = require("ssb-client");
|
||||
const ssbKeys = require("ssb-keys");
|
||||
const ssbConfig = require("./ssb-config");
|
||||
const {
|
||||
asyncRouter,
|
||||
writeKey,
|
||||
|
@ -28,42 +26,8 @@ const cookieEncrypter = require("cookie-encrypter");
|
|||
const expressLayouts = require("express-ejs-layouts");
|
||||
const mobileRoutes = require("./mobile-routes");
|
||||
|
||||
let ssbServer;
|
||||
let mode = process.env.MODE || "client";
|
||||
|
||||
let homeFolder =
|
||||
process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
|
||||
let ssbSecret = ssbKeys.loadOrCreateSync(
|
||||
`${homeFolder}/.${process.env.CONFIG_FOLDER || "social"}/secret`
|
||||
);
|
||||
let syncing = false;
|
||||
|
||||
Client(ssbSecret, ssbConfig, async (err, server) => {
|
||||
if (err) throw err;
|
||||
|
||||
ssbServer = server;
|
||||
mobileRoutes.setSsbServer(server);
|
||||
|
||||
queries.progress(
|
||||
ssbServer,
|
||||
({ rate, feeds, incompleteFeeds, progress, total }) => {
|
||||
if (incompleteFeeds > 0) {
|
||||
if (!syncing) debug("syncing");
|
||||
syncing = true;
|
||||
} else {
|
||||
syncing = false;
|
||||
}
|
||||
|
||||
metrics.ssbProgressRate.set(rate);
|
||||
metrics.ssbProgressFeeds.set(feeds);
|
||||
metrics.ssbProgressIncompleteFeeds.set(incompleteFeeds);
|
||||
metrics.ssbProgressProgress.set(progress);
|
||||
metrics.ssbProgressTotal.set(total);
|
||||
}
|
||||
);
|
||||
console.log("SSB Client ready");
|
||||
});
|
||||
|
||||
let profileUrl = (id, path = "") => {
|
||||
return `/profile/${id}${path}`;
|
||||
};
|
||||
|
@ -94,7 +58,7 @@ app.use(cookieEncrypter(cookieSecret));
|
|||
app.use(expressLayouts);
|
||||
app.set("layout", false);
|
||||
app.use(async (req, res, next) => {
|
||||
if (!ssbServer) {
|
||||
if (!ssb.client()) {
|
||||
setTimeout(() => {
|
||||
console.log("Waiting for SSB to load...");
|
||||
|
||||
|
@ -104,25 +68,25 @@ app.use(async (req, res, next) => {
|
|||
}
|
||||
|
||||
req.context = {
|
||||
syncing: syncing,
|
||||
syncing: ssb.isSyncing(),
|
||||
};
|
||||
res.locals.context = req.context;
|
||||
try {
|
||||
const identities = await ssbServer.identities.list();
|
||||
const identities = await ssb.client().identities.list();
|
||||
const key = req.signedCookies["ssb_key"];
|
||||
if (!key) return next();
|
||||
|
||||
const parsedKey = JSON.parse(key);
|
||||
if (!identities.includes(parsedKey.id)) {
|
||||
const filename = await nextIdentityFilename(ssbServer);
|
||||
const filename = await nextIdentityFilename(ssb.client());
|
||||
|
||||
writeKey(key, `/identities/${filename}`);
|
||||
ssbServer.identities.refresh();
|
||||
ssb.client().identities.refresh();
|
||||
}
|
||||
req.context.profile = await queries.getProfile(ssbServer, parsedKey.id);
|
||||
req.context.profile = await queries.getProfile(parsedKey.id);
|
||||
|
||||
const isRootUser =
|
||||
req.context.profile.id == ssbServer.id ||
|
||||
req.context.profile.id == ssb.client().id ||
|
||||
process.env.NODE_ENV != "production";
|
||||
|
||||
req.context.profile.debug = isRootUser;
|
||||
|
@ -168,9 +132,9 @@ router.get(
|
|||
}
|
||||
|
||||
const [posts, friends, secretMessages] = await Promise.all([
|
||||
queries.getPosts(ssbServer, req.context.profile),
|
||||
queries.getFriends(ssbServer, req.context.profile),
|
||||
queries.getSecretMessages(ssbServer, req.context.profile),
|
||||
queries.getPosts(req.context.profile),
|
||||
queries.getFriends(req.context.profile),
|
||||
queries.getSecretMessages(req.context.profile),
|
||||
]);
|
||||
res.render("home", {
|
||||
posts,
|
||||
|
@ -198,7 +162,7 @@ router.post("/login", { public: true }, async (req, res) => {
|
|||
decodedKey.private = "[removed]";
|
||||
debug("Login with key", decodedKey);
|
||||
|
||||
await queries.autofollow(ssbServer, decodedKey.id);
|
||||
await queries.autofollow(decodedKey.id);
|
||||
|
||||
res.redirect("/");
|
||||
} catch (e) {
|
||||
|
@ -228,10 +192,10 @@ router.post("/signup", { public: true }, async (req, res) => {
|
|||
const name = req.body.name;
|
||||
const picture = req.files && req.files.pic;
|
||||
|
||||
const pictureLink = picture && (await uploadPicture(ssbServer, picture));
|
||||
const pictureLink = picture && (await uploadPicture(ssb.client(), picture));
|
||||
|
||||
const filename = await nextIdentityFilename(ssbServer);
|
||||
const profileId = await ssbServer.identities.create();
|
||||
const filename = await nextIdentityFilename(ssb.client());
|
||||
const profileId = await ssb.client().identities.create();
|
||||
const key = readKey(`/identities/${filename}`);
|
||||
if (key.id != profileId)
|
||||
throw "profileId and key.id don't match, probably race condition, bailing out for safety";
|
||||
|
@ -242,7 +206,7 @@ router.post("/signup", { public: true }, async (req, res) => {
|
|||
key.private = "[removed]";
|
||||
debug("Generated key", key);
|
||||
|
||||
await ssbServer.identities.publishAs({
|
||||
await ssb.client().identities.publishAs({
|
||||
id: profileId,
|
||||
private: false,
|
||||
content: {
|
||||
|
@ -254,7 +218,7 @@ router.post("/signup", { public: true }, async (req, res) => {
|
|||
});
|
||||
debug("Published about", { about: profileId, name, image: pictureLink });
|
||||
|
||||
await queries.autofollow(ssbServer, profileId);
|
||||
await queries.autofollow(profileId);
|
||||
|
||||
res.redirect("/keys");
|
||||
});
|
||||
|
@ -292,7 +256,7 @@ router.get("/keys/copy", (req, res) => {
|
|||
});
|
||||
|
||||
router.get("/keys/download", async (req, res) => {
|
||||
const identities = await ssbServer.identities.list();
|
||||
const identities = await ssb.client().identities.list();
|
||||
const index = identities.indexOf(req.context.profile.id) - 1;
|
||||
const filename = identityFilename(index);
|
||||
const secretPath = `${ssbFolder()}/identities/${filename}`;
|
||||
|
@ -312,10 +276,10 @@ router.get(
|
|||
}
|
||||
|
||||
const [profile, posts, friends, friendshipStatus] = await Promise.all([
|
||||
queries.getProfile(ssbServer, id),
|
||||
queries.getPosts(ssbServer, { id }),
|
||||
queries.getFriends(ssbServer, { id }),
|
||||
queries.getFriendshipStatus(ssbServer, req.context.profile.id, id),
|
||||
queries.getProfile(id),
|
||||
queries.getPosts({ id }),
|
||||
queries.getFriends({ id }),
|
||||
queries.getFriendshipStatus(req.context.profile.id, id),
|
||||
]);
|
||||
|
||||
res.render("profile", { profile, posts, friends, friendshipStatus });
|
||||
|
@ -328,7 +292,7 @@ router.post("/profile/:id(*)/add_friend", async (req, res) => {
|
|||
throw "cannot befriend yourself";
|
||||
}
|
||||
|
||||
await ssbServer.identities.publishAs({
|
||||
await ssb.client().identities.publishAs({
|
||||
id: req.context.profile.id,
|
||||
private: false,
|
||||
content: {
|
||||
|
@ -347,7 +311,7 @@ router.post("/profile/:id(*)/reject_friend", async (req, res) => {
|
|||
throw "cannot reject yourself";
|
||||
}
|
||||
|
||||
await ssbServer.identities.publishAs({
|
||||
await ssb.client().identities.publishAs({
|
||||
id: req.context.profile.id,
|
||||
private: false,
|
||||
content: {
|
||||
|
@ -361,7 +325,7 @@ router.post("/profile/:id(*)/reject_friend", async (req, res) => {
|
|||
});
|
||||
|
||||
router.post("/publish", async (req, res) => {
|
||||
await ssbServer.identities.publishAs({
|
||||
await ssb.client().identities.publishAs({
|
||||
id: req.context.profile.id,
|
||||
private: false,
|
||||
content: {
|
||||
|
@ -377,7 +341,7 @@ router.post("/publish", async (req, res) => {
|
|||
router.post("/publish_secret", async (req, res) => {
|
||||
const recipients = req.body.recipients;
|
||||
|
||||
await ssbServer.identities.publishAs({
|
||||
await ssb.client().identities.publishAs({
|
||||
id: req.context.profile.id,
|
||||
private: true,
|
||||
content: {
|
||||
|
@ -395,7 +359,7 @@ router.post("/vanish", async (req, res) => {
|
|||
const keys = req.body.keys.split(",");
|
||||
|
||||
for (const key of keys) {
|
||||
await ssbServer.identities.publishAs({
|
||||
await ssb.client().identities.publishAs({
|
||||
id: req.context.profile.id,
|
||||
private: false,
|
||||
content: {
|
||||
|
@ -411,7 +375,7 @@ router.post("/vanish", async (req, res) => {
|
|||
router.post("/profile/:id(*)/publish", async (req, res) => {
|
||||
const id = req.params.id;
|
||||
|
||||
await ssbServer.identities.publishAs({
|
||||
await ssb.client().identities.publishAs({
|
||||
id: req.context.profile.id,
|
||||
private: false,
|
||||
content: {
|
||||
|
@ -427,7 +391,7 @@ router.post("/profile/:id(*)/publish", async (req, res) => {
|
|||
router.post("/profile/:id(*)/publish_secret", async (req, res) => {
|
||||
const id = req.params.id;
|
||||
|
||||
await ssbServer.identities.publishAs({
|
||||
await ssb.client().identities.publishAs({
|
||||
id: req.context.profile.id,
|
||||
private: true,
|
||||
content: {
|
||||
|
@ -441,8 +405,8 @@ router.post("/profile/:id(*)/publish_secret", async (req, res) => {
|
|||
});
|
||||
|
||||
router.get("/pubs", async (_req, res) => {
|
||||
const invite = await ssbServer.invite.create({ uses: 10 });
|
||||
const peers = await ssbServer.gossip.peers();
|
||||
const invite = await ssb.client().invite.create({ uses: 10 });
|
||||
const peers = await ssb.client().gossip.peers();
|
||||
|
||||
res.render("pubs", { invite, peers });
|
||||
});
|
||||
|
@ -450,7 +414,7 @@ router.get("/pubs", async (_req, res) => {
|
|||
router.post("/pubs/add", async (req, res) => {
|
||||
const inviteCode = req.body.invite_code;
|
||||
|
||||
await ssbServer.invite.accept(inviteCode);
|
||||
await ssb.client().invite.accept(inviteCode);
|
||||
|
||||
res.redirect("/");
|
||||
});
|
||||
|
@ -463,7 +427,7 @@ router.post("/about", async (req, res) => {
|
|||
const { name, description } = req.body;
|
||||
const picture = req.files && req.files.pic;
|
||||
|
||||
const pictureLink = picture && (await uploadPicture(ssbServer, picture));
|
||||
const pictureLink = picture && (await uploadPicture(ssb.client(), picture));
|
||||
|
||||
let update = {
|
||||
type: "about",
|
||||
|
@ -480,7 +444,7 @@ router.post("/about", async (req, res) => {
|
|||
}
|
||||
|
||||
if (update.name || update.image || update.description) {
|
||||
await ssbServer.identities.publishAs({
|
||||
await ssb.client().identities.publishAs({
|
||||
id: req.context.profile.id,
|
||||
private: false,
|
||||
content: update,
|
||||
|
@ -496,7 +460,7 @@ router.get(
|
|||
"/communities",
|
||||
{ mobileVersion: "/mobile/communities" },
|
||||
async (_req, res) => {
|
||||
const communities = await queries.getCommunities(ssbServer);
|
||||
const communities = await queries.getCommunities();
|
||||
|
||||
res.render("communities/list", { communities });
|
||||
}
|
||||
|
@ -504,7 +468,7 @@ router.get(
|
|||
|
||||
const communityData = (req) => {
|
||||
const name = req.params.name;
|
||||
return queries.getCommunityMembers(ssbServer, name).then((members) => ({
|
||||
return queries.getCommunityMembers(name).then((members) => ({
|
||||
name,
|
||||
members,
|
||||
}));
|
||||
|
@ -518,7 +482,7 @@ router.get(
|
|||
|
||||
const [community, posts] = await Promise.all([
|
||||
communityData(req),
|
||||
queries.getCommunityPosts(ssbServer, name),
|
||||
queries.getCommunityPosts(name),
|
||||
]);
|
||||
|
||||
res.render("communities/community", {
|
||||
|
@ -543,7 +507,7 @@ router.post("/communities/:name/new", async (req, res) => {
|
|||
const title = req.body.title;
|
||||
const post = req.body.post;
|
||||
|
||||
const topic = await ssbServer.identities.publishAs({
|
||||
const topic = await ssb.client().identities.publishAs({
|
||||
id: req.context.profile.id,
|
||||
private: false,
|
||||
content: {
|
||||
|
@ -562,7 +526,7 @@ router.post("/communities/:name/:key(*)/publish", async (req, res) => {
|
|||
const key = req.params.key;
|
||||
const reply = req.body.reply;
|
||||
|
||||
await ssbServer.identities.publishAs({
|
||||
await ssb.client().identities.publishAs({
|
||||
id: req.context.profile.id,
|
||||
private: false,
|
||||
content: {
|
||||
|
@ -582,7 +546,7 @@ router.get("/communities/:name/:key(*)", async (req, res) => {
|
|||
|
||||
const [community, posts] = await Promise.all([
|
||||
communityData(req),
|
||||
queries.getPostWithReplies(ssbServer, name, key),
|
||||
queries.getPostWithReplies(name, key),
|
||||
]);
|
||||
|
||||
res.render("communities/topic", {
|
||||
|
@ -600,7 +564,7 @@ router.get("/search", async (req, res) => {
|
|||
communities: [],
|
||||
};
|
||||
if (query.length >= 3) {
|
||||
results = await queries.search(ssbServer, query);
|
||||
results = await queries.search(query);
|
||||
metrics.searchResultsPeople.observe(results.people.length);
|
||||
metrics.searchResultsCommunities.observe(results.communities.length);
|
||||
}
|
||||
|
@ -609,7 +573,7 @@ router.get("/search", async (req, res) => {
|
|||
});
|
||||
|
||||
router.get("/blob/*", { public: true }, (req, res) => {
|
||||
serveBlobs(ssbServer)(req, res);
|
||||
serveBlobs(ssb.client())(req, res);
|
||||
});
|
||||
|
||||
router.get("/syncing", (req, res) => {
|
||||
|
@ -619,7 +583,7 @@ router.get("/syncing", (req, res) => {
|
|||
router.get("/debug", async (req, res) => {
|
||||
const query = req.query || {};
|
||||
|
||||
const entries = await queries.getAllEntries(ssbServer, query);
|
||||
const entries = await queries.getAllEntries(query);
|
||||
|
||||
res.render("debug", { entries, query });
|
||||
});
|
||||
|
|
|
@ -1,9 +1,5 @@
|
|||
const queries = require("./queries");
|
||||
|
||||
let ssbServer;
|
||||
module.exports.setSsbServer = (server) => {
|
||||
ssbServer = server;
|
||||
};
|
||||
const ssb = require("./ssb-client");
|
||||
|
||||
module.exports.setupRoutes = (router) => {
|
||||
router.get(
|
||||
|
@ -14,7 +10,7 @@ module.exports.setupRoutes = (router) => {
|
|||
return res.render("index");
|
||||
}
|
||||
|
||||
const posts = await queries.getPosts(ssbServer, req.context.profile);
|
||||
const posts = await queries.getPosts(req.context.profile);
|
||||
|
||||
res.render("mobile/home", {
|
||||
posts,
|
||||
|
@ -26,8 +22,8 @@ module.exports.setupRoutes = (router) => {
|
|||
|
||||
router.get("/mobile/secrets", { desktopVersion: "/" }, async (req, res) => {
|
||||
const [friends, secretMessages] = await Promise.all([
|
||||
queries.getFriends(ssbServer, req.context.profile),
|
||||
queries.getSecretMessages(ssbServer, req.context.profile),
|
||||
queries.getFriends(req.context.profile),
|
||||
queries.getSecretMessages(req.context.profile),
|
||||
]);
|
||||
|
||||
res.render("mobile/secrets", {
|
||||
|
@ -39,7 +35,7 @@ module.exports.setupRoutes = (router) => {
|
|||
});
|
||||
|
||||
router.get("/mobile/friends", { desktopVersion: "/" }, async (req, res) => {
|
||||
const friends = await queries.getFriends(ssbServer, req.context.profile);
|
||||
const friends = await queries.getFriends(req.context.profile);
|
||||
|
||||
res.render("mobile/friends", {
|
||||
friends,
|
||||
|
@ -59,10 +55,10 @@ module.exports.setupRoutes = (router) => {
|
|||
}
|
||||
|
||||
const [profile, posts, friends, friendshipStatus] = await Promise.all([
|
||||
queries.getProfile(ssbServer, id),
|
||||
queries.getPosts(ssbServer, { id }),
|
||||
queries.getFriends(ssbServer, { id }),
|
||||
queries.getFriendshipStatus(ssbServer, req.context.profile.id, id),
|
||||
queries.getProfile(id),
|
||||
queries.getPosts({ id }),
|
||||
queries.getFriends({ id }),
|
||||
queries.getFriendshipStatus(req.context.profile.id, id),
|
||||
]);
|
||||
|
||||
res.render("mobile/profile", {
|
||||
|
@ -79,7 +75,7 @@ module.exports.setupRoutes = (router) => {
|
|||
"/mobile/communities",
|
||||
{ desktopVersion: "/communities" },
|
||||
async (_req, res) => {
|
||||
const communities = await queries.getCommunities(ssbServer);
|
||||
const communities = await queries.getCommunities();
|
||||
|
||||
res.render("mobile/communities/list", {
|
||||
communities,
|
||||
|
@ -94,7 +90,7 @@ module.exports.setupRoutes = (router) => {
|
|||
async (req, res) => {
|
||||
const name = req.params.name;
|
||||
|
||||
const posts = await queries.getCommunityPosts(ssbServer, name);
|
||||
const posts = await queries.getCommunityPosts(name);
|
||||
|
||||
res.render("mobile/communities/community", {
|
||||
community: { name },
|
||||
|
|
|
@ -11,10 +11,11 @@ const debugPosts = require("debug")("queries:posts"),
|
|||
debugCommunityPosts = require("debug")("queries:communityPosts");
|
||||
const paramap = require("pull-paramap");
|
||||
const { promisePull, mapValues } = require("./utils");
|
||||
const ssb = require("./ssb-client");
|
||||
|
||||
const latestOwnerValue = (ssbServer, { key, dest }) => {
|
||||
const latestOwnerValue = ({ key, dest }) => {
|
||||
return promisePull(
|
||||
ssbServer.query.read({
|
||||
ssb.client().query.read({
|
||||
reverse: true,
|
||||
query: [
|
||||
{
|
||||
|
@ -39,25 +40,25 @@ const latestOwnerValue = (ssbServer, { key, dest }) => {
|
|||
if (entry) {
|
||||
return entry.value.content[key];
|
||||
}
|
||||
return ssbServer.about.latestValue({ key, dest });
|
||||
return ssb.client().about.latestValue({ key, dest });
|
||||
});
|
||||
};
|
||||
|
||||
const mapProfiles = (ssbServer) => (data, callback) =>
|
||||
getProfile(ssbServer, data.value.author)
|
||||
const mapProfiles = (data, callback) =>
|
||||
getProfile(data.value.author)
|
||||
.then((author) => {
|
||||
data.value.authorProfile = author;
|
||||
callback(null, data);
|
||||
})
|
||||
.catch((err) => callback(err, null));
|
||||
|
||||
const getPosts = async (ssbServer, profile) => {
|
||||
const getPosts = async (profile) => {
|
||||
debugPosts("Fetching");
|
||||
|
||||
const posts = await promisePull(
|
||||
// @ts-ignore
|
||||
cat([
|
||||
ssbServer.query.read({
|
||||
ssb.client().query.read({
|
||||
reverse: true,
|
||||
query: [
|
||||
{
|
||||
|
@ -73,7 +74,7 @@ const getPosts = async (ssbServer, profile) => {
|
|||
],
|
||||
limit: 100,
|
||||
}),
|
||||
ssbServer.query.read({
|
||||
ssb.client().query.read({
|
||||
reverse: true,
|
||||
query: [
|
||||
{
|
||||
|
@ -94,7 +95,7 @@ const getPosts = async (ssbServer, profile) => {
|
|||
}),
|
||||
]),
|
||||
pull.filter((msg) => msg.value.content.type == "post"),
|
||||
paramap(mapProfiles(ssbServer))
|
||||
paramap(mapProfiles)
|
||||
);
|
||||
|
||||
debugPosts("Done");
|
||||
|
@ -102,12 +103,12 @@ const getPosts = async (ssbServer, profile) => {
|
|||
return mapValues(posts);
|
||||
};
|
||||
|
||||
const getSecretMessages = async (ssbServer, profile) => {
|
||||
const getSecretMessages = async (profile) => {
|
||||
debugMessages("Fetching");
|
||||
const messagesPromise = promisePull(
|
||||
// @ts-ignore
|
||||
cat([
|
||||
ssbServer.private.read({
|
||||
ssb.client().private.read({
|
||||
reverse: true,
|
||||
limit: 100,
|
||||
}),
|
||||
|
@ -121,7 +122,7 @@ const getSecretMessages = async (ssbServer, profile) => {
|
|||
);
|
||||
|
||||
const deletedPromise = promisePull(
|
||||
ssbServer.query.read({
|
||||
ssb.client().query.read({
|
||||
reverse: true,
|
||||
query: [
|
||||
{
|
||||
|
@ -172,7 +173,7 @@ const getSecretMessages = async (ssbServer, profile) => {
|
|||
}
|
||||
|
||||
const profilesList = await Promise.all(
|
||||
Object.keys(messagesByAuthor).map((id) => getProfile(ssbServer, id))
|
||||
Object.keys(messagesByAuthor).map((id) => getProfile(id))
|
||||
);
|
||||
const profilesHash = profilesList.reduce((hash, profile) => {
|
||||
hash[profile.id] = profile;
|
||||
|
@ -188,7 +189,7 @@ const getSecretMessages = async (ssbServer, profile) => {
|
|||
return chatList;
|
||||
};
|
||||
|
||||
const search = async (ssbServer, search) => {
|
||||
const search = async (search) => {
|
||||
debugSearch("Fetching");
|
||||
|
||||
// https://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
|
||||
|
@ -203,7 +204,7 @@ const search = async (ssbServer, search) => {
|
|||
const searchRegex = new RegExp(`.*${loosenSpacesSearch}.*`, "i");
|
||||
|
||||
const peoplePromise = promisePull(
|
||||
ssbServer.query.read({
|
||||
ssb.client().query.read({
|
||||
reverse: true,
|
||||
query: [
|
||||
{
|
||||
|
@ -226,11 +227,11 @@ const search = async (ssbServer, search) => {
|
|||
.replace(/[\u0300-\u036f]/g, "");
|
||||
return searchRegex.exec(normalizedName);
|
||||
}),
|
||||
paramap(mapProfiles(ssbServer))
|
||||
paramap(mapProfiles)
|
||||
);
|
||||
|
||||
const communitiesPostsPromise = promisePull(
|
||||
ssbServer.query.read({
|
||||
ssb.client().query.read({
|
||||
reverse: true,
|
||||
query: [
|
||||
{
|
||||
|
@ -267,10 +268,10 @@ const search = async (ssbServer, search) => {
|
|||
return { people: Object.values(mapValues(people)), communities };
|
||||
};
|
||||
|
||||
const getFriends = async (ssbServer, profile) => {
|
||||
const getFriends = async (profile) => {
|
||||
debugFriends("Fetching");
|
||||
|
||||
let graph = await ssbServer.friends.getGraph();
|
||||
let graph = await ssb.client().friends.getGraph();
|
||||
|
||||
let connections = {};
|
||||
for (let key in graph) {
|
||||
|
@ -287,7 +288,7 @@ const getFriends = async (ssbServer, profile) => {
|
|||
}
|
||||
|
||||
const profilesList = await Promise.all(
|
||||
Object.keys(connections).map((id) => getProfile(ssbServer, id))
|
||||
Object.keys(connections).map((id) => getProfile(id))
|
||||
);
|
||||
const profilesHash = profilesList.reduce((hash, profile) => {
|
||||
hash[profile.id] = profile;
|
||||
|
@ -307,11 +308,11 @@ const getFriends = async (ssbServer, profile) => {
|
|||
return result;
|
||||
};
|
||||
|
||||
const getFriendshipStatus = async (ssbServer, source, dest) => {
|
||||
const getFriendshipStatus = async (source, dest) => {
|
||||
debugFriendshipStatus("Fetching");
|
||||
|
||||
let requestRejectionsPromise = promisePull(
|
||||
ssbServer.query.read({
|
||||
ssb.client().query.read({
|
||||
reverse: true,
|
||||
query: [
|
||||
{
|
||||
|
@ -331,8 +332,8 @@ const getFriendshipStatus = async (ssbServer, source, dest) => {
|
|||
).then(mapValues);
|
||||
|
||||
const [isFollowing, isFollowingBack, requestRejections] = await Promise.all([
|
||||
ssbServer.friends.isFollowing({ source: source, dest: dest }),
|
||||
ssbServer.friends.isFollowing({ source: dest, dest: source }),
|
||||
ssb.client().friends.isFollowing({ source: source, dest: dest }),
|
||||
ssb.client().friends.isFollowing({ source: dest, dest: source }),
|
||||
requestRejectionsPromise.then((x) => x.map((y) => y.content.contact)),
|
||||
]);
|
||||
|
||||
|
@ -353,7 +354,7 @@ const getFriendshipStatus = async (ssbServer, source, dest) => {
|
|||
return status;
|
||||
};
|
||||
|
||||
const getAllEntries = (ssbServer, query) => {
|
||||
const getAllEntries = (query) => {
|
||||
let queries = [];
|
||||
if (query.author) {
|
||||
queries.push({ $filter: { value: { author: query.author } } });
|
||||
|
@ -364,7 +365,7 @@ const getAllEntries = (ssbServer, query) => {
|
|||
const queryOpts = queries.length > 0 ? { query: queries } : {};
|
||||
|
||||
return promisePull(
|
||||
ssbServer.query.read({
|
||||
ssb.client().query.read({
|
||||
reverse: true,
|
||||
limit: 1000,
|
||||
...queryOpts,
|
||||
|
@ -373,10 +374,10 @@ const getAllEntries = (ssbServer, query) => {
|
|||
};
|
||||
|
||||
let profileCache = {};
|
||||
const getProfile = async (ssbServer, id) => {
|
||||
const getProfile = async (id) => {
|
||||
if (profileCache[id]) return profileCache[id];
|
||||
|
||||
let getKey = (key) => latestOwnerValue(ssbServer, { key, dest: id });
|
||||
let getKey = (key) => latestOwnerValue({ key, dest: id });
|
||||
|
||||
let [name, image, description] = await Promise.all([
|
||||
getKey("name"),
|
||||
|
@ -392,23 +393,23 @@ const getProfile = async (ssbServer, id) => {
|
|||
return profile;
|
||||
};
|
||||
|
||||
const progress = (ssbServer, callback) => {
|
||||
const progress = (callback) => {
|
||||
pull(
|
||||
ssbServer.replicate.changes(),
|
||||
ssb.client().replicate.changes(),
|
||||
pull.drain(callback, (err) => {
|
||||
console.error("Progress drain error", err);
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
const autofollow = async (ssbServer, id) => {
|
||||
const isFollowing = await ssbServer.friends.isFollowing({
|
||||
source: ssbServer.id,
|
||||
const autofollow = async (id) => {
|
||||
const isFollowing = await ssb.client().friends.isFollowing({
|
||||
source: ssb.client().id,
|
||||
dest: id,
|
||||
});
|
||||
|
||||
if (!isFollowing) {
|
||||
await ssbServer.publish({
|
||||
await ssb.client().publish({
|
||||
type: "contact",
|
||||
contact: id,
|
||||
following: true,
|
||||
|
@ -417,11 +418,11 @@ const autofollow = async (ssbServer, id) => {
|
|||
}
|
||||
};
|
||||
|
||||
const getCommunities = async (ssbServer) => {
|
||||
const getCommunities = async () => {
|
||||
debugCommunities("Fetching");
|
||||
|
||||
const communitiesPosts = await promisePull(
|
||||
ssbServer.query.read({
|
||||
ssb.client().query.read({
|
||||
reverse: true,
|
||||
query: [
|
||||
{
|
||||
|
@ -449,11 +450,11 @@ const getCommunities = async (ssbServer) => {
|
|||
return communities;
|
||||
};
|
||||
|
||||
const getCommunityMembers = async (ssbServer, name) => {
|
||||
const getCommunityMembers = async (name) => {
|
||||
debugCommunityMembers("Fetching");
|
||||
|
||||
const communityMembers = await promisePull(
|
||||
ssbServer.query.read({
|
||||
ssb.client().query.read({
|
||||
reverse: true,
|
||||
query: [
|
||||
{
|
||||
|
@ -469,7 +470,7 @@ const getCommunityMembers = async (ssbServer, name) => {
|
|||
],
|
||||
limit: 100,
|
||||
}),
|
||||
paramap(mapProfiles(ssbServer))
|
||||
paramap(mapProfiles)
|
||||
);
|
||||
|
||||
debugCommunityMembers("Done");
|
||||
|
@ -477,13 +478,13 @@ const getCommunityMembers = async (ssbServer, name) => {
|
|||
return communityMembers.map((x) => x.value.authorProfile);
|
||||
};
|
||||
|
||||
const getPostWithReplies = async (ssbServer, channel, key) => {
|
||||
const getPostWithReplies = async (channel, key) => {
|
||||
debugCommunityPosts("Fetching");
|
||||
|
||||
const postWithReplies = await promisePull(
|
||||
// @ts-ignore
|
||||
cat([
|
||||
ssbServer.query.read({
|
||||
ssb.client().query.read({
|
||||
reverse: false,
|
||||
limit: 1,
|
||||
query: [
|
||||
|
@ -500,7 +501,7 @@ const getPostWithReplies = async (ssbServer, channel, key) => {
|
|||
},
|
||||
],
|
||||
}),
|
||||
ssbServer.query.read({
|
||||
ssb.client().query.read({
|
||||
reverse: false,
|
||||
limit: 50,
|
||||
query: [
|
||||
|
@ -516,7 +517,7 @@ const getPostWithReplies = async (ssbServer, channel, key) => {
|
|||
},
|
||||
],
|
||||
}),
|
||||
ssbServer.query.read({
|
||||
ssb.client().query.read({
|
||||
reverse: false,
|
||||
limit: 50,
|
||||
query: [
|
||||
|
@ -533,18 +534,18 @@ const getPostWithReplies = async (ssbServer, channel, key) => {
|
|||
],
|
||||
}),
|
||||
]),
|
||||
paramap(mapProfiles(ssbServer))
|
||||
paramap(mapProfiles)
|
||||
);
|
||||
|
||||
debugCommunityPosts("Done");
|
||||
return postWithReplies;
|
||||
};
|
||||
|
||||
const getCommunityPosts = async (ssbServer, name) => {
|
||||
const getCommunityPosts = async (name) => {
|
||||
debugCommunityPosts("Fetching");
|
||||
|
||||
const communityPosts = await promisePull(
|
||||
ssbServer.query.read({
|
||||
ssb.client().query.read({
|
||||
reverse: true,
|
||||
query: [
|
||||
{
|
||||
|
@ -560,7 +561,7 @@ const getCommunityPosts = async (ssbServer, name) => {
|
|||
],
|
||||
limit: 1000,
|
||||
}),
|
||||
paramap(mapProfiles(ssbServer))
|
||||
paramap(mapProfiles)
|
||||
);
|
||||
let communityPostsByKey = {};
|
||||
let replies = [];
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
const Client = require("ssb-client");
|
||||
const ssbKeys = require("ssb-keys");
|
||||
const ssbConfig = require("./ssb-config");
|
||||
const queries = require("./queries");
|
||||
const debug = require("debug")("express");
|
||||
const metrics = require("./metrics");
|
||||
|
||||
let ssbClient;
|
||||
|
||||
let homeFolder =
|
||||
process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
|
||||
let ssbSecret = ssbKeys.loadOrCreateSync(
|
||||
`${homeFolder}/.${process.env.CONFIG_FOLDER || "social"}/secret`
|
||||
);
|
||||
let syncing = false;
|
||||
|
||||
Client(ssbSecret, ssbConfig, async (err, server) => {
|
||||
if (err) throw err;
|
||||
|
||||
ssbClient = server;
|
||||
|
||||
queries.progress(({ rate, feeds, incompleteFeeds, progress, total }) => {
|
||||
if (incompleteFeeds > 0) {
|
||||
if (!syncing) debug("syncing");
|
||||
syncing = true;
|
||||
} else {
|
||||
syncing = false;
|
||||
}
|
||||
|
||||
metrics.ssbProgressRate.set(rate);
|
||||
metrics.ssbProgressFeeds.set(feeds);
|
||||
metrics.ssbProgressIncompleteFeeds.set(incompleteFeeds);
|
||||
metrics.ssbProgressProgress.set(progress);
|
||||
metrics.ssbProgressTotal.set(total);
|
||||
});
|
||||
console.log("SSB Client ready");
|
||||
});
|
||||
|
||||
module.exports.client = () => ssbClient;
|
||||
module.exports.isSyncing = () => syncing;
|
|
@ -71,8 +71,8 @@ module.exports.identityFilename = (index) => {
|
|||
return "secret_" + leftpad(index, 2, "0") + ".butt";
|
||||
};
|
||||
|
||||
module.exports.nextIdentityFilename = async (ssbServer) => {
|
||||
const identities = await ssbServer.identities.list();
|
||||
module.exports.nextIdentityFilename = async (ssbClient) => {
|
||||
const identities = await ssbClient.identities.list();
|
||||
return module.exports.identityFilename(identities.length - 1);
|
||||
};
|
||||
|
||||
|
@ -98,14 +98,14 @@ module.exports.readKey = (path) => {
|
|||
return module.exports.reconstructKeys(keyfile);
|
||||
};
|
||||
|
||||
module.exports.uploadPicture = async (ssbServer, picture) => {
|
||||
module.exports.uploadPicture = async (ssbClient, picture) => {
|
||||
const maxSize = 5 * 1024 * 1024; // 5 MB
|
||||
if (picture.size > maxSize) throw "Max size exceeded";
|
||||
|
||||
return await new Promise((resolve, reject) =>
|
||||
pull(
|
||||
pull.values(split(picture.data, 64 * 1024)),
|
||||
ssbServer.blobs.add((err, result) => {
|
||||
ssbClient.blobs.add((err, result) => {
|
||||
if (err) return reject(err);
|
||||
return resolve(result);
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue