Skip to content

Commit

Permalink
Move non-BG specific PKM util fns into own module
Browse files Browse the repository at this point in the history
- This fixes a problem where linkedom's service worker was being included in the options UI script bundle.
- It stemmed from certain PKM util fns being imported into the options UI code, but living in the same module which (indirectly) imported linkedom/worker. As they lived together, it all got brought in together even though that linkedom/worker stuff wasn't being imported in the UI specifically.
- Those non-BG specific PKM util fns have now been moved to their own `utils.ts` module
  • Loading branch information
poltak committed Jul 26, 2024
1 parent 25eab1c commit 81317c0
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 103 deletions.
6 changes: 2 additions & 4 deletions src/annotations/background/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ import { STORAGE_VERSIONS } from 'src/storage/constants'
import type { Annotation, AnnotListEntry } from 'src/annotations/types'
import { normalizeUrl } from '@worldbrain/memex-common/lib/url-utils/normalize'
import type { PKMSyncBackgroundModule } from 'src/pkm-integrations/background'
import {
isPkmSyncEnabled,
shareAnnotationWithPKM,
} from 'src/pkm-integrations/background/backend/utils'
import { shareAnnotationWithPKM } from 'src/pkm-integrations/background/backend/utils'
import { isPkmSyncEnabled } from 'src/pkm-integrations/utils'
import type { Storage } from 'webextension-polyfill'

export default class AnnotationStorage extends StorageModule {
Expand Down
2 changes: 1 addition & 1 deletion src/backup-restore/background/backend/simple-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
separateDataFromImageChanges,
shouldWriteImages,
} from 'src/backup-restore/background/backend/utils'
import { getPkmSyncKey } from 'src/pkm-integrations/background/backend/utils'
import { getPkmSyncKey } from 'src/pkm-integrations/utils'
import type { Storage } from 'webextension-polyfill'

export class MemexLocalBackend extends BackupBackend {
Expand Down
2 changes: 1 addition & 1 deletion src/backup-restore/ui/backup-pane/panes/restore-where.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ProviderList } from 'src/backup-restore/ui/backup-pane/components/provi
import { DownloadOverlay } from '../components/overlays'
import { fetchBackupPath, checkServerStatus } from '../../utils'
import { PrimaryAction } from '@worldbrain/memex-common/lib/common-ui/components/PrimaryAction'
import { getFolder } from 'src/pkm-integrations/background/backend/utils'
import { getFolder } from 'src/pkm-integrations/utils'

const settingsStyle = require('src/options/settings/components/settings.css')
const STYLES = require('../../styles.css')
Expand Down
2 changes: 1 addition & 1 deletion src/backup-restore/ui/backup-pane/panes/setup-location.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Icon from '@worldbrain/memex-common/lib/common-ui/components/icon'
import SettingSection from '@worldbrain/memex-common/lib/common-ui/components/setting-section'
import TextField from '@worldbrain/memex-common/lib/common-ui/components/text-field'
import ButtonBar from 'src/options/imports/components/ButtonBar'
import { getFolder } from 'src/pkm-integrations/background/backend/utils'
import { getFolder } from 'src/pkm-integrations/utils'

