diff --git a/app/controllers/dmails_controller.rb b/app/controllers/dmails_controller.rb index b68033e82..51275da70 100644 --- a/app/controllers/dmails_controller.rb +++ b/app/controllers/dmails_controller.rb @@ -17,7 +17,7 @@ class DmailsController < ApplicationController if params[:folder] && params[:set_default_folder] cookies.permanent[:dmail_folder] = params[:folder] end - @query = Dmail.visible.search(params[:search]) + @query = Dmail.active.visible.search(params[:search]) @dmails = @query.order("dmails.created_at desc").paginate(params[:page], :limit => params[:limit]) respond_with(@dmails) do |format| format.xml do diff --git a/app/controllers/maintenance/user/dmail_filters_controller.rb b/app/controllers/maintenance/user/dmail_filters_controller.rb new file mode 100644 index 000000000..1d7b06d00 --- /dev/null +++ b/app/controllers/maintenance/user/dmail_filters_controller.rb @@ -0,0 +1,29 @@ +module Maintenance + module User + class DmailFiltersController < ApplicationController + before_filter :ensure_ownership + before_filter :member_only + + def edit + @dmail_filter = CurrentUser.dmail_filter || DmailFilter.new + end + + def update + @dmail_filter = CurrentUser.dmail_filter || DmailFilter.new + @dmail_filter.update_attributes(params[:dmail_filter]) + flash[:notice] = "Filter updated" + redirect_to(dmail_path(@dmail.id)) + end + + private + + def ensure_ownership + @dmail = Dmail.find(params[:dmail_id]) + + if @dmail.owner_id != CurrentUser.user.id + raise User::PrivilegeError.new + end + end + end + end +end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 57e1e203e..a9dff7022 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -76,11 +76,16 @@ module ApplicationHelper time_tag(time.strftime("%Y-%m-%d %H:%M"), time) end - def link_to_user(user) + def link_to_user(user, options = {}) user_class = user.level_class user_class = user_class + " user-post-approver" if user.can_approve_posts? user_class = user_class + " with-style" if CurrentUser.user.style_usernames? - link_to(user.pretty_name, user_path(user), :class => user_class) + if options[:raw_name] + name = user.name + else + name = user.pretty_name + end + link_to(name, user_path(user), :class => user_class) end def mod_link_to_user(user, positive_or_negative) diff --git a/app/logical/anonymous_user.rb b/app/logical/anonymous_user.rb index 8d44c5ec0..70f7a2a0f 100644 --- a/app/logical/anonymous_user.rb +++ b/app/logical/anonymous_user.rb @@ -20,6 +20,10 @@ class AnonymousUser Time.now end + def dmail_filter + nil + end + def name "Anonymous" end diff --git a/app/models/dmail_filter.rb b/app/models/dmail_filter.rb index 9c7186612..4bdefc671 100644 --- a/app/models/dmail_filter.rb +++ b/app/models/dmail_filter.rb @@ -11,7 +11,7 @@ class DmailFilter < ActiveRecord::Base end def filtered?(dmail) - dmail.from.level <= User::Levels::MODERATOR && has_filter? && (dmail.body =~ regexp || dmail.title =~ regexp) + dmail.from.level <= User::Levels::MODERATOR && has_filter? && (dmail.body =~ regexp || dmail.title =~ regexp || dmail.from.name =~ regexp) end def has_filter? @@ -19,6 +19,6 @@ class DmailFilter < ActiveRecord::Base end def regexp - @regexp ||= Regexp.compile(words.scan(/\S+/).map {|x| Regexp.escape(x)}.join("|")) + @regexp ||= Regexp.compile('\b(?:' + words.scan(/\S+/).map {|x| Regexp.escape(x)}.join("|") + ')\b') end end diff --git a/app/views/dmails/show.html.erb b/app/views/dmails/show.html.erb index 0e4964bff..39d8e926c 100644 --- a/app/views/dmails/show.html.erb +++ b/app/views/dmails/show.html.erb @@ -21,6 +21,7 @@

<%= link_to "Respond", new_dmail_path(:respond_to_id => @dmail) %> | <%= link_to "Forward", new_dmail_path(:respond_to_id => @dmail, :forward => true) %> + | <%= link_to "Filter messages like these", edit_maintenance_user_dmail_filter_path(:dmail_id => @dmail.id) %>

diff --git a/app/views/maintenance/user/dmail_filters/edit.html.erb b/app/views/maintenance/user/dmail_filters/edit.html.erb new file mode 100644 index 000000000..e08698976 --- /dev/null +++ b/app/views/maintenance/user/dmail_filters/edit.html.erb @@ -0,0 +1,37 @@ +
+
+

Edit Message Filters

+ +
+

<%= @dmail.title %>

+ +
    +
  • Sender: <%= link_to_user @dmail.from %>
  • +
  • Recipient: <%= link_to_user @dmail.to, :raw => true %>
  • +
  • Date: <%= compact_time(@dmail.created_at) %>
  • + <% if CurrentUser.user.is_moderator? %> +
  • Sender IP: <%= @dmail.creator_ip_addr %>
  • + <% end %> +
+ +

Body

+
+ <%= format_text(@dmail.body) %> +
+
+ + <%= simple_form_for @dmail_filter, :url => maintenance_user_dmail_filter_path(:dmail_id => @dmail.id), :method => :put do |f| %> +
+ + <%= text_area_tag "dmail_filter[words]", @dmail_filter.words, :id => "dmail_filter_words", :class => "text", :style => "height: 10em;" %> +

A list of banned words or users (space delimited). Any message you receive with a banned word will automatically be deleted. Make sure user names have no spaces in them (replace with underscores).

+
+ + <%= f.button :submit, "Submit" %> + <% end %> +
+
+ +<% content_for(:page_title) do %> + Edit Message Filters - <%= Danbooru.config.app_name %> +<% end %> diff --git a/config/routes.rb b/config/routes.rb index 25571b193..9a7f03ad3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -56,6 +56,7 @@ Rails.application.routes.draw do resource :login_reminder, :only => [:new, :create] resource :deletion, :only => [:show, :destroy] resource :email_change, :only => [:new, :create] + resource :dmail_filter, :only => [:edit, :update] end end diff --git a/test/models/dmail_filter_test.rb b/test/models/dmail_filter_test.rb index da59193f8..d4dc7d615 100644 --- a/test/models/dmail_filter_test.rb +++ b/test/models/dmail_filter_test.rb @@ -1,7 +1,43 @@ require 'test_helper' class DmailFilterTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end + def setup + super + + @receiver = FactoryGirl.create(:user) + @sender = FactoryGirl.create(:user) + end + + def create_dmail(body, title) + CurrentUser.scoped(@sender, "127.0.0.1") do + Dmail.create_split(:to_id => @receiver.id, :body => body, :title => title) + end + end + + context "a dmail filter for a word" do + setup do + @dmail_filter = @receiver.create_dmail_filter(:words => "banned") + end + + should "filter on that word in the body" do + create_dmail("banned", "okay") + assert_equal(true, @receiver.dmails.last.is_deleted?) + end + + should "filter on that word in the title" do + create_dmail("okay", "banned") + assert_equal(true, @receiver.dmails.last.is_deleted?) + end + end + + context "a dmail filter for a user name" do + setup do + @dmail_filter = @receiver.create_dmail_filter(:words => @sender.name) + end + + should "filter on the sender" do + create_dmail("okay", "okay") + assert_equal(true, @receiver.dmails.last.is_deleted?) + end + end end