Skip to content

Commit

Permalink
feat: Draft through API for Cross-Device Support (#8018)
Browse files Browse the repository at this point in the history
- Add APIs for cross-device message drafts

ref: #6890
  • Loading branch information
jderecho committed May 3, 2024
1 parent b24626a commit 0d7773d
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Api::V1::Accounts::Conversations::DraftMessagesController < Api::V1::Accounts::Conversations::BaseController
def show
render json: { has_draft: false } and return unless Redis::Alfred.exists?(draft_redis_key)

draft_message = Redis::Alfred.get(draft_redis_key)
render json: { has_draft: true, message: draft_message }
end

def update
Redis::Alfred.set(draft_redis_key, draft_message_params)
head :ok
end

def destroy
Redis::Alfred.delete(draft_redis_key)
head :ok
end

private

def draft_redis_key
format(Redis::Alfred::CONVERSATION_DRAFT_MESSAGE, id: @conversation.id)
end

def draft_message_params
params.dig(:draft_message, :message) || ''
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
resources :labels, only: [:create, :index]
resource :participants, only: [:show, :create, :update, :destroy]
resource :direct_uploads, only: [:create]
resource :draft_messages, only: [:show, :update, :destroy]
end
member do
post :mute
Expand Down
1 change: 1 addition & 0 deletions lib/redis/redis_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Redis::RedisKeys
CONVERSATION_MAILER_KEY = 'CONVERSATION::%<conversation_id>d'.freeze
# Whether a conversation is muted ?
CONVERSATION_MUTE_KEY = 'CONVERSATION::%<id>d::MUTED'.freeze
CONVERSATION_DRAFT_MESSAGE = 'CONVERSATION::%<id>d::DRAFT_MESSAGE'.freeze

## User Keys
# SSO Auth Tokens
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require 'rails_helper'

RSpec.describe 'Conversation Draft Messages API', type: :request do
let(:account) { create(:account) }

describe 'POST /api/v1/accounts/{account.id}/conversations/<id>/draft_messages' do
let(:conversation) { create(:conversation, account: account) }
let(:cache_key) { format(Redis::Alfred::CONVERSATION_DRAFT_MESSAGE, id: conversation.id) }

context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get api_v1_account_conversation_draft_messages_url(account_id: account.id, conversation_id: conversation.display_id)

expect(response).to have_http_status(:unauthorized)
end
end

context 'when it is an authenticated user with access to the inbox' do
let(:agent) { create(:user, account: account, role: :agent) }
let(:message) { Faker::Lorem.paragraph }

before do
create(:inbox_member, inbox: conversation.inbox, user: agent)
end

it 'saves the draft message for the conversation' do
params = { draft_message: { message: message } }

patch api_v1_account_conversation_draft_messages_url(account_id: account.id, conversation_id: conversation.display_id),
params: params,
headers: agent.create_new_auth_token,
as: :json

expect(response).to have_http_status(:success)
expect(Redis::Alfred.get(cache_key)).to eq(params[:draft_message][:message])
end

it 'gets the draft message for the conversation' do
Redis::Alfred.set(cache_key, message)

get api_v1_account_conversation_draft_messages_url(account_id: account.id, conversation_id: conversation.display_id),
headers: agent.create_new_auth_token,
as: :json

expect(response).to have_http_status(:success)
expect(response.body).to include(message)
end

it 'removes the draft messages for the conversation' do
Redis::Alfred.set(cache_key, message)
expect(Redis::Alfred.get(cache_key)).to eq(message)

delete api_v1_account_conversation_draft_messages_url(account_id: account.id, conversation_id: conversation.display_id),
headers: agent.create_new_auth_token,
as: :json

expect(response).to have_http_status(:success)
expect(Redis::Alfred.get(cache_key)).to be_nil
end
end
end
end

0 comments on commit 0d7773d

Please sign in to comment.