Bring back likes on comments
This commit is contained in:
parent
bb882daeae
commit
82ff57a750
6 changed files with 84 additions and 5 deletions
|
|
@ -4,7 +4,10 @@ app.collections.Likes = Backbone.Collection.extend({
|
||||||
model: app.models.Like,
|
model: app.models.Like,
|
||||||
|
|
||||||
initialize : function(models, options) {
|
initialize : function(models, options) {
|
||||||
this.url = "/posts/" + options.post.id + "/likes"; //not delegating to post.url() because when it is in a stream collection it delegates to that url
|
this.url = (options.post != null) ?
|
||||||
|
// not delegating to post.url() because when it is in a stream collection it delegates to that url
|
||||||
|
"/posts/" + options.post.id + "/likes" :
|
||||||
|
"/comments/" + options.comment.id + "/likes";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// @license-end
|
// @license-end
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,53 @@
|
||||||
// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-v3-or-Later
|
// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-v3-or-Later
|
||||||
|
|
||||||
app.models.Comment = Backbone.Model.extend({
|
app.models.Comment = Backbone.Model.extend({
|
||||||
urlRoot: "/comments"
|
urlRoot: "/comments",
|
||||||
|
|
||||||
|
initialize: function() {
|
||||||
|
this.likes = new app.collections.Likes(this.get("likes"), {comment: this});
|
||||||
|
},
|
||||||
|
|
||||||
|
// Copied from Post.Interaction. To be merged in an "interactable" class once comments can be commented too
|
||||||
|
likesCount: function() {
|
||||||
|
return this.get("likes_count");
|
||||||
|
},
|
||||||
|
|
||||||
|
userLike: function() {
|
||||||
|
return this.likes.select(function(like) {
|
||||||
|
return like.get("author") && like.get("author").guid === app.currentUser.get("guid");
|
||||||
|
})[0];
|
||||||
|
},
|
||||||
|
|
||||||
|
toggleLike: function() {
|
||||||
|
if (this.userLike()) {
|
||||||
|
this.unlike();
|
||||||
|
} else {
|
||||||
|
this.like();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
like: function() {
|
||||||
|
var self = this;
|
||||||
|
this.likes.create({}, {
|
||||||
|
success: function() {
|
||||||
|
self.post.set({participation: true});
|
||||||
|
self.trigger("change");
|
||||||
|
self.set({"likes_count": self.get("likes_count") + 1});
|
||||||
|
self.likes.trigger("change");
|
||||||
|
},
|
||||||
|
error: function(model, response) {
|
||||||
|
app.flashMessages.handleAjaxError(response);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
unlike: function() {
|
||||||
|
var self = this;
|
||||||
|
this.userLike().destroy({success: function() {
|
||||||
|
self.trigger("change");
|
||||||
|
self.set({"likes_count": self.get("likes_count") - 1});
|
||||||
|
self.likes.trigger("change");
|
||||||
|
}});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
// @license-end
|
// @license-end
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,8 @@ app.views.Comment = app.views.Content.extend({
|
||||||
events : function() {
|
events : function() {
|
||||||
return _.extend({}, app.views.Content.prototype.events, {
|
return _.extend({}, app.views.Content.prototype.events, {
|
||||||
"click .comment_delete": "destroyModel",
|
"click .comment_delete": "destroyModel",
|
||||||
"click .comment_report": "report"
|
"click .comment_report": "report",
|
||||||
|
"click .like": "toggleLike"
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -35,6 +36,11 @@ app.views.Comment = app.views.Content.extend({
|
||||||
|
|
||||||
canRemove : function() {
|
canRemove : function() {
|
||||||
return app.currentUser.authenticated() && (this.ownComment() || this.postOwner());
|
return app.currentUser.authenticated() && (this.ownComment() || this.postOwner());
|
||||||
|
},
|
||||||
|
|
||||||
|
toggleLike: function(evt) {
|
||||||
|
if (evt) { evt.preventDefault(); }
|
||||||
|
this.model.toggleLike();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -36,5 +36,15 @@
|
||||||
<div class="collapsible comment-content markdown-content">
|
<div class="collapsible comment-content markdown-content">
|
||||||
{{{text}}}
|
{{{text}}}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="info">
|
||||||
|
<a href="#" class="like" rel='nofollow'>
|
||||||
|
{{~#if userLike~}}
|
||||||
|
{{~t "stream.unlike"~}}
|
||||||
|
{{~else~}}
|
||||||
|
{{~t "stream.like"~}}
|
||||||
|
{{~/if~}}
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,11 @@ class LikesController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
like = like_service.create(params[:post_id])
|
like = if params[:post_id]
|
||||||
|
like_service.create_for_post(params[:post_id])
|
||||||
|
else
|
||||||
|
like_service.create_for_comment(params[:comment_id])
|
||||||
|
end
|
||||||
rescue ActiveRecord::RecordNotFound, ActiveRecord::RecordInvalid
|
rescue ActiveRecord::RecordNotFound, ActiveRecord::RecordInvalid
|
||||||
render plain: I18n.t("likes.create.error"), status: 422
|
render plain: I18n.t("likes.create.error"), status: 422
|
||||||
else
|
else
|
||||||
|
|
|
||||||
|
|
@ -5,11 +5,16 @@ class LikeService
|
||||||
@user = user
|
@user = user
|
||||||
end
|
end
|
||||||
|
|
||||||
def create(post_id)
|
def create_for_post(post_id)
|
||||||
post = post_service.find!(post_id)
|
post = post_service.find!(post_id)
|
||||||
user.like!(post)
|
user.like!(post)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def create_for_comment(comment_id)
|
||||||
|
comment = comment_service.find!(comment_id)
|
||||||
|
user.like!(comment)
|
||||||
|
end
|
||||||
|
|
||||||
def destroy(like_id)
|
def destroy(like_id)
|
||||||
like = Like.find(like_id)
|
like = Like.find(like_id)
|
||||||
if user.owns?(like)
|
if user.owns?(like)
|
||||||
|
|
@ -43,4 +48,8 @@ class LikeService
|
||||||
def post_service
|
def post_service
|
||||||
@post_service ||= PostService.new(user)
|
@post_service ||= PostService.new(user)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def comment_service
|
||||||
|
@comment_service ||= CommentService.new(user)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue