From 44c443173efd054592aab9c3545b3121349e95e7 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Sat, 21 Aug 2021 13:52:17 -0400 Subject: [PATCH] Integrating resolve_user into search. --- package.json | 2 +- src/shared/components/search.tsx | 220 ++++++++++++++++++++++++------- yarn.lock | 8 +- 3 files changed, 181 insertions(+), 49 deletions(-) diff --git a/package.json b/package.json index 7d3ef6d4..9f3fb633 100644 --- a/package.json +++ b/package.json @@ -69,7 +69,7 @@ "husky": "^7.0.1", "import-sort-style-module": "^6.0.0", "iso-639-1": "^2.1.9", - "lemmy-js-client": "0.11.4-rc.14", + "lemmy-js-client": "0.11.4-rc.15", "lint-staged": "^11.0.1", "mini-css-extract-plugin": "^2.1.0", "node-fetch": "^2.6.1", diff --git a/src/shared/components/search.tsx b/src/shared/components/search.tsx index 61c3000e..4a1c8521 100644 --- a/src/shared/components/search.tsx +++ b/src/shared/components/search.tsx @@ -11,6 +11,8 @@ import { PersonViewSafe, PostResponse, PostView, + ResolveObject, + ResolveObjectResponse, Search as SearchForm, SearchResponse, SearchType, @@ -91,6 +93,7 @@ interface SearchState { loading: boolean; site: Site; searchText: string; + resolveObjectResponse: ResolveObjectResponse; } interface UrlParams { @@ -103,6 +106,12 @@ interface UrlParams { page?: number; } +interface Combined { + type_: string; + data: CommentView | PostView | CommunityView | PersonViewSafe; + published: string; +} + export class Search extends Component { private isoData = setIsoData(this.context); private communityChoices: any; @@ -128,6 +137,12 @@ export class Search extends Component { communities: [], users: [], }, + resolveObjectResponse: { + comment: null, + post: null, + community: null, + person: null, + }, loading: true, site: this.isoData.site_res.site_view.site, communities: [], @@ -187,6 +202,7 @@ export class Search extends Component { } if (this.state.q != "") { this.state.searchResponse = this.isoData.routeData[2]; + this.state.resolveObjectResponse = this.isoData.routeData[3]; this.state.loading = false; } else { this.search(); @@ -283,8 +299,14 @@ export class Search extends Component { } setOptionalAuth(form, req.auth); + let resolveObjectForm: ResolveObject = { + q: this.getSearchQueryFromProps(pathSplit[3]), + }; + setOptionalAuth(resolveObjectForm, req.auth); + if (form.q != "") { promises.push(req.client.search(form)); + promises.push(req.client.resolveObject(resolveObjectForm)); } return promises; @@ -402,33 +424,78 @@ export class Search extends Component { ); } - all() { - let combined: { - type_: string; - data: CommentView | PostView | CommunityView | PersonViewSafe; - published: string; - }[] = []; - let comments = this.state.searchResponse.comments.map(e => { - return { type_: "comments", data: e, published: e.comment.published }; - }); - let posts = this.state.searchResponse.posts.map(e => { - return { type_: "posts", data: e, published: e.post.published }; - }); - let communities = this.state.searchResponse.communities.map(e => { - return { - type_: "communities", - data: e, - published: e.community.published, - }; - }); - let users = this.state.searchResponse.users.map(e => { - return { type_: "users", data: e, published: e.person.published }; - }); + postViewToCombined(postView: PostView): Combined { + return { + type_: "posts", + data: postView, + published: postView.post.published, + }; + } - combined.push(...comments); - combined.push(...posts); - combined.push(...communities); - combined.push(...users); + commentViewToCombined(commentView: CommentView): Combined { + return { + type_: "comments", + data: commentView, + published: commentView.comment.published, + }; + } + + communityViewToCombined(communityView: CommunityView): Combined { + return { + type_: "communities", + data: communityView, + published: communityView.community.published, + }; + } + + personViewSafeToCombined(personViewSafe: PersonViewSafe): Combined { + return { + type_: "users", + data: personViewSafe, + published: personViewSafe.person.published, + }; + } + + buildCombined(): Combined[] { + let combined: Combined[] = []; + + // Push the possible resolve / federated objects first + let resolveComment = this.state.resolveObjectResponse.comment; + if (resolveComment) { + combined.push(this.commentViewToCombined(resolveComment)); + } + let resolvePost = this.state.resolveObjectResponse.post; + if (resolvePost) { + combined.push(this.postViewToCombined(resolvePost)); + } + let resolveCommunity = this.state.resolveObjectResponse.community; + if (resolveCommunity) { + combined.push(this.communityViewToCombined(resolveCommunity)); + } + let resolveUser = this.state.resolveObjectResponse.person; + if (resolveUser) { + combined.push(this.personViewSafeToCombined(resolveUser)); + } + + // Push the search results + combined.push( + ...this.state.searchResponse.comments.map(e => + this.commentViewToCombined(e) + ) + ); + combined.push( + ...this.state.searchResponse.posts.map(e => this.postViewToCombined(e)) + ); + combined.push( + ...this.state.searchResponse.communities.map(e => + this.communityViewToCombined(e) + ) + ); + combined.push( + ...this.state.searchResponse.users.map(e => + this.personViewSafeToCombined(e) + ) + ); // Sort it if (this.state.sort == SortType.New) { @@ -444,7 +511,11 @@ export class Search extends Component { (a.data as PersonViewSafe).counts.comment_score) ); } + return combined; + } + all() { + let combined = this.buildCombined(); return (
{combined.map(i => ( @@ -482,9 +553,18 @@ export class Search extends Component { } comments() { + let comments: CommentView[] = []; + + let resolveComment = this.state.resolveObjectResponse.comment; + if (resolveComment) { + comments.push(resolveComment); + } + + comments.push(...this.state.searchResponse.comments); + return ( { } posts() { + let posts: PostView[] = []; + + let resolvePost = this.state.resolveObjectResponse.post; + if (resolvePost) { + posts.push(resolvePost); + } + + posts.push(...this.state.searchResponse.posts); + return ( <> - {this.state.searchResponse.posts.map(post => ( + {posts.map(post => (
{ } communities() { + let communities: CommunityView[] = []; + + let resolveCommunity = this.state.resolveObjectResponse.community; + if (resolveCommunity) { + communities.push(resolveCommunity); + } + + communities.push(...this.state.searchResponse.communities); return ( <> - {this.state.searchResponse.communities.map(community => ( + {communities.map(community => (
{this.communityListing(community)}
@@ -523,6 +620,26 @@ export class Search extends Component { ); } + users() { + let users: PersonViewSafe[] = []; + + let resolveUser = this.state.resolveObjectResponse.person; + if (resolveUser) { + users.push(resolveUser); + } + + users.push(...this.state.searchResponse.users); + return ( + <> + {users.map(user => ( +
+
{this.userListing(user)}
+
+ ))} + + ); + } + communityListing(community_view: CommunityView) { return ( <> @@ -549,18 +666,6 @@ export class Search extends Component { ]; } - users() { - return ( - <> - {this.state.searchResponse.users.map(user => ( -
-
{this.userListing(user)}
-
- ))} - - ); - } - communityFilter() { return (
@@ -609,11 +714,17 @@ export class Search extends Component { resultsCount(): number { let res = this.state.searchResponse; + let resObj = this.state.resolveObjectResponse; + let resObjCount = + resObj.post || resObj.person || resObj.community || resObj.comment + ? 1 + : 0; return ( res.posts.length + res.comments.length + res.communities.length + - res.users.length + res.users.length + + resObjCount ); } @@ -638,8 +749,14 @@ export class Search extends Component { form.creator_id = this.state.creatorId; } + let resolveObjectForm: ResolveObject = { + q: this.state.q, + auth: authField(false), + }; + if (this.state.q != "") { WebSocketService.Instance.send(wsClient.search(form)); + WebSocketService.Instance.send(wsClient.resolveObject(resolveObjectForm)); } } @@ -769,8 +886,19 @@ export class Search extends Component { console.log(msg); let op = wsUserOp(msg); if (msg.error) { - toast(i18n.t(msg.error), "danger"); - return; + if (msg.error != "couldnt_find_object") { + toast(i18n.t(msg.error), "danger"); + return; + } else { + this.setState({ + resolveObjectResponse: { + comment: null, + community: null, + person: null, + post: null, + }, + }); + } } else if (op == UserOperation.Search) { let data = wsJsonToRes(msg).data; this.state.searchResponse = data; @@ -794,6 +922,10 @@ export class Search extends Component { this.state.communities = data.communities; this.setState(this.state); this.setupCommunityFilter(); + } else if (op == UserOperation.ResolveObject) { + let data = wsJsonToRes(msg).data; + this.state.resolveObjectResponse = data; + this.setState(this.state); } } } diff --git a/yarn.lock b/yarn.lock index 92d963ed..d529d7be 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4664,10 +4664,10 @@ lcid@^1.0.0: dependencies: invert-kv "^1.0.0" -lemmy-js-client@0.11.4-rc.14: - version "0.11.4-rc.14" - resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.11.4-rc.14.tgz#dcac5b8dc78c3b04e6b3630ff9351a94aa73e109" - integrity sha512-R8M+myyriNQljQlTweVqtUKGBpgmaM7RI4ebYb7N7sYr5Bk5Ip6v2qTNvKAV6BlsDOCTWANOonfeoz/cIerLEg== +lemmy-js-client@0.11.4-rc.15: + version "0.11.4-rc.15" + resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.11.4-rc.15.tgz#aefe7cf9de4fcafba6534763929e51cd8fe6da54" + integrity sha512-PaXwEitQpo88hrTNdr9kwSljZMVAkNsZQQsaXfwwSSRa+0+eJXBNaU3ZjdjhT90LEAhw1Yr0dU2/zmbXg5vx0w== levn@^0.4.1: version "0.4.1"