|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "fiatjaf.com/nostr/nip19" |
| 7 | + "github.com/fatih/color" |
| 8 | + "github.com/urfave/cli/v3" |
| 9 | +) |
| 10 | + |
| 11 | +var profile = &cli.Command{ |
| 12 | + Name: "profile", |
| 13 | + Usage: "displays profile information for a given pubkey", |
| 14 | + Description: `fetches and displays profile metadata, relays, and contact count for a given pubkey. |
| 15 | +
|
| 16 | +example usage: |
| 17 | + nak profile npub1h8spmtw9m2huyv6v2j2qd5zv956z2zdugl6mgx02f2upffwpm3nqv0j4ps |
| 18 | + nak profile user@example.com`, |
| 19 | + ArgsUsage: "[pubkey]", |
| 20 | + Action: func(ctx context.Context, c *cli.Command) error { |
| 21 | + for pubkeyInput := range getStdinLinesOrArguments(c.Args()) { |
| 22 | + pk, err := parsePubKey(pubkeyInput) |
| 23 | + if err != nil { |
| 24 | + ctx = lineProcessingError(ctx, "invalid pubkey '%s': %s", pubkeyInput, err) |
| 25 | + continue |
| 26 | + } |
| 27 | + |
| 28 | + pm := sys.FetchProfileMetadata(ctx, pk) |
| 29 | + |
| 30 | + npub := nip19.EncodeNpub(pk) |
| 31 | + stdout(colors.bold("pubkey (hex):"), pk.Hex()) |
| 32 | + stdout(colors.bold("npub:"), color.HiCyanString(npub)) |
| 33 | + |
| 34 | + relayList := sys.FetchRelayList(ctx, pk) |
| 35 | + writeRelays := make([]string, 0, 3) |
| 36 | + for _, rl := range relayList.Items { |
| 37 | + if rl.Outbox { |
| 38 | + writeRelays = append(writeRelays, rl.URL) |
| 39 | + if len(writeRelays) == 3 { |
| 40 | + break |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + if len(writeRelays) > 0 { |
| 45 | + nprofile := nip19.EncodeNprofile(pk, writeRelays) |
| 46 | + stdout(colors.bold("profile uri:"), color.HiCyanString("nostr:"+nprofile)) |
| 47 | + } |
| 48 | + |
| 49 | + if pm.Name != "" { |
| 50 | + stdout(colors.bold("name:"), color.HiBlueString(pm.Name)) |
| 51 | + } |
| 52 | + if pm.DisplayName != "" { |
| 53 | + stdout(colors.bold("display_name:"), color.HiBlueString(pm.DisplayName)) |
| 54 | + } |
| 55 | + if pm.About != "" { |
| 56 | + stdout(colors.bold("about:"), color.HiBlueString(pm.About)) |
| 57 | + } |
| 58 | + if pm.Picture != "" { |
| 59 | + stdout(colors.bold("picture:"), color.HiBlueString(pm.Picture)) |
| 60 | + } |
| 61 | + if pm.Banner != "" { |
| 62 | + stdout(colors.bold("banner:"), color.HiBlueString(pm.Banner)) |
| 63 | + } |
| 64 | + if pm.Website != "" { |
| 65 | + stdout(colors.bold("website:"), color.HiBlueString(pm.Website)) |
| 66 | + } |
| 67 | + if pm.NIP05 != "" { |
| 68 | + isValid := pm.NIP05Valid(ctx) |
| 69 | + if isValid { |
| 70 | + stdout(colors.bold("nip05:"), color.HiGreenString(pm.NIP05), color.HiGreenString("(verified)")) |
| 71 | + } else { |
| 72 | + stdout(colors.bold("nip05:"), color.HiRedString(pm.NIP05), color.HiRedString("(not verified)")) |
| 73 | + } |
| 74 | + } |
| 75 | + if pm.LUD16 != "" { |
| 76 | + stdout(colors.bold("lud16:"), color.HiBlueString(pm.LUD16)) |
| 77 | + } |
| 78 | + |
| 79 | + if len(relayList.Items) > 0 { |
| 80 | + stdout(colors.bold("relays:")) |
| 81 | + for _, relay := range relayList.Items { |
| 82 | + access := "" |
| 83 | + if relay.Inbox && relay.Outbox { |
| 84 | + access = "read/write" |
| 85 | + } else if relay.Inbox { |
| 86 | + access = "read" |
| 87 | + } else if relay.Outbox { |
| 88 | + access = "write" |
| 89 | + } |
| 90 | + stdout(" ", color.HiBlueString(relay.URL), color.HiCyanString("(%s)", access)) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + followList := sys.FetchFollowList(ctx, pk) |
| 95 | + contactCount := len(followList.Items) |
| 96 | + stdout(colors.bold("follows:"), color.HiCyanString("%d", contactCount)) |
| 97 | + } |
| 98 | + |
| 99 | + exitIfLineProcessingError(ctx) |
| 100 | + return nil |
| 101 | + }, |
| 102 | +} |
0 commit comments