docbrown/app/assets/javascripts/initializers/initializeFetchFollowedArticles.js.erb
Ben Halpern eeb408511a
Add extra caching to home query (#643)
* Update home feed logic and display

* Remove console log

* Add extra caching to home query

* Fix conflict
2018-09-12 16:34:10 -04:00

83 lines
3.1 KiB
Text

function initializeFetchFollowedArticles() {
if ( document.getElementById("featured-story-marker") && checkUserLoggedIn() ) {
algoliaFollowedArticles();
}
}
function insertArticle(article,parent,insertPlace) {
if (article){
var newItem = document.createElement("DIV");
newItem.innerHTML = buildArticleHTML(article)
parent.insertBefore(newItem, insertPlace);
initializeReadingListIcons();
}
}
function insertInitialArticles(user) {
var el = document.getElementById("home-articles-object")
if (!el) {return}
var articlesJSON = JSON.parse(el.dataset.articles)
el.outerHTML = "";
var insertPlace = document.getElementById("article-index-hidden-div");
if (insertPlace) {
var parent = insertPlace.parentNode;
articlesJSON.forEach(function(article){
var containsTag = findOne(user.followed_tag_names, article.cached_tag_list_array)
var containsUserID = findOne([article.user_id], user.followed_user_ids)
if ( (containsTag || containsUserID) && !document.getElementById("article-link-"+article.id) ) {
insertArticle(article,parent,insertPlace);
}
});
}
}
function algoliaFollowedArticles(){
var user = userData();
if (user && user.followed_tag_names && user.followed_tag_names.length > 0) {
insertInitialArticles(user)
var followedUsersArray = [];
if (user.followed_user_ids){
followedUsersArray = user.followed_user_ids.map(function(id){return 'user_'+id});
}
var publicSearchKey = '<%= ALGOLIASEARCH_PUBLIC_SEARCH_ONLY_KEY %>'
var client = algoliasearch('<%= ApplicationConfig["ALGOLIASEARCH_APPLICATION_ID"] %>', publicSearchKey);
var index = client.initIndex('ordered_articles_<%= Rails.env %>');
var searchObj = {
hitsPerPage: 20,
page: "0",
attributesToHighlight: [],
tagFilters: [user.followed_tag_names.concat(followedUsersArray)],
}
var homeEl = document.getElementById("index-container");
if (homeEl.dataset.feed === "base-feed") {
articlesIndex = client.initIndex('ordered_articles_<%= Rails.env %>');
} else if (homeEl.dataset.feed === "latest") {
articlesIndex = client.initIndex('ordered_articles_by_published_at_<%= Rails.env %>');
} else {
searchObj["filters"] = ('published_at_int > ' + homeEl.dataset.articlesSince);
articlesIndex = client.initIndex('ordered_articles_by_positive_reactions_count_<%= Rails.env %>');
}
articlesIndex.search("*", searchObj)
.then(function searchDone(content) {
var insertPlace = document.getElementById("article-index-hidden-div");
if (insertPlace) {
var parent = insertPlace.parentNode;
content.hits.forEach(function(article){
if ( !document.getElementById("article-link-"+article.id) ) {
insertArticle(article,parent,insertPlace);
}
});
}
var el = document.getElementById("home-articles-object")
if (el){el.outerHTML = ''}
});
}
}
function findOne(haystack, arr) {
return arr.some(function (v) {
return haystack.indexOf(v) >= 0;
});
};