2019-04-29 00:19:04 +00:00
|
|
|
import { Component } from 'inferno';
|
2019-04-05 19:14:54 +00:00
|
|
|
import { Link } from 'inferno-router';
|
2020-02-09 16:44:24 +00:00
|
|
|
import { Post, SortType } from '../interfaces';
|
|
|
|
import { postSort } from '../utils';
|
2019-04-05 19:14:54 +00:00
|
|
|
import { PostListing } from './post-listing';
|
2020-01-31 20:52:27 +00:00
|
|
|
import { i18n } from '../i18next';
|
2020-02-05 17:56:01 +00:00
|
|
|
import { T } from 'inferno-i18next';
|
2019-04-05 19:14:54 +00:00
|
|
|
|
|
|
|
interface PostListingsProps {
|
|
|
|
posts: Array<Post>;
|
2019-04-29 00:19:04 +00:00
|
|
|
showCommunity?: boolean;
|
2020-02-04 00:52:39 +00:00
|
|
|
removeDuplicates?: boolean;
|
2020-02-09 16:44:24 +00:00
|
|
|
sort?: SortType;
|
2020-07-08 05:02:14 +00:00
|
|
|
enableDownvotes: boolean;
|
|
|
|
enableNsfw: boolean;
|
2019-04-05 19:14:54 +00:00
|
|
|
}
|
|
|
|
|
2019-04-29 00:19:04 +00:00
|
|
|
export class PostListings extends Component<PostListingsProps, any> {
|
2019-04-08 05:19:02 +00:00
|
|
|
constructor(props: any, context: any) {
|
2019-04-05 19:14:54 +00:00
|
|
|
super(props, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div>
|
2019-10-19 00:20:27 +00:00
|
|
|
{this.props.posts.length > 0 ? (
|
2020-02-09 16:44:24 +00:00
|
|
|
this.outer().map(post => (
|
2019-10-19 00:20:27 +00:00
|
|
|
<>
|
|
|
|
<PostListing
|
|
|
|
post={post}
|
|
|
|
showCommunity={this.props.showCommunity}
|
2020-07-08 05:02:14 +00:00
|
|
|
enableDownvotes={this.props.enableDownvotes}
|
|
|
|
enableNsfw={this.props.enableNsfw}
|
2019-10-19 00:20:27 +00:00
|
|
|
/>
|
2020-03-19 15:45:23 +00:00
|
|
|
<hr class="my-2" />
|
2019-10-19 00:20:27 +00:00
|
|
|
</>
|
|
|
|
))
|
|
|
|
) : (
|
2019-08-10 00:14:43 +00:00
|
|
|
<>
|
2020-02-02 18:50:44 +00:00
|
|
|
<div>{i18n.t('no_posts')}</div>
|
2019-10-19 00:20:27 +00:00
|
|
|
{this.props.showCommunity !== undefined && (
|
2020-02-05 17:56:01 +00:00
|
|
|
<T i18nKey="subscribe_to_communities">
|
|
|
|
#<Link to="/communities">#</Link>
|
|
|
|
</T>
|
2019-10-19 00:20:27 +00:00
|
|
|
)}
|
2019-08-10 00:14:43 +00:00
|
|
|
</>
|
2019-10-19 00:20:27 +00:00
|
|
|
)}
|
2019-04-05 19:14:54 +00:00
|
|
|
</div>
|
2019-10-19 00:20:27 +00:00
|
|
|
);
|
2019-04-05 19:14:54 +00:00
|
|
|
}
|
2020-02-04 00:52:39 +00:00
|
|
|
|
2020-02-09 16:44:24 +00:00
|
|
|
outer(): Array<Post> {
|
|
|
|
let out = this.props.posts;
|
|
|
|
if (this.props.removeDuplicates) {
|
|
|
|
out = this.removeDuplicates(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.props.sort !== undefined) {
|
2020-03-04 16:46:34 +00:00
|
|
|
postSort(out, this.props.sort, this.props.showCommunity == undefined);
|
2020-02-09 16:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2020-02-04 00:52:39 +00:00
|
|
|
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;
|
|
|
|
}
|
2019-04-05 19:14:54 +00:00
|
|
|
}
|