-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Draft through API for Cross-Device Support (#8018)
- Add APIs for cross-device message drafts ref: #6890
- Loading branch information
Showing
4 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
app/controllers/api/v1/accounts/conversations/draft_messages_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
spec/controllers/api/v1/accounts/conversations/draft_messages_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |