2012-06-17 03:15:46 +02:00
|
|
|
class CommentsController < ApplicationController
|
|
|
|
before_filter :require_logged_in_user_or_400,
|
|
|
|
:only => [ :create, :preview, :upvote, :downvote, :unvote ]
|
|
|
|
|
|
|
|
def create
|
2012-07-05 02:33:12 +02:00
|
|
|
if !(story = Story.find_by_short_id(params[:story_id])) || story.is_gone?
|
2012-06-17 03:15:46 +02:00
|
|
|
return render :text => "can't find story", :status => 400
|
|
|
|
end
|
|
|
|
|
|
|
|
comment = Comment.new
|
|
|
|
comment.comment = params[:comment].to_s
|
|
|
|
comment.story_id = story.id
|
|
|
|
comment.user_id = @user.id
|
|
|
|
|
|
|
|
if params[:parent_comment_short_id]
|
|
|
|
if pc = Comment.find_by_story_id_and_short_id(story.id,
|
|
|
|
params[:parent_comment_short_id])
|
|
|
|
comment.parent_comment_id = pc.id
|
2012-06-30 18:18:36 +02:00
|
|
|
comment.parent_comment_short_id = pc.short_id
|
2012-06-30 21:14:35 +02:00
|
|
|
comment.thread_id = pc.thread_id
|
2012-06-17 03:15:46 +02:00
|
|
|
else
|
|
|
|
return render :json => { :error => "invalid parent comment",
|
|
|
|
:status => 400 }
|
|
|
|
end
|
2012-06-30 21:14:35 +02:00
|
|
|
else
|
|
|
|
comment.thread_id = Keystore.incremented_value_for("thread_id")
|
2012-06-17 03:15:46 +02:00
|
|
|
end
|
|
|
|
|
2012-06-30 18:18:36 +02:00
|
|
|
if comment.valid? && !params[:preview].present? && comment.save
|
|
|
|
comment.current_vote = { :vote => 1 }
|
|
|
|
|
2012-06-30 21:14:35 +02:00
|
|
|
if comment.parent_comment_id
|
|
|
|
render :partial => "postedreply", :layout => false,
|
|
|
|
:content_type => "text/html", :locals => { :story => story,
|
|
|
|
:show_comment => comment }
|
|
|
|
else
|
|
|
|
render :partial => "commentbox", :layout => false,
|
|
|
|
:content_type => "text/html", :locals => { :story => story,
|
|
|
|
:comment => Comment.new, :show_comment => comment }
|
|
|
|
end
|
2012-08-12 17:49:53 +02:00
|
|
|
|
|
|
|
Countinual.count!("lobsters.comments.submitted", "+1")
|
2012-06-30 18:18:36 +02:00
|
|
|
else
|
|
|
|
comment.previewing = true
|
|
|
|
comment.upvotes = 1
|
|
|
|
comment.current_vote = { :vote => 1 }
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2012-06-30 21:14:35 +02:00
|
|
|
render :partial => "commentbox", :layout => false,
|
2012-06-30 18:18:36 +02:00
|
|
|
:content_type => "text/html", :locals => { :story => story,
|
|
|
|
:comment => comment, :show_comment => comment }
|
|
|
|
end
|
2012-06-17 03:15:46 +02:00
|
|
|
end
|
2012-07-05 02:55:45 +02:00
|
|
|
|
|
|
|
def preview_new
|
2012-06-17 03:15:46 +02:00
|
|
|
params[:preview] = true
|
|
|
|
return create
|
|
|
|
end
|
|
|
|
|
2012-07-05 02:55:45 +02:00
|
|
|
def edit
|
|
|
|
if !((comment = Comment.find_by_short_id(params[:comment_id])) &&
|
|
|
|
comment.is_editable_by_user?(@user))
|
|
|
|
return render :text => "can't find comment", :status => 400
|
|
|
|
end
|
|
|
|
|
|
|
|
render :partial => "commentbox", :layout => false,
|
|
|
|
:content_type => "text/html", :locals => { :story => comment.story,
|
|
|
|
:comment => comment }
|
2012-07-12 20:30:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete
|
|
|
|
if !((comment = Comment.find_by_short_id(params[:comment_id])) &&
|
2012-09-02 16:50:07 +02:00
|
|
|
comment.is_deletable_by_user?(@user))
|
2012-07-12 20:30:20 +02:00
|
|
|
return render :text => "can't find comment", :status => 400
|
|
|
|
end
|
|
|
|
|
|
|
|
comment.delete_for_user(@user)
|
|
|
|
|
|
|
|
render :partial => "comment", :layout => false,
|
|
|
|
:content_type => "text/html", :locals => { :story => comment.story,
|
|
|
|
:comment => comment }
|
|
|
|
end
|
|
|
|
|
|
|
|
def undelete
|
|
|
|
if !((comment = Comment.find_by_short_id(params[:comment_id])) &&
|
|
|
|
comment.is_undeletable_by_user?(@user))
|
|
|
|
return render :text => "can't find comment", :status => 400
|
|
|
|
end
|
|
|
|
|
|
|
|
comment.undelete_for_user(@user)
|
|
|
|
|
|
|
|
render :partial => "comment", :layout => false,
|
|
|
|
:content_type => "text/html", :locals => { :story => comment.story,
|
|
|
|
:comment => comment }
|
2012-07-05 02:55:45 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
if !((comment = Comment.find_by_short_id(params[:comment_id])) &&
|
|
|
|
comment.is_editable_by_user?(@user))
|
|
|
|
return render :text => "can't find comment", :status => 400
|
|
|
|
end
|
|
|
|
|
|
|
|
comment.comment = params[:comment]
|
|
|
|
|
|
|
|
if comment.save
|
|
|
|
# TODO: render the comment again properly, it's indented wrong
|
|
|
|
|
|
|
|
render :partial => "postedreply", :layout => false,
|
|
|
|
:content_type => "text/html", :locals => { :story => comment.story,
|
|
|
|
:show_comment => comment }
|
|
|
|
else
|
|
|
|
comment.previewing = true
|
|
|
|
comment.current_vote = { :vote => 1 }
|
|
|
|
|
|
|
|
render :partial => "commentbox", :layout => false,
|
|
|
|
:content_type => "text/html", :locals => { :story => comment.story,
|
|
|
|
:comment => comment, :show_comment => comment }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def preview
|
|
|
|
if !((comment = Comment.find_by_short_id(params[:comment_id])) &&
|
|
|
|
comment.is_editable_by_user?(@user))
|
|
|
|
return render :text => "can't find comment", :status => 400
|
|
|
|
end
|
|
|
|
|
|
|
|
comment.comment = params[:comment]
|
|
|
|
|
|
|
|
comment.previewing = true
|
|
|
|
comment.current_vote = { :vote => 1 }
|
|
|
|
|
|
|
|
render :partial => "commentbox", :layout => false,
|
|
|
|
:content_type => "text/html", :locals => { :story => comment.story,
|
|
|
|
:comment => comment, :show_comment => comment }
|
|
|
|
end
|
|
|
|
|
2012-06-17 03:15:46 +02:00
|
|
|
def unvote
|
|
|
|
if !(comment = Comment.find_by_short_id(params[:comment_id]))
|
|
|
|
return render :text => "can't find comment", :status => 400
|
|
|
|
end
|
|
|
|
|
|
|
|
Vote.vote_thusly_on_story_or_comment_for_user_because(0, comment.story_id,
|
|
|
|
comment.id, @user.id, nil)
|
|
|
|
|
|
|
|
render :text => "ok"
|
|
|
|
end
|
|
|
|
|
|
|
|
def upvote
|
|
|
|
if !(comment = Comment.find_by_short_id(params[:comment_id]))
|
|
|
|
return render :text => "can't find comment", :status => 400
|
|
|
|
end
|
|
|
|
|
|
|
|
Vote.vote_thusly_on_story_or_comment_for_user_because(1, comment.story_id,
|
|
|
|
comment.id, @user.id, params[:reason])
|
|
|
|
|
|
|
|
render :text => "ok"
|
|
|
|
end
|
|
|
|
|
|
|
|
def downvote
|
|
|
|
if !(comment = Comment.find_by_short_id(params[:comment_id]))
|
|
|
|
return render :text => "can't find comment", :status => 400
|
|
|
|
end
|
|
|
|
|
|
|
|
if !Vote::COMMENT_REASONS[params[:reason]]
|
|
|
|
return render :text => "invalid reason", :status => 400
|
|
|
|
end
|
|
|
|
|
|
|
|
Vote.vote_thusly_on_story_or_comment_for_user_because(-1, comment.story_id,
|
|
|
|
comment.id, @user.id, params[:reason])
|
|
|
|
|
|
|
|
render :text => "ok"
|
|
|
|
end
|
2012-06-30 21:14:35 +02:00
|
|
|
|
2012-07-21 00:54:09 +02:00
|
|
|
def index
|
2012-09-07 16:18:15 +02:00
|
|
|
@heading = @title = "Newest Comments"
|
2012-07-21 00:54:09 +02:00
|
|
|
@cur_url = "/comments"
|
|
|
|
|
2012-09-02 16:50:07 +02:00
|
|
|
@comments = Comment.find(:all, :conditions => "is_deleted = 0",
|
|
|
|
:order => "created_at DESC", :limit => 20, :include => [ :user, :story ])
|
2012-07-21 00:54:09 +02:00
|
|
|
|
|
|
|
if @user
|
2012-07-21 00:58:01 +02:00
|
|
|
@votes = Vote.comment_votes_by_user_for_comment_ids_hash(@user.id,
|
|
|
|
@comments.map{|c| c.id })
|
2012-07-21 00:54:09 +02:00
|
|
|
|
|
|
|
@comments.each do |c|
|
|
|
|
if @votes[c.id]
|
|
|
|
c.current_vote = @votes[c.id]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-30 21:14:35 +02:00
|
|
|
def threads
|
2012-09-04 00:11:53 +02:00
|
|
|
if params[:user]
|
|
|
|
@showing_user = User.find_by_username!(params[:user])
|
2012-09-07 16:18:15 +02:00
|
|
|
@heading = @title = "Threads for #{@showing_user.username}"
|
2012-09-04 00:11:53 +02:00
|
|
|
@cur_url = "/threads/#{@showing_user.username}"
|
|
|
|
elsif !@user
|
|
|
|
# TODO: show all recent threads
|
|
|
|
return redirect_to "/login"
|
|
|
|
else
|
|
|
|
@showing_user = @user
|
2012-09-07 16:18:15 +02:00
|
|
|
@heading = @title = "Your Threads"
|
2012-09-04 00:11:53 +02:00
|
|
|
@cur_url = "/threads"
|
|
|
|
end
|
2012-07-11 22:34:53 +02:00
|
|
|
|
2012-09-04 00:11:53 +02:00
|
|
|
@threads = @showing_user.recent_threads(20).map{|r|
|
|
|
|
cs = Comment.ordered_for_story_or_thread_for_user(nil, r, @showing_user)
|
2012-07-03 21:29:00 +02:00
|
|
|
|
2012-09-04 00:11:53 +02:00
|
|
|
if @user && (@showing_user.id == @user.id)
|
|
|
|
@votes = Vote.comment_votes_by_user_for_story_hash(@user.id,
|
|
|
|
cs.map{|c| c.story_id }.uniq)
|
2012-07-03 21:29:00 +02:00
|
|
|
|
2012-09-04 00:11:53 +02:00
|
|
|
cs.each do |c|
|
|
|
|
if @votes[c.id]
|
|
|
|
c.current_vote = @votes[c.id]
|
|
|
|
end
|
2012-07-03 21:29:00 +02:00
|
|
|
end
|
2012-09-04 00:11:53 +02:00
|
|
|
else
|
|
|
|
@votes = []
|
2012-07-03 21:29:00 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
cs
|
|
|
|
}
|
2012-06-30 21:14:35 +02:00
|
|
|
|
|
|
|
# trim each thread to this user's first response
|
2012-07-11 21:32:04 +02:00
|
|
|
# XXX: busted
|
|
|
|
if false
|
2012-06-30 21:14:35 +02:00
|
|
|
@threads.map!{|th|
|
|
|
|
th.each do |c|
|
|
|
|
if c.user_id == @user.id
|
|
|
|
break
|
|
|
|
else
|
|
|
|
th.shift
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
th
|
|
|
|
}
|
2012-07-11 21:32:04 +02:00
|
|
|
end
|
|
|
|
|
2012-06-30 21:14:35 +02:00
|
|
|
@comments = @threads.flatten
|
|
|
|
end
|
2012-06-17 03:15:46 +02:00
|
|
|
end
|