class TheatresController < ApplicationController def show theatre = Theatre.find_by(id: params[:id]) return head :not_found unless theatre render json: TheatreRepr.base(theatre) end def watching return head :unauthorized unless current_user theatre = Theatre.find_by(id: params[:id]) return head :not_found unless theatre host_flg = false post_id = nil post_started_at = nil theatre.with_lock do TheatreWatchingUser.find_or_initialize_by(theatre:, user: current_user).tap { _1.expires_at = 30.seconds.from_now }.save! if (!(theatre.host_user_id?) || !(theatre.watching_users.exists?(id: theatre.host_user_id))) theatre.update!(host_user_id: current_user.id) end host_flg = theatre.host_user_id == current_user.id post_id = theatre.current_post_id 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]) } end def next_post return head :unauthorized unless current_user theatre = Theatre.find_by(id: params[:id]) 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) head :no_content end end