diff --git a/app/assets/javascripts/initializers/initScrolling.js.erb b/app/assets/javascripts/initializers/initScrolling.js.erb index b46144491..a018c760f 100644 --- a/app/assets/javascripts/initializers/initScrolling.js.erb +++ b/app/assets/javascripts/initializers/initScrolling.js.erb @@ -179,46 +179,40 @@ function fetchNextPodcastPage(el) { fetchNext(el, "/api/podcast_episodes", insertArticles) } -function paginate(tag, params, requiresApproval) { - const searchHash = { ...{ per_page: 15, page: nextPage }, ...JSON.parse(params) }; - +function paginate(tag, requiresApproval) { + var tagFilters = []; if (tag && tag.length > 0) { - searchHash.tag_names = searchHash.tag_names || []; - searchHash.tag_names.push(tag); + tagFilters.push(tag); + } + + var searchObj = { + hitsPerPage: 15, + page: nextPage, + attributesToHighlight: [], + tagFilters, + filters: (requiresApproval === 'true') ? 'approved:true' : '' } - searchHash.approved = (requiresApproval === 'true') ? 'true' : ''; var homeEl = document.getElementById("index-container"); + var paginationArticlesIndex; if (homeEl.dataset.feed === "base-feed") { - searchHash.class_name = "Article"; + paginationArticlesIndex = client.initIndex('ordered_articles_<%= Rails.env %>'); } else if (homeEl.dataset.feed === "latest") { - searchHash.class_name = "Article"; - searchHash.sort_by = "published_at"; + paginationArticlesIndex = client.initIndex('ordered_articles_by_published_at_<%= Rails.env %>'); } else { - searchHash.class_name = "Article"; - searchHash["published_at[gte]"] = homeEl.dataset.articlesSince; - searchHash.sort_by = "positive_reactions_count"; + var preamble = (requiresApproval === 'true') ? ' AND ' : '' + searchObj.filters += ( preamble +'published_at_int > ' + homeEl.dataset.articlesSince); + paginationArticlesIndex = client.initIndex('ordered_articles_by_positive_reactions_count_<%= Rails.env %>'); } - const searchParams = new URLSearchParams(searchHash).toString(); - - fetch(`/search/feed_content?${searchParams}`, { - method: 'GET', - headers: { - Accept: 'application/json', - 'X-CSRF-Token': window.csrfToken, - 'Content-Type': 'application/json', - }, - credentials: 'same-origin', - }) - .then(response => response.json()) - .then((content) => { + paginationArticlesIndex.search("*", searchObj) + .then(function searchDone(content) { nextPage += 1; - insertArticles(content.result); + insertArticles(content.hits); const checkBlockedContentEvent = new CustomEvent('checkBlockedContent') window.dispatchEvent(checkBlockedContentEvent); initializeReadingListIcons(); - if (content.result.length === 0) { + if (content.hits.length === 0) { document.getElementById("loading-articles").style.display = "none" done = true; } @@ -259,11 +253,7 @@ function fetchNextPageIfNearBottom() { } else { scrollableElemId = "articles-list"; fetchCallback = function fetch() { - paginate( - indexContainer.dataset.tag, - indexContainer.dataset.params, - indexContainer.dataset.requiresApproval - ); + paginate(indexContainer.dataset.tag, indexContainer.dataset.requiresApproval); }; } @@ -276,6 +266,9 @@ function fetchNextPageIfNearBottom() { } function checkIfNearBottomOfPage() { + var publicSearchKey = '<%= ALGOLIASEARCH_PUBLIC_SEARCH_ONLY_KEY %>'; + client = algoliasearch('<%= ApplicationConfig["ALGOLIASEARCH_APPLICATION_ID"] %>', publicSearchKey); + if (document.getElementsByClassName("single-article").length < 2 || window.location.search.indexOf("q=") > -1 ) { document.getElementById("loading-articles").style.display = "none"; done = true; diff --git a/app/assets/javascripts/utilities/buildArticleHTML.js.erb b/app/assets/javascripts/utilities/buildArticleHTML.js.erb index 1ccfacedf..55d8cf2e5 100644 --- a/app/assets/javascripts/utilities/buildArticleHTML.js.erb +++ b/app/assets/javascripts/utilities/buildArticleHTML.js.erb @@ -37,44 +37,42 @@ function buildArticleHTML(article) { if (article.class_name == "PodcastEpisode"){ flareTag = "podcast" } - if (article.class_name == "Comment"){ - flareTag = "comment" - } if (article.class_name == "User"){ flareTag = "person" } - var rc = article.positive_reactions_count + var rc = article.positive_reactions_count || article.reactions_count var reactionsCountHTML = '' if ((rc || '0') > 0 && article.class_name != "User") { var reactionsCountHTML = '
' } - var picUrl; - var profileUsername; - var userName; - if (article.class_name == "PodcastEpisode"){ - picUrl = article.main_image; - profileUsername = article.slug; - userName = article.title; - } else { - picUrl = article.user.profile_image_90; - profileUsername = article.user.username; - userName = article.user.name; - } + var picUrl = article.user.profile_image_90 + var profileUsername = article.user.username var orgHeadline = ""; if (article.organization && !document.getElementById("organization-article-index")) { orgHeadline = '' } var bodyTextSnippet = ""; + var commentsBlobSnippet = ""; var searchSnippetHTML = ""; - if (article.highlight && article.highlight.body_text.length > 0){ - var firstSnippetChar = article.highlight.body_text[0]; - var startingEllipsis = "" - if (firstSnippetChar.toLowerCase() != firstSnippetChar.toUpperCase()){ - startingEllipsis = "…" + if (article._snippetResult && article._snippetResult.body_text){ + if (article._snippetResult.body_text.matchLevel != "none"){ + var firstSnippetChar = article._snippetResult.body_text.value[0]; + var startingEllipsis = "" + if (firstSnippetChar.toLowerCase() != firstSnippetChar.toUpperCase()){ + startingEllipsis = "…" + } + bodyTextSnippet = startingEllipsis+article._snippetResult.body_text.value + "…" } - bodyTextSnippet = startingEllipsis + article.highlight.body_text.join("...") + "…"; - if (bodyTextSnippet.length > 0){ - searchSnippetHTML = '