diff --git a/app/models/pool_version.rb b/app/models/pool_version.rb deleted file mode 100644 index 47a240077..000000000 --- a/app/models/pool_version.rb +++ /dev/null @@ -1,110 +0,0 @@ -class PoolVersion < ApplicationRecord - class Error < StandardError; end - - belongs_to :pool - belongs_to_updater - - module SearchMethods - def for_user(user_id) - where("updater_id = ?", user_id) - end - - def search(params) - q = super - q = q.search_attributes(params, :updater, :pool_id) - q.apply_default_order(params) - end - end - - extend SearchMethods - - def self.export_to_archives(starting_version_id = 0) - raise NotImplementedError.new("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 pretty_name - name.tr("_", " ") - end - - def post_id_array - @post_id_array ||= post_ids.scan(/\d+/).map(&:to_i) - end - - def diff(version) - new_posts = post_id_array - old_posts = version.present? ? version.post_id_array : [] - - return { - :added_posts => array_difference_with_duplicates(new_posts, old_posts), - :removed_posts => array_difference_with_duplicates(old_posts, new_posts), - :unchanged_posts => array_intersect_with_duplicates(new_posts, old_posts) - } - end - - def array_difference_with_duplicates(array, other_array) - new_array = array.dup - other_array.each do |id| - index = new_array.index(id) - if index - new_array.delete_at(index) - end - end - new_array - end - - def array_intersect_with_duplicates(array, other_array) - other_array = other_array.dup - array.inject([]) do |intersect, id| - index = other_array.index(id) - if index - intersect << id - other_array.delete_at(index) - end - intersect - end - end - - def changes - @changes ||= diff(previous) - end - - def previous - PoolArchive.where(["pool_id = ? and updated_at < ?", pool_id, updated_at]).order("updated_at desc").first - end -end diff --git a/app/models/post_version.rb b/app/models/post_version.rb deleted file mode 100644 index a2d0c43ff..000000000 --- a/app/models/post_version.rb +++ /dev/null @@ -1,165 +0,0 @@ -class PostVersion < ApplicationRecord - belongs_to :post - belongs_to :updater, :class_name => "User" - before_validation :initialize_updater - - module SearchMethods - def for_user(user_id) - where("updater_id = ?", user_id) - end - - def updater_name(name) - where("updater_id = (select _.id from users _ where lower(_.name) = ?)", name.mb_chars.downcase) - end - - def search(params) - q = super - - if params[:updater_name].present? - q = q.updater_name(params[:updater_name]) - end - - if params[:updater_id].present? - q = q.where("updater_id = ?", params[:updater_id].to_i) - end - - if params[:post_id].present? - q = q.where("post_id = ?", params[:post_id].to_i) - end - - if params[:start_id].present? - q = q.where("id <= ?", params[:start_id].to_i) - end - - q.apply_default_order(params) - end - end - - extend SearchMethods - - def self.create_from_post(post) - if post.created_at == post.updated_at - create_from_created_post(post) - else - create_from_updated_post(post) - end - end - - def initialize_updater - self.updater_id = CurrentUser.id - self.updater_ip_addr = CurrentUser.ip_addr - end - - def tag_array - @tag_array ||= tags.split - end - - def reload - @tag_array = nil - super - end - - def diff(version) - latest_tags = post.tag_array - latest_tags << "rating:#{post.rating}" if post.rating.present? - latest_tags << "parent:#{post.parent_id}" if post.parent_id.present? - latest_tags << "source:#{post.source}" if post.source.present? - - new_tags = tag_array - new_tags << "rating:#{rating}" if rating.present? - new_tags << "parent:#{parent_id}" if parent_id.present? - new_tags << "source:#{source}" if source.present? - - old_tags = version.present? ? version.tag_array : [] - if version.present? - old_tags << "rating:#{version.rating}" if version.rating.present? - old_tags << "parent:#{version.parent_id}" if version.parent_id.present? - old_tags << "source:#{version.source}" if version.source.present? - end - - added_tags = new_tags - old_tags - removed_tags = old_tags - new_tags - - return { - :added_tags => added_tags, - :removed_tags => removed_tags, - :obsolete_added_tags => added_tags - latest_tags, - :obsolete_removed_tags => removed_tags & latest_tags, - :unchanged_tags => new_tags & old_tags - } - end - - def changes - @changes ||= diff(previous) - end - - def added_tags - changes[:added_tags].join(" ") - end - - def removed_tags - changes[:removed_tags].join(" ") - end - - def obsolete_added_tags - changes[:obsolete_added_tags].join(" ") - end - - def obsolete_removed_tags - changes[:obsolete_removed_tags].join(" ") - end - - def unchanged_tags - changes[:unchanged_tags].join(" ") - end - - def previous - if updated_at.to_i == Time.zone.parse("2007-03-14T19:38:12Z").to_i - # Old post versions which didn't have updated_at set correctly - PostVersion.where("post_id = ? and updated_at = ? and id < ?", post_id, updated_at, id).order("updated_at desc, id desc").first - else - PostVersion.where("post_id = ? and updated_at < ?", post_id, updated_at).order("updated_at desc, id desc").first - end - end - - def truncated_source - source.gsub(/^http:\/\//, "").sub(/\/.+/, "") - end - - def undo - changes = diff(previous) - added = changes[:added_tags] - changes[:obsolete_added_tags] - removed = changes[:removed_tags] - changes[:obsolete_removed_tags] - - added.each do |tag| - if tag =~ /^source:/ - post.source = "" - elsif tag =~ /^parent:/ - post.parent_id = nil - else - escaped_tag = Regexp.escape(tag) - post.tag_string = post.tag_string.sub(/(?:\A| )#{escaped_tag}(?:\Z| )/, " ").strip - end - end - removed.each do |tag| - if tag =~ /^source:(.+)$/ - post.source = $1 - else - post.tag_string = "#{post.tag_string} #{tag}".strip - end - end - end - - def undo! - undo - post.save! - end - - def updater_name - updater.name - end - - def method_attributes - super + [:added_tags, :removed_tags, :obsolete_added_tags, :obsolete_removed_tags, :unchanged_tags, :updater_name] - end -end