Skip to content

Commit

Permalink
AO3-6563 Attach HTML download copies of works to abuse tickets (#4748)
Browse files Browse the repository at this point in the history
* AO3-6563 Attach work download on abuse report

* AO3-6563 Fix?

* AO3-6563 Tidy

* AO3-6563 Revert "AO3-6563 Fix?"

This reverts commit 09ab379.

* AO3-6563 Correct method name

* AO3-6563 Make rubodog happy

* AO3-6563 Make rubodog happy 2

* AO3-6563 Tidy suggested

* AO3-6563 Fixes

* AO3-6563 Add a new test for feedback reporter

* Update abuse_report.rb

* AO3-6563 Feedback

* AO3-6563 Fix test
  • Loading branch information
weeklies authored Aug 14, 2024
1 parent 61a4f52 commit d53e9da
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 6 deletions.
7 changes: 7 additions & 0 deletions app/jobs/report_attachment_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ReportAttachmentJob < ApplicationJob
def perform(ticket_id, work)
download = Download.new(work, mime_type: "text/html")
html = DownloadWriter.new(download).generate_html
reporter.send_attachment!(ticket_id, html)
end
end
16 changes: 14 additions & 2 deletions app/models/abuse_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def add_work_id_to_url(url)
work_id = Chapter.find_by(id: chapter_id).try(:work_id)

return url if work_id.nil?

uri = Addressable::URI.parse(url)
uri.path = "/works/#{work_id}" + uri.path

Expand Down Expand Up @@ -116,7 +116,19 @@ def send_report
ip_address: ip_address,
url: url
)
reporter.send_report!
response = reporter.send_report!
ticket_id = response.fetch("ticketNumber")
return if ticket_id.blank?

attach_work_download(ticket_id)
end

def attach_work_download(ticket_id)
work_id = url[%r{/works/(\d+)}, 1]
return unless work_id

work = Work.find_by(id: work_id)
ReportAttachmentJob.perform_later(ticket_id, work) if work
end

# if the URL clearly belongs to a work (i.e. contains "/works/123")
Expand Down
11 changes: 11 additions & 0 deletions app/models/feedback_reporters/feedback_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ def send_report!
zoho_resource_client.create_ticket(ticket_attributes: report_attributes)
end

def send_attachment!(id, download)
zoho_resource_client.create_ticket_attachment(
ticket_id: id,
attachment_attributes: attachment_attributes(download)
)
end

def report_attributes
{
"email" => email,
Expand All @@ -44,6 +51,10 @@ def report_attributes
}
end

def attachment_attributes(download)
{ file: download }
end

private

def zoho_contact_id
Expand Down
7 changes: 4 additions & 3 deletions config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ OTWALT_LOGO: 'OTW Logo:closing the circle of the copyright symbol, it symbolizes
REVISION: ''
ES_URL: 'http://127.0.0.1:9400'
MEMCACHED_SERVERS: '127.0.0.1:11211'
ZOHO_URL: 'https://desk.zoho.com'

# tag settings
DELIMITER_FOR_INPUT: ',' # if you change this, you will need to change
Expand Down Expand Up @@ -348,7 +349,7 @@ BANNED_MULTIMEDIA_SRCS: []
#

# We use this list for properties which can take shorthand values and also have
# subproperties or variants (ie, because "border" is in here, we allow through:
# subproperties or variants (ie, because "border" is in here, we allow through:
# border-right, border-bottom-left-radius, -moz-border-foo, etc)
# It is slightly less secure so do NOT put any property in here if you aren't
# sure all variations on it are okay!
Expand Down Expand Up @@ -553,11 +554,11 @@ SUPPORTED_CSS_PROPERTIES:
- z-index

# we allow through any string that is just a-z plus dashes (no spaces), so the
# only keywords you need to specify would be ones that have something else or
# only keywords you need to specify would be ones that have something else or
# url, if you want to allow url values
SUPPORTED_CSS_KEYWORDS: ["!important", "url"]

