Skip to content
Extraits de code Groupes Projets
likes_controller.rb 1,9 ko
Newer Older
  • Learn to ignore specific revisions
  • danielgrippi's avatar
    danielgrippi a validé
    #   Copyright (c) 2010-2011, Diaspora Inc.  This file is
    
    #   licensed under the Affero General Public License version 3 or later.  See
    #   the COPYRIGHT file.
    
    class LikesController < ApplicationController
      include ApplicationHelper
    
      before_action :authenticate_user!
    
    danielgrippi's avatar
    danielgrippi a validé
      respond_to :html,
                 :mobile,
                 :json
    
    Marco Colli's avatar
    Marco Colli a validé
        begin
          @like = if target
            current_user.like!(target)
          end
    
        rescue ActiveRecord::RecordNotFound, ActiveRecord::RecordInvalid => e
    
    Marco Colli's avatar
    Marco Colli a validé
          # do nothing
        end
    
    danielgrippi's avatar
    danielgrippi a validé
        if @like
          respond_to do |format|
            format.html { render :nothing => true, :status => 201 }
            format.mobile { redirect_to post_path(@like.post_id) }
    
            format.json { render :json => @like.as_api_response(:backbone), :status => 201 }
    
          render text: I18n.t("likes.create.error"), status: 422
    
        begin
          @like = Like.find_by_id_and_author_id!(params[:id], current_user.person.id)
        rescue ActiveRecord::RecordNotFound
          render text: I18n.t("likes.destroy.error"), status: 404
          return
        end
    
    danielgrippi's avatar
    danielgrippi a validé
    
    
        current_user.retract(@like)
        respond_to do |format|
          format.json { render :nothing => true, :status => 204 }
    
      #I can go when the old stream goes.
    
    Raphael Sofaer's avatar
    Raphael Sofaer a validé
      def index
    
        @likes = target.likes.includes(:author => :profile)
        @people = @likes.map(&:author)
    
        respond_to do |format|
          format.all { render :layout => false }
          format.json { render :json => @likes.as_api_response(:backbone) }
    
    Raphael Sofaer's avatar
    Raphael Sofaer a validé
        end
      end
    
      def target
        @target ||= if params[:post_id]
    
          current_user.find_visible_shareable_by_id(Post, params[:post_id]) || raise(ActiveRecord::RecordNotFound.new)
    
          Comment.find(params[:comment_id]).tap do |comment|
           raise(ActiveRecord::RecordNotFound.new) unless current_user.find_visible_shareable_by_id(Post, comment.commentable_id)
          end