2021-02-22 02:39:04 +00:00
|
|
|
import { Component } from "inferno";
|
2021-09-18 14:27:49 +00:00
|
|
|
import { T } from "inferno-i18next-dess";
|
2021-02-22 02:39:04 +00:00
|
|
|
import { Link } from "inferno-router";
|
|
|
|
import { PostView } from "lemmy-js-client";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { i18n } from "../../i18next";
|
2021-02-22 02:39:04 +00:00
|
|
|
import { PostListing } from "./post-listing";
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
interface PostListingsProps {
|
2020-12-24 01:58:27 +00:00
|
|
|
posts: PostView[];
|
2020-09-06 16:15:25 +00:00
|
|
|
showCommunity?: boolean;
|
|
|
|
removeDuplicates?: boolean;
|
|
|
|
enableDownvotes: boolean;
|
|
|
|
enableNsfw: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class PostListings extends Component<PostListingsProps, any> {
|
2020-12-24 01:58:27 +00:00
|
|
|
private duplicatesMap = new Map<number, PostView[]>();
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{this.props.posts.length > 0 ? (
|
2020-12-24 01:58:27 +00:00
|
|
|
this.outer().map(post_view => (
|
2020-09-06 16:15:25 +00:00
|
|
|
<>
|
|
|
|
<PostListing
|
2020-12-24 01:58:27 +00:00
|
|
|
post_view={post_view}
|
|
|
|
duplicates={this.duplicatesMap.get(post_view.post.id)}
|
2020-09-06 16:15:25 +00:00
|
|
|
showCommunity={this.props.showCommunity}
|
|
|
|
enableDownvotes={this.props.enableDownvotes}
|
|
|
|
enableNsfw={this.props.enableNsfw}
|
|
|
|
/>
|
|
|
|
<hr class="my-3" />
|
|
|
|
</>
|
|
|
|
))
|
|
|
|
) : (
|
|
|
|
<>
|
2021-02-22 02:39:04 +00:00
|
|
|
<div>{i18n.t("no_posts")}</div>
|
2020-09-06 16:15:25 +00:00
|
|
|
{this.props.showCommunity !== undefined && (
|
|
|
|
<T i18nKey="subscribe_to_communities">
|
|
|
|
#<Link to="/communities">#</Link>
|
|
|
|
</T>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
outer(): PostView[] {
|
2020-09-06 16:15:25 +00:00
|
|
|
let out = this.props.posts;
|
|
|
|
if (this.props.removeDuplicates) {
|
|
|
|
out = this.removeDuplicates(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
removeDuplicates(posts: PostView[]): PostView[] {
|
2020-09-06 16:15:25 +00:00
|
|
|
// A map from post url to list of posts (dupes)
|
2020-12-24 01:58:27 +00:00
|
|
|
let urlMap = new Map<string, PostView[]>();
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
// Loop over the posts, find ones with same urls
|
2020-12-24 01:58:27 +00:00
|
|
|
for (let pv of posts) {
|
2020-09-06 16:15:25 +00:00
|
|
|
if (
|
2020-12-24 01:58:27 +00:00
|
|
|
pv.post.url &&
|
|
|
|
!pv.post.deleted &&
|
|
|
|
!pv.post.removed &&
|
|
|
|
!pv.community.deleted &&
|
|
|
|
!pv.community.removed
|
2020-09-06 16:15:25 +00:00
|
|
|
) {
|
2020-12-24 01:58:27 +00:00
|
|
|
if (!urlMap.get(pv.post.url)) {
|
|
|
|
urlMap.set(pv.post.url, [pv]);
|
2020-09-06 16:15:25 +00:00
|
|
|
} else {
|
2020-12-24 01:58:27 +00:00
|
|
|
urlMap.get(pv.post.url).push(pv);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2020-12-24 01:58:27 +00:00
|
|
|
e[1].sort((a, b) => a.post.published.localeCompare(b.post.published));
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < posts.length; i++) {
|
2020-12-24 01:58:27 +00:00
|
|
|
let pv = posts[i];
|
|
|
|
if (pv.post.url) {
|
|
|
|
let found = urlMap.get(pv.post.url);
|
2020-09-06 16:15:25 +00:00
|
|
|
if (found) {
|
|
|
|
// If its the oldest, add
|
2020-12-24 01:58:27 +00:00
|
|
|
if (pv.post.id == found[0].post.id) {
|
|
|
|
this.duplicatesMap.set(pv.post.id, found.slice(1));
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
// Otherwise, delete it
|
|
|
|
else {
|
|
|
|
posts.splice(i--, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return posts;
|
|
|
|
}
|
|
|
|
}
|