Skip to content

Commit

Permalink
refactor: active themes and stack components into preferences service…
Browse files Browse the repository at this point in the history
…; removes use of .active property on component items
  • Loading branch information
moughxyz committed Jul 8, 2023
1 parent be5098a commit dbf25e7
Show file tree
Hide file tree
Showing 46 changed files with 423 additions and 299 deletions.
1 change: 0 additions & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
},
"devDependencies": {
"@types/jest": "^29.2.3",
"@types/lodash": "^4.14.189",
"@typescript-eslint/eslint-plugin": "*",
"eslint": "^8.27.0",
"eslint-plugin-prettier": "*",
Expand Down
5 changes: 3 additions & 2 deletions packages/models/src/Domain/Syncable/Component/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class SNComponent extends DecryptedItem<ComponentContent> implements Comp
public readonly area: ComponentArea
public readonly permissions: ComponentPermission[] = []
public readonly valid_until: Date
public readonly active: boolean
public readonly legacyActive: boolean
public readonly legacy_url?: string
public readonly isMobileDefault: boolean

Expand All @@ -69,7 +69,6 @@ export class SNComponent extends DecryptedItem<ComponentContent> implements Comp
this.area = payload.content.area
this.package_info = payload.content.package_info || {}
this.permissions = payload.content.permissions || []
this.active = payload.content.active ?? false
this.autoupdateDisabled = payload.content.autoupdateDisabled ?? false
this.disassociatedItemIds = payload.content.disassociatedItemIds || []
this.associatedItemIds = payload.content.associatedItemIds || []
Expand All @@ -85,6 +84,8 @@ export class SNComponent extends DecryptedItem<ComponentContent> implements Comp
this.legacy_url = !payload.content.hosted_url ? payload.content.url : undefined

this.legacyComponentData = this.payload.content.componentData || {}

this.legacyActive = payload.content.active ?? false
}

/** Do not duplicate components under most circumstances. Always keep original */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { ComponentArea, ComponentPermission, FeatureIdentifier, NoteType } from '@standardnotes/features'
import {
ComponentArea,
ComponentPermission,
FeatureIdentifier,
NoteType,
ThirdPartyFeatureDescription,
} from '@standardnotes/features'
import { ComponentPackageInfo } from './PackageInfo'
import { DecryptedItemInterface } from '../../Abstract/Item'
import { ComponentContent } from './ComponentContent'
Expand All @@ -20,8 +26,6 @@ export interface ComponentInterface extends DecryptedItemInterface<ComponentCont
area: ComponentArea
permissions: ComponentPermission[]
valid_until: Date
active: boolean
legacy_url?: string
isMobileDefault: boolean
isDeprecated: boolean

Expand All @@ -33,6 +37,16 @@ export interface ComponentInterface extends DecryptedItemInterface<ComponentCont
get noteType(): NoteType
get displayName(): string
get deprecationMessage(): string | undefined
get thirdPartyPackageInfo(): ThirdPartyFeatureDescription

/**
* @deprecated
* Replaced with active preferences managed by preferences service.
*/
legacyActive: boolean

/** @deprecated */
legacy_url?: string

/** @deprecated */
url?: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import { ComponentContent } from './ComponentContent'
import { DecryptedItemMutator } from '../../Abstract/Item/Mutator/DecryptedItemMutator'

