Skip to content

Commit

Permalink
Adapt to latest development
Browse files Browse the repository at this point in the history
Fixing Tests

User likes

Fixing formatting issues

fixing format issues
  • Loading branch information
tclaus committed Feb 8, 2021
1 parent 1554d4f commit d04946e
Show file tree
Hide file tree
Showing 14 changed files with 222 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ group :mysql, optional: true do
gem "mysql2", "0.5.3"
end
group :postgresql, optional: true do
gem "pg", "1.2.3"
gem "pg", "1.2.3"
end


Expand Down
7 changes: 4 additions & 3 deletions app/assets/javascripts/app/collections/likes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ app.collections.Likes = Backbone.Collection.extend({
model: app.models.Like,

initialize : function(models, options) {
this.url = (options.post != null) ?
// A comment- like has a post reference and a comment reference
this.url = (options.comment != 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";
"/comments/" + options.comment.id + "/likes" :
"/posts/" + options.post.id + "/likes";
}
});
// @license-end
8 changes: 7 additions & 1 deletion app/assets/javascripts/app/models/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ app.models.Comment = Backbone.Model.extend({
urlRoot: "/comments",

initialize: function() {
this.likeInteractions = new app.models.Post.LikeInteractions(_.extend({comment: this}, this.get("likeInteractions")));
this.post = this.collection.post;
this.interactions = new app.models.Post.LikeInteractions(
_.extend({comment: this, post: this.post}, this.get("interactions"))
);
this.likes = this.interactions.likes;
this.likesCount = this.attributes.likes_count;
this.userLike = this.interactions.userLike();
}
});
// @license-end
2 changes: 1 addition & 1 deletion app/assets/javascripts/app/models/post/interactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

app.models.Post.Interactions = app.models.Post.LikeInteractions.extend({
initialize: function(options) {
app.models.Post.LikeInteractions.prototype.initialize({post: this.post}); // I don't know how to call the super method yet
app.models.Post.LikeInteractions.prototype.initialize.apply(this, arguments);
this.post = options.post;
this.comments = new app.collections.Comments(this.get("comments"), {post: this.post});
this.reshares = new app.collections.Reshares(this.get("reshares"), {post: this.post});
Expand Down
3 changes: 2 additions & 1 deletion app/assets/javascripts/app/models/post/like_interactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ app.models.Post.LikeInteractions = Backbone.Model.extend({

initialize: function(options) {
this.likes = new app.collections.Likes(this.get("likes"), options);
this.post = options.post;
},

likesCount: function() {
return this.get("likes_count");
},

userLike: function() {
return this.likes.select(function(like){
return this.likes.select(function(like) {
return like.get("author") && like.get("author").guid === app.currentUser.get("guid");
})[0];
},
Expand Down
6 changes: 4 additions & 2 deletions app/assets/javascripts/app/views/comment_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ app.views.Comment = app.views.Content.extend({
presenter: function() {
return _.extend(this.defaultPresenter(), {
canRemove: this.canRemove(),
text: app.helpers.textFormatter(this.model.get("text"), this.model.get("mentioned_people"))
text: app.helpers.textFormatter(this.model.get("text"), this.model.get("mentioned_people")),
likesCount: this.model.attributes.likesCount,
userLike: this.model.interactions.userLike()
});
},

Expand All @@ -44,7 +46,7 @@ app.views.Comment = app.views.Content.extend({

toggleLike: function(evt) {
if (evt) { evt.preventDefault(); }
this.model.toggleLike();
this.model.interactions.toggleLike();
},

likesInfoView: function() {
Expand Down
7 changes: 6 additions & 1 deletion app/controllers/likes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ def destroy
end

def index
render json: like_service.find_for_post(params[:post_id])
like = if params[:post_id]
like_service.find_for_post(params[:post_id])
else
like_service.find_for_comment(params[:comment_id])
end
render json: like
.includes(author: :profile)
.as_api_response(:backbone)
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/user/social_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def like!(target, opts={})
end
end

def like_comment!(target, opts={})
Like::Generator.new(self, target).create!(opts)
end

def participate_in_poll!(target, answer, opts={})
PollParticipation::Generator.new(self, target, answer).create!(opts).tap do
update_or_create_participation!(target)
Expand Down
20 changes: 18 additions & 2 deletions app/presenters/comment_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ def as_json(opts={})
text: message.plain_text_for_json,
author: author.as_api_response(:backbone),
created_at: created_at,
mentioned_people: mentioned_people.as_api_response(:backbone)
mentioned_people: mentioned_people.as_api_response(:backbone),
interactions: build_interactions_json
}
end

Expand All @@ -19,11 +20,26 @@ def as_api_response
author: PersonPresenter.new(author).as_api_json,
created_at: created_at,
mentioned_people: build_mentioned_people_json,
reported: current_user.present? && reports.where(user: current_user).exists?
reported: current_user.present? && reports.where(user: current_user).exists?,
interactions: build_interactions_json
}
end

def build_interactions_json
{
likes: as_api(likes),
likes_count: likes_count
}
end

def build_mentioned_people_json
mentioned_people.map {|m| PersonPresenter.new(m).as_api_json }
end

def as_api(collection)
collection.includes(author: :profile).map {|element|
element.as_api_response(:backbone)
}
end

end
4 changes: 4 additions & 0 deletions app/services/comment_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def find!(comment_guid)
Comment.find_by!(guid: comment_guid)
end

def find_by_id!(comment_id)
Comment.find_by!(id: comment_id)
end

def destroy(comment_id)
comment = Comment.find(comment_id)
if user.owns?(comment) || user.owns?(comment.parent)
Expand Down
23 changes: 21 additions & 2 deletions app/services/like_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ def create_for_post(post_id)
end

def create_for_comment(comment_id)
comment = comment_service.find!(comment_id)
user.like!(comment)
comment = comment_service.find_by_id!(comment_id)
post = post_service.find!(comment.post.id) # checks implicit for public posts
user.like_comment!(comment)
end

def destroy(like_id)
Expand All @@ -30,6 +31,13 @@ def find_for_post(post_id)
user ? likes.order(Arel.sql("author_id = #{user.person.id} DESC")) : likes
end

def find_for_comment(comment_id)
comment = comment_service.find_by_id!(comment_id)
post = post_service.find!(comment.post.id) # checks implicit for public posts
likes = comment.likes
user ? likes.order(Arel.sql("author_id = #{user.person.id} DESC")) : likes
end

def unlike_post(post_id)
likes = post_service.find!(post_id).likes
likes = likes.order(Arel.sql("author_id = #{user.person.id} DESC"))
Expand All @@ -41,6 +49,17 @@ def unlike_post(post_id)
end
end

def unlike_comment(comment_id)
likes = comment_service.find_by_id!(comment_id).likes
likes = likes.order(Arel.sql("author_id = #{user.person.id} DESC"))
if !likes.empty? && user.owns?(likes[0])
user.retract(likes[0])
true
else
false
end
end

private

attr_reader :user
Expand Down
14 changes: 7 additions & 7 deletions spec/integration/api/likes_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@
end

it "succeeds in getting post with likes" do
like_service(bob).create(@status.guid)
like_service(auth.user).create(@status.guid)
like_service(alice).create(@status.guid)
like_service(bob).create_for_post(@status.guid)
like_service(auth.user).create_for_post(@status.guid)
like_service(alice).create_for_post(@status.guid)
get(
api_v1_post_likes_path(post_id: @status.guid),
params: {access_token: access_token_minimum_scopes}
Expand Down Expand Up @@ -112,7 +112,7 @@

describe "#create" do
context "with right post id" do
it "succeeeds in liking post" do
it "succeeds in liking post" do
post(
api_v1_post_likes_path(post_id: @status.guid),
params: {access_token: access_token}
Expand Down Expand Up @@ -181,7 +181,7 @@

describe "#delete" do
before do
like_service.create(@status.guid)
like_service.create_for_post(@status.guid)
end

context "with right post id" do
Expand Down Expand Up @@ -225,7 +225,7 @@

context "with improper credentials" do
it "fails at unliking private post without private:read" do
like_service(auth_public_only.user).create(@private_status.guid)
like_service(auth_public_only.user).create_for_post(@private_status.guid)
delete(
api_v1_post_likes_path(post_id: @private_status.guid),
params: {access_token: access_token}
Expand All @@ -234,7 +234,7 @@
end

it "fails in unliking post without interactions" do
like_service(auth_minimum_scopes.user).create(@status.guid)
like_service(auth_minimum_scopes.user).create_for_post(@status.guid)
delete(
api_v1_post_likes_path(post_id: @status.guid),
params: {access_token: access_token_minimum_scopes}
Expand Down
2 changes: 1 addition & 1 deletion spec/presenters/likes_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
to: "all"
)
bobs_like_service = LikeService.new(bob)
like = bobs_like_service.create(@status.guid)
like = bobs_like_service.create_for_post(@status.guid)
@presenter = LikesPresenter.new(like, bob)
end

Expand Down

0 comments on commit d04946e

Please sign in to comment.