All files / src/services profile-service.ts

93.79% Statements 136/145
76.66% Branches 23/30
60% Functions 3/5
93.79% Lines 136/145

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 1461x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 7x 9x 9x 9x 9x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x     9x 5x 5x 9x 1x 1x 6x 6x 6x 6x 6x     9x           9x 6x 6x 6x 6x 6x 6x 9x 4x 4x 4x 4x 9x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 2x 2x 2x  
import * as vscode from 'vscode';
import { getSupabase } from '../auth/supabaseClient';
import { globalContext } from '../extension';
 
//storing user's display name
const DISPLAY_NAME_KEY = 'collabAgent.displayName';
 
 
//Result object containing display name and its source.
export interface DisplayNameResult {
  displayName: string;
  source: 'supabase' | 'cached' | 'prompt' | 'fallback';
}
 
/**
 * Gets or initializes a display name for the user.
 * Tries multiple sources: cached, Supabase metadata, user prompt, or fallback.
 * 
 * @param nonInteractive - If true, won't prompt user for input
 * @returns
 */
 
export async function getOrInitDisplayName(nonInteractive = false): Promise<DisplayNameResult> {
  // Debug logging 
  console.log('[getOrInitDisplayName] Starting...');
  console.log('[getOrInitDisplayName] SUPABASE_URL:', process.env.SUPABASE_URL ? 'LOADED' : 'NOT FOUND');
  console.log('[getOrInitDisplayName] SUPABASE_ANON_KEY:', process.env.SUPABASE_ANON_KEY ? 'LOADED' : 'NOT FOUND');
 
  // 1. If cached in globalState, return it.
  const cached = globalContext?.globalState.get<string>(DISPLAY_NAME_KEY);
  if (cached) {
    return { displayName: cached, source: 'cached' };
  }
  
  // 2. Try Supabase auth user metadata.
  try {
    console.log('[getOrInitDisplayName] Attempting to get Supabase client...');
    const supabase = getSupabase();
    console.log('[getOrInitDisplayName] Supabase client obtained.');
    console.log('[getOrInitDisplayName] Getting user...');
    const { data, error } = await supabase.auth.getUser();
    console.log('[getOrInitDisplayName] getUser result - data:', !!data, 'error:', error);
    const user = data?.user;
    console.log('[getOrInitDisplayName] User exists:', !!user);
    
    if (user?.user_metadata) {
      const meta = user.user_metadata as any;
      console.log('[getOrInitDisplayName] User metadata keys:', Object.keys(meta));
      
      // Try multiple possible GitHub metadata fields
      const candidate = meta.user_name 
        || meta.preferred_username 
        || meta.full_name 
        || meta.name 
        || meta.login  // GitHub login name
        || meta.nickname
        || (user.email ? user.email.split('@')[0] : undefined); // Email username as fallback
        
      if (candidate) {
        console.log('[getOrInitDisplayName] Found name from Supabase:', candidate);
        await globalContext.globalState.update(DISPLAY_NAME_KEY, candidate);
        return { displayName: candidate, source: 'supabase' };
      } else {
        console.log('[getOrInitDisplayName] No suitable name found in user metadata.');
      }
    } else {
      console.log('[getOrInitDisplayName] No user metadata available.');
    }
  } catch (e) {
    console.error('[getOrInitDisplayName] Error fetching user from Supabase:', e);
  }
  
  // 3. Try to get from git config as fallback
  try {
    const gitConfigPath = require('os').homedir() + '/.gitconfig';
    const data = await vscode.workspace.fs.readFile(vscode.Uri.file(gitConfigPath));
    const content = Buffer.from(data).toString();
    const match = content.match(/name\s*=\s*(.+)/);
    if (match && match[1].trim()) {
      const gitName = match[1].trim();
      console.log('[getOrInitDisplayName] Found name from git config:', gitName);
      await globalContext.globalState.update(DISPLAY_NAME_KEY, gitName);
      return { displayName: gitName, source: 'fallback' };
    }
  } catch (e) {
    console.log('[getOrInitDisplayName] Could not read git config:', e);
  }
  
  // 4. Try system username
  try {
    const systemUsername = require('os').userInfo().username;
    if (systemUsername) {
      console.log('[getOrInitDisplayName] Using system username:', systemUsername);
      await globalContext.globalState.update(DISPLAY_NAME_KEY, systemUsername);
      return { displayName: systemUsername, source: 'fallback' };
    }
  } catch (e) {
    console.log('[getOrInitDisplayName] Could not get system username:', e);
  }
  
  // 5. Optionally prompt user (unless nonInteractive)
  if (!nonInteractive) {
    const input = await vscode.window.showInputBox({
      title: 'Set Display Name',
      prompt: 'Enter the name to show to others in Live Share sessions',
      ignoreFocusOut: true,
      validateInput: (val) => !val.trim() ? 'Display name cannot be empty' : undefined
    });
    if (input && input.trim()) {
      await globalContext.globalState.update(DISPLAY_NAME_KEY, input.trim());
      return { displayName: input.trim(), source: 'prompt' };
    }
  }
  
  // 6. Final fallback.
  return { displayName: 'User', source: 'fallback' };
}
 
/**
 * Allows user to explicitly set or change their display name.
 * Shows input dialog with current name pre-filled.
 */
export async function setDisplayNameExplicit(): Promise<void> {
  const current = globalContext.globalState.get<string>(DISPLAY_NAME_KEY) || '';
  const input = await vscode.window.showInputBox({
    title: 'Change Display Name',
    value: current,
    prompt: 'Enter a new display name for Live Share sessions',
    ignoreFocusOut: true,
    validateInput: (val) => !val.trim() ? 'Display name cannot be empty' : undefined
  });
  if (input && input.trim()) {
    await globalContext.globalState.update(DISPLAY_NAME_KEY, input.trim());
    vscode.window.showInformationMessage(`Collab Agent: Display name updated to "${input.trim()}"`);
  }
}
 
/**
 * Gets the cached display name from global state.
 * 
 * @returns The cached display name or undefined if not set
 */
export function getCachedDisplayName(): string | undefined {
  return globalContext?.globalState.get<string>(DISPLAY_NAME_KEY);
}