|
|
|
@@ -123,6 +123,7 @@ class PostsController < ApplicationController |
|
|
|
tag_names = params[:tags].to_s.split |
|
|
|
original_created_from = params[:original_created_from] |
|
|
|
original_created_before = params[:original_created_before] |
|
|
|
parent_post_ids = parse_parent_post_ids |
|
|
|
|
|
|
|
post = Post.new(title:, url:, thumbnail_base: nil, uploaded_user: current_user, |
|
|
|
original_created_from:, original_created_before:) |
|
|
|
@@ -130,17 +131,24 @@ class PostsController < ApplicationController |
|
|
|
|
|
|
|
ApplicationRecord.transaction do |
|
|
|
post.save! |
|
|
|
tags = Tag.normalise_tags(tag_names) |
|
|
|
|
|
|
|
tags = Tag.normalise_tags!(tag_names) |
|
|
|
TagVersioning.record_tag_snapshots!(tags, created_by_user: current_user) |
|
|
|
|
|
|
|
tags = Tag.expand_parent_tags(tags) |
|
|
|
sync_post_tags!(post, tags) |
|
|
|
|
|
|
|
sync_parent_posts!(post, parent_post_ids) |
|
|
|
|
|
|
|
post.resized_thumbnail! |
|
|
|
|
|
|
|
PostVersionRecorder.record!(post:, event_type: :create, created_by_user: current_user) |
|
|
|
end |
|
|
|
|
|
|
|
post.reload |
|
|
|
render json: PostRepr.base(post), status: :created |
|
|
|
rescue ArgumentError => e |
|
|
|
render json: { errors: [e.message] }, status: :unprocessable_entity |
|
|
|
rescue ActiveRecord::RecordInvalid |
|
|
|
render json: { errors: post.errors.full_messages }, status: :unprocessable_entity |
|
|
|
rescue Tag::NicoTagNormalisationError |
|
|
|
@@ -169,6 +177,7 @@ class PostsController < ApplicationController |
|
|
|
tag_names = params[:tags].to_s.split |
|
|
|
original_created_from = params[:original_created_from] |
|
|
|
original_created_before = params[:original_created_before] |
|
|
|
parent_post_ids = parse_parent_post_ids |
|
|
|
|
|
|
|
post = Post.find(params[:id].to_i) |
|
|
|
|
|
|
|
@@ -177,12 +186,15 @@ class PostsController < ApplicationController |
|
|
|
|
|
|
|
post.update!(title:, original_created_from:, original_created_before:) |
|
|
|
|
|
|
|
normalised_tags = Tag.normalise_tags(tag_names, with_tagme: false) |
|
|
|
normalised_tags = Tag.normalise_tags!(tag_names, with_tagme: false) |
|
|
|
TagVersioning.record_tag_snapshots!(normalised_tags, created_by_user: current_user) |
|
|
|
|
|
|
|
tags = post.tags.nico.to_a + normalised_tags |
|
|
|
tags = Tag.expand_parent_tags(tags) |
|
|
|
sync_post_tags!(post, tags) |
|
|
|
|
|
|
|
sync_parent_posts!(post, parent_post_ids) |
|
|
|
|
|
|
|
PostVersionRecorder.record!(post:, event_type: :update, created_by_user: current_user) |
|
|
|
end |
|
|
|
|
|
|
|
@@ -190,6 +202,8 @@ class PostsController < ApplicationController |
|
|
|
json = post.as_json |
|
|
|
json['tags'] = build_tag_tree_for(post.tags) |
|
|
|
render json:, status: :ok |
|
|
|
rescue ArgumentError => e |
|
|
|
render json: { errors: [e.message] }, status: :unprocessable_entity |
|
|
|
rescue ActiveRecord::RecordInvalid |
|
|
|
render json: post.errors, status: :unprocessable_entity |
|
|
|
rescue Tag::NicoTagNormalisationError |
|
|
|
@@ -353,4 +367,41 @@ class PostsController < ApplicationController |
|
|
|
|
|
|
|
root_ids.filter_map { |id| build_node.call(id, []) } |
|
|
|
end |
|
|
|
|
|
|
|
def parse_parent_post_ids |
|
|
|
raise ArgumentError, 'parent_post_ids は必須です.' unless params.key?(:parent_post_ids) |
|
|
|
|
|
|
|
params[:parent_post_ids].to_s.split.map { |token| |
|
|
|
id = Integer(token, exception: false) |
|
|
|
raise ArgumentError, "親投稿 Id. が不正です: #{ token }" if id.nil? || id <= 0 |
|
|
|
|
|
|
|
id |
|
|
|
}.uniq |
|
|
|
end |
|
|
|
|
|
|
|
def sync_parent_posts! post, parent_post_ids |
|
|
|
if parent_post_ids.include?(post.id) |
|
|
|
post.errors.add(:base, '自分自身を親投稿にはできません.') |
|
|
|
raise ActiveRecord::RecordInvalid, post |
|
|
|
end |
|
|
|
|
|
|
|
existing_ids = Post.where(id: parent_post_ids).pluck(:id) |
|
|
|
missing_ids = parent_post_ids - existing_ids |
|
|
|
|
|
|
|
if missing_ids.present? |
|
|
|
post.errors.add(:base, "存在しない親投稿 ID があります: #{ missing_ids.join(' ') }") |
|
|
|
raise ActiveRecord::RecordInvalid, post |
|
|
|
end |
|
|
|
|
|
|
|
current_ids = post.parent_posts.pluck(:id) |
|
|
|
|
|
|
|
ids_to_add = parent_post_ids - current_ids |
|
|
|
ids_to_remove = current_ids - parent_post_ids |
|
|
|
|
|
|
|
PostImplication.where(post_id: post.id, parent_post_id: ids_to_remove).delete_all |
|
|
|
|
|
|
|
ids_to_add.each do |parent_post_id| |
|
|
|
PostImplication.create_or_find_by!(post_id: post.id, parent_post_id:) |
|
|
|
end |
|
|
|
end |
|
|
|
end |