Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-griffen committed Jul 28, 2023
1 parent 1173b74 commit deddd0b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 48 deletions.
11 changes: 6 additions & 5 deletions app/mailers/request_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,22 +211,23 @@ def comment_on_alert_plural(info_request, count, earliest_unalerted_comment)


def guess_email_request_address(email)
# The percentage similarity the id or idhash much fulfil
email_guess_threshold = 0.8
# Match the email address in the message without matching the hash
guess_addresses = MailHandler.get_all_addresses(email)
guessed_info_requests = InfoRequest.guess_by_incoming_email(guess_addresses)

# Match the email subject in the message
guess_by_subject = InfoRequest.guess_by_incoming_subject(email.subject)
# guess_by_subject = InfoRequest.guess_by_incoming_subject(email.subject)

(guessed_info_requests + guess_by_subject).
# (guessed_info_requests + guess_by_subject).
guessed_info_requests.
select do |ir_guess|

id_score = ir_guess.id_score
idhash_score = ir_guess.idhash_score

# p [id_score, idhash_score]

id_score == 1 && idhash_score >= 0.8 || id_score >= 0.8 && idhash_score == 1
id_score == 1 && idhash_score >= email_guess_threshold || id_score >= email_guess_threshold && idhash_score == 1

end.map { |ir_guess| ir_guess.info_request }.uniq
end
Expand Down
26 changes: 26 additions & 0 deletions app/models/guess.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'text'
class Guess
attr_reader :info_request, :match_method, :components

def initialize(info_request, match_method, *components)
@info_request = info_request
@match_method = match_method
@components = components
end

def id_score
return 1 unless %i(id idhash).include?(@match_method)
id, _, _ = @components
Text::WhiteSimilarity.similarity(id.to_s, @info_request.id.to_s)
end

def idhash_score
return 1 unless %i(id idhash).include?(@match_method)
_, idhash, _ = @components
Text::WhiteSimilarity.similarity(idhash.to_s, @info_request.idhash.to_s)
end

def ==(other)
return @info_request == other.info_request && @match_method == other.match_method && @components == other.components
end
end
21 changes: 4 additions & 17 deletions app/models/info_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,9 @@

require 'digest/sha1'
require 'fileutils'
require 'text'
require 'guess'

class InfoRequest < ApplicationRecord
Guess = Struct.new(:info_request, :matched_value, :match_method, :components) do
def id_score
return 1 unless %i(id idhash).include?(match_method)
id, _ = *components
Text::WhiteSimilarity.similarity(id.to_s, info_request.id.to_s)
end

def idhash_score
return 1 unless %i(id idhash).include?(match_method)
_, idhash = *components
Text::WhiteSimilarity.similarity(idhash, info_request.idhash)
end
end.freeze

OLD_AGE_IN_DAYS = 21.days

Expand Down Expand Up @@ -258,8 +245,8 @@ def self.guess_by_incoming_email(*emails)
guesses = emails.flatten.reduce([]) do |memo, email|
id, idhash = _extract_id_hash_from_email(email)
id, idhash = _guess_idhash_from_email(email) if idhash.nil? || id.nil?
memo << Guess.new(find_by_id(id), email, :id, [id, idhash])
memo << Guess.new(find_by_idhash(idhash), email, :idhash, [id, idhash])
memo << Guess.new(find_by_id(id), :id, id, idhash)
memo << Guess.new(find_by_idhash(idhash), :idhash, id, idhash)
end

# Unique Guesses where we've found an `InfoRequest`
Expand Down Expand Up @@ -328,7 +315,7 @@ def self.guess_by_incoming_subject(subject_line)
limit(25)

guesses = requests.each.reduce([]) do |memo, request|
memo << Guess.new(request, subject_line, :subject)
memo << Guess.new(request, :subject, nil, nil, subject_line)
end

# Unique Guesses where we've found an `InfoRequest`
Expand Down
52 changes: 26 additions & 26 deletions spec/models/info_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1869,7 +1869,7 @@

context 'email with an intact id and broken idhash' do
let(:email) { "request-#{ info_request.id }-asdfg@example.com" }
let(:guess) { described_class::Guess.new(info_request, email, :id) }
let(:guess) { Guess.new(info_request, :id, info_request.id, 'asdfg') }
it { is_expected.to include(guess) }
end

Expand All @@ -1878,7 +1878,7 @@
"request-#{ info_request.id }ab-#{ info_request.idhash }@example.com"
end

let(:guess) { described_class::Guess.new(info_request, email, :idhash) }
let(:guess) { Guess.new(info_request, :idhash, nil, info_request.idhash) }
it { is_expected.to include(guess) }
end

Expand All @@ -1887,13 +1887,13 @@
"request-a12x3b-#{ info_request.idhash }@example.com"
end

let(:guess) { described_class::Guess.new(info_request, email, :idhash) }
let(:guess) { Guess.new(info_request, :idhash, nil, info_request.idhash) }
it { is_expected.to include(guess) }
end

context 'upper case email with a broken id and otherwise intact idhash' do
let(:email) { "REQUEST-123a-#{ info_request.idhash.upcase }@example.com" }
let(:guess) { described_class::Guess.new(info_request, email, :idhash) }
let(:guess) { Guess.new(info_request, :idhash, nil, info_request.idhash) }
it { is_expected.to include(guess) }
end

Expand All @@ -1902,7 +1902,7 @@
"request#{ info_request.id }#{ info_request.idhash }@example.com"
end

let(:guess) { described_class::Guess.new(info_request, email, :id) }
let(:guess) { Guess.new(info_request, :id, info_request.id, info_request.idhash) }
it { is_expected.to include(guess) }
end

Expand All @@ -1911,33 +1911,33 @@
"request-#{ info_request.id }#{ info_request.idhash }@example.com"
end