export class ComponentMutator extends DecryptedItemMutator<ComponentContent> {
set active(active: boolean) {
this.mutableContent.active = active
}

set isMobileDefault(isMobileDefault: boolean) {
this.mutableContent.isMobileDefault = isMobileDefault
}
Expand Down
23 changes: 1 addition & 22 deletions packages/models/src/Domain/Syncable/Theme/ThemeMutator.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,4 @@
import { AppDataField } from '../../Abstract/Item/Types/AppDataField'
import { ComponentContent } from '../Component/ComponentContent'
import { DecryptedItemMutator } from '../../Abstract/Item/Mutator/DecryptedItemMutator'

export class ThemeMutator extends DecryptedItemMutator<ComponentContent> {
setMobileRules(rules: unknown) {
this.setAppDataItem(AppDataField.MobileRules, rules)
}

setNotAvailOnMobile(notAvailable: boolean) {
this.setAppDataItem(AppDataField.NotAvailableOnMobile, notAvailable)
}

set local_url(local_url: string) {
this.mutableContent.local_url = local_url
}

/**
* We must not use .active because if you set that to true, it will also
* activate that theme on desktop/web
*/
setMobileActive(active: boolean) {
this.setAppDataItem(AppDataField.MobileActive, active)
}
}
export class ThemeMutator extends DecryptedItemMutator<ComponentContent> {}
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import {
PrefKey,
CollectionSort,
NewNoteTitleFormat,
EditorLineHeight,
EditorFontSize,
EditorLineWidth,
PrefValue,
} from '@standardnotes/models'
import { FeatureIdentifier } from '@standardnotes/snjs'
import { FeatureIdentifier } from '@standardnotes/features'
import { CollectionSort } from '../../Runtime/Collection/CollectionSort'
import { EditorFontSize } from './EditorFontSize'
import { EditorLineHeight } from './EditorLineHeight'
import { EditorLineWidth } from './EditorLineWidth'
import { PrefKey, PrefValue } from './PrefKey'
import { NewNoteTitleFormat } from './NewNoteTitleFormat'

export const PrefDefaults = {
[PrefKey.TagsPanelWidth]: 220,
Expand Down Expand Up @@ -45,6 +42,8 @@ export const PrefDefaults = {
[PrefKey.SystemViewPreferences]: {},
[PrefKey.AuthenticatorNames]: '',
[PrefKey.ComponentPreferences]: {},
[PrefKey.ActiveThemes]: [],
[PrefKey.ActiveComponents]: [],
} satisfies {
[key in PrefKey]: PrefValue[key]
}
4 changes: 4 additions & 0 deletions packages/models/src/Domain/Syncable/UserPrefs/PrefKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export enum PrefKey {
AuthenticatorNames = 'authenticatorNames',
PaneGesturesEnabled = 'paneGesturesEnabled',
ComponentPreferences = 'componentPreferences',
ActiveThemes = 'activeThemes',
ActiveComponents = 'activeComponents',
}

export type PrefValue = {
Expand Down Expand Up @@ -82,4 +84,6 @@ export type PrefValue = {
[PrefKey.AuthenticatorNames]: string
[PrefKey.PaneGesturesEnabled]: boolean
[PrefKey.ComponentPreferences]: AllComponentPreferences
[PrefKey.ActiveThemes]: string[]
[PrefKey.ActiveComponents]: string[]
}
1 change: 1 addition & 0 deletions packages/models/src/Domain/Syncable/UserPrefs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './EditorFontSize'
export * from './EditorLineWidth'
export * from './NewNoteTitleFormat'
export * from './ComponentPreferences'
export * from './PrefDefaults'
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PreferenceServiceInterface } from './../Preferences/PreferenceServiceInterface'
import { AsymmetricMessageServiceInterface } from './../AsymmetricMessage/AsymmetricMessageServiceInterface'
import { SyncOptions } from './../Sync/SyncOptions'
import { ImportDataReturnType } from './../Mutator/ImportDataUseCase'
Expand Down Expand Up @@ -105,6 +106,7 @@ export interface ApplicationInterface {
get challenges(): ChallengeServiceInterface
get alerts(): AlertService
get asymmetric(): AsymmetricMessageServiceInterface
get preferences(): PreferenceServiceInterface

readonly identifier: ApplicationIdentifier
readonly platform: Platform
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ComponentOrNativeFeature,
PermissionDialog,
SNNote,
SNTheme,
} from '@standardnotes/models'

import { DesktopManagerInterface } from '../Device/DesktopManagerInterface'
Expand All @@ -31,6 +32,7 @@ export interface ComponentManagerInterface {
presentPermissionsDialog(_dialog: PermissionDialog): void
legacyGetDefaultEditor(): ComponentInterface | undefined
componentWithIdentifier(identifier: FeatureIdentifier | string): ComponentOrNativeFeature | undefined
toggleTheme(uuid: string): Promise<void>

toggleTheme(theme: SNTheme): Promise<void>
toggleComponent(component: ComponentInterface): Promise<void>
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { ComponentOrNativeFeature, ComponentPreferencesEntry, PrefKey, PrefValue } from '@standardnotes/models'
import {
ComponentInterface,
ComponentOrNativeFeature,
ComponentPreferencesEntry,
PrefKey,
PrefValue,
SNTheme,
} from '@standardnotes/models'
import { AbstractService } from '../Service/AbstractService'

/* istanbul ignore file */

export enum PreferencesServiceEvent {
PreferencesChanged = 'PreferencesChanged',
}
Expand All @@ -15,4 +20,16 @@ export interface PreferenceServiceInterface extends AbstractService<PreferencesS

setComponentPreferences(component: ComponentOrNativeFeature, preferences: ComponentPreferencesEntry): Promise<void>
getComponentPreferences(component: ComponentOrNativeFeature): ComponentPreferencesEntry | undefined

addActiveTheme(theme: SNTheme): Promise<void>
replaceActiveTheme(theme: SNTheme): Promise<void>
removeActiveTheme(theme: SNTheme): Promise<void>
getActiveThemes(): SNTheme[]
getActiveThemesUuids(): string[]
isThemeActive(theme: SNTheme): boolean

addActiveComponent(component: ComponentInterface): Promise<void>
removeActiveComponent(component: ComponentInterface): Promise<void>
getActiveComponents(): ComponentInterface[]
isComponentActive(component: ComponentInterface): boolean
}
4 changes: 4 additions & 0 deletions packages/snjs/lib/Application/Application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,10 @@ export class SNApplication implements ApplicationInterface, AppGroupManagedAppli
return this.sharedVaultService
}

public get preferences(): ExternalServices.PreferenceServiceInterface {
return this.preferencesService
}

public computePrivateUsername(username: string): Promise<string | undefined> {
return ComputePrivateUsername(this.options.crypto, username)
}
Expand Down
20 changes: 19 additions & 1 deletion packages/snjs/lib/Migrations/Versions/2_201_6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ApplicationStage } from '@standardnotes/services'
import { Migration } from '@Lib/Migrations/Migration'
import { ContentType } from '@standardnotes/common'
import { AllComponentPreferences, ComponentInterface, PrefKey, isNativeComponent } from '@standardnotes/models'
import { Copy } from '@standardnotes/utils'
import { Copy, Uuids } from '@standardnotes/utils'

