]> Untitled Git - lemmy.git/blobdiff - ui/src/components/post-listings.tsx
routes.api: fix get_captcha endpoint (#1135)
[lemmy.git] / ui / src / components / post-listings.tsx
index 98fe8f21171695728667734fcab63e5473848496..2c9b4a882663e4e154c6e406fbf11fd807c775aa 100644 (file)
-import { Component, linkEvent } from 'inferno';
+import { Component } from 'inferno';
 import { Link } from 'inferno-router';
-import { Subscription } from "rxjs";
-import { retryWhen, delay, take } from 'rxjs/operators';
-import { UserOperation, Community as CommunityI, GetCommunityResponse, CommunityResponse, Post, GetPostsForm, ListingSortType, ListingType, GetPostsResponse, CreatePostLikeForm, CreatePostLikeResponse, CommunityUser} from '../interfaces';
-import { WebSocketService, UserService } from '../services';
-import { MomentTime } from './moment-time';
+import { Post, SortType } from 'lemmy-js-client';
+import { postSort } from '../utils';
 import { PostListing } from './post-listing';
-import { Sidebar } from './sidebar';
-import { msgOp, mdToHtml } from '../utils';
-
+import { i18n } from '../i18next';
+import { T } from 'inferno-i18next';
 
 interface PostListingsProps {
-  communityId?: number;
-}
-
-interface PostListingsState {
-  community: CommunityI;
-  moderators: Array<CommunityUser>;
   posts: Array<Post>;
-  sortType: ListingSortType;
-  type_: ListingType;
+  showCommunity?: boolean;
+  removeDuplicates?: boolean;
+  sort?: SortType;
+  enableDownvotes: boolean;
+  enableNsfw: boolean;
 }
 
-export class PostListings extends Component<PostListingsProps, PostListingsState> {
-
-  private subscription: Subscription;
-  private emptyState: PostListingsState = {
-    community: {
-      id: null,
-      name: null,
-      title: null,
-      category_id: null,
-      category_name: null,
-      creator_id: null,
-      creator_name: null,
-      number_of_subscribers: null,
-      number_of_posts: null,
-      number_of_comments: null,
-      published: null
-    },
-    moderators: [],
-    posts: [],
-    sortType: ListingSortType.Hot,
-    type_: this.props.communityId 
-    ? ListingType.Community 
-    : UserService.Instance.loggedIn
-    ? ListingType.Subscribed 
-    : ListingType.All
-  }
-
-  constructor(props, context) {
+export class PostListings extends Component<PostListingsProps, any> {
+  constructor(props: any, context: any) {
     super(props, context);
-
-
-    this.state = this.emptyState;
-
-    this.subscription = WebSocketService.Instance.subject
-      .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
-      .subscribe(
-        (msg) => this.parseMessage(msg),
-        (err) => console.error(err),
-        () => console.log('complete')
-      );
-
-    let getPostsForm: GetPostsForm = {
-      type_: ListingType[this.state.type_],
-      community_id: this.props.communityId,
-      limit: 10,
-      sort: ListingSortType[ListingSortType.Hot],
-    }
-    WebSocketService.Instance.getPosts(getPostsForm);
-  }
-
-  componentWillUnmount() {
-    this.subscription.unsubscribe();
   }
 
   render() {
     return (
       <div>
-        <div>{this.selects()}</div>
-        {this.state.posts.length > 0 
-          ? this.state.posts.map(post => 
-            <PostListing post={post} showCommunity={!this.props.communityId}/>) 
-          : <div>No Listings. Subscribe to some <Link to="/communities">forums</Link>.</div>
-        }
+        {this.props.posts.length > 0 ? (
+          this.outer().map(post => (
+            <>
+              <PostListing
+                post={post}
+                showCommunity={this.props.showCommunity}
+                enableDownvotes={this.props.enableDownvotes}
+                enableNsfw={this.props.enableNsfw}
+              />
+              <hr class="my-3" />
+            </>
+          ))
+        ) : (
+          <>
+            <div>{i18n.t('no_posts')}</div>
+            {this.props.showCommunity !== undefined && (
+              <T i18nKey="subscribe_to_communities">
+                #<Link to="/communities">#</Link>
+              </T>
+            )}
+          </>
+        )}
       </div>
-    )
+    );
   }
 
-  selects() {
-    return (
-      <div className="mb-2">
-        <select value={this.state.sortType} onChange={linkEvent(this, this.handleSortChange)} class="custom-select w-auto">
-          <option disabled>Sort Type</option>
-          <option value={ListingSortType.Hot}>Hot</option>
-          <option value={ListingSortType.New}>New</option>
-          <option disabled>──────────</option>
-          <option value={ListingSortType.TopDay}>Top Day</option>
-          <option value={ListingSortType.TopWeek}>Week</option>
-          <option value={ListingSortType.TopMonth}>Month</option>
-          <option value={ListingSortType.TopYear}>Year</option>
-          <option value={ListingSortType.TopAll}>All</option>
-        </select>
-        {!this.props.communityId && 
-          UserService.Instance.loggedIn &&
-          <select value={this.state.type_} onChange={linkEvent(this, this.handleTypeChange)} class="ml-2 custom-select w-auto">
-          <option disabled>Type</option>
-          <option value={ListingType.All}>All</option>
-          <option value={ListingType.Subscribed}>Subscribed</option>
-        </select>
+  outer(): Array<Post> {
+    let out = this.props.posts;
+    if (this.props.removeDuplicates) {
+      out = this.removeDuplicates(out);
+    }
 
-        }
-      </div>
-    )
+    if (this.props.sort !== undefined) {
+      postSort(out, this.props.sort, this.props.showCommunity == undefined);
+    }
 
+    return out;
   }
 
-  handleSortChange(i: PostListings, event) {
-    i.state.sortType = Number(event.target.value);
-    i.setState(i.state);
-
-    let getPostsForm: GetPostsForm = {
-      community_id: i.state.community.id,
-      limit: 10,
-      sort: ListingSortType[i.state.sortType],
-      type_: ListingType[ListingType.Community]
+  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);
+        }
+      }
     }
-    WebSocketService.Instance.getPosts(getPostsForm);
-  }
-
-  handleTypeChange(i: PostListings, event) {
-    i.state.type_ = Number(event.target.value);
-    i.setState(i.state);
 
-    let getPostsForm: GetPostsForm = {
-      limit: 10,
-      sort: ListingSortType[i.state.sortType],
-      type_: ListingType[i.state.type_]
+    // 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));
+      }
     }
-    WebSocketService.Instance.getPosts(getPostsForm);
-  }
 
-  parseMessage(msg: any) {
-    console.log(msg);
-    let op: UserOperation = msgOp(msg);
-    if (msg.error) {
-      alert(msg.error);
-      return;
-    } else if (op == UserOperation.GetPosts) {
-      let res: GetPostsResponse = msg;
-      this.state.posts = res.posts;
-      this.setState(this.state);
-    } else if (op == UserOperation.CreatePostLike) {
-      let res: CreatePostLikeResponse = msg;
-      let found = this.state.posts.find(c => c.id == res.post.id);
-      found.my_vote = res.post.my_vote;
-      found.score = res.post.score;
-      found.upvotes = res.post.upvotes;
-      found.downvotes = res.post.downvotes;
-      this.setState(this.state);
+    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;
   }
 }
-
-