mirror of
https://github.com/we-promise/sure.git
synced 2026-07-12 21:05:20 +00:00
Follow-up to #2405. Replace remaining API controller reads of Current.user, Current.family, and Current.session with current_resource_owner. Add an architecture guard test to prevent regression. Scope is limited to the Current sweep only: - Revert balance_sheet user-scoping (moves to account-auth PR B). - Revert provider_connections DebugLogEntry logging (separate PR). - Remove UsersController#destroy attempt to destroy unsaved API session.
90 lines
2.6 KiB
Ruby
90 lines
2.6 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class Api::V1::ChatsController < Api::V1::BaseController
|
|
include Pagy::Backend
|
|
before_action :require_ai_enabled
|
|
before_action :ensure_read_scope, only: [ :index, :show ]
|
|
before_action :ensure_write_scope, only: [ :create, :update, :destroy ]
|
|
before_action :set_chat, only: [ :show, :update, :destroy ]
|
|
|
|
def index
|
|
@pagy, @chats = pagy(current_resource_owner.chats.ordered, items: 20)
|
|
end
|
|
|
|
def show
|
|
return unless @chat
|
|
@pagy, @messages = pagy(@chat.messages.ordered, items: 50)
|
|
end
|
|
|
|
def create
|
|
@chat = current_resource_owner.chats.build(title: chat_params[:title])
|
|
|
|
if @chat.save
|
|
if chat_params[:message].present?
|
|
@message = @chat.messages.build(
|
|
content: chat_params[:message],
|
|
type: "UserMessage",
|
|
ai_model: chat_params[:model].presence || Chat.default_model
|
|
)
|
|
|
|
if @message.save
|
|
# NOTE: Commenting out duplicate job enqueue to fix mobile app receiving duplicate AI responses
|
|
# UserMessage model already triggers AssistantResponseJob via after_create_commit callback
|
|
# in app/models/user_message.rb:10-12, so this manual enqueue causes the job to run twice,
|
|
# resulting in duplicate AI responses with different content and wasted tokens.
|
|
# See: https://github.com/dwvwdv/sure (mobile app integration issue)
|
|
# AssistantResponseJob.perform_later(@message)
|
|
render :show, status: :created
|
|
else
|
|
@chat.destroy
|
|
render json: { error: "Failed to create initial message", details: @message.errors.full_messages }, status: :unprocessable_entity
|
|
end
|
|
else
|
|
render :show, status: :created
|
|
end
|
|
else
|
|
render json: { error: "Failed to create chat", details: @chat.errors.full_messages }, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def update
|
|
return unless @chat
|
|
|
|
if @chat.update(update_chat_params)
|
|
render :show
|
|
else
|
|
render json: { error: "Failed to update chat", details: @chat.errors.full_messages }, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def destroy
|
|
return unless @chat
|
|
@chat.destroy
|
|
head :no_content
|
|
end
|
|
|
|
private
|
|
|
|
def ensure_read_scope
|
|
authorize_scope!(:read)
|
|
end
|
|
|
|
def ensure_write_scope
|
|
authorize_scope!(:write)
|
|
end
|
|
|
|
def set_chat
|
|
@chat = current_resource_owner.chats.find(params[:id])
|
|
rescue ActiveRecord::RecordNotFound
|
|
render json: { error: "Chat not found" }, status: :not_found
|
|
end
|
|
|
|
def chat_params
|
|
params.permit(:title, :message, :model)
|
|
end
|
|
|
|
def update_chat_params
|
|
params.permit(:title)
|
|
end
|
|
end
|