#63 #63 #63 #63 Co-authored-by: miteruzo <miteruzo@naver.com> Reviewed-on: #341
This commit was merged in pull request #341.
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
require 'net/http'
|
||||
require 'uri'
|
||||
|
||||
|
||||
class TagsController < ApplicationController
|
||||
def index
|
||||
post_id = params[:post]
|
||||
@@ -182,7 +186,8 @@ class TagsController < ApplicationController
|
||||
.find_by(id: params[:id])
|
||||
return head :not_found unless tag
|
||||
|
||||
render json: DeerjikistRepr.many(tag.deerjikists)
|
||||
render json: { tag: TagRepr.base(tag),
|
||||
deerjikists: DeerjikistRepr.many(tag.deerjikists) }
|
||||
end
|
||||
|
||||
def deerjikists_by_name
|
||||
@@ -194,7 +199,31 @@ class TagsController < ApplicationController
|
||||
.find_by(tag_names: { name: })
|
||||
return head :not_found unless tag
|
||||
|
||||
render json: DeerjikistRepr.many(tag.deerjikists)
|
||||
render json: { tag: TagRepr.base(tag),
|
||||
deerjikists: DeerjikistRepr.many(tag.deerjikists) }
|
||||
end
|
||||
|
||||
def update_deerjikists
|
||||
return head :unauthorized unless current_user
|
||||
return head :forbidden unless current_user.gte_member?
|
||||
|
||||
tag = Tag.joins(:tag_name)
|
||||
.includes(:tag_name, tag_name: :wiki_page)
|
||||
.find_by(id: params[:id])
|
||||
return head :not_found unless tag
|
||||
|
||||
ApplicationRecord.transaction do
|
||||
tag.deerjikists = []
|
||||
params[:_json].each do
|
||||
platform = _1[:platform]
|
||||
code = normalise_deerjikist_code(platform, _1[:code])
|
||||
deerjikist = Deerjikist.find_or_initialize_by(platform:, code:)
|
||||
deerjikist.tag = tag
|
||||
deerjikist.save!
|
||||
end
|
||||
end
|
||||
|
||||
render json: DeerjikistRepr.many(tag.reload.deerjikists)
|
||||
end
|
||||
|
||||
def materials_by_name
|
||||
@@ -391,4 +420,21 @@ class TagsController < ApplicationController
|
||||
TagImplication.create!(tag:, parent_tag:)
|
||||
end
|
||||
end
|
||||
|
||||
def normalise_deerjikist_code platform, code
|
||||
return code if platform != 'youtube' || code[0] != '@'
|
||||
|
||||
url = "https://www.youtube.com/#{ code }"
|
||||
|
||||
html = Net::HTTP.get(URI(url))
|
||||
|
||||
canonical = html[
|
||||
/<link rel="canonical" href="https:\/\/www\.youtube\.com\/channel\/(UC[a-zA-Z0-9_-]{22})"/,
|
||||
1]
|
||||
return canonical if canonical
|
||||
|
||||
html[/"channelId":"(UC[a-zA-Z0-9_-]{22})"/, 1] || html[/\bUC[a-zA-Z0-9_-]{22}\b/]
|
||||
rescue
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
@@ -79,6 +79,8 @@ class Tag < ApplicationRecord
|
||||
|
||||
def material_id = materials.first&.id
|
||||
|
||||
def has_deerjikists = deerjikists.present?
|
||||
|
||||
def self.tagme = find_or_create_by_tag_name!('タグ希望', category: :meta)
|
||||
def self.bot = find_or_create_by_tag_name!('bot操作', category: :meta)
|
||||
def self.no_deerjikist = find_or_create_by_tag_name!('ニジラー情報不詳', category: :meta)
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
module TagRepr
|
||||
BASE = { only: [:id, :category, :post_count, :created_at, :updated_at],
|
||||
methods: [:name, :has_wiki, :material_id] }.freeze
|
||||
methods: [:name, :has_wiki, :material_id, :has_deerjikists] }.freeze
|
||||
|
||||
module_function
|
||||
|
||||
|
||||
Reference in New Issue
Block a user