diff --git a/app/lib/express.js b/app/lib/express.js index 8cb4285..989d2d4 100644 --- a/app/lib/express.js +++ b/app/lib/express.js @@ -230,6 +230,25 @@ router.post("/profile/:id/add_friend", async (req, res) => { res.redirect(`/profile/${id}`); }); +router.post("/profile/:id/reject_friend", async (req, res) => { + const id = req.params.id; + if (id == req.context.profile.id) { + throw "cannot reject yourself"; + } + + await ssbServer.identities.publishAs({ + id: req.context.profile.id, + private: false, + content: { + type: "contact", + contact: id, + following: false, + }, + }); + + res.redirect(`/profile/${id}`); +}); + router.post("/publish", async (req, res) => { await ssbServer.identities.publishAs({ id: req.context.profile.id, diff --git a/app/lib/queries.js b/app/lib/queries.js index 259927d..dee224d 100644 --- a/app/lib/queries.js +++ b/app/lib/queries.js @@ -286,18 +286,25 @@ const getFriends = async (ssbServer, profile) => { }); let network = {}; + let requestRejections = []; for (let contact of contacts.reverse()) { if (contact.content.following) { network[contact.author] = network[contact.author] || {}; network[contact.author][contact.content.contact] = true; - } else if (contact.content.blocking || contact.content.flagged) { - delete network[contact.author][contact.content.contact]; + } else { + // contact.content.blocking or contact.content.flagged or !contact.content.following + if (contact.author == profile.id && contact.content.following === false) { + requestRejections.push(contact.content.contact); + } + + if (network[contact.author]) + delete network[contact.author][contact.content.contact]; } } let friends = []; - let requests_sent = []; - let requests_received = []; + let requestsSent = []; + let requestsReceived = []; const unique = (x) => Array.from(new Set(x)); const allIds = unique( @@ -319,21 +326,52 @@ const getFriends = async (ssbServer, profile) => { if (isFollowing && isFollowingBack) { friends.push(profilesHash[key]); } else if (isFollowing && !isFollowingBack) { - requests_sent.push(profilesHash[key]); + requestsSent.push(profilesHash[key]); } else if (!isFollowing && isFollowingBack) { - requests_received.push(profilesHash[key]); + if (!requestRejections.includes(key)) + requestsReceived.push(profilesHash[key]); } } debugFriends("Done"); - return { friends, requests_sent, requests_received }; + return { friends, requestsSent, requestsReceived }; }; const getFriendshipStatus = async (ssbServer, source, dest) => { debugFriendshipStatus("Fetching"); - const [isFollowing, isFollowingBack] = await Promise.all([ + + let requestRejectionsPromise = new Promise((resolve, reject) => { + pull( + ssbServer.query.read({ + reverse: true, + query: [ + { + $filter: { + value: { + author: source, + content: { + type: "contact", + following: false, + }, + }, + }, + }, + ], + limit: 100, + }), + pull.collect((err, msgs) => { + const entries = msgs.map((x) => x.value); + + if (err) return reject(err); + return resolve(entries); + }) + ); + }); + + const [isFollowing, isFollowingBack, requestRejections] = await Promise.all([ ssbServer.friends.isFollowing({ source: source, dest: dest }), ssbServer.friends.isFollowing({ source: dest, dest: source }), + requestRejectionsPromise.then((x) => x.map((y) => y.content.contact)), ]); let status = "no_relation"; @@ -342,7 +380,11 @@ const getFriendshipStatus = async (ssbServer, source, dest) => { } else if (isFollowing && !isFollowingBack) { status = "request_sent"; } else if (!isFollowing && isFollowingBack) { - status = "request_received"; + if (requestRejections.includes(dest)) { + status = "request_rejected"; + } else { + status = "request_received"; + } } debugFriendshipStatus("Done"); diff --git a/app/views/home.ejs b/app/views/home.ejs index 29f65c8..616c37e 100644 --- a/app/views/home.ejs +++ b/app/views/home.ejs @@ -52,10 +52,10 @@ <% } %> - <% if (friends.requests_received) { %> + <% if (friends.requestsReceived.length) { %>

Friend Requests

- <% friends.requests_received.map(friend => { %> + <% friends.requestsReceived.map(friend => { %>
<%= friend.name %>
diff --git a/app/views/profile.ejs b/app/views/profile.ejs index ac6db87..6a7dc06 100644 --- a/app/views/profile.ejs +++ b/app/views/profile.ejs @@ -7,17 +7,33 @@ <%= profile.description %> -
" method="POST" style="margin-top: 20px" > +
<% if (friendshipStatus == "no_relation") { %> - + " method="POST"> + + <% } else if (friendshipStatus == "friends") { %> ✅ Friends +
" style="display:inline; margin-left: 15px" method="POST"> + +
<% } else if (friendshipStatus == "request_sent") { %> <% } else if (friendshipStatus == "request_received") { %> - +

<%= profile.name %> sent you a friendship request

+
" style="display:inline" method="POST"> + +
+
" style="display:inline" method="POST"> + +
+ <% } else if (friendshipStatus == "request_rejected") { %> +

You rejected <%= profile.name %> friendship request

+
" method="POST"> + +
<% } %> - +

Friends