このコミットが含まれているのは:
@@ -7,13 +7,24 @@ class GekanatorPostsController < ApplicationController
|
|||||||
.order(Arel.sql(
|
.order(Arel.sql(
|
||||||
'COALESCE(posts.original_created_before - INTERVAL 1 MINUTE, ' \
|
'COALESCE(posts.original_created_before - INTERVAL 1 MINUTE, ' \
|
||||||
'posts.original_created_from, posts.created_at) DESC, posts.id DESC'))
|
'posts.original_created_from, posts.created_at) DESC, posts.id DESC'))
|
||||||
|
.to_a
|
||||||
|
|
||||||
render json: { posts: posts.map { |post| post_json(post) } }
|
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
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def post_json post
|
def post_json post, active_tags_by_post_id:
|
||||||
{
|
{
|
||||||
id: post.id,
|
id: post.id,
|
||||||
url: post.url,
|
url: post.url,
|
||||||
@@ -22,14 +33,24 @@ class GekanatorPostsController < ApplicationController
|
|||||||
thumbnail_base: post.thumbnail_base,
|
thumbnail_base: post.thumbnail_base,
|
||||||
original_created_from: post.original_created_from,
|
original_created_from: post.original_created_from,
|
||||||
original_created_before: post.original_created_before,
|
original_created_before: post.original_created_before,
|
||||||
post_similarity_edges: post.post_similarities.map { |similarity|
|
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,
|
target_post_id: similarity.target_post_id,
|
||||||
cos: similarity.cos.to_f
|
cos: similarity.cos.to_f
|
||||||
}
|
}
|
||||||
},
|
end
|
||||||
tags: post.tags.map { |tag| tag_json(tag) }
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def tag_json tag
|
def tag_json tag
|
||||||
|
|||||||
@@ -5,9 +5,16 @@ class GekanatorQuestionsController < ApplicationController
|
|||||||
.accepted
|
.accepted
|
||||||
.includes(:gekanator_question_examples)
|
.includes(:gekanator_question_examples)
|
||||||
.order(priority_weight: :desc, id: :asc)
|
.order(priority_weight: :desc, id: :asc)
|
||||||
|
.to_a
|
||||||
|
deprecated_tag_keys = deprecated_tag_keys_for(questions)
|
||||||
|
|
||||||
render json: {
|
render json: {
|
||||||
questions: questions.map { |question| question_json(question) }
|
questions: questions.filter_map { |question|
|
||||||
|
json = question_json(question)
|
||||||
|
next if hidden_question?(json[:condition], deprecated_tag_keys)
|
||||||
|
|
||||||
|
json
|
||||||
|
}
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -100,4 +107,41 @@ class GekanatorQuestionsController < ApplicationController
|
|||||||
.first
|
.first
|
||||||
&.first
|
&.first
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ class Post < ApplicationRecord
|
|||||||
has_many :active_post_tags, -> { kept }, class_name: 'PostTag', inverse_of: :post
|
has_many :active_post_tags, -> { kept }, class_name: 'PostTag', inverse_of: :post
|
||||||
has_many :post_tags_with_discarded, -> { with_discarded }, class_name: 'PostTag'
|
has_many :post_tags_with_discarded, -> { with_discarded }, class_name: 'PostTag'
|
||||||
has_many :tags, through: :active_post_tags
|
has_many :tags, through: :active_post_tags
|
||||||
|
has_many :active_tags, -> { where(deprecated_at: nil) }, through: :active_post_tags, source: :tag
|
||||||
|
|
||||||
has_many :user_post_views, dependent: :delete_all
|
has_many :user_post_views, dependent: :delete_all
|
||||||
has_many :post_similarities, dependent: :delete_all
|
has_many :post_similarities, dependent: :delete_all
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
module Similarity
|
module Similarity
|
||||||
class Calc
|
class Calc
|
||||||
def self.call model, tgt
|
def self.call model, tgt, scope: nil
|
||||||
similarity_model = "#{ model.name }Similarity".constantize
|
similarity_model = "#{ model.name }Similarity".constantize
|
||||||
|
|
||||||
# 最大保存件数
|
# 最大保存件数
|
||||||
@@ -8,7 +8,8 @@ module Similarity
|
|||||||
|
|
||||||
similarity_model.delete_all
|
similarity_model.delete_all
|
||||||
|
|
||||||
posts = model.includes(tgt).select(:id).to_a
|
scope ||= model.all
|
||||||
|
posts = scope.includes(tgt).select(:id).to_a
|
||||||
|
|
||||||
tag_ids = { }
|
tag_ids = { }
|
||||||
tag_cnts = { }
|
tag_cnts = { }
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
namespace :post_similarity do
|
namespace :post_similarity do
|
||||||
desc '関聯投稿テーブル作成'
|
desc '関聯投稿テーブル作成'
|
||||||
task calc: :environment do
|
task calc: :environment do
|
||||||
Similarity::Calc.call(Post, :tags)
|
Similarity::Calc.call(Post, :active_tags)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
namespace :tag_similarity do
|
namespace :tag_similarity do
|
||||||
desc '関聯タグ・テーブル作成'
|
desc '関聯タグ・テーブル作成'
|
||||||
task calc: :environment do
|
task calc: :environment do
|
||||||
Similarity::Calc.call(Tag, :posts)
|
Similarity::Calc.call(Tag, :posts, scope: Tag.where(deprecated_at: nil))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
新しい課題から参照
ユーザをブロックする