mirror of
https://github.com/basecamp/once-campfire.git
synced 2026-10-08 15:50:08 +09:00
ac73267b07
Transfer the C completed-response cache lesson into Rails, keeping authentication, room checks and cookies per request. A persistent read-only SQLite observer detects local and foreign commits and rejects racing admission. Whole-page misses render fresh to avoid stale nested fragments; message ETags reflect token-neutral presentation.
65 lines
1.6 KiB
Ruby
65 lines
1.6 KiB
Ruby
class RoomsController < ApplicationController
|
|
before_action :set_room, only: %i[ show destroy ]
|
|
before_action :ensure_can_administer, only: %i[ destroy ]
|
|
before_action :remember_last_room_visited, only: :show
|
|
around_action :cache_read_response, only: :show
|
|
|
|
def index
|
|
redirect_to room_url(Current.user.rooms.last)
|
|
end
|
|
|
|
def show
|
|
@messages = find_messages
|
|
end
|
|
|
|
def destroy
|
|
@room.destroy_later
|
|
|
|
broadcast_remove_room
|
|
redirect_to root_url
|
|
end
|
|
|
|
private
|
|
def set_room
|
|
if room = room_scope.find_by(id: params[:room_id] || params[:id])
|
|
@room = room
|
|
else
|
|
redirect_to root_url, alert: "Room not found or inaccessible"
|
|
end
|
|
end
|
|
|
|
# Subclasses narrow this to the room types they're allowed to act on, so that one
|
|
# room namespace can't be used to reach another's rooms.
|
|
def room_scope
|
|
Current.user.rooms
|
|
end
|
|
|
|
def ensure_can_administer
|
|
head :forbidden unless Current.user.can_administer?(@room)
|
|
end
|
|
|
|
def ensure_permission_to_create_rooms
|
|
if Current.account.settings.restrict_room_creation_to_administrators? && !Current.user.administrator?
|
|
head :forbidden
|
|
end
|
|
end
|
|
|
|
def find_messages
|
|
messages = @room.messages.with_presentation
|
|
|
|
if params[:message_id].present? && (show_first_message = messages.find_by(id: params[:message_id]))
|
|
@messages = messages.page_around(show_first_message)
|
|
else
|
|
@messages = messages.last_page
|
|
end
|
|
end
|
|
|
|
def room_params
|
|
params.require(:room).permit(:name)
|
|
end
|
|
|
|
def broadcast_remove_room
|
|
broadcast_remove_to :rooms, target: [ @room, :list ]
|
|
end
|
|
end
|