Reviewed-on: #357 Co-authored-by: miteruzo <miteruzo@naver.com> Co-committed-by: miteruzo <miteruzo@naver.com>
このコミットはPull リクエスト #357 でマージされました.
このコミットが含まれているのは:
@@ -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(:uploaded_user, 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,7 +96,7 @@ class PostsController < ApplicationController
|
||||
end
|
||||
|
||||
def random
|
||||
post = filtered_posts.preload(:uploaded_user,
|
||||
post = filtered_posts.preload(:uploaded_user, :parents, :children,
|
||||
tags: [:deerjikists, :materials, { tag_name: :wiki_page }])
|
||||
.with_attached_thumbnail
|
||||
.order('RAND()')
|
||||
@@ -108,7 +109,8 @@ class PostsController < ApplicationController
|
||||
def show
|
||||
post =
|
||||
Post
|
||||
.includes(:uploaded_user, tags: [:deerjikists, :materials, { tag_name: :wiki_page }])
|
||||
.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
|
||||
|
||||
@@ -1,14 +1,21 @@
|
||||
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
|
||||
@@ -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
|
||||
|
||||
@@ -81,7 +81,7 @@ class Tag < ApplicationRecord
|
||||
|
||||
def material_id = materials.first&.id
|
||||
|
||||
def has_deerjikists = deerjikists.present?
|
||||
def has_deerjikists = deerjikists.loaded? ? deerjikists.any? : deerjikists.exists?
|
||||
|
||||
def self.tagme = find_or_create_by_tag_name!('タグ希望', category: :meta)
|
||||
def self.bot = find_or_create_by_tag_name!('bot操作', category: :meta)
|
||||
|
||||
@@ -7,6 +7,10 @@ class Theatre < ApplicationRecord
|
||||
class_name: 'TheatreWatchingUser', inverse_of: :theatre
|
||||
has_many :watching_users, through: :active_theatre_watching_users, source: :user
|
||||
|
||||
has_many :programmes, class_name: 'TheatreProgramme'
|
||||
has_many :skip_votes, class_name: 'TheatreSkipVote', dependent: :delete_all
|
||||
has_many :skip_events, class_name: 'TheatreSkipEvent', dependent: :delete_all
|
||||
|
||||
belongs_to :host_user, class_name: 'User', optional: true
|
||||
belongs_to :current_post, class_name: 'Post', optional: true
|
||||
belongs_to :created_by_user, class_name: 'User'
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
class TheatreProgramme < ApplicationRecord
|
||||
self.primary_key = :theatre_id, :position
|
||||
|
||||
belongs_to :theatre
|
||||
belongs_to :post
|
||||
end
|
||||
@@ -0,0 +1,10 @@
|
||||
class TheatreSkipEvent < ApplicationRecord
|
||||
belongs_to :theatre
|
||||
belongs_to :post
|
||||
belongs_to :skipped_by_user, class_name: 'User'
|
||||
|
||||
has_many :voters, class_name: 'TheatreSkipEventVoter', dependent: :delete_all
|
||||
has_many :event_tags, class_name: 'TheatreSkipEventTag', dependent: :delete_all
|
||||
has_many :users, through: :voters
|
||||
has_many :tags, through: :event_tags
|
||||
end
|
||||
@@ -0,0 +1,6 @@
|
||||
class TheatreSkipEventTag < ApplicationRecord
|
||||
self.primary_key = :theatre_skip_event_id, :tag_id
|
||||
|
||||
belongs_to :theatre_skip_event
|
||||
belongs_to :tag
|
||||
end
|
||||
@@ -0,0 +1,6 @@
|
||||
class TheatreSkipEventVoter < ApplicationRecord
|
||||
self.primary_key = :theatre_skip_event_id, :user_id
|
||||
|
||||
belongs_to :theatre_skip_event
|
||||
belongs_to :user
|
||||
end
|
||||
@@ -0,0 +1,7 @@
|
||||
class TheatreSkipVote < ApplicationRecord
|
||||
self.primary_key = :theatre_id, :post_id, :user_id
|
||||
|
||||
belongs_to :theatre
|
||||
belongs_to :post
|
||||
belongs_to :user
|
||||
end
|
||||
@@ -0,0 +1,29 @@
|
||||
class TheatrePostAdvancer
|
||||
def self.call(theatre:)
|
||||
new(theatre:).call
|
||||
end
|
||||
|
||||
def initialize(theatre:)
|
||||
@theatre = theatre
|
||||
end
|
||||
|
||||
def call
|
||||
previous_post = theatre.current_post
|
||||
post = TheatrePostSelector.new(theatre:).select
|
||||
|
||||
TheatreSkipVote.where(theatre:, post: previous_post).delete_all if previous_post
|
||||
|
||||
theatre.update!(current_post: post, current_post_started_at: post ? Time.current : nil)
|
||||
|
||||
if post
|
||||
position = (theatre.programmes.maximum(:position) || 0) + 1
|
||||
theatre.programmes.create!(position:, post:)
|
||||
end
|
||||
|
||||
post
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :theatre
|
||||
end
|
||||
@@ -0,0 +1,119 @@
|
||||
class TheatrePostSelector
|
||||
Candidate = Struct.new(:post, :weight, :penalty, :tags, keyword_init: true)
|
||||
ELIGIBLE_POST_URL_CONDITION =
|
||||
["url LIKE '%nicovideo.jp%'",
|
||||
"url LIKE '%youtube.com/watch%'",
|
||||
"url LIKE '%youtu.be/%'"]
|
||||
.join(' OR ')
|
||||
|
||||
def initialize theatre:
|
||||
@theatre = theatre
|
||||
end
|
||||
|
||||
def select
|
||||
candidates = weighted_candidates
|
||||
return nil if candidates.empty?
|
||||
|
||||
total = candidates.sum(&:weight)
|
||||
target = rand * total
|
||||
|
||||
candidates.each do |candidate|
|
||||
target -= candidate.weight
|
||||
return candidate.post if target <= 0
|
||||
end
|
||||
|
||||
candidates.last.post
|
||||
end
|
||||
|
||||
def weight_json limit: 20
|
||||
candidates = weighted_candidates
|
||||
sorted = candidates.sort_by { |candidate| [candidate.weight, candidate.post.id] }
|
||||
|
||||
{ tag_penalties: tag_penalty_json,
|
||||
lightest_posts: post_weight_json(sorted.first(limit)),
|
||||
heaviest_posts: post_weight_json(sorted.reverse.first(limit)) }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :theatre
|
||||
|
||||
def weighted_candidates
|
||||
@weighted_candidates ||= begin
|
||||
penalties = tag_penalties
|
||||
posts = eligible_posts.includes(tags: :tag_name).to_a
|
||||
|
||||
posts.map do |post|
|
||||
post_tags = post.tags.to_a
|
||||
penalty = post_tags.sum { |tag| penalties[tag.id].to_i }
|
||||
|
||||
Candidate.new(
|
||||
post:,
|
||||
penalty:,
|
||||
tags: post_tags,
|
||||
weight: 1.0 / (1.0 + penalty))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def eligible_posts
|
||||
posts = Post.where(ELIGIBLE_POST_URL_CONDITION)
|
||||
posts = posts.where.not(id: theatre.current_post_id) if theatre.current_post_id
|
||||
posts
|
||||
end
|
||||
|
||||
def active_user_ids
|
||||
@active_user_ids ||= theatre.watching_users.ids
|
||||
end
|
||||
|
||||
def tag_penalties
|
||||
@tag_penalties ||=
|
||||
if active_user_ids.empty?
|
||||
{}
|
||||
else
|
||||
TheatreSkipEventVoter
|
||||
.joins(theatre_skip_event: :event_tags)
|
||||
.where(user_id: active_user_ids)
|
||||
.group('theatre_skip_event_tags.tag_id')
|
||||
.count
|
||||
end
|
||||
end
|
||||
|
||||
def tag_penalty_json
|
||||
return [] if tag_penalties.empty?
|
||||
|
||||
tags = Tag.where(id: tag_penalties.keys).includes(:tag_name).index_by(&:id)
|
||||
|
||||
tag_penalties
|
||||
.map { |tag_id, penalty|
|
||||
tag = tags[tag_id]
|
||||
next unless tag
|
||||
|
||||
{ tag: light_tag_json(tag),
|
||||
penalty: }
|
||||
}
|
||||
.compact
|
||||
.sort_by { |row| [-row[:penalty], row[:tag][:name].to_s] }
|
||||
end
|
||||
|
||||
def post_weight_json candidates
|
||||
candidates.map { |candidate|
|
||||
{ post: light_post_json(candidate.post),
|
||||
weight: candidate.weight,
|
||||
penalty: candidate.penalty,
|
||||
tags: candidate.tags.map { |tag| light_tag_json(tag) } }
|
||||
}
|
||||
end
|
||||
|
||||
def light_post_json post
|
||||
{ id: post.id,
|
||||
title: post.title,
|
||||
url: post.url }
|
||||
end
|
||||
|
||||
def light_tag_json tag
|
||||
{ id: tag.id,
|
||||
name: tag.name,
|
||||
category: tag.category }
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,40 @@
|
||||
class TheatreSkipFinalizer
|
||||
def self.call(theatre:, user:)
|
||||
new(theatre:, user:).call
|
||||
end
|
||||
|
||||
def initialize(theatre:, user:)
|
||||
@theatre = theatre
|
||||
@user = user
|
||||
end
|
||||
|
||||
def call
|
||||
return unless theatre.current_post
|
||||
|
||||
post = theatre.current_post
|
||||
voters = TheatreSkipVote.where(theatre:, post:).includes(:user).map(&:user)
|
||||
return if voters.empty?
|
||||
|
||||
event = TheatreSkipEvent.create!(
|
||||
theatre:,
|
||||
post:,
|
||||
skipped_by_user: user,
|
||||
programme_position: theatre.programmes.maximum(:position))
|
||||
|
||||
voters.uniq(&:id).each do |voter|
|
||||
TheatreSkipEventVoter.create!(theatre_skip_event: event, user: voter)
|
||||
end
|
||||
|
||||
post.tags.find_each do |tag|
|
||||
TheatreSkipEventTag.create!(theatre_skip_event: event, tag:)
|
||||
end
|
||||
|
||||
TheatreSkipVote.where(theatre:, post:).delete_all
|
||||
|
||||
event
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :theatre, :user
|
||||
end
|
||||
新しい課題から参照
ユーザをブロックする