Skip to content

Commit

Permalink
AO3-6563 Fix Zoho attachment calls to force multipart upload
Browse files Browse the repository at this point in the history
  • Loading branch information
brianjaustin committed Aug 21, 2024
1 parent 72f2932 commit a194cec
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/jobs/report_attachment_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ class ReportAttachmentJob < ApplicationJob
def perform(ticket_id, work)
download = Download.new(work, mime_type: "text/html")
html = DownloadWriter.new(download).generate_html
FeedbackReporter.new.send_attachment!(ticket_id, html)
FeedbackReporter.new.send_attachment!(ticket_id, "#{download.file_name}.html", html)
end
end
2 changes: 1 addition & 1 deletion app/models/abuse_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def send_report
url: url
)
response = reporter.send_report!
ticket_id = response.fetch("ticketNumber")
ticket_id = response["id"]
return if ticket_id.blank?

attach_work_download(ticket_id)
Expand Down
15 changes: 11 additions & 4 deletions app/models/feedback_reporters/feedback_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ def send_report!
zoho_resource_client.create_ticket(ticket_attributes: report_attributes)
end

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

Expand All @@ -51,8 +51,15 @@ def report_attributes
}
end

def attachment_attributes(download)
{ file: download }
def attachment_attributes(filename, download)
attachment = StringIO.new(download)
# Workaround for HTTParty not recognizing StringIO as a file-like object:
# https://github.com/jnunemaker/httparty/issues/675#issuecomment-590757288
def attachment.path
filename
end

{ file: ZohoStringAttachment.new(filename, download) }
end

private
Expand Down
5 changes: 3 additions & 2 deletions lib/zoho_auth_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class ZohoAuthClient
ACCESS_TOKEN_REQUEST_ENDPOINT = "https://accounts.zoho.com/oauth/v2/token"
ACCESS_TOKEN_CACHE_KEY = "/v2/zoho_access_token"
ACCESS_TOKEN_CACHE_KEY = "/v3/zoho_access_token"

SCOPE = [
# - Find and create contacts before submitting tickets
Expand All @@ -12,7 +12,8 @@ class ZohoAuthClient
"Desk.contacts.READ",
"Desk.search.READ",
"Desk.tickets.CREATE",
"Desk.tickets.READ"
"Desk.tickets.READ",
"Desk.tickets.UPDATE"
].join(",").freeze

def access_token
Expand Down
7 changes: 5 additions & 2 deletions lib/zoho_resource_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ def create_ticket(ticket_attributes:)
end

def create_ticket_attachment(ticket_id:, attachment_attributes:)
HTTParty.post(
response = HTTParty.post(
ticket_attachment_create_endpoint(ticket_id),
headers: headers,
body: attachment_attributes.to_json
body: attachment_attributes
).parsed_response
raise response["message"] if response["errorCode"]

response
end

def find_contact
Expand Down
1 change: 1 addition & 0 deletions spec/jobs/report_attachment_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
before do
download_mock = instance_double(Download)
allow(Download).to receive(:new).with(work, { mime_type: "text/html" }).and_return(download_mock)
allow(download_mock).to receive(:file_name).and_return("filename")
allow(DownloadWriter).to receive(:new).with(download_mock).and_return(writer_mock)
allow(writer_mock).to receive(:generate_html)
allow(FeedbackReporter).to receive(:new).and_return(reporter)
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/zoho_auth_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
client_id: "111",
client_secret: "a1b2c3",
redirect_uri: "https://archiveofourown.org/support",
scope: "Desk.contacts.CREATE,Desk.contacts.READ,Desk.search.READ,Desk.tickets.CREATE,Desk.tickets.READ",
scope: "Desk.contacts.CREATE,Desk.contacts.READ,Desk.search.READ,Desk.tickets.CREATE,Desk.tickets.READ,Desk.tickets.UPDATE",
grant_type: "refresh_token",
refresh_token: "x1y2z3"
}
Expand Down
7 changes: 6 additions & 1 deletion spec/lib/zoho_resource_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@

describe "#create_ticket_attachment" do
let(:attachment_attributes) do
attachment = StringIO.new("the_file")
def attachment.path
"the_file.html"
end

{ file: "the_file" }
end

Expand All @@ -162,7 +167,7 @@
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
body: "file=the_file"
)
end
end
Expand Down

0 comments on commit a194cec

Please sign in to comment.