added delete all posts button

wired up the ability to enable all themes
This commit is contained in:
Sam Saffron 2013-02-07 18:11:56 +11:00
parent 8f4417f962
commit 85973ce6b0
13 changed files with 93 additions and 17 deletions

View File

@ -1,5 +1,9 @@
window.Discourse.AdminUser = Discourse.Model.extend
deleteAllPosts: ->
@set('can_delete_all_posts', false)
$.ajax "/admin/users/#{@get('id')}/delete_all_posts", type: 'PUT'
# Revoke the user's admin access
revokeAdmin: ->
@set('admin',false)
@ -18,8 +22,6 @@ window.Discourse.AdminUser = Discourse.Model.extend
type: 'POST'
bootbox.alert("Message sent to all clients!")
approve: ->
@set('can_approve', false)
@set('approved', true)

View File

@ -38,7 +38,7 @@
<tr>
<td></td>
<td class='message'>
<div>{{avatar user imageSize="small"}} {{message}}</div>
<div><a href="/admin{{unbound user.path}}">{{avatar user imageSize="small"}}</a> {{message}}</div>
</td>
<td></td>
<td></td>

View File

@ -139,6 +139,14 @@
<div class='display-row'>
<div class='field'>{{i18n admin.user.post_count}}</div>
<div class='value'>{{content.post_count}}</div>
<div class='controls'>
{{#if content.can_delete_all_posts}}
<button class='btn' {{action deleteAllPosts target="content"}}>
<i class='icon icon-trash'></i>
{{i18n admin.user.delete_all_posts}}
</button>
{{/if}}
</div>
</div>
<div class='display-row'>
<div class='field'>{{i18n admin.user.posts_read_count}}</div>

View File

@ -19,6 +19,11 @@ class Admin::UsersController < Admin::AdminController
render_serialized(@user, AdminDetailedUserSerializer, root: false)
end
def delete_all_posts
@user = User.where(id: params[:user_id]).first
@user.delete_all_posts!(guardian)
render nothing: true
end
def ban
@user = User.where(id: params[:user_id]).first
guardian.ensure_can_ban!(@user)

View File

@ -319,11 +319,11 @@ class Post < ActiveRecord::Base
Post.transaction do
self.last_version_at = revised_at
updater.call(true)
EditRateLimiter.new(updated_by).performed!
EditRateLimiter.new(updated_by).performed! unless opts[:bypass_rate_limiter]
# If a new version is created of the last post, bump it.
unless Post.where('post_number > ? and topic_id = ?', self.post_number, self.topic_id).exists?
topic.update_column(:bumped_at, Time.now)
topic.update_column(:bumped_at, Time.now) unless opts[:bypass_bump]
end
end

View File

@ -1,5 +1,6 @@
class SiteCustomization < ActiveRecord::Base
ENABLED_KEY = '7e202ef2-56d7-47d5-98d8-a9c8d15e57dd'
CACHE_PATH = 'stylesheet-cache'
@lock = Mutex.new
@ -49,18 +50,36 @@ footer:after{ content: '#{error}' }"
self.remove_from_cache!
end
def self.enabled_style
@cache ||= {}
preview_style = @cache[ENABLED_KEY]
return nil if preview_style == :none
return preview_style if preview_style
@lock.synchronize do
style = self.where(enabled: true).first
if style
@cache[ENABLED_KEY] = style.key
else
@cache[ENABLED_KEY] = :none
end
end
end
def self.custom_stylesheet(preview_style)
preview_style ||= enabled_style
style = lookup_style(preview_style)
style.stylesheet_link_tag.html_safe if style
end
def self.custom_header(preview_style)
preview_style ||= enabled_style
style = lookup_style(preview_style)
style.header.html_safe if style
end
def self.override_default_style(preview_style)
preview_style ||= enabled_style
style = lookup_style(preview_style)
style.override_default_style if style
end
@ -103,6 +122,7 @@ footer:after{ content: '#{error}' }"
end
def remove_from_cache!
self.class.remove_from_cache!(ENABLED_KEY)
self.class.remove_from_cache!(self.key)
end

View File

@ -335,6 +335,18 @@ class User < ActiveRecord::Base
PrettyText.excerpt(bio_cooked, 350)
end
def delete_all_posts!(guardian)
raise Discourse::InvalidAccess unless guardian.can_delete_all_posts? self
posts.order("post_number desc").each do |p|
if p.post_number == 1
p.topic.destroy
else
p.destroy
end
end
end
def is_banned?
!banned_till.nil? && banned_till > DateTime.now
end

View File

@ -8,7 +8,8 @@ class AdminDetailedUserSerializer < AdminUserSerializer
:post_count,
:flags_given_count,
:flags_received_count,
:private_topics_count
:private_topics_count,
:can_delete_all_posts
has_one :approved_by, serializer: BasicUserSerializer, embed: :objects
@ -20,6 +21,10 @@ class AdminDetailedUserSerializer < AdminUserSerializer
scope.can_grant_admin?(object)
end
def can_delete_all_posts
scope.can_delete_all_posts?(object)
end
def moderator
object.has_trust_level?(:moderator)
end

View File

@ -50,12 +50,8 @@
</style>
<%- unless SiteCustomization.override_default_style(session[:preview_style]) %>
<%- if params[:shiny] %>
<%=stylesheet_link_tag "shiny/shiny"%>
<%- else %>
<%=stylesheet_link_tag "application"%>
<%- end %>
<%- end %>
<%- if mini_profiler_enabled? %>
<%- Rack::MiniProfiler.step "stylsheet" do%>

View File

@ -369,6 +369,7 @@ en:
ban_failed: "Something went wrong banning this user {{error}}"
unban_failed: "Something went wrong unbanning this user {{error}}"
ban_duration: "How long would you like to ban the user for? (days)"
delete_all_posts: "Delete all posts"
ban: "Ban"
unban: "Unban"
banned: "Banned?"

View File

@ -30,6 +30,7 @@ Discourse::Application.routes.draw do
put 'approve-bulk' => 'users#approve_bulk'
end
put 'ban' => 'users#ban'
put 'delete_all_posts' => 'users#delete_all_posts'
put 'unban' => 'users#unban'
put 'revoke_admin' => 'users#revoke_admin'
put 'grant_admin' => 'users#grant_admin'

View File

@ -167,6 +167,13 @@ class Guardian
@user.id == user_id
end
def can_delete_all_posts?(user)
return false unless is_admin?
return false if user.created_at < 7.days.ago
true
end
# Support for ensure_{blah}! methods.
def method_missing(method, *args, &block)
if method.to_s =~ /^ensure_(.*)\!$/

View File

@ -67,12 +67,8 @@ describe User do
user.reload
user.posts_read_count.should == 1
end
end
end
end
context '.enqueue_welcome_message' do
@ -174,6 +170,29 @@ describe User do
end
describe 'delete posts' do
before do
@post1 = Fabricate(:post)
@user = @post1.user
@post2 = Fabricate(:post, topic: @post1.topic, user: @user)
@post3 = Fabricate(:post, user: @user)
@posts = [@post1, @post2, @post3]
@guardian = Guardian.new(Fabricate(:admin))
end
it 'allows moderator to delete all posts' do
@user.delete_all_posts!(@guardian)
@posts.each do |p|
p.reload
if p
p.topic.should be_nil
else
p.should be_nil
end
end
end
end
describe 'new' do