support attachment upload and sending from room api

This commit is contained in:
Bruno Windels 2020-11-11 10:47:55 +01:00
parent 0c70a67ebb
commit 7088b2cdc8
2 changed files with 19 additions and 24 deletions

View File

@ -165,33 +165,18 @@ export class RoomViewModel extends ViewModel {
}
async _sendFile() {
const file = this.platform.openFile();
let blob = file.blob;
let encryptedFile;
if (this._room.isEncrypted) {
const {data, info} = await this._room.encryptAttachment(blob);
blob = data;
encryptedFile = Object.assign(info, {
mimetype: file.blob.mimeType,
url: null
});
let file;
try {
file = this.platform.openFile();
} catch (err) {
return;
}
const mxcUrl = await this._room.mediaRepository.upload(blob, file.name);
const attachment = this._room.uploadAttachment(file.name, file.blob);
const content = {
body: file.name,
msgtype: "m.file",
info: {
size: blob.size,
mimetype: file.blob.mimeType,
},
};
if (encryptedFile) {
encryptedFile.url = mxcUrl;
content.file = encryptedFile;
} else {
content.url = mxcUrl;
}
await this._room.sendEvent("m.room.message", content);
await this._room.sendEvent("m.room.message", content, attachment);
}
get composerViewModel() {

View File

@ -30,7 +30,9 @@ import {EventEntry} from "./timeline/entries/EventEntry.js";
import {EventKey} from "./timeline/EventKey.js";
import {Direction} from "./timeline/Direction.js";
import {ObservedEventMap} from "./ObservedEventMap.js";
import {AttachmentUpload} from "./AttachmentUpload.js";
import {DecryptionSource} from "../e2ee/common.js";
const EVENT_ENCRYPTED_TYPE = "m.room.encrypted";
export class Room extends EventEmitter {
@ -350,10 +352,11 @@ export class Room extends EventEmitter {
}
/** @public */
sendEvent(eventType, content) {
return this._sendQueue.enqueueEvent(eventType, content);
sendEvent(eventType, content, attachment) {
return this._sendQueue.enqueueEvent(eventType, content, attachment);
}
/** @public */
async ensureMessageKeyIsShared() {
return this._roomEncryption?.ensureMessageKeyIsShared(this._hsApi);
}
@ -630,6 +633,13 @@ export class Room extends EventEmitter {
}
}
uploadAttachment(blob, filename) {
const attachment = new AttachmentUpload({blob, filename,
hsApi: this._hsApi, platform: this._platform, isEncrypted: this.isEncrypted});
attachment.upload();
return attachment;
}
dispose() {
this._roomEncryption?.dispose();
this._timeline?.dispose();