fix deleting/undeleting stories for users and moderators
This commit is contained in:
parent
805b0fd1a0
commit
cd568eaa38
|
@ -30,7 +30,7 @@ class ApplicationController < ActionController::Base
|
||||||
def find_stories_for_user_and_tag_and_newest(user, tag = nil, newest = false)
|
def find_stories_for_user_and_tag_and_newest(user, tag = nil, newest = false)
|
||||||
stories = []
|
stories = []
|
||||||
|
|
||||||
conds = [ "is_expired = 0 " ]
|
conds = [ "is_expired = 0 AND is_moderated = 0 " ]
|
||||||
|
|
||||||
if user && !newest
|
if user && !newest
|
||||||
# exclude downvoted items
|
# exclude downvoted items
|
||||||
|
|
|
@ -4,7 +4,7 @@ class CommentsController < ApplicationController
|
||||||
before_filter :require_logged_in_user, :only => [ :threads ]
|
before_filter :require_logged_in_user, :only => [ :threads ]
|
||||||
|
|
||||||
def create
|
def create
|
||||||
if !(story = Story.find_by_short_id(params[:story_id]))
|
if !(story = Story.find_by_short_id(params[:story_id])) || story.is_gone?
|
||||||
return render :text => "can't find story", :status => 400
|
return render :text => "can't find story", :status => 400
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -37,19 +37,29 @@ class StoriesController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
@story.is_expired = true
|
if !@story.is_editable_by_user?(@user)
|
||||||
|
flash[:error] = "You cannot edit that story."
|
||||||
|
return redirect_to "/"
|
||||||
|
end
|
||||||
|
|
||||||
|
if @user.is_admin? && @user.id != @story.user_id
|
||||||
|
@story.is_moderated = true
|
||||||
|
else
|
||||||
|
@story.is_expired = true
|
||||||
|
end
|
||||||
|
|
||||||
@story.save(:validate => false)
|
@story.save(:validate => false)
|
||||||
|
|
||||||
redirect_to @story.comments_url
|
redirect_to @story.comments_url
|
||||||
end
|
end
|
||||||
|
|
||||||
def edit
|
def edit
|
||||||
@page_title = "Edit Story"
|
|
||||||
|
|
||||||
if !@story.is_editable_by_user?(@user)
|
if !@story.is_editable_by_user?(@user)
|
||||||
flash[:error] = "You cannot edit that story."
|
flash[:error] = "You cannot edit that story."
|
||||||
return redirect_to "/"
|
return redirect_to "/"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@page_title = "Edit Story"
|
||||||
end
|
end
|
||||||
|
|
||||||
def fetch_url_title
|
def fetch_url_title
|
||||||
|
@ -81,7 +91,11 @@ class StoriesController < ApplicationController
|
||||||
def show
|
def show
|
||||||
@story = Story.find_by_short_id!(params[:id])
|
@story = Story.find_by_short_id!(params[:id])
|
||||||
|
|
||||||
@page_title = @story.title
|
if @story.can_be_seen_by_user?(@user)
|
||||||
|
@page_title = @story.title
|
||||||
|
else
|
||||||
|
@page_title = "[Story removed]"
|
||||||
|
end
|
||||||
|
|
||||||
@comments = Comment.ordered_for_story_or_thread_for_user(@story.id, nil,
|
@comments = Comment.ordered_for_story_or_thread_for_user(@story.id, nil,
|
||||||
@user ? @user.id : nil)
|
@user ? @user.id : nil)
|
||||||
|
@ -143,13 +157,25 @@ class StoriesController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def undelete
|
def undelete
|
||||||
|
if !(@story.is_editable_by_user?(@user) &&
|
||||||
|
@story.is_undeletable_by_user?(@user))
|
||||||
|
flash[:error] = "You cannot edit that story."
|
||||||
|
return redirect_to "/"
|
||||||
|
end
|
||||||
|
|
||||||
@story.is_expired = false
|
@story.is_expired = false
|
||||||
|
@story.is_moderated = false
|
||||||
@story.save(:validate => false)
|
@story.save(:validate => false)
|
||||||
|
|
||||||
redirect_to @story.comments_url
|
redirect_to @story.comments_url
|
||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
|
if !@story.is_editable_by_user?(@user)
|
||||||
|
flash[:error] = "You cannot edit that story."
|
||||||
|
return redirect_to "/"
|
||||||
|
end
|
||||||
|
|
||||||
@story.is_expired = false
|
@story.is_expired = false
|
||||||
|
|
||||||
if @story.update_attributes(params[:story].except(:url))
|
if @story.update_attributes(params[:story].except(:url))
|
||||||
|
@ -199,9 +225,10 @@ class StoriesController < ApplicationController
|
||||||
private
|
private
|
||||||
def find_story
|
def find_story
|
||||||
if @user.is_admin?
|
if @user.is_admin?
|
||||||
@story = Story.find_by_short_id(params[:id])
|
@story = Story.find_by_short_id(params[:story_id] || params[:id])
|
||||||
else
|
else
|
||||||
@story = Story.find_by_user_id_and_short_id(@user.id, params[:id])
|
@story = Story.find_by_user_id_and_short_id(@user.id,
|
||||||
|
(params[:story_id] || params[:id]))
|
||||||
end
|
end
|
||||||
|
|
||||||
if !@story
|
if !@story
|
||||||
|
|
|
@ -204,22 +204,38 @@ class Story < ActiveRecord::Base
|
||||||
|
|
||||||
def is_editable_by_user?(user)
|
def is_editable_by_user?(user)
|
||||||
if user && user.is_admin?
|
if user && user.is_admin?
|
||||||
true
|
return true
|
||||||
elsif user && user.id == self.user_id
|
elsif user && user.id == self.user_id
|
||||||
(Time.now.to_i - self.created_at.to_i < (60 * MAX_EDIT_MINS))
|
if self.is_moderated?
|
||||||
|
return false
|
||||||
|
else
|
||||||
|
return (Time.now.to_i - self.created_at.to_i < (60 * MAX_EDIT_MINS))
|
||||||
|
end
|
||||||
else
|
else
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def is_undeletable_by_user?(user)
|
def is_undeletable_by_user?(user)
|
||||||
if !user || user.id != self.user_id
|
if user && (user.is_admin? || user.id == self.user_id)
|
||||||
|
return true
|
||||||
|
else
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def can_be_seen_by_user?(user)
|
||||||
|
if is_gone? && !(user && (user.is_admin? || user.id == self.user_id))
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def is_gone?
|
||||||
|
is_expired? || is_moderated?
|
||||||
|
end
|
||||||
|
|
||||||
def update_comment_count!
|
def update_comment_count!
|
||||||
Keystore.put("story:#{self.id}:comment_count",
|
Keystore.put("story:#{self.id}:comment_count",
|
||||||
Comment.where(:story_id => self.id).count)
|
Comment.where(:story_id => self.id).count)
|
||||||
|
|
|
@ -35,14 +35,16 @@ class="comment <%= comment.current_vote ? (comment.current_vote[:vote] == 1 ?
|
||||||
|
|
||||||
<% if !comment.previewing %>
|
<% if !comment.previewing %>
|
||||||
|
|
|
|
||||||
|
|
||||||
<a href="<%= story.comments_url %>/comments/<%= comment.short_id
|
<a href="<%= story.comments_url %>/comments/<%= comment.short_id
|
||||||
%>">link</a>
|
%>">link</a>
|
||||||
|
|
|
||||||
<% if comment.is_editable_by_user?(@user) %>
|
<% if !story.is_gone? %>
|
||||||
<a class="comment_editor">edit</a>
|
|
|
||||||
<% else %>
|
<% if comment.is_editable_by_user?(@user) %>
|
||||||
<a class="comment_replier">reply</a>
|
<a class="comment_editor">edit</a>
|
||||||
|
<% else %>
|
||||||
|
<a class="comment_replier">reply</a>
|
||||||
|
<% end %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
<% if false && defined?(collapsable) && collapsable # XXX %>
|
<% if false && defined?(collapsable) && collapsable # XXX %>
|
||||||
|
|
|
@ -18,35 +18,45 @@ class="story <%= story.vote == 1 ? "upvoted" : (story.vote == -1 ?
|
||||||
</div>
|
</div>
|
||||||
<div class="details">
|
<div class="details">
|
||||||
<span class="link">
|
<span class="link">
|
||||||
<a href="<%= story.url_or_comments_url %>"><%= story.title %></a>
|
<% if story.can_be_seen_by_user?(@user) %>
|
||||||
</span>
|
<a href="<%= story.url_or_comments_url %>"><%= story.title %></a>
|
||||||
<span class="tags">
|
<% end %>
|
||||||
<% story.taggings.each do |tagging| %>
|
<% if story.is_gone? %>
|
||||||
<a href="<%= tag_url(tagging.tag.tag) %>" class="tag tag_<%=
|
[Story removed by <%= story.is_expired? ? "original submitter" :
|
||||||
tagging.tag.tag %>"><%= tagging.tag.tag %></a>
|
"moderator" %>]
|
||||||
<% end %>
|
<% end %>
|
||||||
</span>
|
|
||||||
<span class="domain">
|
|
||||||
<%= story.domain %>
|
|
||||||
</span>
|
</span>
|
||||||
|
<% if story.can_be_seen_by_user?(@user) %>
|
||||||
|
<span class="tags">
|
||||||
|
<% story.taggings.each do |tagging| %>
|
||||||
|
<a href="<%= tag_url(tagging.tag.tag) %>" class="tag tag_<%=
|
||||||
|
tagging.tag.tag %>"><%= tagging.tag.tag %></a>
|
||||||
|
<% end %>
|
||||||
|
</span>
|
||||||
|
<span class="domain">
|
||||||
|
<%= story.domain %>
|
||||||
|
</span>
|
||||||
|
<% end %>
|
||||||
<div class="byline">
|
<div class="byline">
|
||||||
by <a href="/u/<%= story.user.username %>"><%= story.user.username %></a>
|
by <a href="/u/<%= story.user.username %>"><%= story.user.username %></a>
|
||||||
<%= time_ago_in_words(story.created_at).gsub(/^about /, "") %> ago
|
<%= time_ago_in_words(story.created_at).gsub(/^about /, "") %> ago
|
||||||
|
|
||||||
<% if story.is_editable_by_user? @user %>
|
<% if story.is_editable_by_user?(@user) %>
|
||||||
|
|
|
|
||||||
<a href="<%= edit_story_url(story.short_id) %>">edit</a>
|
<a href="<%= edit_story_url(story.short_id) %>">edit</a>
|
||||||
|
|
|
||||||
<% if story.is_expired? %>
|
<% if story.is_gone? && story.is_undeletable_by_user?(@user) %>
|
||||||
|
|
|
||||||
<%= link_to "undelete", story_undelete_url(story.short_id),
|
<%= link_to "undelete", story_undelete_url(story.short_id),
|
||||||
:method => :post,
|
:method => :post,
|
||||||
:confirm => "Are you sure you want to undelete this story?" %>
|
:confirm => "Are you sure you want to undelete this story?" %>
|
||||||
<% else %>
|
<% elsif !story.is_gone? %>
|
||||||
|
|
|
||||||
<%= link_to "delete", story_url(story.short_id), :method => :delete,
|
<%= link_to "delete", story_url(story.short_id), :method => :delete,
|
||||||
:confirm => "Are you sure you want to delete this story?" %>
|
:confirm => "Are you sure you want to delete this story?" %>
|
||||||
<% end %>
|
<% end %>
|
||||||
<% end %>
|
<% end %>
|
||||||
<% if !story.is_expired? %>
|
<% if !story.is_gone? %>
|
||||||
|
|
|
|
||||||
<a href="<%= story.comments_url %>"><%=
|
<a href="<%= story.comments_url %>"><%=
|
||||||
(c = story.comment_count) == 0 ? "discuss" :
|
(c = story.comment_count) == 0 ? "discuss" :
|
||||||
|
|
|
@ -4,14 +4,14 @@
|
||||||
</ol>
|
</ol>
|
||||||
|
|
||||||
<div class="story_content">
|
<div class="story_content">
|
||||||
<% if @story.url.blank? %>
|
<% if @story.description.present? %>
|
||||||
<div class="story_text">
|
<div class="story_text">
|
||||||
<%= raw @story.linkified_text %>
|
<%= raw @story.linkified_text %>
|
||||||
</div>
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
<p></p>
|
<p></p>
|
||||||
<% if @user && !@story.is_expired? %>
|
<% if @user && !@story.is_gone? %>
|
||||||
<%= render :partial => "comments/commentbox",
|
<%= render :partial => "comments/commentbox",
|
||||||
:locals => { :story => @story, :comment => @comment } %>
|
:locals => { :story => @story, :comment => @comment } %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
Loading…
Reference in a new issue