UNPKG

@datalayer/core

Version:
99 lines (98 loc) 3.16 kB
/* * Copyright (c) 2023-2025 Datalayer, Inc. * Distributed under the terms of the Modified BSD License. */ import { asDisplayName, namesAsInitials } from '../utils'; import { asIAMProviderLinked } from "./IAMProviderLinked"; import { BOOTSTRAP_USER_ONBOARDING } from './UserOnboarding'; import { UserSettings } from './UserSettings'; import { asUserEvent } from './UserEvent'; export const ANONYMOUS_USER = undefined; export const ANONYMOUS_USER_TOKEN = undefined; export const ANONYMOUS_USER_EXTERNAL_TOKEN = undefined; /** * Predefined colors for users */ const USER_COLORS = [ 'var(--jp-collaborator-color1)', 'var(--jp-collaborator-color2)', 'var(--jp-collaborator-color3)', 'var(--jp-collaborator-color4)', 'var(--jp-collaborator-color5)', 'var(--jp-collaborator-color6)', 'var(--jp-collaborator-color7)', ]; /** * Get a random color from the list of colors. */ export const getUserRandomColor = () => USER_COLORS[Math.floor(Math.random() * USER_COLORS.length)]; export class User { id; handle; email; firstName; lastName; initials; displayName; joinDate; roles; credits; creditsCustomerId; avatarUrl; origin; invites; iamProviders; settings; unsubscribedFromOutbounds; mfaUrl; onboarding; linkedContactId; events; constructor(u) { this.id = u.uid; this.handle = u.handle_s; this.email = u.email_s; this.firstName = u.first_name_t; this.lastName = u.last_name_t; this.initials = namesAsInitials(u.first_name_t, u.last_name_t); const displayName = asDisplayName(u.first_name_t, u.last_name_t); this.displayName = displayName === "" ? u.handle_s : displayName; this.avatarUrl = u.avatar_url_s; this.origin = u.origin_s; this.joinDate = u.join_ts_dt ? new Date(u.join_ts_dt) : undefined; this.credits = u.credits_i ? Number(u.credits_i) : 0; this.creditsCustomerId = u.credits_customer_uid; this.roles = u.roles_ss ?? []; let iamProviders = []; try { iamProviders = (u.iam_providers ?? []).map(iamProvider => asIAMProviderLinked(iamProvider)); } catch (e) { // no-op for backwards compatibility. } this.iamProviders = iamProviders; this.settings = new UserSettings(u.settings ?? {}); this.unsubscribedFromOutbounds = u.unsubscribed_from_outbounds_b ?? false; this.mfaUrl = u.mfa_url_s; this.onboarding = u.onboarding_s ? JSON.parse(u.onboarding_s) : BOOTSTRAP_USER_ONBOARDING; this.linkedContactId = u.linked_contact_uid; let events = u.events ?? []; if (!Array.isArray(events)) { events = [events]; } this.events = events.map(event => asUserEvent(event)); this.events.sort((x, y) => (x.eventDate < y.eventDate ? 1 : -1)); } setRoles(roles) { this.roles = roles; } } /** * Convert the raw user object to {@link IUser}. * * @param u Raw user object from DB * @returns IUser */ export function asUser(u) { return new User(u); }