Skip to content

Commit

Permalink
Reduce annot create flow sharedAnnotMetadata sync entries from 3 to 1
Browse files Browse the repository at this point in the history
  • Loading branch information
poltak committed Jun 14, 2024
1 parent 395bff4 commit 63ae627
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 149 deletions.
2 changes: 1 addition & 1 deletion external/@worldbrain/memex-common
3 changes: 3 additions & 0 deletions src/annotations/annotation-save-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export async function createAnnotation({
shareData = await contentSharingBG.shareAnnotation({
annotationUrl,
remoteAnnotationId,
excludeFromLists: !shareOpts?.shouldShare ?? true,
shareToParentPageLists: shareOpts?.shouldShare ?? false,
skipPrivacyLevelUpdate: !!shareOpts?.shouldShare ?? true,
})
Expand All @@ -144,6 +145,7 @@ export async function createAnnotation({
shareData = await contentSharingBG.shareAnnotation({
annotationUrl,
remoteAnnotationId,
excludeFromLists: false,
shareToParentPageLists: false,
skipPrivacyLevelUpdate: true,
})
Expand Down Expand Up @@ -234,6 +236,7 @@ export async function updateAnnotation({
remoteAnnotationId,
annotationUrl: annotationData.localId,
shareToParentPageLists: true,
excludeFromLists: false,
}),
!shareOpts?.skipPrivacyLevelUpdate &&
contentSharingBG.setAnnotationPrivacyLevel({
Expand Down
48 changes: 24 additions & 24 deletions src/content-sharing/background/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -701,18 +701,18 @@ export const INTEGRATION_TESTS = backgroundIntegrationTestSuite(
AnnotationPrivacyLevels.SHARED,
},
})
await helper.unshareAnnotations(setup, {
ids: [1],
expectedSharingStates: {
1: {
sharedListIds: [],
privateListIds: [],
privacyLevel:
AnnotationPrivacyLevels.PRIVATE,
hasLink: true,
},
},
})
// await helper.unshareAnnotations(setup, {
// ids: [1],
// expectedSharingStates: {
// 1: {
// sharedListIds: [],
// privateListIds: [],
// privacyLevel:
// AnnotationPrivacyLevels.PRIVATE,
// hasLink: true,
// },
// },
// })
await helper.assertAnnotationPrivacyLevels(
setup,
[
Expand Down Expand Up @@ -774,18 +774,18 @@ export const INTEGRATION_TESTS = backgroundIntegrationTestSuite(
AnnotationPrivacyLevels.SHARED_PROTECTED,
},
})
await helper.unshareAnnotations(setup, {
ids: [1],
expectedSharingStates: {
1: {
sharedListIds: [1, 2],
privateListIds: [],
privacyLevel:
AnnotationPrivacyLevels.SHARED_PROTECTED,
hasLink: true,
},
},
})
// await helper.unshareAnnotations(setup, {
// ids: [1],
// expectedSharingStates: {
// 1: {
// sharedListIds: [1, 2],
// privateListIds: [],
// privacyLevel:
// AnnotationPrivacyLevels.SHARED_PROTECTED,
// hasLink: true,
// },
// },
// })
await helper.assertAnnotationPrivacyLevels(
setup,
[
Expand Down
20 changes: 0 additions & 20 deletions src/content-sharing/background/index.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,26 +474,6 @@ export class SharingTestHelper {
)
}

async unshareAnnotations(
setup: BackgroundIntegrationTestSetup,
options: {
ids: number[]
expectedSharingStates: AnnotationSharingStates
},
) {
const {
sharingStates,
} = await setup.backgroundModules.contentSharing.unshareAnnotations({
annotationUrls: options.ids.map(
(id) => this.annotations[id].localId,
),
})
this._expectAnnotationSharingStates(
sharingStates,
options.expectedSharingStates,
)
}

async unshareAnnotation(
setup: BackgroundIntegrationTestSetup,
options: { id: number; expectedSharingState: AnnotationSharingState },
Expand Down
57 changes: 7 additions & 50 deletions src/content-sharing/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import type StorageManager from '@worldbrain/storex'
import type { StorageOperationEvent } from '@worldbrain/storex-middleware-change-watcher/lib/types'
import type { ContentSharingBackend } from '@worldbrain/memex-common/lib/content-sharing/backend'
import {
makeAnnotationPrivacyLevel,
getAnnotationPrivacyState,
createPageLinkListTitle,
getListShareUrl,
makeAnnotationPrivacyLevel,
} from '@worldbrain/memex-common/lib/content-sharing/utils'
import type { Analytics } from 'src/analytics/types'
import { AnnotationPrivacyLevels } from '@worldbrain/memex-common/lib/annotations/types'
Expand Down Expand Up @@ -261,11 +261,9 @@ export default class ContentSharingBackground {
waitForListShareSideEffects: this.waitForListShareSideEffects,
deleteListAndAllAssociatedData: this.deleteListAndAllAssociatedData,
shareAnnotation: this.shareAnnotation,
shareAnnotations: this.shareAnnotations,
executePendingActions: this.executePendingActions.bind(this),
shareAnnotationToSomeLists: this.shareAnnotationToSomeLists,
unshareAnnotationFromList: this.unshareAnnotationFromList,
unshareAnnotations: this.unshareAnnotations,
ensureRemotePageId: this.ensureRemotePageId,
getRemoteAnnotationLink: this.getRemoteAnnotationLink,
findAnnotationPrivacyLevels: this.findAnnotationPrivacyLevels.bind(
Expand Down Expand Up @@ -543,9 +541,12 @@ export default class ContentSharingBackground {
return this.annotationSharingService.shareAnnotation(options)
}

shareAnnotations: ContentSharingInterface['shareAnnotations'] = async (
options,
) => {
async shareAnnotations(options: {
annotationUrls: string[]
shareToParentPageLists?: boolean
setBulkShareProtected?: boolean
skipAnalytics?: boolean
}): Promise<{ sharingStates: AnnotationSharingStates }> {
const remoteIds = await this.storage.getRemoteAnnotationIds({
localIds: options.annotationUrls,
})
Expand Down Expand Up @@ -775,50 +776,6 @@ export default class ContentSharingBackground {
return { sharingState }
}

unshareAnnotations: ContentSharingInterface['unshareAnnotations'] = async (
options,
) => {
const annotPrivacyLevels = await this.storage.getPrivacyLevelsByAnnotation(
{ annotations: options.annotationUrls },
)
const nonProtectedAnnotations = options.annotationUrls.filter(
(annotationUrl) =>
![
AnnotationPrivacyLevels.PROTECTED,
AnnotationPrivacyLevels.SHARED_PROTECTED,
].includes(annotPrivacyLevels[annotationUrl]?.privacyLevel),
)

const allMetadata = await this.storage.getRemoteAnnotationMetadata({
localIds: nonProtectedAnnotations,
})
await this.storage.setAnnotationsExcludedFromLists({
localIds: Object.values(allMetadata)
.filter((metadata) => !metadata.excludeFromLists)
.map(({ localId }) => localId),
excludeFromLists: true,
})

await this.storage.setAnnotationPrivacyLevelBulk({
annotations: nonProtectedAnnotations,
privacyLevel: options.setBulkShareProtected
? AnnotationPrivacyLevels.PROTECTED
: AnnotationPrivacyLevels.PRIVATE,
})

if (this.options.analyticsBG) {
try {
await trackUnSharedAnnotation(this.options.analyticsBG, {
type: 'bulk',
})
} catch (error) {
console.error(`Error tracking space create event', ${error}`)
}
}

return { sharingStates: await this.getAnnotationSharingStates(options) }
}

unshareAnnotation: __DeprecatedContentSharingInterface['unshareAnnotation'] = async (
options,
) => {
Expand Down
10 changes: 0 additions & 10 deletions src/content-sharing/background/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,6 @@ export interface ContentSharingInterface
deleteListAndAllAssociatedData(params: {
localListId: number
}): Promise<void>
shareAnnotations(options: {
annotationUrls: string[]
shareToParentPageLists?: boolean
setBulkShareProtected?: boolean
skipAnalytics?: boolean
}): Promise<{ sharingStates: AnnotationSharingStates }>
unshareAnnotations(options: {
annotationUrls: string[]
setBulkShareProtected?: boolean
}): Promise<{ sharingStates: AnnotationSharingStates }>
ensureRemotePageId(normalizedPageUrl: string): Promise<string>
getRemoteAnnotationLink(params: {
annotationUrl: string
Expand Down
43 changes: 0 additions & 43 deletions src/overview/sharing/AllNotesShareMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,49 +55,6 @@ export default class AllNotesShareMenu extends React.Component<Props, State> {
this.setState({ link: getPageShareUrl({ remotePageInfoId }) })
}

private shareAllAnnotations = async () => {
try {
const {
sharingStates,
} = await this.props.contentSharingBG.shareAnnotations({
annotationUrls: this.annotationUrls,
shareToParentPageLists: true,
})
this.props.postBulkShareHook?.(sharingStates)
} catch (err) {}
}

private unshareAllAnnotations = async () => {
try {
const {
sharingStates,
} = await this.props.contentSharingBG.unshareAnnotations({
annotationUrls: this.annotationUrls,
})
this.props.postBulkShareHook?.(sharingStates)
} catch (err) {}
}

private handleSetShared = async () => {
await executeReactStateUITask<State, 'shareState'>(
this,
'shareState',
async () => {
await this.shareAllAnnotations()
},
)
}

private handleSetPrivate = async () => {
await executeReactStateUITask<State, 'shareState'>(
this,
'shareState',
async () => {
await this.unshareAllAnnotations()
},
)
}

render() {
return null
// <ShareAnnotationMenu
Expand Down
2 changes: 2 additions & 0 deletions src/overview/sharing/SingleNoteShareMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ export default class SingleNoteShareMenu extends React.PureComponent<
} = this.props
let sharingState = await contentSharingBG.shareAnnotation({
annotationUrl,
excludeFromLists: false,
shareToParentPageLists: true,
skipPrivacyLevelUpdate: true,
})
Expand Down Expand Up @@ -338,6 +339,7 @@ export default class SingleNoteShareMenu extends React.PureComponent<
const { annotationUrl, contentSharingBG } = this.props
await contentSharingBG.shareAnnotation({
annotationUrl,
excludeFromLists: true,
shareToParentPageLists: false,
skipPrivacyLevelUpdate: true,
})
Expand Down
9 changes: 8 additions & 1 deletion src/overview/sharing/components/ShareAnnotationMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { RemoteSyncSettingsInterface } from 'src/sync-settings/background/types'
import { copyToClipboard } from 'src/annotations/content_script/utils'
import { shareOptsToPrivacyLvl } from 'src/annotations/utils'
import { PageAnnotationsCacheInterface } from 'src/annotations/cache/types'
import { AnnotationPrivacyLevels } from '@worldbrain/memex-common/lib/annotations/types'

const COPY_TIMEOUT = 2000

Expand Down Expand Up @@ -110,7 +111,12 @@ class ShareAnnotationMenu extends PureComponent<Props, State> {

let sharingState = await contentSharingBG.shareAnnotation({
annotationUrl,
shareToParentPageLists: currentSharingLevel?.privacyLevel === 200,
shareToParentPageLists:
currentSharingLevel?.privacyLevel ===
AnnotationPrivacyLevels.SHARED,
excludeFromLists:
currentSharingLevel?.privacyLevel !==
AnnotationPrivacyLevels.SHARED,
skipPrivacyLevelUpdate: true,
})

Expand Down Expand Up @@ -163,6 +169,7 @@ class ShareAnnotationMenu extends PureComponent<Props, State> {
await contentSharingBG.shareAnnotation({
annotationUrl,
shareToParentPageLists: false,
excludeFromLists: true,
skipPrivacyLevelUpdate: true,
})

Expand Down

0 comments on commit 63ae627

Please sign in to comment.