This commit is contained in:
2025-07-09 23:58:03 +09:00
parent 9be4bb1532
commit b83fc6369a
4 changed files with 46 additions and 38 deletions
@@ -9,8 +9,6 @@ class ApplicationController < ActionController::API
def authenticate_user def authenticate_user
code = request.headers['X-Transfer-Code'] || request.headers['HTTP_X_TRANSFER_CODE'] code = request.headers['X-Transfer-Code'] || request.headers['HTTP_X_TRANSFER_CODE']
@current_user = User.find_by inheritance_code: code @current_user = User.find_by(inheritance_code: code)
Rails.logger.info("X-Transfer-Code: #{request.headers['X-Transfer-Code']}")
Rails.logger.info("current_user: #{@current_user&.id}")
end end
end end
+28 -22
View File
@@ -62,19 +62,12 @@ class PostsController < ApplicationController
url = params[:url] url = params[:url]
thumbnail = params[:thumbnail] thumbnail = params[:thumbnail]
tag_names = params[:tags].to_s.split(' ') tag_names = params[:tags].to_s.split(' ')
if tag_names.size < 20 && tag_names.none?('タグ希望')
tag_names << 'タグ希望'
end
post = Post.new(title:, url:, thumbnail_base: '', uploaded_user: current_user) post = Post.new(title:, url:, thumbnail_base: '', uploaded_user: current_user)
post.thumbnail.attach(thumbnail) post.thumbnail.attach(thumbnail)
if post.save if post.save
post.resized_thumbnail! post.resized_thumbnail!
# TODO: 接頭辞に応じて category 変へる post.tags = normalise_tags(tags_names)
post.tags = tag_names.map { |name| Tag.find_or_initialize_by(name:) { |tag|
tag.category = 'general'
} }
render json: post.as_json(include: { tags: { only: [:id, :name, :category] } }), render json: post.as_json(include: { tags: { only: [:id, :name, :category] } }),
status: :created status: :created
else else
@@ -103,22 +96,12 @@ class PostsController < ApplicationController
title = params[:title] title = params[:title]
tag_names = params[:tags].to_s.split(' ') tag_names = params[:tags].to_s.split(' ')
if tag_names.size < 20 && tag_names.none?('タグ希望')
tag_names << 'タグ希望'
end
post = Post.find(params[:id].to_i) post = Post.find(params[:id].to_i)
tags = post.tags.where(category: 'nico').to_a tags = post.tags.where(category: 'nico').to_a + normalise_tags(tag_names)
tag_names.each do |name|
# TODO: 接頭辞に応じて category 変へる
tags << Tag.find_or_initialize_by(name:) { |tag|
tag.category = 'general'
}
end
if post.update(title:, tags:) if post.update(title:, tags:)
render({ json: (post render json: post.as_json(include: { tags: { only: [:id, :name, :category] } }),
.as_json(include: { tags: { only: [:id, :name, :category] } })), status: :created
status: :created })
else else
render json: post.errors, status: :unprocessable_entity render json: post.errors, status: :unprocessable_entity
end end
@@ -130,8 +113,16 @@ class PostsController < ApplicationController
private private
CATEGORY_PREFIXES = {
'gen:' => 'general',
'djk:' => 'deerjikist',
'meme:' => 'meme',
'chr:' => 'character',
'mtr:' => 'material',
'meta:' => 'meta' }.freeze
def filtered_posts def filtered_posts
tag_names = params[:tags]&.split(?\ ) tag_names = params[:tags]&.split(' ')
match_type = params[:match] match_type = params[:match]
tag_names.present? ? filter_posts_by_tags(tag_names, match_type) : Post.all tag_names.present? ? filter_posts_by_tags(tag_names, match_type) : Post.all
end end
@@ -147,4 +138,19 @@ class PostsController < ApplicationController
end end
posts.distinct posts.distinct
end end
def normalise_tags tag_names
tags = tag_names.map do |name|
pf, cat = CATEGORY_PREFIXES.find { |p, _| name.start_with?(p) } || ['', nil]
name.delete_prefix!(pf)
Tag.find_or_initialize_by(name:).tap do |tag|
if cat && tag.category != cat
tag.category = cat
tag.save!
end
end
end
tags << Tag.tagme if tags.size < 20 && tags.none?(Tag.tagme)
tags.uniq
end
end end
+12
View File
@@ -19,6 +19,18 @@ class Tag < ApplicationRecord
scope :nico_tags, -> { where category: :nico } scope :nico_tags, -> { where category: :nico }
def self.tagme
@tagme ||= Tag.find_or_initialize_by(name: 'タグ希望') do |tag|
tag.category = 'meta'
end
end
def self.bot
@bot ||= Tag.find_or_initialize_by(name: 'bot操作') do |tag|
tag.category = 'meta'
end
end
private private
def nico_tag_name_must_start_with_nico def nico_tag_name_must_start_with_nico
+4 -12
View File
@@ -45,20 +45,12 @@ namespace :nico do
if current_tags != new_tags if current_tags != new_tags
post.tags.destroy(post.tags.where(name: current_tags)) post.tags.destroy(post.tags.where(name: current_tags))
new_tags.each do |name| new_tags.each do |name|
post.tags << Tag.find_or_initialize_by(name:, category: 'nico') post.tags << Tag.find_or_initialize_by(name:) do |tag|
tag.category = 'nico'
end end
if post.tags.size < 20
name = 'タグ希望'
post.tags.destroy(post.tags.find_by(name:) || [])
post.tags << Tag.find_or_initialize_by(name:) { |tag|
tag.category = 'meta'
}
end end
name = 'bot操作' post.tags << Tag.tagme if post.tags.size < 20 && post.tags.none?(Tag.tagme)
post.tags.destroy(post.tags.find_by(name:) || []) post.tags << Tag.bot if post.tags.none?(Tag.bot)
post.tags << Tag.find_or_initialize_by(name:) { |tag|
tag.category = 'meta'
}
end end
end end
end end