Merge pull request #3302 from r888888888/akismet

akismet integration
This commit is contained in:
Albert Yi
2017-09-14 14:02:15 -07:00
committed by GitHub
14 changed files with 94 additions and 3 deletions

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,2 @@
Danbooru.notice("Message marked as not spam");
$("#spam-links").hide();

View File

@@ -29,6 +29,15 @@
| <%= 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? %>
<span id="spam-links">
<% if @dmail.is_spam? %>
| <%= link_to "Not spam", ham_dmail_path(@dmail), remote: :true, method: :post %>
<% else %>
| <%= link_to "Mark as spam", spam_dmail_path(@dmail), remote: :true, method: :post %>
<% end %>
</span>
<% end %>
</p>
</div>
</div>

View File

@@ -0,0 +1,2 @@
Danbooru.notice("Message marked as spam");
$("#spam-links").hide();