support pool version archive
This commit is contained in:
@@ -11,7 +11,6 @@ class Pool < ActiveRecord::Base
|
||||
validate :updater_can_remove_posts
|
||||
belongs_to :creator, :class_name => "User"
|
||||
belongs_to :updater, :class_name => "User"
|
||||
has_many :versions, lambda {order("pool_versions.id ASC")}, :class_name => "PoolVersion", :dependent => :destroy
|
||||
before_validation :normalize_post_ids
|
||||
before_validation :normalize_name
|
||||
before_validation :initialize_is_active, :on => :create
|
||||
@@ -158,6 +157,14 @@ class Pool < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
def versions
|
||||
if PoolArchive.enabled?
|
||||
PoolArchive.where("pool_id = ?", id).order("id asc")
|
||||
else
|
||||
raise "Archive service not configured"
|
||||
end
|
||||
end
|
||||
|
||||
def is_series?
|
||||
category == "series"
|
||||
end
|
||||
@@ -200,8 +207,10 @@ class Pool < ActiveRecord::Base
|
||||
raise RevertError.new("You cannot revert to a previous version of another pool.")
|
||||
end
|
||||
|
||||
self.post_ids = version.post_ids
|
||||
self.post_ids = version.post_ids.join(" ")
|
||||
self.name = version.name
|
||||
self.description = version.description
|
||||
|
||||
synchronize!
|
||||
end
|
||||
|
||||
@@ -294,7 +303,7 @@ class Pool < ActiveRecord::Base
|
||||
|
||||
def synchronize!
|
||||
synchronize
|
||||
save
|
||||
save if post_ids_changed?
|
||||
end
|
||||
|
||||
def post_id_array
|
||||
@@ -336,17 +345,10 @@ class Pool < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def create_version(force = false)
|
||||
if post_ids_changed? || name_changed? || description_changed? || is_active_changed? || is_deleted_changed? || category_changed? || force
|
||||
last_version = versions.last
|
||||
|
||||
if last_version && last_version.updater_ip_addr == CurrentUser.ip_addr && CurrentUser.user.id == last_version.updater_id && last_version.created_at > 1.hour.ago
|
||||
# merge
|
||||
last_version.update_column(:post_ids, post_ids)
|
||||
last_version.update_column(:name, name)
|
||||
else
|
||||
# create
|
||||
versions.create(:post_ids => post_ids, :name => name)
|
||||
end
|
||||
if PoolArchive.enabled?
|
||||
PoolArchive.queue(self)
|
||||
else
|
||||
Rails.logger.warn("Archive service is not configured. Pool versions will not be saved.")
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
77
app/models/pool_archive.rb
Normal file
77
app/models/pool_archive.rb
Normal file
@@ -0,0 +1,77 @@
|
||||
class PoolArchive < ActiveRecord::Base
|
||||
establish_connection "archive_#{Rails.env}".to_sym
|
||||
self.table_name = "pool_versions"
|
||||
|
||||
module SearchMethods
|
||||
def for_user(user_id)
|
||||
where("updater_id = ?", user_id)
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = where("true")
|
||||
return q if params.blank?
|
||||
|
||||
if params[:updater_id].present?
|
||||
q = q.for_user(params[:updater_id].to_i)
|
||||
end
|
||||
|
||||
if params[:updater_name].present?
|
||||
q = q.where("updater_id = ?", User.name_to_id(params[:updater_name]))
|
||||
end
|
||||
|
||||
if params[:pool_id].present?
|
||||
q = q.where("pool_id = ?", params[:pool_id].to_i)
|
||||
end
|
||||
|
||||
q
|
||||
end
|
||||
end
|
||||
|
||||
extend SearchMethods
|
||||
|
||||
def self.enabled?
|
||||
Danbooru.config.aws_sqs_archives_url.present?
|
||||
end
|
||||
|
||||
def self.sqs_service
|
||||
SqsService.new(Danbooru.config.aws_sqs_archives_url)
|
||||
end
|
||||
|
||||
def self.queue(pool)
|
||||
# queue updates to sqs so that if archives goes down for whatever reason it won't
|
||||
# block pool updates
|
||||
raise "Archive service is not configured" if !enabled?
|
||||
|
||||
json = {
|
||||
pool_id: pool.id,
|
||||
post_ids: pool.post_ids.scan(/\d+/).map(&:to_i),
|
||||
updater_id: CurrentUser.id,
|
||||
updater_ip_addr: CurrentUser.ip_addr.to_s,
|
||||
created_at: pool.created_at.try(:iso8601),
|
||||
updated_at: pool.updated_at.try(:iso8601),
|
||||
description: pool.description,
|
||||
name: pool.name,
|
||||
is_active: pool.is_active?,
|
||||
is_deleted: pool.is_deleted?,
|
||||
category: pool.category
|
||||
}
|
||||
msg = "add pool version\n#{json.to_json}"
|
||||
sqs_service.send_message(msg)
|
||||
end
|
||||
|
||||
def pool
|
||||
Pool.find(pool_id)
|
||||
end
|
||||
|
||||
def updater
|
||||
User.find(updater_id)
|
||||
end
|
||||
|
||||
def updater_name
|
||||
User.id_to_name(updater_id)
|
||||
end
|
||||
|
||||
def pretty_name
|
||||
name.tr("_", " ")
|
||||
end
|
||||
end
|
||||
@@ -34,6 +34,46 @@ class PoolVersion < ActiveRecord::Base
|
||||
|
||||
extend SearchMethods
|
||||
|
||||
def self.export_to_archives(starting_version_id = 0)
|
||||
raise "SQS URL not setup" if Danbooru.config.aws_sqs_archives_url.nil?
|
||||
|
||||
credentials = Aws::Credentials.new(
|
||||
Danbooru.config.aws_access_key_id,
|
||||
Danbooru.config.aws_secret_access_key
|
||||
)
|
||||
sqs = Aws::SQS::Client.new(
|
||||
credentials: credentials,
|
||||
region: Danbooru.config.aws_sqs_region
|
||||
)
|
||||
last_version_id = 0
|
||||
|
||||
where("id > ?", starting_version_id).find_each do |version|
|
||||
last_version_id = version.id
|
||||
|
||||
json = {
|
||||
id: version.id,
|
||||
pool_id: version.pool_id,
|
||||
post_ids: version.post_ids.scan(/\d+/).map(&:to_i),
|
||||
updater_id: version.updater_id,
|
||||
updater_ip_addr: version.updater_ip_addr.to_s,
|
||||
created_at: version.created_at.try(:iso8601),
|
||||
updated_at: version.updated_at.try(:iso8601),
|
||||
description: version.pool.description,
|
||||
name: version.name,
|
||||
is_active: version.pool.is_active?,
|
||||
is_deleted: version.pool.is_deleted?,
|
||||
category: version.pool.category
|
||||
}
|
||||
msg = "add pool version\n#{json.to_json}"
|
||||
sqs.send_message(
|
||||
message_body: msg,
|
||||
queue_url: Danbooru.config.aws_sqs_archives_url
|
||||
)
|
||||
end
|
||||
|
||||
puts "last version id: #{last_version_id}"
|
||||
end
|
||||
|
||||
def updater_name
|
||||
User.id_to_name(updater_id)
|
||||
end
|
||||
@@ -81,7 +121,7 @@ class PoolVersion < ActiveRecord::Base
|
||||
intersect << id
|
||||
other_array.delete_at(index)
|
||||
end
|
||||
intersect
|
||||
intersect
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user