]> Untitled Git - lemmy.git/blob - ui/src/components/post-listings.tsx
Merge remote-tracking branch 'weblate/master'
[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="d-md-none my-2" />
32               <div class="d-none d-md-block my-2"></div>
33             </>
34           ))
35         ) : (
36           <>
37             <div>{i18n.t('no_posts')}</div>
38             {this.props.showCommunity !== undefined && (
39               <T i18nKey="subscribe_to_communities">
40                 #<Link to="/communities">#</Link>
41               </T>
42             )}
43           </>
44         )}
45       </div>
46     );
47   }
48
49   outer(): Array<Post> {
50     let out = this.props.posts;
51     if (this.props.removeDuplicates) {
52       out = this.removeDuplicates(out);
53     }
54
55     if (this.props.sort !== undefined) {
56       postSort(out, this.props.sort, this.props.showCommunity == undefined);
57     }
58
59     return out;
60   }
61
62   removeDuplicates(posts: Array<Post>): Array<Post> {
63     // A map from post url to list of posts (dupes)
64     let urlMap = new Map<string, Array<Post>>();
65
66     // Loop over the posts, find ones with same urls
67     for (let post of posts) {
68       if (
69         post.url &&
70         !post.deleted &&
71         !post.removed &&
72         !post.community_deleted &&
73         !post.community_removed
74       ) {
75         if (!urlMap.get(post.url)) {
76           urlMap.set(post.url, [post]);
77         } else {
78           urlMap.get(post.url).push(post);
79         }
80       }
81     }
82
83     // Sort by oldest
84     // Remove the ones that have no length
85     for (let e of urlMap.entries()) {
86       if (e[1].length == 1) {
87         urlMap.delete(e[0]);
88       } else {
89         e[1].sort((a, b) => a.published.localeCompare(b.published));
90       }
91     }
92
93     for (let i = 0; i < posts.length; i++) {
94       let post = posts[i];
95       if (post.url) {
96         let found = urlMap.get(post.url);
97         if (found) {
98           // If its the oldest, add
99           if (post.id == found[0].id) {
100             post.duplicates = found.slice(1);
101           }
102           // Otherwise, delete it
103           else {
104             posts.splice(i--, 1);
105           }
106         }
107       }
108     }
109
110     return posts;
111   }
112 }