2020-12-09 04:19:10 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: link_blocks
|
|
|
|
#
|
|
|
|
# id :bigint(8) not null, primary key
|
|
|
|
# link :string default(""), not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
#
|
|
|
|
|
|
|
|
class LinkBlock < ApplicationRecord
|
|
|
|
include LinkNormalizable
|
|
|
|
|
|
|
|
validates :link, presence: true, uniqueness: true
|
|
|
|
|
2021-01-19 06:25:25 +00:00
|
|
|
scope :alphabetical, -> { reorder(link: :asc) }
|
|
|
|
|
2020-12-09 04:19:10 +00:00
|
|
|
def self.block?(text)
|
|
|
|
return false if text.nil?
|
|
|
|
return false if text.length < 1
|
|
|
|
|
2021-01-17 22:36:20 +00:00
|
|
|
return true if text.include? '.weebly.com'
|
|
|
|
|
2020-12-16 00:31:30 +00:00
|
|
|
urls = text.scan(FetchLinkCardService::URL_PATTERN).map {|array|
|
|
|
|
Addressable::URI.parse(array[0]).normalize
|
|
|
|
}
|
2020-12-09 04:19:10 +00:00
|
|
|
url = urls.first
|
2020-12-16 07:39:07 +00:00
|
|
|
|
|
|
|
return false if url.nil?
|
|
|
|
|
2020-12-09 04:19:10 +00:00
|
|
|
link_for_fetch = TagManager.instance.normalize_link(url)
|
2020-12-16 00:31:30 +00:00
|
|
|
link_for_fetch = link_for_fetch.chomp("/")
|
|
|
|
|
2021-02-11 05:01:45 +00:00
|
|
|
domain_for_fetch = TagManager.instance.normalize_link_domain(url)
|
|
|
|
|
|
|
|
where("LOWER(link) LIKE LOWER(?)", "%#{link_for_fetch}").or(
|
|
|
|
where("LOWER(link) LIKE LOWER(?)", "#{domain_for_fetch}")
|
|
|
|
).exists?
|
2020-12-09 04:19:10 +00:00
|
|
|
end
|
2021-01-17 22:36:20 +00:00
|
|
|
end
|