Browse Source

#309

feature/309
みてるぞ 2 weeks ago
parent
commit
96307af509
12 changed files with 63 additions and 47 deletions
  1. +1
    -10
      backend/app/controllers/nico_tags_controller.rb
  2. +4
    -4
      backend/app/controllers/posts_controller.rb
  3. +5
    -12
      backend/app/controllers/tags_controller.rb
  4. +7
    -0
      backend/app/models/nico_tag_version.rb
  5. +2
    -3
      backend/app/models/tag.rb
  6. +0
    -2
      backend/app/models/tag_name.rb
  7. +1
    -2
      backend/app/models/tag_version.rb
  8. +4
    -1
      backend/app/services/nico_tag_version_recorder.rb
  9. +8
    -8
      backend/app/services/post_version_recorder.rb
  10. +4
    -4
      backend/app/services/tag_version_recorder.rb
  11. +26
    -0
      backend/app/services/tag_versioning.rb
  12. +1
    -1
      backend/app/services/version_recorder.rb

+ 1
- 10
backend/app/controllers/nico_tags_controller.rb View File

@@ -38,7 +38,7 @@ class NicoTagsController < ApplicationController
return head :bad_request if linked_tags.any? { |t| t.nico? } return head :bad_request if linked_tags.any? { |t| t.nico? }


ApplicationRecord.transaction do ApplicationRecord.transaction do
record_tag_snapshots!(linked_tags, created_by_user: current_user)
TagVersioning.record_tag_snapshots!(linked_tags, created_by_user: current_user)


tag.linked_tags = linked_tags tag.linked_tags = linked_tags
tag.save! tag.save!
@@ -48,13 +48,4 @@ class NicoTagsController < ApplicationController


render json: tag.linked_tags.map { |t| TagRepr.base(t) }, status: :ok render json: tag.linked_tags.map { |t| TagRepr.base(t) }, status: :ok
end end

private

def record_tag_snapshots! tags, created_by_user:
tags.each do |tag|
event_type = tag.tag_versions.exists? ? :update : :create
TagVersionRecorder.record!(tag:, event_type:, created_by_user:)
end
end
end end

+ 4
- 4
backend/app/controllers/posts_controller.rb View File

@@ -131,7 +131,7 @@ class PostsController < ApplicationController
ApplicationRecord.transaction do ApplicationRecord.transaction do
post.save! post.save!
tags = Tag.normalise_tags(tag_names) tags = Tag.normalise_tags(tag_names)
record_tag_snapshots!(tags, created_by_user: current_user)
TagVersioning.record_tag_snapshots!(tags, created_by_user: current_user)


tags = Tag.expand_parent_tags(tags) tags = Tag.expand_parent_tags(tags)
sync_post_tags!(post, tags) sync_post_tags!(post, tags)
@@ -175,10 +175,10 @@ class PostsController < ApplicationController
ApplicationRecord.transaction do ApplicationRecord.transaction do
post.update!(title:, original_created_from:, original_created_before:) post.update!(title:, original_created_from:, original_created_before:)


normalised_tag = Tag.normalise_tags(tag_names, with_tagme: false)
record_tag_snapshots(normalised_tags, create_by_user: current_user)
normalised_tags = Tag.normalise_tags(tag_names, with_tagme: false)
TagVersioning.record_tag_snapshots!(normalised_tags, created_by_user: current_user)


tags = post.tags.where(category: 'nico').to_a + normalised_tags
tags = post.tags.nico.to_a + normalised_tags
tags = Tag.expand_parent_tags(tags) tags = Tag.expand_parent_tags(tags)
sync_post_tags!(post, tags) sync_post_tags!(post, tags)
PostVersionRecorder.record!(post:, event_type: :update, created_by_user: current_user) PostVersionRecorder.record!(post:, event_type: :update, created_by_user: current_user)


+ 5
- 12
backend/app/controllers/tags_controller.rb View File

@@ -218,21 +218,14 @@ class TagsController < ApplicationController


tag = Tag.find(params[:id]) tag = Tag.find(params[:id])


ApplicationRecord.transaction do
old_nico = tag.nico?

if category.present?
new_nico = category == 'nico'

if old_nico != new_nico
return render json: { error: 'ニコタグのカテゴリ変更はできません.' },
status: :unprocessable_entity
end
end
if category.present? && tag.nico? != (category == 'nico')
return render json: { error: 'ニコタグのカテゴリ変更はできません.' },
status: :unprocessable_entity
end


ApplicationRecord.transaction do
tag.tag_name.update!(name:) if name.present? tag.tag_name.update!(name:) if name.present?
tag.update!(category:) if category.present? tag.update!(category:) if category.present?

record_tag_version!(tag, event_type: :update, created_by_user: current_user) record_tag_version!(tag, event_type: :update, created_by_user: current_user)
end end




+ 7
- 0
backend/app/models/nico_tag_version.rb View File

@@ -0,0 +1,7 @@
class NicoTagVersion < ApplicationRecord
include VersionRecord

belongs_to :tag

validates :name, presence: true
end

+ 2
- 3
backend/app/models/tag.rb View File

@@ -157,7 +157,6 @@ class Tag < ApplicationRecord
target_tag => Tag target_tag => Tag


