2012-06-17 03:15:46 +02:00
|
|
|
class CommentsController < ApplicationController
|
2012-09-19 20:40:02 +02:00
|
|
|
COMMENTS_PER_PAGE = 20
|
|
|
|
|
2015-04-28 16:51:50 +02: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
|
|
|
before_filter :require_logged_in_user_or_400,
|
2014-01-16 04:55:31 +01:00
|
|
|
:only => [ :create, :preview, :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
|
|
|
|
2014-11-14 21:09:28 +01:00
|
|
|
if params[:hat_id] && @user.hats.where(:id => params[:hat_id])
|
|
|
|
comment.hat_id = params[:hat_id]
|
|
|
|
end
|
|
|
|
|
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
|
2014-01-16 03:21:05 +01:00
|
|
|
comment.parent_comment = pc
|
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
|
2014-01-16 04:55:31 +01:00
|
|
|
if params[:preview].blank? &&
|
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
|
|
|
|
2014-01-16 04:55:31 +01:00
|
|
|
if comment.valid? && params[:preview].blank? && comment.save
|
2012-06-30 18:18:36 +02:00
|
|
|
comment.current_vote = { :vote => 1 }
|
|
|
|
|
2014-01-16 03:21:05 +01:00
|
|
|
render :partial => "comments/postedreply", :layout => false,
|
|
|
|
:content_type => "text/html", :locals => { :comment => comment }
|
2012-06-30 18:18:36 +02:00
|
|
|
else
|
|
|
|
comment.upvotes = 1
|
|
|
|
comment.current_vote = { :vote => 1 }
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2014-01-16 04:55:31 +01:00
|
|
|
preview comment
|
2012-06-30 18:18:36 +02:00
|
|
|
end
|
2012-06-17 03:15:46 +02:00
|
|
|
end
|
2013-02-14 01:50:51 +01:00
|
|
|
|
2014-08-16 18:37:25 +02:00
|
|
|
def show
|
|
|
|
if !((comment = find_comment) && comment.is_editable_by_user?(@user))
|
|
|
|
return render :text => "can't find comment", :status => 400
|
|
|
|
end
|
|
|
|
|
|
|
|
render :partial => "comment", :layout => false,
|
2014-12-14 07:24:18 +01:00
|
|
|
:content_type => "text/html", :locals => { :comment => comment }
|
2014-08-16 18:37:25 +02:00
|
|
|
end
|
|
|
|
|
2015-08-04 02:12:39 +02:00
|
|
|
def show_short_id
|
|
|
|
if !(comment = find_comment)
|
|
|
|
return render :text => "can't find comment", :status => 400
|
|
|
|
end
|
|
|
|
|
|
|
|
render :json => comment.as_json
|
|
|
|
end
|
|
|
|
|
2015-04-01 08:02:22 +02:00
|
|
|
def redirect_from_short_id
|
|
|
|
if comment = find_comment
|
|
|
|
return redirect_to comment.url
|
|
|
|
else
|
|
|
|
return render :text => "can't find comment", :status => 400
|
|
|
|
end
|
|
|
|
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-12-14 07:24:18 +01:00
|
|
|
:content_type => "text/html", :locals => { :comment => comment }
|
2012-07-12 20:30:20 +02:00
|
|
|
end
|
|
|
|
|
2014-01-16 03:21:05 +01:00
|
|
|
def reply
|
|
|
|
if !(parent_comment = find_comment)
|
|
|
|
return render :text => "can't find comment", :status => 400
|
|
|
|
end
|
|
|
|
|
|
|
|
comment = Comment.new
|
|
|
|
comment.story = parent_comment.story
|
|
|
|
comment.parent_comment = parent_comment
|
|
|
|
|
|
|
|
render :partial => "commentbox", :layout => false,
|
2014-12-14 07:24:18 +01:00
|
|
|
:content_type => "text/html", :locals => { :comment => comment }
|
2014-01-16 03:21:05 +01:00
|
|
|
end
|
|
|
|
|
2012-07-12 20:30:20 +02:00
|
|
|
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
|
|
|
|
|
2016-02-10 15:57:38 +01:00
|
|
|
comment.delete_for_user(@user, params[:reason])
|
2012-07-12 20:30:20 +02:00
|
|
|
|
|
|
|
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
|
2017-05-17 10:23:25 +02:00
|
|
|
|
|
|
|
def dragon
|
|
|
|
if !((comment = find_comment) && @user.is_moderator?)
|
|
|
|
return render :text => "can't find comment", :status => 400
|
|
|
|
end
|
|
|
|
|
|
|
|
comment.become_dragon_for_user(@user)
|
|
|
|
|
|
|
|
render :text => "ok"
|
|
|
|
end
|
|
|
|
|
|
|
|
def undragon
|
|
|
|
if !((comment = find_comment) && @user.is_moderator?)
|
|
|
|
return render :text => "can't find comment", :status => 400
|
|
|
|
end
|
|
|
|
|
|
|
|
comment.remove_dragon_for_user(@user)
|
|
|
|
|
|
|
|
render :text => "ok"
|
|
|
|
end
|
2012-07-05 02:55:45 +02:00
|
|
|
|
|
|
|
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]
|
2014-11-14 21:09:28 +01:00
|
|
|
comment.hat_id = nil
|
|
|
|
if params[:hat_id] && @user.hats.where(:id => params[:hat_id])
|
|
|
|
comment.hat_id = params[:hat_id]
|
|
|
|
end
|
2012-07-05 02:55:45 +02:00
|
|
|
|
2014-01-16 04:55:31 +01:00
|
|
|
if params[:preview].blank? && comment.save
|
2014-01-16 03:21:05 +01:00
|
|
|
votes = Vote.comment_votes_by_user_for_comment_ids_hash(@user.id,
|
|
|
|
[comment.id])
|
|
|
|
comment.current_vote = votes[comment.id]
|
2012-07-05 02:55:45 +02:00
|
|
|
|
2014-01-16 03:21:05 +01:00
|
|
|
render :partial => "comments/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
|
|
|
else
|
|
|
|
comment.current_vote = { :vote => 1 }
|
|
|
|
|
2014-01-16 04:55:31 +01:00
|
|
|
preview comment
|
2012-07-05 02:55:45 +02:00
|
|
|
end
|
|
|
|
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-02-17 17:13:08 +01:00
|
|
|
if !@user.can_downvote?(comment)
|
2014-01-13 06:17:09 +01:00
|
|
|
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
|
2014-03-12 23:50:51 +01:00
|
|
|
@rss_link ||= { :title => "RSS 2.0 - Newest Comments",
|
|
|
|
:href => "/comments.rss#{@user ? "?token=#{@user.rss_token}" : ""}" }
|
2013-01-14 04:39:52 +01:00
|
|
|
|
2016-11-15 17:26:01 +01:00
|
|
|
@heading = @title = I18n.t 'controllers.comments_controller.commentstitle'
|
2012-07-21 00:54:09 +02:00
|
|
|
@cur_url = "/comments"
|
|
|
|
|
2016-07-31 19:36:28 +02:00
|
|
|
@page = params[:page].to_i
|
|
|
|
if @page == 0
|
|
|
|
@page = 1
|
|
|
|
elsif @page < 0 || @page > (2 ** 32)
|
|
|
|
raise ActionController::RoutingError.new("page out of bounds")
|
2012-09-19 20:40:02 +02:00
|
|
|
end
|
|
|
|
|
2013-12-24 04:20:06 +01:00
|
|
|
@comments = Comment.where(
|
|
|
|
:is_deleted => false, :is_moderated => false
|
|
|
|
).order(
|
2017-05-23 10:30:02 +02:00
|
|
|
"id DESC"
|
2013-12-24 04:20:06 +01:00
|
|
|
).offset(
|
|
|
|
(@page - 1) * COMMENTS_PER_PAGE
|
|
|
|
).limit(
|
|
|
|
COMMENTS_PER_PAGE
|
|
|
|
).includes(
|
|
|
|
:user, :story
|
|
|
|
)
|
2012-07-21 00:54:09 +02:00
|
|
|
|
|
|
|
if @user
|
2015-03-13 06:31:34 +01:00
|
|
|
@comments = @comments.where("story_id NOT IN (SELECT story_id FROM " <<
|
|
|
|
"hidden_stories WHERE user_id = ?)", @user.id)
|
|
|
|
|
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
|
2015-04-28 16:51:50 +02:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { render :action => "index" }
|
|
|
|
format.rss {
|
|
|
|
if @user && params[:token].present?
|
|
|
|
@title = "Private comments feed for #{@user.username}"
|
|
|
|
end
|
|
|
|
|
|
|
|
render :action => "index.rss", :layout => false
|
|
|
|
}
|
|
|
|
end
|
2012-07-21 00:54:09 +02:00
|
|
|
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
|
2016-11-15 17:26:01 +01:00
|
|
|
@heading = @title = I18n.t 'controllers.comments_controller.threadstitle'
|
2012-09-04 00:11:53 +02:00
|
|
|
@cur_url = "/threads"
|
|
|
|
end
|
2012-07-11 22:34:53 +02:00
|
|
|
|
2015-12-18 17:51:01 +01:00
|
|
|
thread_ids = @showing_user.recent_threads(20,
|
|
|
|
include_submitted_stories = !!(@user && @user.id == @showing_user.id))
|
2012-07-03 21:29:00 +02:00
|
|
|
|
2014-01-16 03:17:32 +01:00
|
|
|
comments = Comment.where(
|
|
|
|
:thread_id => thread_ids
|
|
|
|
).includes(
|
2017-05-20 15:36:33 +02:00
|
|
|
:user, :story, :hat, :votes => :user
|
2014-01-16 03:17:32 +01:00
|
|
|
).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
|
|
|
|
|
2017-05-16 19:36:52 +02:00
|
|
|
if @user
|
2014-01-16 03:17:32 +01:00
|
|
|
@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
|
|
|
|
|
2014-01-16 04:55:31 +01:00
|
|
|
def preview(comment)
|
|
|
|
comment.previewing = true
|
|
|
|
comment.is_deleted = false # show normal preview for deleted comments
|
|
|
|
|
|
|
|
render :partial => "comments/commentbox", :layout => false,
|
|
|
|
:content_type => "text/html", :locals => {
|
|
|
|
:comment => comment, :show_comment => comment }
|
|
|
|
end
|
|
|
|
|
2013-12-24 04:20:06 +01:00
|
|
|
def find_comment
|
2014-02-17 17:13:08 +01:00
|
|
|
comment = Comment.where(:short_id => params[:id]).first
|
|
|
|
if @user && comment
|
|
|
|
comment.current_vote = Vote.where(:user_id => @user.id,
|
|
|
|
:story_id => comment.story_id, :comment_id => comment.id).first
|
|
|
|
end
|
|
|
|
|
|
|
|
comment
|
2013-12-24 04:20:06 +01:00
|
|
|
end
|
2012-06-17 03:15:46 +02:00
|
|
|
end
|