export default class SetupLocation extends React.Component {
state = {
Expand Down
2 changes: 1 addition & 1 deletion src/backup-restore/ui/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { remoteFunction } from 'src/util/webextensionRPC'
import { LOCAL_SERVER_ENDPOINTS } from './backup-pane/constants'
import { getPkmSyncKey } from 'src/pkm-integrations/background/backend/utils'
import { getPkmSyncKey } from 'src/pkm-integrations/utils'
export async function redirectToGDriveLogin() {
window.location.href = await remoteFunction('getBackupProviderLoginLink')({
returnUrl: 'http://memex.cloud/backup/auth-redirect/google-drive',
Expand Down
6 changes: 2 additions & 4 deletions src/custom-lists/background/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ import {
insertOrderedItemBeforeIndex,
pushOrderedItem,
} from '@worldbrain/memex-common/lib/utils/item-ordering'
import {
isPkmSyncEnabled,
sharePageWithPKM,
} from 'src/pkm-integrations/background/backend/utils'
import { sharePageWithPKM } from 'src/pkm-integrations/background/backend/utils'
import { isPkmSyncEnabled } from 'src/pkm-integrations/utils'
import { normalizeUrl } from '@worldbrain/memex-common/lib/url-utils/normalize'
import {
buildMaterializedPath,
Expand Down
2 changes: 1 addition & 1 deletion src/options/imports/components/Import.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import styled from 'styled-components'
import SettingSection from '@worldbrain/memex-common/lib/common-ui/components/setting-section'
import { PrimaryAction } from '@worldbrain/memex-common/lib/common-ui/components/PrimaryAction'
import TextField from '@worldbrain/memex-common/lib/common-ui/components/text-field'
import { getFolder } from 'src/pkm-integrations/background/backend/utils'
import { getFolder } from 'src/pkm-integrations/utils'
import { MemexLocalBackend } from 'src/pkm-integrations/background/backend'
import { TooltipBox } from '@worldbrain/memex-common/lib/common-ui/components/tooltip-box'
import Checkbox from 'src/common-ui/components/Checkbox'
Expand Down
6 changes: 2 additions & 4 deletions src/page-indexing/background/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ import {
ContentFingerprint,
LocationSchemeType,
} from '@worldbrain/memex-common/lib/personal-cloud/storage/types'
import {
isPkmSyncEnabled,
sharePageWithPKM,
} from 'src/pkm-integrations/background/backend/utils'
import { isPkmSyncEnabled } from 'src/pkm-integrations/utils'
import { sharePageWithPKM } from 'src/pkm-integrations/background/backend/utils'
import type { AuthenticatedUser } from '@worldbrain/memex-common/lib/authentication/types'
import type {
Bookmark,
Expand Down
2 changes: 1 addition & 1 deletion src/personal-cloud/background/handle-incoming-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { transformPageHTML } from '@worldbrain/memex-stemmer/lib/transform-page-
import { transformPageText } from '@worldbrain/memex-stemmer/lib/transform-page-text'
import type { PKMSyncBackgroundModule } from 'src/pkm-integrations/background'
import {
isPkmSyncEnabled,
shareAnnotationWithPKM,
sharePageWithPKM,
} from 'src/pkm-integrations/background/backend/utils'
import { isPkmSyncEnabled } from 'src/pkm-integrations/utils'
import { normalizeUrl } from '@worldbrain/memex-common/lib/url-utils/normalize'
import type { ImageSupportBackground } from 'src/image-support/background'
import type { Browser } from 'webextension-polyfill'
Expand Down
2 changes: 1 addition & 1 deletion src/pkm-integrations/background/backend/simple-http.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getPkmSyncKey } from './utils'
import { getPkmSyncKey } from 'src/pkm-integrations/utils'
import type { Storage } from 'webextension-polyfill'
import type { LocalFolder } from 'src/sidebar/annotations-sidebar/containers/types'

Expand Down
84 changes: 0 additions & 84 deletions src/pkm-integrations/background/backend/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import resolveImgSrc from '@worldbrain/memex-common/lib/annotations/replace-img-src-with-cloud-address.service-worker'
import type { AutoPk } from '@worldbrain/memex-common/lib/storage/types'
import { LOCAL_SERVER_ROOT } from 'src/backup-restore/ui/backup-pane/constants'
import type { Storage } from 'webextension-polyfill'
import type { PKMSyncBackgroundModule } from '..'

export async function shareAnnotationWithPKM(
Expand Down Expand Up @@ -103,88 +101,6 @@ export async function sharePageWithPKM(
}
}

export async function getPkmSyncKey(deps: { storageAPI: Storage.Static }) {
let data = await deps.storageAPI.local.get('PKMSYNCpkmSyncKey')

let pkmSyncKey = data.PKMSYNCpkmSyncKey

// If pkmSyncKey does not exist, create a new one and store it in local storage
if (!pkmSyncKey) {
// Generate a random string for pkmSyncKey
pkmSyncKey =
Math.random().toString(36).substring(2, 15) +
Math.random().toString(36).substring(2, 15)
await deps.storageAPI.local.set({ PKMSYNCpkmSyncKey: pkmSyncKey })
}

return pkmSyncKey
}

export async function isPkmSyncEnabled(deps: { storageAPI: Storage.Static }) {
try {
const data = await deps.storageAPI.local.get('PKMSYNCpkmFolders')
if (
data.PKMSYNCpkmFolders &&
(data.PKMSYNCpkmFolders.obsidianFolder?.length > 0 ||
data.PKMSYNCpkmFolders.logSeqFolder?.length > 0)
) {
return true
}

return false
} catch (e) {
return false
}
}

export async function getFolder(
pkmToSync: string,
deps: { storageAPI: Storage.Static },
) {
const pkmSyncKey = await getPkmSyncKey(deps)

const serverToTalkTo = LOCAL_SERVER_ROOT

const getFolderPath = async (pkmToSync: string) => {
const response = await fetch(`${serverToTalkTo}/set-directory`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
pkmSyncType: pkmToSync,
syncKey: pkmSyncKey,
}),
})
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
const directoryPath = await response.text()

return directoryPath
}

const folderPath = await getFolderPath(pkmToSync)

// Fetch the existing "PKMSYNCpkmFolders" from local storage
let data = await deps.storageAPI.local.get('PKMSYNCpkmFolders')
data = data.PKMSYNCpkmFolders || {}

// Update the value in it that corresponds to the pkmToSync
if (pkmToSync === 'logseq') {
data['logSeqFolder'] = folderPath
} else if (pkmToSync === 'obsidian') {
data['obsidianFolder'] = folderPath
} else if (pkmToSync === 'backup') {
data['backupFolder'] = folderPath
}

// Write the update to local storage
await deps.storageAPI.local.set({ PKMSYNCpkmFolders: data })

return data
}

export type rabbitHoleDocument = {
pageTitle: string
fullUrl: string
Expand Down
84 changes: 84 additions & 0 deletions src/pkm-integrations/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { LOCAL_SERVER_ROOT } from 'src/backup-restore/ui/backup-pane/constants'
import type { Storage } from 'webextension-polyfill'

export async function getPkmSyncKey(deps: { storageAPI: Storage.Static }) {
let data = await deps.storageAPI.local.get('PKMSYNCpkmSyncKey')

let pkmSyncKey = data.PKMSYNCpkmSyncKey

// If pkmSyncKey does not exist, create a new one and store it in local storage
if (!pkmSyncKey) {
// Generate a random string for pkmSyncKey
pkmSyncKey =
Math.random().toString(36).substring(2, 15) +
Math.random().toString(36).substring(2, 15)
await deps.storageAPI.local.set({ PKMSYNCpkmSyncKey: pkmSyncKey })
}

return pkmSyncKey
}

export async function isPkmSyncEnabled(deps: { storageAPI: Storage.Static }) {
try {
const data = await deps.storageAPI.local.get('PKMSYNCpkmFolders')
if (
data.PKMSYNCpkmFolders &&
(data.PKMSYNCpkmFolders.obsidianFolder?.length > 0 ||
data.PKMSYNCpkmFolders.logSeqFolder?.length > 0)
) {
return true
}

return false
} catch (e) {
return false
}
}

export async function getFolder(
pkmToSync: string,
deps: { storageAPI: Storage.Static },
) {
const pkmSyncKey = await getPkmSyncKey(deps)

const serverToTalkTo = LOCAL_SERVER_ROOT

const getFolderPath = async (pkmToSync: string) => {
const response = await fetch(`${serverToTalkTo}/set-directory`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
pkmSyncType: pkmToSync,
syncKey: pkmSyncKey,
}),
})
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
const directoryPath = await response.text()

return directoryPath
}

const folderPath = await getFolderPath(pkmToSync)

// Fetch the existing "PKMSYNCpkmFolders" from local storage
let data = await deps.storageAPI.local.get('PKMSYNCpkmFolders')
data = data.PKMSYNCpkmFolders || {}

// Update the value in it that corresponds to the pkmToSync
if (pkmToSync === 'logseq') {
data['logSeqFolder'] = folderPath
} else if (pkmToSync === 'obsidian') {
data['obsidianFolder'] = folderPath
} else if (pkmToSync === 'backup') {
data['backupFolder'] = folderPath
}

// Write the update to local storage
await deps.storageAPI.local.set({ PKMSYNCpkmFolders: data })

return data
}

0 comments on commit 81317c0

Please sign in to comment.