mirror of
https://github.com/neocities/neocities.git
synced 2025-04-24 17:22:35 +02:00
comment liking
This commit is contained in:
parent
8210670122
commit
51a85a0b03
9 changed files with 125 additions and 29 deletions
10
app.rb
10
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
|
||||
|
|
15
migrations/027_add_comment_likes.rb
Normal file
15
migrations/027_add_comment_likes.rb
Normal file
|
@ -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
|
9
migrations/028_remove_comment_like_site_id.rb
Normal file
9
migrations/028_remove_comment_like_site_id.rb
Normal file
|
@ -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
|
|
@ -2,4 +2,31 @@ class Comment < Sequel::Model
|
|||
include Sequel::ParanoidDelete
|
||||
many_to_one :event
|
||||
many_to_one :actioning_site, class: :Site
|
||||
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
|
4
models/comment_like.rb
Normal file
4
models/comment_like.rb
Normal file
|
@ -0,0 +1,4 @@
|
|||
class CommentLike < Sequel::Model
|
||||
many_to_one :comment
|
||||
many_to_one :actioning_site, class: :Site
|
||||
end
|
|
@ -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('<br>'))
|
||||
})
|
||||
}
|
||||
}
|
|
@ -22,10 +22,21 @@
|
|||
<p><%= comment.message %></p>
|
||||
</div>
|
||||
<div class="actions">
|
||||
<a id="like" href="#" onclick="Comment.toggleLike(<%= comment.id %>, '<%= csrf_token %>)">Like (1)</a>
|
||||
|
||||
<% if current_site %>
|
||||
<a href="#" class="comment_like" id="comment_<%= comment.id %>_like" data-placement="bottom" data-toggle="tooltip" data-original-title="<%= comment.liking_site_names.join('<br>') %>" onclick="Comment.toggleLike(<%= comment.id %>, '<%= csrf_token %>'); return false"><%= comment.site_likes?(current_site) ? 'Unlike' : 'Like' %><%= comment.comment_likes_dataset.count > 0 ? " (#{comment.comment_likes_dataset.count})" : '' %></a>
|
||||
<% 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 %>
|
||||
<a href="#" onclick="Comment.delete(<%= comment.id %>, '<%= csrf_token %>'); return false">Delete</a>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
|
|
|
@ -9,7 +9,6 @@
|
|||
<% end %>
|
||||
<% if current_site %>
|
||||
<a id="reply" href="#" onclick="Template.renderComment(<%= event.id %>); return false">Reply</a>
|
||||
<% end %>
|
||||
<% if event.created_by? current_site %>
|
||||
<% if event.profile_comment_id %>
|
||||
<a id="editLink" href="#" onclick="ProfileComment.displayEditor('<%= event.id %>'); return false">Edit</a>
|
||||
|
@ -18,4 +17,5 @@
|
|||
<% if event.created_by?(current_site) || event.site_id == current_site.id %>
|
||||
<a href="#" onclick="Event.delete(<%= event.id %>, '<%= csrf_token %>'); return false">Delete</a>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</div>
|
|
@ -57,6 +57,7 @@
|
|||
|
||||
<script>
|
||||
$("a#like").tooltip({html: true})
|
||||
$("a.comment_like").tooltip({html: true})
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
Loading…
Add table
Reference in a new issue