diff --git a/app.rb b/app.rb index 10eeff77..a616df45 100644 --- a/app.rb +++ b/app.rb @@ -120,6 +120,7 @@ get '/?' do end get '/plan/?' do + @title = 'Supporter' erb :'plan/index' end @@ -233,6 +234,7 @@ get '/browse' do end get '/api' do + @title = 'Developers API' erb :'api' end @@ -896,6 +898,14 @@ post '/event/:event_id/delete' do |event_id| return {result: 'error'}.to_json end +post '/comment/:comment_id/toggle_like' do |comment_id| + require_login + content_type :json + comment = Comment[id: comment_id] + liked_response = comment.toggle_site_like(current_site) ? 'liked' : 'unliked' + {result: liked_response, comment_like_count: comment.comment_likes_dataset.count, liking_site_names: comment.liking_site_names}.to_json +end + post '/comment/:comment_id/delete' do |comment_id| require_login content_type :json @@ -1014,4 +1024,4 @@ end def api_not_found api_error 404, 'not_found', 'the requested api call does not exist' -end \ No newline at end of file +end diff --git a/migrations/027_add_comment_likes.rb b/migrations/027_add_comment_likes.rb new file mode 100644 index 00000000..d64424f8 --- /dev/null +++ b/migrations/027_add_comment_likes.rb @@ -0,0 +1,15 @@ +Sequel.migration do + up { + DB.create_table! :comment_likes do + primary_key :id + Integer :comment_id + Integer :site_id + Integer :actioning_site_id + DateTime :created_at + end + } + + down { + DB.drop_table :comment_likes + } +end diff --git a/migrations/028_remove_comment_like_site_id.rb b/migrations/028_remove_comment_like_site_id.rb new file mode 100644 index 00000000..55029af1 --- /dev/null +++ b/migrations/028_remove_comment_like_site_id.rb @@ -0,0 +1,9 @@ +Sequel.migration do + up { + DB.drop_column :comment_likes, :site_id + } + + down { + DB.add_column :comment_likes, :site_id, :integer + } +end diff --git a/models/comment.rb b/models/comment.rb index 498e1377..c09e7442 100644 --- a/models/comment.rb +++ b/models/comment.rb @@ -2,4 +2,31 @@ class Comment < Sequel::Model include Sequel::ParanoidDelete many_to_one :event many_to_one :actioning_site, class: :Site -end \ No newline at end of file + one_to_many :comment_likes + + def liking_site_names + comment_likes.collect {|comment_like| comment_like.actioning_site.username } + end + + def site_likes?(site) + comment_likes_dataset.filter(actioning_site_id: site.id).count > 0 + end + + def site_like(site) + add_comment_like actioning_site_id: site.id + end + + def site_unlike(site) + comment_likes_dataset.filter(actioning_site_id: site.id).delete + end + + def toggle_site_like(site) + if site_likes? site + site_unlike site + false + else + site_like site + true + end + end +end diff --git a/models/comment_like.rb b/models/comment_like.rb new file mode 100644 index 00000000..41ceb259 --- /dev/null +++ b/models/comment_like.rb @@ -0,0 +1,4 @@ +class CommentLike < Sequel::Model + many_to_one :comment + many_to_one :actioning_site, class: :Site +end diff --git a/public/assets/scripts/news/comment.js b/public/assets/scripts/news/comment.js index 00b0828e..278c8d61 100644 --- a/public/assets/scripts/news/comment.js +++ b/public/assets/scripts/news/comment.js @@ -15,5 +15,24 @@ var Comment = { console.log(res) location.reload() }) + }, + + toggleLike: function(commentId, csrfToken) { + var link = $('#comment_'+commentId+'_like') + + $.post('/comment/'+commentId+'/toggle_like', {csrf_token: csrfToken}, function(res) { + if(res.result == 'liked') + link.text('Unlike ('+res.comment_like_count+')') + + if(res.result == 'unliked') { + var linkText = 'Like' + + if(res.comment_like_count > 0) + linkText += ' ('+res.comment_like_count+')' + + link.text(linkText) + } + link.attr('data-original-title', res.liking_site_names.join('
')) + }) } -} \ No newline at end of file +} diff --git a/views/_news.erb b/views/_news.erb index 59a689cb..73d7c52e 100644 --- a/views/_news.erb +++ b/views/_news.erb @@ -8,9 +8,9 @@
<%== erb :'_news_profile_comment', layout: false, locals: {profile_comment: event.profile_comment} %> <% end %> - + <%== erb :'_news_actions', layout: false, locals: {event: event} %> - + <% if event.comments_dataset.count > 0 %>
@@ -22,9 +22,20 @@

<%= comment.message %>

- Like (1) - <% if event.site_id == current_site.id || comment.actioning_site_id == current_site.id %> - Delete + + <% if current_site %> + <%= comment.site_likes?(current_site) ? 'Unlike' : 'Like' %><%= comment.comment_likes_dataset.count > 0 ? " (#{comment.comment_likes_dataset.count})" : '' %> + <% else %> + <% comment_like_count = comment.comment_likes_dataset.count %> + <% if comment_like_count > 0 %> + <%= comment_like_count %> <%= comment_like_count == 1 ? 'like' : 'likes' %> + <% end %> + <% end %> + + <% if current_site %> + <% if event.site_id == current_site.id || comment.actioning_site_id == current_site.id %> + Delete + <% end %> <% end %>
<% end %> @@ -45,14 +56,14 @@ Derp followed Foo's website7h
- +
Derp tipped Foo's website: Wow, great work here! Please keep updating :)Apr 23
- +
@@ -60,14 +71,14 @@ Apr 20
- +
Derp followed victoria's websiteApr 7
- +
@@ -94,11 +105,11 @@
- +
- Foo + Foo Apr 7
Your site is amazing. Very helpful information. Would love to see more updates if you have time. Your site is amazing. Very helpful information. Would love to see more updates if you have time.
@@ -109,11 +120,11 @@ victoria Indeed, it's great!Apr 7
Like (1)
-
+ - +
@@ -121,7 +132,7 @@ Apr 7
- +
@@ -133,4 +144,4 @@
---> \ No newline at end of file +--> diff --git a/views/_news_actions.erb b/views/_news_actions.erb index eedd9ad1..0b1d1618 100644 --- a/views/_news_actions.erb +++ b/views/_news_actions.erb @@ -9,13 +9,13 @@ <% end %> <% if current_site %> Reply - <% end %> - <% if event.created_by? current_site %> - <% if event.profile_comment_id %> - Edit + <% if event.created_by? current_site %> + <% if event.profile_comment_id %> + Edit + <% end %> + <% end %> + <% if event.created_by?(current_site) || event.site_id == current_site.id %> + Delete <% end %> <% end %> - <% if event.created_by?(current_site) || event.site_id == current_site.id %> - Delete - <% end %> - \ No newline at end of file + diff --git a/views/layout.erb b/views/layout.erb index a534fb65..97af8cb8 100644 --- a/views/layout.erb +++ b/views/layout.erb @@ -13,11 +13,11 @@ - + - + @@ -32,7 +32,7 @@ - @@ -57,6 +57,7 @@