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
|
@ -42,20 +42,68 @@ window.webxdc = (() => {
|
||||||
|
|
||||||
sendToChat: async (message) => {
|
sendToChat: async (message) => {
|
||||||
const data = {};
|
const data = {};
|
||||||
if (!message.text && !message.file) {
|
if (!message.file && !message.text) {
|
||||||
return Promise.reject("Invalid empty message, at least one of text or file should be provided");
|
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) {
|
if (message.text) {
|
||||||
data.text = message.text;
|
data.text = message.text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @type {{file_name: string, file_message: string} | null} */
|
||||||
if (message.file) {
|
if (message.file) {
|
||||||
if (!message.file.name || typeof message.file.base64 !== 'string') {
|
let base64content;
|
||||||
return Promise.reject("provided file is invalid, you need to set both name and base64 content");
|
if (!message.file.name) {
|
||||||
|
return Promise.reject("file name is missing");
|
||||||
}
|
}
|
||||||
data.base64 = message.file.base64;
|
if (
|
||||||
data.name = message.file.name;
|
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"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// @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));
|
const errorMsg = InternalJSApi.sendToChat(JSON.stringify(data));
|
||||||
if (errorMsg) {
|
if (errorMsg) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue