Ben/add more recent posts to home feed (#775)

* Add meta to sidebar and adjust ranking algo

* Update home feed to show more recent posts from folks you follow

* Only show recent posts on home feed if user signed in
This commit is contained in:
Ben Halpern 2018-09-30 14:05:59 -04:00 committed by GitHub
parent 9f36295c40
commit 7e48a23afa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 60 additions and 5 deletions

View file

@ -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){

View file

@ -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

View file

@ -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

View file

@ -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")

View file

@ -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 %>
<div id="new-articles-object" data-articles="
<%=@new_stories.to_json(only:
[:title, :path, :id, :user_id, :comments_count, :positive_reactions_count],
methods:
[:readable_publish_date, :cached_tag_list_array, :flare_tag, :class_name],
include: [user: {only: [:username, :name], methods: [:profile_image_90]}]) %>">
</div>
<div id="home-articles-object" data-articles="
<%=@stories.to_json(only:
[:title, :path, :id, :user_id, :comments_count, :positive_reactions_count],

View file

@ -0,0 +1,5 @@
class AddScoreToArticles < ActiveRecord::Migration[5.1]
def change
add_column :articles, :score, :integer, default: 0
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20180928161837) do
ActiveRecord::Schema.define(version: 20180930015157) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -90,6 +90,7 @@ ActiveRecord::Schema.define(version: 20180928161837) do
t.integer "reactions_count", default: 0, null: false
t.boolean "receive_notifications", default: true
t.boolean "removed_for_abuse", default: false
t.integer "score", default: 0
t.integer "second_user_id"
t.boolean "show_comments", default: true
t.text "slug"