following button on site

This commit is contained in:
Kyle Drake 2014-05-05 15:25:25 -07:00
parent 51a85a0b03
commit 0a29a496d7
5 changed files with 62 additions and 18 deletions

7
app.rb
View file

@ -205,6 +205,13 @@ get '/site/:username/tip' do |username|
erb :'tip' erb :'tip'
end end
post '/site/:site_id/toggle_follow' do |site_id|
require_login
content_type :json
site = Site[id: site_id]
{result: (current_site.toggle_follow(site) ? 'followed' : 'unfollowed')}.to_json
end
get '/browse' do get '/browse' do
@current_page = params[:current_page] @current_page = params[:current_page]
@current_page = @current_page.to_i @current_page = @current_page.to_i

View file

@ -2,7 +2,7 @@ require 'tilt'
class Site < Sequel::Model class Site < Sequel::Model
include Sequel::ParanoidDelete include Sequel::ParanoidDelete
VALID_MIME_TYPES = %w{ VALID_MIME_TYPES = %w{
text/plain text/plain
text/html text/html
@ -65,7 +65,10 @@ class Site < Sequel::Model
one_to_many :profile_comments one_to_many :profile_comments
one_to_many :profile_commentings, key: :actioning_site_id, class: :ProfileComment one_to_many :profile_commentings, key: :actioning_site_id, class: :ProfileComment
# Who is following this site
one_to_many :follows one_to_many :follows
# Who this site is following
one_to_many :followings, key: :actioning_site_id, class: :Follow one_to_many :followings, key: :actioning_site_id, class: :Follow
one_to_many :tips one_to_many :tips
@ -96,6 +99,20 @@ class Site < Sequel::Model
end end
end end
def is_following?(site)
followings_dataset.select(:id).filter(site_id: site.id).first ? true : false
end
def toggle_follow(site)
if is_following? site
followings_dataset.filter(site_id: site.id).delete
false
else
add_following site_id: site.id
true
end
end
def tip_amount def tip_amount
return '0.00' if tips_dataset.count == 0 return '0.00' if tips_dataset.count == 0
'31.337' '31.337'
@ -390,7 +407,7 @@ class Site < Sequel::Model
def title def title
values[:title] || values[:username] values[:title] || values[:username]
end end
def hits_english def hits_english
values[:hits].to_s.reverse.gsub(/...(?=.)/,'\&,').reverse values[:hits].to_s.reverse.gsub(/...(?=.)/,'\&,').reverse
end end
@ -403,7 +420,7 @@ class Site < Sequel::Model
end end
end end
end end
def thumbnails_delete(filename) def thumbnails_delete(filename)
THUMBNAIL_RESOLUTIONS.each do |res| THUMBNAIL_RESOLUTIONS.each do |res|
begin begin
@ -424,22 +441,22 @@ class Site < Sequel::Model
def screenshot_url(filename, resolution) def screenshot_url(filename, resolution)
"#{SCREENSHOTS_URL_ROOT}/#{values[:username]}/#{filename}.#{resolution}.jpg" "#{SCREENSHOTS_URL_ROOT}/#{values[:username]}/#{filename}.#{resolution}.jpg"
end end
def thumbnail_path(filename, resolution) def thumbnail_path(filename, resolution)
ext = File.extname(filename).gsub('.', '').match(LOSSY_IMAGE_REGEX) ? 'jpg' : 'png' ext = File.extname(filename).gsub('.', '').match(LOSSY_IMAGE_REGEX) ? 'jpg' : 'png'
File.join THUMBNAILS_ROOT, values[:username], "#{filename}.#{resolution}.#{ext}" File.join THUMBNAILS_ROOT, values[:username], "#{filename}.#{resolution}.#{ext}"
end end
def thumbnail_exists?(filename, resolution) def thumbnail_exists?(filename, resolution)
File.exist? thumbnail_path(filename, resolution) File.exist? thumbnail_path(filename, resolution)
end end
def thumbnail_delete(filename, resolution) def thumbnail_delete(filename, resolution)
File.rm thumbnail_path(filename, resolution) File.rm thumbnail_path(filename, resolution)
end end
def thumbnail_url(filename, resolution) def thumbnail_url(filename, resolution)
ext = File.extname(filename).gsub('.', '').match(LOSSY_IMAGE_REGEX) ? 'jpg' : 'png' ext = File.extname(filename).gsub('.', '').match(LOSSY_IMAGE_REGEX) ? 'jpg' : 'png'
"#{THUMBNAILS_URL_ROOT}/#{values[:username]}/#{filename}.#{resolution}.#{ext}" "#{THUMBNAILS_URL_ROOT}/#{values[:username]}/#{filename}.#{resolution}.#{ext}"
end end
end end

View file

@ -0,0 +1,16 @@
var Site = {
toggleFollow: function(siteId, csrfToken) {
var link = $('a#followLink')
var span = $('a#followLink span')
$.post('/site/'+siteId+'/toggle_follow', {csrf_token: csrfToken}, function(res) {
console.log(res)
if(res.result == "followed") {
span.text('Unfollow')
link.removeClass('follow')
} else if(res.result == 'unfollowed') {
span.text('Follow')
link.addClass('follow')
}
})
}
}

View file

@ -3,6 +3,8 @@
<script src="/assets/scripts/news/comment.js"></script> <script src="/assets/scripts/news/comment.js"></script>
<script src="/assets/scripts/news/profile_comment.js"></script> <script src="/assets/scripts/news/profile_comment.js"></script>
<script src="/assets/scripts/news/event.js"></script> <script src="/assets/scripts/news/event.js"></script>
<script src="/assets/scripts/news/site.js"></script>
<% events.each do |event| %> <% events.each do |event| %>
<% if event.profile_comment_id %> <% if event.profile_comment_id %>
<div class="news-item comment for-me" id="event_<%= event.id %>"> <div class="news-item comment for-me" id="event_<%= event.id %>">

View file

@ -16,9 +16,11 @@
<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>
</div> </div>
<div class="actions"> <div class="actions">
<% if site != current_site %> <% if current_site && current_site != site %>
<a href="" class="btn-Action follow"><span>Follow</span></a> <a id="followLink" href="#" onclick="Site.toggleFollow(<%= site.id %>, '<%= csrf_token %>')" class="btn-Action <%= current_site.is_following?(site) ? '' : 'follow' %>">
<a href="" class="btn-Action tip"><span>Tip</span></a> <span><%= current_site.is_following?(site) ? 'Unfollow' : 'Follow' %></span>
</a>
<a href="#" class="btn-Action tip"><span>Tip</span></a>
<% end %> <% end %>
<a href="" class="btn-Action share"><span>Share</span></a> <a href="" class="btn-Action share"><span>Share</span></a>
</div> </div>
@ -46,9 +48,9 @@
<% else %> <% else %>
<%== erb :'_news', layout: false, locals: {site: site, events: site.events_dataset.order(:id.desc).all} %> <%== erb :'_news', layout: false, locals: {site: site, events: site.events_dataset.order(:id.desc).all} %>
<% end %> <% end %>
</div> </div>
<div class="col col-33"> <div class="col col-33">
<h3>Website Stats</h3> <h3>Website Stats</h3>
<div class="stats"> <div class="stats">
@ -74,7 +76,7 @@
<a href=""><img src="http://neocities.org/site_screenshots/codeventurer.jpg"></a> <a href=""><img src="http://neocities.org/site_screenshots/codeventurer.jpg"></a>
<a href="" class="more">See all versions</a> <a href="" class="more">See all versions</a>
</div> </div>
<% if site.followings_dataset.count < 0 %> <% if site.followings_dataset.count < 0 %>
<h3>Following</h3> <h3>Following</h3>
<div class="following"> <div class="following">
@ -83,14 +85,14 @@
<% end %> <% end %>
</div> </div>
<% end %> <% end %>
<%== erb :'_follows', layout: false, locals: {site: site, is_current_site: site == current_site} %> <%== erb :'_follows', layout: false, locals: {site: site, is_current_site: site == current_site} %>
<%== erb :'_tags', layout: false, locals: {site: site, is_current_site: site == current_site} %> <%== erb :'_tags', layout: false, locals: {site: site, is_current_site: site == current_site} %>
<div class="report"> <div class="report">
<a href="">Report</a> | <a href="">Block</a> <a href="">Report</a> | <a href="">Block</a>
</div> </div>
</div> </div>
</div></div> </div></div>
</div> </div>