From 7088b2cdc87ecbc3c8fbc0e69d75145239d5e317 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Wed, 11 Nov 2020 10:47:55 +0100 Subject: [PATCH] support attachment upload and sending from room api --- src/domain/session/room/RoomViewModel.js | 29 ++++++------------------ src/matrix/room/Room.js | 14 ++++++++++-- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/src/domain/session/room/RoomViewModel.js b/src/domain/session/room/RoomViewModel.js index 3b447c60..bad1ecbd 100644 --- a/src/domain/session/room/RoomViewModel.js +++ b/src/domain/session/room/RoomViewModel.js @@ -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() { diff --git a/src/matrix/room/Room.js b/src/matrix/room/Room.js index 65e3c9b4..109c096e 100644 --- a/src/matrix/room/Room.js +++ b/src/matrix/room/Room.js @@ -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();