module Api # The edit lock of a server document: one browser tab edits at a time, # everyone else sees the document read-only until the lock is released or # expires (Document::LOCK_TTL without a heartbeat). class DocumentLocksController < BaseController before_action :load_document # POST /api/documents/:id/lock -- take or refresh (heartbeat) def create return render(json: { lock: { mine: false, holder_name: nil } }) unless Document.role_at_least?(@role, :editor) mine = @doc.acquire_lock!(lock_holder, holder_name) render json: { lock: { mine:, holder_name: (@doc.lock_holder_name unless mine) } } end # DELETE /api/documents/:id/lock def destroy @doc.release_lock!(lock_holder) head :no_content end private def load_document @doc = Document.find(params[:document_id]) @role = @doc.role_for(user: Current.user, token: share_token) raise ActiveRecord::RecordNotFound unless @role end end end