]> Untitled Git - lemmy-ui.git/commitdiff
Fix posts pushed from blocked users/comms. Fixes #697 (#792)
authorDessalines <dessalines@users.noreply.github.com>
Thu, 22 Sep 2022 17:13:59 +0000 (13:13 -0400)
committerGitHub <noreply@github.com>
Thu, 22 Sep 2022 17:13:59 +0000 (17:13 +0000)
src/shared/components/community/community.tsx
src/shared/components/home/home.tsx
src/shared/utils.ts

index a509df1577abdc36981f7a7a6afd37950fa79907..82049100f60205ce772114e72685ce6ea91030f3 100644 (file)
@@ -49,7 +49,9 @@ import {
   getDataTypeFromProps,
   getPageFromProps,
   getSortTypeFromProps,
+  isPostBlocked,
   notifyPost,
+  nsfwCheck,
   postToCommentSortType,
   relTags,
   restoreScrollPosition,
@@ -594,15 +596,24 @@ export class Community extends Component<any, State> {
       this.setState(this.state);
     } else if (op == UserOperation.CreatePost) {
       let data = wsJsonToRes<PostResponse>(msg, PostResponse);
-      this.state.posts.unshift(data.post_view);
+
+      let showPostNotifs = UserService.Instance.myUserInfo
+        .map(m => m.local_user_view.local_user.show_new_post_notifs)
+        .unwrapOr(false);
+
+      // Only push these if you're on the first page, you pass the nsfw check, and it isn't blocked
+      //
       if (
-        UserService.Instance.myUserInfo
-          .map(m => m.local_user_view.local_user.show_new_post_notifs)
-          .unwrapOr(false)
+        this.state.page == 1 &&
+        nsfwCheck(data.post_view) &&
+        !isPostBlocked(data.post_view)
       ) {
-        notifyPost(data.post_view, this.context.router);
+        this.state.posts.unshift(data.post_view);
+        if (showPostNotifs) {
+          notifyPost(data.post_view, this.context.router);
+        }
+        this.setState(this.state);
       }
-      this.setState(this.state);
     } else if (op == UserOperation.CreatePostLike) {
       let data = wsJsonToRes<PostResponse>(msg, PostResponse);
       createPostLikeFindRes(data.post_view, this.state.posts);
index 3761998ea5a7f55ec2b3e08e26262ed79718d7dd..3d163197fff63a4a0ee9010a740965baaaa2ff34 100644 (file)
@@ -52,7 +52,9 @@ import {
   getPageFromProps,
   getSortTypeFromProps,
   isBrowser,
+  isPostBlocked,
   notifyPost,
+  nsfwCheck,
   postToCommentSortType,
   relTags,
   restoreScrollPosition,
@@ -756,21 +758,17 @@ export class Home extends Component<any, HomeState> {
       setupTippy();
     } else if (op == UserOperation.CreatePost) {
       let data = wsJsonToRes<PostResponse>(msg, PostResponse);
-      // NSFW check
-      let nsfw = data.post_view.post.nsfw || data.post_view.community.nsfw;
-      let nsfwCheck =
-        !nsfw ||
-        (nsfw &&
-          UserService.Instance.myUserInfo
-            .map(m => m.local_user_view.local_user.show_nsfw)
-            .unwrapOr(false));
 
       let showPostNotifs = UserService.Instance.myUserInfo
         .map(m => m.local_user_view.local_user.show_new_post_notifs)
         .unwrapOr(false);
 
-      // Only push these if you're on the first page, and you pass the nsfw check
-      if (this.state.page == 1 && nsfwCheck) {
+      // Only push these if you're on the first page, you pass the nsfw check, and it isn't blocked
+      if (
+        this.state.page == 1 &&
+        nsfwCheck(data.post_view) &&
+        !isPostBlocked(data.post_view)
+      ) {
         // If you're on subscribed, only push it if you're subscribed.
         if (this.state.listingType == ListingType.Subscribed) {
           if (
index 6c6db1fdf55f1de51a94b483b4db9632f362f83e..5fda2e60e55fd3b83152a9cc755c2de62e26d36e 100644 (file)
@@ -1487,3 +1487,33 @@ export function canCreateCommunity(siteRes: GetSiteResponse): boolean {
     .unwrapOr(false);
   return !adminOnly || amAdmin(Some(siteRes.admins));
 }
+
+export function isPostBlocked(
+  pv: PostView,
+  myUserInfo = UserService.Instance.myUserInfo
+): boolean {
+  return myUserInfo
+    .map(
+      mui =>
+        mui.community_blocks
+          .map(c => c.community.id)
+          .includes(pv.community.id) ||
+        mui.person_blocks.map(p => p.target.id).includes(pv.creator.id)
+    )
+    .unwrapOr(false);
+}
+
+/// Checks to make sure you can view NSFW posts. Returns true if you can.
+export function nsfwCheck(
+  pv: PostView,
+  myUserInfo = UserService.Instance.myUserInfo
+): boolean {
+  let nsfw = pv.post.nsfw || pv.community.nsfw;
+  return (
+    !nsfw ||
+    (nsfw &&
+      myUserInfo
+        .map(m => m.local_user_view.local_user.show_nsfw)
+        .unwrapOr(false))
+  );
+}