2012-06-17 03:15:46 +02:00
|
|
|
class CommentsController < ApplicationController
|
2012-09-19 20:40:02 +02:00
|
|
|
COMMENTS_PER_PAGE = 20
|
|
|
|
|
2012-06-17 03:15:46 +02:00
|
|
|
before_filter :require_logged_in_user_or_400,
|
2012-12-30 18:47:43 +01:00
|
|
|
:only => [ :create, :preview, :preview_new, :upvote, :downvote, :unvote ]
|
2013-02-14 01:50:51 +01:00
|
|
|
|
2013-01-14 04:39:52 +01:00
|
|
|
# for rss feeds, load the user's tag filters if a token is passed
|
|
|
|
before_filter :find_user_from_rss_token, :only => [ :index ]
|
2012-06-17 03:15:46 +02:00
|
|
|
|
|
|
|
def create
|
2014-01-13 06:17:09 +01:00
|
|
|
if !(story = Story.where(:short_id => params[:story_id]).first) ||
|
|
|
|
story.is_gone?
|
2012-06-17 03:15:46 +02:00
|
|
|
return render :text => "can't find story", :status => 400
|
|
|
|
end
|
|
|
|
|
2014-01-13 08:11:34 +01:00
|
|
|
comment = story.comments.build
|
2012-06-17 03:15:46 +02:00
|
|
|
comment.comment = params[:comment].to_s
|
2014-01-13 08:11:34 +01:00
|
|
|
comment.user = @user
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2013-02-08 17:39:51 +01:00
|
|
|
if params[:parent_comment_short_id].present?
|
2013-12-24 04:20:06 +01:00
|
|
|
if pc = Comment.where(:story_id => story.id, :short_id =>
|
|
|
|
params[:parent_comment_short_id]).first
|
2012-06-17 03:15:46 +02:00
|
|
|
comment.parent_comment_id = pc.id
|
2013-06-25 20:55:40 +02:00
|
|
|
# needed for carryng along in comment preview form
|
|
|
|
comment.parent_comment_short_id = params[:parent_comment_short_id]
|
2012-06-17 03:15:46 +02:00
|
|
|
else
|
|
|
|
return render :json => { :error => "invalid parent comment",
|
|
|
|
:status => 400 }
|
|
|
|
end
|
|
|
|
end
|
2012-09-14 23:17:45 +02:00
|
|
|
|
|
|
|
# prevent double-clicks of the post button
|
|
|
|
if !params[:preview].present? &&
|
2013-12-24 04:20:06 +01:00
|
|
|
(pc = Comment.where(:story_id => story.id, :user_id => @user.id,
|
|
|
|
:parent_comment_id => comment.parent_comment_id).first)
|
2012-09-14 23:17:45 +02:00
|
|
|
if (Time.now - pc.created_at) < 5.minutes
|
|
|
|
comment.errors.add(:comment, "^You have already posted a comment " <<
|
|
|
|
"here recently.")
|
|
|
|
|
|
|
|
return render :partial => "commentbox", :layout => false,
|
2014-01-13 08:11:34 +01:00
|
|
|
:content_type => "text/html", :locals => { :comment => comment }
|
2012-09-14 23:17:45 +02:00
|
|
|
end
|
|
|
|
end
|
2012-06-17 03:15:46 +02:00
|
|
|
|
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,
|
2014-01-13 08:11:34 +01:00
|
|
|
:content_type => "text/html", :locals => { :comment => comment }
|
2012-06-30 21:14:35 +02:00
|
|
|
else
|
|
|
|
render :partial => "commentbox", :layout => false,
|
2014-01-13 08:11:34 +01:00
|
|
|
:content_type => "text/html", :locals => {
|
|
|
|
:comment => story.comments.build, :show_comment => comment }
|
2012-06-30 21:14:35 +02:00
|
|
|
end
|
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,
|
2014-01-13 08:11:34 +01:00
|
|
|
:content_type => "text/html", :locals => {
|
2012-06-30 18:18:36 +02:00
|
|
|
:comment => comment, :show_comment => comment }
|
|
|
|
end
|
2012-06-17 03:15:46 +02:00
|
|
|
end
|
2013-02-14 01:50:51 +01:00
|
|
|
|
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
|
2013-12-24 04:20:06 +01:00
|
|
|
if !((comment = find_comment) && comment.is_editable_by_user?(@user))
|
2012-07-05 02:55:45 +02:00
|
|
|
return render :text => "can't find comment", :status => 400
|
|
|
|
end
|
|
|
|
|
|
|
|
render :partial => "commentbox", :layout => false,
|
2014-01-13 08:11:34 +01:00
|
|
|
:content_type => "text/html", :locals => { :comment => comment }
|
2012-07-12 20:30:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete
|
2013-12-24 04:20:06 +01:00
|
|
|
if !((comment = find_comment) && 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,
|
2014-01-13 08:11:34 +01:00
|
|
|
:content_type => "text/html", :locals => { :comment => comment }
|
2012-07-12 20:30:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def undelete
|
2013-12-24 04:20:06 +01:00
|
|
|
if !((comment = find_comment) && comment.is_undeletable_by_user?(@user))
|
2012-07-12 20:30:20 +02:00
|
|
|
return render :text => "can't find comment", :status => 400
|
|
|
|
end
|
|
|
|
|
|
|
|
comment.undelete_for_user(@user)
|
|
|
|
|
|
|
|
render :partial => "comment", :layout => false,
|
2014-01-13 08:11:34 +01:00
|
|
|
:content_type => "text/html", :locals => { :comment => comment }
|
2012-07-05 02:55:45 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2013-12-24 04:20:06 +01:00
|
|
|
if !((comment = find_comment) && comment.is_editable_by_user?(@user))
|
2012-07-05 02:55:45 +02:00
|
|
|
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,
|
2014-01-13 08:11:34 +01:00
|
|
|
:content_type => "text/html", :locals => { :comment => comment }
|
2012-07-05 02:55:45 +02:00
|
|
|
else
|
|
|
|
comment.previewing = true
|
|
|
|
comment.current_vote = { :vote => 1 }
|
|
|
|
|
|
|
|
render :partial => "commentbox", :layout => false,
|
2014-01-13 08:11:34 +01:00
|
|
|
:content_type => "text/html", :locals => {
|
2012-07-05 02:55:45 +02:00
|
|
|
:comment => comment, :show_comment => comment }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def preview
|
2013-12-24 04:20:06 +01:00
|
|
|
if !((comment = find_comment) && comment.is_editable_by_user?(@user))
|
2012-07-05 02:55:45 +02:00
|
|
|
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,
|
2014-01-13 08:11:34 +01:00
|
|
|
:content_type => "text/html", :locals => {
|
2012-07-05 02:55:45 +02:00
|
|
|
:comment => comment, :show_comment => comment }
|
|
|
|
end
|
|
|
|
|
2012-06-17 03:15:46 +02:00
|
|
|
def unvote
|
2013-12-24 04:20:06 +01:00
|
|
|
if !(comment = find_comment)
|
2012-06-17 03:15:46 +02:00
|
|
|
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
|
2013-12-24 04:20:06 +01:00
|
|
|
if !(comment = find_comment)
|
2012-06-17 03:15:46 +02:00
|
|
|
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
|
2013-12-24 04:20:06 +01:00
|
|
|
if !(comment = find_comment)
|
2012-06-17 03:15:46 +02:00
|
|
|
return render :text => "can't find comment", :status => 400
|
|
|
|
end
|
2013-02-14 01:50:51 +01:00
|
|
|
|
2012-06-17 03:15:46 +02:00
|
|
|
if !Vote::COMMENT_REASONS[params[:reason]]
|
|
|
|
return render :text => "invalid reason", :status => 400
|
|
|
|
end
|
|
|
|
|
2014-01-13 06:17:09 +01:00
|
|
|
if !@user.can_downvote?
|
|
|
|
return render :text => "not permitted to downvote", :status => 400
|
|
|
|
end
|
|
|
|
|
2012-06-17 03:15:46 +02:00
|
|
|
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
|
2013-01-14 04:39:52 +01:00
|
|
|
@rss_link ||= "<link rel=\"alternate\" type=\"application/rss+xml\" " <<
|
|
|
|
"title=\"RSS 2.0\" href=\"/comments.rss" <<
|
|
|
|
(@user ? "?token=#{@user.rss_token}" : "") << "\" />"
|
|
|
|
|
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-19 20:40:02 +02:00
|
|
|
@page = 1
|
|
|
|
if params[:page].to_i > 0
|
|
|
|
@page = params[:page].to_i
|
|
|
|
end
|
|
|
|
|
2013-12-24 04:20:06 +01:00
|
|
|
@comments = Comment.where(
|
|
|
|
:is_deleted => false, :is_moderated => false
|
|
|
|
).order(
|
|
|
|
"created_at DESC"
|
|
|
|
).offset(
|
|
|
|
(@page - 1) * COMMENTS_PER_PAGE
|
|
|
|
).limit(
|
|
|
|
COMMENTS_PER_PAGE
|
|
|
|
).includes(
|
|
|
|
: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]
|
2013-12-24 04:20:06 +01:00
|
|
|
@showing_user = User.where(:username => params[:user]).first!
|
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
|
|
|
|
2014-01-16 03:17:32 +01:00
|
|
|
thread_ids = @showing_user.recent_threads(20)
|
2012-07-03 21:29:00 +02:00
|
|
|
|
2014-01-16 03:17:32 +01:00
|
|
|
comments = Comment.where(
|
|
|
|
:thread_id => thread_ids
|
|
|
|
).includes(
|
|
|
|
:user, :story
|
|
|
|
).arrange_for_user(
|
|
|
|
@showing_user
|
|
|
|
)
|
2012-06-30 21:14:35 +02:00
|
|
|
|
2014-01-16 03:17:32 +01:00
|
|
|
comments_by_thread_id = comments.group_by(&:thread_id)
|
|
|
|
@threads = comments_by_thread_id.values_at(*thread_ids).compact
|
|
|
|
|
|
|
|
if @user && (@showing_user.id == @user.id)
|
|
|
|
@votes = Vote.comment_votes_by_user_for_story_hash(@user.id,
|
|
|
|
comments.map(&:story_id).uniq)
|
|
|
|
|
|
|
|
comments.each do |c|
|
|
|
|
if @votes[c.id]
|
|
|
|
c.current_vote = @votes[c.id]
|
2012-06-30 21:14:35 +02:00
|
|
|
end
|
|
|
|
end
|
2014-01-16 03:17:32 +01:00
|
|
|
end
|
2012-06-30 21:14:35 +02:00
|
|
|
|
2014-01-16 03:17:32 +01:00
|
|
|
# trim each thread to this user's first response
|
|
|
|
# XXX: busted
|
|
|
|
#@threads.each do |th|
|
|
|
|
# th.each do |c|
|
|
|
|
# if c.user_id == @user.id
|
|
|
|
# break
|
|
|
|
# else
|
|
|
|
# th.shift
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#end
|
2012-06-30 21:14:35 +02:00
|
|
|
end
|
2013-12-24 04:20:06 +01:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def find_comment
|
|
|
|
Comment.where(:short_id => params[:comment_id]).first
|
|
|
|
end
|
2012-06-17 03:15:46 +02:00
|
|
|
end
|