Skip to content

Commit

Permalink
CHI-1348 - Use new files-url lambda instead of serverless getDownload…
Browse files Browse the repository at this point in the history
…Url function (#1638)

* Added generateExternalMediaUrl fn

* refactor: abstract away recording path

* refactor: only load metadata to check for error

* chore: remove console log

---------

Co-authored-by: mythilytm <mythily@techmatters.org>
  • Loading branch information
robert-bo-davis and mythilytm authored Sep 8, 2023
1 parent aa664eb commit af15c50
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import CircularProgress from '@material-ui/core/CircularProgress';

import { ErrorFont, LoadMediaButton, LoadMediaButtonText } from './styles';
import { S3StoredRecording } from '../../../types/types';
import { getFileDownloadUrl } from '../../../services/ServerlessService';
import { generateExternalMediaPath } from '../../../services/ContactService';
import fetchHrmApi from '../../../services/fetchHrmApi';

type OwnProps = { contactId: string; externalStoredRecording: S3StoredRecording };

Expand All @@ -34,11 +35,16 @@ const RecordingSection: React.FC<OwnProps> = ({ contactId, externalStoredRecordi
setLoading(true);
setShowButton(false);

const recordingPreSignedUrl = await getFileDownloadUrl(externalStoredRecording.location.key);
const recordingResponse = await fetch(recordingPreSignedUrl.downloadUrl);
const recordingBlob = await recordingResponse.blob();
const voiceRecording = new Audio(URL.createObjectURL(recordingBlob));
setVoiceRecording(voiceRecording.src);
// TODO: this won't currently work for local dev env
const { media_url: recordingPreSignedUrl } = await fetchHrmApi(
generateExternalMediaPath(
contactId,
externalStoredRecording.location.bucket,
externalStoredRecording.location.key,
),
);

setVoiceRecording(recordingPreSignedUrl);

setLoading(false);
} catch (error) {
Expand Down Expand Up @@ -86,7 +92,7 @@ const RecordingSection: React.FC<OwnProps> = ({ contactId, externalStoredRecordi
return (
<div>
{voiceRecording ? (
<audio controls src={voiceRecording} preload="auto" onError={handleFetchAndLoadException}>
<audio controls src={voiceRecording} preload="metadata" onError={handleFetchAndLoadException}>
<track kind="captions" />
</audio>
) : (
Expand Down
4 changes: 4 additions & 0 deletions plugin-hrm-form/src/services/ContactService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
isFailureExternalRecordingInfo,
isSuccessfulExternalRecordingInfo,
} from './getExternalRecordingInfo';
import { generateUrl } from './fetchApi';

type NestedInformation = { name?: { firstName: string; lastName: string } };
type LegacyInformationObject = NestedInformation & {
Expand Down Expand Up @@ -396,3 +397,6 @@ export async function connectToCase(contactId, caseId) {

return fetchHrmApi(`/contacts/${contactId}/connectToCase`, options);
}

export const generateExternalMediaPath = (contactId: string, bucket: string, key: string) =>
`/files/urls?method=getObject&objectType=contact&objectId=${contactId}&fileType=recording&bucket=${bucket}&key=${key}`;
6 changes: 4 additions & 2 deletions plugin-hrm-form/src/services/InsightsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
isSuccessfulExternalRecordingInfo,
} from './getExternalRecordingInfo';
import { generateUrl } from './fetchApi';
import { generateExternalMediaPath } from './ContactService';

/*
* 'Any' is the best we can do, since we're limited by Twilio here.
Expand Down Expand Up @@ -399,12 +400,13 @@ const getInsightsUpdateFunctionsForConfig = (

const generateUrlProviderBlock = (externalRecordingInfo: ExternalRecordingInfoSuccess, contact: HrmServiceContact) => {
const { hrmMicroserviceBaseUrl } = getHrmConfig();

const { bucket, key } = externalRecordingInfo;

const url_provider = generateUrl(
new URL(hrmMicroserviceBaseUrl),
`/files/urls?method=getObject&objectType=contact&objectId=${contact.id}&fileType=recording&bucket=${bucket}&key=${key}`,
generateExternalMediaPath(contact.id, bucket, key),
).toString();

return [
{
type: 'VoiceRecording',
Expand Down

0 comments on commit af15c50

Please sign in to comment.