let(:guess) { described_class::Guess.new(info_request, email, :id) }
let(:guess) { Guess.new(info_request, :id, info_request.id, info_request.idhash) }
it { is_expected.to include(guess) }
end

context 'email with a broken id and an intact idhash but missing punctuation' do
let(:email) { "request123ab#{ info_request.idhash }@example.com" }
let(:guess) { described_class::Guess.new(info_request, email, :idhash) }
let(:guess) { Guess.new(info_request, :idhash, nil, info_request.idhash) }
it { is_expected.to include(guess) }
end

context 'email with an intact id and a broken idhash but missing punctuation' do
let(:email) { "request#{ info_request.id }abcdefgh@example.com" }
let(:guess) { described_class::Guess.new(info_request, email, :id) }
let(:guess) { Guess.new(info_request, :id, info_request.id, "abcdefgh") }
it { is_expected.to include(guess) }
end

context 'email with an id mistyped using letters and missing punctuation' do
before { InfoRequest.where(id: 1_231_014).destroy_all }
let!(:info_request) { FactoryBot.create(:info_request, id: 1_231_014) }
let(:email) { 'request-123loL4abcdefgh@example.com' }
let(:guess) { described_class::Guess.new(info_request, email, :id) }
let(:guess) { Guess.new(info_request, :id, info_request.id, "abcdefgh") }
it { is_expected.to include(guess) }
end

context 'email with a broken id and an intact idhash but broken format' do
let(:email) { "reqeust=123ab#{ info_request.idhash }@example.com" }
let(:guess) { described_class::Guess.new(info_request, email, :idhash) }
let(:guess) { Guess.new(info_request, :idhash, nil, info_request.idhash) }
it { is_expected.to include(guess) }
end

Expand All @@ -1959,10 +1959,10 @@
"request-#{ info_request_1.id }-#{ info_request_2.idhash }@example.com"
end

let(:guess_1) { described_class::Guess.new(info_request_1, email, :id) }
let(:guess_1) { Guess.new(info_request_1, :id, info_request_1.id, info_request_2.idhash) }

let(:guess_2) do
described_class::Guess.new(info_request_2, email, :idhash)
Guess.new(info_request_2, :idhash, info_request_1.id, info_request_2.idhash)
end

it { is_expected.to match_array([guess_1, guess_2]) }
Expand All @@ -1972,7 +1972,7 @@
let(:email_1) { "request-123ab-#{ info_request.idhash }@example.com" }
let(:email_2) { "request-#{ info_request.id }-asdfg@example.com" }
let(:email) { [email_1, email_2] }
let(:guess) { described_class::Guess.new(info_request, email_1, :idhash) }
let(:guess) { Guess.new(info_request, :idhash, nil, info_request.idhash) }

it { is_expected.to match_array([guess]) }
end
Expand All @@ -1991,10 +1991,10 @@

let(:email) { [email_1, email_2] }

let(:guess_1) { described_class::Guess.new(info_request_1, email_1, :id) }
let(:guess_1) { Guess.new(info_request_1, :id, info_request_1.id, info_request_2.idhash) }

let(:guess_2) do
described_class::Guess.new(info_request_2, email_1, :idhash)
Guess.new(info_request_2, :idhash, info_request_1.id, info_request_2.idhash)
end

it { is_expected.to match_array([guess_1, guess_2]) }
Expand All @@ -2015,7 +2015,7 @@
let(:subject_line) { info_request.email_subject_followup }

let(:guess) do
described_class::Guess.new(info_request, subject_line, :subject)
Guess.new(info_request, :subject, nil, nil, subject_line)
end

it { is_expected.to include(guess) }
Expand All @@ -2027,7 +2027,7 @@
end

let(:guess) do
described_class::Guess.new(info_request, subject_line, :subject)
Guess.new(info_request, :subject, nil, nil, subject_line)
end

it { is_expected.to include(guess) }
Expand All @@ -2047,11 +2047,11 @@
end

let(:guess_1) do
described_class::Guess.new(info_request_1, subject_line, :subject)
Guess.new(info_request_1, :subject, nil, nil, subject_line)
end

let(:guess_2) do
described_class::Guess.new(info_request_2, subject_line, :subject)
Guess.new(info_request_2, :subject, nil, nil, subject_line)
end

it { is_expected.to match_array([guess_1, guess_2]) }
Expand All @@ -2075,7 +2075,7 @@
let(:subject_line) { 'Our ref: 12345678' }

let(:guess) do
described_class::Guess.new(info_request, subject_line, :subject)
Guess.new(info_request, :subject, nil, nil, subject_line)
end

it { is_expected.to include(guess) }
Expand All @@ -2091,9 +2091,9 @@
end

let(:guess) do
described_class::Guess.new(InfoRequest.holding_pen_request,
subject_line,
:subject)
Guess.new(InfoRequest.holding_pen_request,
:subject,
nil, nil, subject_line)
end

it { is_expected.to_not include(guess) }
Expand All @@ -2108,7 +2108,7 @@
end

let(:guess) do
described_class::Guess.new(info_request, subject_line, :subject)
Guess.new(info_request, :subject, nil, nil, subject_line)
end

it { is_expected.to include(guess) }
Expand All @@ -2126,11 +2126,11 @@
end

let(:guess_1) do
described_class::Guess.new(info_request_1, subject_line, :subject)
Guess.new(info_request_1, :subject, nil, nil, subject_line)
end

let(:guess_2) do
described_class::Guess.new(info_request_2, subject_line, :subject)
Guess.new(info_request_2, :subject, nil, nil, subject_line)
end

it { is_expected.to match_array([guess_1, guess_2]) }
Expand Down

0 comments on commit deddd0b

Please sign in to comment.