From b944b642b883ad58dea6de310385d68f37b8b147 Mon Sep 17 00:00:00 2001 From: r888888888 Date: Thu, 14 Sep 2017 13:19:16 -0700 Subject: [PATCH] akismet integration --- Gemfile | 1 + Gemfile.lock | 2 ++ app/controllers/dmails_controller.rb | 13 +++++++++ app/helpers/dmails_helper.rb | 4 +++ app/models/dmail.rb | 29 +++++++++++++++++-- app/views/dmails/_secondary_links.html.erb | 1 + app/views/dmails/ham.js.erb | 1 + app/views/dmails/show.html.erb | 7 +++++ app/views/dmails/spam.js.erb | 1 + config/application.rb | 5 ++++ config/routes.rb | 4 +++ .../20170914200122_add_is_spam_to_dmails.rb | 7 +++++ db/structure.sql | 5 +++- test/unit/dmail_test.rb | 13 +++++++++ 14 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 app/views/dmails/ham.js.erb create mode 100644 app/views/dmails/spam.js.erb create mode 100644 db/migrate/20170914200122_add_is_spam_to_dmails.rb diff --git a/Gemfile b/Gemfile index 41c590912..77e28931b 100644 --- a/Gemfile +++ b/Gemfile @@ -46,6 +46,7 @@ gem 'oauth2' gem 'bootsnap' gem 'addressable' gem 'httparty' +gem 'rakismet' # needed for looser jpeg header compat gem 'ruby-imagespec', :require => "image_spec", :git => "https://github.com/r888888888/ruby-imagespec.git", :branch => "exif-fixes" diff --git a/Gemfile.lock b/Gemfile.lock index 0b9e21c4b..bd565cc45 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -277,6 +277,7 @@ GEM thor (>= 0.18.1, < 2.0) raindrops (0.17.0) rake (12.0.0) + rakismet (1.5.4) ref (2.0.0) representable (2.3.0) uber (~> 0.0.7) @@ -432,6 +433,7 @@ DEPENDENCIES pry-byebug radix62 (~> 1.0.1) rails (~> 4.2.0) + rakismet responders rmagick ruby-imagespec! diff --git a/app/controllers/dmails_controller.rb b/app/controllers/dmails_controller.rb index 1ffc2abf5..9483e333b 100644 --- a/app/controllers/dmails_controller.rb +++ b/app/controllers/dmails_controller.rb @@ -1,6 +1,7 @@ class DmailsController < ApplicationController respond_to :html, :xml, :json before_filter :member_only, except: [:index, :show, :destroy, :mark_all_as_read] + before_filter :gold_only, only: [:ham, :spam] def new if params[:respond_to_id] @@ -55,6 +56,18 @@ class DmailsController < ApplicationController CurrentUser.user.save end + def spam + @dmail = Dmail.find(params[:id]) + @dmail.update_column(:is_spam, true) + @dmail.spam! + end + + def ham + @dmail = Dmail.find(params[:id]) + @dmail.update_column(:is_spam, false) + @dmail.ham! + end + private def check_privilege(dmail) diff --git a/app/helpers/dmails_helper.rb b/app/helpers/dmails_helper.rb index 3e700b95d..a61ac948a 100644 --- a/app/helpers/dmails_helper.rb +++ b/app/helpers/dmails_helper.rb @@ -18,6 +18,10 @@ module DmailsHelper dmails_path(search: {from_id: CurrentUser.id}, folder: "sent", **params) end + def spam_dmails_path + dmails_path(search: {to_id: CurrentUser.id, is_spam: true}, folder: "spam") + end + def received_dmails_path(params = {}) dmails_path(search: {to_id: CurrentUser.id}, folder: "received", **params) end diff --git a/app/models/dmail.rb b/app/models/dmail.rb index 02f6d8141..93288c21a 100644 --- a/app/models/dmail.rb +++ b/app/models/dmail.rb @@ -1,6 +1,8 @@ require 'digest/sha1' class Dmail < ApplicationRecord + include Rakismet::Model + with_options on: :create do validates_presence_of :to_id validates_presence_of :from_id @@ -18,6 +20,18 @@ class Dmail < ApplicationRecord after_create :update_recipient after_create :send_dmail + rakismet_attrs author: :from_name, author_email: :from_email, content: :title_and_body, user_ip: :creator_ip_addr_str + + concerning :SpamMethods do + def title_and_body + "#{title}\n\n#{body}" + end + + def creator_ip_addr_str + creator_ip_addr.to_s + end + end + module AddressMethods def to_name User.id_to_pretty_name(to_id) @@ -27,6 +41,10 @@ class Dmail < ApplicationRecord User.id_to_pretty_name(from_id) end + def from_email + from.email + end + def to_name=(name) self.to_id = User.name_to_id(name) end @@ -34,6 +52,8 @@ class Dmail < ApplicationRecord def initialize_attributes self.from_id ||= CurrentUser.id self.creator_ip_addr ||= CurrentUser.ip_addr + self.is_spam = spam? + true end end @@ -160,6 +180,12 @@ class Dmail < ApplicationRecord q = q.where("from_id = ?", params[:from_id].to_i) end + if params[:is_spam].present? + q = q.where("is_spam = ?", true) + else + q = q.where("is_spam = ?", false) + end + if params[:read] == "true" q = q.where("is_read = true") elsif params[:read] == "false" @@ -189,7 +215,7 @@ class Dmail < ApplicationRecord end def send_dmail - if to.receive_email_notifications? && to.email =~ /@/ && owner_id == to.id + if !is_spam? && to.receive_email_notifications? && to.email =~ /@/ && owner_id == to.id UserMailer.dmail_notice(self).deliver_now end end @@ -230,5 +256,4 @@ class Dmail < ApplicationRecord def visible_to?(user, key) owner_id == user.id || (user.is_moderator? && key == self.key) end - end diff --git a/app/views/dmails/_secondary_links.html.erb b/app/views/dmails/_secondary_links.html.erb index 59c205b62..c1f3debb5 100644 --- a/app/views/dmails/_secondary_links.html.erb +++ b/app/views/dmails/_secondary_links.html.erb @@ -4,6 +4,7 @@
  • <%= link_to "All", all_dmails_path(set_default_folder: true) %>
  • <%= link_to "Received", received_dmails_path(set_default_folder: true) %>
  • <%= link_to "Sent", sent_dmails_path(set_default_folder: true) %>
  • +
  • <%= link_to "Spam", spam_dmails_path %>
  • |
  • <%= link_to "New", new_dmail_path %>
  • <%= link_to "Mark all as read", {:controller => "dmails", :action => "mark_all_as_read"}, :method => :post, :remote => true %>
  • diff --git a/app/views/dmails/ham.js.erb b/app/views/dmails/ham.js.erb new file mode 100644 index 000000000..2912c6f60 --- /dev/null +++ b/app/views/dmails/ham.js.erb @@ -0,0 +1 @@ +Danbooru.notice("Message marked as not spam"); \ No newline at end of file diff --git a/app/views/dmails/show.html.erb b/app/views/dmails/show.html.erb index 0600a0b1e..cc3db5d13 100644 --- a/app/views/dmails/show.html.erb +++ b/app/views/dmails/show.html.erb @@ -29,6 +29,13 @@ | <%= 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) %> | <%= link_to "Permalink", dmail_path(@dmail, :key => @dmail.key), :title => "Use this URL to privately share with a moderator" %> + <% if CurrentUser.is_gold? %> + <% if @dmail.is_spam? %> + | <%= link_to "Not spam", ham_dmail_path(@dmail) %> + <% else %> + | <%= link_to "Mark as spam", spam_dmail_path(@dmail) %> + <% end %> + <% end %>

    diff --git a/app/views/dmails/spam.js.erb b/app/views/dmails/spam.js.erb new file mode 100644 index 000000000..b0002deb8 --- /dev/null +++ b/app/views/dmails/spam.js.erb @@ -0,0 +1 @@ +Danbooru.notice("Message marked as spam"); \ No newline at end of file diff --git a/config/application.rb b/config/application.rb index b5627814a..4fa9af05c 100644 --- a/config/application.rb +++ b/config/application.rb @@ -30,6 +30,11 @@ module Danbooru config.x.git_hash = nil end + if ENV["DANBOORU_RAKISMET_KEY"] + config.rakismet.key = ENV["DANBOORU_RAKISMET_KEY"] + config.rakismet.url = ENV["DANBOORU_RAKISMET_URL"] + end + config.after_initialize do Rails.application.routes.default_url_options = { host: Danbooru.config.hostname, diff --git a/config/routes.rb b/config/routes.rb index 3a1e57cde..fb9a00372 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -117,6 +117,10 @@ Rails.application.routes.draw do end end resources :dmails, :only => [:new, :create, :index, :show, :destroy] do + member do + post :spam + post :ham + end collection do post :mark_all_as_read end diff --git a/db/migrate/20170914200122_add_is_spam_to_dmails.rb b/db/migrate/20170914200122_add_is_spam_to_dmails.rb new file mode 100644 index 000000000..d7aa83142 --- /dev/null +++ b/db/migrate/20170914200122_add_is_spam_to_dmails.rb @@ -0,0 +1,7 @@ +class AddIsSpamToDmails < ActiveRecord::Migration + def change + Dmail.without_timeout do + add_column :dmails, :is_spam, :boolean, default: false + end + end +end diff --git a/db/structure.sql b/db/structure.sql index cba8d3098..6536a87a9 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -1009,7 +1009,8 @@ CREATE TABLE dmails ( is_deleted boolean DEFAULT false NOT NULL, created_at timestamp without time zone, updated_at timestamp without time zone, - creator_ip_addr inet NOT NULL + creator_ip_addr inet NOT NULL, + is_spam boolean DEFAULT false ); @@ -7513,3 +7514,5 @@ INSERT INTO schema_migrations (version) VALUES ('20170613200356'); INSERT INTO schema_migrations (version) VALUES ('20170709190409'); +INSERT INTO schema_migrations (version) VALUES ('20170914200122'); + diff --git a/test/unit/dmail_test.rb b/test/unit/dmail_test.rb index b97b4aacf..120f20567 100644 --- a/test/unit/dmail_test.rb +++ b/test/unit/dmail_test.rb @@ -15,6 +15,19 @@ class DmailTest < ActiveSupport::TestCase CurrentUser.user = nil end + context "spam" do + setup do + @recipient = FactoryGirl.create(:user) + end + + should "not validate" do + assert_difference("Dmail.count", 2)do + dmail = Dmail.create_split(:to_id => @recipient.id, :title => "My video", :body => "hey Noneeditsonlyme. My webcam see here http://bit.ly/2vTv9Ki") + assert(dmail.is_spam?) + end + end + end + context "filter" do setup do @recipient = FactoryGirl.create(:user)