掲示板実装
This commit is contained in:
4
app/channels/application_cable/channel.rb
Normal file
4
app/channels/application_cable/channel.rb
Normal file
@@ -0,0 +1,4 @@
|
||||
module ApplicationCable
|
||||
class Channel < ActionCable::Channel::Base
|
||||
end
|
||||
end
|
||||
37
app/channels/application_cable/connection.rb
Normal file
37
app/channels/application_cable/connection.rb
Normal file
@@ -0,0 +1,37 @@
|
||||
module ApplicationCable
|
||||
class Connection < ActionCable::Connection::Base
|
||||
identified_by :current_user
|
||||
|
||||
def connect
|
||||
self.current_user = find_verified_user
|
||||
end
|
||||
|
||||
private
|
||||
def find_verified_user
|
||||
# セッションからuser_idを取得してUserを特定する
|
||||
# 注: セッションストアの設定に依存するが、CookieStore(デフォルト)を想定
|
||||
|
||||
if verified_user = User.find_by(id: session_user_id)
|
||||
verified_user
|
||||
else
|
||||
reject_unauthorized_connection
|
||||
end
|
||||
end
|
||||
|
||||
def session_user_id
|
||||
# RailsのセッションCookieからuser_idを復元する
|
||||
# _dip_front_session は config/initializers/session_store.rb で設定されているキー名、またはデフォルトの _app_session
|
||||
# ここでは汎用的に取得を試みる
|
||||
|
||||
session_key = Rails.application.config.session_options[:key]
|
||||
encrypted_session = cookies.encrypted[session_key]
|
||||
|
||||
if encrypted_session && encrypted_session["user_id"]
|
||||
encrypted_session["user_id"]
|
||||
else
|
||||
# デバッグ用: 認証失敗時はnilを返す
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
34
app/channels/board_channel.rb
Normal file
34
app/channels/board_channel.rb
Normal file
@@ -0,0 +1,34 @@
|
||||
class BoardChannel < ApplicationCable::Channel
|
||||
def subscribed
|
||||
board = Board.find_by(id: params[:board_id])
|
||||
|
||||
unless board
|
||||
reject
|
||||
return
|
||||
end
|
||||
|
||||
# 権限チェック
|
||||
# ゲーム参加者を取得(current_userから)
|
||||
current_participant = board.game.participants.find_by(user: current_user)
|
||||
|
||||
# 参加権限があるか(メンバー、または終了済み、または公開ボード)
|
||||
# コントローラの判定ロジックと合わせる
|
||||
# 共通掲示板は誰でも購読OK(ただしゲーム参加者に限る)
|
||||
|
||||
if board.global?
|
||||
stream_from "board_#{board.id}" if current_participant
|
||||
elsif board.is_public?
|
||||
stream_from "board_#{board.id}" if current_participant
|
||||
elsif board.member?(current_participant)
|
||||
stream_from "board_#{board.id}"
|
||||
elsif board.history_mode?
|
||||
stream_from "board_#{board.id}" if current_participant
|
||||
else
|
||||
reject
|
||||
end
|
||||
end
|
||||
|
||||
def unsubscribed
|
||||
# Any cleanup needed when channel is unsubscribed
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user