Merge remote-tracking branch 'origin/main' into feature/351
このコミットが含まれているのは:
@@ -1,4 +1,7 @@
|
||||
class ApplicationController < ActionController::API
|
||||
rescue_from ActiveRecord::RecordInvalid, with: :render_record_invalid
|
||||
rescue_from ActiveRecord::RecordNotUnique, with: :render_record_not_unique
|
||||
|
||||
before_action :reject_banned_ip_address!
|
||||
before_action :authenticate_user
|
||||
before_action :reject_banned_user!
|
||||
@@ -25,6 +28,27 @@ class ApplicationController < ActionController::API
|
||||
end
|
||||
end
|
||||
|
||||
def render_bad_request message = 'リクエストが不正です.'
|
||||
render json: { type: 'bad_request',
|
||||
message:,
|
||||
errors: { },
|
||||
base_errors: [message] },
|
||||
status: :bad_request
|
||||
end
|
||||
|
||||
def render_unprocessable_entity message = '入力を確認してください.', field: nil
|
||||
render_validation_error(fields: field ? { field => [message] } : { },
|
||||
base: field ? [] : [message])
|
||||
end
|
||||
|
||||
def render_record_invalid error
|
||||
render_validation_error error.record
|
||||
end
|
||||
|
||||
def render_record_not_unique _error = nil
|
||||
render_validation_error base: ['すでに存在してゐます.']
|
||||
end
|
||||
|
||||
def reject_banned_ip_address!
|
||||
ip_address = IpAddress.find_by(ip_address: IPAddr.new(request.remote_ip).hton)
|
||||
return unless ip_address&.banned?
|
||||
@@ -37,4 +61,28 @@ class ApplicationController < ActionController::API
|
||||
|
||||
head :forbidden
|
||||
end
|
||||
|
||||
def render_validation_error record = nil, fields: { }, base: [], status: :unprocessable_entity
|
||||
errors = { }
|
||||
|
||||
if record
|
||||
record.errors.each do |error|
|
||||
errors[error.attribute] ||= []
|
||||
errors[error.attribute] << error.message
|
||||
end
|
||||
end
|
||||
|
||||
fields.each do |attr, messages|
|
||||
errors[attr.to_sym] ||= []
|
||||
errors[attr.to_sym].concat(Array(messages))
|
||||
end
|
||||
|
||||
base_errors = Array(base) + Array(errors.delete(:base))
|
||||
|
||||
render json: { type: 'validation_error',
|
||||
message: '入力内容を確認してください.',
|
||||
errors:,
|
||||
base_errors: },
|
||||
status:
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,7 +2,8 @@ class DeerjikistsController < ApplicationController
|
||||
def show
|
||||
platform = params[:platform].to_s.strip
|
||||
code = params[:code].to_s.strip
|
||||
return head :bad_request if platform.blank? || code.blank?
|
||||
return render_bad_request('platform は必須です.') if platform.blank?
|
||||
return render_bad_request('code は必須です.') if code.blank?
|
||||
|
||||
deerjikist = Deerjikist
|
||||
.joins(:tag)
|
||||
@@ -22,7 +23,9 @@ class DeerjikistsController < ApplicationController
|
||||
platform = params[:platform].to_s.strip
|
||||
code = params[:code].to_s.strip
|
||||
tag_id = params[:tag_id].to_i
|
||||
return head :bad_request if platform.blank? || code.blank? || tag_id <= 0
|
||||
return render_bad_request('platform は必須です.') if platform.blank?
|
||||
return render_bad_request('code は必須です.') if code.blank?
|
||||
return render_bad_request('tag_id が不正です.') if tag_id <= 0
|
||||
|
||||
deerjikist = Deerjikist.find_or_initialize_by(platform:, code:).tap do |d|
|
||||
d.tag_id = tag_id
|
||||
@@ -38,7 +41,8 @@ class DeerjikistsController < ApplicationController
|
||||
|
||||
platform = params[:platform].to_s.strip
|
||||
code = params[:code].to_s.strip
|
||||
return head :bad_request if platform.blank? || code.blank?
|
||||
return render_bad_request('platform は必須です.') if platform.blank?
|
||||
return render_bad_request('code は必須です.') if code.blank?
|
||||
|
||||
Deerjikist.find([platform, code]).destroy!
|
||||
|
||||
|
||||
@@ -0,0 +1,272 @@
|
||||
class GekanatorGamesController < ApplicationController
|
||||
def create
|
||||
return head :unauthorized unless current_user
|
||||
|
||||
guessed_post_id = params.require(:guessed_post_id)
|
||||
correct_post_id = params[:correct_post_id].presence
|
||||
answers = params.require(:answers).as_json
|
||||
|
||||
game = GekanatorGame.new(
|
||||
user: current_user,
|
||||
guessed_post_id:,
|
||||
correct_post_id:,
|
||||
won: correct_post_id.present? && guessed_post_id.to_i == correct_post_id.to_i,
|
||||
question_count: answers.length,
|
||||
answers:)
|
||||
|
||||
if game.invalid?
|
||||
render json: { errors: game.errors.full_messages }, status: :unprocessable_entity
|
||||
return
|
||||
end
|
||||
|
||||
learned_example_count = 0
|
||||
|
||||
ActiveRecord::Base.transaction do
|
||||
game.save!
|
||||
learned_example_count = learn_answers_from_game!(game)
|
||||
end
|
||||
|
||||
render json: {
|
||||
id: game.id,
|
||||
learned_example_count:
|
||||
}, status: :created
|
||||
rescue ActiveRecord::RecordInvalid => e
|
||||
render json: { errors: e.record.errors.full_messages }, status: :unprocessable_entity
|
||||
end
|
||||
|
||||
def extra_questions
|
||||
game = find_owned_game
|
||||
return if performed?
|
||||
|
||||
questions =
|
||||
GekanatorQuestion
|
||||
.accepted
|
||||
.includes(:gekanator_question_examples)
|
||||
.where(kind: 'post_similarity', source: 'user_suggested')
|
||||
.to_a
|
||||
|
||||
selected =
|
||||
prioritized_extra_questions(
|
||||
questions,
|
||||
post_id: game.correct_post_id,
|
||||
user: current_user,
|
||||
limit: 6)
|
||||
|
||||
render json: {
|
||||
questions: selected.map { |question| extra_question_json(question) }
|
||||
}
|
||||
end
|
||||
|
||||
def extra_question_answers
|
||||
game = find_owned_game
|
||||
return if performed?
|
||||
|
||||
answer_params = params.require(:answers)
|
||||
if !answer_params.is_a?(Array)
|
||||
return render_validation_error fields: { answers: ['配列で指定してください.'] }
|
||||
end
|
||||
|
||||
answers = answer_params.map { |answer|
|
||||
{
|
||||
question_id: answer.require(:question_id).to_i,
|
||||
answer: answer.require(:answer)
|
||||
}
|
||||
}
|
||||
questions = GekanatorQuestion.where(id: answers.map { _1[:question_id] })
|
||||
question_by_id = questions.index_by(&:id)
|
||||
if questions.length != answers.length
|
||||
return render_validation_error fields: { answers: ['質問が見つかりません.'] }
|
||||
end
|
||||
if questions.any? { |question| question.status != 'accepted' || question.kind != 'post_similarity' }
|
||||
return render_validation_error fields: { answers: ['質問が不正です.'] }
|
||||
end
|
||||
|
||||
ActiveRecord::Base.transaction do
|
||||
answers.each do |item|
|
||||
question = question_by_id[item[:question_id]]
|
||||
example =
|
||||
GekanatorQuestionExample.find_or_initialize_by(
|
||||
gekanator_question: question,
|
||||
post: game.correct_post,
|
||||
user: current_user)
|
||||
example.record_answer!(
|
||||
answer: item[:answer],
|
||||
source: 'post_game_extra',
|
||||
gekanator_game: game)
|
||||
example.save!
|
||||
end
|
||||
end
|
||||
|
||||
render json: { count: answers.length }, status: :created
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def extra_question_json question
|
||||
{
|
||||
id: question.id,
|
||||
text: question.text,
|
||||
source: question.source,
|
||||
priority_weight: question.priority_weight
|
||||
}
|
||||
end
|
||||
|
||||
def prioritized_extra_questions questions, post_id:, user:, limit:
|
||||
answered_question_ids =
|
||||
GekanatorQuestionExample
|
||||
.where(user:, gekanator_question_id: questions.map(&:id))
|
||||
.distinct
|
||||
.pluck(:gekanator_question_id)
|
||||
unanswered, answered =
|
||||
questions.partition { |question| !answered_question_ids.include?(question.id) }
|
||||
selected = weighted_sample_questions(unanswered, post_id:, limit:)
|
||||
return selected if selected.length >= limit
|
||||
|
||||
selected + weighted_sample_questions(
|
||||
answered.reject { |question| selected.any? { _1.id == question.id } },
|
||||
post_id:,
|
||||
limit: limit - selected.length)
|
||||
end
|
||||
|
||||
def weighted_sample_questions questions, post_id:, limit:
|
||||
remaining = questions.uniq(&:id)
|
||||
selected = []
|
||||
|
||||
while selected.length < limit && remaining.any?
|
||||
weighted =
|
||||
remaining.map { |question|
|
||||
[question, selection_weight_for(question, post_id: post_id)]
|
||||
}
|
||||
total_weight = weighted.sum { |_question, weight| weight }
|
||||
break if total_weight <= 0
|
||||
|
||||
target = rand * total_weight
|
||||
cumulative = 0.0
|
||||
chosen =
|
||||
weighted.find do |_question, weight|
|
||||
cumulative += weight
|
||||
cumulative >= target
|
||||
end&.first || weighted.first.first
|
||||
|
||||
selected << chosen
|
||||
remaining.reject! { |question| question.id == chosen.id }
|
||||
end
|
||||
|
||||
selected
|
||||
end
|
||||
|
||||
def selection_weight_for question, post_id:
|
||||
sample_count =
|
||||
question.gekanator_question_examples.sum { |example|
|
||||
next 0 unless example.post_id == post_id
|
||||
|
||||
example.sample_count.presence || 1
|
||||
}
|
||||
|
||||
question.priority_weight.to_f / (1.0 + sample_count * 0.15)
|
||||
end
|
||||
|
||||
def find_owned_game
|
||||
return head :unauthorized unless current_user
|
||||
|
||||
game = GekanatorGame.find_by(id: params[:id])
|
||||
return head :not_found unless game
|
||||
if !current_user.admin? && game.user_id != current_user.id
|
||||
return head :not_found
|
||||
end
|
||||
|
||||
game
|
||||
end
|
||||
|
||||
def learn_answers_from_game! game
|
||||
correct_post = game.correct_post
|
||||
return 0 if correct_post.blank?
|
||||
|
||||
accepted_questions =
|
||||
GekanatorQuestion
|
||||
.accepted
|
||||
.index_by { |question| public_question_id_for(question) }
|
||||
learned_count = 0
|
||||
|
||||
Array(game.answers).each do |answer|
|
||||
answer_value = answer['answer'].to_s
|
||||
next if answer_value.blank? || answer_value == 'unknown'
|
||||
|
||||
question_id = game_answer_question_id(answer)
|
||||
next if question_id.blank?
|
||||
|
||||
question = accepted_questions[question_id.to_s]
|
||||
next unless learnable_game_answer_question?(question)
|
||||
|
||||
example =
|
||||
GekanatorQuestionExample.find_or_initialize_by(
|
||||
gekanator_question: question,
|
||||
post: correct_post,
|
||||
user: current_user)
|
||||
example.record_answer!(
|
||||
answer: answer_value,
|
||||
source: 'post_game_answer',
|
||||
gekanator_game: game)
|
||||
example.save!
|
||||
learned_count += 1
|
||||
end
|
||||
|
||||
learned_count
|
||||
end
|
||||
|
||||
def public_question_id_for question
|
||||
condition = normalize_condition(question.condition)
|
||||
|
||||
case condition[:type]
|
||||
when 'tag'
|
||||
"tag:#{condition[:key]}"
|
||||
when 'source'
|
||||
"source:#{condition[:host]}"
|
||||
when 'original-year'
|
||||
"original-year:#{condition[:year]}"
|
||||
when 'original-month'
|
||||
"original-month:#{condition[:month]}"
|
||||
when 'original-month-day'
|
||||
"original-month-day:#{condition[:monthDay] || condition[:month_day]}"
|
||||
when 'title-length-at-least'
|
||||
"title:length-at-least:#{condition[:length]}"
|
||||
when 'title-length-greater-than'
|
||||
"title:length-at-least:#{condition[:length].to_i + 1}"
|
||||
when 'title-has-ascii'
|
||||
'title:ascii'
|
||||
when 'title-contains'
|
||||
"title:contains:#{condition[:text]}"
|
||||
when 'post-similarity'
|
||||
"post-similarity:#{question.id}"
|
||||
else
|
||||
"catalog:#{question.id}"
|
||||
end
|
||||
end
|
||||
|
||||
def normalize_condition condition
|
||||
json = condition.deep_dup.as_json
|
||||
|
||||
if json['type'] == 'original-month-day' && json['monthDay'].blank?
|
||||
json['monthDay'] = json.delete('month_day')
|
||||
end
|
||||
|
||||
json.deep_symbolize_keys
|
||||
end
|
||||
|
||||
def learnable_game_answer_question? question
|
||||
return false if question.nil?
|
||||
return true if question.kind == 'post_similarity'
|
||||
return false unless question.kind == 'tag'
|
||||
|
||||
condition = normalize_condition(question.condition)
|
||||
key = condition[:key].to_s
|
||||
!key.start_with?('nico:')
|
||||
end
|
||||
|
||||
def game_answer_question_id answer
|
||||
answer['question_id'] ||
|
||||
answer[:question_id] ||
|
||||
answer['questionId'] ||
|
||||
answer[:questionId]
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,71 @@
|
||||
class GekanatorPostsController < ApplicationController
|
||||
def index
|
||||
posts =
|
||||
Post
|
||||
.preload(:post_similarities, tags: :tag_name)
|
||||
.with_attached_thumbnail
|
||||
.order(Arel.sql(
|
||||
'COALESCE(posts.original_created_before - INTERVAL 1 MINUTE, ' \
|
||||
'posts.original_created_from, posts.created_at) DESC, posts.id DESC'))
|
||||
.to_a
|
||||
|
||||
active_tags_by_post_id =
|
||||
posts.each_with_object({ }) do |post, h|
|
||||
h[post.id] = post.tags.reject(&:deprecated?)
|
||||
end
|
||||
|
||||
render json: {
|
||||
posts: posts.map { |post|
|
||||
post_json(post,
|
||||
active_tags_by_post_id:)
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def post_json post, active_tags_by_post_id:
|
||||
{
|
||||
id: post.id,
|
||||
url: post.url,
|
||||
title: post.title,
|
||||
thumbnail: thumbnail_url(post),
|
||||
thumbnail_base: post.thumbnail_base,
|
||||
original_created_from: post.original_created_from,
|
||||
original_created_before: post.original_created_before,
|
||||
post_similarity_edges: post_similarity_edges_json(
|
||||
post,
|
||||
active_tags_by_post_id:),
|
||||
tags: active_tags_by_post_id.fetch(post.id, []).map { |tag| tag_json(tag) }
|
||||
}
|
||||
end
|
||||
|
||||
def post_similarity_edges_json post, active_tags_by_post_id:
|
||||
post
|
||||
.post_similarities
|
||||
.filter_map do |similarity|
|
||||
next unless active_tags_by_post_id.key?(similarity.target_post_id)
|
||||
|
||||
{
|
||||
target_post_id: similarity.target_post_id,
|
||||
cos: similarity.cos.to_f
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
def tag_json tag
|
||||
{
|
||||
id: tag.id,
|
||||
name: tag.name,
|
||||
category: tag.category
|
||||
}
|
||||
end
|
||||
|
||||
def thumbnail_url post
|
||||
return nil unless post.thumbnail.attached?
|
||||
|
||||
rails_storage_proxy_url(post.thumbnail, only_path: false)
|
||||
rescue
|
||||
nil
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,95 @@
|
||||
class GekanatorQuestionSuggestionsController < ApplicationController
|
||||
def create
|
||||
return head :unauthorized unless current_user
|
||||
|
||||
game = GekanatorGame.find_by(id: params.require(:gekanator_game_id))
|
||||
return head :not_found unless game
|
||||
if !current_user.admin? && game.user_id != current_user.id
|
||||
return head :not_found
|
||||
end
|
||||
|
||||
existing_question_id = params[:existing_question_id].presence
|
||||
if existing_question_id
|
||||
question = GekanatorQuestion.accepted.find_by(id: existing_question_id)
|
||||
return head :not_found unless question
|
||||
unless learnable_existing_question?(question)
|
||||
return render_validation_error fields: { existing_question_id: ['質問が不正です.'] }
|
||||
end
|
||||
|
||||
example =
|
||||
GekanatorQuestionExample.find_or_initialize_by(
|
||||
gekanator_question: question,
|
||||
post: game.correct_post,
|
||||
user: current_user)
|
||||
example.record_answer!(
|
||||
answer: params.require(:answer),
|
||||
source: 'post_game_extra',
|
||||
gekanator_game: game)
|
||||
|
||||
if example.save
|
||||
render json: {
|
||||
id: question.id,
|
||||
count: game.question_suggestions.count
|
||||
}, status: :created
|
||||
else
|
||||
render_validation_error example
|
||||
end
|
||||
return
|
||||
end
|
||||
|
||||
suggestion = GekanatorQuestionSuggestion.new(
|
||||
gekanator_game: game,
|
||||
user: current_user,
|
||||
question_text: params.require(:question_text),
|
||||
answer: params.require(:answer))
|
||||
|
||||
if suggestion.valid?
|
||||
ActiveRecord::Base.transaction do
|
||||
suggestion.save!
|
||||
Gekanator::QuestionSuggestionPromoter.call(
|
||||
suggestion: suggestion,
|
||||
user: current_user)
|
||||
end
|
||||
|
||||
render json: {
|
||||
id: suggestion.id,
|
||||
count: game.question_suggestions.count
|
||||
}, status: :created
|
||||
else
|
||||
render_validation_error suggestion
|
||||
end
|
||||
end
|
||||
|
||||
def ai_convert
|
||||
return head :not_found unless current_user&.admin?
|
||||
|
||||
suggestion = GekanatorQuestionSuggestion.find_by(id: params[:id])
|
||||
return head :not_found unless suggestion
|
||||
if Gekanator::AiRunBudget.exceeded_after_next_run?
|
||||
suggestion.gekanator_ai_runs.create!(
|
||||
model: 'budget_guard',
|
||||
status: 'blocked_budget',
|
||||
input_tokens: 0,
|
||||
output_tokens: 0,
|
||||
estimated_cost_jpy: 0)
|
||||
return head :payment_required
|
||||
end
|
||||
|
||||
Gekanator::QuestionSuggestionAiConverter.call(
|
||||
suggestion: suggestion,
|
||||
user: current_user)
|
||||
head :no_content
|
||||
rescue NotImplementedError
|
||||
head :not_implemented
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def learnable_existing_question? question
|
||||
return true if question.kind == 'post_similarity'
|
||||
return false unless question.kind == 'tag'
|
||||
|
||||
key = question.condition.as_json['key'].to_s
|
||||
!key.start_with?('nico:')
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,147 @@
|
||||
class GekanatorQuestionsController < ApplicationController
|
||||
def index
|
||||
questions =
|
||||
GekanatorQuestion
|
||||
.accepted
|
||||
.includes(:gekanator_question_examples)
|
||||
.order(priority_weight: :desc, id: :asc)
|
||||
.to_a
|
||||
deprecated_tag_keys = deprecated_tag_keys_for(questions)
|
||||
|
||||
render json: {
|
||||
questions: questions.filter_map { |question|
|
||||
json = question_json(question)
|
||||
next if hidden_question?(json[:condition], deprecated_tag_keys)
|
||||
|
||||
json
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def question_json question
|
||||
condition = condition_json(question.condition).deep_symbolize_keys
|
||||
json = {
|
||||
record_id: question.id,
|
||||
id: question_id_for(question, condition),
|
||||
text: question_text_for(question, condition),
|
||||
kind: question.kind,
|
||||
condition: condition,
|
||||
source: question.source,
|
||||
priority_weight: question.priority_weight
|
||||
}
|
||||
if question.kind == 'post_similarity' || question.kind == 'tag'
|
||||
json[:example_answers] = example_answers_json(question)
|
||||
end
|
||||
json
|
||||
end
|
||||
|
||||
def question_id_for question, condition
|
||||
case condition[:type]
|
||||
when 'tag'
|
||||
"tag:#{ condition[:key] }"
|
||||
when 'source'
|
||||
"source:#{ condition[:host] }"
|
||||
when 'original-year'
|
||||
"original-year:#{ condition[:year] }"
|
||||
when 'original-month'
|
||||
"original-month:#{ condition[:month] }"
|
||||
when 'original-month-day'
|
||||
"original-month-day:#{ condition[:monthDay] || condition[:month_day] }"
|
||||
when 'title-length-at-least'
|
||||
"title:length-at-least:#{ condition[:length] }"
|
||||
when 'title-length-greater-than'
|
||||
"title:length-at-least:#{ condition[:length].to_i + 1 }"
|
||||
when 'title-has-ascii'
|
||||
'title:ascii'
|
||||
when 'title-contains'
|
||||
"title:contains:#{ condition[:text] }"
|
||||
when 'post-similarity'
|
||||
"post-similarity:#{ question.id }"
|
||||
else
|
||||
"catalog:#{ question.id }"
|
||||
end
|
||||
end
|
||||
|
||||
def condition_json condition
|
||||
json = condition.deep_dup.as_json
|
||||
|
||||
if json['type'] == 'original-month-day' && json['monthDay'].blank?
|
||||
json['monthDay'] = json.delete('month_day')
|
||||
end
|
||||
|
||||
if json['type'] == 'title-length-greater-than'
|
||||
json['type'] = 'title-length-at-least'
|
||||
json['length'] = json['length'].to_i + 1
|
||||
end
|
||||
|
||||
json
|
||||
end
|
||||
|
||||
def question_text_for question, condition
|
||||
return question.text unless question.kind == 'title'
|
||||
|
||||
case condition[:type]
|
||||
when 'title-length-at-least'
|
||||
"タイトルは #{ condition[:length] } 文字以上?"
|
||||
when 'title-contains'
|
||||
"題名に「#{ condition[:text] }」が含まれる?"
|
||||
else
|
||||
question.text
|
||||
end
|
||||
end
|
||||
|
||||
def example_answers_json question
|
||||
question
|
||||
.gekanator_question_examples
|
||||
.group_by(&:post_id)
|
||||
.transform_values { |examples| aggregate_answer(examples) }
|
||||
end
|
||||
|
||||
def aggregate_answer examples
|
||||
examples
|
||||
.group_by(&:answer)
|
||||
.map { |answer, grouped| [answer, grouped.sum(&:weight), grouped.max_by(&:updated_at)&.updated_at] }
|
||||
.sort_by { |(_answer, weight, updated_at)| [-weight, -(updated_at&.to_f || 0)] }
|
||||
.first
|
||||
&.first
|
||||
end
|
||||
|
||||
def deprecated_tag_keys_for questions
|
||||
tag_keys = questions.filter_map { |question|
|
||||
condition = condition_json(question.condition)
|
||||
next unless condition['type'] == 'tag'
|
||||
|
||||
condition['key'].to_s.presence
|
||||
}.uniq
|
||||
return {} if tag_keys.empty?
|
||||
|
||||
categories = []
|
||||
names = []
|
||||
tag_keys.each do |key|
|
||||
category, name = parse_tag_key(key)
|
||||
categories << category
|
||||
names << name
|
||||
end
|
||||
|
||||
Tag
|
||||
.joins(:tag_name)
|
||||
.where(category: categories.uniq)
|
||||
.where(tag_names: { name: names.uniq })
|
||||
.where.not(deprecated_at: nil)
|
||||
.pluck('tags.category', 'tag_names.name')
|
||||
.each_with_object({ }) do |(category, name), h|
|
||||
h["#{ category }:#{ name }"] = true
|
||||
end
|
||||
end
|
||||
|
||||
def hidden_question? condition, deprecated_tag_keys
|
||||
condition[:type] == 'tag' && deprecated_tag_keys[condition[:key].to_s]
|
||||
end
|
||||
|
||||
def parse_tag_key key
|
||||
parts = key.to_s.split(':')
|
||||
[parts.first.to_s, parts.drop(1).join(':')]
|
||||
end
|
||||
end
|
||||
@@ -40,7 +40,11 @@ class MaterialsController < ApplicationController
|
||||
tag_name_raw = params[:tag].to_s.strip
|
||||
file = params[:file]
|
||||
url = params[:url].to_s.strip.presence
|
||||
return head :bad_request if tag_name_raw.blank? || (file.blank? && url.blank?)
|
||||
return render_unprocessable_entity('タグは必須です.', field: :tag) if tag_name_raw.blank?
|
||||
if file.blank? && url.blank?
|
||||
return render_validation_error fields: { file: ['ファイルまたは URL は必須です.'],
|
||||
url: ['ファイルまたは URL は必須です.'] }
|
||||
end
|
||||
|
||||
tag_name = TagName.find_undiscard_or_create_by!(name: tag_name_raw)
|
||||
tag = tag_name.tag
|
||||
@@ -54,7 +58,7 @@ class MaterialsController < ApplicationController
|
||||
if material.save
|
||||
render json: MaterialRepr.base(material, host: request.base_url), status: :created
|
||||
else
|
||||
render json: { errors: material.errors.full_messages }, status: :unprocessable_entity
|
||||
render_validation_error material
|
||||
end
|
||||
end
|
||||
|
||||
@@ -68,7 +72,11 @@ class MaterialsController < ApplicationController
|
||||
tag_name_raw = params[:tag].to_s.strip
|
||||
file = params[:file]
|
||||
url = params[:url].to_s.strip.presence
|
||||
return head :bad_request if tag_name_raw.blank? || (file.blank? && url.blank?)
|
||||
return render_unprocessable_entity('タグは必須です.', field: :tag) if tag_name_raw.blank?
|
||||
if file.blank? && url.blank?
|
||||
return render_validation_error fields: { file: ['ファイルまたは URL は必須です.'],
|
||||
url: ['ファイルまたは URL は必須です.'] }
|
||||
end
|
||||
|
||||
tag_name = TagName.find_undiscard_or_create_by!(name: tag_name_raw)
|
||||
tag = tag_name.tag
|
||||
@@ -84,7 +92,7 @@ class MaterialsController < ApplicationController
|
||||
if material.save
|
||||
render json: MaterialRepr.base(material, host: request.base_url)
|
||||
else
|
||||
render json: { errors: material.errors.full_messages }, status: :unprocessable_entity
|
||||
render_validation_error material
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -1,26 +1,69 @@
|
||||
class NicoTagsController < ApplicationController
|
||||
def index
|
||||
limit = (params[:limit] || 20).to_i
|
||||
cursor = params[:cursor].presence
|
||||
name = params[:name].presence
|
||||
linked_tag = params[:linked_tag].presence
|
||||
link_status = params[:link_status].presence
|
||||
order = params[:order].to_s.split(':', 2).map(&:strip)
|
||||
order[0] = 'updated_at' unless order[0].in?(['name', 'created_at', 'updated_at'])
|
||||
unless order[1].in?(['asc', 'desc'])
|
||||
order[1] = order[0] == 'name' ? 'asc' : 'desc'
|
||||
end
|
||||
page = (params[:page].presence || 1).to_i
|
||||
limit = (params[:limit].presence || 20).to_i
|
||||
|
||||
page = 1 if page < 1
|
||||
limit = 1 if limit < 1
|
||||
|
||||
post_tag_max_sql =
|
||||
PostTag
|
||||
.select('tag_id, MAX(created_at) AS max_created_at')
|
||||
.group('tag_id')
|
||||
.to_sql
|
||||
|
||||
q = Tag.nico_tags
|
||||
.joins(:tag_name)
|
||||
.joins("LEFT JOIN (#{ post_tag_max_sql }) post_tag_max " \
|
||||
'ON post_tag_max.tag_id = tags.id')
|
||||
.includes(:tag_name, tag_name: :wiki_page, linked_tags: { tag_name: :wiki_page })
|
||||
.order(updated_at: :desc)
|
||||
q = q.where('tags.updated_at < ?', Time.iso8601(cursor)) if cursor
|
||||
|
||||
tags = q.limit(limit + 1).to_a
|
||||
|
||||
next_cursor = nil
|
||||
if tags.size > limit
|
||||
next_cursor = tags.last.updated_at.iso8601(6)
|
||||
tags = tags.first(limit)
|
||||
q = q.where('tag_names.name LIKE ?', "%#{ name }%") if name
|
||||
if linked_tag
|
||||
linked_tag_ids =
|
||||
Tag
|
||||
.joins(:tag_name)
|
||||
.where('tag_names.name LIKE ?', "%#{ linked_tag }%")
|
||||
.pluck(:id)
|
||||
linked_nico_tag_ids = NicoTagRelation.where(tag_id: linked_tag_ids).pluck(:nico_tag_id)
|
||||
q = q.where(id: linked_nico_tag_ids)
|
||||
end
|
||||
if link_status.in?(['linked', 'unlinked'])
|
||||
exists_sql =
|
||||
'EXISTS (SELECT 1 FROM nico_tag_relations ' \
|
||||
'WHERE nico_tag_relations.nico_tag_id = tags.id)'
|
||||
q = link_status == 'linked' ? q.where(exists_sql) : q.where("NOT #{ exists_sql }")
|
||||
end
|
||||
|
||||
count = q.count
|
||||
sort_sql =
|
||||
case order[0]
|
||||
when 'name'
|
||||
'tag_names.name'
|
||||
when 'updated_at'
|
||||
'post_tag_max.max_created_at'
|
||||
else
|
||||
"tags.#{ order[0] }"
|
||||
end
|
||||
tags = q.reselect('tags.*',
|
||||
Arel.sql('post_tag_max.max_created_at AS recent_post_tag_created_at'))
|
||||
.order(Arel.sql("#{ sort_sql } #{ order[1] }, tags.id #{ order[1] }"))
|
||||
.limit(limit)
|
||||
.offset((page - 1) * limit)
|
||||
.to_a
|
||||
|
||||
render json: { tags: tags.map { |tag|
|
||||
TagRepr.base(tag).merge(linked_tags: tag.linked_tags.map { |lt|
|
||||
TagRepr.base(lt)
|
||||
})
|
||||
}, next_cursor: }
|
||||
TagRepr.base(tag).merge(
|
||||
recent_post_tag_created_at: tag.recent_post_tag_created_at,
|
||||
linked_tags: tag.linked_tags.map { |lt| TagRepr.base(lt) })
|
||||
}, count: }
|
||||
end
|
||||
|
||||
def update
|
||||
@@ -30,14 +73,18 @@ class NicoTagsController < ApplicationController
|
||||
id = params[:id].to_i
|
||||
|
||||
tag = Tag.find(id)
|
||||
return head :bad_request unless tag.nico?
|
||||
return render_bad_request('ニコニコ・タグを指定してください.') unless tag.nico?
|
||||
|
||||
linked_tag_names = params[:tags].to_s.split
|
||||
linked_tags = Tag.normalise_tags!(linked_tag_names, with_tagme: false,
|
||||
with_no_deerjikist: false)
|
||||
return head :bad_request if linked_tags.any? { |t| t.nico? }
|
||||
linked_tags = nil
|
||||
|
||||
ApplicationRecord.transaction do
|
||||
linked_tags = Tag.normalise_tags!(linked_tag_names, with_tagme: false,
|
||||
with_no_deerjikist: false)
|
||||
if linked_tags.any? { |t| t.nico? }
|
||||
raise Tag::NicoTagNormalisationError
|
||||
end
|
||||
|
||||
TagVersioning.record_tag_snapshots!(linked_tags, created_by_user: current_user)
|
||||
|
||||
tag.linked_tags = linked_tags
|
||||
@@ -47,5 +94,21 @@ class NicoTagsController < ApplicationController
|
||||
end
|
||||
|
||||
render json: tag.linked_tags.map { |t| TagRepr.base(t) }, status: :ok
|
||||
rescue Tag::NicoTagNormalisationError
|
||||
render_validation_error fields: { tags: ['ニコニコ・タグ同士は連携できません.'] }
|
||||
rescue ActiveRecord::RecordInvalid => e
|
||||
render_nico_tag_form_record_invalid e.record
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def render_nico_tag_form_record_invalid record
|
||||
if record.is_a?(TagName) || record.is_a?(Tag)
|
||||
render_validation_error fields: { tags: record.errors.full_messages.map { |message|
|
||||
"タグ名 “#{ record.name }”: #{ message }"
|
||||
} }
|
||||
else
|
||||
render_validation_error record
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -44,7 +44,8 @@ class PostsController < ApplicationController
|
||||
filtered_posts
|
||||
.joins("LEFT JOIN (#{ pt_max_sql }) pt_max ON pt_max.post_id = posts.id")
|
||||
.reselect('posts.*', Arel.sql("#{ updated_at_all_sql } AS updated_at_all"))
|
||||
.preload(tags: [:deerjikists, :materials, { tag_name: :wiki_page }])
|
||||
.preload(:uploaded_user, :parents, :children,
|
||||
tags: [:deerjikists, :materials, { tag_name: :wiki_page }])
|
||||
.with_attached_thumbnail
|
||||
|
||||
q = q.where('posts.url LIKE ?', "%#{ url }%") if url
|
||||
@@ -95,22 +96,36 @@ class PostsController < ApplicationController
|
||||
end
|
||||
|
||||
def random
|
||||
post = filtered_posts
|
||||
.preload(tags: [:deerjikists, :materials, { tag_name: :wiki_page }])
|
||||
.order('RAND()')
|
||||
.first
|
||||
post = filtered_posts.preload(:uploaded_user, :parents, :children,
|
||||
tags: [:deerjikists, :materials, { tag_name: :wiki_page }])
|
||||
.with_attached_thumbnail
|
||||
.order('RAND()')
|
||||
.first
|
||||
return head :not_found unless post
|
||||
|
||||
render json: PostRepr.base(post, current_user)
|
||||
end
|
||||
|
||||
def show
|
||||
post = Post.includes(tags: [:deerjikists, :materials, { tag_name: :wiki_page }]).find_by(id: params[:id])
|
||||
post =
|
||||
Post
|
||||
.includes(:uploaded_user, :parents, :children,
|
||||
tags: [:deerjikists, :materials, { tag_name: :wiki_page }])
|
||||
.with_attached_thumbnail
|
||||
.find_by(id: params[:id])
|
||||
return head :not_found unless post
|
||||
|
||||
render json: PostRepr.base(post, current_user)
|
||||
.merge(tags: build_tag_tree_for(post),
|
||||
related: PostRepr.many(post.related(limit: 20)))
|
||||
parent_posts = post.parents.with_attached_thumbnail.order(:id).to_a
|
||||
child_posts = post.children.with_attached_thumbnail.order(:id).to_a
|
||||
sibling_posts = sibling_posts_by_parent(parent_posts.map(&:id))
|
||||
related = post.related(limit: 20).to_a
|
||||
|
||||
render json: PostRepr.detail(post, current_user,
|
||||
parent_posts:,
|
||||
child_posts:,
|
||||
sibling_posts:,
|
||||
related:)
|
||||
.merge(tags: build_tag_tree_for(post))
|
||||
end
|
||||
|
||||
def create
|
||||
@@ -133,10 +148,11 @@ class PostsController < ApplicationController
|
||||
ApplicationRecord.transaction do
|
||||
post.save!
|
||||
|
||||
Tag.normalise_tags!(tag_names, with_sections: true) => { tags:, sections: }
|
||||
Tag.normalise_tags!(tag_names, deny_deprecated: true, with_sections: true) =>
|
||||
{ tags:, sections: }
|
||||
TagVersioning.record_tag_snapshots!(tags, created_by_user: current_user)
|
||||
|
||||
tags = Tag.expand_parent_tags(tags)
|
||||
tags = Tag.expand_parent_tags(tags).reject(&:deprecated?)
|
||||
sync_post_tags!(post, tags, sections)
|
||||
|
||||
sync_parent_posts!(post, parent_post_ids)
|
||||
@@ -149,11 +165,13 @@ class PostsController < ApplicationController
|
||||
post.reload
|
||||
render json: PostRepr.base(post), status: :created
|
||||
rescue Tag::NicoTagNormalisationError
|
||||
head :bad_request
|
||||
render_validation_error fields: { tags: 'ニコニコ・タグは直接指定できません.' }
|
||||
rescue Tag::DeprecatedTagNormalisationError
|
||||
render_unprocessable_entity '廃止済みタグは付与できません.', field: :tags
|
||||
rescue ArgumentError => e
|
||||
render json: { errors: [e.message] }, status: :unprocessable_entity
|
||||
render_validation_error fields: { parent_post_ids: [e.message] }
|
||||
rescue ActiveRecord::RecordInvalid => e
|
||||
render json: { errors: e.record.errors.full_messages }, status: :unprocessable_entity
|
||||
render_post_form_record_invalid e.record
|
||||
end
|
||||
|
||||
def viewed
|
||||
@@ -176,10 +194,10 @@ class PostsController < ApplicationController
|
||||
|
||||
force = bool?(:force)
|
||||
merge = bool?(:merge)
|
||||
return head :bad_request if force && merge
|
||||
return render_bad_request('force と merge は同時に指定できません.') if force && merge
|
||||
|
||||
base_version_no = parse_base_version_no
|
||||
return head :bad_request if !(force) && !(base_version_no)
|
||||
return render_bad_request('base_version_no は必須です.') if !(force) && !(base_version_no)
|
||||
|
||||
title = params[:title].presence
|
||||
tag_names = params[:tags].to_s.split
|
||||
@@ -239,11 +257,13 @@ class PostsController < ApplicationController
|
||||
json['tags'] = build_tag_tree_for(post)
|
||||
render json:, status: :ok
|
||||
rescue Tag::NicoTagNormalisationError
|
||||
head :bad_request
|
||||
render_validation_error fields: { tags: ['ニコニコ・タグは直接指定できません.'] }
|
||||
rescue Tag::DeprecatedTagNormalisationError
|
||||
render_unprocessable_entity '廃止済みタグは付与できません.', field: :tags
|
||||
rescue ArgumentError => e
|
||||
render json: { errors: [e.message] }, status: :unprocessable_entity
|
||||
render_validation_error fields: { parent_post_ids: [e.message] }
|
||||
rescue ActiveRecord::RecordInvalid => e
|
||||
render json: { errors: e.record.errors.full_messages }, status: :unprocessable_entity
|
||||
render_post_form_record_invalid e.record
|
||||
end
|
||||
|
||||
def changes
|
||||
@@ -370,7 +390,7 @@ class PostsController < ApplicationController
|
||||
end
|
||||
|
||||
def build_tag_tree_for post
|
||||
tags = post.tags.to_a
|
||||
tags = post.tags.reject(&:deprecated?).to_a
|
||||
tag_ids = tags.map(&:id)
|
||||
|
||||
implications = TagImplication.where(parent_tag_id: tag_ids, tag_id: tag_ids)
|
||||
@@ -385,6 +405,11 @@ class PostsController < ApplicationController
|
||||
root_ids = tag_ids - child_ids
|
||||
|
||||
tags_by_id = tags.index_by(&:id)
|
||||
sections_by_tag_id =
|
||||
PostTagSection
|
||||
.where(post_id: post.id, tag_id: tag_ids)
|
||||
.order(:begin_ms)
|
||||
.group_by(&:tag_id)
|
||||
|
||||
memo = { }
|
||||
|
||||
@@ -392,11 +417,11 @@ class PostsController < ApplicationController
|
||||
tag = tags_by_id[tag_id]
|
||||
return nil unless tag
|
||||
|
||||
sections = PostTagSection.where(post_id: post.id, tag_id:)
|
||||
.as_json(only: [:begin_ms, :end_ms])
|
||||
sections = sections_by_tag_id.fetch(tag_id, [])
|
||||
.as_json(only: [:begin_ms, :end_ms])
|
||||
|
||||
if path.include?(tag_id)
|
||||
return TagRepr.base(tag).merge(children: [], sections:)
|
||||
return TagRepr.inline(tag).merge(children: [], sections:)
|
||||
end
|
||||
|
||||
if memo.key?(tag_id)
|
||||
@@ -408,12 +433,26 @@ class PostsController < ApplicationController
|
||||
|
||||
children = child_ids.filter_map { |cid| build_node.(cid, new_path) }
|
||||
|
||||
memo[tag_id] = TagRepr.base(tag).merge(children:, sections:)
|
||||
memo[tag_id] = TagRepr.inline(tag).merge(children:, sections:)
|
||||
end
|
||||
|
||||
root_ids.filter_map { |id| build_node.call(id, []) }
|
||||
end
|
||||
|
||||
def sibling_posts_by_parent parent_post_ids
|
||||
return { } if parent_post_ids.blank?
|
||||
|
||||
implications =
|
||||
PostImplication
|
||||
.where(parent_post_id: parent_post_ids)
|
||||
.includes(post: { thumbnail_attachment: :blob })
|
||||
.order(:parent_post_id, :post_id)
|
||||
|
||||
implications.group_by(&:parent_post_id).transform_values { |items|
|
||||
items.map(&:post)
|
||||
}
|
||||
end
|
||||
|
||||
def parse_parent_post_ids
|
||||
raise ArgumentError, 'parent_post_ids は必須です.' unless params.key?(:parent_post_ids)
|
||||
|
||||
@@ -427,7 +466,7 @@ class PostsController < ApplicationController
|
||||
|
||||
def sync_parent_posts! post, parent_post_ids
|
||||
if parent_post_ids.include?(post.id)
|
||||
post.errors.add(:base, '自分自身を親投稿にはできません.')
|
||||
post.errors.add :parent_post_ids, '自分自身を親投稿にはできません.'
|
||||
raise ActiveRecord::RecordInvalid, post
|
||||
end
|
||||
|
||||
@@ -435,7 +474,8 @@ class PostsController < ApplicationController
|
||||
missing_ids = parent_post_ids - existing_ids
|
||||
|
||||
if missing_ids.present?
|
||||
post.errors.add(:base, "存在しない親投稿 ID があります: #{ missing_ids.join(' ') }")
|
||||
post.errors.add :parent_post_ids,
|
||||
"存在しない親投稿 Id. があります: #{ missing_ids.join(' ') }"
|
||||
raise ActiveRecord::RecordInvalid, post
|
||||
end
|
||||
|
||||
@@ -486,6 +526,7 @@ class PostsController < ApplicationController
|
||||
.kept
|
||||
.joins(tag: :tag_name)
|
||||
.merge(Tag.not_nico)
|
||||
.merge(Tag.where(deprecated_at: nil))
|
||||
.includes(:sections, tag: :tag_name)
|
||||
.order('tag_names.name')
|
||||
.map do |post_tag|
|
||||
@@ -527,10 +568,11 @@ class PostsController < ApplicationController
|
||||
end
|
||||
|
||||
def incoming_tag_names_for_snapshot raw_tag_names
|
||||
Tag.normalise_tags!(raw_tag_names, with_tagme: false, with_sections: true) =>
|
||||
Tag.normalise_tags!(raw_tag_names, with_tagme: false, deny_deprecated: true,
|
||||
with_sections: true) =>
|
||||
{ tags:, sections: }
|
||||
|
||||
Tag.expand_parent_tags(tags).uniq(&:id).map { |tag|
|
||||
Tag.expand_parent_tags(tags).reject(&:deprecated?).uniq(&:id).map { |tag|
|
||||
"#{ tag.name }#{ sections[tag.id].to_a.map { section_literal(_1) }.join }"
|
||||
}.sort
|
||||
end
|
||||
@@ -623,14 +665,16 @@ class PostsController < ApplicationController
|
||||
original_created_from: snapshot[:original_created_from],
|
||||
original_created_before: snapshot[:original_created_before])
|
||||
|
||||
Tag.normalise_tags!(snapshot[:tag_names], with_tagme: false, with_sections: true) =>
|
||||
Tag.normalise_tags!(snapshot[:tag_names], with_tagme: false,
|
||||
deny_deprecated: true,
|
||||
with_sections: true) =>
|
||||
{ tags: editable_tags, sections: }
|
||||
TagVersioning.record_tag_snapshots!(editable_tags, created_by_user: current_user)
|
||||
|
||||
readonly_tags = post.tags.nico.to_a
|
||||
|
||||
tags = readonly_tags + editable_tags
|
||||
tags = Tag.expand_parent_tags(tags)
|
||||
tags = Tag.expand_parent_tags(tags).reject(&:deprecated?)
|
||||
|
||||
sync_post_tags!(post, tags, sections)
|
||||
sync_parent_posts!(post, snapshot[:parent_post_ids])
|
||||
@@ -673,4 +717,14 @@ class PostsController < ApplicationController
|
||||
|
||||
merged.uniq.sort
|
||||
end
|
||||
|
||||
def render_post_form_record_invalid record
|
||||
if record.is_a?(TagName) || record.is_a?(Tag)
|
||||
render_validation_error fields: { tags: record.errors.full_messages.map { |message|
|
||||
"タグ名 “#{ record.name }”: #{ message }"
|
||||
} }
|
||||
else
|
||||
render_validation_error record
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,7 +4,7 @@ class PreviewController < ApplicationController
|
||||
return head :unauthorized unless current_user
|
||||
|
||||
url = params[:url]
|
||||
return head :bad_request unless url.present?
|
||||
return render_bad_request('URL は必須です.') unless url.present?
|
||||
|
||||
unless url.start_with?(/http(s)?:\/\//)
|
||||
url = 'http://' + url
|
||||
@@ -16,7 +16,7 @@ class PreviewController < ApplicationController
|
||||
|
||||
render json: { title: title }
|
||||
rescue => e
|
||||
render json: { error: e.message }, status: :bad_request
|
||||
render_bad_request(e.message)
|
||||
end
|
||||
|
||||
def thumbnail
|
||||
@@ -25,7 +25,7 @@ class PreviewController < ApplicationController
|
||||
return head :unauthorized unless current_user
|
||||
|
||||
url = params[:url]
|
||||
return head :bad_request if url.blank?
|
||||
return render_bad_request('URL は必須です.') if url.blank?
|
||||
|
||||
unless url.start_with?(/http(s)?:\/\//)
|
||||
url = 'http://' + url
|
||||
@@ -40,7 +40,11 @@ class PreviewController < ApplicationController
|
||||
File.delete(path) rescue nil
|
||||
send_file image.path, type: 'image/png', disposition: 'inline'
|
||||
else
|
||||
render json: { error: 'Failed to generate thumbnail' }, status: :internal_server_error
|
||||
render json: { type: 'internal_server_error',
|
||||
message: 'サムネールを生成できませんでした.',
|
||||
errors: { },
|
||||
base_errors: ['サムネールを生成できませんでした.'] },
|
||||
status: :internal_server_error
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -5,11 +5,12 @@ class TagChildrenController < ApplicationController
|
||||
|
||||
parent_id = params[:parent_id]
|
||||
child_id = params[:child_id]
|
||||
return head :bad_request if parent_id.blank? || child_id.blank?
|
||||
return render_bad_request('parent_id は必須です.') if parent_id.blank?
|
||||
return render_bad_request('child_id は必須です.') if child_id.blank?
|
||||
|
||||
parent = Tag.find(parent_id)
|
||||
child = Tag.find(child_id)
|
||||
return head :bad_request if parent.nico? || child.nico?
|
||||
return render_bad_request('ニコニコ・タグの階層は変更できません.') if parent.nico? || child.nico?
|
||||
|
||||
ApplicationRecord.transaction do
|
||||
TagVersioning.ensure_snapshot!(child, created_by_user: current_user)
|
||||
@@ -27,11 +28,12 @@ class TagChildrenController < ApplicationController
|
||||
|
||||
parent_id = params[:parent_id]
|
||||
child_id = params[:child_id]
|
||||
return head :bad_request if parent_id.blank? || child_id.blank?
|
||||
return render_bad_request('parent_id は必須です.') if parent_id.blank?
|
||||
return render_bad_request('child_id は必須です.') if child_id.blank?
|
||||
|
||||
parent = Tag.find(parent_id)
|
||||
child = Tag.find(child_id)
|
||||
return head :bad_request if parent.nico? || child.nico?
|
||||
return render_bad_request('ニコニコ・タグの階層は変更できません.') if parent.nico? || child.nico?
|
||||
|
||||
ApplicationRecord.transaction do
|
||||
TagVersioning.ensure_snapshot!(child, created_by_user: current_user)
|
||||
|
||||
@@ -17,6 +17,7 @@ class TagVersionsController < ApplicationController
|
||||
AND prev.version_no = tag_versions.version_no - 1
|
||||
SQL
|
||||
.select('tag_versions.*', 'prev.name AS prev_name', 'prev.category AS prev_category',
|
||||
'prev.deprecated_at AS prev_deprecated_at',
|
||||
'prev.aliases AS prev_aliases', 'prev.parent_tag_ids AS prev_parent_tag_ids')
|
||||
q = q.where('tag_versions.tag_id = ?', tag_id) if tag_id
|
||||
|
||||
@@ -62,6 +63,8 @@ class TagVersionsController < ApplicationController
|
||||
event_type: row.event_type,
|
||||
name: { current: row.name, prev: row.attributes['prev_name'] },
|
||||
category: { current: row.category, prev: row.attributes['prev_category'] },
|
||||
deprecated_at: { current: row.deprecated_at&.iso8601,
|
||||
prev: row.attributes['prev_deprecated_at']&.iso8601 },
|
||||
aliases: build_version_values(cur_aliases, prev_aliases, key: :name),
|
||||
parent_tags:,
|
||||
created_at: row.created_at.iso8601,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
require 'net/http'
|
||||
require 'uri'
|
||||
require 'set'
|
||||
|
||||
|
||||
class TagsController < ApplicationController
|
||||
@@ -14,6 +15,8 @@ class TagsController < ApplicationController
|
||||
post_count_between[1] = nil if post_count_between[1] < 0
|
||||
created_between = params[:created_from].presence, params[:created_to].presence
|
||||
updated_between = params[:updated_from].presence, params[:updated_to].presence
|
||||
deprecated_given = params.key?(:deprecated)
|
||||
deprecated = bool?(:deprecated)
|
||||
|
||||
order = params[:order].to_s.split(':', 2).map(&:strip)
|
||||
unless order[0].in?(['name', 'category', 'post_count', 'created_at', 'updated_at'])
|
||||
@@ -48,6 +51,9 @@ class TagsController < ApplicationController
|
||||
q = q.where('tags.created_at <= ?', created_between[1]) if created_between[1]
|
||||
q = q.where('tags.updated_at >= ?', updated_between[0]) if updated_between[0]
|
||||
q = q.where('tags.updated_at <= ?', updated_between[1]) if updated_between[1]
|
||||
if deprecated_given
|
||||
q = deprecated ? q.where.not(deprecated_at: nil) : q.where(deprecated_at: nil)
|
||||
end
|
||||
|
||||
sort_sql =
|
||||
case order[0]
|
||||
@@ -77,37 +83,27 @@ class TagsController < ApplicationController
|
||||
parent_tag_id = params[:parent].to_i
|
||||
parent_tag_id = nil if parent_tag_id <= 0
|
||||
|
||||
graph = build_with_depth_graph
|
||||
|
||||
tag_ids =
|
||||
if parent_tag_id
|
||||
TagImplication.where(parent_tag_id:).select(:tag_id)
|
||||
visible_child_tag_ids(parent_tag_id, graph)
|
||||
else
|
||||
Tag.where.not(id: TagImplication.select(:tag_id)).select(:id)
|
||||
visible_root_tag_ids(graph)
|
||||
end
|
||||
|
||||
tags =
|
||||
Tag
|
||||
.joins(:tag_name)
|
||||
.includes(:tag_name, :materials, tag_name: :wiki_page)
|
||||
.where(category: [:meme, :character, :material])
|
||||
.where(id: tag_ids)
|
||||
.order('tag_names.name')
|
||||
.distinct
|
||||
.to_a
|
||||
|
||||
has_children_tag_ids =
|
||||
if tags.empty?
|
||||
[]
|
||||
else
|
||||
TagImplication
|
||||
.joins(:tag)
|
||||
.where(parent_tag_id: tags.map(&:id),
|
||||
tags: { category: [:meme, :character, :material] })
|
||||
.distinct
|
||||
.pluck(:parent_tag_id)
|
||||
end
|
||||
|
||||
render json: tags.map { |tag|
|
||||
TagRepr.base(tag).merge(has_children: has_children_tag_ids.include?(tag.id), children: [])
|
||||
TagRepr.base(tag).merge(has_children: visible_child_tag_ids(tag.id, graph).present?,
|
||||
children: [])
|
||||
}
|
||||
end
|
||||
|
||||
@@ -133,6 +129,7 @@ class TagsController < ApplicationController
|
||||
|
||||
base = Tag.joins(:tag_name)
|
||||
.includes(:tag_name, :materials, tag_name: :wiki_page)
|
||||
.where(deprecated_at: nil)
|
||||
base = base.where('tags.post_count > 0') if present_only
|
||||
|
||||
canonical_hit =
|
||||
@@ -168,7 +165,7 @@ class TagsController < ApplicationController
|
||||
|
||||
def show_by_name
|
||||
name = params[:name].to_s.strip
|
||||
return head :bad_request if name.blank?
|
||||
return render_bad_request('name は必須です.') if name.blank?
|
||||
|
||||
tag = Tag.joins(:tag_name)
|
||||
.includes(:tag_name, :materials, tag_name: :wiki_page)
|
||||
@@ -192,7 +189,7 @@ class TagsController < ApplicationController
|
||||
|
||||
def deerjikists_by_name
|
||||
name = params[:name].to_s.strip
|
||||
return head :bad_request if name.blank?
|
||||
return render_bad_request('name は必須です.') if name.blank?
|
||||
|
||||
tag = Tag.joins(:tag_name)
|
||||
.includes(:tag_name, tag_name: :wiki_page)
|
||||
@@ -214,21 +211,24 @@ class TagsController < ApplicationController
|
||||
|
||||
ApplicationRecord.transaction do
|
||||
tag.deerjikists = []
|
||||
params[:_json].each do
|
||||
platform = _1[:platform]
|
||||
code = normalise_deerjikist_code(platform, _1[:code])
|
||||
params[:_json].each.with_index do |item, i|
|
||||
platform = item[:platform]
|
||||
code = normalise_deerjikist_code(platform, item[:code])
|
||||
deerjikist = Deerjikist.find_or_initialize_by(platform:, code:)
|
||||
deerjikist.tag = tag
|
||||
deerjikist.save!
|
||||
render_deerjikist_form_record_invalid(deerjikist, i) unless deerjikist.save
|
||||
raise ActiveRecord::Rollback if performed?
|
||||
end
|
||||
end
|
||||
|
||||
return if performed?
|
||||
|
||||
render json: DeerjikistRepr.many(tag.reload.deerjikists)
|
||||
end
|
||||
|
||||
def materials_by_name
|
||||
name = params[:name].to_s.strip
|
||||
return head :bad_request if name.blank?
|
||||
return render_bad_request('name は必須です.') if name.blank?
|
||||
|
||||
tag = Tag.joins(:tag_name)
|
||||
.includes(:tag_name, :materials, tag_name: :wiki_page)
|
||||
@@ -247,21 +247,26 @@ class TagsController < ApplicationController
|
||||
|
||||
name = params[:name].to_s.strip
|
||||
category = params[:category].to_s.strip
|
||||
return head :unprocessable_entity if name.blank? || category.blank?
|
||||
return render_unprocessable_entity('名前は必須です.', field: :name) if name.blank?
|
||||
return render_unprocessable_entity('カテゴリは必須です.', field: :category) if category.blank?
|
||||
return render_unprocessable_entity '廃止状態は必須です.', field: :deprecated unless params.key?(:deprecated)
|
||||
|
||||
if name != tag.name &&
|
||||
tag.in?([Tag.tagme, Tag.bot, Tag.no_deerjikist, Tag.video, Tag.niconico])
|
||||
return render json: { error: 'システム・タグの名称は変更できません.' },
|
||||
status: :unprocessable_entity
|
||||
end
|
||||
|
||||
if tag.nico? || category == 'nico'
|
||||
return render json: { error: 'ニコタグは変更できません.' },
|
||||
status: :unprocessable_entity
|
||||
if (name != tag.name &&
|
||||
tag.in?([Tag.tagme, Tag.bot, Tag.no_deerjikist, Tag.video, Tag.niconico]))
|
||||
return render_unprocessable_entity 'システム・タグの名称は変更できません.', field: :name
|
||||
end
|
||||
|
||||
alias_names = params[:aliases].to_s.split.uniq
|
||||
parent_names = params[:parent_tags].to_s.split.uniq
|
||||
deprecated = bool?(:deprecated)
|
||||
|
||||
if tag.nico? && deprecated
|
||||
return render_unprocessable_entity 'ニコタグは廃止できません.', field: :deprecated
|
||||
end
|
||||
|
||||
if tag.nico? || category == 'nico'
|
||||
return render_unprocessable_entity 'ニコタグは変更できません.', field: :category
|
||||
end
|
||||
|
||||
ApplicationRecord.transaction do
|
||||
TagVersioning.ensure_snapshot!(tag, created_by_user: current_user)
|
||||
@@ -270,7 +275,11 @@ class TagsController < ApplicationController
|
||||
name_changed = name != old_name
|
||||
wiki_page = tag.tag_name.wiki_page if name_changed
|
||||
|
||||
tag.update!(category:)
|
||||
if tag.deprecated? == deprecated
|
||||
tag.update!(category:)
|
||||
else
|
||||
tag.update!(category:, deprecated_at: deprecated ? Time.current : nil)
|
||||
end
|
||||
tag.tag_name.update!(name:)
|
||||
|
||||
alias_names << old_name if name_changed
|
||||
@@ -298,12 +307,17 @@ class TagsController < ApplicationController
|
||||
|
||||
name = params[:name].presence
|
||||
category = params[:category].presence
|
||||
deprecated_given = params.key?(:deprecated)
|
||||
deprecated = bool?(:deprecated)
|
||||
|
||||
tag = Tag.find(params[:id])
|
||||
|
||||
if tag.nico? && deprecated_given && deprecated
|
||||
return render_unprocessable_entity 'ニコタグは廃止できません.', field: :deprecated
|
||||
end
|
||||
|
||||
if tag.nico? || (category.present? && category == 'nico')
|
||||
return render json: { error: 'ニコタグは変更できません.' },
|
||||
status: :unprocessable_entity
|
||||
return render_unprocessable_entity 'ニコタグは変更できません.', field: :category
|
||||
end
|
||||
|
||||
ApplicationRecord.transaction do
|
||||
@@ -315,6 +329,9 @@ class TagsController < ApplicationController
|
||||
|
||||
tag.tag_name.update!(name:) if name.present?
|
||||
tag.update!(category:) if category.present?
|
||||
if deprecated_given && tag.deprecated? != deprecated
|
||||
tag.update!(deprecated_at: deprecated ? Time.current : nil)
|
||||
end
|
||||
|
||||
tag.reload
|
||||
|
||||
@@ -331,6 +348,93 @@ class TagsController < ApplicationController
|
||||
|
||||
private
|
||||
|
||||
def build_with_depth_graph
|
||||
children_by_parent_id = Hash.new { |h, k| h[k] = [] }
|
||||
parent_ids_by_child_id = Hash.new { |h, k| h[k] = [] }
|
||||
|
||||
TagImplication.pluck(:parent_tag_id, :tag_id).each do |parent_id, child_id|
|
||||
children_by_parent_id[parent_id] << child_id
|
||||
parent_ids_by_child_id[child_id] << parent_id
|
||||
end
|
||||
|
||||
tag_ids = (children_by_parent_id.keys +
|
||||
parent_ids_by_child_id.keys +
|
||||
Tag.where(category: ['meme', 'character', 'material']).pluck(:id)).uniq
|
||||
|
||||
tags_by_id = Tag.where(id: tag_ids)
|
||||
.pluck(:id, :category, :deprecated_at)
|
||||
.each_with_object({ }) do |(id, category, deprecated_at), h|
|
||||
h[id] = { category:, deprecated: deprecated_at.present? }
|
||||
end
|
||||
|
||||
{ children_by_parent_id:, parent_ids_by_child_id:, tags_by_id:,
|
||||
visible_child_tag_ids_by_parent_id: { } }
|
||||
end
|
||||
|
||||
def visible_root_tag_ids graph
|
||||
graph[:tags_by_id].filter_map do |tag_id, attrs|
|
||||
next unless with_depth_visible_tag?(attrs)
|
||||
next unless visible_root_tag?(tag_id, graph)
|
||||
|
||||
tag_id
|
||||
end
|
||||
end
|
||||
|
||||
def visible_root_tag? tag_id, graph
|
||||
seen = Set.new([tag_id])
|
||||
stack = graph[:parent_ids_by_child_id][tag_id].dup
|
||||
|
||||
until stack.empty?
|
||||
parent_id = stack.pop
|
||||
next if seen.include?(parent_id)
|
||||
|
||||
seen << parent_id
|
||||
|
||||
parent = graph[:tags_by_id][parent_id]
|
||||
next unless parent
|
||||
|
||||
return false unless parent[:deprecated]
|
||||
|
||||
stack.concat(graph[:parent_ids_by_child_id][parent_id])
|
||||
end
|
||||
|
||||
true
|
||||
end
|
||||
|
||||
def visible_child_tag_ids parent_tag_id, graph
|
||||
cache = graph[:visible_child_tag_ids_by_parent_id]
|
||||
return cache[parent_tag_id] if cache.key?(parent_tag_id)
|
||||
|
||||
visible_ids = Set.new
|
||||
|
||||
graph[:children_by_parent_id][parent_tag_id].each do |child_tag_id|
|
||||
collect_visible_child_tag_ids(child_tag_id, graph, visible_ids, Set.new([parent_tag_id]))
|
||||
end
|
||||
|
||||
cache[parent_tag_id] = visible_ids.to_a
|
||||
end
|
||||
|
||||
def collect_visible_child_tag_ids tag_id, graph, visible_ids, seen
|
||||
return if seen.include?(tag_id)
|
||||
|
||||
seen = seen.dup << tag_id
|
||||
tag = graph[:tags_by_id][tag_id]
|
||||
return unless tag
|
||||
|
||||
if tag[:deprecated]
|
||||
graph[:children_by_parent_id][tag_id].each do |child_tag_id|
|
||||
collect_visible_child_tag_ids(child_tag_id, graph, visible_ids, seen)
|
||||
end
|
||||
return
|
||||
end
|
||||
|
||||
visible_ids << tag_id if with_depth_visible_tag?(tag)
|
||||
end
|
||||
|
||||
def with_depth_visible_tag? tag
|
||||
tag[:category].in?(['meme', 'character', 'material']) && !tag[:deprecated]
|
||||
end
|
||||
|
||||
def build_tag_children tag
|
||||
material = tag.materials.first
|
||||
file = nil
|
||||
@@ -437,4 +541,23 @@ class TagsController < ApplicationController
|
||||
rescue
|
||||
nil
|
||||
end
|
||||
|
||||
def render_deerjikist_form_record_invalid deerjikist, index
|
||||
fields = { }
|
||||
|
||||
deerjikist.errors.each do |error|
|
||||
field =
|
||||
case error.attribute
|
||||
when :platform, :code
|
||||
"deerjikists.#{ index }.#{ error.attribute }"
|
||||
else
|
||||
:deerjikists
|
||||
end
|
||||
|
||||
fields[field] ||= []
|
||||
fields[field] << error.full_message
|
||||
end
|
||||
|
||||
render_validation_error fields:
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,21 +1,28 @@
|
||||
class TheatreCommentsController < ApplicationController
|
||||
def index
|
||||
limit = params[:limit].to_i
|
||||
limit = 20 if limit <= 0
|
||||
|
||||
no_gt = params[:no_gt].to_i
|
||||
no_gt = 0 if no_gt.negative?
|
||||
no_gt = 0 if no_gt < 0
|
||||
|
||||
comments = TheatreComment
|
||||
.where(theatre_id: params[:theatre_id])
|
||||
.where('no > ?', no_gt)
|
||||
.order(no: :desc)
|
||||
.limit(limit)
|
||||
|
||||
render json: comments.as_json(include: { user: { only: [:id, :name] } })
|
||||
render json: comments.map {
|
||||
_1.as_json(include: { user: { only: [:id, :name] } })
|
||||
.merge(content: _1.discarded? ? nil : _1.content, deleted: _1.discarded?)
|
||||
}
|
||||
end
|
||||
|
||||
def create
|
||||
return head :unauthorized unless current_user
|
||||
|
||||
content = params[:content]
|
||||
return head :unprocessable_entity if content.blank?
|
||||
return render_unprocessable_entity('本文は必須です.', field: :content) if content.blank?
|
||||
|
||||
theatre = Theatre.find_by(id: params[:theatre_id])
|
||||
return head :not_found unless theatre
|
||||
@@ -29,4 +36,19 @@ class TheatreCommentsController < ApplicationController
|
||||
|
||||
render json: comment, status: :created
|
||||
end
|
||||
|
||||
def destroy
|
||||
return head :unauthorized unless current_user
|
||||
|
||||
theatre_id = params[:theatre_id].to_i
|
||||
no = params[:id].to_i
|
||||
|
||||
comment = TheatreComment.find_by(theatre_id:, no:)
|
||||
return head :not_found unless comment
|
||||
return head :forbidden unless comment.user == current_user
|
||||
|
||||
comment.discard!
|
||||
|
||||
head :no_content
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
class TheatreProgrammesController < ApplicationController
|
||||
def index
|
||||
limit = params[:limit].to_i
|
||||
limit = 100 if limit <= 0
|
||||
|
||||
position_gt = params[:position_gt].to_i
|
||||
position_gt = 0 if position_gt < 0
|
||||
|
||||
programmes = TheatreProgramme
|
||||
.where(theatre_id: params[:theatre_id])
|
||||
.where('position > ?', position_gt)
|
||||
.includes(:post)
|
||||
.order(position: :desc).limit(100)
|
||||
.limit(limit)
|
||||
|
||||
render json: programmes.map { |programme|
|
||||
programme.as_json.merge(post: { id: programme.post.id,
|
||||
title: programme.post.title,
|
||||
url: programme.post.url })
|
||||
}
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,22 @@
|
||||
class TheatreSkipEventsController < ApplicationController
|
||||
def index
|
||||
limit = params[:limit].to_i
|
||||
limit = 50 if limit <= 0
|
||||
|
||||
events =
|
||||
TheatreSkipEvent
|
||||
.where(theatre_id: params[:theatre_id])
|
||||
.includes(:post, tags: :tag_name)
|
||||
.order(created_at: :desc)
|
||||
.limit(limit)
|
||||
|
||||
render json: events.map { |event|
|
||||
{ id: event.id,
|
||||
theatre_id: event.theatre_id,
|
||||
post: { id: event.post.id, title: event.post.title, url: event.post.url },
|
||||
tags: event.tags.map { |tag| { id: tag.id, name: tag.name } },
|
||||
programme_position: event.programme_position,
|
||||
created_at: event.created_at }
|
||||
}
|
||||
end
|
||||
end
|
||||
@@ -31,9 +31,7 @@ class TheatresController < ApplicationController
|
||||
post_started_at = theatre.current_post_started_at
|
||||
end
|
||||
|
||||
render json: {
|
||||
host_flg:, post_id:, post_started_at:,
|
||||
watching_users: theatre.watching_users.as_json(only: [:id, :name]) }
|
||||
render json: theatre_info_json(theatre, host_flg:, post_id:, post_started_at:)
|
||||
end
|
||||
|
||||
def next_post
|
||||
@@ -43,12 +41,119 @@ class TheatresController < ApplicationController
|
||||
return head :not_found unless theatre
|
||||
return head :forbidden if theatre.host_user != current_user
|
||||
|
||||
post = Post.where("url LIKE '%nicovideo.jp%'")
|
||||
.or(Post.where("url LIKE '%youtube.com%'"))
|
||||
.order('RAND()')
|
||||
.first
|
||||
theatre.update!(current_post: post, current_post_started_at: Time.current)
|
||||
ApplicationRecord.transaction do
|
||||
theatre.lock!
|
||||
TheatrePostAdvancer.call(theatre:)
|
||||
end
|
||||
|
||||
head :no_content
|
||||
end
|
||||
|
||||
def skip_vote
|
||||
return head :unauthorized unless current_user
|
||||
|
||||
theatre = Theatre.find_by(id: params[:id])
|
||||
return head :not_found unless theatre
|
||||
requested_post_id = params[:post_id].to_i
|
||||
return head :unprocessable_entity if requested_post_id <= 0
|
||||
|
||||
skipped = false
|
||||
conflicted = false
|
||||
|
||||
ApplicationRecord.transaction do
|
||||
theatre.lock!
|
||||
if theatre.current_post
|
||||
TheatreWatchingUser.find_or_initialize_by(theatre:, user: current_user).tap {
|
||||
_1.expires_at = 30.seconds.from_now
|
||||
}.save!
|
||||
|
||||
if theatre.current_post_id != requested_post_id
|
||||
conflicted = true
|
||||
next
|
||||
end
|
||||
|
||||
TheatreSkipVote.find_or_create_by!(theatre:, post_id: requested_post_id, user: current_user)
|
||||
|
||||
vote_status = skip_vote_status(theatre)
|
||||
if vote_status[:votes_count] >= vote_status[:required_count]
|
||||
TheatreSkipFinalizer.call(theatre:, user: current_user)
|
||||
TheatrePostAdvancer.call(theatre:)
|
||||
skipped = true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
theatre.reload
|
||||
return render json: theatre_info_json(theatre, skipped: false), status: :conflict if conflicted
|
||||
|
||||
render json: theatre_info_json(theatre, skipped:)
|
||||
end
|
||||
|
||||
def unskip_vote
|
||||
return head :unauthorized unless current_user
|
||||
|
||||
theatre = Theatre.find_by(id: params[:id])
|
||||
return head :not_found unless theatre
|
||||
requested_post_id = params[:post_id].to_i
|
||||
return head :unprocessable_entity if requested_post_id <= 0
|
||||
|
||||
conflicted = false
|
||||
|
||||
theatre.with_lock do
|
||||
if theatre.current_post
|
||||
if theatre.current_post_id != requested_post_id
|
||||
conflicted = true
|
||||
else
|
||||
TheatreSkipVote.where(theatre:, post_id: requested_post_id, user: current_user).delete_all
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
theatre.reload
|
||||
return render json: theatre_info_json(theatre, skipped: false), status: :conflict if conflicted
|
||||
|
||||
render json: theatre_info_json(theatre, skipped: false)
|
||||
end
|
||||
|
||||
def post_selection_weights
|
||||
theatre = Theatre.find_by(id: params[:id])
|
||||
return head :not_found unless theatre
|
||||
|
||||
render json: TheatrePostSelector.new(theatre:).weight_json
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def theatre_info_json(theatre, host_flg: nil, post_id: nil, post_started_at: nil, skipped: nil)
|
||||
host_flg = theatre.host_user_id == current_user&.id if host_flg.nil?
|
||||
post_id = theatre.current_post_id if post_id.nil?
|
||||
post_started_at = theatre.current_post_started_at if post_started_at.nil?
|
||||
|
||||
json = { host_flg:,
|
||||
post_id:,
|
||||
post_started_at:,
|
||||
post_elapsed_ms: post_started_at ? ((Time.current - post_started_at) * 1000).floor : nil,
|
||||
watching_users: theatre.watching_users.as_json(only: [:id, :name]),
|
||||
skip_vote: skip_vote_status(theatre) }
|
||||
json[:skipped] = skipped unless skipped.nil?
|
||||
json
|
||||
end
|
||||
|
||||
def skip_vote_status(theatre)
|
||||
watching_user_ids = theatre.watching_users.ids
|
||||
watching_users_count = watching_user_ids.size
|
||||
required_count = (watching_users_count / 2) + 1
|
||||
post = theatre.current_post
|
||||
votes =
|
||||
if post
|
||||
TheatreSkipVote.where(theatre:, post:, user_id: watching_user_ids)
|
||||
else
|
||||
TheatreSkipVote.none
|
||||
end
|
||||
|
||||
{ votes_count: post ? votes.count : 0,
|
||||
required_count:,
|
||||
watching_users_count:,
|
||||
voted: post && current_user ? votes.exists?(user_id: current_user.id) : false }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -42,12 +42,12 @@ class UsersController < ApplicationController
|
||||
return head :unauthorized if user&.id != params[:id].to_i
|
||||
|
||||
name = params[:name]
|
||||
return head :bad_request if name.blank?
|
||||
return render_unprocessable_entity('名前は必須です.', field: :name) if name.blank?
|
||||
|
||||
if user.update(name:)
|
||||
render json: user.slice(:id, :name, :inheritance_code, :role), status: :ok
|
||||
else
|
||||
render json: user.errors, status: :unprocessable_entity
|
||||
render_validation_error user
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -4,17 +4,18 @@ class WikiPagesController < ApplicationController
|
||||
def index
|
||||
title = params[:title].to_s.strip
|
||||
if title.blank?
|
||||
return render json: WikiPageRepr.base(WikiPage.joins(:tag_name).includes(:tag_name))
|
||||
return render json: WikiPageRepr.base(
|
||||
WikiPage.joins(:tag_name).includes(tag_name: :tag))
|
||||
end
|
||||
|
||||
q = WikiPage.joins(:tag_name).includes(:tag_name)
|
||||
q = WikiPage.joins(:tag_name).includes(tag_name: :tag)
|
||||
.where('tag_names.name LIKE ?', "%#{ WikiPage.sanitize_sql_like(title) }%")
|
||||
render json: WikiPageRepr.base(q.limit(20))
|
||||
end
|
||||
|
||||
def show
|
||||
page = WikiPage.joins(:tag_name)
|
||||
.includes(:tag_name)
|
||||
.includes(tag_name: :tag)
|
||||
.find_by(id: params[:id])
|
||||
render_wiki_page_or_404 page
|
||||
end
|
||||
@@ -22,7 +23,7 @@ class WikiPagesController < ApplicationController
|
||||
def show_by_title
|
||||
title = params[:title].to_s.strip
|
||||
page = WikiPage.joins(:tag_name)
|
||||
.includes(:tag_name)
|
||||
.includes(tag_name: :tag)
|
||||
.find_by(tag_name: { name: title })
|
||||
render_wiki_page_or_404 page
|
||||
end
|
||||
@@ -46,17 +47,17 @@ class WikiPagesController < ApplicationController
|
||||
|
||||
def diff
|
||||
id = params[:id]
|
||||
return head :bad_request if id.blank?
|
||||
return render_bad_request('id は必須です.') if id.blank?
|
||||
|
||||
from = params[:from].presence
|
||||
to = params[:to].presence
|
||||
|
||||
page = WikiPage.joins(:tag_name).includes(:tag_name).find(id)
|
||||
page = WikiPage.joins(:tag_name).includes(tag_name: :tag).find(id)
|
||||
|
||||
from_rev = from && page.wiki_revisions.find(from)
|
||||
to_rev = to ? page.wiki_revisions.find(to) : page.current_revision
|
||||
if ((from_rev && !(from_rev.content?)) || !(to_rev&.content?))
|
||||
return head :unprocessable_entity
|
||||
return render_unprocessable_entity('差分を表示できない版です.')
|
||||
end
|
||||
|
||||
diffs = Diff::LCS.sdiff(from_rev&.body&.lines || [], to_rev.body.lines)
|
||||
@@ -76,6 +77,7 @@ class WikiPagesController < ApplicationController
|
||||
|
||||
render json: { wiki_page_id: page.id,
|
||||
title: page.title,
|
||||
deprecated_at: page.deprecated_at,
|
||||
older_revision_id: from_rev&.id,
|
||||
newer_revision_id: to_rev.id,
|
||||
diff: diff_json }
|
||||
@@ -89,7 +91,8 @@ class WikiPagesController < ApplicationController
|
||||
body = params[:body].to_s
|
||||
message = params[:message].presence
|
||||
|
||||
return head :unprocessable_entity if title.blank? || body.blank?
|
||||
return render_unprocessable_entity('タイトルは必須です.', field: :title) if title.blank?
|
||||
return render_unprocessable_entity('本文は必須です.', field: :body) if body.blank?
|
||||
|
||||
tag_name = TagName.find_undiscard_or_create_by!(name: title)
|
||||
|
||||
@@ -101,8 +104,10 @@ class WikiPagesController < ApplicationController
|
||||
message:)
|
||||
|
||||
render json: WikiPageRepr.base(page), status: :created
|
||||
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique
|
||||
head :unprocessable_entity
|
||||
rescue ActiveRecord::RecordInvalid => e
|
||||
render_validation_error e.record
|
||||
rescue ActiveRecord::RecordNotUnique
|
||||
render_record_not_unique
|
||||
end
|
||||
|
||||
def update
|
||||
@@ -112,7 +117,8 @@ class WikiPagesController < ApplicationController
|
||||
title = params[:title]&.strip
|
||||
body = params[:body].to_s
|
||||
|
||||
return head :unprocessable_entity if title.blank? || body.blank?
|
||||
return render_unprocessable_entity('タイトルは必須です.', field: :title) if title.blank?
|
||||
return render_unprocessable_entity('本文は必須です.', field: :body) if body.blank?
|
||||
|
||||
page = WikiPage.find(params[:id])
|
||||
base_revision_id = params[:base_revision_id].presence
|
||||
@@ -153,7 +159,7 @@ class WikiPagesController < ApplicationController
|
||||
def changes
|
||||
id = params[:id].presence
|
||||
q = WikiRevision.joins(wiki_page: :tag_name)
|
||||
.includes(:created_user, wiki_page: :tag_name)
|
||||
.includes(:created_user, wiki_page: { tag_name: :tag })
|
||||
.order(id: :desc)
|
||||
q = q.where(wiki_page_id: id) if id
|
||||
|
||||
@@ -161,7 +167,9 @@ class WikiPagesController < ApplicationController
|
||||
{ revision_id: rev.id,
|
||||
pred: rev.base_revision_id,
|
||||
succ: nil,
|
||||
wiki_page: { id: rev.wiki_page_id, title: rev.wiki_page.title },
|
||||
wiki_page: { id: rev.wiki_page_id,
|
||||
title: rev.wiki_page.title,
|
||||
deprecated_at: rev.wiki_page.deprecated_at },
|
||||
user: rev.created_user && { id: rev.created_user.id, name: rev.created_user.name },
|
||||
kind: rev.kind,
|
||||
message: rev.message,
|
||||
|
||||
新しい課題から参照
ユーザをブロックする