removes whitespaces and uses scope

This commit is contained in:
Gerhard Schlager 2017-11-13 15:20:36 +01:00
parent 7370adeae3
commit d3baae5365
3 changed files with 15 additions and 13 deletions

View File

@ -4,6 +4,8 @@ class IncomingEmail < ActiveRecord::Base
belongs_to :post belongs_to :post
scope :errored, -> { where("NOT is_bounce AND error IS NOT NULL") } scope :errored, -> { where("NOT is_bounce AND error IS NOT NULL") }
scope :addressed_to, -> (email) { where('incoming_emails.to_addresses ILIKE :email OR incoming_emails.cc_addresses ILIKE :email', email: "%#{email}%") }
end end
# == Schema Information # == Schema Information

View File

@ -446,8 +446,8 @@ module Email
incoming_emails = IncomingEmail incoming_emails = IncomingEmail
.joins(:post) .joins(:post)
.where('posts.topic_id = ?', email_log.topic_id) .where('posts.topic_id = ?', email_log.topic_id)
.where('incoming_emails.to_addresses ILIKE :email OR incoming_emails.cc_addresses ILIKE :email', email: "%#{email_log.reply_key}%") .addressed_to(email_log.reply_key)
.where('incoming_emails.to_addresses ILIKE :email OR incoming_emails.cc_addresses ILIKE :email', email: "%#{user.email}%") .addressed_to(user.email)
incoming_emails.each do |email| incoming_emails.each do |email|
next unless contains_email_address?(email.to_addresses, user.email) || next unless contains_email_address?(email.to_addresses, user.email) ||

View File

@ -399,18 +399,18 @@ describe Email::Receiver do
it "accepts emails with wrong reply key if the system knows about the forwareded email" do it "accepts emails with wrong reply key if the system knows about the forwareded email" do
Fabricate(:incoming_email, Fabricate(:incoming_email,
raw: <<~RAW, raw: <<~RAW,
Return-Path: <discourse@bar.com> Return-Path: <discourse@bar.com>
From: Alice <discourse@bar.com> From: Alice <discourse@bar.com>
To: dave@bar.com, reply+4f97315cc828096c9cb34c6f1a0d6fe8@bar.com To: dave@bar.com, reply+4f97315cc828096c9cb34c6f1a0d6fe8@bar.com
CC: carol@bar.com, bob@bar.com CC: carol@bar.com, bob@bar.com
Subject: Hello world Subject: Hello world
Date: Fri, 15 Jan 2016 00:12:43 +0100 Date: Fri, 15 Jan 2016 00:12:43 +0100
Message-ID: <10@foo.bar.mail> Message-ID: <10@foo.bar.mail>
Mime-Version: 1.0 Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8 Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable Content-Transfer-Encoding: quoted-printable
This post was created by email. This post was created by email.
RAW RAW
from_address: "discourse@bar.com", from_address: "discourse@bar.com",
to_addresses: "dave@bar.com;reply+4f97315cc828096c9cb34c6f1a0d6fe8@bar.com", to_addresses: "dave@bar.com;reply+4f97315cc828096c9cb34c6f1a0d6fe8@bar.com",