Skip to content

Commit

Permalink
Merge pull request #321 from near/320-update-the-feed-components-in-o…
Browse files Browse the repository at this point in the history
…ur-near-account-to-exclude-posts-from-moderated-users

feat: Filter out accounts and posts that have been self moderated
  • Loading branch information
charleslavon authored Sep 18, 2023
2 parents 1d0664f + d871040 commit e860e08
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions src/Posts.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function selectTab(selectedTab) {
loadMorePosts();
}

let filterUsersRaw = Social.get(
let gatewayModeratedUsersRaw = Social.get(
`${moderatorAccount}/moderate/users`,
"optimistic",
{
Expand All @@ -64,25 +64,52 @@ const selfFlaggedPosts = context.accountId
})
: [];

if (filterUsersRaw === null) {
const selfModeration = context.accountId
? Social.index("moderate", "main", {
accountId: context.accountId,
})
: [];

if (gatewayModeratedUsersRaw === null) {
// haven't loaded filter list yet, return early
return "";
}

const filterUsers = filterUsersRaw ? JSON.parse(filterUsersRaw) : [];
const gatewayModeratedUsers = gatewayModeratedUsersRaw ? JSON.parse(gatewayModeratedUsersRaw) : [];

// get the full list of posts that the current user has flagged so
// they can be hidden

// expecting moderation structure for accounts and posts like
// { moderate: {
// "account1.near": "block",
// "account2.near": {
// "100000123": "spam",
// },
// }
// }
function matchesModeration(moderated, item) {
let accountFound = moderated[accountId];
if (typeof accountFound === "undefined") {
return false;
}
if (typeof accountFound === "string") {
return true;
}
// match posts
return typeof accountFound[item.block_height] !== "undefined";
}

const shouldFilter = (item) => {
return (
filterUsers.includes(item.account_id) ||
gatewayModeratedUsers.includes(item.account_id) ||
selfFlaggedPosts.find((flagged) => {
return (
flagged?.value?.blockHeight === item.block_height &&
flagged?.value?.path.includes(item.account_id)
);
})
}) ||
matchesModeration(selfModeration, item)
);
};
function fetchGraphQL(operationsDoc, operationName, variables) {
Expand Down Expand Up @@ -222,7 +249,7 @@ const loadMorePosts = () => {

const hasMore = state.postsCountLeft != state.posts.length;

if (!state.initLoadPostsAll && selfFlaggedPosts && filterUsers) {
if (!state.initLoadPostsAll && selfFlaggedPosts && selfModeration && gatewayModeratedUsers) {
loadMorePosts();
State.update({ initLoadPostsAll: true });
}
Expand Down

0 comments on commit e860e08

Please sign in to comment.