Make home feed more relevant and varied (#757)

This commit is contained in:
Ben Halpern 2018-09-27 13:21:51 -04:00 committed by GitHub
parent b95f8ae028
commit 4205d50bd8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 4 deletions

View file

@ -20,11 +20,21 @@ function insertInitialArticles(user) {
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 articlePoints = 0
var containsUserID = findOne([article.user_id], user.followed_user_ids)
if ( (containsTag || containsUserID) && !document.getElementById("article-link-"+article.id) ) {
articlePoints = articlePoints + intersect_arrays(user.followed_tag_names, article.cached_tag_list_array).length
if (containsUserID) {
articlePoints = articlePoints + 1
}
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);
}
});
@ -81,3 +91,29 @@ function findOne(haystack, arr) {
return haystack.indexOf(v) >= 0;
});
};
function intersect_arrays(a, b) {
var sorted_a = a.concat().sort();
var sorted_b = b.concat().sort();
var common = [];
var a_i = 0;
var b_i = 0;
while (a_i < a.length
&& b_i < b.length)
{
if (sorted_a[a_i] === sorted_b[b_i]) {
common.push(sorted_a[a_i]);
a_i++;
b_i++;
}
else if(sorted_a[a_i] < sorted_b[b_i]) {
a_i++;
}
else {
b_i++;
}
}
return common;
}

View file

@ -98,7 +98,7 @@ class StoriesController < ApplicationController
def handle_base_index
@home_page = true
@page = (params[:page] || 1).to_i
num_articles = user_signed_in? ? 9 : 15
num_articles = 15
@stories = article_finder(num_articles)
if ["week", "month", "year", "infinity"].include?(params[:timeframe])
@ -114,6 +114,10 @@ class StoriesController < ApplicationController
@stories = @stories.
where("reactions_count > ? OR featured = ?", 10, true).
order("hotness_score DESC")
if user_signed_in?
offset = [0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7].sample #random offset, weighted more towards zero
@stories = @stories.offset(offset)
end
@featured_story = @stories.where.not(main_image: nil).first&.decorate || Article.new
end
@stories = @stories.decorate