From 3312bd27c90d54309ddc070bed5012edf50c27ad Mon Sep 17 00:00:00 2001
From: Henry Heino <46334387+personalizedrefrigerator@users.noreply.github.com>
Date: Sun, 12 May 2024 02:01:02 -0700
Subject: [PATCH] Mobile: Simplify Dropbox sync workarond (#10415)

---
 packages/lib/file-api-driver-dropbox.js | 41 ++++++++-----------------
 1 file changed, 13 insertions(+), 28 deletions(-)

diff --git a/packages/lib/file-api-driver-dropbox.js b/packages/lib/file-api-driver-dropbox.js
index e9aa09410..32213bc67 100644
--- a/packages/lib/file-api-driver-dropbox.js
+++ b/packages/lib/file-api-driver-dropbox.js
@@ -1,9 +1,6 @@
 const time = require('./time').default;
 const shim = require('./shim').default;
 const JoplinError = require('./JoplinError').default;
-const Logger = require('@joplin/utils/Logger').default;
-
-const logger = Logger.create('file-api-driver-dropbox');
 
 class FileApiDriverDropbox {
 	constructor(api) {
@@ -100,14 +97,14 @@ class FileApiDriverDropbox {
 		}
 	}
 
-	async list(path, options) {
+	async list(path) {
 		let response = await this.api().exec('POST', 'files/list_folder', {
 			path: this.makePath_(path),
 		});
 
 		let output = this.metadataToStats_(response.entries);
 
-		while (response.has_more && !options?.firstPageOnly) {
+		while (response.has_more) {
 			response = await this.api().exec('POST', 'files/list_folder/continue', {
 				cursor: response.cursor,
 			});
@@ -117,7 +114,7 @@ class FileApiDriverDropbox {
 
 		return {
 			items: output,
-			hasMore: !!response.has_more,
+			hasMore: false,
 			context: { cursor: response.cursor },
 		};
 	}
@@ -135,12 +132,12 @@ class FileApiDriverDropbox {
 			// https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Error-1017-quot-cannot-parse-response-quot/td-p/589595
 			const needsFetchWorkaround = shim.mobilePlatform() === 'ios';
 
-			const fetchPath = (method, path) => {
+			const fetchPath = (method, path, extraHeaders) => {
 				return this.api().exec(
 					method,
 					'files/download',
 					null,
-					{ 'Dropbox-API-Arg': JSON.stringify({ path: this.makePath_(path) }) },
+					{ 'Dropbox-API-Arg': JSON.stringify({ path: this.makePath_(path) }), ...extraHeaders },
 					options,
 				);
 			};
@@ -149,26 +146,14 @@ class FileApiDriverDropbox {
 			if (!needsFetchWorkaround) {
 				response = await fetchPath('POST', path);
 			} else {
-				try {
-					response = await fetchPath('GET', path);
-				} catch (error) {
-					logger.warn('Request to files/download failed. Retrying with workaround. Error: ', error);
-					// May 2024: Sending a GET request to files/download sometimes fails
-					// until another file is requested. Because POST requests with empty bodies don't work on iOS,
-					// we send a request for a different file, then re-request the original.
-					//
-					// See https://github.com/laurent22/joplin/issues/10396
-
-					// This workaround requires that the file we request exist.
-					const { items } = await this.list('', { firstPageOnly: true });
-					const files = items.filter(item => !item.isDir && item.path !== path);
-
-					if (files.length > 0) {
-						await fetchPath('GET', files[0].path);
-					}
-
-					response = await fetchPath('GET', path);
-				}
+				// Use a random If-None-Match value to prevent React Native from using the cache.
+				// Passing "cache: no-store" doesn't seem to be sufficient, so If-None-Match is set to a value
+				// that will never match the ETag.
+				//
+				// Something similar is done for WebDAV.
+				//
+				// See https://github.com/laurent22/joplin/issues/10396
+				response = await fetchPath('GET', path, { 'If-None-Match': `JoplinIgnore-${Math.floor(Math.random() * 100000)}` });
 			}
 			return response;
 		} catch (error) {