diff --git a/Rakefile b/Rakefile index 07bc027f..c1926403 100644 --- a/Rakefile +++ b/Rakefile @@ -422,12 +422,15 @@ task :shard_migration => [:environment] do FileUtils.mv './public/newtestsites', './public/testsites' end -desc 'prime_follow_count' -task :prime_follow_count => [:environment] do - DB['update sites set follow_count=0'].first - Site.select(:id,:username).all.each do |site| - count = site.follows_dataset.count - next if count == 0 +desc 'compute_follow_count_scores' +task :compute_follow_count_scores => [:environment] do + + Site.select(:id,:username,:follow_count).all.each do |site| + count = site.scorable_follow_count + + if count != 0 + puts "#{site.username} #{site.follow_count} => #{count}" + end DB['update sites set follow_count=? where id=?', count, site.id].first end end diff --git a/models/site.rb b/models/site.rb index 19751a8d..f7833041 100644 --- a/models/site.rb +++ b/models/site.rb @@ -351,28 +351,35 @@ class Site < Sequel::Model return false end - def can_follow?(site) - return false if site.id == self.id # Do not follow yourself - return false if site.owned_by?(self) # Do not follow your own sites - # return false if account_sites_follow?(site) # Do not follow if any of your other sites follow + def scorable_follow?(site) + return false if site.id == self.id # Do not count follow of yourself + return false if site.owned_by?(self) # Do not count follow of your own sites + return false if account_sites_follow?(site) # Do not count follow if any of your other sites follow true end + def scorable_follow_count + score_follow_count = 0 + + follows_dataset.all.each do |follow| + score_follow_count += 1 if scorable_follow?(follow.actioning_site) + end + score_follow_count + end + def toggle_follow(site) if is_following? site DB.transaction do follow = followings_dataset.filter(site_id: site.id).first site.events_dataset.filter(follow_id: follow.id).delete follow.delete - DB['update sites set follow_count=follow_count-1 where id=?', site.id].first + DB['update sites set follow_count=follow_count-1 where id=?', site.id].first if scorable_follow?(site) end false else - return false unless can_follow?(site) - DB.transaction do follow = add_following site_id: site.id - DB['update sites set follow_count=follow_count+1 where id=?', site.id].first + DB['update sites set follow_count=follow_count+1 where id=?', site.id].first if scorable_follow?(site) Event.create site_id: site.id, actioning_site_id: self.id, follow_id: follow.id end diff --git a/views/site.erb b/views/site.erb index dada2193..dac5a132 100644 --- a/views/site.erb +++ b/views/site.erb @@ -26,9 +26,10 @@

<%= site.latest_archive.ipfs_hash %>(what is this?)

<% end %> --> + <% follow_count = site.follows.count %>
<%= site.views.format_large_number %> view<%= site.views == 1 ? '' : 's' %>
-
<%= site.follow_count.format_large_number %> follower<%= site.follow_count == 1 ? '' : 's' %>
+
<%= follow_count.format_large_number %> follower<%= follow_count == 1 ? '' : 's' %>
<%= site.changed_count.format_large_number %> update<%= site.changed_count == 1 ? '' : 's' %>
<%= site.tips_dataset.count %> tips
@@ -41,7 +42,7 @@ Archives <% end %> - <% if current_site && current_site != site && current_site.can_follow?(site) %> + <% if current_site && current_site != site %> <% is_following = current_site.is_following?(site) %>