Browse Source

fix: add a JSON-parseable field, make small data tweaks, and add commentary on JSON fields

pull/188/head
Trent Larson 2 days ago
parent
commit
28cea8f55b
  1. 2
      src/db/databaseUtil.ts
  2. 2
      src/db/tables/contacts.ts
  3. 6
      src/db/tables/settings.ts
  4. 2
      src/libs/endorserServer.ts
  5. 2
      src/test/index.ts
  6. 27
      src/utils/PlatformServiceMixin.ts

2
src/db/databaseUtil.ts

@ -567,6 +567,8 @@ export async function debugSettingsData(did?: string): Promise<void> {
* - Web SQLite (wa-sqlite/absurd-sql): Auto-parses JSON strings to objects
* - Capacitor SQLite: Returns raw strings that need manual parsing
*
* Maybe consolidate with PlatformServiceMixin._parseJsonField
*
* @param value The value to parse (could be string or already parsed object)
* @param defaultValue Default value if parsing fails
* @returns Parsed object or default value

2
src/db/tables/contacts.ts

@ -9,6 +9,8 @@ export type Contact = {
// When adding a property:
// - Consider whether it should be added when exporting & sharing contacts, eg. DataExportSection
// - If it's a boolean, it should be converted from a 0/1 integer in PlatformServiceMixin._mapColumnsToValues
// - If it's a JSON string, it should be converted to an object/array in PlatformServiceMixin._mapColumnsToValues
//
did: string;
contactMethods?: Array<ContactMethod>;

6
src/db/tables/settings.ts

@ -14,6 +14,12 @@ export type BoundingBox = {
* New entries that are boolean should also be added to PlatformServiceMixin._mapColumnsToValues
*/
export type Settings = {
//
// When adding a property:
// - If it's a boolean, it should be converted from a 0/1 integer in PlatformServiceMixin._mapColumnsToValues
// - If it's a JSON string, it should be converted to an object/array in PlatformServiceMixin._mapColumnsToValues
//
// default entry is keyed with MASTER_SETTINGS_KEY; other entries are linked to an account with account ID
id?: string | number; // this is erased for all those entries that are keyed with accountDid

2
src/libs/endorserServer.ts

@ -315,7 +315,7 @@ export function didInfoForContact(
return { displayName: "You", known: true };
} else if (contact) {
return {
displayName: contact.name || "Contact With No Name",
displayName: contact.name || "Contact Without a Name",
known: true,
profileImageUrl: contact.profileImageUrl,
};

2
src/test/index.ts

@ -66,7 +66,7 @@ export async function testServerRegisterUser() {
// Make a payload for the claim
const vcPayload = {
sub: "RegisterAction",
sub: identity0.did,
vc: {
"@context": ["https://www.w3.org/2018/credentials/v1"],
type: ["VerifiableCredential"],

27
src/utils/PlatformServiceMixin.ts

@ -275,16 +275,22 @@ export const PlatformServiceMixin = {
// Convert SQLite integer booleans to JavaScript booleans
if (
// settings
column === "isRegistered" ||
column === "finishedOnboarding" ||
column === "filterFeedByVisible" ||
column === "filterFeedByNearby" ||
column === "hasBackedUpSeed" ||
column === "hideRegisterPromptOnNewContact" ||
column === "showContactGivesInline" ||
column === "showGeneralAdvanced" ||
column === "showShortcutBvc" ||
column === "warnIfProdServer" ||
column === "warnIfTestServer"
column === "warnIfTestServer" ||
// contacts
column === "iViewContent" ||
column === "registered" ||
column === "seesMe"
) {
if (value === 1) {
value = true;
@ -294,13 +300,9 @@ export const PlatformServiceMixin = {
// Keep null values as null
}
// Handle JSON fields like contactMethods
if (column === "contactMethods" && typeof value === "string") {
try {
value = JSON.parse(value);
} catch {
value = [];
}
// Convert SQLite JSON strings to objects/arrays
if (column === "contactMethods" || column === "searchBoxes") {
value = this._parseJsonField(value, []);
}
obj[column] = value;
@ -310,10 +312,13 @@ export const PlatformServiceMixin = {
},
/**
* Self-contained implementation of parseJsonField
* Safely parses JSON strings with fallback to default value
* Safely parses JSON strings with fallback to default value.
* Handles different SQLite implementations:
* - Web SQLite (wa-sqlite/absurd-sql): Auto-parses JSON strings to objects
* - Capacitor SQLite: Returns raw strings that need manual parsing
*
* Consolidate this with src/libs/util.ts parseJsonField
* See also src/db/databaseUtil.ts parseJsonField
* and maybe consolidate
*/
_parseJsonField<T>(value: unknown, defaultValue: T): T {
if (typeof value === "string") {

Loading…
Cancel
Save