このコミットが含まれているのは:
@@ -20,4 +20,91 @@ class GekanatorGamesController < ApplicationController
|
||||
render json: { errors: game.errors.full_messages }, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def extra_questions
|
||||
return head :not_found unless current_user&.admin?
|
||||
|
||||
game = GekanatorGame.find_by(id: params[:id])
|
||||
return head :not_found unless game
|
||||
|
||||
asked_ids = Array(game.answers).filter_map { |answer|
|
||||
answer['questionId'] || answer[:questionId]
|
||||
}
|
||||
existing_example_ids =
|
||||
GekanatorQuestionExample.where(post_id: game.correct_post_id)
|
||||
.select(:gekanator_question_id)
|
||||
questions =
|
||||
GekanatorQuestion
|
||||
.accepted
|
||||
.where(kind: 'post_similarity', source: 'user_suggested')
|
||||
.where.not(id: existing_example_ids)
|
||||
.order(priority_weight: :desc, id: :asc)
|
||||
|
||||
render json: {
|
||||
questions: questions.filter_map { |question|
|
||||
json = extra_question_json(question)
|
||||
next if asked_ids.include?(json[:id].to_s)
|
||||
next if asked_ids.include?("post-similarity:#{ json[:id] }")
|
||||
|
||||
json
|
||||
}.first(2)
|
||||
}
|
||||
end
|
||||
|
||||
def extra_question_answers
|
||||
return head :not_found unless current_user&.admin?
|
||||
|
||||
game = GekanatorGame.find_by(id: params[:id])
|
||||
return head :not_found unless game
|
||||
|
||||
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),
|
||||
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.assign_attributes(
|
||||
gekanator_game: game,
|
||||
answer: item[:answer],
|
||||
source: 'post_game_extra',
|
||||
weight: 1.0)
|
||||
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
|
||||
end
|
||||
|
||||
@@ -11,7 +11,14 @@ class GekanatorQuestionSuggestionsController < ApplicationController
|
||||
question_text: params.require(:question_text),
|
||||
answer: params.require(:answer))
|
||||
|
||||
if suggestion.save
|
||||
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
|
||||
|
||||
@@ -2,7 +2,11 @@ class GekanatorQuestionsController < ApplicationController
|
||||
def index
|
||||
return head :not_found unless current_user&.admin?
|
||||
|
||||
questions = GekanatorQuestion.accepted.order(priority_weight: :desc, id: :asc)
|
||||
questions =
|
||||
GekanatorQuestion
|
||||
.accepted
|
||||
.includes(:gekanator_question_examples)
|
||||
.order(priority_weight: :desc, id: :asc)
|
||||
|
||||
render json: {
|
||||
questions: questions.map { |question| question_json(question) }
|
||||
@@ -12,7 +16,7 @@ class GekanatorQuestionsController < ApplicationController
|
||||
private
|
||||
|
||||
def question_json question
|
||||
{
|
||||
json = {
|
||||
id: question_id_for(question),
|
||||
text: question.text,
|
||||
kind: question.kind,
|
||||
@@ -20,6 +24,10 @@ class GekanatorQuestionsController < ApplicationController
|
||||
source: question.source,
|
||||
priority_weight: question.priority_weight
|
||||
}
|
||||
if question.kind == 'post_similarity'
|
||||
json[:example_answers] = example_answers_json(question)
|
||||
end
|
||||
json
|
||||
end
|
||||
|
||||
def question_id_for question
|
||||
@@ -40,6 +48,8 @@ class GekanatorQuestionsController < ApplicationController
|
||||
"title:length-greater-than:#{ condition[:length] }"
|
||||
when 'title-has-ascii'
|
||||
'title:ascii'
|
||||
when 'post-similarity'
|
||||
"post-similarity:#{ question.id }"
|
||||
else
|
||||
"catalog:#{ question.id }"
|
||||
end
|
||||
@@ -54,4 +64,20 @@ class GekanatorQuestionsController < ApplicationController
|
||||
|
||||
json
|
||||
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
|
||||
end
|
||||
|
||||
@@ -5,6 +5,9 @@ class GekanatorGame < ApplicationRecord
|
||||
has_many :question_suggestions,
|
||||
class_name: 'GekanatorQuestionSuggestion',
|
||||
dependent: :delete_all
|
||||
has_many :question_examples,
|
||||
class_name: 'GekanatorQuestionExample',
|
||||
dependent: :delete_all
|
||||
|
||||
validates :answers, presence: true
|
||||
validates :question_count, numericality: { greater_than_or_equal_to: 0 }
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
class GekanatorQuestion < ApplicationRecord
|
||||
KINDS = ['tag', 'source', 'title', 'original_date'].freeze
|
||||
KINDS = ['tag', 'source', 'title', 'original_date', 'post_similarity'].freeze
|
||||
SOURCES = ['user_suggested', 'ai_generated', 'admin_curated'].freeze
|
||||
STATUSES = ['pending', 'accepted', 'rejected'].freeze
|
||||
STATUSES = ['pending', 'accepted', 'rejected', 'disabled'].freeze
|
||||
|
||||
belongs_to :gekanator_question_suggestion, optional: true
|
||||
belongs_to :created_by, class_name: 'User', optional: true
|
||||
has_many :gekanator_question_examples, dependent: :delete_all
|
||||
|
||||
validates :kind, presence: true, inclusion: { in: KINDS }
|
||||
validates :source, presence: true, inclusion: { in: SOURCES }
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
class GekanatorQuestionExample < ApplicationRecord
|
||||
ANSWERS = GekanatorQuestionSuggestion::ANSWERS
|
||||
SOURCES = ['initial_suggestion', 'post_game_extra'].freeze
|
||||
|
||||
belongs_to :gekanator_question
|
||||
belongs_to :post
|
||||
belongs_to :user
|
||||
belongs_to :gekanator_game, optional: true
|
||||
|
||||
validates :answer, presence: true, inclusion: { in: ANSWERS }
|
||||
validates :source, presence: true, inclusion: { in: SOURCES }
|
||||
validates :weight,
|
||||
presence: true,
|
||||
numericality: {
|
||||
greater_than: 0
|
||||
}
|
||||
end
|
||||
@@ -21,6 +21,7 @@ class Post < ApplicationRecord
|
||||
foreign_key: :correct_post_id,
|
||||
dependent: :delete_all,
|
||||
inverse_of: :correct_post
|
||||
has_many :gekanator_question_examples, dependent: :delete_all
|
||||
|
||||
has_many :parent_post_implications,
|
||||
class_name: 'PostImplication',
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
module Gekanator
|
||||
class QuestionSuggestionPromoter
|
||||
def self.call(...) = new(...).call
|
||||
|
||||
def initialize suggestion:, user:
|
||||
@suggestion = suggestion
|
||||
@user = user
|
||||
end
|
||||
|
||||
def call
|
||||
suggestion.with_lock do
|
||||
return promoted_question if suggestion.processed?
|
||||
|
||||
question = GekanatorQuestion.create!(
|
||||
text: suggestion.question_text,
|
||||
kind: 'post_similarity',
|
||||
source: 'user_suggested',
|
||||
status: 'accepted',
|
||||
priority_weight: 1.2,
|
||||
condition: {
|
||||
type: 'post-similarity',
|
||||
postId: suggestion.gekanator_game.correct_post_id,
|
||||
answer: suggestion.answer,
|
||||
threshold: 0.65
|
||||
},
|
||||
gekanator_question_suggestion: suggestion,
|
||||
created_by: user)
|
||||
GekanatorQuestionExample.create!(
|
||||
gekanator_question: question,
|
||||
post: suggestion.gekanator_game.correct_post,
|
||||
user: user,
|
||||
gekanator_game: suggestion.gekanator_game,
|
||||
answer: suggestion.answer,
|
||||
source: 'initial_suggestion')
|
||||
suggestion.update!(processed: true)
|
||||
question
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :suggestion, :user
|
||||
|
||||
def promoted_question
|
||||
suggestion.gekanator_questions.order(id: :desc).first
|
||||
end
|
||||
end
|
||||
end
|
||||
新しい課題から参照
ユーザをブロックする