@@ -1,6 +1,6 @@
|
||||
class Tag < ApplicationRecord
|
||||
COSINE_SIMILARITY_RELATED_TAG_THRESHOLD = 1000
|
||||
METATAGS = "-user|user|-approver|approver|commenter|comm|noter|noteupdater|artcomm|-pool|pool|ordpool|-favgroup|favgroup|-fav|fav|ordfav|sub|md5|-rating|rating|-locked|locked|width|height|mpixels|ratio|score|favcount|filesize|source|-source|id|-id|date|age|order|limit|-status|status|tagcount|gentags|arttags|chartags|copytags|parent|-parent|child|pixiv_id|pixiv|search|upvote|downvote|filetype|-filetype|flagger|-flagger|appealer|-appealer"
|
||||
METATAGS = "-user|user|-approver|approver|commenter|comm|noter|noteupdater|artcomm|-pool|pool|ordpool|-favgroup|favgroup|-fav|fav|ordfav|md5|-rating|rating|-locked|locked|width|height|mpixels|ratio|score|favcount|filesize|source|-source|id|-id|date|age|order|limit|-status|status|tagcount|gentags|arttags|chartags|copytags|parent|-parent|child|pixiv_id|pixiv|search|upvote|downvote|filetype|-filetype|flagger|-flagger|appealer|-appealer"
|
||||
SUBQUERY_METATAGS = "commenter|comm|noter|noteupdater|artcomm|flagger|-flagger|appealer|-appealer"
|
||||
attr_accessible :category, :as => [:moderator, :gold, :platinum, :member, :anonymous, :default, :builder, :admin]
|
||||
attr_accessible :is_locked, :as => [:moderator, :admin]
|
||||
|
||||
@@ -1,13 +1,7 @@
|
||||
class TagSubscription < ApplicationRecord
|
||||
belongs_to :creator, :class_name => "User"
|
||||
before_validation :initialize_creator, :on => :create
|
||||
before_validation :initialize_post_ids, :on => :create
|
||||
before_save :normalize_name
|
||||
before_save :limit_tag_count
|
||||
attr_accessible :name, :tag_query, :post_ids, :is_public, :is_visible_on_profile
|
||||
validates_presence_of :name, :tag_query, :creator_id
|
||||
validate :validate_number_of_queries
|
||||
validate :creator_can_create_subscriptions, :on => :create
|
||||
|
||||
def migrate_to_saved_searches
|
||||
tag_query.split(/\r\n|\r|\n/).each do |query|
|
||||
@@ -15,10 +9,6 @@ class TagSubscription < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
def normalize_name
|
||||
self.name = name.gsub(/\s+/, "_")
|
||||
end
|
||||
|
||||
def pretty_name
|
||||
name.tr("_", " ")
|
||||
end
|
||||
@@ -27,60 +17,14 @@ class TagSubscription < ApplicationRecord
|
||||
tag_query_array.join(", ")
|
||||
end
|
||||
|
||||
def initialize_creator
|
||||
self.creator_id = CurrentUser.id
|
||||
end
|
||||
|
||||
def initialize_post_ids
|
||||
process
|
||||
end
|
||||
|
||||
def creator_can_create_subscriptions
|
||||
if TagSubscription.owned_by(creator).count >= Danbooru.config.max_tag_subscriptions
|
||||
self.errors.add(:creator, "can create up to #{Danbooru.config.max_tag_subscriptions} tag subscriptions")
|
||||
return false
|
||||
else
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
def validate_number_of_queries
|
||||
if tag_query_array.size > 20 || tag_query_array.size < 1
|
||||
self.errors.add(:tag_query, "can have up to 20 queries")
|
||||
return false
|
||||
else
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
def tag_query_array
|
||||
tag_query.scan(/[^\r\n]+/).map(&:strip)
|
||||
end
|
||||
|
||||
def limit_tag_count
|
||||
# self.tag_query = tag_query_array.slice(0, 20).join(" ")
|
||||
end
|
||||
|
||||
def process
|
||||
divisor = [tag_query_array.size / 2, 1].max
|
||||
post_ids = tag_query_array.inject([]) do |all, query|
|
||||
all += PostReadOnly.tag_match(query).limit(Danbooru.config.tag_subscription_post_limit / divisor).select("posts.id").order("posts.id DESC").map(&:id)
|
||||
end
|
||||
self.post_ids = post_ids.sort.reverse.slice(0, Danbooru.config.tag_subscription_post_limit).join(",")
|
||||
end
|
||||
|
||||
def is_active?
|
||||
creator.is_gold? && creator.last_logged_in_at && creator.last_logged_in_at > 3.months.ago
|
||||
end
|
||||
|
||||
def editable_by?(user)
|
||||
user.is_moderator? || creator_id == user.id
|
||||
end
|
||||
|
||||
def post_id_array
|
||||
post_ids.split(/,/)
|
||||
end
|
||||
|
||||
module SearchMethods
|
||||
def visible_to(user)
|
||||
where("(is_public = TRUE OR creator_id = ? OR ?)", user.id, user.is_moderator?)
|
||||
@@ -117,71 +61,4 @@ class TagSubscription < ApplicationRecord
|
||||
end
|
||||
|
||||
extend SearchMethods
|
||||
|
||||
def self.find_tags(subscription_name)
|
||||
if subscription_name =~ /^(.+?):(.+)$/
|
||||
user_name = $1
|
||||
sub_group = $2
|
||||
else
|
||||
user_name = subscription_name
|
||||
sub_group = nil
|
||||
end
|
||||
|
||||
user = User.find_by_name(user_name)
|
||||
|
||||
if user
|
||||
relation = where(["creator_id = ?", user.id])
|
||||
|
||||
if sub_group
|
||||
relation = relation.where(["name ILIKE ? ESCAPE E'\\\\'", sub_group.to_escaped_for_sql_like])
|
||||
end
|
||||
|
||||
relation.map {|x| x.tag_query_array}.flatten
|
||||
else
|
||||
[]
|
||||
end
|
||||
end
|
||||
|
||||
def self.find_post_ids(user_id, name = nil, limit = Danbooru.config.tag_subscription_post_limit)
|
||||
relation = where("creator_id = ?", user_id)
|
||||
|
||||
if name
|
||||
relation = relation.where("lower(name) LIKE ? ESCAPE E'\\\\'", name.mb_chars.downcase.to_escaped_for_sql_like)
|
||||
end
|
||||
|
||||
relation.each do |tag_sub|
|
||||
tag_sub.update_column(:last_accessed_at, Time.now)
|
||||
end
|
||||
|
||||
relation.map {|x| x.post_ids.split(/,/)}.flatten.uniq.map(&:to_i).sort.reverse.slice(0, limit)
|
||||
end
|
||||
|
||||
def self.find_posts(user_id, name = nil, limit = Danbooru.config.tag_subscription_post_limit)
|
||||
arel = Post.where(["id in (?)", find_post_ids(user_id, name, limit)])
|
||||
|
||||
if CurrentUser.user.hide_deleted_posts?
|
||||
arel = arel.where("is_deleted = false")
|
||||
end
|
||||
|
||||
arel.order("id DESC").limit(limit)
|
||||
end
|
||||
|
||||
def self.process(id)
|
||||
tag_subscription = TagSubscription.find(id)
|
||||
CurrentUser.scoped(tag_subscription.creator, "127.0.0.1") do
|
||||
tag_subscription.process
|
||||
tag_subscription.save
|
||||
end
|
||||
rescue Exception => x
|
||||
raise if Rails.env != "production"
|
||||
end
|
||||
|
||||
def self.process_all
|
||||
find_each do |tag_subscription|
|
||||
if tag_subscription.is_active?
|
||||
time = rand(20 * 60 * 60).seconds.from_now
|
||||
TagSubscription.delay(:run_at => time, :queue => "default", :priority => 10).process(tag_subscription.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user