| @@ -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 | end | ||||
| end | end | ||||
| resources :theatres, only: [:show] do | |||||
| member do | |||||
| put :watching | |||||
| patch :next_post | |||||
| end | |||||
| end | |||||
| end | end | ||||
| @@ -4,9 +4,11 @@ class CreateTheatres < ActiveRecord::Migration[8.0] | |||||
| t.string :name | t.string :name | ||||
| t.datetime :opens_at, null: false, index: true | t.datetime :opens_at, null: false, index: true | ||||
| t.datetime :closes_at, 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.references :current_post, foreign_key: { to_table: :posts }, index: true | ||||
| t.datetime :current_post_started_at | t.datetime :current_post_started_at | ||||
| t.integer :next_comment_no, null: false, default: 1 | 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.references :created_by_user, null: false, foreign_key: { to_table: :users }, index: true | ||||
| t.timestamps | t.timestamps | ||||
| t.datetime :discarded_at, index: true | 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 | |||||
| @@ -10,7 +10,7 @@ | |||||
| # | # | ||||
| # It's strongly recommended that you check this file into your version control system. | # 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| | create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| | ||||
| t.string "name", null: false | t.string "name", null: false | ||||
| t.string "record_type", 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" | t.index ["user_id"], name: "index_theatre_comments_on_user_id" | ||||
| end | 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| | create_table "theatres", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| | ||||
| t.string "name" | t.string "name" | ||||
| t.datetime "opens_at", null: false | t.datetime "opens_at", null: false | ||||
| t.datetime "closes_at" | t.datetime "closes_at" | ||||
| t.integer "kind", null: false | |||||
| t.bigint "current_post_id" | t.bigint "current_post_id" | ||||
| t.datetime "current_post_started_at" | t.datetime "current_post_started_at" | ||||
| t.integer "next_comment_no", default: 1, null: false | t.integer "next_comment_no", default: 1, null: false | ||||
| t.bigint "host_user_id" | |||||
| t.bigint "created_by_user_id", null: false | t.bigint "created_by_user_id", null: false | ||||
| t.datetime "created_at", null: false | t.datetime "created_at", null: false | ||||
| t.datetime "updated_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 ["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 ["current_post_id"], name: "index_theatres_on_current_post_id" | ||||
| t.index ["discarded_at"], name: "index_theatres_on_discarded_at" | 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" | t.index ["opens_at"], name: "index_theatres_on_opens_at" | ||||
| end | 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 "tags", "tag_names" | ||||
| add_foreign_key "theatre_comments", "theatres" | add_foreign_key "theatre_comments", "theatres" | ||||
| add_foreign_key "theatre_comments", "users" | 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", "posts", column: "current_post_id" | ||||
| add_foreign_key "theatres", "users", column: "created_by_user_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", "ip_addresses" | ||||
| add_foreign_key "user_ips", "users" | add_foreign_key "user_ips", "users" | ||||
| add_foreign_key "user_post_views", "posts" | add_foreign_key "user_post_views", "posts" | ||||
| @@ -19,6 +19,7 @@ import PostNewPage from '@/pages/posts/PostNewPage' | |||||
| import PostSearchPage from '@/pages/posts/PostSearchPage' | import PostSearchPage from '@/pages/posts/PostSearchPage' | ||||
| import ServiceUnavailable from '@/pages/ServiceUnavailable' | import ServiceUnavailable from '@/pages/ServiceUnavailable' | ||||
| import SettingPage from '@/pages/users/SettingPage' | import SettingPage from '@/pages/users/SettingPage' | ||||
| import TheatreDetailPage from '@/pages/theatres/TheatreDetailPage' | |||||
| import WikiDetailPage from '@/pages/wiki/WikiDetailPage' | import WikiDetailPage from '@/pages/wiki/WikiDetailPage' | ||||
| import WikiDiffPage from '@/pages/wiki/WikiDiffPage' | import WikiDiffPage from '@/pages/wiki/WikiDiffPage' | ||||
| import WikiEditPage from '@/pages/wiki/WikiEditPage' | import WikiEditPage from '@/pages/wiki/WikiEditPage' | ||||
| @@ -47,6 +48,7 @@ const RouteTransitionWrapper = ({ user, setUser }: { | |||||
| <Route path="/posts/:id" element={<PostDetailRoute user={user}/>}/> | <Route path="/posts/:id" element={<PostDetailRoute user={user}/>}/> | ||||
| <Route path="/posts/changes" element={<PostHistoryPage/>}/> | <Route path="/posts/changes" element={<PostHistoryPage/>}/> | ||||
| <Route path="/tags/nico" element={<NicoTagListPage user={user}/>}/> | <Route path="/tags/nico" element={<NicoTagListPage user={user}/>}/> | ||||
| <Route path="/theatres/:id" element={<TheatreDetailPage user={user}/>}/> | |||||
| <Route path="/wiki" element={<WikiSearchPage/>}/> | <Route path="/wiki" element={<WikiSearchPage/>}/> | ||||
| <Route path="/wiki/:title" element={<WikiDetailPage/>}/> | <Route path="/wiki/:title" element={<WikiDetailPage/>}/> | ||||
| <Route path="/wiki/new" element={<WikiNewPage user={user}/>}/> | <Route path="/wiki/new" element={<WikiNewPage user={user}/>}/> | ||||
| @@ -79,6 +79,8 @@ export default (({ user }: Props) => { | |||||
| { name: '上位タグ', to: '/tags/implications', visible: false }, | { name: '上位タグ', to: '/tags/implications', visible: false }, | ||||
| { name: 'ニコニコ連携', to: '/tags/nico' }, | { name: 'ニコニコ連携', to: '/tags/nico' }, | ||||
| { name: 'ヘルプ', to: '/wiki/ヘルプ:タグ' }] }, | { name: 'ヘルプ', to: '/wiki/ヘルプ:タグ' }] }, | ||||
| { name: '上映会', to: '/theatres/1', base: '/theatres', subMenu: [ | |||||
| { name: '一覧', to: '/theatres' }] }, | |||||
| { name: 'Wiki', to: '/wiki/ヘルプ:ホーム', base: '/wiki', subMenu: [ | { name: 'Wiki', to: '/wiki/ヘルプ:ホーム', base: '/wiki', subMenu: [ | ||||
| { name: '検索', to: '/wiki' }, | { name: '検索', to: '/wiki' }, | ||||
| { name: '新規', to: '/wiki/new' }, | { name: '新規', to: '/wiki/new' }, | ||||
| @@ -1,8 +1,85 @@ | |||||
| import { useEffect, useState } from 'react' | |||||
| import { Helmet } from 'react-helmet-async' | |||||
| import { useParams } from 'react-router-dom' | |||||
| import PostEmbed from '@/components/PostEmbed' | |||||
| import MainArea from '@/components/layout/MainArea' | |||||
| import { SITE_TITLE } from '@/config' | |||||
| import { apiGet, apiPatch, apiPut } from '@/lib/api' | |||||
| import { fetchPost } from '@/lib/posts' | |||||
| import type { FC } from 'react' | import type { FC } from 'react' | ||||
| import type { Theatre } from '@/types' | |||||
| type TheatreInfo = { | |||||
| hostFlg: boolean | |||||
| postId: number | null | |||||
| postStartedAt: string | null } | |||||
| type Props = { user: User } | |||||
| export default (({ user }: Props) => { | |||||
| const { id } = useParams () | |||||
| const [loading, setLoading] = useState (false) | |||||
| const [hostFlg, setHostFlg] = useState (false) | |||||
| const [theatre, setTheatre] = useState<Theatre | null> (null) | |||||
| const [theatreInfo, setTheatreInfo] = | |||||
| useState<TheatreInfo> ({ hostFlg: false, postId: null, postStartedAt: null }) | |||||
| const [post, setPost] = useState<Post | null> (null) | |||||
| useEffect (() => { | |||||
| if (!(id)) | |||||
| return | |||||
| void (async () => { | |||||
| setTheatre (await apiGet<Theatre> (`/theatres/${ id }`)) | |||||
| }) () | |||||
| const interval = setInterval (async () => { | |||||
| setTheatreInfo (await apiPut<TheatreInfo> (`/theatres/${ id }/watching`)) | |||||
| }, 1_000) | |||||
| return () => clearInterval (interval) | |||||
| }, [id]) | |||||
| useEffect (() => { | |||||
| if (!(theatreInfo.hostFlg) || loading) | |||||
| return | |||||
| if (theatreInfo.postId == null) | |||||
| { | |||||
| void (async () => { | |||||
| setLoading (true) | |||||
| await apiPatch<void> (`/theatres/${ id }/next_post`) | |||||
| setLoading (false) | |||||
| }) () | |||||
| return | |||||
| } | |||||
| }, [theatreInfo]) | |||||
| useEffect (() => { | |||||
| if (theatreInfo.postId == null) | |||||
| return | |||||
| void (async () => { | |||||
| setPost (await fetchPost (theatreInfo.postId)) | |||||
| }) () | |||||
| }, [theatreInfo.postId, theatreInfo.postStartedAt]) | |||||
| export default (() => { | |||||
| return ( | return ( | ||||
| <MainArea> | <MainArea> | ||||
| <Helmet> | |||||
| {theatre && ( | |||||
| <title> | |||||
| {'上映会場' | |||||
| + (theatre.name ? `『${ theatre.name }』` : ` #${ theatre.id }`) | |||||
| + ` | ${ SITE_TITLE }`} | |||||
| </title>)} | |||||
| </Helmet> | |||||
| {post && <PostEmbed post={post}/>} | |||||
| </MainArea>) | </MainArea>) | ||||
| }) satisfies FC | |||||
| }) satisfies FC<Props> | |||||
| @@ -75,6 +75,15 @@ export type Tag = { | |||||
| children?: Tag[] | children?: Tag[] | ||||
| matchedAlias?: string | null } | matchedAlias?: string | null } | ||||
| export type Theatre = { | |||||
| id: number | |||||
| name: string | null | |||||
| opensAt: string | |||||
| closesAt: string | null | |||||
| createdByUser: { id: number; name: string } | |||||
| createdAt: string | |||||
| updatedAt: string } | |||||
| export type User = { | export type User = { | ||||
| id: number | id: number | ||||
| name: string | null | name: string | null | ||||