This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user