add documententities from @mentions in documents

This commit is contained in:
Andrew Brown 2019-08-02 16:55:06 -05:00
parent 41a69df3fd
commit da9973664a
4 changed files with 51 additions and 3 deletions

View File

@ -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

View File

@ -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.

View File

@ -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

View File

@ -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