From af78545d43e7559b3afd0709b1f46e852efa579b Mon Sep 17 00:00:00 2001 From: "Alexander \"Ace\" Olofsson" Date: Fri, 21 Jun 2024 14:28:33 +0200 Subject: [PATCH] Fix author handling and story refresh --- lib/fic_tracker/models/story.rb | 56 ++++++++++++++------------------- 1 file changed, 24 insertions(+), 32 deletions(-) diff --git a/lib/fic_tracker/models/story.rb b/lib/fic_tracker/models/story.rb index 95c10f0..fb5cd66 100644 --- a/lib/fic_tracker/models/story.rb +++ b/lib/fic_tracker/models/story.rb @@ -6,8 +6,8 @@ module FicTracker::Models class Story < Sequel::Model # 1/week METADATA_REFRESH_INTERVAL = 7 * 24 * 60 * 60 - # 3/day - CONTENT_REFRESH_INTERVAL = 12 * 60 * 60 + # 6/day + CONTENT_REFRESH_INTERVAL = 4 * 60 * 60 # 2 months STORY_EXPIRY = 2 * 30 * 24 * 60 * 60 @@ -19,16 +19,8 @@ module FicTracker::Models plugin :serialization, :json, :data # Defer creation of authors/chapters until the story requiring them is to be saved - def before_create - @authors.reject(&:id).each(&:save) if @authors - - super - end - def after_create - return if [@authors, @chapters].all?(&:nil?) - - self.authors = @authors if @authors + @authors&.each { |author| add_author author } @authors = nil if @chapters @@ -73,22 +65,22 @@ module FicTracker::Models to_remove = self.authors.map(&:id) authors.each do |entry| - author = entry if entry.is_a?(FicTracker::Models::Author) + aut = entry if entry.is_a?(FicTracker::Models::Author) - if author - to_add << author + if aut + to_add << aut else - author = self.authors.find { |c| c.slug == entry[:slug] } + aut = self.authors.find { |c| c.slug == entry[:slug] } - if author - author.set(**entry) + if aut + aut.set(**entry) else entry[:backend_name] = backend.name - author = FicTracker::Models::Author.new(**entry) - to_add << author + aut = FicTracker::Models::Author.new(**entry) + to_add << aut end end - to_remove.delete author.id if author.id + to_remove.delete aut.id end if id @@ -102,7 +94,7 @@ module FicTracker::Models author_dataset.where(id: to_remove).destroy end else - @authors = to_add + @authors = (@authors || []) + to_add - to_remove end end @@ -138,7 +130,7 @@ module FicTracker::Models end if to_remove.any? logger.debug "Removing chapter(s) #{to_remove.inspect} from story #{self}" - chapter_dataset.where(id: to_remove).delete + chapter_dataset.where(id: to_remove).destroy end update(updated_at: latest_chapter_at) if latest_chapter_at > Time.at(0) && (updated_at.nil? || latest_chapter_at >= updated_at) @@ -148,13 +140,11 @@ module FicTracker::Models end def ensure_fully_loaded + refresh_content + refresh_metadata + # FIXME: Should check for a reasonable set of parameters - full load unless XX% (75%?) of chapters have content - if chapters && chapters.any? && chapters.all? { |c| c.content? && c.content_type? } - refresh_content - refresh_metadata - else - backend.load_full_story(self) - end + backend.load_full_story(self) unless chapters && chapters.any? && chapters.all? { |c| c.content? && c.content_type? } end def ensure_chapters @@ -193,16 +183,18 @@ module FicTracker::Models end def refresh_content - backend.find_chapters(self) if backend && needs_content_refresh? - chapters.each(&:refresh_content) + refresh_content! if backend && needs_content_refresh? + # chapters.each(&:refresh_content) end def refresh_content! backend.find_chapters(self) - chapters.each(&:refresh_content!) + # chapters.each(&:refresh_content!) end def needs_metadata_refresh? + return true if id && authors.empty? + Time.now - (last_metadata_refresh || Time.at(0)) >= METADATA_REFRESH_INTERVAL end @@ -219,7 +211,7 @@ module FicTracker::Models end def to_s - author_names = authors.map(&:to_s) + author_names = self.authors.map(&:to_s) if author_names.empty? author_names = '' elsif author_names.size == 1