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