Websites/app/logical/api_category.rb

67 lines
1.3 KiB
Ruby

# frozen_string_literal: true
class APICategory
attr_reader :name, :db, :sfw
SOURCES = [
%w[e621.net E621],
%w[furaffinity.net FurAffinity],
%w[inkbunny.net InkBunny],
%w[deviantart.com DeviantArt],
%w[twitter.com Twitter],
].freeze
def initialize(name, db, sfw)
@name = name
@db = db
@sfw = sfw
end
def count
APIImage.cached_count(db)
end
def real_count
APIImage.where(category: db).count
end
def unsourced_count
APIImage.where(category: db, sources: []).count
end
def sources
r = APIImage.joins("CROSS JOIN unnest(sources) AS source").where(category: db).distinct
other = r
list = []
APICategory::SOURCES.each do |value, name|
count = r.where("source ILIKE ?", "%#{value}%").count
list << { name: name, count: count }
other = other.where("source NOT ILIKE ?", "%#{value}%")
end
list << { name: "Unsourced", count: unsourced_count }
# noinspection RubyMismatchedArgumentType
list << { name: "Other", count: other.count }
list
end
def created_at
APIImage.where(category: db).minimum(:created_at)
end
def updated_at
APIImage.where(category: db).maximum(:updated_at)
end
def as_json(*)
{
name: name,
db: db,
sfw: sfw,
count: count,
}
end
alias to_h as_json
end