affected_post_ids = Set.new affected_post_ids = Set.new
affected_tag_ids = Set.new


Tag.transaction do Tag.transaction do
Array(source_tags).compact.uniq.each do |source_tag| Array(source_tags).compact.uniq.each do |source_tag|
@@ -181,7 +180,7 @@ class Tag < ApplicationRecord
end end


source_tag.discard! source_tag.discard!
record_tag_discard!(source_tag, current_by_user: nil)
TagVersioning.record!(source_tag, event_type: :discard, created_by_user:)


if source_tag.nico? if source_tag.nico?
source_tag_name.discard! source_tag_name.discard!
@@ -190,7 +189,7 @@ class Tag < ApplicationRecord
updated_at: Time.current) updated_at: Time.current)
end end


record_tag_version!(target_tag, event_type: :update, created_by_user: nil)
TagVersioning.record!(target_tag, event_type: :update, created_by_user:)
end end


Post.where(id: affected_post_ids.to_a).find_each do |post| Post.where(id: affected_post_ids.to_a).find_each do |post|


+ 0
- 2
backend/app/models/tag_name.rb View File

@@ -1,8 +1,6 @@
class TagName < ApplicationRecord class TagName < ApplicationRecord
include MyDiscard include MyDiscard


default_scope -> { kept }

has_one :tag has_one :tag
has_one :wiki_page has_one :wiki_page




+ 1
- 2
backend/app/models/tag_version.rb View File

@@ -8,8 +8,7 @@ class TagVersion < ApplicationRecord
character: 'character', character: 'character',
general: 'general', general: 'general',
material: 'material', material: 'material',
meta: 'meta',
nico: 'nico' }, validate: true
meta: 'meta' }, validate: true


validates :name, presence: true validates :name, presence: true
validates :category, presence: true validates :category, presence: true


+ 4
- 1
backend/app/services/nico_tag_version_recorder.rb View File

@@ -12,5 +12,8 @@ class NicoTagVersionRecorder < VersionRecorder
def version_class = NicoTagVersion def version_class = NicoTagVersion
def version_association = :nico_tag_versions def version_association = :nico_tag_versions
def record_key = :tag def record_key = :tag
def snapshot_attributes = { name: @tag.name, linked_tags: @tag.snapshot_linked_tags.join(' ') }

def snapshot_attributes
{ name: @record.name, linked_tags: @record.snapshot_linked_tags.join(' ') }
end
end end

+ 8
- 8
backend/app/services/post_version_recorder.rb View File

@@ -1,4 +1,4 @@
class PostVersionRecorder
class PostVersionRecorder < VersionRecorder
def self.record! post:, event_type:, created_by_user: def self.record! post:, event_type:, created_by_user:
new(post:, event_type:, created_by_user:).record! new(post:, event_type:, created_by_user:).record!
end end
@@ -14,12 +14,12 @@ class PostVersionRecorder
def record_key = :post def record_key = :post


def snapshot_attributes def snapshot_attributes
{ title: @post.title,
url: @post.url,
thumbnail_base: @post.thumbnail_base,
tags: @post.snapshot_tag_names.join(' '),
parent: @post.parent,
original_created_from: @post.original_created_from,
original_created_before: @post.original_created_before }
{ title: @record.title,
url: @record.url,
thumbnail_base: @record.thumbnail_base,
tags: @record.snapshot_tag_names.join(' '),
parent: @record.parent,
original_created_from: @record.original_created_from,
original_created_before: @record.original_created_before }
end end
end end

+ 4
- 4
backend/app/services/tag_version_recorder.rb View File

@@ -14,9 +14,9 @@ class TagVersionRecorder < VersionRecorder
def record_key = :tag def record_key = :tag


def snapshot_attributes def snapshot_attributes
{ name: @tag.name,
category: @tag.category,
aliases: @tag.snapshot_aliases.join(' '),
parent_tag_ids: @tag.snapshot_parent_tag_ids.join(' ') }
{ name: @record.name,
category: @record.category,
aliases: @record.snapshot_aliases.join(' '),
parent_tag_ids: @record.snapshot_parent_tag_ids.join(' ') }
end end
end end

+ 26
- 0
backend/app/services/tag_versioning.rb View File

@@ -0,0 +1,26 @@
class TagVersioning
def self.record! tag, event_type:, created_by_user:
if tag.nico?
NicoTagVersionRecorder.record!(tag:, event_type:, created_by_user:)
else
TagVersionRecorder.record!(tag:, event_type:, created_by_user:)
end
end

def self.record_tag_snapshot! tag, created_by_user:
event_type =
if tag.nico?
tag.nico_tag_versions.exists? ? :update : :create
else
tag.tag_versions.exists? ? :update : :create
end

record!(tag, event_type:, created_by_user:)
end

def self.record_tag_snapshots! tags, created_by_user:
tags.each do |tag|
record_tag_snapshot!(tag, created_by_user:)
end
end
end

+ 1
- 1
backend/app/services/version_recorder.rb View File

@@ -9,7 +9,7 @@ class VersionRecorder
validate_event_type! validate_event_type!
end end


def record! record, event_type:, created_by_user:
def record!
@record.with_lock do @record.with_lock do
latest = latest_version latest = latest_version




Loading…
Cancel
Save