Allow to reject friendship requests

This commit is contained in:
Rogerio Chaves 2020-04-12 11:39:51 +02:00
parent 18a618a15d
commit 938db34a11
No known key found for this signature in database
GPG Key ID: E6AF5440509B1D94
4 changed files with 92 additions and 15 deletions

View File

@ -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,

View File

@ -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");

View File

@ -52,10 +52,10 @@
</div>
<% } %>
<% if (friends.requests_received) { %>
<% if (friends.requestsReceived.length) { %>
<h2>Friend Requests</h2>
<div class="notifications" style="padding-bottom: 20px">
<% friends.requests_received.map(friend => { %>
<% friends.requestsReceived.map(friend => { %>
<a href="<%= profileUrl(friend.id) %>" class="notification-box">
<div><img src="<%= profileImageUrl(friend) %>" class="post-profile-pic" /></div>
<div><%= friend.name %></div>

View File

@ -7,17 +7,33 @@
<%= profile.description %>
<form action="<%= profileUrl(profile.id, "/add_friend") %>" method="POST" style="margin-top: 20px" >
<div style="margin-top: 20px">
<% if (friendshipStatus == "no_relation") { %>
<input type="submit" value="Add as friend" />
<form action="<%= profileUrl(profile.id, "/add_friend") %>" method="POST">
<input type="submit" value="Add as friend" />
</form>
<% } else if (friendshipStatus == "friends") { %>
✅ Friends
<form action="<%= profileUrl(profile.id, "/reject_friend") %>" style="display:inline; margin-left: 15px" method="POST">
<input type="submit" class="button-secondary" value="unfriend" />
</form>
<% } else if (friendshipStatus == "request_sent") { %>
<button disabled>Request sent</button>
<% } else if (friendshipStatus == "request_received") { %>
<input type="submit" value="Accept friendship" />
<p><%= profile.name %> sent you a friendship request</p>
<form action="<%= profileUrl(profile.id, "/add_friend") %>" style="display:inline" method="POST">
<input type="submit" value="Accept" />
</form>
<form action="<%= profileUrl(profile.id, "/reject_friend") %>" style="display:inline" method="POST">
<input type="submit" class="button-secondary" value="Reject" />
</form>
<% } else if (friendshipStatus == "request_rejected") { %>
<p>You rejected <%= profile.name %> friendship request</p>
<form action="<%= profileUrl(profile.id, "/add_friend") %>" method="POST">
<input type="submit" value="Add as friend" />
</form>
<% } %>
</form>
</div>
<h2>Friends</h2>