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() { async _sendFile() {
const file = this.platform.openFile(); let file;
let blob = file.blob; try {
let encryptedFile; file = this.platform.openFile();
if (this._room.isEncrypted) { } catch (err) {
const {data, info} = await this._room.encryptAttachment(blob); return;
blob = data;
encryptedFile = Object.assign(info, {
mimetype: file.blob.mimeType,
url: null
});
} }
const mxcUrl = await this._room.mediaRepository.upload(blob, file.name); const attachment = this._room.uploadAttachment(file.name, file.blob);
const content = { const content = {
body: file.name, body: file.name,
msgtype: "m.file", msgtype: "m.file",
info: {
size: blob.size,
mimetype: file.blob.mimeType,
},
}; };
if (encryptedFile) { await this._room.sendEvent("m.room.message", content, attachment);
encryptedFile.url = mxcUrl;
content.file = encryptedFile;
} else {
content.url = mxcUrl;
}
await this._room.sendEvent("m.room.message", content);
} }
get composerViewModel() { get composerViewModel() {

View file

@ -30,7 +30,9 @@ import {EventEntry} from "./timeline/entries/EventEntry.js";
import {EventKey} from "./timeline/EventKey.js"; import {EventKey} from "./timeline/EventKey.js";
import {Direction} from "./timeline/Direction.js"; import {Direction} from "./timeline/Direction.js";
import {ObservedEventMap} from "./ObservedEventMap.js"; import {ObservedEventMap} from "./ObservedEventMap.js";
import {AttachmentUpload} from "./AttachmentUpload.js";
import {DecryptionSource} from "../e2ee/common.js"; import {DecryptionSource} from "../e2ee/common.js";
const EVENT_ENCRYPTED_TYPE = "m.room.encrypted"; const EVENT_ENCRYPTED_TYPE = "m.room.encrypted";
export class Room extends EventEmitter { export class Room extends EventEmitter {
@ -350,10 +352,11 @@ export class Room extends EventEmitter {
} }
/** @public */ /** @public */
sendEvent(eventType, content) { sendEvent(eventType, content, attachment) {
return this._sendQueue.enqueueEvent(eventType, content); return this._sendQueue.enqueueEvent(eventType, content, attachment);
} }
/** @public */
async ensureMessageKeyIsShared() { async ensureMessageKeyIsShared() {
return this._roomEncryption?.ensureMessageKeyIsShared(this._hsApi); 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() { dispose() {
this._roomEncryption?.dispose(); this._roomEncryption?.dispose();
this._timeline?.dispose(); this._timeline?.dispose();