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
code = request.headers['X-Transfer-Code'] || request.headers['HTTP_X_TRANSFER_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}")
@current_user = User.find_by(inheritance_code: code)
end
end
+28 -22
View File
@@ -62,19 +62,12 @@ class PostsController < ApplicationController
url = params[:url]
thumbnail = params[:thumbnail]
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.thumbnail.attach(thumbnail)
if post.save
post.resized_thumbnail!
# TODO: 接頭辞に応じて category 変へる
post.tags = tag_names.map { |name| Tag.find_or_initialize_by(name:) { |tag|
tag.category = 'general'
} }
post.tags = normalise_tags(tags_names)
render json: post.as_json(include: { tags: { only: [:id, :name, :category] } }),
status: :created
else
@@ -103,22 +96,12 @@ class PostsController < ApplicationController
title = params[:title]
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)
tags = post.tags.where(category: 'nico').to_a
tag_names.each do |name|
# TODO: 接頭辞に応じて category 変へる
tags << Tag.find_or_initialize_by(name:) { |tag|
tag.category = 'general'
}
end
tags = post.tags.where(category: 'nico').to_a + normalise_tags(tag_names)
if post.update(title:, tags:)
render({ json: (post
.as_json(include: { tags: { only: [:id, :name, :category] } })),
status: :created })
render json: post.as_json(include: { tags: { only: [:id, :name, :category] } }),
status: :created
else
render json: post.errors, status: :unprocessable_entity
end
@@ -130,8 +113,16 @@ class PostsController < ApplicationController
private
CATEGORY_PREFIXES = {
'gen:' => 'general',
'djk:' => 'deerjikist',
'meme:' => 'meme',
'chr:' => 'character',
'mtr:' => 'material',
'meta:' => 'meta' }.freeze
def filtered_posts
tag_names = params[:tags]&.split(?\ )
tag_names = params[:tags]&.split(' ')
match_type = params[:match]
tag_names.present? ? filter_posts_by_tags(tag_names, match_type) : Post.all
end
@@ -147,4 +138,19 @@ class PostsController < ApplicationController
end
posts.distinct
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
+12
View File
@@ -19,6 +19,18 @@ class Tag < ApplicationRecord
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
def nico_tag_name_must_start_with_nico
+5 -13
View File
@@ -45,20 +45,12 @@ namespace :nico do
if current_tags != new_tags
post.tags.destroy(post.tags.where(name: current_tags))
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
name = 'bot操作'
post.tags.destroy(post.tags.find_by(name:) || [])
post.tags << Tag.find_or_initialize_by(name:) { |tag|
tag.category = 'meta'
}
post.tags << Tag.tagme if post.tags.size < 20 && post.tags.none?(Tag.tagme)
post.tags << Tag.bot if post.tags.none?(Tag.bot)
end
end
end