eBooru/app/models/artist_url.rb

164 lines
3.8 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class ArtistUrl < ApplicationRecord
2018-05-26 15:58:19 -04:00
before_validation :initialize_normalized_url, on: :create
before_validation :normalize
validates :url, presence: true, uniqueness: { scope: :artist_id }
2019-09-05 08:59:51 -04:00
validates :url, length: { in: 1..4096 }
validate :validate_url_format
2015-01-19 14:40:33 -05:00
belongs_to :artist, :touch => true
2013-03-19 08:10:10 -04:00
scope :url_matches, ->(url) { url_attribute_matches(:url, url) }
scope :normalized_url_matches, ->(url) { url_attribute_matches(:normalized_url, url) }
def self.parse_prefix(url)
prefix, url = url.match(/\A(-)?(.*)/)[1, 2]
is_active = prefix.nil?
2018-05-26 15:58:19 -04:00
[is_active, url]
2018-05-26 15:58:19 -04:00
end
2010-02-15 13:59:58 -05:00
def self.normalize(url)
if url.nil?
nil
else
url = url.sub(%r!^https://!, "http://")
url = url.sub(%r!^http://([^/]+)!i) { |domain| domain.downcase }
2010-02-15 14:17:08 -05:00
url = url.gsub(/\/+\Z/, "")
url = url.gsub(%r!^https://!, "http://")
2010-02-15 13:59:58 -05:00
url + "/"
end
end
2013-03-19 08:10:10 -04:00
def self.search(params)
q = super
q = q.attribute_matches(:artist_id, params[:artist_id])
q = q.attribute_matches(:is_active, params[:is_active])
q = q.attribute_matches(:url, params[:url])
q = q.attribute_matches(:normalized_url, params[:normalized_url])
if params[:artist_name].present?
q = q.joins(:artist).where("artists.name = ?", params[:artist_name])
end
# Legacy param?
q = q.artist_matches(params[:artist])
q = q.url_matches(params[:url_matches])
q = q.normalized_url_matches(params[:normalized_url_matches])
case params[:order]
when /\A(id|artist_id|url|normalized_url|is_active|created_at|updated_at)(?:_(asc|desc))?\z/i
dir = $2 || :desc
q = q.order($1 => dir).order(id: :desc)
else
2023-07-07 08:32:57 -04:00
q = q.apply_basic_order(params)
end
q
end
def self.artist_matches(params = {})
return all if params.blank?
where(artist_id: Artist.search(params).reorder(nil))
end
def self.url_attribute_matches(attr, url)
if url.blank?
all
else
url = "*#{url}*" if url.exclude?("*")
where_ilike(attr, url)
end
end
2021-06-15 09:12:44 -04:00
# sites apearing at the start have higher priority than those below
SITES_PRIORITY_ORDER = [
"furaffinity.net",
"deviantart.com",
"twitter.com",
"pixiv.net",
"pixiv.me",
2021-06-15 09:12:44 -04:00
"inkbunny.net",
"sofurry.com",
"weasyl.com",
"furrynetwork.com",
"itaku.ee",
2021-06-15 09:12:44 -04:00
"tumblr.com",
"newgrounds.com",
"derpibooru.org",
"cohost.org",
2021-06-15 09:12:44 -04:00
"hentai-foundry.com",
"artstation.com",
"baraag.net",
"pawoo.net",
"skeb.jp",
2021-06-15 09:12:44 -04:00
"pillowfort.social",
"reddit.com",
"bsky.app",
2021-06-15 09:12:44 -04:00
"youtube.com",
"t.me",
2021-06-15 09:12:44 -04:00
"instagram.com",
"vk.com",
"facebook.com",
"tiktok.com",
2021-06-15 09:12:44 -04:00
# livestreams
"picarto.tv",
"piczel.tv",
"twitch.tv",
# support the artist
"patreon.com",
"subscribestar.adult",
"ko-fi.com",
"commishes.com",
"boosty.to",
2021-06-15 09:12:44 -04:00
"fanbox.cc",
"itch.io",
2021-06-15 09:12:44 -04:00
"gumroad.com",
"redbubble.com",
"etsy.com",
2021-06-15 09:12:44 -04:00
# misc
"discord.gg",
"discord.com",
2021-06-15 09:12:44 -04:00
"trello.com",
"curiouscat.me",
"toyhou.se",
"linktr.ee",
"carrd.co",
].reverse!
# higher priority will apear higher in the artist url list
# inactive urls will be pushed to the bottom
2017-09-14 14:37:42 -04:00
def priority
2021-06-15 09:12:44 -04:00
prio = 0
prio -= 1000 unless is_active
host = Addressable::URI.parse(url).domain
prio += SITES_PRIORITY_ORDER.index(host).to_i
prio
2017-09-14 14:37:42 -04:00
end
2010-02-15 13:59:58 -05:00
def normalize
self.normalized_url = self.class.normalize(url)
end
def initialize_normalized_url
self.normalized_url = url
2010-02-15 13:59:58 -05:00
end
2013-03-19 08:10:10 -04:00
2010-02-15 13:59:58 -05:00
def to_s
if is_active?
url
else
"-#{url}"
end
2010-02-15 13:59:58 -05:00
end
def validate_url_format
uri = Addressable::URI.parse(url)
2021-02-07 20:16:52 -05:00
errors.add(:url, "'#{uri}' must begin with http:// or https:// ") if !uri.scheme.in?(%w[http https])
rescue Addressable::URI::InvalidURIError => error
2021-02-07 20:16:52 -05:00
errors.add(:url, "'#{uri}' is malformed: #{error}")
end
2010-02-15 13:59:58 -05:00
end