export class Migration2_201_6 extends Migration {
static override version(): string {
Expand All @@ -12,6 +12,7 @@ export class Migration2_201_6 extends Migration {
protected registerStageHandlers(): void {
this.registerStageHandler(ApplicationStage.FullSyncCompleted_13, async () => {
await this.migrateComponentDataToUserPreferences()
await this.migrateActiveComponentsToUserPreferences()
this.markDone()
})
}
Expand Down Expand Up @@ -49,4 +50,21 @@ export class Migration2_201_6 extends Migration {

await this.services.preferences.setValue(PrefKey.ComponentPreferences, mutablePreferencesValue)
}

private async migrateActiveComponentsToUserPreferences(): Promise<void> {
const allActiveitems = [
...this.services.itemManager.getItems<ComponentInterface>(ContentType.Component),
...this.services.itemManager.getItems<ComponentInterface>(ContentType.Theme),
].filter((component) => component.legacyActive)

if (allActiveitems.length === 0) {
return
}

const activeThemes = allActiveitems.filter((component) => component.isTheme())
const activeComponents = allActiveitems.filter((component) => !component.isTheme())

await this.services.preferences.setValue(PrefKey.ActiveThemes, Uuids(activeThemes))
await this.services.preferences.setValue(PrefKey.ActiveComponents, Uuids(activeComponents))
}
}
10 changes: 6 additions & 4 deletions packages/snjs/lib/Services/Api/ApiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ import { isUrlFirstParty, TRUSTED_FEATURE_HOSTS } from '@Lib/Hosts'
import { Paths } from './Paths'
import { DiskStorageService } from '../Storage/DiskStorageService'
import { UuidString } from '../../Types/UuidString'
import merge from 'lodash/merge'
import { SettingsServerInterface } from '../Settings/SettingsServerInterface'
import { Strings } from '@Lib/Strings'

Expand Down Expand Up @@ -199,9 +198,12 @@ export class SNApiService
}

private params(inParams: Record<string | number | symbol, unknown>): HttpRequestParams {
const params = merge(inParams, {
[ApiEndpointParam.ApiVersion]: this.apiVersion,
})
const params = {
...inParams,
...{
[ApiEndpointParam.ApiVersion]: this.apiVersion,
},
}
return params
}

Expand Down
Loading

0 comments on commit dbf25e7

Please sign in to comment.