このコミットが含まれているのは:
@@ -12,6 +12,8 @@ class TheatreProgrammesController < ApplicationController
|
||||
.order(position: :desc).limit(100)
|
||||
.limit(limit)
|
||||
|
||||
render json: programmes.as_json(include: { post: PostRepr::BASE })
|
||||
render json: programmes.map { |programme|
|
||||
programme.as_json.merge(post: PostRepr.base(programme.post))
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
class TheatreSkipEventsController < ApplicationController
|
||||
def index
|
||||
limit = params[:limit].to_i
|
||||
limit = 50 if limit <= 0
|
||||
|
||||
events =
|
||||
TheatreSkipEvent
|
||||
.where(theatre_id: params[:theatre_id])
|
||||
.includes(:skipped_by_user, :users, :tags, post: { tags: :tag_name })
|
||||
.order(created_at: :desc)
|
||||
.limit(limit)
|
||||
|
||||
render json: events.map { |event|
|
||||
{ id: event.id,
|
||||
theatre_id: event.theatre_id,
|
||||
post: PostRepr.base(event.post),
|
||||
skipped_by_user: UserRepr.base(event.skipped_by_user),
|
||||
voters: event.users.map { |user| UserRepr.base(user) },
|
||||
tags: event.tags.map { |tag| TagRepr.inline(tag) },
|
||||
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
|
||||
@@ -44,14 +42,95 @@ class TheatresController < ApplicationController
|
||||
return head :forbidden if theatre.host_user != current_user
|
||||
|
||||
ApplicationRecord.transaction do
|
||||
post = Post.where("url LIKE '%nicovideo.jp%'")
|
||||
.order('RAND()')
|
||||
.first
|
||||
theatre.update!(current_post: post, current_post_started_at: Time.current)
|
||||
position = (theatre.programmes.maximum(:position) || 0) + 1
|
||||
theatre.programmes.create!(position:, post:)
|
||||
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
|
||||
|
||||
skipped = 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!
|
||||
|
||||
TheatreSkipVote.find_or_create_by!(theatre:, post: theatre.current_post, 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
|
||||
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
|
||||
|
||||
if theatre.current_post
|
||||
TheatreSkipVote.where(theatre:, post: theatre.current_post, user: current_user).delete_all
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
@@ -8,6 +8,8 @@ class Theatre < ApplicationRecord
|
||||
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
|
||||
|
||||
@@ -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,92 @@
|
||||
class TheatrePostSelector
|
||||
Candidate = Struct.new(:post, :weight, :penalty, :tags, keyword_init: true)
|
||||
|
||||
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("url LIKE '%nicovideo.jp%'")
|
||||
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: TagRepr.inline(tag), penalty: }
|
||||
}.compact.sort_by { |row| [-row[:penalty], row[:tag]['name'].to_s] }
|
||||
end
|
||||
|
||||
def post_weight_json(candidates)
|
||||
candidates.map { |candidate|
|
||||
{ post: PostRepr.base(candidate.post),
|
||||
weight: candidate.weight,
|
||||
penalty: candidate.penalty,
|
||||
tags: candidate.tags.map { |tag| TagRepr.inline(tag) } }
|
||||
}
|
||||
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
|
||||
新しい課題から参照
ユーザをブロックする