akismet integration

This commit is contained in:
r888888888
2017-09-14 13:19:16 -07:00
parent d12f082f9a
commit b944b642b8
14 changed files with 90 additions and 3 deletions

View File

@@ -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"

View File

@@ -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!

View File

@@ -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)

View File

@@ -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

View File

@@ -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

View File

@@ -4,6 +4,7 @@
<li><%= link_to "All", all_dmails_path(set_default_folder: true) %></li>
<li><%= link_to "Received", received_dmails_path(set_default_folder: true) %></li>
<li><%= link_to "Sent", sent_dmails_path(set_default_folder: true) %></li>
<li><%= link_to "Spam", spam_dmails_path %></li>
<li>|</li>
<li><%= link_to "New", new_dmail_path %></li>
<li><%= link_to "Mark all as read", {:controller => "dmails", :action => "mark_all_as_read"}, :method => :post, :remote => true %></li>

View File

@@ -0,0 +1 @@
Danbooru.notice("Message marked as not spam");

View File

@@ -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 %>
</p>
</div>
</div>

View File

@@ -0,0 +1 @@
Danbooru.notice("Message marked as spam");

View File

@@ -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,

View File

@@ -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

View File

@@ -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

View File

@@ -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');

View File

@@ -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)