コミットを比較
5
コミット
| 作成者 | SHA1 | 日付 | |
|---|---|---|---|
|
|
3a5d79b5d7 | ||
|
|
6e33c5f192 | ||
|
|
2425f1af1f | ||
|
|
a947032247 | ||
|
|
3115e31fb1 |
@@ -36,8 +36,8 @@ class PostsController < ApplicationController
|
||||
offset = (page - 1) * limit
|
||||
|
||||
pt_max_sql =
|
||||
PostTag
|
||||
.select('post_id, MAX(updated_at) AS max_updated_at')
|
||||
PostVersion
|
||||
.select('post_id, MAX(created_at) AS max_updated_at')
|
||||
.group('post_id')
|
||||
.to_sql
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -200,14 +200,45 @@ class TagsController < ApplicationController
|
||||
.find_by(id: params[:id])
|
||||
return head :not_found unless tag
|
||||
|
||||
rows = normalise_deerjikist_rows(tag)
|
||||
return if performed?
|
||||
|
||||
ApplicationRecord.transaction do
|
||||
tag.deerjikists = []
|
||||
params[:_json].each.with_index do |item, i|
|
||||
platform = item[:platform]
|
||||
code = normalise_deerjikist_code(platform, item[:code])
|
||||
deerjikist = Deerjikist.find_or_initialize_by(platform:, code:)
|
||||
deerjikist.tag = tag
|
||||
render_deerjikist_form_record_invalid(deerjikist, i) unless deerjikist.save
|
||||
tag.lock!
|
||||
|
||||
requested_keys = rows.map { |row| [row[:platform], row[:code]] }.uniq
|
||||
row_indexes_by_key = rows_by_key(rows)
|
||||
locked_deerjikists = lock_deerjikists_for_tag_update(tag.id, requested_keys)
|
||||
current_deerjikists = locked_deerjikists.filter { |deerjikist|
|
||||
deerjikist.tag_id == tag.id
|
||||
}
|
||||
requested_deerjikists = locked_deerjikists.filter { |deerjikist|
|
||||
row_indexes_by_key.key?([deerjikist.platform, deerjikist.code])
|
||||
}
|
||||
|
||||
render_deerjikist_conflicts(requested_deerjikists, row_indexes_by_key, tag)
|
||||
raise ActiveRecord::Rollback if performed?
|
||||
|
||||
requested_keys_set = requested_keys.to_set
|
||||
current_deerjikists.each do |deerjikist|
|
||||
key = [deerjikist.platform, deerjikist.code]
|
||||
deerjikist.destroy! unless requested_keys_set.include?(key)
|
||||
end
|
||||
|
||||
existing_keys = requested_deerjikists.to_h { |deerjikist|
|
||||
[[deerjikist.platform, deerjikist.code], true]
|
||||
}
|
||||
requested_keys.each do |platform, code|
|
||||
next if existing_keys[[platform, code]]
|
||||
|
||||
deerjikist = Deerjikist.new(platform:, code:, tag:)
|
||||
row_index = row_indexes_by_key[[platform, code]].first
|
||||
begin
|
||||
render_deerjikist_form_record_invalid(deerjikist, row_index) unless deerjikist.save
|
||||
rescue ActiveRecord::RecordNotUnique
|
||||
conflicts = lock_deerjikists_for_tag_update(tag.id, [[platform, code]])
|
||||
render_deerjikist_conflicts(conflicts, row_indexes_by_key, tag)
|
||||
end
|
||||
raise ActiveRecord::Rollback if performed?
|
||||
end
|
||||
end
|
||||
@@ -653,6 +684,7 @@ class TagsController < ApplicationController
|
||||
end
|
||||
|
||||
def normalise_deerjikist_code platform, code
|
||||
code = code.to_s
|
||||
return code if platform != 'youtube' || code[0] != '@'
|
||||
|
||||
url = "https://www.youtube.com/#{ code }"
|
||||
@@ -669,6 +701,82 @@ class TagsController < ApplicationController
|
||||
nil
|
||||
end
|
||||
|
||||
def normalise_deerjikist_rows tag
|
||||
rows = []
|
||||
|
||||
params[:_json].each.with_index do |item, index|
|
||||
platform = item[:platform]
|
||||
unless Deerjikist.platforms.key?(platform)
|
||||
render_deerjikist_platform_invalid(index)
|
||||
return rows
|
||||
end
|
||||
|
||||
code = normalise_deerjikist_code(platform, item[:code])
|
||||
deerjikist = Deerjikist.new(platform:, code:, tag:)
|
||||
unless deerjikist.valid?
|
||||
render_deerjikist_form_record_invalid(deerjikist, index)
|
||||
return rows
|
||||
end
|
||||
|
||||
rows << { index:, platform:, code: }
|
||||
end
|
||||
|
||||
rows
|
||||
end
|
||||
|
||||
def lock_deerjikists_for_tag_update tag_id, keys
|
||||
clauses = ['tag_id = ?']
|
||||
values = [tag_id]
|
||||
|
||||
keys.each do |platform, code|
|
||||
clauses << '(platform = ? AND code = ?)'
|
||||
values << platform << code
|
||||
end
|
||||
|
||||
Deerjikist
|
||||
.where(clauses.join(' OR '), *values)
|
||||
.order(:platform, :code)
|
||||
.lock
|
||||
.to_a
|
||||
end
|
||||
|
||||
def render_deerjikist_conflicts deerjikists, row_indexes_by_key, tag
|
||||
conflicts = deerjikists.filter { |deerjikist| deerjikist.tag_id != tag.id }
|
||||
return if conflicts.empty?
|
||||
|
||||
tag_names_by_id = Tag
|
||||
.joins(:tag_name)
|
||||
.where(id: conflicts.map(&:tag_id).uniq)
|
||||
.pluck('tags.id', 'tag_names.name')
|
||||
.to_h
|
||||
fields = { }
|
||||
|
||||
conflicts.each do |deerjikist|
|
||||
message = "この情報は既に「#{ tag_names_by_id[deerjikist.tag_id] }」に紐づいてゐます."
|
||||
row_indexes_by_key[[deerjikist.platform, deerjikist.code]].each do |index|
|
||||
field = :"deerjikists.#{ index }.code"
|
||||
fields[field] ||= []
|
||||
fields[field] << message
|
||||
end
|
||||
end
|
||||
|
||||
render_validation_error fields:
|
||||
end
|
||||
|
||||
def render_deerjikist_platform_invalid index
|
||||
render_validation_error fields: {
|
||||
:"deerjikists.#{ index }.platform" => ['値が不正です.'],
|
||||
}
|
||||
end
|
||||
|
||||
def rows_by_key rows
|
||||
rows.each_with_object({ }) do |row, result|
|
||||
key = [row[:platform], row[:code]]
|
||||
result[key] ||= []
|
||||
result[key] << row[:index]
|
||||
end
|
||||
end
|
||||
|
||||
def render_deerjikist_form_record_invalid deerjikist, index
|
||||
fields = { }
|
||||
|
||||
|
||||
@@ -55,11 +55,10 @@ 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 :active_tags, -> { where(tags: { discarded_at: nil }) },
|
||||
through: :post_tags,
|
||||
source: :tag
|
||||
|
||||
has_many :user_post_views, dependent: :delete_all
|
||||
has_many :post_similarities, dependent: :delete_all
|
||||
@@ -123,7 +122,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 +148,6 @@ class Post < ApplicationRecord
|
||||
|
||||
def snapshot_tags_json
|
||||
post_tags
|
||||
.kept
|
||||
.joins(tag: :tag_name)
|
||||
.includes(:sections, tag: :tag_name)
|
||||
.order('tags.id')
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
class DeleteInactiveRecordsFromPostTags < ActiveRecord::Migration[8.0]
|
||||
def up
|
||||
execute <<~SQL
|
||||
DELETE
|
||||
FROM
|
||||
post_tags
|
||||
WHERE
|
||||
discarded_at IS NOT NULL
|
||||
SQL
|
||||
|
||||
remove_index :post_tags, [:tag_id, :discarded_at]
|
||||
remove_index :post_tags, [:post_id, :discarded_at]
|
||||
remove_index :post_tags, name: 'idx_post_tags_active_unique'
|
||||
remove_index :post_tags, :discarded_at
|
||||
|
||||
remove_foreign_key :post_tags, column: :deleted_user_id
|
||||
remove_index :post_tags, :deleted_user_id
|
||||
|
||||
remove_column :post_tags, :active_unique_key
|
||||
remove_column :post_tags, :is_active
|
||||
remove_column :post_tags, :discarded_at
|
||||
remove_column :post_tags, :deleted_user_id
|
||||
remove_column :post_tags, :updated_at
|
||||
|
||||
execute <<~SQL
|
||||
ALTER TABLE
|
||||
post_tags
|
||||
MODIFY COLUMN
|
||||
id BIGINT NOT NULL
|
||||
SQL
|
||||
|
||||
execute <<~SQL
|
||||
ALTER TABLE
|
||||
post_tags
|
||||
DROP PRIMARY KEY
|
||||
SQL
|
||||
|
||||
remove_column :post_tags, :id
|
||||
|
||||
execute <<~SQL
|
||||
ALTER TABLE
|
||||
post_tags
|
||||
ADD PRIMARY KEY
|
||||
(post_id, tag_id)
|
||||
SQL
|
||||
|
||||
remove_index :post_tags, :post_id
|
||||
end
|
||||
|
||||
def down
|
||||
raise ActiveRecord::IrreversibleMigration, '戻せません.'
|
||||
end
|
||||
end
|
||||
生成ファイル
+15
-27
@@ -10,7 +10,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema[8.0].define(version: 2026_07_27_123600) do
|
||||
ActiveRecord::Schema[8.0].define(version: 2026_09_21_000000) do
|
||||
create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.string "name", null: false
|
||||
t.string "record_type", null: false
|
||||
@@ -311,23 +311,12 @@ ActiveRecord::Schema[8.0].define(version: 2026_07_27_123600) do
|
||||
t.check_constraint "`begin_ms` >= 0", name: "chk_post_tag_sections_begin_ms_natural"
|
||||
end
|
||||
|
||||
create_table "post_tags", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
create_table "post_tags", primary_key: ["post_id", "tag_id"], charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.bigint "post_id", null: false
|
||||
t.bigint "tag_id", null: false
|
||||
t.bigint "created_user_id"
|
||||
t.bigint "deleted_user_id"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.datetime "discarded_at"
|
||||
t.virtual "is_active", type: :boolean, as: "(`discarded_at` is null)", stored: true
|
||||
t.virtual "active_unique_key", type: :string, as: "(case when (`discarded_at` is null) then concat(`post_id`,_utf8mb4':',`tag_id`) else NULL end)", stored: true
|
||||
t.index ["active_unique_key"], name: "idx_post_tags_active_unique", unique: true
|
||||
t.index ["created_user_id"], name: "index_post_tags_on_created_user_id"
|
||||
t.index ["deleted_user_id"], name: "index_post_tags_on_deleted_user_id"
|
||||
t.index ["discarded_at"], name: "index_post_tags_on_discarded_at"
|
||||
t.index ["post_id", "discarded_at"], name: "index_post_tags_on_post_id_and_discarded_at"
|
||||
t.index ["post_id"], name: "index_post_tags_on_post_id"
|
||||
t.index ["tag_id", "discarded_at"], name: "index_post_tags_on_tag_id_and_discarded_at"
|
||||
t.index ["tag_id"], name: "index_post_tags_on_tag_id"
|
||||
end
|
||||
|
||||
@@ -395,19 +384,6 @@ ActiveRecord::Schema[8.0].define(version: 2026_07_27_123600) do
|
||||
t.index ["user_id"], name: "index_settings_on_user_id", unique: true
|
||||
end
|
||||
|
||||
create_table "wiki_assets", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.bigint "wiki_page_id", null: false
|
||||
t.integer "no", null: false
|
||||
t.string "alt_text"
|
||||
t.binary "sha256", limit: 32, null: false
|
||||
t.bigint "created_by_user_id", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["created_by_user_id"], name: "index_wiki_assets_on_created_by_user_id"
|
||||
t.index ["wiki_page_id", "no"], name: "index_wiki_assets_on_wiki_page_id_and_no", unique: true
|
||||
t.index ["wiki_page_id", "sha256"], name: "index_wiki_assets_on_wiki_page_id_and_sha256", unique: true
|
||||
end
|
||||
|
||||
create_table "tag_implications", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.bigint "tag_id", null: false
|
||||
t.bigint "parent_tag_id", null: false
|
||||
@@ -616,6 +592,19 @@ ActiveRecord::Schema[8.0].define(version: 2026_07_27_123600) do
|
||||
t.index ["banned_at"], name: "index_users_on_banned_at"
|
||||
end
|
||||
|
||||
create_table "wiki_assets", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.bigint "wiki_page_id", null: false
|
||||
t.integer "no", null: false
|
||||
t.string "alt_text"
|
||||
t.binary "sha256", limit: 32, null: false
|
||||
t.bigint "created_by_user_id", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["created_by_user_id"], name: "index_wiki_assets_on_created_by_user_id"
|
||||
t.index ["wiki_page_id", "no"], name: "index_wiki_assets_on_wiki_page_id_and_no", unique: true
|
||||
t.index ["wiki_page_id", "sha256"], name: "index_wiki_assets_on_wiki_page_id_and_sha256", unique: true
|
||||
end
|
||||
|
||||
create_table "wiki_lines", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.string "sha256", limit: 64, null: false
|
||||
t.text "body", null: false
|
||||
@@ -729,7 +718,6 @@ ActiveRecord::Schema[8.0].define(version: 2026_07_27_123600) do
|
||||
add_foreign_key "post_tags", "posts"
|
||||
add_foreign_key "post_tags", "tags"
|
||||
add_foreign_key "post_tags", "users", column: "created_user_id"
|
||||
add_foreign_key "post_tags", "users", column: "deleted_user_id"
|
||||
add_foreign_key "post_versions", "posts"
|
||||
add_foreign_key "post_versions", "users", column: "created_by_user_id"
|
||||
add_foreign_key "posts", "users", column: "uploaded_user_id"
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -226,6 +226,14 @@ RSpec.describe 'Tags deerjikists API', type: :request do
|
||||
[platform2, code2],
|
||||
)
|
||||
end
|
||||
|
||||
it 'locks the tag before replacing the complete list' do
|
||||
expect_any_instance_of(Tag).to receive(:lock!).and_call_original
|
||||
|
||||
do_request
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when tag already has deerjikists' do
|
||||
@@ -299,6 +307,81 @@ RSpec.describe 'Tags deerjikists API', type: :request do
|
||||
end
|
||||
end
|
||||
|
||||
context 'when platform is outside the enum' do
|
||||
let(:payload) do
|
||||
[
|
||||
{ platform: 'invalid', code: code1 },
|
||||
]
|
||||
end
|
||||
|
||||
it 'returns 422 with an indexed platform error without changing the list' do
|
||||
Deerjikist.create!(platform: platform1, code: code1, tag: tag)
|
||||
|
||||
expect {
|
||||
do_request
|
||||
}.not_to change { Deerjikist.where(tag: tag).map { |d| [d.platform, d.code] } }
|
||||
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
expect(json.fetch('errors')).to include(
|
||||
'deerjikists.0.platform' => [be_present],
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when a requested deerjikist belongs to another tag' do
|
||||
let!(:other_tag) { create(:tag, category: :deerjikist) }
|
||||
let!(:owned_deerjikist) do
|
||||
Deerjikist.create!(platform: platform1, code: code1, tag: tag)
|
||||
end
|
||||
let!(:conflicting_deerjikist) do
|
||||
Deerjikist.create!(platform: platform2, code: code2, tag: other_tag)
|
||||
end
|
||||
let(:payload) do
|
||||
[
|
||||
{ platform: 'nico', code: 'new-code' },
|
||||
{ platform: platform2, code: code2 },
|
||||
]
|
||||
end
|
||||
|
||||
before do
|
||||
other_tag.tag_name.update!(name: 'existing-deerjikist')
|
||||
end
|
||||
|
||||
it 'returns an indexed 422 error and rolls back the complete replacement' do
|
||||
expect {
|
||||
do_request
|
||||
}.not_to change { Deerjikist.order(:platform, :code).pluck(:platform, :code, :tag_id) }
|
||||
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
expect(json.fetch('errors')).to include(
|
||||
'deerjikists.1.code' => [include('existing-deerjikist')],
|
||||
)
|
||||
expect(owned_deerjikist.reload.tag_id).to eq(tag.id)
|
||||
expect(conflicting_deerjikist.reload.tag_id).to eq(other_tag.id)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when a requested deerjikist already belongs to the same tag' do
|
||||
let!(:existing_deerjikist) do
|
||||
Deerjikist.create!(platform: platform1, code: code1, tag: tag)
|
||||
end
|
||||
let(:payload) do
|
||||
[
|
||||
{ platform: platform1, code: code1 },
|
||||
]
|
||||
end
|
||||
|
||||
it 'keeps the existing row' do
|
||||
expect {
|
||||
do_request
|
||||
}.not_to change { existing_deerjikist.reload.created_at }
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(Deerjikist.where(tag: tag).pluck(:platform, :code))
|
||||
.to eq([[platform1, code1]])
|
||||
end
|
||||
end
|
||||
|
||||
context 'when youtube code is handle' do
|
||||
let(:channel_id) { 'UCabcdefghijklmnopqrstuv' }
|
||||
let(:payload) do
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする