Improve handling of authors on stories

This commit is contained in:
Alexander Olofsson 2024-07-08 07:51:30 +02:00
parent 3a650b1cef
commit 742be4b6c4
Signed by: ace
GPG key ID: D439C9470CB04C73
2 changed files with 20 additions and 9 deletions

View file

@ -62,6 +62,7 @@ module FicTracker::Models
def authors=(authors) def authors=(authors)
to_add = [] to_add = []
to_keep = []
to_remove = self.authors.map(&:id) to_remove = self.authors.map(&:id)
authors.each do |entry| authors.each do |entry|
@ -70,17 +71,16 @@ module FicTracker::Models
if aut if aut
to_add << aut to_add << aut
else else
aut = self.authors.find { |c| c.slug == entry[:slug] } aut = FicTracker::Models::Author.find(backend_name: backend.name, slug: entry[:slug])
if aut unless aut
aut.set(**entry)
else
entry[:backend_name] = backend.name entry[:backend_name] = backend.name
aut = FicTracker::Models::Author.new(**entry) aut = FicTracker::Models::Author.new(**entry)
to_add << aut to_add << aut if id
end end
to_keep << aut unless id
end end
to_remove.delete aut.id to_remove.delete aut.id if id
end end
if id if id
@ -94,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 = (@authors || []) + to_add - to_remove @authors = to_keep
end end
end end

View file

@ -2,9 +2,14 @@
require 'sinatra/base' require 'sinatra/base'
require_relative 'server/helpers'
require_relative 'server/story'
module FicTracker module FicTracker
# Web server for providing your fic tracking needs # Web server for providing your fic tracking needs
class Server < Sinatra::Base class Server < Sinatra::Base
include Helpers
def initialize(*) def initialize(*)
@task_runner = Thread.new { background_tasks } @task_runner = Thread.new { background_tasks }
@ -17,6 +22,9 @@ module FicTracker
end end
configure do configure do
require 'sinatra/namespace'
register Sinatra::Namespace
root = File.join(__dir__, '../..') root = File.join(__dir__, '../..')
set :views, File.join(root, 'views') set :views, File.join(root, 'views')
@ -98,6 +106,10 @@ module FicTracker
collection&.save_changes collection&.save_changes
end end
map '/story' do
run Story.new
end
head '/story/:backend/*.*' do |_backend_name, slug, format| head '/story/:backend/*.*' do |_backend_name, slug, format|
mime = nil mime = nil
case format case format
@ -210,9 +222,8 @@ module FicTracker
FicTracker.cache.expire FicTracker.cache.expire
rescue StandardError => e rescue StandardError => e
FicTracker.logger.error "Failed when running background tasks, #{e.class}: #{e}\n#{e.backtrace[-5,5].join("\n ")}" FicTracker.logger.error "Error in background tasks, #{e.class}: #{e}\n#{e.backtrace[-5,5].join("\n ")}"
ensure ensure
iter += 1
sleep 30 * 60 sleep 30 * 60
end end
rescue StandardError => e rescue StandardError => e