summaryrefslogtreecommitdiffstats
path: root/ui/src/components/post-listings.tsx
diff options
context:
space:
mode:
authorDessalines <tyhou13@gmx.com>2020-02-03 19:52:39 -0500
committerDessalines <tyhou13@gmx.com>2020-02-03 19:52:39 -0500
commite4dfa5e52f911a6dbb5df41932a70dd9af3c9753 (patch)
tree28a97128f786ac758cbe3b2b2507d67565b0995a /ui/src/components/post-listings.tsx
parenta28f8c7ac44594fbefb1d170b4e0365d04b6a204 (diff)
Combine duplicate front page posts. Fixes #284
Diffstat (limited to 'ui/src/components/post-listings.tsx')
-rw-r--r--ui/src/components/post-listings.tsx57
1 files changed, 56 insertions, 1 deletions
diff --git a/ui/src/components/post-listings.tsx b/ui/src/components/post-listings.tsx
index df0bac85..65db3727 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<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;
+ }
}