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