Browse Source

fix: correct active identity fallback logic for phase c

- Fix fallback condition to use DROP_SETTINGS_ACTIVEDID flag
- Update code formatting and import organization
- Add missing Active Identity façade method declarations
- Ensure proper TypeScript interface coverage

This fixes the fallback logic that was incorrectly checking
USE_ACTIVE_IDENTITY_ONLY instead of DROP_SETTINGS_ACTIVEDID,
preventing proper Phase C behavior.
activedid_migration
Matthew Raymer 20 hours ago
parent
commit
e172faaaf2
  1. 71
      src/utils/PlatformServiceMixin.ts

71
src/utils/PlatformServiceMixin.ts

@ -49,10 +49,7 @@ import {
type Settings, type Settings,
type SettingsWithJsonStrings, type SettingsWithJsonStrings,
} from "@/db/tables/settings"; } from "@/db/tables/settings";
import { import { DEFAULT_SCOPE, type ActiveIdentity } from "@/db/tables/activeIdentity";
DEFAULT_SCOPE,
type ActiveIdentity
} from "@/db/tables/activeIdentity";
import { FLAGS } from "@/config/featureFlags"; import { FLAGS } from "@/config/featureFlags";
import { logger } from "@/utils/logger"; import { logger } from "@/utils/logger";
import { Contact, ContactMaybeWithJsonStrings } from "@/db/tables/contacts"; import { Contact, ContactMaybeWithJsonStrings } from "@/db/tables/contacts";
@ -984,30 +981,30 @@ export const PlatformServiceMixin = {
try { try {
// Try new active_identity table first // Try new active_identity table first
const row = await this.$first<ActiveIdentity>( const row = await this.$first<ActiveIdentity>(
'SELECT active_did FROM active_identity WHERE scope = ? LIMIT 1', "SELECT active_did FROM active_identity WHERE scope = ? LIMIT 1",
[scope] [scope],
); );
if (row?.active_did) { if (row?.active_did) {
return row.active_did; return row.active_did;
} }
// Fallback to legacy settings.activeDid during Phase A (unless flag prevents it) // Fallback to legacy settings.activeDid during Phase A/B (unless Phase C is complete)
if (!FLAGS.USE_ACTIVE_IDENTITY_ONLY) { if (!FLAGS.DROP_SETTINGS_ACTIVEDID) {
if (FLAGS.LOG_ACTIVE_ID_FALLBACK) { if (FLAGS.LOG_ACTIVE_ID_FALLBACK) {
logger.warn('[ActiveDid] Fallback to legacy settings.activeDid'); logger.warn("[ActiveDid] Fallback to legacy settings.activeDid");
} }
const legacy = await this.$first<Settings>( const legacy = await this.$first<Settings>(
'SELECT activeDid FROM settings WHERE id = ? LIMIT 1', "SELECT activeDid FROM settings WHERE id = ? LIMIT 1",
[MASTER_SETTINGS_KEY] [MASTER_SETTINGS_KEY],
); );
return legacy?.activeDid || null; return legacy?.activeDid || null;
} }
return null; return null;
} catch (error) { } catch (error) {
logger.error('[PlatformServiceMixin] Error getting activeDid:', error); logger.error("[PlatformServiceMixin] Error getting activeDid:", error);
return null; return null;
} }
}, },
@ -1020,27 +1017,32 @@ export const PlatformServiceMixin = {
* @param scope Scope identifier (default: 'default') * @param scope Scope identifier (default: 'default')
* @returns Promise<void> * @returns Promise<void>
*/ */
async $setActiveDid(did: string | null, scope: string = DEFAULT_SCOPE): Promise<void> { async $setActiveDid(
did: string | null,
scope: string = DEFAULT_SCOPE,
): Promise<void> {
try { try {
if (!did) { if (!did) {
throw new Error('Cannot set null DID as active'); throw new Error("Cannot set null DID as active");
} }
// Validate that the DID exists in accounts table // Validate that the DID exists in accounts table
const accountExists = await this.$first<Account>( const accountExists = await this.$first<Account>(
'SELECT did FROM accounts WHERE did = ? LIMIT 1', "SELECT did FROM accounts WHERE did = ? LIMIT 1",
[did] [did],
); );
if (!accountExists) { if (!accountExists) {
throw new Error(`Cannot set activeDid to non-existent account: ${did}`); throw new Error(
`Cannot set activeDid to non-existent account: ${did}`,
);
} }
await this.$withTransaction(async () => { await this.$withTransaction(async () => {
// Update/insert into active_identity table // Update/insert into active_identity table
const existingRecord = await this.$first<ActiveIdentity>( const existingRecord = await this.$first<ActiveIdentity>(
'SELECT id FROM active_identity WHERE scope = ? LIMIT 1', "SELECT id FROM active_identity WHERE scope = ? LIMIT 1",
[scope] [scope],
); );
if (existingRecord) { if (existingRecord) {
@ -1049,22 +1051,22 @@ export const PlatformServiceMixin = {
`UPDATE active_identity `UPDATE active_identity
SET active_did = ?, updated_at = strftime('%Y-%m-%dT%H:%M:%fZ','now') SET active_did = ?, updated_at = strftime('%Y-%m-%dT%H:%M:%fZ','now')
WHERE scope = ?`, WHERE scope = ?`,
[did, scope] [did, scope],
); );
} else { } else {
// Insert new record // Insert new record
await this.$dbExec( await this.$dbExec(
`INSERT INTO active_identity (scope, active_did, updated_at) `INSERT INTO active_identity (scope, active_did, updated_at)
VALUES (?, ?, strftime('%Y-%m-%dT%H:%M:%fZ','now'))`, VALUES (?, ?, strftime('%Y-%m-%dT%H:%M:%fZ','now'))`,
[scope, did] [scope, did],
); );
} }
// Maintain legacy settings.activeDid during Phase A (unless Phase C is complete) // Maintain legacy settings.activeDid during Phase A (unless Phase C is complete)
if (!FLAGS.DROP_SETTINGS_ACTIVEDID) { if (!FLAGS.DROP_SETTINGS_ACTIVEDID) {
await this.$dbExec( await this.$dbExec(
'UPDATE settings SET activeDid = ? WHERE id = ?', "UPDATE settings SET activeDid = ? WHERE id = ?",
[did, MASTER_SETTINGS_KEY] [did, MASTER_SETTINGS_KEY],
); );
} }
}); });
@ -1074,7 +1076,7 @@ export const PlatformServiceMixin = {
logger.info(`[PlatformServiceMixin] Active DID updated to: ${did}`); logger.info(`[PlatformServiceMixin] Active DID updated to: ${did}`);
} catch (error) { } catch (error) {
logger.error('[PlatformServiceMixin] Error setting activeDid:', error); logger.error("[PlatformServiceMixin] Error setting activeDid:", error);
throw error; throw error;
} }
}, },
@ -1099,11 +1101,14 @@ export const PlatformServiceMixin = {
async $getActiveIdentityScopes(): Promise<string[]> { async $getActiveIdentityScopes(): Promise<string[]> {
try { try {
const scopes = await this.$query<{ scope: string }>( const scopes = await this.$query<{ scope: string }>(
'SELECT DISTINCT scope FROM active_identity ORDER BY scope' "SELECT DISTINCT scope FROM active_identity ORDER BY scope",
); );
return scopes.map(row => row.scope); return scopes.map((row) => row.scope);
} catch (error) { } catch (error) {
logger.error('[PlatformServiceMixin] Error getting active identity scopes:', error); logger.error(
"[PlatformServiceMixin] Error getting active identity scopes:",
error,
);
return []; return [];
} }
}, },
@ -1852,6 +1857,12 @@ export interface IPlatformServiceMixin {
// Debug methods // Debug methods
$debugDidSettings(did: string): Promise<Settings | null>; $debugDidSettings(did: string): Promise<Settings | null>;
$debugMergedSettings(did: string): Promise<void>; $debugMergedSettings(did: string): Promise<void>;
// Active Identity façade methods
$getActiveDid(scope?: string): Promise<string | null>;
$setActiveDid(did: string | null, scope?: string): Promise<void>;
$switchActiveIdentity(did: string): Promise<void>;
$getActiveIdentityScopes(): Promise<string[]>;
} }
// TypeScript declaration merging to eliminate (this as any) type assertions // TypeScript declaration merging to eliminate (this as any) type assertions
@ -1868,6 +1879,12 @@ declare module "@vue/runtime-core" {
currentActiveDid: string | null; currentActiveDid: string | null;
$updateActiveDid(newDid: string | null): Promise<void>; $updateActiveDid(newDid: string | null): Promise<void>;
// Active Identity façade methods
$getActiveDid(scope?: string): Promise<string | null>;
$setActiveDid(did: string | null, scope?: string): Promise<void>;
$switchActiveIdentity(did: string): Promise<void>;
$getActiveIdentityScopes(): Promise<string[]>;
// Ultra-concise database methods (shortest possible names) // Ultra-concise database methods (shortest possible names)
$db(sql: string, params?: unknown[]): Promise<QueryExecResult | undefined>; $db(sql: string, params?: unknown[]): Promise<QueryExecResult | undefined>;
$exec(sql: string, params?: unknown[]): Promise<DatabaseExecResult>; $exec(sql: string, params?: unknown[]): Promise<DatabaseExecResult>;

Loading…
Cancel
Save