function initializeFetchFollowedArticles() {
insertPodcasts();
if ( document.getElementById("featured-story-marker") && checkUserLoggedIn() ) {
algoliaFollowedArticles();
}
insertTimes();
}
function insertArticle(article,parent,insertPlace) {
if (article){
var newItem = document.createElement("DIV");
if (article.cached_user) {
article.user = article.cached_user.table
}
if (article.cached_organization) {
article.organization = article.cached_organization.table
}
newItem.innerHTML = buildArticleHTML(article)
parent.insertBefore(newItem, insertPlace);
initializeReadingListIcons();
}
}
function insertInitialArticles(user) {
var el = document.getElementById("home-articles-object");
if (!el) {return}
el.innerHTML = "";
insertNewArticles(user);
insertTopArticles(user);
el.outerHTML = "";
}
function insertNewArticles(user){
var el = document.getElementById("new-articles-object");
var articlesJSON = JSON.parse(el.dataset.articles);
var insertPlace = document.getElementById("article-index-hidden-div");
if (insertPlace) {
articlesJSON.forEach(function(article){
var articlePoints = 0
var containsUserID = findOne([article.user_id], user.followed_user_ids || [])
var containsOrganizationID = findOne([article.organization_id], user.followed_organization_ids || [])
var intersectedTags = intersect_arrays(user.followed_tag_names, article.cached_tag_list_array)
var followedPoints = 1
var experienceDifference = Math.abs(article['experience_level_rating'] - user.experience_level || 5)
var containsPreferredLanguage = findOne([article.language || 'en'], user.preferred_languages_array || ['en']);
JSON.parse(user.followed_tags).map(function(tag) {
if (intersectedTags.includes(tag.name)) {
followedPoints = followedPoints + tag.points
}
})
articlePoints = articlePoints + (followedPoints*2) + article.positive_reactions_count
if (containsUserID || article.user_id === user.id) {
articlePoints = articlePoints + 16
}
if (containsOrganizationID) {
articlePoints = articlePoints + 16
}
if (containsPreferredLanguage) {
articlePoints = articlePoints + 1
} else {
articlePoints = articlePoints - 10
}
var rand = Math.random();
if (rand < 0.3) {
articlePoints = articlePoints + 3
} else if (rand < 0.6) {
articlePoints = articlePoints + 6
}
articlePoints = articlePoints - (experienceDifference/2);
article['points'] = articlePoints
});
var sortedArticles = articlesJSON.sort(function(a, b) {
return b.points - a.points;
});
sortedArticles.forEach(function(article){
var parent = insertPlace.parentNode;
if ( article.points > 12 && !document.getElementById("article-link-"+article.id) ) {
insertArticle(article,parent,insertPlace);
}
});
}
}
function insertTopArticles(user){
var el = document.getElementById("home-articles-object");
var articlesJSON = JSON.parse(el.dataset.articles)
var insertPlace = document.getElementById("article-index-hidden-div");
if (insertPlace) {
articlesJSON.forEach(function(article){
var articlePoints = 0
var containsUserID = findOne([article.user_id], user.followed_user_ids || [])
var containsOrganizationID = findOne([article.organization_id], user.followed_organization_ids || [])
var intersectedTags = intersect_arrays(user.followed_tag_names, article.cached_tag_list_array)
var followedPoints = 1
var experienceDifference = Math.abs(article['experience_level_rating'] - user.experience_level || 5)
var containsPreferredLanguage = findOne([article.language || 'en'], user.preferred_languages_array || ['en'])
JSON.parse(user.followed_tags).map(function(tag) {
if (intersectedTags.includes(tag.name)) {
followedPoints = followedPoints + tag.points
}
})
articlePoints = articlePoints + followedPoints
if (containsUserID) {
articlePoints = articlePoints + 1
}
if (containsOrganizationID) {
articlePoints = articlePoints + 1
}
var rand = Math.random();
if (rand < 0.3) {
articlePoints = articlePoints + 3
} else if (rand < 0.6) {
articlePoints = articlePoints + 6
}
if (containsPreferredLanguage) {
articlePoints = articlePoints + 1
} else {
articlePoints = articlePoints - 10
}
articlePoints = articlePoints - (experienceDifference/2);
article['points'] = articlePoints
});
var sortedArticles = articlesJSON.sort(function(a, b) {
return b.points - a.points;
});
sortedArticles.forEach(function(article){
var parent = insertPlace.parentNode;
if ( article.points > 0 && !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)
const checkBlockedContentEvent = new CustomEvent('checkBlockedContent')
window.dispatchEvent(checkBlockedContentEvent)
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 insertPodcasts() {
var el = document.getElementById('followed-podcasts');
var insertPlace = document.getElementById('article-index-podcast-div');
if (el && insertPlace){
var user = userData();
if (user && user.followed_podcast_ids && user.followed_podcast_ids.length > 0) {
var data = JSON.parse(el.dataset.episodes);
var podcastHTML = '';
var episodeCount = 0;
data.forEach(function(ep) {
if (user.followed_podcast_ids.indexOf(ep.podcast.id) > -1) {
episodeCount += 1;
podcastHTML = podcastHTML + '\