]> Untitled Git - lemmy.git/blob - ui/src/components/post-listings.tsx
Merge branch 'nutomic-federation' into federation
[lemmy.git] / ui / src / components / post-listings.tsx
1 import { Component } from 'inferno';
2 import { Link } from 'inferno-router';
3 import { Post, SortType } from '../interfaces';
4 import { postSort } from '../utils';
5 import { PostListing } from './post-listing';
6 import { i18n } from '../i18next';
7 import { T } from 'inferno-i18next';
8
9 interface PostListingsProps {
10   posts: Array<Post>;
11   showCommunity?: boolean;
12   removeDuplicates?: boolean;
13   sort?: SortType;
14 }
15
16 export class PostListings extends Component<PostListingsProps, any> {
17   constructor(props: any, context: any) {
18     super(props, context);
19   }
20
21   render() {
22     return (
23       <div>
24         {this.props.posts.length > 0 ? (
25           this.outer().map(post => (
26             <>
27               <PostListing
28                 post={post}
29                 showCommunity={this.props.showCommunity}
30               />
31               <hr class="my-2" />
32             </>
33           ))
34         ) : (
35           <>
36             <div>{i18n.t('no_posts')}</div>
37             {this.props.showCommunity !== undefined && (
38               <T i18nKey="subscribe_to_communities">
39                 #<Link to="/communities">#</Link>
40               </T>
41             )}
42           </>
43         )}
44       </div>
45     );
46   }
47
48   outer(): Array<Post> {
49     let out = this.props.posts;
50     if (this.props.removeDuplicates) {
51       out = this.removeDuplicates(out);
52     }
53
54     if (this.props.sort !== undefined) {
55       postSort(out, this.props.sort, this.props.showCommunity == undefined);
56     }
57
58     return out;
59   }
60
61   removeDuplicates(posts: Array<Post>): Array<Post> {
62     // A map from post url to list of posts (dupes)
63     let urlMap = new Map<string, Array<Post>>();
64
65     // Loop over the posts, find ones with same urls
66     for (let post of posts) {
67       if (
68         post.url &&
69         !post.deleted &&
70         !post.removed &&
71         !post.community_deleted &&
72         !post.community_removed
73       ) {
74         if (!urlMap.get(post.url)) {
75           urlMap.set(post.url, [post]);
76         } else {
77           urlMap.get(post.url).push(post);
78         }
79       }
80     }
81
82     // Sort by oldest
83     // Remove the ones that have no length
84     for (let e of urlMap.entries()) {
85       if (e[1].length == 1) {
86         urlMap.delete(e[0]);
87       } else {
88         e[1].sort((a, b) => a.published.localeCompare(b.published));
89       }
90     }
91
92     for (let i = 0; i < posts.length; i++) {
93       let post = posts[i];
94       if (post.url) {
95         let found = urlMap.get(post.url);
96         if (found) {
97           // If its the oldest, add
98           if (post.id == found[0].id) {
99             post.duplicates = found.slice(1);
100           }
101           // Otherwise, delete it
102           else {
103             posts.splice(i--, 1);
104           }
105         }
106       }
107     }
108
109     return posts;
110   }
111 }