diff --git a/app.rb b/app.rb index a616df45..3d1b9295 100644 --- a/app.rb +++ b/app.rb @@ -205,6 +205,13 @@ get '/site/:username/tip' do |username| erb :'tip' 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 @current_page = params[:current_page] @current_page = @current_page.to_i diff --git a/models/site.rb b/models/site.rb index 601f9c1d..f2e02a92 100644 --- a/models/site.rb +++ b/models/site.rb @@ -2,7 +2,7 @@ require 'tilt' class Site < Sequel::Model include Sequel::ParanoidDelete - + VALID_MIME_TYPES = %w{ text/plain text/html @@ -65,7 +65,10 @@ class Site < Sequel::Model one_to_many :profile_comments one_to_many :profile_commentings, key: :actioning_site_id, class: :ProfileComment + # Who is following this site one_to_many :follows + + # Who this site is following one_to_many :followings, key: :actioning_site_id, class: :Follow one_to_many :tips @@ -96,6 +99,20 @@ class Site < Sequel::Model 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 return '0.00' if tips_dataset.count == 0 '31.337' @@ -390,7 +407,7 @@ class Site < Sequel::Model def title values[:title] || values[:username] end - + def hits_english values[:hits].to_s.reverse.gsub(/...(?=.)/,'\&,').reverse end @@ -403,7 +420,7 @@ class Site < Sequel::Model end end end - + def thumbnails_delete(filename) THUMBNAIL_RESOLUTIONS.each do |res| begin @@ -424,22 +441,22 @@ class Site < Sequel::Model def screenshot_url(filename, resolution) "#{SCREENSHOTS_URL_ROOT}/#{values[:username]}/#{filename}.#{resolution}.jpg" end - + def thumbnail_path(filename, resolution) ext = File.extname(filename).gsub('.', '').match(LOSSY_IMAGE_REGEX) ? 'jpg' : 'png' File.join THUMBNAILS_ROOT, values[:username], "#{filename}.#{resolution}.#{ext}" end - + def thumbnail_exists?(filename, resolution) File.exist? thumbnail_path(filename, resolution) end - + def thumbnail_delete(filename, resolution) File.rm thumbnail_path(filename, resolution) end - + def thumbnail_url(filename, resolution) ext = File.extname(filename).gsub('.', '').match(LOSSY_IMAGE_REGEX) ? 'jpg' : 'png' "#{THUMBNAILS_URL_ROOT}/#{values[:username]}/#{filename}.#{resolution}.#{ext}" end -end \ No newline at end of file +end diff --git a/public/assets/scripts/news/site.js b/public/assets/scripts/news/site.js new file mode 100644 index 00000000..39820975 --- /dev/null +++ b/public/assets/scripts/news/site.js @@ -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') + } + }) + } +} diff --git a/views/_news.erb b/views/_news.erb index 73d7c52e..b90da7d0 100644 --- a/views/_news.erb +++ b/views/_news.erb @@ -3,6 +3,8 @@ + + <% events.each do |event| %> <% if event.profile_comment_id %>