Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

mobile: fix saving note from share extension (#2874)

Ammar Ahmed81cac1e54f4d
diff --git a/apps/mobile/app/common/filesystem/upload.js b/apps/mobile/app/common/filesystem/upload.js
index 19ec7446c..bef25c8ae 100644
--- a/apps/mobile/app/common/filesystem/upload.js
+++ b/apps/mobile/app/common/filesystem/upload.js
@@ -85,7 +85,7 @@ export async function uploadFile(filename, data, cancelToken) {
RNFetchBlob.fs.unlink(`${cacheDir}/${filename}`).catch(console.log);
}
}
- DatabaseLogger.info(`File uploaded: ${filename}`);
+ DatabaseLogger.info(`File upload status: ${filename}, ${status}, ${text}`);
return result;
} catch (e) {
useAttachmentStore.getState().remove(filename);
diff --git a/apps/mobile/app/utils/note-bundle.js b/apps/mobile/app/utils/note-bundle.js
index e2fff1097..6cb52f355 100644
--- a/apps/mobile/app/utils/note-bundle.js
+++ b/apps/mobile/app/utils/note-bundle.js
@@ -22,7 +22,6 @@ import { isImage } from "@notesnook/core/utils/filename";
import { Platform } from "react-native";
import RNFetchBlob from "react-native-blob-util";
import { db } from "../common/database";
-import { MMKV } from "../common/database/mmkv";
import { IOS_APPGROUPID } from "./constants";
export async function attachFile(uri, hash, type, filename, options) {
@@ -57,63 +56,53 @@ export async function attachFile(uri, hash, type, filename, options) {
}
}
-async function createNotes() {
- const bundles = MMKV.getArray("shared:noteBundles") || [];
- console.log("creating notes from note bundles", bundles);
+async function createNotes(bundle) {
+ const sessionId = bundle.note.sessionId;
+ const id = await db.notes.add(bundle.note);
- for (let i = 0; i < bundles.length; i++) {
- const bundle = bundles[i];
- const id = await db.notes.add(bundle.note);
- for (const item of bundle.notebooks) {
- if (item.type === "notebook") {
- db.relations.add(item, { id, type: "note" });
- } else {
- db.notes.addToNotebook(
- {
- id: item.notebookId,
- topic: item.id
- },
- id
- );
- }
- }
-
- for (const file of bundle.files) {
- const uri =
- Platform.OS === "ios"
- ? `${file.value.replace("file://", "")}`
- : `${file.value}`;
- const hash = await Sodium.hashFile({
- uri: uri,
- type: "cache"
- });
- await attachFile(uri, hash, file.type, file.name, {
- type: "cache",
- id: id,
- appGroupId: IOS_APPGROUPID
- });
- let content = ``;
- if (isImage(file.type)) {
- content = `<img data-hash="${hash}" data-mime="${file.type}" data-filename="${file.name}" />`;
- } else {
- content = `<p><span data-hash="${hash}" data-mime="${file.type}" data-filename="${file.name}" data-size="${file.size}" /></p>`;
- }
- const rawContent = await db.content.raw(
- db.notes.note(id).data?.contentId
- );
- await db.notes.add({
- id: id,
- content: {
- type: "tiptap",
- data: rawContent?.data ? rawContent?.data + content : content
+ for (const item of bundle.notebooks) {
+ if (item.type === "notebook") {
+ db.relations.add(item, { id, type: "note" });
+ } else {
+ db.notes.addToNotebook(
+ {
+ id: item.notebookId,
+ topic: item.id
},
- sessionId: Date.now()
- });
+ id
+ );
}
+ }
- const _bundles = [...bundles];
- _bundles.splice(i, 1);
- MMKV.setArray("shared:noteBundles", _bundles);
+ for (const file of bundle.files) {
+ const uri =
+ Platform.OS === "ios"
+ ? `${file.value.replace("file://", "")}`
+ : `${file.value}`;
+ const hash = await Sodium.hashFile({
+ uri: uri,
+ type: "cache"
+ });
+ await attachFile(uri, hash, file.type, file.name, {
+ type: "cache",
+ id: id,
+ appGroupId: IOS_APPGROUPID
+ });
+ let content = ``;
+ if (isImage(file.type)) {
+ content = `<img data-hash="${hash}" data-mime="${file.type}" data-filename="${file.name}" />`;
+ } else {
+ content = `<p><span data-hash="${hash}" data-mime="${file.type}" data-filename="${file.name}" data-size="${file.size}" /></p>`;
+ }
+ const rawContent = await db.content.raw(db.notes.note(id).data?.contentId);
+ await db.notes.add({
+ id: id,
+ content: {
+ type: "tiptap",
+ data: rawContent?.data ? rawContent?.data + content : content
+ },
+ sessionId: sessionId
+ });
}
}
diff --git a/apps/mobile/share/editor.js b/apps/mobile/share/editor.js
index e6b34bda5..b741086c2 100644
--- a/apps/mobile/share/editor.js
+++ b/apps/mobile/share/editor.js
@@ -41,7 +41,7 @@ import { EventTypes } from "../app/screens/editor/tiptap/editor-events";
const useEditor = () => {
const ref = useRef();
- const [sessionId] = useState("share-editor-session");
+ const [sessionId] = useState("share-editor-session" + Date.now());
const colors = useShareStore((state) => state.colors);
const accent = useShareStore((state) => state.accent);
const commands = useMemo(() => new Commands(ref), [ref]);
diff --git a/apps/mobile/share/share.js b/apps/mobile/share/share.js
index 3d16ccbae..cceebf005 100644
--- a/apps/mobile/share/share.js
+++ b/apps/mobile/share/share.js
@@ -17,7 +17,10 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+import ShareExtension from "@ammarahmed/react-native-share-extension";
import { getPreviewData } from "@flyerhq/react-native-link-preview";
+import { formatBytes } from "@notesnook/common";
+import { isImage } from "@notesnook/core/utils/filename";
import { parseHTML } from "@notesnook/core/utils/html-parser";
import React, { useCallback, useEffect, useRef, useState } from "react";
import {
@@ -34,27 +37,23 @@ import {
View,
useWindowDimensions
} from "react-native";
+import RNFetchBlob from "react-native-blob-util";
import {
SafeAreaProvider,
useSafeAreaInsets
} from "react-native-safe-area-context";
import Icon from "react-native-vector-icons/MaterialCommunityIcons";
-import ShareExtension from "@ammarahmed/react-native-share-extension";
import isURL from "validator/lib/isURL";
import { db } from "../app/common/database";
-import { MMKV } from "../app/common/database/mmkv";
import Storage from "../app/common/database/storage";
import { eSendEvent } from "../app/services/event-manager";
+import { FILE_SIZE_LIMIT, IMAGE_SIZE_LIMIT } from "../app/utils/constants";
import { getElevationStyle } from "../app/utils/elevation";
-import { formatBytes } from "@notesnook/common";
import { eOnLoadNote } from "../app/utils/events";
+import { NoteBundle } from "../app/utils/note-bundle";
import { Editor } from "./editor";
import { Search } from "./search";
import { initDatabase, useShareStore } from "./store";
-import { isImage } from "@notesnook/core/utils/filename";
-import { NoteBundle } from "../app/utils/note-bundle";
-import { FILE_SIZE_LIMIT, IMAGE_SIZE_LIMIT } from "../app/utils/constants";
-import RNFetchBlob from "react-native-blob-util";
const getLinkPreview = (url) => {
return getPreviewData(url, 5000);
@@ -369,15 +368,11 @@ const ShareView = ({ quicknote = false }) => {
_note.sessionId = Date.now();
}
- const noteBundle = {
+ await NoteBundle.createNotes({
files: rawFiles,
note: _note,
notebooks: useShareStore.getState().selectedNotebooks
- };
- const bundles = MMKV.getArray("shared:noteBundles") || [];
- bundles.push(noteBundle);
- MMKV.setArray("shared:noteBundles", bundles);
- await NoteBundle.createNotes();
+ });
try {
await db.sync(false, false);
diff --git a/apps/mobile/share/store.js b/apps/mobile/share/store.js
index f550c4a87..6797d4fcc 100644
--- a/apps/mobile/share/store.js
+++ b/apps/mobile/share/store.js
@@ -30,6 +30,8 @@ import { db } from "../app/common/database";
export async function initDatabase() {
if (!db.isInitialized) {
await db.init();
+ } else {
+ await db.initCollections();
}
await db.notes.init();
}