From 6e33c5f1923ece8deae16e994014a4752db3d239 Mon Sep 17 00:00:00 2001 From: miteruzo Date: Mon, 21 Sep 2026 03:30:22 +0900 Subject: [PATCH] #411 --- backend/app/controllers/posts_controller.rb | 24 +++++++++------------ backend/app/models/post.rb | 8 +------ backend/app/models/post_tag.rb | 22 +------------------ backend/app/models/tag.rb | 12 +++++------ backend/app/representations/post_repr.rb | 6 +++--- backend/app/services/post_creator.rb | 7 +++--- backend/app/services/youtube/sync.rb | 6 +++--- backend/lib/tasks/sync_nico.rake | 6 +++--- 8 files changed, 30 insertions(+), 61 deletions(-) diff --git a/backend/app/controllers/posts_controller.rb b/backend/app/controllers/posts_controller.rb index 3f5a92c..0730ff7 100644 --- a/backend/app/controllers/posts_controller.rb +++ b/backend/app/controllers/posts_controller.rb @@ -50,9 +50,8 @@ class PostsController < ApplicationController .joins("LEFT JOIN (#{ pt_max_sql }) pt_max ON pt_max.post_id = posts.id") .reselect('posts.*', Arel.sql("#{ updated_at_all_sql } AS updated_at_all")) .preload(:uploaded_user, :parents, :children, - active_post_tags: [:sections, - { tag: [:deerjikists, :materials, - { tag_name: :wiki_page }] }]) + post_tags: [:sections, { tag: [:deerjikists, :materials, + { tag_name: :wiki_page }] }]) .with_attached_thumbnail q = q.where('posts.url LIKE ?', "%#{ url }%") if url @@ -104,9 +103,8 @@ class PostsController < ApplicationController def random post = filtered_posts.preload(:uploaded_user, :parents, :children, - active_post_tags: [:sections, - { tag: [:deerjikists, :materials, - { tag_name: :wiki_page }] }]) + post_tags: [:sections, { tag: [:deerjikists, :materials, + { tag_name: :wiki_page }] }]) .with_attached_thumbnail .order('RAND()') .first @@ -190,9 +188,8 @@ class PostsController < ApplicationController post = Post .includes(:uploaded_user, :parents, :children, - active_post_tags: [:sections, - { tag: [:deerjikists, :materials, - { tag_name: :wiki_page }] }]) + post_tags: [:sections, { tag: [:deerjikists, :materials, + { tag_name: :wiki_page }] }]) .with_attached_thumbnail .find_by(id: params[:id]) return head :not_found unless post @@ -395,7 +392,7 @@ class PostsController < ApplicationController offset = (page - 1) * limit - pts = PostTag.with_discarded + pts = PostTag pts = pts.where(post_id: id) if id.present? pts = pts.where(tag_id:) if tag_id.present? pts = pts.includes(:post, :created_user, :deleted_user, @@ -502,13 +499,13 @@ class PostsController < ApplicationController end end - PostTag.where(post_id: post.id, tag_id: to_remove.to_a).kept.find_each do |pt| - pt.discard_by!(current_user) + PostTag.where(post_id: post.id, tag_id: to_remove.to_a).find_each do |pt| + pt.destroy! end end def build_tag_tree_for post - post_tags = post.active_post_tags.reject { |post_tag| post_tag.tag.deprecated? } + post_tags = post.post_tags.reject { |post_tag| post_tag.tag.deprecated? } tags = post_tags.map(&:tag) tag_ids = tags.map(&:id) @@ -717,7 +714,6 @@ class PostsController < ApplicationController def editable_tag_names_from_post post post .post_tags - .kept .joins(tag: :tag_name) .merge(Tag.not_nico) .merge(Tag.where(deprecated_at: nil)) diff --git a/backend/app/models/post.rb b/backend/app/models/post.rb index 08f8fec..d18d34e 100644 --- a/backend/app/models/post.rb +++ b/backend/app/models/post.rb @@ -55,11 +55,7 @@ class Post < ApplicationRecord belongs_to :uploaded_user, class_name: 'User', optional: true has_many :post_tags, dependent: :destroy, inverse_of: :post - has_many :active_post_tags, -> { kept }, class_name: 'PostTag', inverse_of: :post - has_many :post_tags_with_discarded, -> { with_discarded }, class_name: 'PostTag' - has_many :tags, through: :active_post_tags - has_many :active_tags, -> { where(tags: { deprecated_at: nil }) }, - through: :active_post_tags, source: :tag + has_many :tags, through: :post_tags has_many :user_post_views, dependent: :delete_all has_many :post_similarities, dependent: :delete_all @@ -123,7 +119,6 @@ class Post < ApplicationRecord def snapshot_tag_names post_tags - .kept .joins(tag: :tag_name) .includes(:sections, tag: :tag_name) .order('tag_names.name') @@ -150,7 +145,6 @@ class Post < ApplicationRecord def snapshot_tags_json post_tags - .kept .joins(tag: :tag_name) .includes(:sections, tag: :tag_name) .order('tags.id') diff --git a/backend/app/models/post_tag.rb b/backend/app/models/post_tag.rb index ac56c77..7ed85b0 100644 --- a/backend/app/models/post_tag.rb +++ b/backend/app/models/post_tag.rb @@ -1,14 +1,7 @@ class PostTag < ApplicationRecord - include Discard::Model - - before_destroy do - raise ActiveRecord::ReadOnlyRecord, '消さないでください.' - end - belongs_to :post belongs_to :tag, counter_cache: :post_count belongs_to :created_user, class_name: 'User', optional: true - belongs_to :deleted_user, class_name: 'User', optional: true has_many :sections, -> { order(:begin_ms) }, class_name: 'PostTagSection', foreign_key: [:post_id, :tag_id], @@ -18,18 +11,5 @@ class PostTag < ApplicationRecord validates :post_id, presence: true validates :tag_id, presence: true - validates :post_id, uniqueness: { - scope: :tag_id, - conditions: -> { where(discarded_at: nil) } } - - def discard_by! deleted_user - return self if discarded? - - transaction do - update!(discarded_at: Time.current, deleted_user:) - Tag.where(id: tag_id).update_all('post_count = GREATEST(post_count - 1, 0)') - end - - self - end + validates :post_id, uniqueness: { scope: :tag_id } end diff --git a/backend/app/models/tag.rb b/backend/app/models/tag.rb index 202610b..4a4598f 100644 --- a/backend/app/models/tag.rb +++ b/backend/app/models/tag.rb @@ -28,9 +28,7 @@ class Tag < ApplicationRecord end has_many :post_tags, inverse_of: :tag - has_many :active_post_tags, -> { kept }, class_name: 'PostTag', inverse_of: :tag - has_many :post_tags_with_discarded, -> { with_discarded }, class_name: 'PostTag' - has_many :posts, through: :active_post_tags + has_many :posts, through: :post_tags has_many :nico_tag_relations, foreign_key: :nico_tag_id, dependent: :destroy has_many :linked_tags, through: :nico_tag_relations, source: :tag @@ -259,11 +257,11 @@ class Tag < ApplicationRecord TagVersioning.ensure_snapshot!(source_tag, created_by_user:) - source_tag.post_tags.kept.find_each do |source_pt| + source_tag.post_tags.find_each do |source_pt| post_id = source_pt.post_id affected_post_ids << post_id - source_pt.discard_by!(created_by_user) - unless PostTag.kept.exists?(post_id:, tag: target_tag) + source_pt.destroy! + unless PostTag.exists?(post_id:, tag: target_tag) PostTag.create!(post_id:, tag: target_tag) end end @@ -293,7 +291,7 @@ class Tag < ApplicationRecord end # 投稿件数を再集計 - target_tag.update_columns(post_count: PostTag.kept.where(tag: target_tag).count) + target_tag.update_columns(post_count: PostTag.where(tag: target_tag).count) end target_tag.reload diff --git a/backend/app/representations/post_repr.rb b/backend/app/representations/post_repr.rb index 0dc8a9b..79a1b1d 100644 --- a/backend/app/representations/post_repr.rb +++ b/backend/app/representations/post_repr.rb @@ -88,14 +88,14 @@ module PostRepr def tag_json post post - .active_post_tags + .post_tags .reject { _1.tag.deprecated? } .sort_by { _1.tag.name } - .map { |post_tag| + .map do |post_tag| TagRepr.inline(post_tag.tag).merge( 'children' => [], 'sections' => post_tag.sections.as_json(only: [:begin_ms, :end_ms])) - } + end end def thumbnail_url post, host: nil diff --git a/backend/app/services/post_creator.rb b/backend/app/services/post_creator.rb index 8078991..788a671 100644 --- a/backend/app/services/post_creator.rb +++ b/backend/app/services/post_creator.rb @@ -121,9 +121,11 @@ class PostCreator def sync_post_tags! post, desired_tags, sections desired_ids = desired_tags.map(&:id).to_set current_ids = post.tags.pluck(:id).to_set + Tag.where(id: desired_ids - current_ids).find_each do |tag| PostTag.create_or_find_by!(post:, tag:, created_user: @actor) end + PostTagSection.where(post_id: post.id).destroy_all sections.each do |tag_id, ranges| ranges.each do |begin_ms, end_ms| @@ -133,10 +135,9 @@ class PostCreator end_ms:) end end + PostTag.where(post_id: post.id, - tag_id: (current_ids - desired_ids).to_a).kept.find_each do |post_tag| - post_tag.discard_by!(@actor) - end + tag_id: (current_ids - desired_ids).to_a).destroy_all end def sync_parent_posts! post, ids diff --git a/backend/app/services/youtube/sync.rb b/backend/app/services/youtube/sync.rb index 7fe46f0..e415c95 100644 --- a/backend/app/services/youtube/sync.rb +++ b/backend/app/services/youtube/sync.rb @@ -103,7 +103,7 @@ module Youtube end def sync_post_tags! post, desired_tag_ids, current_tag_ids: nil - current_tag_ids ||= PostTag.kept.where(post_id: post.id).pluck(:tag_id).to_set + current_tag_ids ||= PostTag.where(post_id: post.id).pluck(:tag_id).to_set desired_tag_ids = desired_tag_ids.compact.to_set to_add = desired_tag_ids - current_tag_ids @@ -117,8 +117,8 @@ module Youtube end end - PostTag.where(post_id: post.id, tag_id: to_remove.to_a).kept.find_each do |pt| - pt.discard_by!(nil) + PostTag.where(post_id: post.id, tag_id: to_remove.to_a).find_each do |pt| + pt.destroy! end end diff --git a/backend/lib/tasks/sync_nico.rake b/backend/lib/tasks/sync_nico.rake index 7aca540..f95fba1 100644 --- a/backend/lib/tasks/sync_nico.rake +++ b/backend/lib/tasks/sync_nico.rake @@ -16,7 +16,7 @@ namespace :nico do end def sync_post_tags! post, desired_tag_ids, current_tag_ids: nil - current_tag_ids ||= PostTag.kept.where(post_id: post.id).pluck(:tag_id).to_set + current_tag_ids ||= PostTag.where(post_id: post.id).pluck(:tag_id).to_set desired_tag_ids = desired_tag_ids.compact.to_set to_add = desired_tag_ids - current_tag_ids @@ -30,8 +30,8 @@ namespace :nico do end end - PostTag.where(post_id: post.id, tag_id: to_remove.to_a).kept.find_each do |pt| - pt.discard_by!(nil) + PostTag.where(post_id: post.id, tag_id: to_remove.to_a).find_each do |pt| + pt.destroy! end end