alert the user when submitting a long-ago-submitted story

show the user a link to the previous story and let them know they
can submit it again if they want to (just by submitting the form
again)
This commit is contained in:
joshua stein 2014-01-13 00:10:31 -06:00
parent 22b77573a5
commit f1f6b3c7e0
4 changed files with 58 additions and 31 deletions

View file

@ -1034,6 +1034,9 @@ div.flash-notice {
border-color: rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.25);
}
div.flash-error h2,
div.flash-notice h2,
div.flash-success h2,
div.errorExplanation h2 {
font-size: 1.25em;
margin: 0;

View file

@ -16,28 +16,19 @@ class StoriesController < ApplicationController
@story.url = params[:story][:url]
@story.user_id = @user.id
if @story.save
Vote.vote_thusly_on_story_or_comment_for_user_because(1, @story.id,
nil, @user.id, nil)
if @story.valid? && !(@story.already_posted_story && !@story.seen_previous)
if @story.save
Vote.vote_thusly_on_story_or_comment_for_user_because(1, @story.id,
nil, @user.id, nil)
Countinual.count!("#{Rails.application.shortname}.stories.submitted",
"+1")
Countinual.count!("#{Rails.application.shortname}.stories.submitted",
"+1")
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)
flash[:success] = "This URL has already been submitted recently."
return redirect_to @story.already_posted_story.comments_url
return redirect_to @story.comments_url
end
return render :action => "new"
end
return render :action => "new"
end
def destroy
@ -87,11 +78,16 @@ class StoriesController < ApplicationController
if params[:url].present?
@story.url = params[:url]
# if this story was already submitted, don't bother the user filling out
# tags and stuff, just redirect them right away
if s = Story.find_recent_similar_by_url(@story.url)
flash[:success] = "This URL has already been submitted recently."
return redirect_to s.comments_url
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."
return redirect_to s.comments_url
else
# user will see a warning like with preview screen
@story.already_posted_story = s
end
end
if params[:title].present?
@ -112,6 +108,8 @@ class StoriesController < ApplicationController
@story.valid?
@story.seen_previous = true
return render :action => "new", :layout => false
end

View file

@ -12,10 +12,15 @@ class Story < ActiveRecord::Base
# after this many minutes old, a story cannot be edited
MAX_EDIT_MINS = 30
attr_accessor :vote, :already_posted_story, :fetched_content, :previewing
# days a story is considered recent
RECENT_DAYS = 30
attr_accessor :vote, :already_posted_story, :fetched_content, :previewing,
:seen_previous
attr_accessor :editor_user_id, :moderation_reason
attr_accessible :title, :description, :tags_a, :moderation_reason
attr_accessible :title, :description, :tags_a, :moderation_reason,
:seen_previous
before_validation :assign_short_id,
:on => :create
@ -27,9 +32,12 @@ class Story < ActiveRecord::Base
# URI.parse is not very lenient, so we can't use it
if self.url.match(/\Ahttps?:\/\/([^\.]+\.)+[a-z]+(\/|\z)/)
if self.new_record? && (s = Story.find_recent_similar_by_url(self.url))
errors.add(:url, "has already been submitted recently")
if self.new_record? && (s = Story.find_similar_by_url(self.url))
self.already_posted_story = s
if s.is_recent?
errors.add(:url, "has already been submitted within the past " <<
"#{RECENT_DAYS} days")
end
end
else
errors.add(:url, "is not valid")
@ -41,7 +49,7 @@ class Story < ActiveRecord::Base
check_tags
end
def self.find_recent_similar_by_url(url)
def self.find_similar_by_url(url)
urls = [ url.to_s ]
urls2 = [ url.to_s ]
@ -66,14 +74,13 @@ class Story < ActiveRecord::Base
end
urls = urls2.clone
conds = [ "created_at >= ? AND (", (Time.now - 30.days) ]
conds = [ "" ]
urls.uniq.each_with_index do |u,x|
conds[0] << (x == 0 ? "" : " OR ") << "url = ?"
conds.push u
end
conds[0] << ")"
if s = Story.where(*conds).first
if s = Story.where(*conds).order("id DESC").first
return s
end
@ -376,6 +383,10 @@ class Story < ActiveRecord::Base
is_expired?
end
def is_recent?
self.created_at >= RECENT_DAYS.days.ago
end
def recalculate_hotness!
update_column :hotness, calculated_hotness
end

View file

@ -1,5 +1,20 @@
<%= error_messages_for f.object %>
<% if !f.object.errors.any? && f.object.already_posted_story %>
<div class="flash-notice">
<h2>Note: This story was already submitted <%=
time_ago_in_words(f.object.already_posted_story.created_at) %> ago, but may
be submitted again.</h2>
<p>
Please view the <a href="<%= f.object.already_posted_story.comments_url %>"
target="_blank">previous discussion</a> for this story first. If the content
has changed or warrants new discussion, you may submit it again.
</p>
</div>
<%= f.hidden_field :seen_previous %>
<% end %>
<div class="box">
<div class="boxline">
<% if f.object.url_is_editable_by_user?(@user) %>