comments_controller.rb 2.62 KB
Newer Older
danielgrippi's avatar
danielgrippi committed
1
#   Copyright (c) 2010-2011, Diaspora Inc.  This file is
Raphael's avatar
Raphael committed
2
#   licensed under the Affero General Public License version 3 or later.  See
Raphael's avatar
Raphael committed
3
#   the COPYRIGHT file.
4

5
class CommentsController < ApplicationController
6
  include ApplicationHelper
7
  before_filter :authenticate_user!, :except => [:index]
8

danielgrippi's avatar
danielgrippi committed
9 10 11
  respond_to :html,
             :mobile,
             :json
12

13 14 15 16
  rescue_from ActiveRecord::RecordNotFound do
    render :nothing => true, :status => 404
  end

17
  def create
18
    target = current_user.find_visible_shareable_by_id Post, params[:post_id]
19
    text = params[:text]
20

21
    if target
22
      @comment = current_user.build_comment(:text => text, :post => target)
23

24
      if @comment.save
Maxwell Salzberg's avatar
Maxwell Salzberg committed
25 26
        Rails.logger.info("event => :create, :type => :comment, :user => #{current_user.diaspora_handle},
                          :status => :success, :comment => #{@comment.id}, :chars => #{params[:text].length}")
27
        Postzord::Dispatcher.build(current_user, @comment).post
28

29
        respond_to do |format|
danielgrippi's avatar
danielgrippi committed
30
          format.json{ render :json => @comment.as_api_response(:backbone), :status => 201 }
31
          format.html{ render :nothing => true, :status => 201 }
32
          format.mobile{ render :partial => 'comment', :locals => {:post => @comment.post, :comment => @comment} }
33 34
        end
      else
35
        render :nothing => true, :status => 422
36
      end
Raphael's avatar
Raphael committed
37
    else
38
      render :nothing => true, :status => 422
Raphael's avatar
Raphael committed
39
    end
40 41
  end

42
  def destroy
43 44 45 46
    @comment = Comment.find(params[:id])
    if current_user.owns?(@comment) || current_user.owns?(@comment.parent)
      current_user.retract(@comment)
      respond_to do |format|
47
        format.js { render :nothing => true, :status => 204 }
48
        format.json { render :nothing => true, :status => 204 }
49
        format.mobile{ redirect_to @comment.post }
50 51
      end
    else
52 53 54
      respond_to do |format|
        format.mobile {redirect_to :back}
        format.js {render :nothing => true, :status => 403}
55
        format.json { render :nothing => true, :status => 403 }
56
      end
57 58 59
    end
  end

60 61 62 63
  def new
    render :layout => false
  end

64
  def index
65
    if user_signed_in?
66
      @post = current_user.find_visible_shareable_by_id(Post, params[:post_id])
67 68 69 70
    else
      @post = Post.where(:id => params[:post_id], :public => true).includes(:author, :comments => :author).first
    end

71
    if @post
72
      @comments = @post.comments.includes(:author => :profile).order('created_at ASC')
danielgrippi's avatar
danielgrippi committed
73 74
      respond_with do |format|
        format.json  { render :json => @post.comments.as_api_response(:backbone), :status => 200 }
75
        format.mobile{render :layout => false}
danielgrippi's avatar
danielgrippi committed
76
      end
77 78 79 80
    else
      raise ActiveRecord::RecordNotFound.new
    end
  end
81
end