diff --git a/backend/app/controllers/theatres_controller.rb b/backend/app/controllers/theatres_controller.rb new file mode 100644 index 0000000..67228d7 --- /dev/null +++ b/backend/app/controllers/theatres_controller.rb @@ -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 diff --git a/backend/app/models/theatre.rb b/backend/app/models/theatre.rb new file mode 100644 index 0000000..1da8555 --- /dev/null +++ b/backend/app/models/theatre.rb @@ -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 diff --git a/backend/app/models/theatre_comment.rb b/backend/app/models/theatre_comment.rb new file mode 100644 index 0000000..73883e0 --- /dev/null +++ b/backend/app/models/theatre_comment.rb @@ -0,0 +1,8 @@ +class TheatreComment < ApplicationRecord + include MyDiscard + + self.primary_key = :theatre_id, :no + + belongs_to :theatre +end + diff --git a/backend/app/models/theatre_watching_user.rb b/backend/app/models/theatre_watching_user.rb new file mode 100644 index 0000000..c39ddd3 --- /dev/null +++ b/backend/app/models/theatre_watching_user.rb @@ -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 diff --git a/backend/app/representations/theatre_repr.rb b/backend/app/representations/theatre_repr.rb new file mode 100644 index 0000000..23e0d8f --- /dev/null +++ b/backend/app/representations/theatre_repr.rb @@ -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 diff --git a/backend/config/routes.rb b/backend/config/routes.rb index ed7b4ac..57eb470 100644 --- a/backend/config/routes.rb +++ b/backend/config/routes.rb @@ -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 diff --git a/backend/db/migrate/20260316082500_create_theatres.rb b/backend/db/migrate/20260316082500_create_theatres.rb index 828623a..1640bb7 100644 --- a/backend/db/migrate/20260316082500_create_theatres.rb +++ b/backend/db/migrate/20260316082500_create_theatres.rb @@ -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 diff --git a/backend/db/migrate/20260317015000_create_theatre_watching_users.rb b/backend/db/migrate/20260317015000_create_theatre_watching_users.rb new file mode 100644 index 0000000..a2959bf --- /dev/null +++ b/backend/db/migrate/20260317015000_create_theatre_watching_users.rb @@ -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 diff --git a/backend/db/schema.rb b/backend/db/schema.rb index 05b9aa0..6a2096b 100644 --- a/backend/db/schema.rb +++ b/backend/db/schema.rb @@ -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" diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index f7c9545..3b437cb 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -19,6 +19,7 @@ import PostNewPage from '@/pages/posts/PostNewPage' import PostSearchPage from '@/pages/posts/PostSearchPage' import ServiceUnavailable from '@/pages/ServiceUnavailable' import SettingPage from '@/pages/users/SettingPage' +import TheatreDetailPage from '@/pages/theatres/TheatreDetailPage' import WikiDetailPage from '@/pages/wiki/WikiDetailPage' import WikiDiffPage from '@/pages/wiki/WikiDiffPage' import WikiEditPage from '@/pages/wiki/WikiEditPage' @@ -47,6 +48,7 @@ const RouteTransitionWrapper = ({ user, setUser }: { }/> }/> }/> + }/> }/> }/> }/> diff --git a/frontend/src/components/TopNav.tsx b/frontend/src/components/TopNav.tsx index 06c30cf..b6a624b 100644 --- a/frontend/src/components/TopNav.tsx +++ b/frontend/src/components/TopNav.tsx @@ -79,6 +79,8 @@ export default (({ user }: Props) => { { name: '上位タグ', to: '/tags/implications', visible: false }, { name: 'ニコニコ連携', to: '/tags/nico' }, { name: 'ヘルプ', to: '/wiki/ヘルプ:タグ' }] }, + { name: '上映会', to: '/theatres/1', base: '/theatres', subMenu: [ + { name: '一覧', to: '/theatres' }] }, { name: 'Wiki', to: '/wiki/ヘルプ:ホーム', base: '/wiki', subMenu: [ { name: '検索', to: '/wiki' }, { name: '新規', to: '/wiki/new' }, diff --git a/frontend/src/pages/theatres/TheatreDetailPage.tsx b/frontend/src/pages/theatres/TheatreDetailPage.tsx index 0cd10d2..2657daa 100644 --- a/frontend/src/pages/theatres/TheatreDetailPage.tsx +++ b/frontend/src/pages/theatres/TheatreDetailPage.tsx @@ -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 { 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 (null) + const [theatreInfo, setTheatreInfo] = + useState ({ hostFlg: false, postId: null, postStartedAt: null }) + const [post, setPost] = useState (null) + + useEffect (() => { + if (!(id)) + return + + void (async () => { + setTheatre (await apiGet (`/theatres/${ id }`)) + }) () + + const interval = setInterval (async () => { + setTheatreInfo (await apiPut (`/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 (`/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 ( + + {theatre && ( + + {'上映会場' + + (theatre.name ? `『${ theatre.name }』` : ` #${ theatre.id }`) + + ` | ${ SITE_TITLE }`} + )} + + + {post && } ) -}) satisfies FC +}) satisfies FC diff --git a/frontend/src/types.ts b/frontend/src/types.ts index f40f533..f19020c 100644 --- a/frontend/src/types.ts +++ b/frontend/src/types.ts @@ -75,6 +75,15 @@ export type Tag = { children?: Tag[] 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 = { id: number name: string | null