Merge branch 'main' into feature/332
このコミットが含まれているのは:
@@ -140,10 +140,11 @@ class PostsController < ApplicationController
|
||||
original_created_from = params[:original_created_from]
|
||||
original_created_before = params[:original_created_before]
|
||||
parent_post_ids = parse_parent_post_ids
|
||||
resized_thumbnail = thumbnail.present? ? Post.resized_thumbnail_attachment(thumbnail) : nil
|
||||
|
||||
post = Post.new(title:, url:, thumbnail_base: nil, uploaded_user: current_user,
|
||||
original_created_from:, original_created_before:)
|
||||
post.thumbnail.attach(thumbnail) if thumbnail.present?
|
||||
post.thumbnail.attach(resized_thumbnail) if resized_thumbnail
|
||||
|
||||
ApplicationRecord.transaction do
|
||||
post.save!
|
||||
@@ -156,8 +157,6 @@ class PostsController < ApplicationController
|
||||
|
||||
sync_parent_posts!(post, parent_post_ids)
|
||||
|
||||
post.resized_thumbnail!
|
||||
|
||||
PostVersionRecorder.record!(post:, event_type: :create, created_by_user: current_user)
|
||||
end
|
||||
|
||||
@@ -167,6 +166,8 @@ class PostsController < ApplicationController
|
||||
render_validation_error fields: { tags: 'ニコニコ・タグは直接指定できません.' }
|
||||
rescue Tag::DeprecatedTagNormalisationError
|
||||
render_unprocessable_entity '廃止済みタグは付与できません.', field: :tags
|
||||
rescue MiniMagick::Error
|
||||
render_validation_error fields: { thumbnail: ['サムネイル画像の変換に失敗しました.'] }
|
||||
rescue ArgumentError => e
|
||||
render_validation_error fields: { parent_post_ids: [e.message] }
|
||||
rescue ActiveRecord::RecordInvalid => e
|
||||
|
||||
@@ -245,10 +245,7 @@ class TagsController < ApplicationController
|
||||
return render_unprocessable_entity('カテゴリは必須です.', field: :category) if category.blank?
|
||||
return render_unprocessable_entity '廃止状態は必須です.', field: :deprecated unless params.key?(:deprecated)
|
||||
|
||||
if (name != tag.name &&
|
||||
tag.in?([Tag.tagme, Tag.bot, Tag.no_deerjikist, Tag.video, Tag.niconico]))
|
||||
return render_unprocessable_entity 'システム・タグの名称は変更できません.', field: :name
|
||||
end
|
||||
return unless validate_tag_rename(tag, name)
|
||||
|
||||
alias_names = params[:aliases].to_s.split.uniq
|
||||
parent_names = params[:parent_tags].to_s.split.uniq
|
||||
@@ -274,7 +271,7 @@ class TagsController < ApplicationController
|
||||
else
|
||||
tag.update!(category:, deprecated_at: deprecated ? Time.current : nil)
|
||||
end
|
||||
tag.tag_name.update!(name:)
|
||||
rename_tag_name!(tag, name) if name_changed
|
||||
|
||||
alias_names << old_name if name_changed
|
||||
alias_names.delete(name)
|
||||
@@ -310,6 +307,8 @@ class TagsController < ApplicationController
|
||||
return render_unprocessable_entity 'ニコタグは廃止できません.', field: :deprecated
|
||||
end
|
||||
|
||||
return unless validate_tag_rename(tag, name)
|
||||
|
||||
if tag.nico? || (category.present? && category == 'nico')
|
||||
return render_unprocessable_entity 'ニコタグは変更できません.', field: :category
|
||||
end
|
||||
@@ -321,7 +320,7 @@ class TagsController < ApplicationController
|
||||
name_changed = name.present? && name != old_name
|
||||
wiki_page = tag.tag_name.wiki_page if name_changed
|
||||
|
||||
tag.tag_name.update!(name:) if name.present?
|
||||
rename_tag_name!(tag, name) if name_changed
|
||||
tag.update!(category:) if category.present?
|
||||
if deprecated_given && tag.deprecated? != deprecated
|
||||
tag.update!(deprecated_at: deprecated ? Time.current : nil)
|
||||
@@ -535,6 +534,67 @@ class TagsController < ApplicationController
|
||||
created_by_user:)
|
||||
end
|
||||
|
||||
def validate_tag_rename tag, name
|
||||
return true if name.blank? || name == tag.name
|
||||
|
||||
if tag.in?([Tag.tagme, Tag.bot, Tag.no_deerjikist, Tag.video, Tag.niconico])
|
||||
render_unprocessable_entity 'システム・タグの名称は変更できません.', field: :name
|
||||
return false
|
||||
end
|
||||
|
||||
target_tag_name = TagName.with_discarded.find_by(name:)
|
||||
return true if target_tag_name.nil?
|
||||
return true if target_tag_name.canonical_id?
|
||||
|
||||
render_unprocessable_entity 'その名前は既に使はれてゐます.', field: :name
|
||||
false
|
||||
end
|
||||
|
||||
def rename_tag_name! tag, name
|
||||
return if name == tag.name
|
||||
|
||||
current_tag_name = tag.tag_name
|
||||
target_tag_name = TagName.with_discarded.find_by(name:)
|
||||
|
||||
if target_tag_name.nil?
|
||||
current_tag_name.update!(name:)
|
||||
return
|
||||
end
|
||||
|
||||
promote_tag_alias!(
|
||||
tag,
|
||||
current_tag_name:,
|
||||
promoted_tag_name: target_tag_name)
|
||||
end
|
||||
|
||||
def promote_tag_alias! tag, current_tag_name:, promoted_tag_name:
|
||||
old_owner_tag = promoted_tag_name.canonical&.tag
|
||||
|
||||
if old_owner_tag && old_owner_tag != tag
|
||||
TagVersioning.ensure_snapshot!(old_owner_tag, created_by_user: current_user)
|
||||
end
|
||||
|
||||
promoted_tag_name.undiscard! if promoted_tag_name.discarded?
|
||||
promoted_tag_name.update!(canonical: nil)
|
||||
|
||||
TagName.with_discarded
|
||||
.where(canonical_id: current_tag_name.id)
|
||||
.where.not(id: promoted_tag_name.id)
|
||||
.find_each do |alias_tag_name|
|
||||
alias_tag_name.update!(canonical: promoted_tag_name)
|
||||
end
|
||||
|
||||
current_tag_name.wiki_page&.update!(tag_name: promoted_tag_name)
|
||||
tag.update!(tag_name: promoted_tag_name)
|
||||
current_tag_name.association(:wiki_page).reset
|
||||
current_tag_name.association(:tag).reset
|
||||
current_tag_name.reload.update!(canonical: promoted_tag_name)
|
||||
|
||||
return unless old_owner_tag && old_owner_tag != tag
|
||||
|
||||
record_tag_version!(old_owner_tag.reload, event_type: :update, created_by_user: current_user)
|
||||
end
|
||||
|
||||
def update_aliases! tag, alias_names
|
||||
alias_names = alias_names.uniq
|
||||
|
||||
|
||||
@@ -1,5 +1,19 @@
|
||||
class Post < ApplicationRecord
|
||||
require 'mini_magick'
|
||||
require 'stringio'
|
||||
|
||||
def self.resized_thumbnail_attachment(upload)
|
||||
upload.rewind
|
||||
image = MiniMagick::Image.read(upload.read)
|
||||
image.resize '180x180'
|
||||
image.format 'jpg'
|
||||
|
||||
{ io: StringIO.new(image.to_blob),
|
||||
filename: 'resized_thumbnail.jpg',
|
||||
content_type: 'image/jpeg' }
|
||||
ensure
|
||||
upload.rewind
|
||||
end
|
||||
|
||||
belongs_to :uploaded_user, class_name: 'User', optional: true
|
||||
|
||||
@@ -87,12 +101,7 @@ class Post < ApplicationRecord
|
||||
def resized_thumbnail!
|
||||
return unless thumbnail.attached?
|
||||
|
||||
image = MiniMagick::Image.read(thumbnail.download)
|
||||
image.resize '180x180'
|
||||
thumbnail.purge
|
||||
thumbnail.attach(io: File.open(image.path),
|
||||
filename: 'resized_thumbnail.jpg',
|
||||
content_type: 'image/jpeg')
|
||||
thumbnail.attach(self.class.resized_thumbnail_attachment(StringIO.new(thumbnail.download)))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
新しい課題から参照
ユーザをブロックする