mirror of
https://github.com/deltachat/deltachat-android.git
synced 2025-10-06 03:49:58 +02:00
Merge pull request #2576 from deltachat/webxdc-update-sendToChat-api
webxdc update "send to chat" api
This commit is contained in:
commit
154efc0e7f
1 changed files with 64 additions and 16 deletions
|
@ -41,26 +41,74 @@ window.webxdc = (() => {
|
|||
},
|
||||
|
||||
sendToChat: async (message) => {
|
||||
const data = {};
|
||||
if (!message.text && !message.file) {
|
||||
return Promise.reject("Invalid empty message, at least one of text or file should be provided");
|
||||
const data = {};
|
||||
if (!message.file && !message.text) {
|
||||
return Promise.reject(
|
||||
"Error from sendToChat: Invalid empty message, at least one of text or file should be provided"
|
||||
);
|
||||
}
|
||||
/** @type {(file: Blob) => Promise<string>} */
|
||||
const blob_to_base64 = (file) => {
|
||||
const data_start = ";base64,";
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.readAsDataURL(file);
|
||||
reader.onload = () => {
|
||||
/** @type {string} */
|
||||
//@ts-ignore
|
||||
let data = reader.result;
|
||||
resolve(data.slice(data.indexOf(data_start) + data_start.length));
|
||||
};
|
||||
reader.onerror = () => reject(reader.error);
|
||||
});
|
||||
};
|
||||
if (message.text) {
|
||||
data.text = message.text;
|
||||
}
|
||||
|
||||
/** @type {{file_name: string, file_message: string} | null} */
|
||||
if (message.file) {
|
||||
let base64content;
|
||||
if (!message.file.name) {
|
||||
return Promise.reject("file name is missing");
|
||||
}
|
||||
if (message.text) {
|
||||
data.text = message.text;
|
||||
}
|
||||
if (message.file) {
|
||||
if (!message.file.name || typeof message.file.base64 !== 'string') {
|
||||
return Promise.reject("provided file is invalid, you need to set both name and base64 content");
|
||||
}
|
||||
data.base64 = message.file.base64;
|
||||
data.name = message.file.name;
|
||||
if (
|
||||
Object.keys(message.file).filter((key) =>
|
||||
["blob", "base64", "plainText"].includes(key)
|
||||
).length > 1
|
||||
) {
|
||||
return Promise.reject(
|
||||
"you can only set one of `blob`, `base64` or `plainText`, not multiple ones"
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
const errorMsg = InternalJSApi.sendToChat(JSON.stringify(data));
|
||||
if (errorMsg) {
|
||||
return Promise.reject(errorMsg);
|
||||
// @ts-ignore - needed because typescript imagines that blob would not exist
|
||||
if (message.file.blob instanceof Blob) {
|
||||
// @ts-ignore - needed because typescript imagines that blob would not exist
|
||||
base64content = await blob_to_base64(message.file.blob);
|
||||
// @ts-ignore - needed because typescript imagines that base64 would not exist
|
||||
} else if (typeof message.file.base64 === "string") {
|
||||
// @ts-ignore - needed because typescript imagines that base64 would not exist
|
||||
base64content = message.file.base64;
|
||||
// @ts-ignore - needed because typescript imagines that plainText would not exist
|
||||
} else if (typeof message.file.plainText === "string") {
|
||||
base64content = await blob_to_base64(
|
||||
// @ts-ignore - needed because typescript imagines that plainText would not exist
|
||||
new Blob([message.file.plainText])
|
||||
);
|
||||
} else {
|
||||
return Promise.reject(
|
||||
"data is not set or wrong format, set one of `blob`, `base64` or `plainText`, see webxdc documentation for sendToChat"
|
||||
);
|
||||
}
|
||||
data.base64 = base64content;
|
||||
data.name = message.file.name;
|
||||
}
|
||||
|
||||
const errorMsg = InternalJSApi.sendToChat(JSON.stringify(data));
|
||||
if (errorMsg) {
|
||||
return Promise.reject(errorMsg);
|
||||
}
|
||||
},
|
||||
};
|
||||
})();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue