2012-06-17 03:15:46 +02:00
|
|
|
class StoriesController < ApplicationController
|
|
|
|
before_filter :require_logged_in_user_or_400,
|
2014-03-18 06:06:43 +01:00
|
|
|
:only => [ :upvote, :downvote, :unvote, :hide, :unhide, :preview ]
|
2014-01-13 17:12:17 +01:00
|
|
|
before_filter :require_logged_in_user, :only => [ :destroy, :create, :edit,
|
2015-12-02 18:01:27 +01:00
|
|
|
:fetch_url_attributes, :new, :suggest ]
|
2014-01-13 17:12:17 +01:00
|
|
|
before_filter :find_user_story, :only => [ :destroy, :edit, :undelete,
|
|
|
|
:update ]
|
2015-10-15 03:32:24 +02:00
|
|
|
before_filter :find_story!, :only => [ :suggest, :submit_suggestions ]
|
2012-06-17 03:15:46 +02:00
|
|
|
|
|
|
|
def create
|
2012-07-11 22:34:53 +02:00
|
|
|
@title = "Submit Story"
|
|
|
|
@cur_url = "/stories/new"
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2014-02-02 21:41:38 +01:00
|
|
|
@story = Story.new(story_params)
|
2012-06-17 03:15:46 +02:00
|
|
|
@story.user_id = @user.id
|
|
|
|
|
2014-01-13 07:10:31 +01:00
|
|
|
if @story.valid? && !(@story.already_posted_story && !@story.seen_previous)
|
|
|
|
if @story.save
|
|
|
|
Countinual.count!("#{Rails.application.shortname}.stories.submitted",
|
|
|
|
"+1")
|
2012-08-12 17:49:53 +02:00
|
|
|
|
2015-01-03 01:33:13 +01:00
|
|
|
return redirect_to @story.comments_path
|
2012-06-17 03:15:46 +02:00
|
|
|
end
|
|
|
|
end
|
2014-01-13 07:10:31 +01:00
|
|
|
|
|
|
|
return render :action => "new"
|
2012-06-17 03:15:46 +02:00
|
|
|
end
|
|
|
|
|
2012-06-30 18:18:36 +02:00
|
|
|
def destroy
|
2012-07-05 02:33:12 +02:00
|
|
|
if !@story.is_editable_by_user?(@user)
|
|
|
|
flash[:error] = "You cannot edit that story."
|
|
|
|
return redirect_to "/"
|
|
|
|
end
|
|
|
|
|
2012-09-02 16:50:07 +02:00
|
|
|
@story.is_expired = true
|
2014-03-06 20:54:30 +01:00
|
|
|
@story.editor = @user
|
2013-01-08 00:20:41 +01:00
|
|
|
|
|
|
|
if params[:reason].present? && @story.user_id != @user.id
|
|
|
|
@story.moderation_reason = params[:reason]
|
|
|
|
end
|
|
|
|
|
2012-06-30 18:18:36 +02:00
|
|
|
@story.save(:validate => false)
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2015-01-03 01:33:13 +01:00
|
|
|
redirect_to @story.comments_path
|
2012-06-17 03:15:46 +02:00
|
|
|
end
|
2013-02-14 01:50:51 +01:00
|
|
|
|
2012-06-30 18:18:36 +02:00
|
|
|
def edit
|
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
|
2013-02-14 01:50:51 +01:00
|
|
|
|
2012-07-11 22:34:53 +02:00
|
|
|
@title = "Edit Story"
|
2014-04-09 00:51:12 +02:00
|
|
|
|
|
|
|
if @story.merged_into_story
|
|
|
|
@story.merge_story_short_id = @story.merged_into_story.short_id
|
|
|
|
end
|
2012-06-30 18:18:36 +02:00
|
|
|
end
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2015-03-10 23:41:40 +01:00
|
|
|
def fetch_url_attributes
|
2012-07-01 00:45:57 +02:00
|
|
|
s = Story.new
|
2015-03-10 23:41:40 +01:00
|
|
|
s.fetching_ip = request.remote_ip
|
2012-07-01 00:45:57 +02:00
|
|
|
s.url = params[:fetch_url]
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2015-12-02 18:01:27 +01:00
|
|
|
sattrs = s.fetched_attributes
|
|
|
|
sattrs.delete(:content)
|
2015-03-10 23:41:40 +01:00
|
|
|
|
2015-12-02 18:01:27 +01:00
|
|
|
return render :json => sattrs
|
2012-06-17 03:15:46 +02:00
|
|
|
end
|
|
|
|
|
2012-06-30 18:18:36 +02:00
|
|
|
def new
|
2012-07-11 22:34:53 +02:00
|
|
|
@title = "Submit Story"
|
|
|
|
@cur_url = "/stories/new"
|
2012-06-30 18:18:36 +02:00
|
|
|
|
|
|
|
@story = Story.new
|
2015-03-10 23:41:40 +01:00
|
|
|
@story.fetching_ip = request.remote_ip
|
2012-06-30 18:18:36 +02:00
|
|
|
|
2013-02-08 17:39:51 +01:00
|
|
|
if params[:url].present?
|
2012-06-30 18:18:36 +02:00
|
|
|
@story.url = params[:url]
|
|
|
|
|
2015-12-02 18:01:27 +01:00
|
|
|
sattrs = @story.fetched_attributes
|
|
|
|
|
|
|
|
if sattrs[:url].present? && @story.url != sattrs[:url]
|
2015-03-10 23:41:40 +01:00
|
|
|
flash.now[:notice] = "Note: URL has been changed to fetched " <<
|
|
|
|
"canonicalized version"
|
2015-12-02 18:01:27 +01:00
|
|
|
@story.url = sattrs[:url]
|
2015-03-10 23:41:40 +01:00
|
|
|
end
|
|
|
|
|
2014-01-13 07:10:31 +01:00
|
|
|
if s = Story.find_similar_by_url(@story.url)
|
|
|
|
if s.is_recent?
|
|
|
|
# user won't be able to submit this story as new, so just redirect
|
|
|
|
# them to the previous story
|
|
|
|
flash[:success] = "This URL has already been submitted recently."
|
2015-01-03 01:33:13 +01:00
|
|
|
return redirect_to s.comments_path
|
2014-01-13 07:10:31 +01:00
|
|
|
else
|
|
|
|
# user will see a warning like with preview screen
|
|
|
|
@story.already_posted_story = s
|
|
|
|
end
|
2013-07-29 22:31:57 +02:00
|
|
|
end
|
|
|
|
|
2015-02-03 18:20:07 +01:00
|
|
|
# ignore what the user brought unless we need it as a fallback
|
2015-12-02 18:01:27 +01:00
|
|
|
@story.title = sattrs[:title]
|
2015-02-03 18:20:07 +01:00
|
|
|
if !@story.title.present? && params[:title].present?
|
2012-06-30 18:18:36 +02:00
|
|
|
@story.title = params[:title]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-12 01:28:27 +02:00
|
|
|
def preview
|
2014-02-02 21:41:38 +01:00
|
|
|
@story = Story.new(story_params)
|
2012-07-12 01:28:27 +02:00
|
|
|
@story.user_id = @user.id
|
|
|
|
@story.previewing = true
|
|
|
|
|
2015-02-13 18:26:28 +01:00
|
|
|
@story.vote = Vote.new(:vote => 1)
|
2012-07-12 01:28:27 +02:00
|
|
|
@story.upvotes = 1
|
|
|
|
|
|
|
|
@story.valid?
|
|
|
|
|
2014-01-13 07:10:31 +01:00
|
|
|
@story.seen_previous = true
|
|
|
|
|
2012-07-12 01:28:27 +02:00
|
|
|
return render :action => "new", :layout => false
|
|
|
|
end
|
|
|
|
|
2012-07-03 19:12:20 +02:00
|
|
|
def show
|
2013-12-24 04:20:06 +01:00
|
|
|
@story = Story.where(:short_id => params[:id]).first!
|
2012-06-30 18:18:36 +02:00
|
|
|
|
2014-04-09 00:51:12 +02:00
|
|
|
if @story.merged_into_story
|
|
|
|
flash[:success] = "\"#{@story.title}\" has been merged into this story."
|
2015-01-03 01:33:13 +01:00
|
|
|
return redirect_to @story.merged_into_story.comments_path
|
2014-04-09 00:51:12 +02:00
|
|
|
end
|
|
|
|
|
2012-07-05 02:33:12 +02:00
|
|
|
if @story.can_be_seen_by_user?(@user)
|
2012-09-07 16:18:15 +02:00
|
|
|
@title = @story.title
|
2012-07-05 02:33:12 +02:00
|
|
|
else
|
2012-09-07 16:18:15 +02:00
|
|
|
@title = "[Story removed]"
|
2012-07-05 02:33:12 +02:00
|
|
|
end
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2012-07-07 01:10:08 +02:00
|
|
|
@short_url = @story.short_id_url
|
|
|
|
|
2014-11-14 21:09:28 +01:00
|
|
|
@comments = @story.merged_comments.includes(:user, :story,
|
|
|
|
:hat).arrange_for_user(@user)
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2014-01-21 06:30:46 +01:00
|
|
|
if params[:comment_short_id]
|
|
|
|
@comments.each do |c,x|
|
|
|
|
if c.short_id == params[:comment_short_id]
|
|
|
|
c.highlighted = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-30 19:13:19 +01:00
|
|
|
respond_to do |format|
|
|
|
|
format.html {
|
2014-01-13 08:11:34 +01:00
|
|
|
@comment = @story.comments.build
|
2012-12-30 19:13:19 +01:00
|
|
|
|
|
|
|
load_user_votes
|
|
|
|
|
|
|
|
render :action => "show"
|
|
|
|
}
|
|
|
|
format.json {
|
|
|
|
render :json => @story.as_json(:with_comments => @comments)
|
|
|
|
}
|
|
|
|
end
|
2012-07-03 19:12:20 +02:00
|
|
|
end
|
|
|
|
|
2015-10-15 03:32:24 +02:00
|
|
|
def suggest
|
|
|
|
if (st = @story.suggested_taggings.where(:user_id => @user.id)).any?
|
|
|
|
@story.tags_a = st.map{|st| st.tag.tag }
|
|
|
|
end
|
|
|
|
if tt = @story.suggested_titles.where(:user_id => @user.id).first
|
|
|
|
@story.title = tt.title
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def submit_suggestions
|
2015-10-18 22:09:39 +02:00
|
|
|
ostory = @story.dup
|
2015-10-15 03:32:24 +02:00
|
|
|
|
|
|
|
@story.title = params[:story][:title]
|
|
|
|
if @story.valid?
|
2015-10-17 17:39:58 +02:00
|
|
|
dsug = false
|
2015-10-15 03:32:24 +02:00
|
|
|
if @story.title != ostory.title
|
|
|
|
@story.save_suggested_title_for_user!(@story.title, @user)
|
2015-10-17 17:39:58 +02:00
|
|
|
dsug = true
|
2015-10-15 03:32:24 +02:00
|
|
|
end
|
2015-10-17 17:39:58 +02:00
|
|
|
|
|
|
|
sugtags = params[:story][:tags_a].reject{|t| t.to_s.strip == "" }.sort
|
|
|
|
if @story.tags_a.sort != sugtags
|
|
|
|
@story.save_suggested_tags_a_for_user!(sugtags, @user)
|
|
|
|
dsug = true
|
|
|
|
end
|
|
|
|
|
|
|
|
if dsug
|
2015-10-18 22:09:39 +02:00
|
|
|
ostory = @story.reload
|
2015-10-17 17:39:58 +02:00
|
|
|
flash[:success] = "Your suggested changes have been noted."
|
2015-10-15 03:32:24 +02:00
|
|
|
end
|
|
|
|
redirect_to ostory.comments_path
|
|
|
|
else
|
|
|
|
render :action => "suggest"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-30 18:18:36 +02:00
|
|
|
def undelete
|
2012-07-05 02:33:12 +02:00
|
|
|
if !(@story.is_editable_by_user?(@user) &&
|
|
|
|
@story.is_undeletable_by_user?(@user))
|
|
|
|
flash[:error] = "You cannot edit that story."
|
|
|
|
return redirect_to "/"
|
|
|
|
end
|
|
|
|
|
2012-06-30 18:18:36 +02:00
|
|
|
@story.is_expired = false
|
2014-03-06 20:54:30 +01:00
|
|
|
@story.editor = @user
|
2012-06-30 18:18:36 +02:00
|
|
|
@story.save(:validate => false)
|
|
|
|
|
2015-01-03 01:33:13 +01:00
|
|
|
redirect_to @story.comments_path
|
2012-06-30 18:18:36 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2012-07-05 02:33:12 +02:00
|
|
|
if !@story.is_editable_by_user?(@user)
|
|
|
|
flash[:error] = "You cannot edit that story."
|
|
|
|
return redirect_to "/"
|
|
|
|
end
|
|
|
|
|
2012-07-01 00:43:45 +02:00
|
|
|
@story.is_expired = false
|
2014-03-06 20:54:30 +01:00
|
|
|
@story.editor = @user
|
2012-06-30 18:18:36 +02:00
|
|
|
|
2013-03-26 18:22:23 +01:00
|
|
|
if @story.url_is_editable_by_user?(@user)
|
2014-02-02 21:41:38 +01:00
|
|
|
@story.attributes = story_params
|
|
|
|
else
|
|
|
|
@story.attributes = story_params.except(:url)
|
2013-03-26 18:22:23 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
if @story.save
|
2015-01-03 01:33:13 +01:00
|
|
|
return redirect_to @story.comments_path
|
2012-07-01 00:43:45 +02:00
|
|
|
else
|
|
|
|
return render :action => "edit"
|
2012-06-30 18:18:36 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-17 03:15:46 +02:00
|
|
|
def unvote
|
2013-12-24 04:20:06 +01:00
|
|
|
if !(story = find_story)
|
2012-06-17 03:15:46 +02:00
|
|
|
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,
|
2012-06-17 03:15:46 +02:00
|
|
|
nil, @user.id, nil)
|
|
|
|
|
|
|
|
render :text => "ok"
|
|
|
|
end
|
|
|
|
|
|
|
|
def upvote
|
2013-12-24 04:20:06 +01:00
|
|
|
if !(story = find_story)
|
2012-06-17 03:15:46 +02:00
|
|
|
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,
|
2012-06-17 03:15:46 +02:00
|
|
|
nil, @user.id, nil)
|
|
|
|
|
|
|
|
render :text => "ok"
|
|
|
|
end
|
|
|
|
|
2014-03-18 06:06:43 +01:00
|
|
|
def downvote
|
|
|
|
if !(story = find_story)
|
|
|
|
return render :text => "can't find story", :status => 400
|
|
|
|
end
|
|
|
|
|
|
|
|
if !Vote::STORY_REASONS[params[:reason]]
|
|
|
|
return render :text => "invalid reason", :status => 400
|
|
|
|
end
|
|
|
|
|
|
|
|
if !@user.can_downvote?(story)
|
|
|
|
return render :text => "not permitted to downvote", :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
|
|
|
|
|
2014-03-04 00:14:33 +01:00
|
|
|
def hide
|
2013-12-24 04:20:06 +01:00
|
|
|
if !(story = find_story)
|
2012-06-17 03:15:46 +02:00
|
|
|
return render :text => "can't find story", :status => 400
|
|
|
|
end
|
2012-07-01 00:43:45 +02:00
|
|
|
|
2015-02-11 18:37:03 +01:00
|
|
|
HiddenStory.hide_story_for_user(story.id, @user.id)
|
2014-03-04 00:14:33 +01:00
|
|
|
|
|
|
|
render :text => "ok"
|
|
|
|
end
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2014-03-04 00:14:33 +01:00
|
|
|
def unhide
|
|
|
|
if !(story = find_story)
|
|
|
|
return render :text => "can't find story", :status => 400
|
2014-01-13 06:17:09 +01:00
|
|
|
end
|
|
|
|
|
2015-02-11 18:37:03 +01:00
|
|
|
HiddenStory.where(:user_id => @user.id, :story_id => story.id).delete_all
|
2012-06-17 03:15:46 +02:00
|
|
|
|
|
|
|
render :text => "ok"
|
|
|
|
end
|
2012-06-30 18:18:36 +02:00
|
|
|
|
|
|
|
private
|
2013-12-24 04:20:06 +01:00
|
|
|
|
2014-02-02 21:41:38 +01:00
|
|
|
def story_params
|
2014-02-04 18:42:03 +01:00
|
|
|
p = params.require(:story).permit(
|
|
|
|
:title, :url, :description, :moderation_reason, :seen_previous,
|
2015-07-31 00:15:48 +02:00
|
|
|
:merge_story_short_id, :is_unavailable, :user_is_author, :tags_a => [],
|
2014-02-02 21:41:38 +01:00
|
|
|
)
|
2014-02-04 18:42:03 +01:00
|
|
|
|
|
|
|
if @user.is_moderator?
|
|
|
|
p
|
|
|
|
else
|
2015-01-06 21:22:18 +01:00
|
|
|
p.except(:moderation_reason, :merge_story_short_id, :is_unavailable)
|
2014-02-04 18:42:03 +01:00
|
|
|
end
|
2014-02-02 21:41:38 +01:00
|
|
|
end
|
|
|
|
|
2012-06-30 18:18:36 +02:00
|
|
|
def find_story
|
2014-02-17 17:13:08 +01:00
|
|
|
story = Story.where(:short_id => params[:story_id]).first
|
|
|
|
if @user && story
|
|
|
|
story.vote = Vote.where(:user_id => @user.id,
|
|
|
|
:story_id => story.id, :comment_id => nil).first.try(:vote)
|
|
|
|
end
|
|
|
|
|
|
|
|
story
|
2013-12-24 04:20:06 +01:00
|
|
|
end
|
|
|
|
|
2015-10-15 03:32:24 +02:00
|
|
|
def find_story!
|
|
|
|
@story = find_story
|
|
|
|
if !@story
|
|
|
|
raise ActiveRecord::RecordNotFound
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-24 04:20:06 +01:00
|
|
|
def find_user_story
|
2012-09-02 16:50:07 +02:00
|
|
|
if @user.is_moderator?
|
2013-12-24 04:20:06 +01:00
|
|
|
@story = Story.where(:short_id => params[:story_id] || params[:id]).first
|
2012-06-30 18:18:36 +02:00
|
|
|
else
|
2013-12-24 04:20:06 +01:00
|
|
|
@story = Story.where(:user_id => @user.id, :short_id =>
|
|
|
|
(params[:story_id] || params[:id])).first
|
2012-06-30 18:18:36 +02:00
|
|
|
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."
|
2012-06-30 18:18:36 +02:00
|
|
|
redirect_to "/"
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
2012-10-11 00:29:02 +02:00
|
|
|
|
|
|
|
def load_user_votes
|
|
|
|
if @user
|
2013-12-24 04:20:06 +01:00
|
|
|
if v = Vote.where(:user_id => @user.id, :story_id => @story.id,
|
|
|
|
:comment_id => nil).first
|
2015-02-11 18:37:03 +01:00
|
|
|
@story.vote = { :vote => v.vote, :reason => v.reason }
|
2012-10-11 00:29:02 +02:00
|
|
|
end
|
2013-02-14 01:50:51 +01:00
|
|
|
|
2015-02-11 18:37:03 +01:00
|
|
|
@story.is_hidden_by_cur_user = @story.is_hidden_by_user?(@user)
|
|
|
|
|
2012-10-11 00:29:02 +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
|
|
|
|
end
|
|
|
|
end
|
2012-06-17 03:15:46 +02:00
|
|
|
end
|