mirror of
https://github.com/we-promise/sure.git
synced 2026-09-03 13:51:29 +00:00
521946b9d6
* fix(messages): handle chat not found during message creation * test(messages): cover missing chat race --------- Co-authored-by: sentry[bot] <39604003+sentry[bot]@users.noreply.github.com> Co-authored-by: sure-admin <sure-admin@splashblot.com>
42 lines
1.1 KiB
Ruby
42 lines
1.1 KiB
Ruby
class MessagesController < ApplicationController
|
|
guard_feature unless: -> { Current.user.ai_enabled? }
|
|
|
|
before_action :set_chat
|
|
|
|
def create
|
|
@message = UserMessage.new(
|
|
chat: @chat,
|
|
content: message_params[:content],
|
|
ai_model: message_params[:ai_model].presence || Chat.default_model
|
|
)
|
|
|
|
if @message.save
|
|
redirect_to chat_path(@chat, thinking: true)
|
|
else
|
|
redirect_to chat_path(@chat), alert: @message.errors.full_messages.to_sentence
|
|
end
|
|
rescue ActiveRecord::InvalidForeignKey
|
|
redirect_to chats_path, alert: t(".chat_not_found")
|
|
end
|
|
|
|
# Called by the chat watchdog when an assistant "Thinking…" bubble has waited
|
|
# too long for a response that never arrived. Idempotent and scoped to the
|
|
# current user's chat.
|
|
def report_timeout
|
|
message = @chat.messages.find(params[:id])
|
|
@chat.handle_undelivered_response!(message)
|
|
head :ok
|
|
rescue ActiveRecord::RecordNotFound
|
|
head :not_found
|
|
end
|
|
|
|
private
|
|
def set_chat
|
|
@chat = Current.user.chats.find(params[:chat_id])
|
|
end
|
|
|
|
def message_params
|
|
params.require(:message).permit(:content, :ai_model)
|
|
end
|
|
end
|