]> Untitled Git - lemmy.git/commitdiff
Combine duplicate front page posts. Fixes #284
authorDessalines <tyhou13@gmx.com>
Tue, 4 Feb 2020 00:52:39 +0000 (19:52 -0500)
committerDessalines <tyhou13@gmx.com>
Tue, 4 Feb 2020 00:52:39 +0000 (19:52 -0500)
ui/src/components/community.tsx
ui/src/components/main.tsx
ui/src/components/post-listing.tsx
ui/src/components/post-listings.tsx
ui/src/interfaces.ts

index 9f96ac51edc1e0e22feb3cf175c57395399483d1..7e3e2cd7c86e2851effd22950abce6349e9996fd 100644 (file)
@@ -145,7 +145,7 @@ export class Community extends Component<any, State> {
                 )}
               </h5>
               {this.selects()}
-              <PostListings posts={this.state.posts} />
+              <PostListings posts={this.state.posts} removeDuplicates />
               {this.paginator()}
             </div>
             <div class="col-12 col-md-4">
index ed31fff4d70ff194a03efa83ace03c3fd3e72fae..6d3e18157aed5229a46886dd7856c9905a9f0ce4 100644 (file)
@@ -392,7 +392,11 @@ export class Main extends Component<any, MainState> {
         ) : (
           <div>
             {this.selects()}
-            <PostListings posts={this.state.posts} showCommunity />
+            <PostListings
+              posts={this.state.posts}
+              showCommunity
+              removeDuplicates
+            />
             {this.paginator()}
           </div>
         )}
index ba8e6980c9149b9f069be5f998aef0d8ce695cba..f05adf3945ee843e2cf3e63a70d3443d46d29280 100644 (file)
@@ -329,6 +329,18 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
               </Link>
             </li>
           </ul>
+          <ul class="list-inline mb-1 text-muted small">
+            {this.props.post.duplicates && (
+              <>
+                <li className="list-inline-item mr-2">cross-posted to:</li>
+                {this.props.post.duplicates.map(post => (
+                  <li className="list-inline-item mr-2">
+                    <Link to={`/post/${post.id}`}>{post.community_name}</Link>
+                  </li>
+                ))}
+              </>
+            )}
+          </ul>
           <ul class="list-inline mb-1 text-muted small font-weight-bold">
             {UserService.Instance.user && (
               <>
index df0bac8592f903fdffe888241b009a1a9192db73..65db3727cce040ab07f2215d1f52331277de11b5 100644 (file)
@@ -7,6 +7,7 @@ import { i18n } from '../i18next';
 interface PostListingsProps {
   posts: Array<Post>;
   showCommunity?: boolean;
+  removeDuplicates?: boolean;
 }
 
 export class PostListings extends Component<PostListingsProps, any> {
@@ -18,7 +19,10 @@ export class PostListings extends Component<PostListingsProps, any> {
     return (
       <div>
         {this.props.posts.length > 0 ? (
-          this.props.posts.map(post => (
+          (this.props.removeDuplicates
+            ? this.removeDuplicates(this.props.posts)
+            : this.props.posts
+          ).map(post => (
             <>
               <PostListing
                 post={post}
@@ -43,4 +47,55 @@ export class PostListings extends Component<PostListingsProps, any> {
       </div>
     );
   }
+
+  removeDuplicates(posts: Array<Post>): Array<Post> {
+    // A map from post url to list of posts (dupes)
+    let urlMap = new Map<string, Array<Post>>();
+
+    // Loop over the posts, find ones with same urls
+    for (let post of posts) {
+      if (
+        post.url &&
+        !post.deleted &&
+        !post.removed &&
+        !post.community_deleted &&
+        !post.community_removed
+      ) {
+        if (!urlMap.get(post.url)) {
+          urlMap.set(post.url, [post]);
+        } else {
+          urlMap.get(post.url).push(post);
+        }
+      }
+    }
+
+    // Sort by oldest
+    // Remove the ones that have no length
+    for (let e of urlMap.entries()) {
+      if (e[1].length == 1) {
+        urlMap.delete(e[0]);
+      } else {
+        e[1].sort((a, b) => a.published.localeCompare(b.published));
+      }
+    }
+
+    for (let i = 0; i < posts.length; i++) {
+      let post = posts[i];
+      if (post.url) {
+        let found = urlMap.get(post.url);
+        if (found) {
+          // If its the oldest, add
+          if (post.id == found[0].id) {
+            post.duplicates = found.slice(1);
+          }
+          // Otherwise, delete it
+          else {
+            posts.splice(i--, 1);
+          }
+        }
+      }
+    }
+
+    return posts;
+  }
 }
index 98cdc7630e030e3a4b1d1c320892703ac3a4c2d0..6c87d3e931e0d7b8625dc3db136f079bb73b5753 100644 (file)
@@ -172,6 +172,7 @@ export interface Post {
   saved?: boolean;
   upvoteLoading?: boolean;
   downvoteLoading?: boolean;
+  duplicates?: Array<Post>;
 }
 
 export interface Comment {