Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e40f7a3620 | |||
| e8be071064 | |||
| be40b4bcc4 | |||
| fb275b4763 | |||
| 2adff3966a | |||
| ef6219dcb1 | |||
| 04b01bf1c6 | |||
| 4c474d2bdf |
@@ -2,8 +2,8 @@ class TagsController < ApplicationController
|
|||||||
def index
|
def index
|
||||||
post_id = params[:post]
|
post_id = params[:post]
|
||||||
|
|
||||||
name = params[:name].to_s.strip.presence
|
name = params[:name].presence
|
||||||
category = params[:category].to_s.strip.presence
|
category = params[:category].presence
|
||||||
post_count_between = (params[:post_count_gte].presence || -1).to_i,
|
post_count_between = (params[:post_count_gte].presence || -1).to_i,
|
||||||
(params[:post_count_lte].presence || -1).to_i
|
(params[:post_count_lte].presence || -1).to_i
|
||||||
post_count_between[0] = nil if post_count_between[0] < 0
|
post_count_between[0] = nil if post_count_between[0] < 0
|
||||||
@@ -36,9 +36,7 @@ class TagsController < ApplicationController
|
|||||||
.includes(:tag_name, tag_name: :wiki_page)
|
.includes(:tag_name, tag_name: :wiki_page)
|
||||||
q = q.where(posts: { id: post_id }) if post_id.present?
|
q = q.where(posts: { id: post_id }) if post_id.present?
|
||||||
|
|
||||||
if name
|
q = q.where('tag_names.name LIKE ?', "%#{ name }%") if name
|
||||||
q = q.where('tag_names.name LIKE ?', "%#{ ActiveRecord::Base.sanitize_sql_like(name) }%")
|
|
||||||
end
|
|
||||||
q = q.where(category: category) if category
|
q = q.where(category: category) if category
|
||||||
q = q.where('tags.post_count >= ?', post_count_between[0]) if post_count_between[0]
|
q = q.where('tags.post_count >= ?', post_count_between[0]) if post_count_between[0]
|
||||||
q = q.where('tags.post_count <= ?', post_count_between[1]) if post_count_between[1]
|
q = q.where('tags.post_count <= ?', post_count_between[1]) if post_count_between[1]
|
||||||
@@ -79,7 +77,7 @@ class TagsController < ApplicationController
|
|||||||
|
|
||||||
alias_rows =
|
alias_rows =
|
||||||
TagName
|
TagName
|
||||||
.where('name LIKE ?', "#{ ActiveRecord::Base.sanitize_sql_like(q) }%")
|
.where('name LIKE ?', "#{ q }%")
|
||||||
.where.not(canonical_id: nil)
|
.where.not(canonical_id: nil)
|
||||||
.pluck(:canonical_id, :name)
|
.pluck(:canonical_id, :name)
|
||||||
|
|
||||||
@@ -99,8 +97,7 @@ class TagsController < ApplicationController
|
|||||||
base
|
base
|
||||||
.where(((with_nico ? '(tags.category = ? AND tag_names.name LIKE ?) OR ' : '') +
|
.where(((with_nico ? '(tags.category = ? AND tag_names.name LIKE ?) OR ' : '') +
|
||||||
'tag_names.name LIKE ?'),
|
'tag_names.name LIKE ?'),
|
||||||
*(with_nico ? ['nico', "nico:#{ ActiveRecord::Base.sanitise_sql_like(q) }%"] : []),
|
*(with_nico ? ['nico', "nico:#{ q }%"] : []), "#{ q }%")
|
||||||
"#{ ActiveRecord::Base.sanitise_sql_like(q) }%")
|
|
||||||
|
|
||||||
tags =
|
tags =
|
||||||
if canonical_ids.present?
|
if canonical_ids.present?
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
class TheatreCommentsController < ApplicationController
|
||||||
|
def index
|
||||||
|
no_gt = params[:no_gt].to_i
|
||||||
|
no_gt = 0 if no_gt.negative?
|
||||||
|
|
||||||
|
comments = TheatreComment
|
||||||
|
.where(theatre_id: params[:theatre_id])
|
||||||
|
.where('no > ?', no_gt)
|
||||||
|
.order(no: :desc)
|
||||||
|
|
||||||
|
render json: comments.as_json(include: { user: { only: [:id, :name] } })
|
||||||
|
end
|
||||||
|
|
||||||
|
def create
|
||||||
|
return head :unauthorized unless current_user
|
||||||
|
|
||||||
|
content = params[:content]
|
||||||
|
return head :unprocessable_entity if content.blank?
|
||||||
|
|
||||||
|
theatre = Theatre.find_by(id: params[:theatre_id])
|
||||||
|
return head :not_found unless theatre
|
||||||
|
|
||||||
|
comment = nil
|
||||||
|
theatre.with_lock do
|
||||||
|
no = theatre.next_comment_no
|
||||||
|
comment = TheatreComment.create!(theatre:, no:, user: current_user, content:)
|
||||||
|
theatre.update!(next_comment_no: no + 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
render json: comment, status: :created
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -31,7 +31,9 @@ class TheatresController < ApplicationController
|
|||||||
post_started_at = theatre.current_post_started_at
|
post_started_at = theatre.current_post_started_at
|
||||||
end
|
end
|
||||||
|
|
||||||
render json: { host_flg:, post_id:, post_started_at: }
|
render json: {
|
||||||
|
host_flg:, post_id:, post_started_at:,
|
||||||
|
watching_users: theatre.watching_users.as_json(only: [:id, :name]) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def next_post
|
def next_post
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
require 'digest'
|
||||||
|
|
||||||
|
|
||||||
|
class WikiAssetsController < ApplicationController
|
||||||
|
def index
|
||||||
|
page_id = params[:wiki_page_id].to_i
|
||||||
|
page = WikiPage.find_by(id: page_id)
|
||||||
|
return head :not_found unless page
|
||||||
|
|
||||||
|
render json: WikiAssetRepr.many(page.assets)
|
||||||
|
end
|
||||||
|
|
||||||
|
def create
|
||||||
|
return head :unauthorized unless current_user
|
||||||
|
return head :forbidden unless current_user.gte_member?
|
||||||
|
|
||||||
|
wiki_page_id = params[:wiki_page_id].to_i
|
||||||
|
page = WikiPage.find_by(id: wiki_page_id)
|
||||||
|
return head :not_found unless page
|
||||||
|
|
||||||
|
file = params[:file]
|
||||||
|
return head :bad_request if file.blank?
|
||||||
|
|
||||||
|
asset = nil
|
||||||
|
page.with_lock do
|
||||||
|
no = page.next_asset_no
|
||||||
|
alt_text = params[:alt_text].presence
|
||||||
|
sha256 = Digest::SHA256.file(file.tempfile.path).digest
|
||||||
|
|
||||||
|
asset = WikiAsset.new(wiki_page_id:, no:, alt_text:, sha256:, created_by_user: current_user)
|
||||||
|
asset.file.attach(file)
|
||||||
|
asset.save!
|
||||||
|
|
||||||
|
page.update!(next_asset_no: no + 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
render json: WikiAssetRepr.base(asset)
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -96,7 +96,7 @@ class WikiPagesController < ApplicationController
|
|||||||
message = params[:message].presence
|
message = params[:message].presence
|
||||||
Wiki::Commit.content!(page:, body:, created_user: current_user, message:)
|
Wiki::Commit.content!(page:, body:, created_user: current_user, message:)
|
||||||
|
|
||||||
render json: WikiPageRepr.base(page), status: :created
|
render json: WikiPageRepr.base(page).merge(body:), status: :created
|
||||||
else
|
else
|
||||||
render json: { errors: page.errors.full_messages },
|
render json: { errors: page.errors.full_messages },
|
||||||
status: :unprocessable_entity
|
status: :unprocessable_entity
|
||||||
@@ -107,7 +107,7 @@ class WikiPagesController < ApplicationController
|
|||||||
return head :unauthorized unless current_user
|
return head :unauthorized unless current_user
|
||||||
return head :forbidden unless current_user.gte_member?
|
return head :forbidden unless current_user.gte_member?
|
||||||
|
|
||||||
title = params[:title]&.strip
|
title = params[:title].to_s.strip
|
||||||
body = params[:body].to_s
|
body = params[:body].to_s
|
||||||
|
|
||||||
return head :unprocessable_entity if title.blank? || body.blank?
|
return head :unprocessable_entity if title.blank? || body.blank?
|
||||||
@@ -126,7 +126,7 @@ class WikiPagesController < ApplicationController
|
|||||||
message:,
|
message:,
|
||||||
base_revision_id:)
|
base_revision_id:)
|
||||||
|
|
||||||
head :ok
|
render json: WikiPageRepr.base(page).merge(body:)
|
||||||
end
|
end
|
||||||
|
|
||||||
def search
|
def search
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
class TheatreComment < ApplicationRecord
|
class TheatreComment < ApplicationRecord
|
||||||
include MyDiscard
|
include Discard::Model
|
||||||
|
|
||||||
self.primary_key = :theatre_id, :no
|
self.primary_key = :theatre_id, :no
|
||||||
|
|
||||||
belongs_to :theatre
|
belongs_to :theatre
|
||||||
|
belongs_to :user
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
class WikiAsset < ApplicationRecord
|
||||||
|
belongs_to :wiki_page
|
||||||
|
belongs_to :created_by_user, class_name: 'User'
|
||||||
|
|
||||||
|
has_one_attached :file
|
||||||
|
|
||||||
|
validates :file, presence: true
|
||||||
|
|
||||||
|
def url
|
||||||
|
Rails.application.routes.url_helpers.rails_blob_url(file, only_path: true)
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -15,6 +15,8 @@ class WikiPage < ApplicationRecord
|
|||||||
foreign_key: :redirect_page_id,
|
foreign_key: :redirect_page_id,
|
||||||
dependent: :nullify
|
dependent: :nullify
|
||||||
|
|
||||||
|
has_many :assets, class_name: 'WikiAsset', dependent: :destroy
|
||||||
|
|
||||||
belongs_to :tag_name
|
belongs_to :tag_name
|
||||||
validates :tag_name, presence: true
|
validates :tag_name, presence: true
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
|
||||||
|
module WikiAssetRepr
|
||||||
|
BASE = { only: [:wiki_page_id, :no], methods: [:url] }.freeze
|
||||||
|
|
||||||
|
module_function
|
||||||
|
|
||||||
|
def base wiki_asset
|
||||||
|
wiki_asset.as_json(BASE)
|
||||||
|
end
|
||||||
|
|
||||||
|
def many wiki_assets
|
||||||
|
wiki_assets.map { |a| base(a) }
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -41,6 +41,8 @@ Rails.application.routes.draw do
|
|||||||
get :exists
|
get :exists
|
||||||
get :diff
|
get :diff
|
||||||
end
|
end
|
||||||
|
|
||||||
|
resources :assets, controller: :wiki_assets, only: [:index, :create]
|
||||||
end
|
end
|
||||||
|
|
||||||
resources :posts, only: [:index, :show, :create, :update] do
|
resources :posts, only: [:index, :show, :create, :update] do
|
||||||
@@ -78,5 +80,7 @@ Rails.application.routes.draw do
|
|||||||
put :watching
|
put :watching
|
||||||
patch :next_post
|
patch :next_post
|
||||||
end
|
end
|
||||||
|
|
||||||
|
resources :comments, controller: :theatre_comments, only: [:index, :create]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
class CreateWikiAssets < ActiveRecord::Migration[8.0]
|
||||||
|
def change
|
||||||
|
create_table :wiki_assets do |t|
|
||||||
|
t.references :wiki_page, null: false, foreign_key: true, index: false
|
||||||
|
t.integer :no, null: false
|
||||||
|
t.string :alt_text
|
||||||
|
t.column :sha256, 'binary(32)', null: false
|
||||||
|
t.references :created_by_user, null: false, foreign_key: { to_table: :users }
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
|
||||||
|
add_index :wiki_assets, [:wiki_page_id, :sha256], unique: true
|
||||||
|
add_index :wiki_assets, [:wiki_page_id, :no], unique: true
|
||||||
|
|
||||||
|
add_column :wiki_pages, :next_asset_no, :integer, null: false, default: 1
|
||||||
|
end
|
||||||
|
end
|
||||||
Generated
+17
-1
@@ -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_17_015000) do
|
ActiveRecord::Schema[8.0].define(version: 2026_03_23_192300) 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
|
||||||
@@ -239,6 +239,19 @@ ActiveRecord::Schema[8.0].define(version: 2026_03_17_015000) do
|
|||||||
t.datetime "updated_at", null: false
|
t.datetime "updated_at", null: false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
create_table "wiki_assets", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||||
|
t.bigint "wiki_page_id", null: false
|
||||||
|
t.integer "no", null: false
|
||||||
|
t.string "alt_text"
|
||||||
|
t.binary "sha256", limit: 32, null: false
|
||||||
|
t.bigint "created_by_user_id", null: false
|
||||||
|
t.datetime "created_at", null: false
|
||||||
|
t.datetime "updated_at", null: false
|
||||||
|
t.index ["created_by_user_id"], name: "index_wiki_assets_on_created_by_user_id"
|
||||||
|
t.index ["wiki_page_id", "no"], name: "index_wiki_assets_on_wiki_page_id_and_no", unique: true
|
||||||
|
t.index ["wiki_page_id", "sha256"], name: "index_wiki_assets_on_wiki_page_id_and_sha256", unique: true
|
||||||
|
end
|
||||||
|
|
||||||
create_table "wiki_lines", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
create_table "wiki_lines", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||||
t.string "sha256", limit: 64, null: false
|
t.string "sha256", limit: 64, null: false
|
||||||
t.text "body", null: false
|
t.text "body", null: false
|
||||||
@@ -254,6 +267,7 @@ ActiveRecord::Schema[8.0].define(version: 2026_03_17_015000) do
|
|||||||
t.datetime "created_at", null: false
|
t.datetime "created_at", null: false
|
||||||
t.datetime "updated_at", null: false
|
t.datetime "updated_at", null: false
|
||||||
t.datetime "discarded_at"
|
t.datetime "discarded_at"
|
||||||
|
t.integer "next_asset_no", default: 1, null: false
|
||||||
t.index ["created_user_id"], name: "index_wiki_pages_on_created_user_id"
|
t.index ["created_user_id"], name: "index_wiki_pages_on_created_user_id"
|
||||||
t.index ["discarded_at"], name: "index_wiki_pages_on_discarded_at"
|
t.index ["discarded_at"], name: "index_wiki_pages_on_discarded_at"
|
||||||
t.index ["tag_name_id"], name: "index_wiki_pages_on_tag_name_id", unique: true
|
t.index ["tag_name_id"], name: "index_wiki_pages_on_tag_name_id", unique: true
|
||||||
@@ -320,6 +334,8 @@ ActiveRecord::Schema[8.0].define(version: 2026_03_17_015000) do
|
|||||||
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"
|
||||||
add_foreign_key "user_post_views", "users"
|
add_foreign_key "user_post_views", "users"
|
||||||
|
add_foreign_key "wiki_assets", "users", column: "created_by_user_id"
|
||||||
|
add_foreign_key "wiki_assets", "wiki_pages"
|
||||||
add_foreign_key "wiki_pages", "tag_names"
|
add_foreign_key "wiki_pages", "tag_names"
|
||||||
add_foreign_key "wiki_pages", "users", column: "created_user_id"
|
add_foreign_key "wiki_pages", "users", column: "created_user_id"
|
||||||
add_foreign_key "wiki_pages", "users", column: "updated_user_id"
|
add_foreign_key "wiki_pages", "users", column: "updated_user_id"
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
FactoryBot.define do
|
||||||
|
factory :theatre_comment do
|
||||||
|
association :theatre
|
||||||
|
association :user
|
||||||
|
sequence (:no) { |n| n }
|
||||||
|
content { 'test comment' }
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
FactoryBot.define do
|
||||||
|
factory :theatre do
|
||||||
|
name { 'Test Theatre' }
|
||||||
|
kind { 1 }
|
||||||
|
opens_at { Time.current }
|
||||||
|
closes_at { 1.day.from_now }
|
||||||
|
next_comment_no { 1 }
|
||||||
|
|
||||||
|
association :created_by_user, factory: :user
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,150 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe 'TheatreComments', type: :request do
|
||||||
|
def sign_in_as(user)
|
||||||
|
allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(user)
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'GET /theatres/:theatre_id/comments' do
|
||||||
|
let(:theatre) { create(:theatre) }
|
||||||
|
let(:other_theatre) { create(:theatre) }
|
||||||
|
let(:alice) { create(:user, name: 'Alice') }
|
||||||
|
let(:bob) { create(:user, name: 'Bob') }
|
||||||
|
|
||||||
|
let!(:comment_3) do
|
||||||
|
create(
|
||||||
|
:theatre_comment,
|
||||||
|
theatre: theatre,
|
||||||
|
no: 3,
|
||||||
|
user: alice,
|
||||||
|
content: 'third comment'
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
let!(:comment_1) do
|
||||||
|
create(
|
||||||
|
:theatre_comment,
|
||||||
|
theatre: theatre,
|
||||||
|
no: 1,
|
||||||
|
user: alice,
|
||||||
|
content: 'first comment'
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
let!(:comment_2) do
|
||||||
|
create(
|
||||||
|
:theatre_comment,
|
||||||
|
theatre: theatre,
|
||||||
|
no: 2,
|
||||||
|
user: bob,
|
||||||
|
content: 'second comment'
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
let!(:other_comment) do
|
||||||
|
create(
|
||||||
|
:theatre_comment,
|
||||||
|
theatre: other_theatre,
|
||||||
|
no: 1,
|
||||||
|
user: bob,
|
||||||
|
content: 'other theatre comment'
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'theatre_id で絞り込み、no_gt より大きいものを no 降順で返す' do
|
||||||
|
get "/theatres/#{theatre.id}/comments", params: { no_gt: 1 }
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
expect(response.parsed_body.map { |row| row['no'] }).to eq([3, 2])
|
||||||
|
expect(response.parsed_body.map { |row| row['content'] }).to eq([
|
||||||
|
'third comment',
|
||||||
|
'second comment'
|
||||||
|
])
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'user は id と name だけを含む' do
|
||||||
|
get "/theatres/#{theatre.id}/comments", params: { no_gt: 1 }
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
|
||||||
|
expect(response.parsed_body.first['user']).to eq({
|
||||||
|
'id' => alice.id,
|
||||||
|
'name' => 'Alice'
|
||||||
|
})
|
||||||
|
expect(response.parsed_body.first['user'].keys).to contain_exactly('id', 'name')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'no_gt が負数なら 0 として扱う' do
|
||||||
|
get "/theatres/#{theatre.id}/comments", params: { no_gt: -100 }
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
expect(response.parsed_body.map { |row| row['no'] }).to eq([3, 2, 1])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'POST /theatres/:theatre_id/comments' do
|
||||||
|
let(:user) { create(:user, name: 'Alice') }
|
||||||
|
let(:theatre) { create(:theatre, next_comment_no: 2) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
create(
|
||||||
|
:theatre_comment,
|
||||||
|
theatre: theatre,
|
||||||
|
no: 1,
|
||||||
|
user: user,
|
||||||
|
content: 'existing comment'
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it '未ログインなら 401 を返す' do
|
||||||
|
expect {
|
||||||
|
post "/theatres/#{theatre.id}/comments", params: { content: 'hello' }
|
||||||
|
}.not_to change(TheatreComment, :count)
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:unauthorized)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'content が blank なら 422 を返す' do
|
||||||
|
sign_in_as(user)
|
||||||
|
|
||||||
|
expect {
|
||||||
|
post "/theatres/#{theatre.id}/comments", params: { content: ' ' }
|
||||||
|
}.not_to change(TheatreComment, :count)
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:unprocessable_entity)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'theatre が存在しなければ 404 を返す' do
|
||||||
|
sign_in_as(user)
|
||||||
|
|
||||||
|
expect {
|
||||||
|
post '/theatres/999999/comments', params: { content: 'hello' }
|
||||||
|
}.not_to change(TheatreComment, :count)
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:not_found)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'コメントを作成し、user を紐づけ、next_comment_no を進める' do
|
||||||
|
sign_in_as(user)
|
||||||
|
|
||||||
|
expect {
|
||||||
|
post "/theatres/#{theatre.id}/comments", params: { content: 'new comment' }
|
||||||
|
}.to change(TheatreComment, :count).by(1)
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:created)
|
||||||
|
|
||||||
|
comment = TheatreComment.find_by!(theatre: theatre, no: 2)
|
||||||
|
|
||||||
|
expect(comment.user).to eq(user)
|
||||||
|
expect(comment.content).to eq('new comment')
|
||||||
|
expect(theatre.reload.next_comment_no).to eq(3)
|
||||||
|
|
||||||
|
expect(response.parsed_body.slice('theatre_id', 'no', 'user_id', 'content')).to eq({
|
||||||
|
'theatre_id' => theatre.id,
|
||||||
|
'no' => 2,
|
||||||
|
'user_id' => user.id,
|
||||||
|
'content' => 'new comment'
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -117,11 +117,18 @@ RSpec.describe 'Theatres API', type: :request do
|
|||||||
expect(theatre.host_user_id).to eq(member.id)
|
expect(theatre.host_user_id).to eq(member.id)
|
||||||
expect(watch.expires_at).to be_within(1.second).of(30.seconds.from_now)
|
expect(watch.expires_at).to be_within(1.second).of(30.seconds.from_now)
|
||||||
|
|
||||||
expect(json).to eq(
|
expect(json).to include(
|
||||||
'host_flg' => true,
|
'host_flg' => true,
|
||||||
'post_id' => nil,
|
'post_id' => nil,
|
||||||
'post_started_at' => nil
|
'post_started_at' => nil
|
||||||
)
|
)
|
||||||
|
|
||||||
|
expect(json.fetch('watching_users')).to contain_exactly(
|
||||||
|
{
|
||||||
|
'id' => member.id,
|
||||||
|
'name' => 'member user'
|
||||||
|
}
|
||||||
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -167,11 +174,22 @@ RSpec.describe 'Theatres API', type: :request do
|
|||||||
expect(response).to have_http_status(:ok)
|
expect(response).to have_http_status(:ok)
|
||||||
expect(theatre.reload.host_user_id).to eq(other_user.id)
|
expect(theatre.reload.host_user_id).to eq(other_user.id)
|
||||||
|
|
||||||
expect(json).to eq(
|
expect(json).to include(
|
||||||
'host_flg' => false,
|
'host_flg' => false,
|
||||||
'post_id' => nil,
|
'post_id' => nil,
|
||||||
'post_started_at' => nil
|
'post_started_at' => nil
|
||||||
)
|
)
|
||||||
|
|
||||||
|
expect(json.fetch('watching_users')).to contain_exactly(
|
||||||
|
{
|
||||||
|
'id' => member.id,
|
||||||
|
'name' => 'member user'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'id' => other_user.id,
|
||||||
|
'name' => 'other user'
|
||||||
|
}
|
||||||
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,191 @@
|
|||||||
|
require 'digest'
|
||||||
|
require 'rails_helper'
|
||||||
|
require 'stringio'
|
||||||
|
|
||||||
|
|
||||||
|
RSpec.describe 'WikiAssets API', type: :request do
|
||||||
|
def dummy_upload(content = 'dummy-image', filename: 'dummy.png', content_type: 'image/png')
|
||||||
|
Rack::Test::UploadedFile.new(StringIO.new(content),
|
||||||
|
content_type,
|
||||||
|
original_filename: filename)
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:member) { create(:user, :member, name: 'member user') }
|
||||||
|
let(:guest) { create(:user, name: 'guest user') }
|
||||||
|
|
||||||
|
let!(:tag_name) { TagName.create!(name: 'spec_wiki_asset_page') }
|
||||||
|
let!(:page) do
|
||||||
|
WikiPage.create!(tag_name: tag_name, created_user: member, updated_user: member).tap do |p|
|
||||||
|
Wiki::Commit.content!(page: p, body: 'init', created_user: member, message: 'init')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'GET /wiki/:wiki_page_id/assets' do
|
||||||
|
subject(:do_request) do
|
||||||
|
get "/wiki/#{wiki_page_id}/assets"
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:wiki_page_id) { page.id }
|
||||||
|
|
||||||
|
let!(:asset) do
|
||||||
|
WikiAsset.new(wiki_page: page,
|
||||||
|
no: 1,
|
||||||
|
alt_text: 'spec alt',
|
||||||
|
sha256: Digest::SHA256.digest('asset-1'),
|
||||||
|
created_by_user: member).tap do |record|
|
||||||
|
record.file.attach(dummy_upload('asset-1'))
|
||||||
|
record.save!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when wiki page exists' do
|
||||||
|
it 'returns assets for the page' do
|
||||||
|
do_request
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
expect(json).to be_an(Array)
|
||||||
|
expect(json.size).to eq(1)
|
||||||
|
|
||||||
|
expect(json.first).to include(
|
||||||
|
'wiki_page_id' => page.id,
|
||||||
|
'no' => 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'does not include assets from other pages' do
|
||||||
|
other_tag_name = TagName.create!(name: 'spec_other_wiki_asset_page')
|
||||||
|
other_page = WikiPage.create!(tag_name: other_tag_name,
|
||||||
|
created_user: member,
|
||||||
|
updated_user: member)
|
||||||
|
Wiki::Commit.content!(page: other_page, body: 'other', created_user: member, message: 'other')
|
||||||
|
|
||||||
|
WikiAsset.new(wiki_page: other_page,
|
||||||
|
no: 1,
|
||||||
|
alt_text: 'other alt',
|
||||||
|
sha256: Digest::SHA256.digest('asset-2'),
|
||||||
|
created_by_user: member).tap do |record|
|
||||||
|
record.file.attach(dummy_upload('asset-2', filename: 'other.png'))
|
||||||
|
record.save!
|
||||||
|
end
|
||||||
|
|
||||||
|
do_request
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
expect(json.size).to eq(1)
|
||||||
|
expect(json.first['wiki_page_id']).to eq(page.id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when wiki page does not exist' do
|
||||||
|
let(:wiki_page_id) { 999_999_999 }
|
||||||
|
|
||||||
|
it 'returns 404' do
|
||||||
|
do_request
|
||||||
|
expect(response).to have_http_status(:not_found)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'POST /wiki/:wiki_page_id/assets' do
|
||||||
|
subject(:do_request) do
|
||||||
|
post "/wiki/#{wiki_page_id}/assets", params: params
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:wiki_page_id) { page.id }
|
||||||
|
let(:params) do
|
||||||
|
{ file: dummy_upload(upload_content),
|
||||||
|
alt_text: 'uploaded alt' }
|
||||||
|
end
|
||||||
|
let(:upload_content) { 'uploaded-image-binary' }
|
||||||
|
|
||||||
|
context 'when not logged in' do
|
||||||
|
it 'returns 401' do
|
||||||
|
sign_out
|
||||||
|
do_request
|
||||||
|
expect(response).to have_http_status(:unauthorized)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when logged in but not member' do
|
||||||
|
it 'returns 403' do
|
||||||
|
sign_in_as(guest)
|
||||||
|
do_request
|
||||||
|
expect(response).to have_http_status(:forbidden)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when wiki page does not exist' do
|
||||||
|
let(:wiki_page_id) { 999_999_999 }
|
||||||
|
|
||||||
|
it 'returns 404' do
|
||||||
|
sign_in_as(member)
|
||||||
|
do_request
|
||||||
|
expect(response).to have_http_status(:not_found)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when file is blank' do
|
||||||
|
let(:params) { { alt_text: 'uploaded alt' } }
|
||||||
|
|
||||||
|
it 'returns 400' do
|
||||||
|
sign_in_as(member)
|
||||||
|
do_request
|
||||||
|
expect(response).to have_http_status(:bad_request)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when success' do
|
||||||
|
before do
|
||||||
|
sign_in_as(member)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'creates asset, attaches file, increments next_asset_no, and returns json' do
|
||||||
|
expect { do_request }
|
||||||
|
.to change(WikiAsset, :count).by(1)
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
|
||||||
|
asset = WikiAsset.order(:id).last
|
||||||
|
expect(asset.wiki_page_id).to eq(page.id)
|
||||||
|
expect(asset.no).to eq(1)
|
||||||
|
expect(asset.alt_text).to eq('uploaded alt')
|
||||||
|
expect(asset.sha256).to eq(Digest::SHA256.digest(upload_content))
|
||||||
|
expect(asset.created_by_user_id).to eq(member.id)
|
||||||
|
expect(asset.file).to be_attached
|
||||||
|
expect(asset.file.download).to eq(upload_content)
|
||||||
|
|
||||||
|
expect(page.reload.next_asset_no).to eq(2)
|
||||||
|
|
||||||
|
expect(json).to include(
|
||||||
|
'wiki_page_id' => page.id,
|
||||||
|
'no' => 1,
|
||||||
|
'url' => asset.url
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'uses the next page-local number when assets already exist' do
|
||||||
|
existing = WikiAsset.new(wiki_page: page,
|
||||||
|
no: 1,
|
||||||
|
alt_text: 'existing alt',
|
||||||
|
sha256: Digest::SHA256.digest('existing'),
|
||||||
|
created_by_user: member)
|
||||||
|
existing.file.attach(dummy_upload('existing', filename: 'existing.png'))
|
||||||
|
existing.save!
|
||||||
|
page.update!(next_asset_no: 2)
|
||||||
|
|
||||||
|
do_request
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
|
||||||
|
asset = WikiAsset.order(:id).last
|
||||||
|
expect(asset.no).to eq(2)
|
||||||
|
expect(page.reload.next_asset_no).to eq(3)
|
||||||
|
|
||||||
|
expect(json).to include(
|
||||||
|
'wiki_page_id' => page.id,
|
||||||
|
'no' => 2,
|
||||||
|
'url' => asset.url
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -104,6 +104,7 @@ RSpec.describe 'Wiki API', type: :request do
|
|||||||
|
|
||||||
page_id = json.fetch('id')
|
page_id = json.fetch('id')
|
||||||
expect(json.fetch('title')).to eq('TestPage')
|
expect(json.fetch('title')).to eq('TestPage')
|
||||||
|
expect(json.fetch('body')).to eq("a\nb\nc")
|
||||||
|
|
||||||
page = WikiPage.find(page_id)
|
page = WikiPage.find(page_id)
|
||||||
rev = page.current_revision
|
rev = page.current_revision
|
||||||
@@ -111,30 +112,11 @@ RSpec.describe 'Wiki API', type: :request do
|
|||||||
expect(rev).to be_content
|
expect(rev).to be_content
|
||||||
expect(rev.message).to eq('init')
|
expect(rev.message).to eq('init')
|
||||||
|
|
||||||
# body が復元できること
|
|
||||||
expect(page.body).to eq("a\nb\nc")
|
expect(page.body).to eq("a\nb\nc")
|
||||||
|
|
||||||
# 行数とリレーションの整合
|
|
||||||
expect(rev.lines_count).to eq(3)
|
expect(rev.lines_count).to eq(3)
|
||||||
expect(rev.wiki_revision_lines.order(:position).pluck(:position)).to eq([0, 1, 2])
|
expect(rev.wiki_revision_lines.order(:position).pluck(:position)).to eq([0, 1, 2])
|
||||||
expect(rev.wiki_lines.pluck(:body)).to match_array(%w[a b c])
|
expect(rev.wiki_lines.pluck(:body)).to match_array(%w[a b c])
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'reuses existing WikiLine rows by sha256' do
|
|
||||||
# 先に同じ行を作っておく
|
|
||||||
WikiLine.create!(sha256: Digest::SHA256.hexdigest('a'), body: 'a', created_at: Time.current, updated_at: Time.current)
|
|
||||||
|
|
||||||
post endpoint,
|
|
||||||
params: { title: 'Reuse', body: "a\na" },
|
|
||||||
headers: auth_headers(member)
|
|
||||||
|
|
||||||
page = WikiPage.find(JSON.parse(response.body).fetch('id'))
|
|
||||||
rev = page.current_revision
|
|
||||||
expect(rev.lines_count).to eq(2)
|
|
||||||
|
|
||||||
# "a" の WikiLine が増殖しない(1行のはず)
|
|
||||||
expect(WikiLine.where(body: 'a').count).to eq(1)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -71,7 +71,8 @@ export default forwardRef<HTMLAnchorElement, Props> (({
|
|||||||
|| ev.metaKey
|
|| ev.metaKey
|
||||||
|| ev.ctrlKey
|
|| ev.ctrlKey
|
||||||
|| ev.shiftKey
|
|| ev.shiftKey
|
||||||
|| ev.altKey)
|
|| ev.altKey
|
||||||
|
|| (rest.target && rest.target !== '_self'))
|
||||||
return
|
return
|
||||||
|
|
||||||
ev.preventDefault ()
|
ev.preventDefault ()
|
||||||
|
|||||||
@@ -79,9 +79,11 @@ 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/ヘルプ:タグ' }] },
|
||||||
// TODO: 本実装時に消す.
|
{ name: '上映会', to: '/theatres/1', base: '/theatres', subMenu: [
|
||||||
// { name: '上映会', to: '/theatres/1', base: '/theatres', subMenu: [
|
{ name: <>第 1 会場</>, to: '/theatres/1' },
|
||||||
// { name: '一覧', to: '/theatres' }] },
|
{ name: 'CyTube', to: '//cytube.mm428.net/r/deernijika' },
|
||||||
|
{ name: <>ニジカ放送局第 1 チャンネル</>,
|
||||||
|
to: '//www.youtube.com/watch?v=DCU3hL4Uu6A' }] },
|
||||||
{ 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' },
|
||||||
@@ -92,7 +94,7 @@ export default (({ user }: Props) => {
|
|||||||
visible: wikiPageFlg },
|
visible: wikiPageFlg },
|
||||||
{ name: '履歴', to: `/wiki/changes?id=${ wikiId }`, visible: wikiPageFlg },
|
{ name: '履歴', to: `/wiki/changes?id=${ wikiId }`, visible: wikiPageFlg },
|
||||||
{ name: '編輯', to: `/wiki/${ wikiId || wikiTitle }/edit`, visible: wikiPageFlg }] },
|
{ name: '編輯', to: `/wiki/${ wikiId || wikiTitle }/edit`, visible: wikiPageFlg }] },
|
||||||
{ name: 'ユーザ', to: '/users', subMenu: [
|
{ name: 'ユーザ', to: '/users/settings', subMenu: [
|
||||||
{ name: '一覧', to: '/users', visible: false },
|
{ name: '一覧', to: '/users', visible: false },
|
||||||
{ name: 'お前', to: `/users/${ user?.id }`, visible: false },
|
{ name: 'お前', to: `/users/${ user?.id }`, visible: false },
|
||||||
{ name: '設定', to: '/users/settings', visible: Boolean (user) }] }]
|
{ name: '設定', to: '/users/settings', visible: Boolean (user) }] }]
|
||||||
@@ -209,6 +211,7 @@ export default (({ user }: Props) => {
|
|||||||
<PrefetchLink
|
<PrefetchLink
|
||||||
key={`l-${ i }`}
|
key={`l-${ i }`}
|
||||||
to={item.to}
|
to={item.to}
|
||||||
|
target={item.to.slice (0, 2) === '//' ? '_blank' : undefined}
|
||||||
className="h-full flex items-center px-3">
|
className="h-full flex items-center px-3">
|
||||||
{item.name}
|
{item.name}
|
||||||
</PrefetchLink>)))}
|
</PrefetchLink>)))}
|
||||||
@@ -275,6 +278,9 @@ export default (({ user }: Props) => {
|
|||||||
<PrefetchLink
|
<PrefetchLink
|
||||||
key={`sp-l-${ i }-${ j }`}
|
key={`sp-l-${ i }-${ j }`}
|
||||||
to={subItem.to}
|
to={subItem.to}
|
||||||
|
target={subItem.to.slice (0, 2) === '//'
|
||||||
|
? '_blank'
|
||||||
|
: undefined}
|
||||||
className="w-full min-h-[36px] flex items-center pl-12">
|
className="w-full min-h-[36px] flex items-center pl-12">
|
||||||
{subItem.name}
|
{subItem.name}
|
||||||
</PrefetchLink>)))}
|
</PrefetchLink>)))}
|
||||||
|
|||||||
@@ -1,48 +1,10 @@
|
|||||||
import { useQuery } from '@tanstack/react-query'
|
import WikiMarkdown from '@/components/WikiMarkdown'
|
||||||
import { useMemo } from 'react'
|
|
||||||
import ReactMarkdown from 'react-markdown'
|
|
||||||
import remarkGFM from 'remark-gfm'
|
|
||||||
|
|
||||||
import PrefetchLink from '@/components/PrefetchLink'
|
|
||||||
import SectionTitle from '@/components/common/SectionTitle'
|
|
||||||
import SubsectionTitle from '@/components/common/SubsectionTitle'
|
|
||||||
import { wikiKeys } from '@/lib/queryKeys'
|
|
||||||
import remarkWikiAutoLink from '@/lib/remark-wiki-autolink'
|
|
||||||
import { fetchWikiPages } from '@/lib/wiki'
|
|
||||||
|
|
||||||
import type { FC } from 'react'
|
import type { FC } from 'react'
|
||||||
import type { Components } from 'react-markdown'
|
|
||||||
|
|
||||||
type Props = { title: string
|
type Props = { title: string
|
||||||
body?: string }
|
body?: string }
|
||||||
|
|
||||||
const mdComponents = { h1: ({ children }) => <SectionTitle>{children}</SectionTitle>,
|
|
||||||
h2: ({ children }) => <SubsectionTitle>{children}</SubsectionTitle>,
|
|
||||||
ol: ({ children }) => <ol className="list-decimal pl-6">{children}</ol>,
|
|
||||||
ul: ({ children }) => <ul className="list-disc pl-6">{children}</ul>,
|
|
||||||
a: (({ href, children }) => (
|
|
||||||
['/', '.'].some (e => href?.startsWith (e))
|
|
||||||
? <PrefetchLink to={href!}>{children}</PrefetchLink>
|
|
||||||
: (
|
|
||||||
<a href={href}
|
|
||||||
target="_blank"
|
|
||||||
rel="noopener noreferrer">
|
|
||||||
{children}
|
|
||||||
</a>))) } as const satisfies Components
|
|
||||||
|
|
||||||
|
export default (({ title, body }: Props) =>
|
||||||
export default (({ title, body }: Props) => {
|
<WikiMarkdown title={title} body={body ?? ''}/>) satisfies FC<Props>
|
||||||
const { data } = useQuery ({
|
|
||||||
enabled: Boolean (body),
|
|
||||||
queryKey: wikiKeys.index ({ }),
|
|
||||||
queryFn: () => fetchWikiPages ({ }) })
|
|
||||||
const pageNames = (data ?? []).map (page => page.title).sort ((a, b) => b.length - a.length)
|
|
||||||
|
|
||||||
const remarkPlugins = useMemo (
|
|
||||||
() => [() => remarkWikiAutoLink (pageNames), remarkGFM], [pageNames])
|
|
||||||
|
|
||||||
return (
|
|
||||||
<ReactMarkdown components={mdComponents} remarkPlugins={remarkPlugins}>
|
|
||||||
{body || `このページは存在しません。[新規作成してください](/wiki/new?title=${ encodeURIComponent (title) })。`}
|
|
||||||
</ReactMarkdown>)
|
|
||||||
}) satisfies FC<Props>
|
|
||||||
|
|||||||
@@ -0,0 +1,76 @@
|
|||||||
|
import { useEffect, useState } from 'react'
|
||||||
|
import MdEditor from 'react-markdown-editor-lite'
|
||||||
|
|
||||||
|
import WikiMarkdown from '@/components/WikiMarkdown'
|
||||||
|
import Label from '@/components/common/Label'
|
||||||
|
import { apiPost } from '@/lib/api'
|
||||||
|
|
||||||
|
import type { FC } from 'react'
|
||||||
|
|
||||||
|
import type { WikiAsset } from '@/types'
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
title: string
|
||||||
|
body: string
|
||||||
|
onSubmit: (title: string, body: string) => void
|
||||||
|
id?: number | null }
|
||||||
|
|
||||||
|
|
||||||
|
export default (({ title: initTitle, body: initBody, onSubmit, id }: Props) => {
|
||||||
|
const forEdit = id != null
|
||||||
|
|
||||||
|
const [title, setTitle] = useState<string> (initTitle)
|
||||||
|
const [body, setBody] = useState<string> (initBody)
|
||||||
|
|
||||||
|
useEffect (() => {
|
||||||
|
setTitle (initTitle)
|
||||||
|
setBody (initBody)
|
||||||
|
}, [initTitle, initBody])
|
||||||
|
|
||||||
|
const handleImageUpload = async (file: File) => {
|
||||||
|
if (!(forEdit))
|
||||||
|
throw new Error ('画像は Wiki 作成前に追加することができません.')
|
||||||
|
|
||||||
|
const formData = new FormData
|
||||||
|
formData.append ('file', file)
|
||||||
|
|
||||||
|
const asset = await apiPost<WikiAsset> (
|
||||||
|
`/wiki/${ id }/assets`,
|
||||||
|
formData,
|
||||||
|
{ headers: { 'Content-Type': 'multipart/form-data' } })
|
||||||
|
|
||||||
|
return asset.url
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{/* タイトル */}
|
||||||
|
{/* TODO: タグ補完 */}
|
||||||
|
<div>
|
||||||
|
<Label>タイトル</Label>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
value={title}
|
||||||
|
onChange={e => setTitle (e.target.value)}
|
||||||
|
className="w-full border p-2 rounded"/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* 本文 */}
|
||||||
|
<div>
|
||||||
|
<Label>本文</Label>
|
||||||
|
<MdEditor
|
||||||
|
value={body}
|
||||||
|
style={{ height: '500px' }}
|
||||||
|
renderHTML={text => <WikiMarkdown body={text} preview/>}
|
||||||
|
onChange={({ text }) => setBody (text)}
|
||||||
|
onImageUpload={handleImageUpload}/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* 送信 */}
|
||||||
|
<button
|
||||||
|
onClick={() => onSubmit (title, body)}
|
||||||
|
className="px-4 py-2 bg-blue-600 text-white rounded disabled:bg-gray-400">
|
||||||
|
{forEdit ? '編輯' : '追加'}
|
||||||
|
</button>
|
||||||
|
</>)
|
||||||
|
}) satisfies FC<Props>
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
import { useQuery } from '@tanstack/react-query'
|
||||||
|
import { useMemo } from 'react'
|
||||||
|
import ReactMarkdown from 'react-markdown'
|
||||||
|
import remarkGFM from 'remark-gfm'
|
||||||
|
|
||||||
|
import PrefetchLink from '@/components/PrefetchLink'
|
||||||
|
import SectionTitle from '@/components/common/SectionTitle'
|
||||||
|
import SubsectionTitle from '@/components/common/SubsectionTitle'
|
||||||
|
import { wikiKeys } from '@/lib/queryKeys'
|
||||||
|
import remarkWikiAutoLink from '@/lib/remark-wiki-autolink'
|
||||||
|
import { fetchWikiPages } from '@/lib/wiki'
|
||||||
|
|
||||||
|
import type { FC } from 'react'
|
||||||
|
import type { Components } from 'react-markdown'
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
title?: string
|
||||||
|
body: string
|
||||||
|
preview?: boolean }
|
||||||
|
|
||||||
|
|
||||||
|
const makeComponents = (preview = false) => (
|
||||||
|
{ h1: ({ children }) => <SectionTitle>{children}</SectionTitle>,
|
||||||
|
h2: ({ children }) => <SubsectionTitle>{children}</SubsectionTitle>,
|
||||||
|
ol: ({ children }) => <ol className="list-decimal pl-6">{children}</ol>,
|
||||||
|
ul: ({ children }) => <ul className="list-disc pl-6">{children}</ul>,
|
||||||
|
a: ({ href, children }) => {
|
||||||
|
if (!(href))
|
||||||
|
return <>{children}</>
|
||||||
|
|
||||||
|
if (!(preview) && ['/', '.'].some (e => href.startsWith (e)))
|
||||||
|
return <PrefetchLink to={href}>{children}</PrefetchLink>
|
||||||
|
|
||||||
|
const ext = /^(?:https?:)?\/\//.test (href)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<a href={href}
|
||||||
|
target={ext ? '_blank' : undefined}
|
||||||
|
rel={ext ? 'noopener noreferrer' : undefined}>
|
||||||
|
{children}
|
||||||
|
</a>)
|
||||||
|
},
|
||||||
|
img: (({ src, alt }) => (
|
||||||
|
<img src={src ?? ''}
|
||||||
|
alt={alt ?? ''}
|
||||||
|
className="max-w-[240px] max-h-[320px]"/>)),
|
||||||
|
} as const satisfies Components)
|
||||||
|
|
||||||
|
|
||||||
|
export default (({ title, body, preview = false }: Props) => {
|
||||||
|
const { data } = useQuery ({
|
||||||
|
queryKey: wikiKeys.index ({ }),
|
||||||
|
queryFn: () => fetchWikiPages ({ }) })
|
||||||
|
|
||||||
|
const pageNames = useMemo (
|
||||||
|
() => (data ?? []).map ((page) => page.title).sort ((a, b) => b.length - a.length),
|
||||||
|
[data])
|
||||||
|
|
||||||
|
const remarkPlugins = useMemo (
|
||||||
|
() => [() => remarkWikiAutoLink (pageNames), remarkGFM],
|
||||||
|
[pageNames])
|
||||||
|
|
||||||
|
const components = useMemo (
|
||||||
|
() => makeComponents (preview),
|
||||||
|
[preview])
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ReactMarkdown
|
||||||
|
components={components}
|
||||||
|
remarkPlugins={remarkPlugins}>
|
||||||
|
{body
|
||||||
|
|| (title
|
||||||
|
? ('このページは存在しません。'
|
||||||
|
+`[新規作成してください](/wiki/new?title=${ encodeURIComponent (title) })。`)
|
||||||
|
: '')}
|
||||||
|
</ReactMarkdown>)
|
||||||
|
}) satisfies FC<Props>
|
||||||
@@ -2,85 +2,225 @@ import { useEffect, useRef, useState } from 'react'
|
|||||||
import { Helmet } from 'react-helmet-async'
|
import { Helmet } from 'react-helmet-async'
|
||||||
import { useParams } from 'react-router-dom'
|
import { useParams } from 'react-router-dom'
|
||||||
|
|
||||||
|
import ErrorScreen from '@/components/ErrorScreen'
|
||||||
import PostEmbed from '@/components/PostEmbed'
|
import PostEmbed from '@/components/PostEmbed'
|
||||||
|
import PrefetchLink from '@/components/PrefetchLink'
|
||||||
|
import TagDetailSidebar from '@/components/TagDetailSidebar'
|
||||||
import MainArea from '@/components/layout/MainArea'
|
import MainArea from '@/components/layout/MainArea'
|
||||||
|
import SidebarComponent from '@/components/layout/SidebarComponent'
|
||||||
import { SITE_TITLE } from '@/config'
|
import { SITE_TITLE } from '@/config'
|
||||||
import { apiGet, apiPatch, apiPut } from '@/lib/api'
|
import { apiGet, apiPatch, apiPost, apiPut } from '@/lib/api'
|
||||||
import { fetchPost } from '@/lib/posts'
|
import { fetchPost } from '@/lib/posts'
|
||||||
|
import { dateString } from '@/lib/utils'
|
||||||
|
|
||||||
import type { FC } from 'react'
|
import type { FC } from 'react'
|
||||||
|
|
||||||
import type { NiconicoMetadata, NiconicoViewerHandle, Post, Theatre } from '@/types'
|
import type { NiconicoMetadata,
|
||||||
|
NiconicoViewerHandle,
|
||||||
|
Post,
|
||||||
|
Theatre,
|
||||||
|
TheatreComment } from '@/types'
|
||||||
|
|
||||||
type TheatreInfo = {
|
type TheatreInfo = {
|
||||||
hostFlg: boolean
|
hostFlg: boolean
|
||||||
postId: number | null
|
postId: number | null
|
||||||
postStartedAt: string | null }
|
postStartedAt: string | null
|
||||||
|
watchingUsers: { id: number; name: string }[] }
|
||||||
|
|
||||||
|
const INITIAL_THEATRE_INFO =
|
||||||
|
{ hostFlg: false,
|
||||||
|
postId: null,
|
||||||
|
postStartedAt: null,
|
||||||
|
watchingUsers: [] as { id: number; name: string }[] } as const
|
||||||
|
|
||||||
|
|
||||||
export default (() => {
|
export default (() => {
|
||||||
const { id } = useParams ()
|
const { id } = useParams ()
|
||||||
|
|
||||||
|
const commentsRef = useRef<HTMLDivElement> (null)
|
||||||
const embedRef = useRef<NiconicoViewerHandle> (null)
|
const embedRef = useRef<NiconicoViewerHandle> (null)
|
||||||
|
const theatreInfoRef = useRef<TheatreInfo> (INITIAL_THEATRE_INFO)
|
||||||
|
const videoLengthRef = useRef (0)
|
||||||
|
const lastCommentNoRef = useRef (0)
|
||||||
|
|
||||||
|
const [comments, setComments] = useState<TheatreComment[]> ([])
|
||||||
|
const [content, setContent] = useState ('')
|
||||||
const [loading, setLoading] = useState (false)
|
const [loading, setLoading] = useState (false)
|
||||||
|
const [sending, setSending] = useState (false)
|
||||||
|
const [status, setStatus] = useState (200)
|
||||||
const [theatre, setTheatre] = useState<Theatre | null> (null)
|
const [theatre, setTheatre] = useState<Theatre | null> (null)
|
||||||
const [theatreInfo, setTheatreInfo] =
|
const [theatreInfo, setTheatreInfo] = useState<TheatreInfo> (INITIAL_THEATRE_INFO)
|
||||||
useState<TheatreInfo> ({ hostFlg: false, postId: null, postStartedAt: null })
|
|
||||||
const [post, setPost] = useState<Post | null> (null)
|
const [post, setPost] = useState<Post | null> (null)
|
||||||
const [videoLength, setVideoLength] = useState (9_999_999_999)
|
const [videoLength, setVideoLength] = useState (0)
|
||||||
|
|
||||||
|
useEffect (() => {
|
||||||
|
theatreInfoRef.current = theatreInfo
|
||||||
|
}, [theatreInfo])
|
||||||
|
|
||||||
|
useEffect (() => {
|
||||||
|
videoLengthRef.current = videoLength
|
||||||
|
}, [videoLength])
|
||||||
|
|
||||||
|
useEffect (() => {
|
||||||
|
lastCommentNoRef.current = comments[0]?.no ?? 0
|
||||||
|
}, [comments])
|
||||||
|
|
||||||
useEffect (() => {
|
useEffect (() => {
|
||||||
if (!(id))
|
if (!(id))
|
||||||
return
|
return
|
||||||
|
|
||||||
|
let cancelled = false
|
||||||
|
|
||||||
|
setComments ([])
|
||||||
|
setTheatre (null)
|
||||||
|
setPost (null)
|
||||||
|
setTheatreInfo (INITIAL_THEATRE_INFO)
|
||||||
|
setVideoLength (0)
|
||||||
|
lastCommentNoRef.current = 0
|
||||||
|
|
||||||
void (async () => {
|
void (async () => {
|
||||||
setTheatre (await apiGet<Theatre> (`/theatres/${ id }`))
|
try
|
||||||
|
{
|
||||||
|
const data = await apiGet<Theatre> (`/theatres/${ id }`)
|
||||||
|
if (!(cancelled))
|
||||||
|
setTheatre (data)
|
||||||
|
}
|
||||||
|
catch (error)
|
||||||
|
{
|
||||||
|
setStatus ((error as any)?.response.status ?? 200)
|
||||||
|
}
|
||||||
}) ()
|
}) ()
|
||||||
|
|
||||||
const interval = setInterval (async () => {
|
return () => {
|
||||||
if (theatreInfo.hostFlg
|
cancelled = true
|
||||||
&& theatreInfo.postStartedAt
|
}
|
||||||
&& ((new Date).getTime () - (new Date (theatreInfo.postStartedAt)).getTime ()
|
}, [id])
|
||||||
> videoLength))
|
|
||||||
setTheatreInfo ({ hostFlg: true, postId: null, postStartedAt: null })
|
|
||||||
else
|
|
||||||
setTheatreInfo (await apiPut<TheatreInfo> (`/theatres/${ id }/watching`))
|
|
||||||
}, 1_000)
|
|
||||||
|
|
||||||
return () => clearInterval (interval)
|
|
||||||
}, [id, theatreInfo.hostFlg, theatreInfo.postStartedAt, videoLength])
|
|
||||||
|
|
||||||
useEffect (() => {
|
useEffect (() => {
|
||||||
if (!(theatreInfo.hostFlg) || loading)
|
if (!(id))
|
||||||
return
|
return
|
||||||
|
|
||||||
if (theatreInfo.postId == null)
|
let cancelled = false
|
||||||
|
let running = false
|
||||||
|
|
||||||
|
const tick = async () => {
|
||||||
|
if (running)
|
||||||
|
return
|
||||||
|
|
||||||
|
running = true
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
void (async () => {
|
const newComments = await apiGet<TheatreComment[]> (
|
||||||
setLoading (true)
|
`/theatres/${ id }/comments`,
|
||||||
await apiPatch<void> (`/theatres/${ id }/next_post`)
|
{ params: { no_gt: lastCommentNoRef.current } })
|
||||||
setLoading (false)
|
|
||||||
}) ()
|
if (!(cancelled) && newComments.length > 0)
|
||||||
|
{
|
||||||
|
lastCommentNoRef.current = newComments[newComments.length - 1].no
|
||||||
|
setComments (prev => [...newComments, ...prev])
|
||||||
|
}
|
||||||
|
|
||||||
|
const currentInfo = theatreInfoRef.current
|
||||||
|
const ended =
|
||||||
|
currentInfo.hostFlg
|
||||||
|
&& currentInfo.postStartedAt
|
||||||
|
&& ((Date.now () - (new Date (currentInfo.postStartedAt)).getTime ())
|
||||||
|
> videoLengthRef.current + 3_000)
|
||||||
|
|
||||||
|
if (ended)
|
||||||
|
{
|
||||||
|
if (!(cancelled))
|
||||||
|
setTheatreInfo (prev => ({ ...prev, postId: null, postStartedAt: null }))
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}, [id, loading, theatreInfo.hostFlg, theatreInfo.postId])
|
|
||||||
|
const nextInfo = await apiPut<TheatreInfo> (`/theatres/${ id }/watching`)
|
||||||
|
if (!(cancelled))
|
||||||
|
setTheatreInfo (nextInfo)
|
||||||
|
}
|
||||||
|
catch (error)
|
||||||
|
{
|
||||||
|
console.error (error)
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
running = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tick ()
|
||||||
|
const interval = setInterval (() => tick (), 1_500)
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
cancelled = true
|
||||||
|
clearInterval (interval)
|
||||||
|
}
|
||||||
|
}, [id])
|
||||||
|
|
||||||
useEffect (() => {
|
useEffect (() => {
|
||||||
|
if (!(id) || !(theatreInfo.hostFlg) || loading || theatreInfo.postId != null)
|
||||||
|
return
|
||||||
|
|
||||||
|
let cancelled = false
|
||||||
|
|
||||||
|
void (async () => {
|
||||||
|
setLoading (true)
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await apiPatch<void> (`/theatres/${ id }/next_post`)
|
||||||
|
}
|
||||||
|
catch (error)
|
||||||
|
{
|
||||||
|
console.error (error)
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (!(cancelled))
|
||||||
|
setLoading (false)
|
||||||
|
}
|
||||||
|
}) ()
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
cancelled = true
|
||||||
|
}
|
||||||
|
}, [id, theatreInfo.hostFlg, theatreInfo.postId])
|
||||||
|
|
||||||
|
useEffect (() => {
|
||||||
|
setVideoLength (0)
|
||||||
|
|
||||||
if (theatreInfo.postId == null)
|
if (theatreInfo.postId == null)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
let cancelled = false
|
||||||
|
|
||||||
void (async () => {
|
void (async () => {
|
||||||
setPost (await fetchPost (String (theatreInfo.postId)))
|
try
|
||||||
|
{
|
||||||
|
const nextPost = await fetchPost (String (theatreInfo.postId))
|
||||||
|
if (!(cancelled))
|
||||||
|
setPost (nextPost)
|
||||||
|
}
|
||||||
|
catch (error)
|
||||||
|
{
|
||||||
|
console.error (error)
|
||||||
|
}
|
||||||
}) ()
|
}) ()
|
||||||
}, [theatreInfo.postId, theatreInfo.postStartedAt])
|
|
||||||
|
return () => {
|
||||||
|
cancelled = true
|
||||||
|
}
|
||||||
|
}, [theatreInfo.postId])
|
||||||
|
|
||||||
const syncPlayback = (meta: NiconicoMetadata) => {
|
const syncPlayback = (meta: NiconicoMetadata) => {
|
||||||
if (!(theatreInfo.postStartedAt))
|
if (!(theatreInfo.postStartedAt))
|
||||||
return
|
return
|
||||||
|
|
||||||
const targetTime =
|
const targetTime = Math.min (
|
||||||
((new Date).getTime () - (new Date (theatreInfo.postStartedAt)).getTime ())
|
Math.max (0, Date.now () - (new Date (theatreInfo.postStartedAt)).getTime ()),
|
||||||
|
videoLength)
|
||||||
|
|
||||||
const drift = Math.abs (meta.currentTime - targetTime)
|
const drift = Math.abs (meta.currentTime - targetTime)
|
||||||
|
|
||||||
@@ -88,8 +228,11 @@ export default (() => {
|
|||||||
embedRef.current?.seek (targetTime)
|
embedRef.current?.seek (targetTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (status >= 400)
|
||||||
|
return <ErrorScreen status={status}/>
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<MainArea>
|
<div className="md:flex md:flex-1">
|
||||||
<Helmet>
|
<Helmet>
|
||||||
{theatre && (
|
{theatre && (
|
||||||
<title>
|
<title>
|
||||||
@@ -99,16 +242,100 @@ export default (() => {
|
|||||||
</title>)}
|
</title>)}
|
||||||
</Helmet>
|
</Helmet>
|
||||||
|
|
||||||
{post && (
|
<div className="hidden md:block">
|
||||||
|
{post && <TagDetailSidebar post={post}/>}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<MainArea>
|
||||||
|
{post ? (
|
||||||
|
<>
|
||||||
<PostEmbed
|
<PostEmbed
|
||||||
|
key={post.id}
|
||||||
ref={embedRef}
|
ref={embedRef}
|
||||||
post={post}
|
post={post}
|
||||||
onLoadComplete={info => {
|
onLoadComplete={info => {
|
||||||
embedRef.current?.play ()
|
embedRef.current?.play ()
|
||||||
setVideoLength (info.lengthInSeconds * 1_000)
|
setVideoLength (info.lengthInSeconds * 1_000)
|
||||||
}}
|
}}
|
||||||
onMetadataChange={meta => {
|
onMetadataChange={syncPlayback}/>
|
||||||
syncPlayback (meta)
|
<div className="m-2">
|
||||||
}}/>)}
|
<>再生中:</>
|
||||||
</MainArea>)
|
<PrefetchLink to={`/posts/${ post.id }`} className="font-bold">
|
||||||
|
{post.title || post.url}
|
||||||
|
</PrefetchLink>
|
||||||
|
</div>
|
||||||
|
</>) : 'Loading...'}
|
||||||
|
</MainArea>
|
||||||
|
|
||||||
|
<SidebarComponent>
|
||||||
|
<form
|
||||||
|
className="w-auto h-auto border border-black dark:border-white rounded mx-2"
|
||||||
|
onSubmit={async e => {
|
||||||
|
e.preventDefault ()
|
||||||
|
|
||||||
|
if (!(content))
|
||||||
|
return
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
setSending (true)
|
||||||
|
await apiPost (`/theatres/${ id }/comments`, { content })
|
||||||
|
setContent ('')
|
||||||
|
commentsRef.current?.scrollTo ({ top: 0, behavior: 'smooth' })
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
setSending (false)
|
||||||
|
}
|
||||||
|
}}>
|
||||||
|
<input
|
||||||
|
className="w-full p-2 border rounded"
|
||||||
|
type="text"
|
||||||
|
placeholder="ここにコメントを入力"
|
||||||
|
value={content}
|
||||||
|
onChange={e => setContent (e.target.value)}
|
||||||
|
disabled={sending}/>
|
||||||
|
|
||||||
|
<div
|
||||||
|
ref={commentsRef}
|
||||||
|
className="overflow-x-hidden overflow-y-scroll text-wrap w-full
|
||||||
|
h-[32vh] md:h-[64vh] border rounded">
|
||||||
|
{comments.map (comment => (
|
||||||
|
<div key={comment.no} className="p-2">
|
||||||
|
<div className="w-full">
|
||||||
|
{comment.content}
|
||||||
|
</div>
|
||||||
|
<div className="w-full text-sm text-right">
|
||||||
|
by {comment.user
|
||||||
|
? (comment.user.name || `名もなきニジラー(#${ comment.user.id })`)
|
||||||
|
: '運営'}
|
||||||
|
</div>
|
||||||
|
<div className="w-full text-sm text-right">
|
||||||
|
{dateString (comment.createdAt)}
|
||||||
|
</div>
|
||||||
|
</div>))}
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<div className="w-auto h-auto border border-black dark:border-white rounded mx-2 mt-4">
|
||||||
|
<div className="p-2">
|
||||||
|
現在の同接数:{theatreInfo.watchingUsers.length}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="overflow-x-hidden overflow-y-scroll text-wrap w-full h-32
|
||||||
|
border rounded">
|
||||||
|
<ul className="list-inside list-disc">
|
||||||
|
{theatreInfo.watchingUsers.map (user => (
|
||||||
|
<li key={user.id} className="px-4 py-1 text-sm">
|
||||||
|
{user.name || `名もなきニジラー(#${ user.id })`}
|
||||||
|
</li>))}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</SidebarComponent>
|
||||||
|
|
||||||
|
<div className="md:hidden">
|
||||||
|
{post && <TagDetailSidebar post={post} sp/>}
|
||||||
|
</div>
|
||||||
|
</div>)
|
||||||
}) satisfies FC
|
}) satisfies FC
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
import { useQueryClient } from '@tanstack/react-query'
|
import { useQueryClient } from '@tanstack/react-query'
|
||||||
import MarkdownIt from 'markdown-it'
|
|
||||||
import { useEffect, useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
import { Helmet } from 'react-helmet-async'
|
import { Helmet } from 'react-helmet-async'
|
||||||
import MdEditor from 'react-markdown-editor-lite'
|
|
||||||
import { useParams, useNavigate } from 'react-router-dom'
|
import { useParams, useNavigate } from 'react-router-dom'
|
||||||
|
|
||||||
|
import WikiEditForm from '@/components/WikiEditForm'
|
||||||
import MainArea from '@/components/layout/MainArea'
|
import MainArea from '@/components/layout/MainArea'
|
||||||
import { toast } from '@/components/ui/use-toast'
|
import { toast } from '@/components/ui/use-toast'
|
||||||
import { SITE_TITLE } from '@/config'
|
import { SITE_TITLE } from '@/config'
|
||||||
@@ -18,8 +17,6 @@ import type { FC } from 'react'
|
|||||||
|
|
||||||
import type { User, WikiPage } from '@/types'
|
import type { User, WikiPage } from '@/types'
|
||||||
|
|
||||||
const mdParser = new MarkdownIt
|
|
||||||
|
|
||||||
type Props = { user: User | null }
|
type Props = { user: User | null }
|
||||||
|
|
||||||
|
|
||||||
@@ -37,7 +34,7 @@ export default (({ user }: Props) => {
|
|||||||
const [loading, setLoading] = useState (true)
|
const [loading, setLoading] = useState (true)
|
||||||
const [title, setTitle] = useState ('')
|
const [title, setTitle] = useState ('')
|
||||||
|
|
||||||
const handleSubmit = async () => {
|
const handleSubmit = async (title: string, body: string) => {
|
||||||
const formData = new FormData ()
|
const formData = new FormData ()
|
||||||
formData.append ('title', title)
|
formData.append ('title', title)
|
||||||
formData.append ('body', body)
|
formData.append ('body', body)
|
||||||
@@ -46,8 +43,6 @@ export default (({ user }: Props) => {
|
|||||||
{
|
{
|
||||||
await apiPut (`/wiki/${ id }`, formData,
|
await apiPut (`/wiki/${ id }`, formData,
|
||||||
{ headers: { 'Content-Type': 'multipart/form-data' } })
|
{ headers: { 'Content-Type': 'multipart/form-data' } })
|
||||||
qc.setQueryData (wikiKeys.show (title, { }),
|
|
||||||
(prev: WikiPage) => ({ ...prev, title, body }))
|
|
||||||
qc.invalidateQueries ({ queryKey: wikiKeys.root })
|
qc.invalidateQueries ({ queryKey: wikiKeys.root })
|
||||||
toast ({ title: '投稿成功!' })
|
toast ({ title: '投稿成功!' })
|
||||||
navigate (`/wiki/${ title }`)
|
navigate (`/wiki/${ title }`)
|
||||||
@@ -77,32 +72,11 @@ export default (({ user }: Props) => {
|
|||||||
<h1 className="text-2xl font-bold mb-2">Wiki ページを編輯</h1>
|
<h1 className="text-2xl font-bold mb-2">Wiki ページを編輯</h1>
|
||||||
|
|
||||||
{loading ? 'Loading...' : (
|
{loading ? 'Loading...' : (
|
||||||
<>
|
<WikiEditForm
|
||||||
{/* タイトル */}
|
title={title}
|
||||||
{/* TODO: タグ補完 */}
|
body={body}
|
||||||
<div>
|
onSubmit={handleSubmit}
|
||||||
<label className="block font-semibold mb-1">タイトル</label>
|
id={Number (id)}/>)}
|
||||||
<input type="text"
|
|
||||||
value={title}
|
|
||||||
onChange={e => setTitle (e.target.value)}
|
|
||||||
className="w-full border p-2 rounded"/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* 本文 */}
|
|
||||||
<div>
|
|
||||||
<label className="block font-semibold mb-1">本文</label>
|
|
||||||
<MdEditor value={body}
|
|
||||||
style={{ height: '500px' }}
|
|
||||||
renderHTML={text => mdParser.render (text)}
|
|
||||||
onChange={({ text }) => setBody (text)}/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* 送信 */}
|
|
||||||
<button onClick={handleSubmit}
|
|
||||||
className="px-4 py-2 bg-blue-600 text-white rounded disabled:bg-gray-400">
|
|
||||||
編輯
|
|
||||||
</button>
|
|
||||||
</>)}
|
|
||||||
</div>
|
</div>
|
||||||
</MainArea>)
|
</MainArea>)
|
||||||
}) satisfies FC<Props>
|
}) satisfies FC<Props>
|
||||||
|
|||||||
@@ -1,21 +1,19 @@
|
|||||||
import MarkdownIt from 'markdown-it'
|
import { useQueryClient } from '@tanstack/react-query'
|
||||||
import { useState } from 'react'
|
|
||||||
import { Helmet } from 'react-helmet-async'
|
import { Helmet } from 'react-helmet-async'
|
||||||
import MdEditor from 'react-markdown-editor-lite'
|
|
||||||
import { useLocation, useNavigate } from 'react-router-dom'
|
import { useLocation, useNavigate } from 'react-router-dom'
|
||||||
|
|
||||||
|
import WikiEditForm from '@/components/WikiEditForm'
|
||||||
import MainArea from '@/components/layout/MainArea'
|
import MainArea from '@/components/layout/MainArea'
|
||||||
import { toast } from '@/components/ui/use-toast'
|
import { toast } from '@/components/ui/use-toast'
|
||||||
import { SITE_TITLE } from '@/config'
|
import { SITE_TITLE } from '@/config'
|
||||||
import { apiPost } from '@/lib/api'
|
import { apiPost } from '@/lib/api'
|
||||||
|
import { wikiKeys } from '@/lib/queryKeys'
|
||||||
import Forbidden from '@/pages/Forbidden'
|
import Forbidden from '@/pages/Forbidden'
|
||||||
|
|
||||||
import 'react-markdown-editor-lite/lib/index.css'
|
import 'react-markdown-editor-lite/lib/index.css'
|
||||||
|
|
||||||
import type { User, WikiPage } from '@/types'
|
import type { User, WikiPage } from '@/types'
|
||||||
|
|
||||||
const mdParser = new MarkdownIt
|
|
||||||
|
|
||||||
type Props = { user: User | null }
|
type Props = { user: User | null }
|
||||||
|
|
||||||
|
|
||||||
@@ -26,13 +24,12 @@ export default ({ user }: Props) => {
|
|||||||
const location = useLocation ()
|
const location = useLocation ()
|
||||||
const navigate = useNavigate ()
|
const navigate = useNavigate ()
|
||||||
|
|
||||||
|
const qc = useQueryClient ()
|
||||||
|
|
||||||
const query = new URLSearchParams (location.search)
|
const query = new URLSearchParams (location.search)
|
||||||
const titleQuery = query.get ('title') ?? ''
|
const titleQuery = query.get ('title') ?? ''
|
||||||
|
|
||||||
const [title, setTitle] = useState (titleQuery)
|
const handleSubmit = async (title: string, body: string) => {
|
||||||
const [body, setBody] = useState ('')
|
|
||||||
|
|
||||||
const handleSubmit = async () => {
|
|
||||||
const formData = new FormData
|
const formData = new FormData
|
||||||
formData.append ('title', title)
|
formData.append ('title', title)
|
||||||
formData.append ('body', body)
|
formData.append ('body', body)
|
||||||
@@ -41,6 +38,7 @@ export default ({ user }: Props) => {
|
|||||||
{
|
{
|
||||||
const data = await apiPost<WikiPage> ('/wiki', formData,
|
const data = await apiPost<WikiPage> ('/wiki', formData,
|
||||||
{ headers: { 'Content-Type': 'multipart/form-data' } })
|
{ headers: { 'Content-Type': 'multipart/form-data' } })
|
||||||
|
qc.invalidateQueries ({ queryKey: wikiKeys.root })
|
||||||
toast ({ title: '投稿成功!' })
|
toast ({ title: '投稿成功!' })
|
||||||
navigate (`/wiki/${ data.title }`)
|
navigate (`/wiki/${ data.title }`)
|
||||||
}
|
}
|
||||||
@@ -58,30 +56,10 @@ export default ({ user }: Props) => {
|
|||||||
<div className="max-w-xl mx-auto p-4 space-y-4">
|
<div className="max-w-xl mx-auto p-4 space-y-4">
|
||||||
<h1 className="text-2xl font-bold mb-2">新規 Wiki ページ</h1>
|
<h1 className="text-2xl font-bold mb-2">新規 Wiki ページ</h1>
|
||||||
|
|
||||||
{/* タイトル */}
|
<WikiEditForm
|
||||||
{/* TODO: タグ補完 */}
|
title={titleQuery}
|
||||||
<div>
|
body=""
|
||||||
<label className="block font-semibold mb-1">タイトル</label>
|
onSubmit={handleSubmit}/>
|
||||||
<input type="text"
|
|
||||||
value={title}
|
|
||||||
onChange={e => setTitle (e.target.value)}
|
|
||||||
className="w-full border p-2 rounded"/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* 本文 */}
|
|
||||||
<div>
|
|
||||||
<label className="block font-semibold mb-1">本文</label>
|
|
||||||
<MdEditor value={body}
|
|
||||||
style={{ height: '500px' }}
|
|
||||||
renderHTML={text => mdParser.render (text)}
|
|
||||||
onChange={({ text }) => setBody (text)}/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* 送信 */}
|
|
||||||
<button onClick={handleSubmit}
|
|
||||||
className="px-4 py-2 bg-blue-600 text-white rounded disabled:bg-gray-400">
|
|
||||||
追加
|
|
||||||
</button>
|
|
||||||
</div>
|
</div>
|
||||||
</MainArea>)
|
</MainArea>)
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-2
@@ -52,7 +52,7 @@ export type FetchTagsParams = {
|
|||||||
export type Menu = MenuItem[]
|
export type Menu = MenuItem[]
|
||||||
|
|
||||||
export type MenuItem = {
|
export type MenuItem = {
|
||||||
name: string
|
name: ReactNode
|
||||||
to: string
|
to: string
|
||||||
base?: string
|
base?: string
|
||||||
subMenu: SubMenuItem[] }
|
subMenu: SubMenuItem[] }
|
||||||
@@ -117,7 +117,7 @@ export type PostTagChange = {
|
|||||||
export type SubMenuItem =
|
export type SubMenuItem =
|
||||||
| { component: ReactNode
|
| { component: ReactNode
|
||||||
visible: boolean }
|
visible: boolean }
|
||||||
| { name: string
|
| { name: ReactNode
|
||||||
to: string
|
to: string
|
||||||
visible?: boolean }
|
visible?: boolean }
|
||||||
|
|
||||||
@@ -141,6 +141,13 @@ export type Theatre = {
|
|||||||
createdAt: string
|
createdAt: string
|
||||||
updatedAt: string }
|
updatedAt: string }
|
||||||
|
|
||||||
|
export type TheatreComment = {
|
||||||
|
theatreId: number,
|
||||||
|
no: number,
|
||||||
|
user: { id: number, name: string } | null
|
||||||
|
content: string
|
||||||
|
createdAt: string }
|
||||||
|
|
||||||
export type User = {
|
export type User = {
|
||||||
id: number
|
id: number
|
||||||
name: string | null
|
name: string | null
|
||||||
@@ -149,6 +156,11 @@ export type User = {
|
|||||||
|
|
||||||
export type ViewFlagBehavior = typeof ViewFlagBehavior[keyof typeof ViewFlagBehavior]
|
export type ViewFlagBehavior = typeof ViewFlagBehavior[keyof typeof ViewFlagBehavior]
|
||||||
|
|
||||||
|
export type WikiAsset = {
|
||||||
|
wikiPageId: number
|
||||||
|
no: number
|
||||||
|
url: string }
|
||||||
|
|
||||||
export type WikiPage = {
|
export type WikiPage = {
|
||||||
id: number
|
id: number
|
||||||
title: string
|
title: string
|
||||||
|
|||||||
Reference in New Issue
Block a user