diff --git a/app/controllers/documents_controller.rb b/app/controllers/documents_controller.rb index bd37386b..6bbe1bda 100644 --- a/app/controllers/documents_controller.rb +++ b/app/controllers/documents_controller.rb @@ -81,7 +81,7 @@ class DocumentsController < ApplicationController # If there's no document analysis present, we're creating an entity without an associated analysis yet # So we just create a, uh, placeholder I guess document = Document.find_by(id: linked_entity_params[:document_id], user: current_user.id) - analysis = document.document_analysis.first_or_create + analysis = Documents::Analysis::DocumentAnalysisService.create_placeholder_analysis(document) document_analysis_id = analysis.id # todo document entities might make more sense to be tied to documents instead of analyses @@ -167,6 +167,8 @@ class DocumentsController < ApplicationController def update document = Document.with_deleted.find_or_initialize_by(id: params[:id], user: current_user) + DocumentMentionJob.perform_later(document.id) + unless document.user == current_user redirect_to(dashboard_path, notice: "You don't have permission to do that!") return diff --git a/app/jobs/document_mention_job.rb b/app/jobs/document_mention_job.rb new file mode 100644 index 00000000..3119b4d8 --- /dev/null +++ b/app/jobs/document_mention_job.rb @@ -0,0 +1,34 @@ +# This job is kicked off each time a document is edited. It looks at the document +# text for mentions (e.g. [[Character-123]] and adds that entity as a DocumentEntity). +class DocumentMentionJob < ApplicationJob + queue_as :analysis # todo do we need to make this lower priority? + + def perform(*args) + document_id = args.shift + + document = Document.find(document_id) + return unless document.present? + + analysis = Documents::Analysis::DocumentAnalysisService.create_placeholder_analysis(document) + + # Create document entities for @mentions + current_entities = document.document_entities.pluck(:entity_type, :entity_id) + document.body.scan(ContentFormatterService::TOKEN_REGEX).each do |entity_type, id| + unless current_entities.include? [entity_type, id] + analysis.document_entities.create( + entity_type: entity_type, + entity_id: id, + document_analysis_id: analysis.id + ) + + current_entities << [entity_type, id] + end + end + end +end + +# Release note (8/2/19): +# We could pretty easily kick off a DocumentMentionJob for every document in the system +# to start everyone off. We probably want to wait for a window after release to monitor +# page speeds once people start linking quick reference pages, but if all looks well lets +# do it. \ No newline at end of file diff --git a/app/services/documents/analysis/document_analysis_service.rb b/app/services/documents/analysis/document_analysis_service.rb new file mode 100644 index 00000000..3259eb3b --- /dev/null +++ b/app/services/documents/analysis/document_analysis_service.rb @@ -0,0 +1,11 @@ +module Documents + module Analysis + class DocumentAnalysisService < Service + def self.create_placeholder_analysis(document) + document.document_analysis.first_or_create + end + + + end + end +end diff --git a/tomorrow.txt b/tomorrow.txt index ef0ea0cd..6aaee6a8 100644 --- a/tomorrow.txt +++ b/tomorrow.txt @@ -1,6 +1,7 @@ Still to do: -- [ ] Auto-create document entities when @mentioning - - hmm, tie DE to doc instead of DA? +- [X] Auto-create document entities when @mentioning + - [S] hmm, tie DE to doc instead of DA? + - we can just use placeholder analysis objects - [ ] Optimize serializer wrt linked fields (!!!) - [ ] Optimize serializer wrt multiple pages (!!!) - [S] Gallery in quick reference