# if you include "url" in the SUPPORTED_CSS_KEYWORDS, only urls pointing to this
# if you include "url" in the SUPPORTED_CSS_KEYWORDS, only urls pointing to this
# kind of resource will be allowed
SUPPORTED_EXTERNAL_URLS: ["jpg", "jpeg", "png", "gif"]

Expand Down
14 changes: 14 additions & 0 deletions lib/zoho_resource_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ def create_ticket(ticket_attributes:)
).parsed_response
end

def create_ticket_attachment(ticket_id:, attachment_attributes:)
HTTParty.post(
ticket_attachment_create_endpoint(ticket_id),
headers: headers,
body: attachment_attributes.to_json
).parsed_response
end

def find_contact
response = HTTParty.get(
CONTACT_SEARCH_ENDPOINT,
Expand Down Expand Up @@ -76,4 +84,10 @@ def contact_body
"email" => @email
}
end

private

def ticket_attachment_create_endpoint(ticket_id)
"#{ArchiveConfig.ZOHO_URL}/api/v1/tickets/#{ticket_id}/attachments"
end
end
24 changes: 24 additions & 0 deletions spec/lib/zoho_resource_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,28 @@
)
end
end

describe "#create_ticket_attachment" do
let(:attachment_attributes) do
{ file: "the_file" }
end

before do
WebMock.stub_request(:post, /zoho/)
.to_return(headers: { content_type: "application/json" }, body: '{"id":"31"}')
end

it "submits a post request to the correct endpoint with the expected arguments" do
expect(subject.create_ticket_attachment(
ticket_id: 3,
attachment_attributes: attachment_attributes
).fetch("id")).to eq("31")

expect(WebMock).to have_requested(:post, "https://desk.zoho.com/api/v1/tickets/3/attachments")
.with(
headers: expected_request_headers,
body: attachment_attributes.to_json
)
end
end
end
23 changes: 22 additions & 1 deletion spec/models/abuse_report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
let(:work) { create(:work) }
let(:chapter) { work.chapters.first }
let(:missing_work_id) { build(:abuse_report, url: "http://archiveofourown.org/chapters/#{chapter.id}/") }

it "saves and adds the correct work id to the URL" do
expect(missing_work_id.save).to be_truthy
expect(missing_work_id.url).to eq("http://archiveofourown.org/works/#{work.id}/chapters/#{chapter.id}/")
Expand Down Expand Up @@ -345,4 +345,25 @@
expect(safe_report.save).to be_truthy
end
end

describe "#attach_work_download" do
include ActiveJob::TestHelper

let(:ticket_id) { "123" }
let(:work) { create(:work) }

it "does not attach a download for non-work URLs asynchronously" do
allow(subject).to receive(:url).and_return("http://archiveofourown.org/users/someone/")

expect { subject.attach_work_download(ticket_id) }
.not_to have_enqueued_job
end

it "attaches a download for work URLs asynchronously" do
allow(subject).to receive(:url).and_return("http://archiveofourown.org/works/#{work.id}/")

expect { subject.attach_work_download(ticket_id) }
.to have_enqueued_job
end
end
end
21 changes: 21 additions & 0 deletions spec/models/feedback_reporters/feedback_reporter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,27 @@
end
end

describe "#send_attachment!" do
let(:work_id) { "123" }
let(:download) { "the_file" }

let(:expected_attachment_attributes) do
{ file: download }
end

it "calls the Zoho ticket attachment creator with the expected arguments" do
expect(ZohoResourceClient).to receive_message_chain(
:new,
:create_ticket_attachment
).with(
ticket_id: work_id,
attachment_attributes: expected_attachment_attributes
)

subject.send_attachment!(work_id, download)
end
end

describe "#report_attributes" do
it "returns the expected attributes" do
expect(subject.report_attributes).to eq(expected_ticket_attributes)
Expand Down

0 comments on commit d53e9da

Please sign in to comment.