From fb3950a5584a253e00bf306873013b4b11003bd8 Mon Sep 17 00:00:00 2001 From: Owen Hu Date: Sun, 21 Apr 2024 20:41:20 -0700 Subject: [PATCH 1/3] parse upload url to check if it is a embeddable snap project --- .../admin/commercials_controller.rb | 4 ++- app/models/commercial.rb | 27 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/app/controllers/admin/commercials_controller.rb b/app/controllers/admin/commercials_controller.rb index 0b7c8af3f..093ea3914 100644 --- a/app/controllers/admin/commercials_controller.rb +++ b/app/controllers/admin/commercials_controller.rb @@ -90,7 +90,9 @@ def aggregate_errors(errors) end def commercial_params - params.require(:commercial).permit(:title, :url) + params.require(:commercial).permit(:title, :url).tap do |params| + params[:url] = Commercial.generate_snap_embed(params[:url]) if params[:url] + end end end end diff --git a/app/models/commercial.rb b/app/models/commercial.rb index 1fcc0a45a..606c8e01a 100644 --- a/app/models/commercial.rb +++ b/app/models/commercial.rb @@ -30,6 +30,7 @@ class Commercial < ApplicationRecord def self.render_from_url(url) register_provider + url = generate_snap_embed(url) begin resource = OEmbed::Providers.get(url, maxwidth: 560, maxheight: 315) { html: resource.html.html_safe } @@ -39,6 +40,32 @@ def self.render_from_url(url) end end + def self.generate_snap_embed(url) + uri = URI.parse(url) + if uri.host == 'snap.berkeley.edu' && uri.path == '/project' + args = URI.decode_www_form(uri.query).to_h + else + return url + end + if args.key?('username') && args.key?('projectname') + new_query = URI.encode_www_form({ + 'projectname' => args['projectname'], + 'username' => args['username'], + 'showTitle' => 'true', + 'showAuthor' => 'true', + 'editButton' => 'true', + 'pauseButton' => 'true' + }) + new_uri = URI::HTTPS.build( + host: uri.host, + path: '/embed', + query: new_query + ) + return new_uri.to_s + end + url + end + def self.iframe_fallback(url) "".html_safe end From 2591edbca6906fff3611e29e7769e00d1a5f52f0 Mon Sep 17 00:00:00 2001 From: Owen Hu Date: Mon, 22 Apr 2024 14:25:25 -0700 Subject: [PATCH 2/3] add test coverage for snap embed message. Fix cases where url is nil --- app/models/commercial.rb | 2 ++ spec/models/commercial_spec.rb | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/app/models/commercial.rb b/app/models/commercial.rb index 606c8e01a..42d4ccadf 100644 --- a/app/models/commercial.rb +++ b/app/models/commercial.rb @@ -41,6 +41,8 @@ def self.render_from_url(url) end def self.generate_snap_embed(url) + return url unless url + uri = URI.parse(url) if uri.host == 'snap.berkeley.edu' && uri.path == '/project' args = URI.decode_www_form(uri.query).to_h diff --git a/spec/models/commercial_spec.rb b/spec/models/commercial_spec.rb index a09077f8e..f2d0a749e 100644 --- a/spec/models/commercial_spec.rb +++ b/spec/models/commercial_spec.rb @@ -15,6 +15,7 @@ # commercialable_id :integer # require 'spec_helper' +require 'uri' describe Commercial do it { is_expected.to validate_presence_of(:url) } @@ -29,4 +30,11 @@ commercial = build(:conference_commercial) expect(commercial.valid?).to be true end + + it 'parses snap url' do + url = 'https://snap.berkeley.edu/project?username=avi_shor&projectname=stamps' + transformed_url = Commercial.generate_snap_embed(url) + expected_url = 'https://snap.berkeley.edu/embed?projectname=stamps&username=avi_shor&showTitle=true&showAuthor=true&editButton=true&pauseButton=true' + expect(transformed_url).to eq expected_url + end end From 3c73c9528b708c7b4ba68fb68195ad61c9f65045 Mon Sep 17 00:00:00 2001 From: Owen Hu Date: Mon, 22 Apr 2024 16:35:36 -0700 Subject: [PATCH 3/3] remove unused import --- spec/models/commercial_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/models/commercial_spec.rb b/spec/models/commercial_spec.rb index f2d0a749e..2adc17c97 100644 --- a/spec/models/commercial_spec.rb +++ b/spec/models/commercial_spec.rb @@ -15,7 +15,6 @@ # commercialable_id :integer # require 'spec_helper' -require 'uri' describe Commercial do it { is_expected.to validate_presence_of(:url) }