Fix author handling and story refresh
This commit is contained in:
parent
0cf85cb6dc
commit
af78545d43
1 changed files with 24 additions and 32 deletions
|
|
@ -6,8 +6,8 @@ module FicTracker::Models
|
||||||
class Story < Sequel::Model
|
class Story < Sequel::Model
|
||||||
# 1/week
|
# 1/week
|
||||||
METADATA_REFRESH_INTERVAL = 7 * 24 * 60 * 60
|
METADATA_REFRESH_INTERVAL = 7 * 24 * 60 * 60
|
||||||
# 3/day
|
# 6/day
|
||||||
CONTENT_REFRESH_INTERVAL = 12 * 60 * 60
|
CONTENT_REFRESH_INTERVAL = 4 * 60 * 60
|
||||||
# 2 months
|
# 2 months
|
||||||
STORY_EXPIRY = 2 * 30 * 24 * 60 * 60
|
STORY_EXPIRY = 2 * 30 * 24 * 60 * 60
|
||||||
|
|
||||||
|
|
@ -19,16 +19,8 @@ module FicTracker::Models
|
||||||
plugin :serialization, :json, :data
|
plugin :serialization, :json, :data
|
||||||
|
|
||||||
# Defer creation of authors/chapters until the story requiring them is to be saved
|
# 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
|
def after_create
|
||||||
return if [@authors, @chapters].all?(&:nil?)
|
@authors&.each { |author| add_author author }
|
||||||
|
|
||||||
self.authors = @authors if @authors
|
|
||||||
@authors = nil
|
@authors = nil
|
||||||
|
|
||||||
if @chapters
|
if @chapters
|
||||||
|
|
@ -73,22 +65,22 @@ module FicTracker::Models
|
||||||
to_remove = self.authors.map(&:id)
|
to_remove = self.authors.map(&:id)
|
||||||
|
|
||||||
authors.each do |entry|
|
authors.each do |entry|
|
||||||
author = entry if entry.is_a?(FicTracker::Models::Author)
|
aut = entry if entry.is_a?(FicTracker::Models::Author)
|
||||||
|
|
||||||
if author
|
if aut
|
||||||
to_add << author
|
to_add << aut
|
||||||
else
|
else
|
||||||
author = self.authors.find { |c| c.slug == entry[:slug] }
|
aut = self.authors.find { |c| c.slug == entry[:slug] }
|
||||||
|
|
||||||
if author
|
if aut
|
||||||
author.set(**entry)
|
aut.set(**entry)
|
||||||
else
|
else
|
||||||
entry[:backend_name] = backend.name
|
entry[:backend_name] = backend.name
|
||||||
author = FicTracker::Models::Author.new(**entry)
|
aut = FicTracker::Models::Author.new(**entry)
|
||||||
to_add << author
|
to_add << aut
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
to_remove.delete author.id if author.id
|
to_remove.delete aut.id
|
||||||
end
|
end
|
||||||
|
|
||||||
if id
|
if id
|
||||||
|
|
@ -102,7 +94,7 @@ module FicTracker::Models
|
||||||
author_dataset.where(id: to_remove).destroy
|
author_dataset.where(id: to_remove).destroy
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
@authors = to_add
|
@authors = (@authors || []) + to_add - to_remove
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -138,7 +130,7 @@ module FicTracker::Models
|
||||||
end
|
end
|
||||||
if to_remove.any?
|
if to_remove.any?
|
||||||
logger.debug "Removing chapter(s) #{to_remove.inspect} from story #{self}"
|
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
|
end
|
||||||
|
|
||||||
update(updated_at: latest_chapter_at) if latest_chapter_at > Time.at(0) && (updated_at.nil? || latest_chapter_at >= updated_at)
|
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
|
end
|
||||||
|
|
||||||
def ensure_fully_loaded
|
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
|
# 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? }
|
backend.load_full_story(self) unless chapters && chapters.any? && chapters.all? { |c| c.content? && c.content_type? }
|
||||||
refresh_content
|
|
||||||
refresh_metadata
|
|
||||||
else
|
|
||||||
backend.load_full_story(self)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def ensure_chapters
|
def ensure_chapters
|
||||||
|
|
@ -193,16 +183,18 @@ module FicTracker::Models
|
||||||
end
|
end
|
||||||
|
|
||||||
def refresh_content
|
def refresh_content
|
||||||
backend.find_chapters(self) if backend && needs_content_refresh?
|
refresh_content! if backend && needs_content_refresh?
|
||||||
chapters.each(&:refresh_content)
|
# chapters.each(&:refresh_content)
|
||||||
end
|
end
|
||||||
|
|
||||||
def refresh_content!
|
def refresh_content!
|
||||||
backend.find_chapters(self)
|
backend.find_chapters(self)
|
||||||
chapters.each(&:refresh_content!)
|
# chapters.each(&:refresh_content!)
|
||||||
end
|
end
|
||||||
|
|
||||||
def needs_metadata_refresh?
|
def needs_metadata_refresh?
|
||||||
|
return true if id && authors.empty?
|
||||||
|
|
||||||
Time.now - (last_metadata_refresh || Time.at(0)) >= METADATA_REFRESH_INTERVAL
|
Time.now - (last_metadata_refresh || Time.at(0)) >= METADATA_REFRESH_INTERVAL
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -219,7 +211,7 @@ module FicTracker::Models
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
author_names = authors.map(&:to_s)
|
author_names = self.authors.map(&:to_s)
|
||||||
if author_names.empty?
|
if author_names.empty?
|
||||||
author_names = '<Unknown>'
|
author_names = '<Unknown>'
|
||||||
elsif author_names.size == 1
|
elsif author_names.size == 1
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue