このコミットが含まれているのは:
@@ -0,0 +1,52 @@
|
||||
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: }
|
||||
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
|
||||
@@ -0,0 +1,13 @@
|
||||
class Theatre < ApplicationRecord
|
||||
include MyDiscard
|
||||
|
||||
has_many :comments, class_name: 'TheatreComment'
|
||||
has_many :theatre_watching_users, dependent: :delete_all
|
||||
has_many :active_theatre_watching_users, -> { active },
|
||||
class_name: 'TheatreWatchingUser', inverse_of: :theatre
|
||||
has_many :watching_users, through: :active_theatre_watching_users, source: :user
|
||||
|
||||
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'
|
||||
end
|
||||
@@ -0,0 +1,8 @@
|
||||
class TheatreComment < ApplicationRecord
|
||||
include MyDiscard
|
||||
|
||||
self.primary_key = :theatre_id, :no
|
||||
|
||||
belongs_to :theatre
|
||||
end
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
class TheatreWatchingUser < ApplicationRecord
|
||||
self.primary_key = :theatre_id, :user_id
|
||||
|
||||
belongs_to :theatre
|
||||
belongs_to :user
|
||||
|
||||
scope :active, -> { where('expires_at >= ?', Time.current) }
|
||||
scope :expired, -> { where('expires_at < ?', Time.current) }
|
||||
|
||||
def active? = expires_at >= Time.current
|
||||
|
||||
def refresh! = update!(expires_at: 30.seconds.from_now)
|
||||
end
|
||||
@@ -0,0 +1,17 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
|
||||
module TheatreRepr
|
||||
BASE = { only: [:id, :name, :opens_at, :closes_at, :created_at, :updated_at],
|
||||
include: { created_by_user: { only: [:id, :name] } } }.freeze
|
||||
|
||||
module_function
|
||||
|
||||
def base theatre
|
||||
theatre.as_json(BASE)
|
||||
end
|
||||
|
||||
def many theatre
|
||||
theatre.map { |t| base(t) }
|
||||
end
|
||||
end
|
||||
@@ -72,4 +72,11 @@ Rails.application.routes.draw do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
resources :theatres, only: [:show] do
|
||||
member do
|
||||
put :watching
|
||||
patch :next_post
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,9 +4,11 @@ class CreateTheatres < ActiveRecord::Migration[8.0]
|
||||
t.string :name
|
||||
t.datetime :opens_at, null: false, index: true
|
||||
t.datetime :closes_at, index: true
|
||||
t.integer :kind, null: false, index: true
|
||||
t.references :current_post, foreign_key: { to_table: :posts }, index: true
|
||||
t.datetime :current_post_started_at
|
||||
t.integer :next_comment_no, null: false, default: 1
|
||||
t.references :host_user, foreign_key: { to_table: :users }
|
||||
t.references :created_by_user, null: false, foreign_key: { to_table: :users }, index: true
|
||||
t.timestamps
|
||||
t.datetime :discarded_at, index: true
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
class CreateTheatreWatchingUsers < ActiveRecord::Migration[8.0]
|
||||
def change
|
||||
create_table :theatre_watching_users, primary_key: [:theatre_id, :user_id] do |t|
|
||||
t.references :theatre, null: false, foreign_key: { to_table: :theatres }
|
||||
t.references :user, null: false, foreign_key: { to_table: :users }, index: true
|
||||
t.datetime :expires_at, null: false, index: true
|
||||
t.timestamps
|
||||
|
||||
t.index [:theatre_id, :expires_at]
|
||||
end
|
||||
end
|
||||
end
|
||||
生成ファイル
+20
-1
@@ -10,7 +10,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema[8.0].define(version: 2026_03_16_083500) do
|
||||
ActiveRecord::Schema[8.0].define(version: 2026_03_17_015000) do
|
||||
create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.string "name", null: false
|
||||
t.string "record_type", null: false
|
||||
@@ -180,13 +180,27 @@ ActiveRecord::Schema[8.0].define(version: 2026_03_16_083500) do
|
||||
t.index ["user_id"], name: "index_theatre_comments_on_user_id"
|
||||
end
|
||||
|
||||
create_table "theatre_watching_users", primary_key: ["theatre_id", "user_id"], charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.bigint "theatre_id", null: false
|
||||
t.bigint "user_id", null: false
|
||||
t.datetime "expires_at", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["expires_at"], name: "index_theatre_watching_users_on_expires_at"
|
||||
t.index ["theatre_id", "expires_at"], name: "index_theatre_watching_users_on_theatre_id_and_expires_at"
|
||||
t.index ["theatre_id"], name: "index_theatre_watching_users_on_theatre_id"
|
||||
t.index ["user_id"], name: "index_theatre_watching_users_on_user_id"
|
||||
end
|
||||
|
||||
create_table "theatres", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.string "name"
|
||||
t.datetime "opens_at", null: false
|
||||
t.datetime "closes_at"
|
||||
t.integer "kind", null: false
|
||||
t.bigint "current_post_id"
|
||||
t.datetime "current_post_started_at"
|
||||
t.integer "next_comment_no", default: 1, null: false
|
||||
t.bigint "host_user_id"
|
||||
t.bigint "created_by_user_id", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
@@ -195,6 +209,8 @@ ActiveRecord::Schema[8.0].define(version: 2026_03_16_083500) do
|
||||
t.index ["created_by_user_id"], name: "index_theatres_on_created_by_user_id"
|
||||
t.index ["current_post_id"], name: "index_theatres_on_current_post_id"
|
||||
t.index ["discarded_at"], name: "index_theatres_on_discarded_at"
|
||||
t.index ["host_user_id"], name: "index_theatres_on_host_user_id"
|
||||
t.index ["kind"], name: "index_theatres_on_kind"
|
||||
t.index ["opens_at"], name: "index_theatres_on_opens_at"
|
||||
end
|
||||
|
||||
@@ -295,8 +311,11 @@ ActiveRecord::Schema[8.0].define(version: 2026_03_16_083500) do
|
||||
add_foreign_key "tags", "tag_names"
|
||||
add_foreign_key "theatre_comments", "theatres"
|
||||
add_foreign_key "theatre_comments", "users"
|
||||
add_foreign_key "theatre_watching_users", "theatres"
|
||||
add_foreign_key "theatre_watching_users", "users"
|
||||
add_foreign_key "theatres", "posts", column: "current_post_id"
|
||||
add_foreign_key "theatres", "users", column: "created_by_user_id"
|
||||
add_foreign_key "theatres", "users", column: "host_user_id"
|
||||
add_foreign_key "user_ips", "ip_addresses"
|
||||
add_foreign_key "user_ips", "users"
|
||||
add_foreign_key "user_post_views", "posts"
|
||||
|
||||
新しい課題から参照
ユーザをブロックする