diff --git a/js/models/conversations.js b/js/models/conversations.js index 1336a0c01..c1a65b60f 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -2304,7 +2304,9 @@ async deletePublicMessages(messages) { const channelAPI = await this.getPublicSendData(); + if (!channelAPI) { + log.error('Unable to get public channel API'); return false; } diff --git a/js/modules/loki_app_dot_net_api.d.ts b/js/modules/loki_app_dot_net_api.d.ts index b898ae4ea..03945e4c3 100644 --- a/js/modules/loki_app_dot_net_api.d.ts +++ b/js/modules/loki_app_dot_net_api.d.ts @@ -9,11 +9,6 @@ interface UploadResponse { id?: number; } -interface DownloadResponse { - statucCode: number; - reponse: any; -} - export interface LokiAppDotNetServerInterface { findOrCreateChannel( api: LokiPublicChatFactoryAPI, @@ -24,7 +19,7 @@ export interface LokiAppDotNetServerInterface { uploadAvatar(data: FormData): Promise; putAttachment(data: ArrayBuffer): Promise; putAvatar(data: ArrayBuffer): Promise; - downloadAttachment(url: String): Promise; // todo: add return type + downloadAttachment(url: String): Promise; } export interface LokiPublicChannelAPI { diff --git a/js/modules/loki_app_dot_net_api.js b/js/modules/loki_app_dot_net_api.js index 02e63ca9d..043e05548 100644 --- a/js/modules/loki_app_dot_net_api.js +++ b/js/modules/loki_app_dot_net_api.js @@ -154,10 +154,7 @@ const sendViaOnion = async (srvPubKey, url, fetchOptions, options = {}) => { return { result, txtResponse: result.body, - response: { - data: result.body, - headers: result.headers, - }, + response: result.body, }; } @@ -174,7 +171,7 @@ const sendViaOnion = async (srvPubKey, url, fetchOptions, options = {}) => { body = JSON.parse(result.body); } catch (e) { log.error( - `loki_app_dot_net:::sendViaOnion #${options.requestNumber} - Cant decode JSON body`, + `loki_app_dot_net:::sendViaOnion #${options.requestNumber} - Can't decode JSON body`, typeof result.body, result.body ); @@ -1016,12 +1013,25 @@ class LokiAppDotNetServerAPI { return this.uploadAvatar(formData); } + // This should return Uint8Array in response.data async downloadAttachment(url) { const endpoint = new URL(url).pathname; - return this.serverRequest(`loki/v1${endpoint}`, { + // With the new protocol, there is no json in body, we shouldn't try to parse it + const noJson = window.lokiFeatureFlags.useFileOnionRequestsV2; + + const res = await this.serverRequest(`loki/v1${endpoint}`, { method: 'GET', + noJson, }); + + if (window.lokiFeatureFlags.useFileOnionRequestsV2) { + const buffer = dcodeIO.ByteBuffer.fromBase64( + res.response + ).toArrayBuffer(); + return buffer; + } + return new Uint8Array(res.response.data).buffer; } } diff --git a/js/modules/loki_file_server_api.d.ts b/js/modules/loki_file_server_api.d.ts index 9b92f3576..050b2014f 100644 --- a/js/modules/loki_file_server_api.d.ts +++ b/js/modules/loki_file_server_api.d.ts @@ -13,5 +13,5 @@ interface DeviceMappingAnnotation { interface LokiFileServerInstance { getUserDeviceMapping(pubKey: string): Promise; clearOurDeviceMappingAnnotations(): Promise; - downloadAttachment(url: string): Promise; + downloadAttachment(url: string): Promise; } diff --git a/js/modules/loki_public_chat_api.js b/js/modules/loki_public_chat_api.js index a26d1b33a..116c5e28e 100644 --- a/js/modules/loki_public_chat_api.js +++ b/js/modules/loki_public_chat_api.js @@ -17,6 +17,7 @@ const validOpenGroupServer = async serverUrl => { const result = await window.tokenlessFileServerAdnAPI.serverRequest( `loki/v1/getOpenGroupKey/${url.hostname}` ); + if (result.response.meta.code === 200) { // supports it const obj = JSON.parse(result.response.data); diff --git a/preload.js b/preload.js index 52779f149..69595f605 100644 --- a/preload.js +++ b/preload.js @@ -461,6 +461,7 @@ window.lokiFeatureFlags = { useOnionRequests: true, useOnionRequestsV2: true, useFileOnionRequests: true, + useFileOnionRequestsV2: false, // more compact encoding of files in response enableSenderKeys: true, onionRequestHops: 3, debugMessageLogs: process.env.ENABLE_MESSAGE_LOGS, diff --git a/ts/receiver/attachments.ts b/ts/receiver/attachments.ts index 26bf845ee..b3bd2c126 100644 --- a/ts/receiver/attachments.ts +++ b/ts/receiver/attachments.ts @@ -12,7 +12,7 @@ export async function downloadAttachment(attachment: any) { serverUrl ); - let res: any; + let res: ArrayBuffer | null = null; // TODO: we need attachments to remember which API should be used to retrieve them if (!defaultFileserver) { @@ -31,7 +31,7 @@ export async function downloadAttachment(attachment: any) { } // The attachment id is actually just the absolute url of the attachment - let data = new Uint8Array(res.response.data).buffer; + let data = res; if (!attachment.isRaw) { const { key, digest, size } = attachment; diff --git a/ts/session/snode_api/onions.ts b/ts/session/snode_api/onions.ts index df0ecbb9a..7adfc7964 100644 --- a/ts/session/snode_api/onions.ts +++ b/ts/session/snode_api/onions.ts @@ -140,7 +140,11 @@ async function buildOnionCtxs( const relayingToFinalDestination = i === firstPos; // if last position if (relayingToFinalDestination && fileServerOptions) { - const target = useV2 ? '/loki/v2/lsrpc' : '/loki/v1/lsrpc'; + let target = useV2 ? '/loki/v2/lsrpc' : '/loki/v1/lsrpc'; + + if (window.lokiFeatureFlags.useFileOnionRequestsV2) { + target = '/loki/v3/lsrpc'; + } dest = { host: fileServerOptions.host, @@ -397,9 +401,11 @@ const sendOnionRequest = async ( const useV2 = window.lokiFeatureFlags.useOnionRequestsV2; + const isLsrpc = !!finalRelayOptions; + let destCtx; try { - if (useV2 && !finalRelayOptions) { + if (useV2 && !isLsrpc) { const body = options.body || ''; delete options.body; diff --git a/ts/window.d.ts b/ts/window.d.ts index 3898eb869..1adb33d8b 100644 --- a/ts/window.d.ts +++ b/ts/window.d.ts @@ -59,6 +59,7 @@ declare global { useOnionRequests: boolean; useOnionRequestsV2: boolean; useFileOnionRequests: boolean; + useFileOnionRequestsV2: boolean; enableSenderKeys: boolean; onionRequestHops: number; debugMessageLogs: boolean;