From 7037506566a64a18c0507f8a3490a15441e28648 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Mon, 3 Feb 2020 19:52:39 -0500 Subject: [PATCH] Combine duplicate front page posts. Fixes #284 --- ui/src/components/community.tsx | 2 +- ui/src/components/main.tsx | 6 ++- ui/src/components/post-listing.tsx | 12 ++++++ ui/src/components/post-listings.tsx | 57 ++++++++++++++++++++++++++++- ui/src/interfaces.ts | 1 + 5 files changed, 75 insertions(+), 3 deletions(-) diff --git a/ui/src/components/community.tsx b/ui/src/components/community.tsx index 9f96ac51e..7e3e2cd7c 100644 --- a/ui/src/components/community.tsx +++ b/ui/src/components/community.tsx @@ -145,7 +145,7 @@ export class Community extends Component { )} {this.selects()} - + {this.paginator()}
diff --git a/ui/src/components/main.tsx b/ui/src/components/main.tsx index ed31fff4d..6d3e18157 100644 --- a/ui/src/components/main.tsx +++ b/ui/src/components/main.tsx @@ -392,7 +392,11 @@ export class Main extends Component { ) : (
{this.selects()} - + {this.paginator()}
)} diff --git a/ui/src/components/post-listing.tsx b/ui/src/components/post-listing.tsx index ba8e6980c..f05adf394 100644 --- a/ui/src/components/post-listing.tsx +++ b/ui/src/components/post-listing.tsx @@ -329,6 +329,18 @@ export class PostListing extends Component { +
    + {this.props.post.duplicates && ( + <> +
  • cross-posted to:
  • + {this.props.post.duplicates.map(post => ( +
  • + {post.community_name} +
  • + ))} + + )} +
    {UserService.Instance.user && ( <> diff --git a/ui/src/components/post-listings.tsx b/ui/src/components/post-listings.tsx index df0bac859..65db3727c 100644 --- a/ui/src/components/post-listings.tsx +++ b/ui/src/components/post-listings.tsx @@ -7,6 +7,7 @@ import { i18n } from '../i18next'; interface PostListingsProps { posts: Array; showCommunity?: boolean; + removeDuplicates?: boolean; } export class PostListings extends Component { @@ -18,7 +19,10 @@ export class PostListings extends Component { return (
    {this.props.posts.length > 0 ? ( - this.props.posts.map(post => ( + (this.props.removeDuplicates + ? this.removeDuplicates(this.props.posts) + : this.props.posts + ).map(post => ( <> {
    ); } + + removeDuplicates(posts: Array): Array { + // A map from post url to list of posts (dupes) + let urlMap = new Map>(); + + // 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; + } } diff --git a/ui/src/interfaces.ts b/ui/src/interfaces.ts index 98cdc7630..6c87d3e93 100644 --- a/ui/src/interfaces.ts +++ b/ui/src/interfaces.ts @@ -172,6 +172,7 @@ export interface Post { saved?: boolean; upvoteLoading?: boolean; downvoteLoading?: boolean; + duplicates?: Array; } export interface Comment {