-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: api to add attachments for mms (#743)
* feat: support to add attachments for mms * feat: api to send mms * misc: use attachment.content * feat: limit attachment mime
- Loading branch information
Showing
4 changed files
with
156 additions
and
54 deletions.
There are no files selected for viewing
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
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,118 @@ | ||
import { | ||
messageIsTextMessage, | ||
} from '@ringcentral-integration/commons/lib/messageHelper'; | ||
|
||
export function findExistedConversation(conversations, phoneNumber) { | ||
return conversations.find((conversation) => { | ||
if (!conversation.to || conversation.to.length > 1) { | ||
return false; | ||
} | ||
if (!messageIsTextMessage(conversation)) { | ||
return false; | ||
} | ||
if (conversation.direction === 'Inbound') { | ||
return conversation.from && ( | ||
conversation.from.phoneNumber === phoneNumber || | ||
conversation.from.extensionNumber === phoneNumber | ||
); | ||
} | ||
return conversation.to.find( | ||
number => ( | ||
number.phoneNumber === phoneNumber || | ||
number.extensionNumber === phoneNumber | ||
) | ||
); | ||
}); | ||
} | ||
|
||
export function setOutputDeviceWhenCall(webphone, audioSettings) { | ||
if (webphone._webphone) { | ||
if (webphone._remoteVideo && webphone._remoteVideo.setSinkId) { | ||
if (audioSettings.outputDeviceId === 'default') { | ||
const defaultDevice = audioSettings.outputDevice; | ||
const defaultDeviceLabel = defaultDevice.label; | ||
const deviceLabel = defaultDeviceLabel.split(' - ')[1]; | ||
if (deviceLabel) { | ||
const device = audioSettings.availableOutputDevices.find( | ||
(device) => device.label === deviceLabel | ||
); | ||
if (device) { | ||
webphone._remoteVideo.setSinkId(device.deviceId); | ||
} | ||
} | ||
} else { | ||
webphone._remoteVideo.setSinkId(audioSettings.outputDeviceId); | ||
} | ||
} | ||
} | ||
} | ||
|
||
const SUPPORTED_MIME = [ | ||
'image/jpeg', | ||
'image/png', | ||
'image/bmp', | ||
'image/gif', | ||
'image/tiff', | ||
'image/svg+xml', | ||
'video/3gpp', | ||
'video/mp4', | ||
'video/mpeg', | ||
'video/msvideo', | ||
'audio/mpeg', | ||
'text/vcard', | ||
'application/zip', | ||
'application/gzip', | ||
'application/rtf' | ||
]; | ||
|
||
function dataURLtoBlob(dataUrl) { | ||
const arr = dataUrl.split(','); | ||
const mime = arr[0].match(/:(.*?);/)[1]; | ||
if (!SUPPORTED_MIME.includes(mime)) { | ||
console.warn('Unsupported mime type:', mime); | ||
return null; | ||
} | ||
const bStr = atob(arr[1]); | ||
let n = bStr.length; | ||
const u8arr = new Uint8Array(n); | ||
while (n--) { | ||
u8arr[n] = bStr.charCodeAt(n); | ||
} | ||
return new Blob([u8arr], { type: mime }); | ||
} | ||
|
||
export function getValidAttachments(attachments = []) { | ||
if (!attachments || !attachments.length) { | ||
return []; | ||
} | ||
const validAttachments = []; | ||
attachments.forEach((attachment) => { | ||
if (!attachment || !attachment.name || !attachment.content) { | ||
console.warn('Invalid attachment:', attachment); | ||
return; | ||
} | ||
if (typeof attachment.name !== 'string') { | ||
console.warn('Invalid attachment name:', attachment.name); | ||
return; | ||
} | ||
// only support base64 url | ||
if ( | ||
typeof attachment.content !== 'string' || | ||
attachment.content.indexOf('data:') !== 0 || | ||
attachment.content.indexOf(';base64,') === -1 | ||
) { | ||
console.warn('Invalid attachment content:', attachment.content); | ||
return; | ||
} | ||
const blob = dataURLtoBlob(attachment.content); | ||
if (!blob) { | ||
return; | ||
} | ||
validAttachments.push({ | ||
name: attachment.name, | ||
file: blob, | ||
size: blob.size, | ||
}); | ||
}); | ||
return validAttachments; | ||
} |
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