2024-02-25 12:15:55 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-14 22:27:53 -04:00
|
|
|
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 }
|
2017-07-29 02:37:30 -04:00
|
|
|
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
|
|
|
|
2018-09-15 12:44:05 -04:00
|
|
|
scope :url_matches, ->(url) { url_attribute_matches(:url, url) }
|
|
|
|
scope :normalized_url_matches, ->(url) { url_attribute_matches(:normalized_url, url) }
|
|
|
|
|
2018-10-04 18:38:44 -04:00
|
|
|
def self.parse_prefix(url)
|
|
|
|
prefix, url = url.match(/\A(-)?(.*)/)[1, 2]
|
|
|
|
is_active = prefix.nil?
|
2018-05-26 15:58:19 -04:00
|
|
|
|
2018-10-04 18:38:44 -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
|
2018-07-27 15:26:39 -04:00
|
|
|
url = url.sub(%r!^https://!, "http://")
|
2018-09-05 20:14:24 -04:00
|
|
|
url = url.sub(%r!^http://([^/]+)!i) { |domain| domain.downcase }
|
2019-09-04 08:30:37 -04:00
|
|
|
|
2010-02-15 14:17:08 -05:00
|
|
|
url = url.gsub(/\/+\Z/, "")
|
2018-07-27 17:25:47 -04:00
|
|
|
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
|
|
|
|
2023-06-02 10:15:17 -04:00
|
|
|
def self.search(params)
|
2018-09-15 12:40:27 -04:00
|
|
|
q = super
|
|
|
|
|
|
|
|
q = q.attribute_matches(:artist_id, params[:artist_id])
|
|
|
|
q = q.attribute_matches(:is_active, params[:is_active])
|
2023-06-01 15:45:51 -04:00
|
|
|
q = q.attribute_matches(:url, params[:url])
|
|
|
|
q = q.attribute_matches(:normalized_url, params[:normalized_url])
|
2018-09-15 12:40:27 -04:00
|
|
|
|
2022-03-31 15:43:38 -04:00
|
|
|
if params[:artist_name].present?
|
|
|
|
q = q.joins(:artist).where("artists.name = ?", params[:artist_name])
|
|
|
|
end
|
|
|
|
|
|
|
|
# Legacy param?
|
2018-09-15 12:44:05 -04:00
|
|
|
q = q.artist_matches(params[:artist])
|
|
|
|
q = q.url_matches(params[:url_matches])
|
|
|
|
q = q.normalized_url_matches(params[:normalized_url_matches])
|
|
|
|
|
2018-09-15 12:40:27 -04:00
|
|
|
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)
|
2018-09-15 12:40:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
q
|
|
|
|
end
|
|
|
|
|
2018-09-15 12:44:05 -04:00
|
|
|
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
|
2022-10-13 12:22:07 -04:00
|
|
|
url = "*#{url}*" if url.exclude?("*")
|
|
|
|
where_ilike(attr, url)
|
2018-09-15 12:44:05 -04:00
|
|
|
end
|
|
|
|
end
|
2018-09-15 12:40:27 -04:00
|
|
|
|
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",
|
2023-10-15 11:11:43 -04:00
|
|
|
"pixiv.me",
|
2021-06-15 09:12:44 -04:00
|
|
|
"inkbunny.net",
|
|
|
|
"sofurry.com",
|
|
|
|
"weasyl.com",
|
|
|
|
"furrynetwork.com",
|
2023-10-15 11:11:43 -04:00
|
|
|
"itaku.ee",
|
2021-06-15 09:12:44 -04:00
|
|
|
"tumblr.com",
|
|
|
|
"newgrounds.com",
|
2023-10-15 11:11:43 -04:00
|
|
|
"derpibooru.org",
|
|
|
|
"cohost.org",
|
2021-06-15 09:12:44 -04:00
|
|
|
"hentai-foundry.com",
|
|
|
|
"artstation.com",
|
|
|
|
"baraag.net",
|
|
|
|
"pawoo.net",
|
2023-10-15 11:11:43 -04:00
|
|
|
"skeb.jp",
|
2021-06-15 09:12:44 -04:00
|
|
|
"pillowfort.social",
|
|
|
|
"reddit.com",
|
2023-11-01 16:19:24 -04:00
|
|
|
"bsky.app",
|
2021-06-15 09:12:44 -04:00
|
|
|
"youtube.com",
|
2023-10-15 11:11:43 -04:00
|
|
|
"t.me",
|
2021-06-15 09:12:44 -04:00
|
|
|
"instagram.com",
|
|
|
|
"vk.com",
|
|
|
|
"facebook.com",
|
2023-10-15 11:11:43 -04:00
|
|
|
"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",
|
2023-10-15 11:11:43 -04:00
|
|
|
"boosty.to",
|
2021-06-15 09:12:44 -04:00
|
|
|
"fanbox.cc",
|
2023-10-15 11:11:43 -04:00
|
|
|
"itch.io",
|
2021-06-15 09:12:44 -04:00
|
|
|
"gumroad.com",
|
|
|
|
"redbubble.com",
|
2023-10-15 11:11:43 -04:00
|
|
|
"etsy.com",
|
2021-06-15 09:12:44 -04:00
|
|
|
# misc
|
|
|
|
"discord.gg",
|
2023-10-15 11:11:43 -04:00
|
|
|
"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
|
2017-11-19 11:27:38 -05:00
|
|
|
self.normalized_url = self.class.normalize(url)
|
2014-10-13 15:11:02 -04:00
|
|
|
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
|
2018-05-16 18:33:32 -04:00
|
|
|
if is_active?
|
|
|
|
url
|
|
|
|
else
|
|
|
|
"-#{url}"
|
|
|
|
end
|
2010-02-15 13:59:58 -05:00
|
|
|
end
|
2017-07-29 02:37:30 -04:00
|
|
|
|
|
|
|
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])
|
2017-07-29 02:37:30 -04:00
|
|
|
rescue Addressable::URI::InvalidURIError => error
|
2021-02-07 20:16:52 -05:00
|
|
|
errors.add(:url, "'#{uri}' is malformed: #{error}")
|
2017-07-29 02:37:30 -04:00
|
|
|
end
|
2010-02-15 13:59:58 -05:00
|
|
|
end
|