]> 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 5fbb6cbf861b998797c9f378f6da4109e62d3c02..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, Post, GetPostsForm, SortType, ListingType, GetPostsResponse, CreatePostLikeResponse, CommunityUser} from '../interfaces';
-import { WebSocketService, UserService } from '../services';
+import { Post, SortType } from 'lemmy-js-client';
+import { postSort } from '../utils';
 import { PostListing } from './post-listing';
-import { msgOp, fetchLimit } 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: SortType;
-  type_: ListingType;
-  page: number;
-  loading: boolean;
+  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: SortType.Hot,
-    type_: this.props.communityId 
-    ? ListingType.Community 
-    : UserService.Instance.user
-    ? ListingType.Subscribed 
-    : ListingType.All,
-    page: 1,
-    loading: true
-  }
-
+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')
-      );
-
-      this.refetch();
-  }
-
-  componentWillUnmount() {
-    this.subscription.unsubscribe();
   }
 
   render() {
     return (
       <div>
-        {this.state.loading ? 
-        <h4><svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg></h4> : 
-        <div>
-          {this.selects()}
-          {this.state.posts.length > 0 
-            ? this.state.posts.map(post => 
-              <PostListing post={post} showCommunity={!this.props.communityId}/>) 
-                : <div>No Listings. {!this.props.communityId && <span>Subscribe to some <Link to="/communities">forums</Link>.</span>}</div>
-          }
-          {this.paginator()}
-        </div>
-        }
-      </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={SortType.Hot}>Hot</option>
-          <option value={SortType.New}>New</option>
-          <option disabled>──────────</option>
-          <option value={SortType.TopDay}>Top Day</option>
-          <option value={SortType.TopWeek}>Week</option>
-          <option value={SortType.TopMonth}>Month</option>
-          <option value={SortType.TopYear}>Year</option>
-          <option value={SortType.TopAll}>All</option>
-        </select>
-        {!this.props.communityId && 
-          UserService.Instance.user &&
-            <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>
-
-        }
-      </div>
-    )
-  }
-
-  paginator() {
-    return (
-      <div class="mt-2">
-        {this.state.page > 1 && 
-          <button class="btn btn-sm btn-secondary mr-1" onClick={linkEvent(this, this.prevPage)}>Prev</button>
-        }
-        <button class="btn btn-sm btn-secondary" onClick={linkEvent(this, this.nextPage)}>Next</button>
+        {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>
     );
   }
 
-  nextPage(i: PostListings) { 
-    i.state.page++;
-    i.setState(i.state);
-    i.refetch();
-  }
+  outer(): Array<Post> {
+    let out = this.props.posts;
+    if (this.props.removeDuplicates) {
+      out = this.removeDuplicates(out);
+    }
 
-  prevPage(i: PostListings) { 
-    i.state.page--;
-    i.setState(i.state);
-    i.refetch();
-  }
+    if (this.props.sort !== undefined) {
+      postSort(out, this.props.sort, this.props.showCommunity == undefined);
+    }
 
-  handleSortChange(i: PostListings, event: any) {
-    i.state.sortType = Number(event.target.value);
-    i.state.page = 1;
-    i.setState(i.state);
-    i.refetch();
+    return out;
   }
 
-  refetch() {
-    let getPostsForm: GetPostsForm = {
-      community_id: this.state.community.id,
-      page: this.state.page,
-      limit: fetchLimit,
-      sort: SortType[this.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: any) {
-    i.state.type_ = Number(event.target.value);
-    i.state.page = 1;
-    i.setState(i.state);
-    i.refetch();
-  }
+    // 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));
+      }
+    }
 
-  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.state.loading = false;
-      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;
   }
 }
-
-