fix: Set avatar for users(agent/contact) in slack channels (#7960)
This commit is contained in:
27
app/controllers/slack_uploads_controller.rb
Normal file
27
app/controllers/slack_uploads_controller.rb
Normal file
@@ -0,0 +1,27 @@
|
||||
class SlackUploadsController < ApplicationController
|
||||
include Rails.application.routes.url_helpers
|
||||
before_action :set_blob, only: [:show]
|
||||
|
||||
def show
|
||||
if @blob
|
||||
redirect_to blob_url
|
||||
else
|
||||
redirect_to avatar_url
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_blob
|
||||
@blob = ActiveStorage::Blob.find_by(key: params[:blob_key])
|
||||
end
|
||||
|
||||
def blob_url
|
||||
url_for(@blob.representation(resize_to_fill: [250, nil]))
|
||||
end
|
||||
|
||||
def avatar_url
|
||||
base_url = ENV.fetch('FRONTEND_URL', nil)
|
||||
"#{base_url}/integrations/slack/#{params[:sender_type]}.png"
|
||||
end
|
||||
end
|
||||
@@ -25,6 +25,7 @@ Rails.application.routes.draw do
|
||||
namespace :survey do
|
||||
resources :responses, only: [:show]
|
||||
end
|
||||
resource :slack_uploads, only: [:show]
|
||||
end
|
||||
|
||||
get '/api', to: 'api#index'
|
||||
|
||||
@@ -74,7 +74,13 @@ class Integrations::Slack::SendOnSlackService < Base::SendOnChannelService
|
||||
|
||||
def avatar_url(sender)
|
||||
sender_type = sender.instance_of?(Contact) ? 'contact' : 'user'
|
||||
"#{ENV.fetch('FRONTEND_URL', nil)}/integrations/slack/#{sender_type}.png"
|
||||
blob_key = sender&.avatar&.attached? ? sender.avatar.blob.key : nil
|
||||
generate_url(sender_type, blob_key)
|
||||
end
|
||||
|
||||
def generate_url(sender_type, blob_key)
|
||||
base_url = ENV.fetch('FRONTEND_URL', nil)
|
||||
"#{base_url}/slack_uploads?blob_key=#{blob_key}&sender_type=#{sender_type}"
|
||||
end
|
||||
|
||||
def send_message
|
||||
|
||||
44
spec/controllers/slack_uploads_controller_spec.rb
Normal file
44
spec/controllers/slack_uploads_controller_spec.rb
Normal file
@@ -0,0 +1,44 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe SlackUploadsController do
|
||||
describe 'GET #show' do
|
||||
context 'when a valid blob key is provided' do
|
||||
file = Rack::Test::UploadedFile.new('spec/assets/avatar.png', 'image/png')
|
||||
blob = ActiveStorage::Blob.create_and_upload! io: file, filename: 'avatar.png'
|
||||
|
||||
it 'redirects to the blob service URL' do
|
||||
get :show, params: { blob_key: blob.key }
|
||||
redirect_path = response.location
|
||||
expect(redirect_path).to match(%r{rails/active_storage/representations/redirect/.*/avatar.png})
|
||||
end
|
||||
end
|
||||
|
||||
context 'when an invalid blob key is provided' do
|
||||
it 'returns contact default avatar url if the user is contact' do
|
||||
get :show, params: { key: 'invalid_key', sender_type: 'contact' }
|
||||
redirect_path = response.location
|
||||
expect(redirect_path).to match(%r{integrations/slack/contact.png})
|
||||
end
|
||||
|
||||
it 'returns agent default avatar url if the user is agent' do
|
||||
get :show, params: { key: 'invalid_key', sender_type: 'user' }
|
||||
redirect_path = response.location
|
||||
expect(redirect_path).to match(%r{integrations/slack/user.png})
|
||||
end
|
||||
end
|
||||
|
||||
context 'when no blob key is provided' do
|
||||
it 'returns contact default avatar url if the user is contact' do
|
||||
get :show, params: { sender_type: 'contact' }
|
||||
redirect_path = response.location
|
||||
expect(redirect_path).to match(%r{integrations/slack/contact.png})
|
||||
end
|
||||
|
||||
it 'returns agent default avatar url if the user is agent' do
|
||||
get :show, params: { sender_type: 'user' }
|
||||
redirect_path = response.location
|
||||
expect(redirect_path).to match(%r{integrations/slack/user.png})
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user