add views display/search, and improve initial site suggestions

This commit is contained in:
Kyle Drake 2014-09-15 16:48:19 -07:00
parent df4cf8a3d6
commit 5cb988b3e7
6 changed files with 56 additions and 16 deletions

17
app.rb
View file

@ -216,9 +216,7 @@ get '/?' do
if current_site if current_site
require_login require_login
if current_site.followings_dataset.count == 0 @suggestions = current_site.suggestions
@suggestions = current_site.suggestions
end
@current_page = params[:current_page].to_i @current_page = params[:current_page].to_i
@current_page = 1 if @current_page == 0 @current_page = 1 if @current_page == 0
@ -398,9 +396,17 @@ get '/browse/?' do
site_dataset.order!(:created_at) site_dataset.order!(:created_at)
when 'random' when 'random'
site_dataset.where! 'random() < 0.01' site_dataset.where! 'random() < 0.01'
else when 'last_updated'
params[:sort_by] = 'last_updated' params[:sort_by] = 'last_updated'
site_dataset.order!(:updated_at.desc, :hits.desc) site_dataset.order!(:updated_at.desc, :views.desc)
else
if params[:tag]
params[:sort_by] = 'views'
site_dataset.order!(:views.desc)
else
params[:sort_by] = 'last_updated'
site_dataset.order!(:updated_at.desc, :views.desc)
end
end end
site_dataset.filter! is_nsfw: (params[:is_nsfw] == 'true' ? true : false) site_dataset.filter! is_nsfw: (params[:is_nsfw] == 'true' ? true : false)
@ -1221,6 +1227,7 @@ def api_info_for(site)
{ {
info: { info: {
sitename: site.username, sitename: site.username,
views: site.views,
hits: site.hits, hits: site.hits,
created_at: site.created_at.rfc2822, created_at: site.created_at.rfc2822,
last_updated: site.updated_at.rfc2822, last_updated: site.updated_at.rfc2822,

View file

@ -93,6 +93,9 @@ class Site < Sequel::Model
} }
} }
SUGGESTIONS_LIMIT = 32
SUGGESTIONS_VIEWS_MIN = 500
PLAN_FEATURES[:catbus] = PLAN_FEATURES[:fatcat].merge( PLAN_FEATURES[:catbus] = PLAN_FEATURES[:fatcat].merge(
name: 'Cat Bus', name: 'Cat Bus',
space: Filesize.from('10GB').to_i, space: Filesize.from('10GB').to_i,
@ -793,6 +796,10 @@ class Site < Sequel::Model
values[:hits].to_s.reverse.gsub(/...(?=.)/,'\&,').reverse values[:hits].to_s.reverse.gsub(/...(?=.)/,'\&,').reverse
end end
def views_english
values[:views].to_s.reverse.gsub(/...(?=.)/,'\&,').reverse
end
def screenshots_delete(path) def screenshots_delete(path)
SCREENSHOT_RESOLUTIONS.each do |res| SCREENSHOT_RESOLUTIONS.each do |res|
begin begin
@ -811,8 +818,13 @@ class Site < Sequel::Model
end end
end end
def suggestions(limit=8, offset=0) def suggestions(limit=SUGGESTIONS_LIMIT, offset=0)
Site.where(tags: tags).limit(limit, offset).order(:updated_at.desc).all suggestions_dataset = Site.exclude(id: id).order(:views.desc, :updated_at.desc)
suggestions = suggestions_dataset.where(tags: tags).limit(limit, offset).all
return suggestions if suggestions.length == 32
suggestions += suggestions_dataset.where("views >= #{SUGGESTIONS_VIEWS_MIN}").limit(limit-suggestions.length).order(Sequel.lit('RANDOM()')).all
end end
def screenshot_path(path, resolution) def screenshot_path(path, resolution)

View file

@ -7,7 +7,7 @@ def app
Sinatra::Application Sinatra::Application
end end
describe 'site' do describe Site do
describe 'plan_name' do describe 'plan_name' do
it 'should set to free for missing stripe_customer_id' do it 'should set to free for missing stripe_customer_id' do
site = Fabricate :site site = Fabricate :site
@ -26,4 +26,22 @@ describe 'site' do
end end
end end
end end
describe 'suggestions' do
it 'should return suggestions for tags' do
site = Fabricate :site, new_tags_string: 'vegetables'
Site::SUGGESTIONS_LIMIT.times { Fabricate :site, new_tags_string: 'vegetables' }
site.suggestions.length.must_equal Site::SUGGESTIONS_LIMIT
site.suggestions.each {|s| s.tags.first.name.must_equal 'vegetables'}
site = Fabricate :site, new_tags_string: 'gardening'
(Site::SUGGESTIONS_LIMIT-5).times {
Fabricate :site, new_tags_string: 'gardening', views: Site::SUGGESTIONS_VIEWS_MIN
}
site.suggestions.length.must_equal Site::SUGGESTIONS_LIMIT
end
end
end end

View file

@ -81,7 +81,11 @@
</div> </div>
<div style="float: right"> <div style="float: right">
<a href="/site/<%= site.username %>"> <a href="/site/<%= site.username %>">
<%= site.hits %> hit<%= site.hits == 1 ? '' : 's' %> <% if params[:sort_by] == 'hits' %>
<%= site.hits %> hit<%= site.hits == 1 ? '' : 's' %>
<% else %>
<%= site.views %> view<%= site.views == 1 ? '' : 's' %>
<% end %>
</a> </a>
</div> </div>
</div> </div>

View file

@ -31,11 +31,7 @@
<p> <p>
You aren't following any websites yet! Once you do, updates will show up here and you can like and comment on them. You aren't following any websites yet! Once you do, updates will show up here and you can like and comment on them.
<% if @suggestions.length > 0 %> Here are some website suggestions for you. Want to find some sites to follow? <a href="/browse">Check out all the sites on Neocities!</a>
Here are some website suggestions based on your tags, or <a href="/browse">check out all the sites on Neocities!</a>
<% else %>
Want to find some sites to follow? <a href="/browse">Check out all the sites on Neocities!</a>
<% end %>
</p> </p>
</div> </div>
@ -45,8 +41,9 @@
<a href="//<%= suggested_site.host %>"> <a href="//<%= suggested_site.host %>">
<img src="<%= suggested_site.screenshot_url('index.html', '270x162') %>"> <img src="<%= suggested_site.screenshot_url('index.html', '270x162') %>">
</a> </a>
<span class="caption"> <span class="caption">
<a href="/site/<%= suggested_site.username %>"><%= suggested_site.title %></a></span> <a href="/site/<%= suggested_site.username %>"><%= suggested_site.title %></a>
</span>
</div> </div>
<% suggested_site.tags.each do |tag| %> <% suggested_site.tags.each do |tag| %>
<a class="tag" href="/browse?tag=<%= Rack::Utils.escape tag.name %>"><%= tag.name %></a> <a class="tag" href="/browse?tag=<%= Rack::Utils.escape tag.name %>"><%= tag.name %></a>
@ -67,6 +64,7 @@
<% end %> <% end %>
</div> </div>
<div class="col col-50"> <div class="col col-50">
<div><strong><%= site.views_english %></strong> unique views</div>
<div><strong><%= site.hits_english %></strong> hits</div> <div><strong><%= site.hits_english %></strong> hits</div>
<div><strong><%= site.follows_dataset.count %></strong> followers</div> <div><strong><%= site.follows_dataset.count %></strong> followers</div>
<div><strong><%= site.tips_dataset.count %></strong> tips ($<%= site.tip_amount %>)</div> <div><strong><%= site.tips_dataset.count %></strong> tips ($<%= site.tip_amount %>)</div>

View file

@ -22,6 +22,7 @@
<h2 class="eps title-with-badge"><span><%= site.title %></span> <% if site.supporter? && !site.ended_supporter? %><a href="/plan" class="supporter-badge" title="Neocities Supporter"></a> <% end %></h2> <h2 class="eps title-with-badge"><span><%= site.title %></span> <% if site.supporter? && !site.ended_supporter? %><a href="/plan" class="supporter-badge" title="Neocities Supporter"></a> <% end %></h2>
<p class="site-url"><a href="//<%= site.host %>"><%= site.host %></a></p> <p class="site-url"><a href="//<%= site.host %>"><%= site.host %></a></p>
<div class="stats"> <div class="stats">
<div class="stat"><strong><%= site.views_english %></strong> <span>views</span></div>
<div class="stat"><strong><%= site.hits_english %></strong> <span>hits</span></div> <div class="stat"><strong><%= site.hits_english %></strong> <span>hits</span></div>
<div class="stat"><strong><%= site.follows_dataset.count %></strong> <span>followers</span></div> <div class="stat"><strong><%= site.follows_dataset.count %></strong> <span>followers</span></div>
<!-- <div class="stat tips"><strong><%= site.tips_dataset.count %></strong> <span>tips</span></div> --> <!-- <div class="stat tips"><strong><%= site.tips_dataset.count %></strong> <span>tips</span></div> -->