aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/messaging.ts
blob: a647a6578caba41c4d3c4d0655bb0245bf27e1ee (plain) (blame)
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
/**
 * Messaging facade — MLS groups only.
 *
 * Every conversation is an MLS group (1:1 DMs are 2-person groups).
 */

export {
  publishKeyPackage,
  fetchKeyPackage,
  searchMlsUsers,
  preloadMlsUsers,
  getGroups,
  getGroupRumours,
  sendGroupMessage,
  sendGroupMediaMessage,
  findOrCreateDmGroup,
  subscribeToMessages,
  fetchGroupHistory,
  decryptMediaUrl,
  isConnected,
  onConnectionChange,
  catchUpMessages,
} from "$lib/mls-relay";

export type { GroupInfo } from "$lib/mls-relay";

import { getNostrUserInfo } from "$lib/nip01";
import { isValid } from "nostr-tools/nip05";

// ---------------------------------------------------------------------------
// User info cache (shared across pages)
// ---------------------------------------------------------------------------

const userInfoCache = new Map<string, any>();

export function getCachedUserInfo(pubkey: string): any | null {
  return userInfoCache.get(pubkey) ?? null;
}

export function setCachedUserInfo(pubkey: string, info: any): void {
  userInfoCache.set(pubkey, info);
}

/** Resolve a pubkey to user info (coinos user, nostr profile, or anonymous). */
export async function resolveUser(pubkey: string): Promise<any> {
  const cached = userInfoCache.get(pubkey);
  if (cached) return cached;

  const nostrUserInfo = (await getNostrUserInfo(pubkey)) || {};
  const valid = "nip05" in nostrUserInfo && (await isValid(pubkey, nostrUserInfo.nip05));

  let coinosUsername = null;
  let picture = nostrUserInfo?.picture || null;
  try {
    const response = await fetch(`/api/users/${pubkey}`);
    if (response.ok) {
      const info = await response.json();
      if (!info.anon) {
        coinosUsername = info.username || null;
        picture = info.picture || picture;
      }
    }
  } catch {}

  const info = {
    coinosUsername,
    picture,
    nostrName: nostrUserInfo?.name || null,
    nip05: nostrUserInfo?.nip05 || null,
    nip05Valid: valid,
    pubkey,
  };
  userInfoCache.set(pubkey, info);
  return info;
}

/** Get a display name for a user info object. */
export function displayName(info: any): string {
  return info?.coinosUsername || info?.nostrName || "Anonymous";
}