2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-08-14 23:05:53 +08:00
|
|
|
# A ScreenedUrl record represents a URL that is being watched.
|
|
|
|
# If the URL is found in a post, some action can be performed.
|
|
|
|
|
|
|
|
# For now, nothing is done. We're just collecting the data and will decide
|
|
|
|
# what to do with it later.
|
|
|
|
class ScreenedUrl < ActiveRecord::Base
|
|
|
|
|
|
|
|
include ScreeningModel
|
|
|
|
|
|
|
|
default_action :do_nothing
|
|
|
|
|
2013-09-17 22:25:45 +08:00
|
|
|
before_validation :normalize
|
2013-08-14 23:05:53 +08:00
|
|
|
|
|
|
|
validates :url, presence: true, uniqueness: true
|
|
|
|
validates :domain, presence: true
|
|
|
|
|
2013-09-17 22:25:45 +08:00
|
|
|
def normalize
|
2013-10-17 04:39:13 +08:00
|
|
|
self.url = ScreenedUrl.normalize_url(self.url) if self.url
|
2013-11-05 05:24:32 +08:00
|
|
|
self.domain = self.domain.downcase.sub(/^www\./, '') if self.domain
|
2013-08-14 23:05:53 +08:00
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def self.watch(url, domain, opts = {})
|
2013-10-17 04:39:13 +08:00
|
|
|
find_match(url) || create(opts.slice(:action_type, :ip_address).merge(url: url, domain: domain))
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.find_match(url)
|
|
|
|
find_by_url normalize_url(url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.normalize_url(url)
|
|
|
|
normalized = url.gsub(/http(s?):\/\//i, '')
|
|
|
|
normalized.gsub!(/(\/)+$/, '') # trim trailing slashes
|
|
|
|
normalized.gsub!(/^([^\/]+)(?:\/)?/) { |m| m.downcase } # downcase the domain part of the url
|
|
|
|
normalized
|
2013-08-14 23:05:53 +08:00
|
|
|
end
|
|
|
|
end
|
2013-08-28 08:42:58 +08:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: screened_urls
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
2019-01-12 03:29:56 +08:00
|
|
|
# url :string not null
|
|
|
|
# domain :string not null
|
2013-08-28 08:42:58 +08:00
|
|
|
# action_type :integer not null
|
|
|
|
# match_count :integer default(0), not null
|
|
|
|
# last_match_at :datetime
|
2014-08-27 13:19:25 +08:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2013-12-05 14:40:35 +08:00
|
|
|
# ip_address :inet
|
2013-08-28 08:42:58 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_screened_urls_on_last_match_at (last_match_at)
|
|
|
|
# index_screened_urls_on_url (url) UNIQUE
|
|
|
|
#
|