Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add setIsArchiveDevice() method #932

Merged
merged 9 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions drizzle/client/0001_nifty_colleen_wing.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE localDeviceInfo ADD `isArchiveDevice` integer;
206 changes: 206 additions & 0 deletions drizzle/client/meta/0001_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
{
"version": "5",
"dialect": "sqlite",
"id": "b833dd92-7d1e-45c1-bd85-297bac508977",
"prevId": "e75103ab-bbcb-4ca9-a0c4-2d9095b63add",
"tables": {
"localDeviceInfo": {
"name": "localDeviceInfo",
"columns": {
"deviceId": {
"name": "deviceId",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"deviceInfo": {
"name": "deviceInfo",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"isArchiveDevice": {
"name": "isArchiveDevice",
"type": "integer",
"primaryKey": false,
"notNull": false,
"autoincrement": false
}
},
"indexes": {
"localDeviceInfo_deviceId_unique": {
"name": "localDeviceInfo_deviceId_unique",
"columns": [
"deviceId"
],
"isUnique": true
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"projectSettings_backlink": {
"name": "projectSettings_backlink",
"columns": {
"versionId": {
"name": "versionId",
"type": "text",
"primaryKey": true,
"notNull": true,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"projectKeys": {
"name": "projectKeys",
"columns": {
"projectId": {
"name": "projectId",
"type": "text",
"primaryKey": true,
"notNull": true,
"autoincrement": false
},
"projectPublicId": {
"name": "projectPublicId",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"projectInviteId": {
"name": "projectInviteId",
"type": "blob",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"keysCipher": {
"name": "keysCipher",
"type": "blob",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"projectInfo": {
"name": "projectInfo",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": "'{}'"
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"projectSettings": {
"name": "projectSettings",
"columns": {
"docId": {
"name": "docId",
"type": "text",
"primaryKey": true,
"notNull": true,
"autoincrement": false
},
"versionId": {
"name": "versionId",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"originalVersionId": {
"name": "originalVersionId",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"schemaName": {
"name": "schemaName",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"createdAt": {
"name": "createdAt",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"updatedAt": {
"name": "updatedAt",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"links": {
"name": "links",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"deleted": {
"name": "deleted",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"defaultPresets": {
"name": "defaultPresets",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"configMetadata": {
"name": "configMetadata",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"forks": {
"name": "forks",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
}
}
7 changes: 7 additions & 0 deletions drizzle/client/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
"when": 1726514276007,
"tag": "0000_bumpy_carnage",
"breakpoints": true
},
{
"idx": 1,
"version": "5",
"when": 1729596145319,
"tag": "0001_nifty_colleen_wing",
"breakpoints": true
}
]
}
62 changes: 56 additions & 6 deletions src/mapeo-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { randomBytes } from 'crypto'
import path from 'path'
import { KeyManager } from '@mapeo/crypto'
import Database from 'better-sqlite3'
import { eq } from 'drizzle-orm'
import { count, eq } from 'drizzle-orm'
import { drizzle } from 'drizzle-orm/better-sqlite3'
import { migrate } from 'drizzle-orm/better-sqlite3/migrator'
import Hypercore from 'hypercore'
Expand All @@ -16,10 +16,11 @@ import {
kBlobStore,
kClearDataIfLeft,
kProjectLeave,
kSetIsArchiveDevice,
kSetOwnDeviceInfo,
} from './mapeo-project.js'
import {
localDeviceInfoTable,
deviceSettingsTable,
projectKeysTable,
projectSettingsTable,
} from './schema/client.js'
Expand Down Expand Up @@ -507,6 +508,7 @@ export class MapeoManager extends TypedEmitter {
async #createProjectInstance(projectKeys) {
validateProjectKeys(projectKeys)
const projectId = keyToId(projectKeys.projectKey)
const isArchiveDevice = this.getIsArchiveDevice()
const project = new MapeoProject({
...this.#projectStorage(projectId),
...projectKeys,
Expand All @@ -517,6 +519,7 @@ export class MapeoManager extends TypedEmitter {
localPeers: this.#localPeers,
logger: this.#loggerBase,
getMediaBaseUrl: this.#getMediaBaseUrl.bind(this),
isArchiveDevice,
})
await project[kClearDataIfLeft]()
return project
Expand Down Expand Up @@ -746,10 +749,10 @@ export class MapeoManager extends TypedEmitter {
async setDeviceInfo(deviceInfo) {
const values = { deviceId: this.#deviceId, deviceInfo }
this.#db
.insert(localDeviceInfoTable)
.insert(deviceSettingsTable)
.values(values)
.onConflictDoUpdate({
target: localDeviceInfoTable.deviceId,
target: deviceSettingsTable.deviceId,
set: values,
})
.run()
Expand Down Expand Up @@ -784,8 +787,8 @@ export class MapeoManager extends TypedEmitter {
getDeviceInfo() {
const row = this.#db
.select()
.from(localDeviceInfoTable)
.where(eq(localDeviceInfoTable.deviceId, this.#deviceId))
.from(deviceSettingsTable)
.where(eq(deviceSettingsTable.deviceId, this.#deviceId))
.get()
return {
deviceId: this.#deviceId,
Expand All @@ -794,6 +797,53 @@ export class MapeoManager extends TypedEmitter {
}
}

/**
* Set whether this device is an archive device. Archive devices will download
* all media during sync, where-as non-archive devices will not download media
* original variants, and only download preview and thumbnail variants.
* @param {boolean} isArchiveDevice
*/
gmaclennan marked this conversation as resolved.
Show resolved Hide resolved
setIsArchiveDevice(isArchiveDevice) {
const result = this.#db
.update(deviceSettingsTable)
.set({ isArchiveDevice })
.where(eq(deviceSettingsTable.deviceId, this.#deviceId))
.run()
if (result.changes === 0) {
const rowCount = this.#db
.select({ count: count() })
.from(deviceSettingsTable)
.where(eq(deviceSettingsTable.deviceId, this.#deviceId))
.get()
if (!rowCount || rowCount.count === 0) {
throw new Error('Must set device info before setting archive device')
}
EvanHahn marked this conversation as resolved.
Show resolved Hide resolved
}
for (const project of this.#activeProjects.values()) {
project[kSetIsArchiveDevice](isArchiveDevice)
}
}

/**
* Get whether this device is an archive device. Archive devices will download
* all media during sync, where-as non-archive devices will not download media
* original variants, and only download preview and thumbnail variants.
* @returns {boolean} isArchiveDevice
*/
getIsArchiveDevice() {
const row = this.#db
.select()
.from(deviceSettingsTable)
.where(eq(deviceSettingsTable.deviceId, this.#deviceId))
.get()
if (typeof row?.isArchiveDevice === 'boolean') {
return row.isArchiveDevice
} else {
// TODO: Possibly change default depending on device type
gmaclennan marked this conversation as resolved.
Show resolved Hide resolved
return true
}
}

/**
* @returns {InviteApi}
*/
Expand Down
15 changes: 15 additions & 0 deletions src/mapeo-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export const kProjectReplicate = Symbol('replicate project')
export const kDataTypes = Symbol('dataTypes')
export const kProjectLeave = Symbol('leave project')
export const kClearDataIfLeft = Symbol('clear data if left project')
export const kSetIsArchiveDevice = Symbol('set isArchiveDevice')
export const kIsArchiveDevice = Symbol('isArchiveDevice (temp - test only)')

const EMPTY_PROJECT_SETTINGS = Object.freeze({})

Expand Down Expand Up @@ -93,6 +95,7 @@ export class MapeoProject extends TypedEmitter {
#loadingConfig

static EMPTY_PROJECT_SETTINGS = EMPTY_PROJECT_SETTINGS
#isArchiveDevice
EvanHahn marked this conversation as resolved.
Show resolved Hide resolved

/**
* @param {Object} opts
Expand All @@ -108,6 +111,7 @@ export class MapeoProject extends TypedEmitter {
* @param {(mediaType: 'blobs' | 'icons') => Promise<string>} opts.getMediaBaseUrl
* @param {import('./local-peers.js').LocalPeers} opts.localPeers
* @param {Logger} [opts.logger]
* @param {boolean} [opts.isArchiveDevice] Whether this device is an archive device
gmaclennan marked this conversation as resolved.
Show resolved Hide resolved
*
*/
constructor({
Expand All @@ -123,13 +127,15 @@ export class MapeoProject extends TypedEmitter {
getMediaBaseUrl,
localPeers,
logger,
isArchiveDevice,
}) {
super()

this.#l = Logger.create('project', logger)
this.#deviceId = getDeviceId(keyManager)
this.#projectId = projectKeyToId(projectKey)
this.#loadingConfig = false
this.#isArchiveDevice = isArchiveDevice

///////// 1. Setup database
this.#sqlite = new Database(dbPath)
Expand Down Expand Up @@ -625,6 +631,15 @@ export class MapeoProject extends TypedEmitter {
return deviceInfo.update(existingDoc.versionId, doc)
}

/** @param {boolean} isArchiveDevice */
async [kSetIsArchiveDevice](isArchiveDevice) {
this.#isArchiveDevice = isArchiveDevice
}

get [kIsArchiveDevice]() {
gmaclennan marked this conversation as resolved.
Show resolved Hide resolved
return this.#isArchiveDevice
}

/**
* @returns {import('./icon-api.js').IconApi}
*/
Expand Down
Loading
Loading