Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intercom notes #4034

Draft
wants to merge 5 commits into
base: develop-old
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/models/concerns/media_object_intercom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ def to_ingest_api_hash(include_structure = true, remove_identifiers: false)
comment: comment.to_a,
bibliographic_id: (bibliographic_id.present? ? bibliographic_id[:id] : nil),
bibliographic_id_label: (bibliographic_id.present? ? bibliographic_id[:source] : nil),
note: (note.present? ? note.collect { |n| n[:note] } : nil),
note_type: (note.present? ? note.collect { |n| n[:type] } : nil),
note: (note.present? ? note.select { |n| n[:type] != 'intercom' }.collect { |n| n[:note] } : nil),
note_type: (note.present? ? note.select { |n| n[:type] != 'intercom' }.collect { |n| n[:type] } : nil),
language: (language.present? ? language.collect { |n| n[:code] } : nil),
related_item_url: (related_item_url.present? ? related_item_url.collect { |n| n[:url] } : nil),
related_item_label: (related_item_url.present? ? related_item_url.collect { |n| n[:label] } : nil),
Expand Down
6 changes: 6 additions & 0 deletions app/models/media_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ def section_physical_descriptions
all_pds.uniq
end

# Returns true if the media object has been pushed from one avalon to another, i.e. it has some notes with type "intercom"
def intercom_pushed?
note.present? && note.any? { |n| n[:type] == 'intercom' }
end

def to_solr
super.tap do |solr_doc|
solr_doc[ActiveFedora.index_field_mapper.solr_name("workflow_published", :facetable, type: :string)] = published? ? 'Published' : 'Unpublished'
Expand All @@ -242,6 +247,7 @@ def to_solr
solr_doc['avalon_resource_type_ssim'] = self.avalon_resource_type.map(&:titleize)
solr_doc['identifier_ssim'] = self.identifier.map(&:downcase)
solr_doc['all_comments_sim'] = all_comments
solr_doc['intercom_pushed_bsi'] = intercom_pushed?
#Add all searchable fields to the all_text_timv field
all_text_values = []
all_text_values << solr_doc["title_tesi"]
Expand Down
7 changes: 6 additions & 1 deletion lib/avalon/intercom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ def push_media_object(media_object, collection_id, include_structure = true)
verify_ssl: false,
timeout: 3600
)
{ link: URI.join(@avalon['url'], 'media_objects/', JSON.parse(resp.body)['id']).to_s }
result = { link: URI.join(@avalon['url'], 'media_objects/', JSON.parse(resp.body)['id']).to_s }
# add a note to indicate the item has been successfully pushed
pushnote = [{ note: "Pushed by #{@user} at #{Time.now.utc.strftime('%m/%d/%Y %H:%M%p %Z')}", type: 'intercom' }]
media_object.note = media_object.note.present? ? media_object.note + pushnote : pushnote
result[:message] = "The media object has been pushed to #{avalon}, but the note for this push failed to be saved." unless media_object.save
return result
rescue StandardError => e
{ message: e.message, status: e.respond_to?(:response) && e.response.present? ? e.response.code : 500 }
end
Expand Down
16 changes: 16 additions & 0 deletions spec/controllers/catalog_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,22 @@
expect(assigns(:document_list).count).to eq 1
expect(assigns(:document_list).map(&:id)).to eq([mo.id])
end
it "should show results for all pushed items" do
mo = FactoryBot.create(:fully_searchable_media_object, note: [{note: 'Pushed by user at time', type: 'intercom'}])
get 'index', :q => "intercom_pushed_bsi:true"
expect(response).to be_success
expect(response).to render_template('catalog/index')
expect(assigns(:document_list).count).to eq 1
expect(assigns(:document_list).map(&:id)).to eq([mo.id])
end
it "should show results for all non-pushed items" do
mo = FactoryBot.create(:fully_searchable_media_object)
get 'index', :q => "intercom_pushed_bsi:false"
expect(response).to be_success
expect(response).to render_template('catalog/index')
expect(assigns(:document_list).count).to eq 1
expect(assigns(:document_list).map(&:id)).to eq([mo.id])
end
end
describe "as an lti user" do
let!(:user) { login_lti 'student' }
Expand Down
8 changes: 6 additions & 2 deletions spec/lib/avalon/intercom_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,18 @@
response = Avalon::Intercom.new(username).push_media_object(media_object, 'cupcake_collection', false)
expect(response[:message]).to eq('Avalon intercom target is not configured.')
end
it "should respond to a failed api call with error" do
it "should respond to a failed api call with error, while the media object push status shall be false" do
allow(RestClient::Request).to receive(:execute).and_call_original
allow(RestClient::Request).to receive(:execute).with(hash_including(method: :post)).and_raise(StandardError)
response = intercom.push_media_object(media_object, 'cupcake_collection', false)
expect(response).to eq({ message: 'StandardError', status: 500 })
expect(media_object.intercom_pushed?).eql?(false)
end
it "should respond with a link to the pushed object on target" do
it "should respond with a link to the pushed object on target, while the pushed content shall not include any intercom notes, and the media object push status shall be true" do
media_object.ordered_master_files=[master_file_with_structure]
media_object_hash = media_object.to_ingest_api_hash(false)
expect(media_object_hash[:fields][:note]).eql?(nil)
expect(media_object_hash[:fields][:notetype]).eql?(nil)
media_object_hash.merge!(
{ 'collection_id' => 'cupcake_collection', 'import_bib_record' => true, 'publish' => false }
)
Expand All @@ -96,6 +99,7 @@
with(body: media_object_json).to_return(status: 200, body: { 'id' => 'def456' }.to_json, headers: {})
response = intercom.push_media_object(media_object, 'cupcake_collection', false)
expect(response).to eq({ link: 'https://target.avalon.com/media_objects/def456'})
expect(media_object.intercom_pushed?).eql?(true)
end
end
end