Fix author handling and story refresh

This commit is contained in:
Alexander Olofsson 2024-06-21 14:28:33 +02:00
parent 0cf85cb6dc
commit af78545d43
Signed by: ace
GPG key ID: D439C9470CB04C73

View file

@ -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 = '<Unknown>'
elsif author_names.size == 1