Skip to content

Commit

Permalink
Update so we only send the necessary data to media
Browse files Browse the repository at this point in the history
Depending on the type of Media we try to create, it needs different data
from the ProjectMedia object. Still, Media shouldn't need to know too much
about ProjectMedia.

So, we are deciding on what to send in ProjectMediaCreators, and only
sending what is necessary:
- I created a method that gets those media_arguments
- Moved that and set_media_type under protected
- Inside media it only gets that data and forwards it
     - There were a few changes to method's signatures (so tests were
     also updated)
     - Because there are a few differences between arguments needed, we
     are sending those as an additional_args hash
  • Loading branch information
vasconsaurus committed Feb 20, 2025
1 parent e4d1c9d commit 277347c
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 68 deletions.
81 changes: 55 additions & 26 deletions app/models/concerns/project_media_creators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,33 +54,9 @@ def create_annotation

def create_media!
self.set_media_type if self.set_original_claim || self.media_type.blank?
self.media = Media.find_or_create_media_associated_to(self)
end

def set_media_type
original_claim = self.set_original_claim&.strip
# byebug
if original_claim && original_claim.match?(/\A#{URI::DEFAULT_PARSER.make_regexp(['http', 'https'])}\z/)
uri = URI.parse(original_claim)
content_type = Net::HTTP.get_response(uri)['content-type']

case content_type
when /^image\//
self.media_type = 'UploadedImage'
when /^video\//
self.media_type = 'UploadedVideo'
when /^audio\//
self.media_type = 'UploadedAudio'
else
self.media_type = 'Link'
end
elsif original_claim
self.media_type = 'Claim'
elsif !self.url.blank?
self.media_type = 'Link'
elsif !self.quote.blank?
self.media_type = 'Claim'
end
media_type, media_content, additional_args = self.media_arguments
self.media = Media.find_or_create_media_from_content(media_type, media_content, additional_args)
end

def set_quote_metadata
Expand Down Expand Up @@ -140,6 +116,59 @@ def add_source_creation_log

protected

def set_media_type
original_claim = self.set_original_claim&.strip

if original_claim && original_claim.match?(/\A#{URI::DEFAULT_PARSER.make_regexp(['http', 'https'])}\z/)
uri = URI.parse(original_claim)
content_type = Net::HTTP.get_response(uri)['content-type']

case content_type
when /^image\//
self.media_type = 'UploadedImage'
when /^video\//
self.media_type = 'UploadedVideo'
when /^audio\//
self.media_type = 'UploadedAudio'
else
self.media_type = 'Link'
end
elsif original_claim
self.media_type = 'Claim'
elsif !self.url.blank?
self.media_type = 'Link'
elsif !self.quote.blank?
self.media_type = 'Claim'
end
end

def media_arguments
media_type = self.media_type
original_claim = self.set_original_claim&.strip

if original_claim
case media_type
when 'UploadedImage', 'UploadedVideo', 'UploadedAudio'
return media_type, original_claim, { has_original_claim: true }
when 'Claim'
return media_type, original_claim, { has_original_claim: true }
when 'Link'
return media_type, original_claim, { team: self.team, has_original_claim: true }
end
else
case media_type
when 'UploadedImage', 'UploadedVideo', 'UploadedAudio'
return media_type, self.file
when 'Claim'
return media_type, self.quote, { quote_attributions: self.quote_attributions }
when 'Link'
return media_type, self.url, { team: self.team }
when 'Blank'
return media_type
end
end
end

def set_jsonld_response(task)
jsonld = self.media.metadata['raw']['json+ld'] if self.media.metadata.has_key?('raw')
unless jsonld.nil?
Expand Down
60 changes: 24 additions & 36 deletions app/models/media.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,36 +79,17 @@ def domain
''
end

def self.find_or_create_media_associated_to(project_media)
m = nil
original_claim = project_media.set_original_claim&.strip
media_type = project_media.media_type
team = project_media.team

if original_claim
case media_type
when 'UploadedImage', 'UploadedVideo', 'UploadedAudio'
m = find_or_create_uploaded_file_media(original_claim, media_type, true)
when 'Claim'
m = find_or_create_claim_media(original_claim, nil, true)
when 'Link'
m = find_or_create_link_media(original_claim, team, true)
when 'Blank'
m = Blank.create!
end
else
case media_type
when 'UploadedImage', 'UploadedVideo', 'UploadedAudio'
m = find_or_create_uploaded_file_media(project_media.file, media_type)
when 'Claim'
m = find_or_create_claim_media(project_media.quote, project_media.quote_attributions)
when 'Link'
m = find_or_create_link_media(project_media.url, team)
when 'Blank'
m = Blank.create!
end
def self.find_or_create_media_from_content(media_type, media_content = nil, additional_args = {})
case media_type
when 'UploadedImage', 'UploadedVideo', 'UploadedAudio'
find_or_create_uploaded_file_media(media_content, media_type, additional_args)
when 'Claim'
find_or_create_claim_media(media_content, additional_args)
when 'Link'
find_or_create_link_media(media_content, additional_args)
when 'Blank'
Blank.create!
end
m
end

private
Expand Down Expand Up @@ -155,19 +136,20 @@ def self.download_file(url, ext)

# we set it to UploadedImage by default, should we?
# def self.create_with_file(project_media, media_type = 'UploadedImage')
def self.find_or_create_uploaded_file_media(file_media, media_type, has_original_claim = false)
def self.find_or_create_uploaded_file_media(file_media, media_type, additional_args = {})
has_original_claim = additional_args&.fetch(:has_original_claim, nil)
klass = media_type.constantize

if has_original_claim
uri = URI.parse(file_media)
ext = File.extname(uri.path)

existing_media = klass.find_by(original_claim_hash: Digest::MD5.hexdigest(file_media))

if existing_media
existing_media
else
uri = URI.parse(file_media)
ext = File.extname(uri.path)
file = download_file(file_media, ext)

klass.create!(file: file, original_claim: file_media)
end
else
Expand All @@ -177,16 +159,22 @@ def self.find_or_create_uploaded_file_media(file_media, media_type, has_original
end
end

def self.find_or_create_claim_media(claim_media, quote_attributions = nil, has_original_claim = false)
def self.find_or_create_claim_media(claim_media, additional_args = {})
has_original_claim = additional_args.fetch(:has_original_claim, nil)
quote_attributions = additional_args.fetch(:quote_attributions, nil)

if has_original_claim
Claim.find_by(original_claim_hash: Digest::MD5.hexdigest(claim_media)) || Claim.create!(quote: claim_media, original_claim: claim_media)
else
Claim.create!(quote: claim_media, quote_attributions: quote_attributions)
end
end

def self.find_or_create_link_media(link_media, project_media_team, has_original_claim = false)
pender_key = project_media_team.get_pender_key if project_media_team
def self.find_or_create_link_media(link_media, additional_args = {})
has_original_claim = additional_args.fetch(:has_original_claim, nil)
project_media_team = additional_args.fetch(:team, nil)

pender_key = project_media_team&.get_pender_key if project_media_team
url_from_pender = Link.normalized(link_media, pender_key)

if has_original_claim
Expand Down
12 changes: 6 additions & 6 deletions test/models/media_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ def setup

test "Claim Media: should save the original_claim and original_claim_hash when created from original claim" do
claim = 'This is a claim.'
claim_media = Media.find_or_create_claim_media(claim, nil, true)
claim_media = Media.find_or_create_claim_media(claim, { has_original_claim: true })

assert_not_nil claim_media.original_claim_hash
assert_not_nil claim_media.original_claim
Expand All @@ -645,7 +645,7 @@ def setup

test "Claim Media: should not create duplicate media if media with original_claim_hash exists" do
assert_difference 'Claim.count', 1 do
2.times { Media.find_or_create_claim_media('This is a claim.', nil, true) }
2.times { Media.find_or_create_claim_media('This is a claim.', { has_original_claim: true }) }
end
end

Expand All @@ -664,7 +664,7 @@ def setup
}.to_json
WebMock.stub_request(:get, pender_url).with(query: { url: link_url }).to_return(body: link_response)

link_media = Media.find_or_create_link_media(link_url, team, true)
link_media = Media.find_or_create_link_media(link_url, { team: team, has_original_claim: true })

assert_not_nil link_media.original_claim_hash
assert_not_nil link_media.original_claim
Expand Down Expand Up @@ -706,7 +706,7 @@ def setup
WebMock.stub_request(:get, pender_url).with(query: { url: link_url }).to_return(body: link_response)

assert_difference 'Link.count', 1 do
2.times { Media.find_or_create_link_media(link_url, team) }
2.times { Media.find_or_create_link_media(link_url, { team: team, has_original_claim: true }) }
end
end

Expand All @@ -717,7 +717,7 @@ def setup
audio_url = "http://example.com/#{file.path.split('/').last}"
WebMock.stub_request(:get, audio_url).to_return(body: file.read, headers: { 'Content-Type' => 'audio/mp3' })

uploaded_media = Media.find_or_create_uploaded_file_media(audio_url, 'UploadedAudio', true)
uploaded_media = Media.find_or_create_uploaded_file_media(audio_url, 'UploadedAudio', { has_original_claim: true })

assert_not_nil uploaded_media.original_claim_hash
assert_not_nil uploaded_media.original_claim
Expand All @@ -740,7 +740,7 @@ def setup
WebMock.stub_request(:get, audio_url).to_return(body: file.read, headers: { 'Content-Type' => 'audio/mp3' })

assert_difference 'UploadedAudio.count', 1 do
2.times { Media.find_or_create_uploaded_file_media(audio_url, 'UploadedAudio', true) }
2.times { Media.find_or_create_uploaded_file_media(audio_url, 'UploadedAudio', { has_original_claim: true }) }
end
end
end
Expand Down

0 comments on commit 277347c

Please sign in to comment.