This commit is contained in:
2025-07-13 17:18:02 +09:00
parent 0c46cf28db
commit 9e0eb3b3c5
9 changed files with 173 additions and 92 deletions
@@ -3,7 +3,7 @@ class NicoTagsController < ApplicationController
limit = (params[:limit] || 20).to_i
cursor = params[:cursor].presence
q = Tag.nico_tags.includes(:linked_tags)
q = Tag.nico_tags.includes(:linked_tags).order(updated_at: :desc)
q = q.where('tags.updated_at < ?', Time.iso8601(cursor)) if cursor
tags = q.limit(limit + 1)
@@ -19,6 +19,23 @@ class NicoTagsController < ApplicationController
}, next_cursor: }
end
def upload
def update
return head :unauthorized unless current_user
return head :forbidden unless current_user.member?
id = params[:id].to_i
tag = Tag.find(id)
return head :bad_request if tag.category != 'nico'
linked_tag_names = params[:tags].to_s.split(' ')
linked_tags = Tag.normalise_tags(linked_tag_names, with_tagme: false)
return head :bad_request if linked_tags.filter { |t| t.category == 'nico' }.present?
tag.linked_tags = linked_tags
tag.updated_at = Time.now
tag.update!
render json: tag.linked_tags, status: :ok
end
end
+3 -26
View File
@@ -63,7 +63,7 @@ class PostsController < ApplicationController
post.thumbnail.attach(thumbnail)
if post.save
post.resized_thumbnail!
post.tags = normalise_tags(tags_names)
post.tags = Tag.normalise_tags(tags_names)
render json: post.as_json(include: { tags: { only: [:id, :name, :category] } }),
status: :created
else
@@ -94,10 +94,10 @@ class PostsController < ApplicationController
tag_names = params[:tags].to_s.split(' ')
post = Post.find(params[:id].to_i)
tags = post.tags.where(category: 'nico').to_a + normalise_tags(tag_names)
tags = post.tags.where(category: 'nico').to_a + Tag.normalise_tags(tag_names)
if post.update(title:, tags:)
render json: post.as_json(include: { tags: { only: [:id, :name, :category] } }),
status: :created
status: :ok
else
render json: post.errors, status: :unprocessable_entity
end
@@ -109,14 +109,6 @@ 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(' ')
match_type = params[:match]
@@ -134,19 +126,4 @@ 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
+25 -2
View File
@@ -26,6 +26,14 @@ class Tag < ApplicationRecord
scope :nico_tags, -> { where(category: :nico) }
CATEGORY_PREFIXES = {
'gen:' => 'general',
'djk:' => 'deerjikist',
'meme:' => 'meme',
'chr:' => 'character',
'mtr:' => 'material',
'meta:' => 'meta' }.freeze
def self.tagme
@tagme ||= Tag.find_or_initialize_by(name: 'タグ希望') do |tag|
tag.category = 'meta'
@@ -38,11 +46,26 @@ class Tag < ApplicationRecord
end
end
def self.normalise_tags tag_names, with_tagme: true
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 with_tagme && tags.size < 20 && tags.none?(Tag.tagme)
tags.uniq
end
private
def nico_tag_name_must_start_with_nico
if ((category == 'nico' && name&.[](0, 5) != 'nico:') ||
(category != 'nico' && name&.[](0, 5) == 'nico:'))
if ((category == 'nico' && !(name.start_with?('nico:'))) ||
(category != 'nico' && name.start_with?('nico:')))
errors.add :name, 'ニコニコ・タグの命名規則に反してゐます.'
end
end