From 9c0f8cdf1190e5244bc97d34821688b674cfcfff Mon Sep 17 00:00:00 2001 From: Thorsten Claus Date: Sun, 4 Sep 2022 17:19:00 +0200 Subject: [PATCH] Dont send all likes with list of comments Due to historic reasons with a comment the list of all likes was sent to the frontend. This is needed just to detect if one of the likes is current users like. So if sending just the own like, the frontend can do it's job. When the frontend is refactured in any way, post and comment like handling should be improved. --- app/assets/templates/comment_tpl.jst.hbs | 21 ++++++++++--------- app/controllers/comments_controller.rb | 2 +- app/presenters/comment_presenter.rb | 20 ++++++++++++++---- .../last_three_comments_decorator.rb | 11 +++++++--- 4 files changed, 36 insertions(+), 18 deletions(-) diff --git a/app/assets/templates/comment_tpl.jst.hbs b/app/assets/templates/comment_tpl.jst.hbs index 4d58f45e4..f515e0c45 100644 --- a/app/assets/templates/comment_tpl.jst.hbs +++ b/app/assets/templates/comment_tpl.jst.hbs @@ -37,16 +37,17 @@ {{{text}}} -
- -
- + {{#if loggedIn}} +
+ +
+ {{/if}}
diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 647d42e35..9f98dce2c 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -49,7 +49,7 @@ class CommentsController < ApplicationController def index comments = comment_service.find_for_post(params[:post_id]) respond_with do |format| - format.json { render json: CommentPresenter.as_collection(comments), status: 200 } + format.json { render json: CommentPresenter.as_collection(comments, :as_json, current_user), status: 200 } format.mobile { render layout: false, locals: {comments: comments} } end end diff --git a/app/presenters/comment_presenter.rb b/app/presenters/comment_presenter.rb index ae118a9d4..1dc078e2e 100644 --- a/app/presenters/comment_presenter.rb +++ b/app/presenters/comment_presenter.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class CommentPresenter < BasePresenter - def as_json(opts={}) + def as_json(_opts={}) { id: id, guid: guid, @@ -27,14 +27,14 @@ class CommentPresenter < BasePresenter def build_interaction_state { - liked: current_user.present? && likes.where(author: current_user.person).exists?, - likes_count: likes_count + liked: current_user.present? && likes.exists?(author: current_user.person), + likes_count: likes_count } end def build_interactions_json { - likes: as_api(likes), + likes: as_api(own_likes(likes)), likes_count: likes_count } end @@ -43,6 +43,18 @@ class CommentPresenter < BasePresenter mentioned_people.map {|m| PersonPresenter.new(m).as_api_json } end + # TODO: Only send the own_like boolean. + # Frontend uses the same methods for post-likes as for comment-likes + # Whenever the frontend will be refactored, just send the own_like boolean, instead of a full list of likes + # The list of likes is already send when API requests the full list. + def own_likes(likes) + if current_user + likes.where(author: current_user.person) + else + likes.none + end + end + def as_api(collection) collection.includes(author: :profile).map {|element| element.as_api_response(:backbone) diff --git a/app/presenters/last_three_comments_decorator.rb b/app/presenters/last_three_comments_decorator.rb index 9812d11d5..ad098f5e3 100644 --- a/app/presenters/last_three_comments_decorator.rb +++ b/app/presenters/last_three_comments_decorator.rb @@ -5,9 +5,14 @@ class LastThreeCommentsDecorator @presenter = presenter end - def as_json(options={}) + def as_json(_options={}) + current_user = @presenter.current_user @presenter.as_json.tap do |post| - post[:interactions].merge!(:comments => CommentPresenter.as_collection(@presenter.post.last_three_comments)) + post[:interactions].merge!(comments: CommentPresenter.as_collection( + @presenter.post.last_three_comments, + :as_json, + current_user + )) end end -end \ No newline at end of file +end