diff --git a/app/assets/javascripts/initializers/initializeFetchFollowedArticles.js.erb b/app/assets/javascripts/initializers/initializeFetchFollowedArticles.js.erb index 255aab0ff..2a0085009 100644 --- a/app/assets/javascripts/initializers/initializeFetchFollowedArticles.js.erb +++ b/app/assets/javascripts/initializers/initializeFetchFollowedArticles.js.erb @@ -14,10 +14,43 @@ function insertArticle(article,parent,insertPlace) { } function insertInitialArticles(user) { - var el = document.getElementById("home-articles-object") + var el = document.getElementById("home-articles-object"); if (!el) {return} - var articlesJSON = JSON.parse(el.dataset.articles) + 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) + articlePoints = articlePoints + (intersect_arrays(user.followed_tag_names, article.cached_tag_list_array).length*2) + article.positive_reactions_count + if (containsUserID || article.user_id === user.id) { + articlePoints = articlePoints + 16 + } + 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 > 15 && !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){ diff --git a/app/black_box/black_box.rb b/app/black_box/black_box.rb index 9febf74f3..905c82938 100644 --- a/app/black_box/black_box.rb +++ b/app/black_box/black_box.rb @@ -1,7 +1,7 @@ class BlackBox def self.article_hotness_score(article) return (article.featured_number || 10000) / 10000 unless Rails.env.production? - reaction_points = article.reactions.sum(:points) + reaction_points = article.score super_super_recent_bonus = article.published_at > 1.hours.ago ? 18 : 0 super_recent_bonus = article.published_at > 3.hours.ago ? 11 : 0 recency_bonus = article.published_at > 11.hours.ago ? 70 : 0 diff --git a/app/controllers/stories_controller.rb b/app/controllers/stories_controller.rb index 4254a3545..7096bb8dd 100644 --- a/app/controllers/stories_controller.rb +++ b/app/controllers/stories_controller.rb @@ -119,6 +119,14 @@ class StoriesController < ApplicationController @stories = @stories.offset(offset) end @featured_story = @stories.where.not(main_image: nil).first&.decorate || Article.new + if user_signed_in? + @new_stories = Article.where("published_at > ? AND score > ?", 4.hours.ago, -30). + includes(:user). + limit(45). + order("published_at DESC"). + limited_column_select. + decorate + end end @stories = @stories.decorate assign_podcasts diff --git a/app/models/article.rb b/app/models/article.rb index e6b48aa9e..90d38e779 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -355,6 +355,7 @@ class Article < ApplicationRecord end def async_score_calc + update_column(:score, reactions.sum(:points)) update_column(:hotness_score, BlackBox.article_hotness_score(self)) update_column(:spaminess_rating, BlackBox.calculate_spaminess(self)) index! if tag_list.exclude?("hiring") diff --git a/app/views/articles/_main_stories_feed.html.erb b/app/views/articles/_main_stories_feed.html.erb index f826343c8..19cf78805 100644 --- a/app/views/articles/_main_stories_feed.html.erb +++ b/app/views/articles/_main_stories_feed.html.erb @@ -28,7 +28,14 @@ <% end %> <% end %> <% else %> - <% cache("fetched-home-articles-object", :expires_in => 5.minutes) do %> + <% cache("fetched-home-articles-object", :expires_in => 3.minutes) do %> +