From b7e73a5559d05a8d06ef2e01f7382a90bb50f44f Mon Sep 17 00:00:00 2001 From: Dessalines Date: Wed, 21 Aug 2019 22:17:15 -0700 Subject: [PATCH 1/3] View where a URL has been cross-posted to in the past - This shows when creating a post, or when viewing a post. - Fixes #131 --- server/src/api/post.rs | 1 + server/src/api/site.rs | 20 ++++++++++++ server/src/api/user.rs | 2 ++ server/src/db/mod.rs | 2 +- server/src/db/post_view.rs | 56 +++++++++++++++++++-------------- server/src/websocket/server.rs | 1 + ui/src/components/post-form.tsx | 29 ++++++++++++++++- ui/src/components/post.tsx | 29 +++++++++++++++-- ui/src/components/search.tsx | 1 + ui/src/interfaces.ts | 3 +- ui/src/translations/en.ts | 1 + 11 files changed, 117 insertions(+), 28 deletions(-) diff --git a/server/src/api/post.rs b/server/src/api/post.rs index 35363a171..c5985f467 100644 --- a/server/src/api/post.rs +++ b/server/src/api/post.rs @@ -254,6 +254,7 @@ impl Perform for Oper { data.community_id, None, None, + None, user_id, show_nsfw, false, diff --git a/server/src/api/site.rs b/server/src/api/site.rs index 8f094aace..c98539bee 100644 --- a/server/src/api/site.rs +++ b/server/src/api/site.rs @@ -23,6 +23,7 @@ pub struct Search { #[derive(Serialize, Deserialize)] pub struct SearchResponse { op: String, + type_: String, comments: Vec, posts: Vec, communities: Vec, @@ -288,6 +289,7 @@ impl Perform for Oper { data.community_id, None, Some(data.q.to_owned()), + None, None, true, false, @@ -333,6 +335,7 @@ impl Perform for Oper { data.community_id, None, Some(data.q.to_owned()), + None, None, true, false, @@ -363,6 +366,22 @@ impl Perform for Oper { Some(data.q.to_owned()), data.page, data.limit)?; + }, + SearchType::Url => { + posts = PostView::list( + &conn, + PostListingType::All, + &sort, + data.community_id, + None, + None, + Some(data.q.to_owned()), + None, + true, + false, + false, + data.page, + data.limit)?; } }; @@ -371,6 +390,7 @@ impl Perform for Oper { Ok( SearchResponse { op: self.op.to_string(), + type_: data.type_.to_owned(), comments: comments, posts: posts, communities: communities, diff --git a/server/src/api/user.rs b/server/src/api/user.rs index 672eca562..425cc1cbd 100644 --- a/server/src/api/user.rs +++ b/server/src/api/user.rs @@ -318,6 +318,7 @@ impl Perform for Oper { data.community_id, None, None, + None, Some(user_details_id), show_nsfw, data.saved_only, @@ -332,6 +333,7 @@ impl Perform for Oper { data.community_id, Some(user_details_id), None, + None, user_id, show_nsfw, data.saved_only, diff --git a/server/src/db/mod.rs b/server/src/db/mod.rs index 9f0c79b8f..3de0abb46 100644 --- a/server/src/db/mod.rs +++ b/server/src/db/mod.rs @@ -67,7 +67,7 @@ pub enum SortType { #[derive(EnumString,ToString,Debug, Serialize, Deserialize)] pub enum SearchType { - All, Comments, Posts, Communities, Users + All, Comments, Posts, Communities, Users, Url } pub fn fuzzy_search(q: &str) -> String { diff --git a/server/src/db/post_view.rs b/server/src/db/post_view.rs index c7e6eea3e..c9d8cff7b 100644 --- a/server/src/db/post_view.rs +++ b/server/src/db/post_view.rs @@ -79,6 +79,7 @@ impl PostView { for_community_id: Option, for_creator_id: Option, search_term: Option, + url_search: Option, my_user_id: Option, show_nsfw: bool, saved_only: bool, @@ -104,6 +105,10 @@ impl PostView { query = query.filter(name.ilike(fuzzy_search(&search_term))); }; + if let Some(url_search) = url_search { + query = query.filter(url.eq(url_search)); + }; + // TODO these are wrong, bc they'll only show saved for your logged in user, not theirs if saved_only { query = query.filter(saved.eq(true)); @@ -326,29 +331,34 @@ mod tests { }; - let read_post_listings_with_user = PostView::list(&conn, - PostListingType::Community, - &SortType::New, Some(inserted_community.id), - None, - None, - Some(inserted_user.id), - false, - false, - false, - None, - None).unwrap(); - let read_post_listings_no_user = PostView::list(&conn, - PostListingType::Community, - &SortType::New, - Some(inserted_community.id), - None, - None, - None, - false, - false, - false, - None, - None).unwrap(); + let read_post_listings_with_user = PostView::list( + &conn, + PostListingType::Community, + &SortType::New, + Some(inserted_community.id), + None, + None, + None, + Some(inserted_user.id), + false, + false, + false, + None, + None).unwrap(); + let read_post_listings_no_user = PostView::list( + &conn, + PostListingType::Community, + &SortType::New, + Some(inserted_community.id), + None, + None, + None, + None, + false, + false, + false, + None, + None).unwrap(); let read_post_listing_no_user = PostView::read(&conn, inserted_post.id, None).unwrap(); let read_post_listing_with_user = PostView::read(&conn, inserted_post.id, Some(inserted_user.id)).unwrap(); diff --git a/server/src/websocket/server.rs b/server/src/websocket/server.rs index 64f94f4cd..c0dee2679 100644 --- a/server/src/websocket/server.rs +++ b/server/src/websocket/server.rs @@ -142,6 +142,7 @@ impl ChatServer { None, None, None, + None, false, false, false, diff --git a/ui/src/components/post-form.tsx b/ui/src/components/post-form.tsx index 42620c9fe..d21b2fb4c 100644 --- a/ui/src/components/post-form.tsx +++ b/ui/src/components/post-form.tsx @@ -23,6 +23,7 @@ interface PostFormState { loading: boolean; suggestedTitle: string; suggestedPosts: Array; + crossPosts: Array; } export class PostForm extends Component { @@ -40,6 +41,7 @@ export class PostForm extends Component { loading: false, suggestedTitle: undefined, suggestedPosts: [], + crossPosts: [], } constructor(props: any, context: any) { @@ -95,6 +97,12 @@ export class PostForm extends Component { {this.state.suggestedTitle &&
#
} + {this.state.crossPosts.length > 0 && + <> +
#
+ + + }
@@ -170,13 +178,27 @@ export class PostForm extends Component { handlePostUrlChange(i: PostForm, event: any) { i.state.postForm.url = event.target.value; if (validURL(i.state.postForm.url)) { + + let form: SearchForm = { + q: i.state.postForm.url, + type_: SearchType[SearchType.Url], + sort: SortType[SortType.TopAll], + page: 1, + limit: 6, + }; + + WebSocketService.Instance.search(form); + + // Fetch the page title getPageTitle(i.state.postForm.url).then(d => { i.state.suggestedTitle = d; i.setState(i.state); }); } else { i.state.suggestedTitle = undefined; + i.state.crossPosts = []; } + i.setState(i.state); } @@ -248,7 +270,12 @@ export class PostForm extends Component { this.props.onEdit(res.post); } else if (op == UserOperation.Search) { let res: SearchResponse = msg; - this.state.suggestedPosts = res.posts; + + if (res.type_ == SearchType[SearchType.Posts]) { + this.state.suggestedPosts = res.posts; + } else if (res.type_ == SearchType[SearchType.Url]) { + this.state.crossPosts = res.posts; + } this.setState(this.state); } } diff --git a/ui/src/components/post.tsx b/ui/src/components/post.tsx index b0204d388..97a9cd722 100644 --- a/ui/src/components/post.tsx +++ b/ui/src/components/post.tsx @@ -1,10 +1,11 @@ import { Component, linkEvent } from 'inferno'; import { Subscription } from "rxjs"; import { retryWhen, delay, take } from 'rxjs/operators'; -import { UserOperation, Community, Post as PostI, GetPostResponse, PostResponse, Comment, CommentForm as CommentFormI, CommentResponse, CommentSortType, CreatePostLikeResponse, CommunityUser, CommunityResponse, CommentNode as CommentNodeI, BanFromCommunityResponse, BanUserResponse, AddModToCommunityResponse, AddAdminResponse, UserView } from '../interfaces'; +import { UserOperation, Community, Post as PostI, GetPostResponse, PostResponse, Comment, CommentForm as CommentFormI, CommentResponse, CommentSortType, CreatePostLikeResponse, CommunityUser, CommunityResponse, CommentNode as CommentNodeI, BanFromCommunityResponse, BanUserResponse, AddModToCommunityResponse, AddAdminResponse, UserView, SearchType, SortType, SearchForm, SearchResponse } from '../interfaces'; import { WebSocketService, UserService } from '../services'; import { msgOp, hotRank } from '../utils'; import { PostListing } from './post-listing'; +import { PostListings } from './post-listings'; import { Sidebar } from './sidebar'; import { CommentForm } from './comment-form'; import { CommentNodes } from './comment-nodes'; @@ -22,6 +23,7 @@ interface PostState { scrolled?: boolean; scrolled_comment_id?: number; loading: boolean; + crossPosts: Array; } export class Post extends Component { @@ -35,7 +37,8 @@ export class Post extends Component { moderators: [], admins: [], scrolled: false, - loading: true + loading: true, + crossPosts: [], } constructor(props: any, context: any) { @@ -112,6 +115,12 @@ export class Post extends Component { moderators={this.state.moderators} admins={this.state.admins} /> + {this.state.crossPosts.length > 0 && + <> +
#
+ + + }
{this.sortRadios()} @@ -256,6 +265,18 @@ export class Post extends Component { this.state.admins = res.admins; this.state.loading = false; document.title = `${this.state.post.name} - ${WebSocketService.Instance.site.name}`; + + // Get cross-posts + let form: SearchForm = { + q: res.post.url, + type_: SearchType[SearchType.Url], + sort: SortType[SortType.TopAll], + page: 1, + limit: 6, + }; + + WebSocketService.Instance.search(form); + this.setState(this.state); } else if (op == UserOperation.CreateComment) { let res: CommentResponse = msg; @@ -332,6 +353,10 @@ export class Post extends Component { let res: AddAdminResponse = msg; this.state.admins = res.admins; this.setState(this.state); + } else if (op == UserOperation.Search) { + let res: SearchResponse = msg; + this.state.crossPosts = res.posts.filter(p => p.id != this.state.post.id); + this.setState(this.state); } } diff --git a/ui/src/components/search.tsx b/ui/src/components/search.tsx index 0f8727cb8..34a4a3d31 100644 --- a/ui/src/components/search.tsx +++ b/ui/src/components/search.tsx @@ -29,6 +29,7 @@ export class Search extends Component { page: 1, searchResponse: { op: null, + type_: null, posts: [], comments: [], communities: [], diff --git a/ui/src/interfaces.ts b/ui/src/interfaces.ts index ebd42340d..91d89783d 100644 --- a/ui/src/interfaces.ts +++ b/ui/src/interfaces.ts @@ -15,7 +15,7 @@ export enum SortType { } export enum SearchType { - All, Comments, Posts, Communities, Users + All, Comments, Posts, Communities, Users, Url } export interface User { @@ -551,6 +551,7 @@ export interface SearchForm { export interface SearchResponse { op: string; + type_: string; posts?: Array; comments?: Array; communities: Array; diff --git a/ui/src/translations/en.ts b/ui/src/translations/en.ts index b5c066729..90497ada5 100644 --- a/ui/src/translations/en.ts +++ b/ui/src/translations/en.ts @@ -8,6 +8,7 @@ export const en = { number_of_posts:'{{count}} Posts', posts: 'Posts', related_posts: 'These posts might be related', + cross_posts: 'This link has also been posted to:', comments: 'Comments', number_of_comments:'{{count}} Comments', remove_comment: 'Remove Comment', From 954c89edaf1bcc5dc46c3b593bf8b065f0db74a9 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Thu, 22 Aug 2019 11:46:54 -0700 Subject: [PATCH 2/3] cross-posting almost done. --- ui/src/components/post-listing.tsx | 14 ++++++++++++++ ui/src/translations/en.ts | 1 + 2 files changed, 15 insertions(+) diff --git a/ui/src/components/post-listing.tsx b/ui/src/components/post-listing.tsx index bb6f2cece..f67a0fc70 100644 --- a/ui/src/components/post-listing.tsx +++ b/ui/src/components/post-listing.tsx @@ -150,6 +150,9 @@ export class PostListing extends Component {
  • {post.saved ? i18n.t('unsave') : i18n.t('save')}
  • +
  • + # +
  • {this.myPost && <>
  • @@ -270,6 +273,17 @@ export class PostListing extends Component { WebSocketService.Instance.savePost(form); } + handleCrossPostClick(i: PostListing) { + let params = `?name=${i.props.post.name}`; + if (i.props.post.url) { + params += `&url=${i.props.post.url}`; + } + if (i.props.post.body) { + params += `&body=${i.props.post.body}`; + } + i.context.router.history.push(`/create_post${params}`); + } + handleModRemoveShow(i: PostListing) { i.state.showRemoveDialog = true; i.setState(i.state); diff --git a/ui/src/translations/en.ts b/ui/src/translations/en.ts index 90497ada5..94e06d02d 100644 --- a/ui/src/translations/en.ts +++ b/ui/src/translations/en.ts @@ -9,6 +9,7 @@ export const en = { posts: 'Posts', related_posts: 'These posts might be related', cross_posts: 'This link has also been posted to:', + cross_post: 'cross-post', comments: 'Comments', number_of_comments:'{{count}} Comments', remove_comment: 'Remove Comment', From 20fec100b5c0d99f71e7e38c219f39686b990938 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Thu, 22 Aug 2019 16:13:26 -0700 Subject: [PATCH 3/3] Cross posting working. --- ui/src/components/create-post.tsx | 15 +++++++++++- ui/src/components/post-form.tsx | 37 +++++++++++++++++++----------- ui/src/components/post-listing.tsx | 16 ++++++------- ui/src/components/post.tsx | 35 +++++++++++++++++++--------- ui/src/components/sidebar.tsx | 2 +- ui/src/index.tsx | 1 - ui/src/interfaces.ts | 7 ++++++ 7 files changed, 77 insertions(+), 36 deletions(-) diff --git a/ui/src/components/create-post.tsx b/ui/src/components/create-post.tsx index dd93a3c53..3e00bd80a 100644 --- a/ui/src/components/create-post.tsx +++ b/ui/src/components/create-post.tsx @@ -1,6 +1,7 @@ import { Component } from 'inferno'; import { PostForm } from './post-form'; import { WebSocketService } from '../services'; +import { PostFormParams } from '../interfaces'; import { i18n } from '../i18next'; import { T } from 'inferno-i18next'; @@ -21,13 +22,25 @@ export class CreatePost extends Component {
    #
    - +
  • ) } + get params(): PostFormParams { + let urlParams = new URLSearchParams(this.props.location.search); + let params: PostFormParams = { + name: urlParams.get("name"), + community: urlParams.get("community") || this.prevCommunityName, + body: urlParams.get("body"), + url: urlParams.get("url"), + }; + + return params; + } + get prevCommunityName(): string { if (this.props.match.params.name) { return this.props.match.params.name; diff --git a/ui/src/components/post-form.tsx b/ui/src/components/post-form.tsx index d21b2fb4c..f502e7f3e 100644 --- a/ui/src/components/post-form.tsx +++ b/ui/src/components/post-form.tsx @@ -2,7 +2,7 @@ import { Component, linkEvent } from 'inferno'; import { PostListings } from './post-listings'; import { Subscription } from "rxjs"; import { retryWhen, delay, take } from 'rxjs/operators'; -import { PostForm as PostFormI, Post, PostResponse, UserOperation, Community, ListCommunitiesResponse, ListCommunitiesForm, SortType, SearchForm, SearchType, SearchResponse } from '../interfaces'; +import { PostForm as PostFormI, PostFormParams, Post, PostResponse, UserOperation, Community, ListCommunitiesResponse, ListCommunitiesForm, SortType, SearchForm, SearchType, SearchResponse } from '../interfaces'; import { WebSocketService, UserService } from '../services'; import { msgOp, getPageTitle, debounce, validURL, capitalizeFirstLetter } from '../utils'; import * as autosize from 'autosize'; @@ -11,7 +11,7 @@ import { T } from 'inferno-i18next'; interface PostFormProps { post?: Post; // If a post is given, that means this is an edit - prevCommunityName?: string; + params?: PostFormParams; onCancel?(): any; onCreate?(id: number): any; onEdit?(post: Post): any; @@ -62,20 +62,30 @@ export class PostForm extends Component { } } + if (this.props.params) { + this.state.postForm.name = this.props.params.name; + if (this.props.params.url) { + this.state.postForm.url = this.props.params.url; + } + if (this.props.params.body) { + this.state.postForm.body = this.props.params.body; + } + } + this.subscription = WebSocketService.Instance.subject - .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10)))) - .subscribe( - (msg) => this.parseMessage(msg), + .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10)))) + .subscribe( + (msg) => this.parseMessage(msg), (err) => console.error(err), () => console.log('complete') - ); + ); - let listCommunitiesForm: ListCommunitiesForm = { - sort: SortType[SortType.TopAll], - limit: 9999, - } + let listCommunitiesForm: ListCommunitiesForm = { + sort: SortType[SortType.TopAll], + limit: 9999, + } - WebSocketService.Instance.listCommunities(listCommunitiesForm); + WebSocketService.Instance.listCommunities(listCommunitiesForm); } componentDidMount() { @@ -123,7 +133,6 @@ export class PostForm extends Component {