From 85973ce6b0a68fc1726cc5485affbf099147a2b9 Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Thu, 7 Feb 2013 18:11:56 +1100 Subject: [PATCH] added delete all posts button wired up the ability to enable all themes --- .../admin/models/admin_user.js.coffee | 10 ++++--- .../admin/templates/flags.js.handlebars | 2 +- .../admin/templates/user.js.handlebars | 8 ++++++ app/controllers/admin/users_controller.rb | 5 ++++ app/models/post.rb | 4 +-- app/models/site_customization.rb | 20 ++++++++++++++ app/models/user.rb | 12 +++++++++ .../admin_detailed_user_serializer.rb | 7 ++++- app/views/layouts/application.html.erb | 6 +---- config/locales/en.yml | 1 + config/routes.rb | 1 + lib/guardian.rb | 7 +++++ spec/models/user_spec.rb | 27 ++++++++++++++++--- 13 files changed, 93 insertions(+), 17 deletions(-) diff --git a/app/assets/javascripts/admin/models/admin_user.js.coffee b/app/assets/javascripts/admin/models/admin_user.js.coffee index 148149b767f..a4fb524e91d 100644 --- a/app/assets/javascripts/admin/models/admin_user.js.coffee +++ b/app/assets/javascripts/admin/models/admin_user.js.coffee @@ -1,10 +1,14 @@ 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) @set('can_grant_admin',true) - @set('can_revoke_admin',false) + @set('can_revoke_admin',false) $.ajax "/admin/users/#{@get('id')}/revoke_admin", type: 'PUT' grantAdmin: -> @@ -18,13 +22,11 @@ window.Discourse.AdminUser = Discourse.Model.extend type: 'POST' bootbox.alert("Message sent to all clients!") - - approve: -> @set('can_approve', false) @set('approved', true) @set('approved_by', Discourse.get('currentUser')) - $.ajax "/admin/users/#{@get('id')}/approve", type: 'PUT' + $.ajax "/admin/users/#{@get('id')}/approve", type: 'PUT' username_lower:(-> @get('username').toLowerCase() diff --git a/app/assets/javascripts/admin/templates/flags.js.handlebars b/app/assets/javascripts/admin/templates/flags.js.handlebars index 929076b5cec..17670abeee9 100644 --- a/app/assets/javascripts/admin/templates/flags.js.handlebars +++ b/app/assets/javascripts/admin/templates/flags.js.handlebars @@ -38,7 +38,7 @@ -
{{avatar user imageSize="small"}} {{message}}
+
{{avatar user imageSize="small"}} {{message}}
diff --git a/app/assets/javascripts/admin/templates/user.js.handlebars b/app/assets/javascripts/admin/templates/user.js.handlebars index c109eb7b853..1d29846be58 100644 --- a/app/assets/javascripts/admin/templates/user.js.handlebars +++ b/app/assets/javascripts/admin/templates/user.js.handlebars @@ -139,6 +139,14 @@
{{i18n admin.user.post_count}}
{{content.post_count}}
+
+ {{#if content.can_delete_all_posts}} + + {{/if}} +
{{i18n admin.user.posts_read_count}}
diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index f9c842b199d..6186d40b220 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -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) diff --git a/app/models/post.rb b/app/models/post.rb index 42d7852019d..550de6fe2b1 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -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 diff --git a/app/models/site_customization.rb b/app/models/site_customization.rb index af2dca2f063..625b907ce21 100644 --- a/app/models/site_customization.rb +++ b/app/models/site_customization.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 91ce8efde0f..3ed14f3b015 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/serializers/admin_detailed_user_serializer.rb b/app/serializers/admin_detailed_user_serializer.rb index c23bc79ff6a..d1254f553ed 100644 --- a/app/serializers/admin_detailed_user_serializer.rb +++ b/app/serializers/admin_detailed_user_serializer.rb @@ -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 @@ -19,6 +20,10 @@ class AdminDetailedUserSerializer < AdminUserSerializer def can_grant_admin 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) diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 94ec95c7e43..106abd5f98d 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -50,11 +50,7 @@ <%- unless SiteCustomization.override_default_style(session[:preview_style]) %> - <%- if params[:shiny] %> - <%=stylesheet_link_tag "shiny/shiny"%> - <%- else %> - <%=stylesheet_link_tag "application"%> - <%- end %> + <%=stylesheet_link_tag "application"%> <%- end %> <%- if mini_profiler_enabled? %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 27c3ff8e3b9..db014fc301a 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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?" diff --git a/config/routes.rb b/config/routes.rb index b0a8db09293..9bcd44afd2a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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' diff --git a/lib/guardian.rb b/lib/guardian.rb index 72f475a4ee2..48b4d412871 100644 --- a/lib/guardian.rb +++ b/lib/guardian.rb @@ -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_(.*)\!$/ diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index e758f7311b7..28b25af358b 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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