mirror of
https://github.com/basecamp/once-campfire.git
synced 2026-08-12 10:00:42 +09:00
80c9e7fbfe
- Use jbuilder instead of hashes
- Use resource instead of direct HTTP verbs
Verbs only make sense if you have one or two routes, if there are
multiple that emulate what resource does then it's better to use resource.
- Paginate using link headers
- Cache responses
58 lines
1.4 KiB
Ruby
58 lines
1.4 KiB
Ruby
class Messages::ByBotsController < MessagesController
|
|
include RawRequestBody
|
|
|
|
allow_bot_access only: %i[ index create ]
|
|
|
|
before_action :set_room
|
|
before_action :ensure_body_or_attachment_present, only: :create
|
|
|
|
def index
|
|
@messages = find_paged_messages
|
|
set_pagination_headers
|
|
end
|
|
|
|
def create
|
|
super
|
|
head :created, location: message_url(@message)
|
|
end
|
|
|
|
private
|
|
def set_room
|
|
@room = Current.user.rooms.find_by(id: params[:room_id])
|
|
|
|
head :not_found unless @room
|
|
end
|
|
|
|
def ensure_body_or_attachment_present
|
|
if params[:attachment].blank? && raw_request_body.blank?
|
|
head :unprocessable_content
|
|
end
|
|
end
|
|
|
|
def set_pagination_headers
|
|
headers["X-Total-Count"] = @room.messages.count.to_s
|
|
|
|
if next_page = next_page_params
|
|
headers["Link"] = %(<#{room_bot_messages_url(@room, params[:bot_key], **next_page)}>; rel="next")
|
|
end
|
|
end
|
|
|
|
def next_page_params
|
|
if @messages.any?
|
|
if params[:after].present?
|
|
{ after: @messages.last.id } if @room.messages.after(@messages.last).exists?
|
|
else
|
|
{ before: @messages.first.id } if @room.messages.before(@messages.first).exists?
|
|
end
|
|
end
|
|
end
|
|
|
|
def message_params
|
|
if params[:attachment]
|
|
params.permit(:attachment)
|
|
else
|
|
{ body: raw_request_body }
|
|
end
|
|
end
|
|
end
|