journalduhacker/app/controllers/stories_controller.rb

215 lines
5.1 KiB
Ruby
Raw Normal View History

class StoriesController < ApplicationController
before_filter :require_logged_in_user_or_400,
:only => [ :upvote, :downvote, :unvote ]
before_filter :require_logged_in_user, :only => [ :delete, :create, :edit,
:fetch_url_title, :new ]
before_filter :find_story, :only => [ :destroy, :edit, :undelete, :update ]
def create
@page_title = "Submit a New Story"
# we don't allow the url to be changed, so we have to set it manually
@story = Story.new(params[:story].reject{|k,v| k == "url" })
@story.url = params[:story][:url]
@story.user_id = @user.id
if @story.save
2012-07-01 00:43:45 +02:00
Vote.vote_thusly_on_story_or_comment_for_user_because(1, @story.id,
nil, @user.id, nil)
return redirect_to @story.comments_url
else
if @story.already_posted_story
# consider it an upvote
Vote.vote_thusly_on_story_or_comment_for_user_because(1,
@story.already_posted_story.id, nil, @user.id, nil)
2012-07-04 05:31:42 +02:00
flash[:success] = "This URL has already been submitted recently."
2012-06-30 21:14:35 +02:00
return redirect_to @story.already_posted_story.comments_url
end
return render :action => "new"
end
end
def destroy
@story.is_expired = true
@story.save(:validate => false)
redirect_to @story.comments_url
end
def edit
@page_title = "Edit Story"
2012-06-30 21:14:35 +02:00
if !@story.is_editable_by_user?(@user)
2012-07-04 05:31:42 +02:00
flash[:error] = "You cannot edit that story."
2012-06-30 21:14:35 +02:00
return redirect_to "/"
end
end
def fetch_url_title
2012-07-01 00:45:57 +02:00
s = Story.new
s.url = params[:fetch_url]
2012-07-01 00:45:57 +02:00
if (title = s.fetched_title(request.remote_ip)).present?
return render :json => { :title => title }
else
return render :json => "error"
end
end
def new
@page_title = "Submit a New Story"
@story = Story.new
@story.story_type = "link"
if !params[:url].blank?
@story.url = params[:url]
if !params[:title].blank?
@story.title = params[:title]
end
end
end
2012-07-03 19:12:20 +02:00
def show
@story = Story.find_by_short_id!(params[:id])
@page_title = @story.title
2012-07-03 19:12:20 +02:00
@comments = Comment.ordered_for_story_or_thread_for_user(@story.id, nil,
@user ? @user.id : nil)
@comment = Comment.new
2012-07-03 19:12:20 +02:00
if @user
if v = Vote.find_by_user_id_and_story_id_and_comment_id(@user.id,
@story.id, nil)
2012-07-03 19:12:20 +02:00
@story.vote = v.vote
end
2012-07-03 19:12:20 +02:00
@votes = Vote.comment_votes_by_user_for_story_hash(@user.id, @story.id)
@comments.each do |c|
if @votes[c.id]
c.current_vote = @votes[c.id]
end
end
2012-07-03 19:12:20 +02:00
end
end
def show_comment
@story = Story.find_by_short_id!(params[:id])
@page_title = @story.title
@showing_comment = Comment.find_by_short_id(params[:comment_short_id])
if !@showing_comment
flash[:error] = "Could not find comment. It may have been deleted."
return redirect_to @story.comments_url
end
@comments = Comment.ordered_for_story_or_thread_for_user(@story.id,
@showing_comment.thread_id, @user ? @user.id : nil)
@comments.each do |c,x|
if c.id == @showing_comment.id
c.highlighted = true
break
end
end
@comment = Comment.new
if @user
if v = Vote.find_by_user_id_and_story_id(@user.id, @story.id)
@story.vote = v.vote
end
@votes = Vote.comment_votes_by_user_for_story_hash(@user.id, @story.id)
@comments.each do |c|
if @votes[c.id]
c.current_vote = @votes[c.id]
end
end
end
render :action => "show"
end
def undelete
@story.is_expired = false
@story.save(:validate => false)
redirect_to @story.comments_url
end
def update
2012-07-01 00:43:45 +02:00
@story.is_expired = false
if @story.update_attributes(params[:story].except(:url))
2012-07-01 00:43:45 +02:00
return redirect_to @story.comments_url
else
return render :action => "edit"
end
end
def unvote
if !(story = Story.find_by_short_id(params[:story_id]))
return render :text => "can't find story", :status => 400
end
2012-07-01 00:43:45 +02:00
Vote.vote_thusly_on_story_or_comment_for_user_because(0, story.id,
nil, @user.id, nil)
render :text => "ok"
end
def upvote
if !(story = Story.find_by_short_id(params[:story_id]))
return render :text => "can't find story", :status => 400
end
2012-07-01 00:43:45 +02:00
Vote.vote_thusly_on_story_or_comment_for_user_because(1, story.id,
nil, @user.id, nil)
render :text => "ok"
end
def downvote
if !(story = Story.find_by_short_id(params[:story_id]))
return render :text => "can't find story", :status => 400
end
2012-07-01 00:43:45 +02:00
if !Vote::STORY_REASONS[params[:reason]]
return render :text => "invalid reason", :status => 400
end
Vote.vote_thusly_on_story_or_comment_for_user_because(-1, story.id,
nil, @user.id, params[:reason])
render :text => "ok"
end
private
def find_story
if @user.is_admin?
@story = Story.find_by_short_id(params[:id])
else
@story = Story.find_by_user_id_and_short_id(@user.id, params[:id])
end
if !@story
2012-07-01 20:31:31 +02:00
flash[:error] = "Could not find story or you are not authorized " <<
"to manage it."
redirect_to "/"
return false
end
end
end