Merge pull request #372 from christianbundy/block-unblock

Add block and unblock to author profile
This commit is contained in:
Sean Billig 2020-03-28 15:51:24 -07:00 committed by GitHub
commit 573d7096f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 101 additions and 47 deletions

View File

@ -353,7 +353,7 @@ router
name,
description,
avatarUrl,
relationship: null,
relationship: { me: true },
});
})
.get("/profile/edit", async (ctx) => {
@ -681,6 +681,18 @@ router
ctx.body = await friend.unfollow(feed);
ctx.redirect(referer.href);
})
.post("/block/:feed", koaBody(), async (ctx) => {
const { feed } = ctx.params;
const referer = new URL(ctx.request.header.referer);
ctx.body = await friend.block(feed);
ctx.redirect(referer.href);
})
.post("/unblock/:feed", koaBody(), async (ctx) => {
const { feed } = ctx.params;
const referer = new URL(ctx.request.header.referer);
ctx.body = await friend.unblock(feed);
ctx.redirect(referer.href);
})
.post("/like/:message", koaBody(), async (ctx) => {
const { message } = ctx.params;
// TODO: convert all so `message` is full message and `messageKey` is key

View File

@ -153,45 +153,66 @@ module.exports = ({ cooler, isPublic }) => {
};
models.friend = {
isFollowing: async (feedId) => {
const ssb = await cooler.open();
const { id } = ssb;
/** @param {{ feedId: string, following: boolean, blocking: boolean }} input */
setRelationship: async ({ feedId, following, blocking }) => {
if (following && blocking) {
throw new Error("Cannot follow and block at the same time");
}
const current = await models.friend.getRelationship(feedId);
const alreadySet =
current.following === following && current.blocking === blocking;
if (alreadySet) {
// The following state is already set, don't re-set it.
return;
}
const isFollowing = await ssb.friends.isFollowing({
source: id,
dest: feedId,
});
return isFollowing;
},
setFollowing: async ({ feedId, following }) => {
const ssb = await cooler.open();
const content = {
type: "contact",
contact: feedId,
following,
blocking,
};
return ssb.publish(content);
},
follow: async (feedId) => {
const isFollowing = await models.friend.isFollowing(feedId);
if (!isFollowing) {
await models.friend.setFollowing({ feedId, following: true });
}
},
unfollow: async (feedId) => {
const isFollowing = await models.friend.isFollowing(feedId);
if (isFollowing) {
await models.friend.setFollowing({ feedId, following: false });
}
},
follow: (feedId) =>
models.friend.setRelationship({
feedId,
following: true,
blocking: false,
}),
unfollow: (feedId) =>
models.friend.setRelationship({
feedId,
following: false,
blocking: false,
}),
block: (feedId) =>
models.friend.setRelationship({
feedId,
blocking: true,
following: false,
}),
unblock: (feedId) =>
models.friend.setRelationship({
feedId,
blocking: false,
following: false,
}),
/**
* @param feedId {string}
* @returns {Promise<{me: boolean, following: boolean, blocking: boolean }>}
*/
getRelationship: async (feedId) => {
const ssb = await cooler.open();
const { id } = ssb;
if (feedId === id) {
return null;
return { me: true, following: false, blocking: false };
}
const isFollowing = await ssb.friends.isFollowing({
@ -205,6 +226,7 @@ module.exports = ({ cooler, isPublic }) => {
});
return {
me: false,
following: isFollowing,
blocking: isBlocking,
};
@ -537,6 +559,16 @@ module.exports = ({ cooler, isPublic }) => {
const myFeedId = ssb.id;
const options = configure({ id: feedId }, customOptions);
const { blocking } = await models.friend.getRelationship(feedId);
// Avoid streaming any messages from this feed. If we used the social
// filter here it would continue streaming all messages from this author
// until it consumed the entire feed.
if (blocking) {
return [];
}
const source = ssb.createUserStream(options);
const messages = await new Promise((resolve, reject) => {

View File

@ -59,6 +59,8 @@ const i18n = {
// relationships
unfollow: "Unfollow",
follow: "Follow",
block: "Block",
unblock: "Unblock",
relationshipFollowing: "You are following",
relationshipYou: "This is you",
relationshipBlocking: "You are blocking",

View File

@ -618,6 +618,9 @@ exports.editProfileView = ({ name, description }) =>
)
);
/**
* @param {{avatarUrl: string, description: string, feedId: string, messages: any[], name: string, relationship: object}} input
*/
exports.authorView = ({
avatarUrl,
description,
@ -629,32 +632,37 @@ exports.authorView = ({
const mention = `[@${name}](${feedId})`;
const markdownMention = highlightJs.highlight("markdown", mention).value;
const areFollowing =
relationship !== null &&
relationship.following === true &&
relationship.blocking === false;
const contactForms = [];
const contactFormType = areFollowing ? "unfollow" : "follow";
const contactFormTypeLabel = i18n[contactFormType];
const contactForm =
relationship === null
? null // We're on our own profile!
: form(
const addForm = ({ action }) =>
contactForms.push(
form(
{
action: `/${action}/${encodeURIComponent(feedId)}`,
method: "post",
},
button(
{
action: `/${contactFormType}/${encodeURIComponent(feedId)}`,
method: "post",
type: "submit",
},
button(
{
type: "submit",
},
contactFormTypeLabel
)
);
i18n[action]
)
)
);
if (relationship.me === false) {
if (relationship.following) {
addForm({ action: "unfollow" });
} else if (relationship.blocking) {
addForm({ action: "unblock" });
} else {
addForm({ action: "follow" });
addForm({ action: "block" });
}
}
const relationshipText = (() => {
if (relationship === null) {
if (relationship.me === true) {
return i18n.relationshipYou;
} else if (
relationship.following === true &&
@ -697,8 +705,8 @@ exports.authorView = ({
div(
a({ href: `/likes/${encodeURIComponent(feedId)}` }, i18n.viewLikes),
span(nbsp, relationshipText),
contactForm,
relationship === null
...contactForms,
relationship.me
? a({ href: `/profile/edit` }, nbsp, i18n.editProfile)
: null
),