Skip to content

Commit

Permalink
Implement server side search (NEAR-DevHub#298)
Browse files Browse the repository at this point in the history
* Implement server side search

* case insensitive search on content

* add search test
  • Loading branch information
ailisp authored and elliotBraem committed Oct 17, 2023
1 parent 00f7737 commit d29232f
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 796 deletions.
20 changes: 20 additions & 0 deletions playwright-tests/search.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { test } from "@playwright/test";

test("should show post history for posts in the feed", async ({ page }) => {
await page.goto("/devgovgigs.near/widget/gigs-board.pages.Feed");

// Fill the search by content by to
const searchInputSelector = 'input.form-control[type="search"]';
let searchInput = await page.waitForSelector(searchInputSelector, {
state: "visible",
});
await searchInput.fill("zero knowledge");

const searchButton = await page.getByRole("button", { name: "Search" });
await searchButton.click();

await page.waitForSelector('span:has-text("zero knowledge")', {
state: "visible",
timeout: 10000,
});
});
115 changes: 2 additions & 113 deletions src/gigs-board/entity/post/List.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ function defaultRenderItem(postId, additionalProps) {
const renderItem = props.renderItem ?? defaultRenderItem;

const cachedRenderItem = (item, i) => {
if (props.searchResult && props.searchResult.keywords[item]) {
if (props.searchResult && props.searchResult.keywords) {
return renderItem(item, {
searchKeywords: props.searchResult.keywords[item],
searchKeywords: props.searchResult.keywords,
});
}

Expand All @@ -144,61 +144,6 @@ const cachedRenderItem = (item, i) => {
const initialRenderLimit = props.initialRenderLimit ?? 3;
const addDisplayCount = props.nextLimit ?? initialRenderLimit;

function getPostsByLabel() {
let postIds = Near.view(
nearDevGovGigsContractAccountId,
"get_posts_by_label",
{
label: props.tag,
}
);
if (postIds) {
postIds.reverse();
}
return postIds;
}

function getPostsByAuthor() {
let postIds = Near.view(
nearDevGovGigsContractAccountId,
"get_posts_by_author",
{
author: props.author,
}
);
if (postIds) {
postIds.reverse();
}
return postIds;
}

function intersectPostsWithLabel(postIds) {
if (props.tag) {
let postIdLabels = getPostsByLabel();
if (postIdLabels === null) {
// wait until postIdLabels are loaded
return null;
}
postIdLabels = new Set(postIdLabels);
return postIds.filter((id) => postIdLabels.has(id));
}
return postIds;
}

function intersectPostsWithAuthor(postIds) {
if (props.author) {
let postIdsByAuthor = getPostsByAuthor();
if (postIdsByAuthor == null) {
// wait until postIdsByAuthor are loaded
return null;
} else {
postIdsByAuthor = new Set(postIdsByAuthor);
return postIds.filter((id) => postIdsByAuthor.has(id));
}
}
return postIds;
}

const ONE_DAY = 60 * 60 * 24 * 1000;
const ONE_WEEK = 60 * 60 * 24 * 1000 * 7;
const ONE_MONTH = 60 * 60 * 24 * 1000 * 30;
Expand All @@ -225,72 +170,16 @@ const getPeriodText = (period) => {
return text;
};

const findHottestsPosts = (postIds, period) => {
let allPosts;
if (!state.allPosts) {
allPosts = Near.view("devgovgigs.near", "get_posts");
if (!allPosts) {
return [];
}
State.update({ allPosts });
} else {
allPosts = state.allPosts;
}
let postIdsSet = new Set(postIds);
let posts = allPosts.filter((post) => postIdsSet.has(post.id));

let periodTime = ONE_DAY;
if (period === "week") {
periodTime = ONE_WEEK;
}
if (period === "month") {
periodTime = ONE_MONTH;
}
const periodLimitedPosts = posts.filter((post) => {
const timestamp = post.snapshot.timestamp / 1000000;
return Date.now() - timestamp < periodTime;
});
const modifiedPosts = periodLimitedPosts.map((post) => {
const comments =
Near.view("devgovgigs.near", "get_children_ids", {
post_id: post.id,
}) || [];
post = { ...post, comments };
return {
...post,
postScore: getHotnessScore(post),
};
});
modifiedPosts.sort((a, b) => b.postScore - a.postScore);
return modifiedPosts.map((post) => post.id);
};

let postIds;
if (props.searchResult) {
postIds = props.searchResult.postIds;
postIds = intersectPostsWithLabel(postIds);
postIds = intersectPostsWithAuthor(postIds);
} else if (props.tag) {
postIds = getPostsByLabel();
postIds = intersectPostsWithAuthor(postIds);
} else if (props.author) {
postIds = getPostsByAuthor();
} else if (props.recency == "all") {
postIds = Near.view(nearDevGovGigsContractAccountId, "get_all_post_ids");
if (postIds) {
postIds.reverse();
}
} else {
postIds = Near.view(nearDevGovGigsContractAccountId, "get_children_ids");
if (postIds) {
postIds.reverse();
}
}

if (props.recency == "hot") {
postIds = findHottestsPosts(postIds, state.period);
}

const loader = (
<div className="loader" key={"loader"}>
<span
Expand Down
Loading

0 comments on commit d29232f

Please sign in to comment.