diff --git a/encode.go b/encode.go index d15bf90..438d600 100644 --- a/encode.go +++ b/encode.go @@ -25,13 +25,6 @@ var encode = &cli.Command{ "relays":["wss://nada.zero"], "author":"ebb6ff85430705651b311ed51328767078fd790b14f02d22efba68d5513376bc" } | nak encode`, - Flags: []cli.Flag{ - &cli.StringSliceFlag{ - Name: "relay", - Aliases: []string{"r"}, - Usage: "attach relay hints to naddr code", - }, - }, DisableSliceFlagSeparator: true, Action: func(ctx context.Context, c *cli.Command) error { if c.Args().Len() != 0 { @@ -126,7 +119,12 @@ var encode = &cli.Command{ &cli.StringSliceFlag{ Name: "relay", Aliases: []string{"r"}, - Usage: "attach relay hints to nprofile code", + Usage: "attach relay hints to the code", + }, + &BoolIntFlag{ + Name: "outbox", + Usage: "automatically appends outbox relays to the code", + Value: 3, }, }, DisableSliceFlagSeparator: true, @@ -139,6 +137,13 @@ var encode = &cli.Command{ } relays := c.StringSlice("relay") + + if getBoolInt(c, "outbox") > 0 { + for _, r := range sys.FetchOutboxRelays(ctx, pk, int(getBoolInt(c, "outbox"))) { + relays = appendUnique(relays, r) + } + } + if err := normalizeAndValidateRelayURLs(relays); err != nil { return err } @@ -159,6 +164,16 @@ var encode = &cli.Command{ Aliases: []string{"a"}, Usage: "attach an author pubkey as a hint to the nevent code", }, + &cli.StringSliceFlag{ + Name: "relay", + Aliases: []string{"r"}, + Usage: "attach relay hints to the code", + }, + &BoolIntFlag{ + Name: "outbox", + Usage: "automatically appends outbox relays to the code", + Value: 3, + }, }, DisableSliceFlagSeparator: true, Action: func(ctx context.Context, c *cli.Command) error { @@ -171,6 +186,13 @@ var encode = &cli.Command{ author := getPubKey(c, "author") relays := c.StringSlice("relay") + + if getBoolInt(c, "outbox") > 0 && author != nostr.ZeroPK { + for _, r := range sys.FetchOutboxRelays(ctx, author, int(getBoolInt(c, "outbox"))) { + relays = appendUnique(relays, r) + } + } + if err := normalizeAndValidateRelayURLs(relays); err != nil { return err } @@ -204,6 +226,16 @@ var encode = &cli.Command{ Usage: "kind of referred replaceable event", Required: true, }, + &cli.StringSliceFlag{ + Name: "relay", + Aliases: []string{"r"}, + Usage: "attach relay hints to the code", + }, + &BoolIntFlag{ + Name: "outbox", + Usage: "automatically appends outbox relays to the code", + Value: 3, + }, }, DisableSliceFlagSeparator: true, Action: func(ctx context.Context, c *cli.Command) error { @@ -224,6 +256,13 @@ var encode = &cli.Command{ } relays := c.StringSlice("relay") + + if getBoolInt(c, "outbox") > 0 { + for _, r := range sys.FetchOutboxRelays(ctx, pubkey, int(getBoolInt(c, "outbox"))) { + relays = appendUnique(relays, r) + } + } + if err := normalizeAndValidateRelayURLs(relays); err != nil { return err } diff --git a/flags.go b/flags.go index 671e2d2..895118d 100644 --- a/flags.go +++ b/flags.go @@ -11,6 +11,62 @@ import ( "github.com/urfave/cli/v3" ) +type ( + BoolIntFlag = cli.FlagBase[int, struct{}, boolIntValue] +) + +type boolIntValue struct { + int int + defaultWhenSet int + hasDefault bool + hasBeenSet bool +} + +var _ cli.ValueCreator[int, struct{}] = boolIntValue{} + +func (t boolIntValue) Create(val int, p *int, c struct{}) cli.Value { + *p = val + + return &boolIntValue{ + defaultWhenSet: val, + hasDefault: true, + } +} + +func (t boolIntValue) IsBoolFlag() bool { + return true +} + +func (t boolIntValue) ToString(b int) string { return "<<>>" } + +func (t *boolIntValue) Set(value string) error { + t.hasBeenSet = true + if value == "true" { + if t.hasDefault { + t.int = t.defaultWhenSet + } else { + t.int = 1 + } + return nil + } else { + var err error + t.int, err = strconv.Atoi(value) + return err + } +} + +func (t *boolIntValue) String() string { return fmt.Sprintf("%#v", t.int) } +func (t *boolIntValue) Value() int { return t.int } +func (t *boolIntValue) Get() any { return t.int } + +func getBoolInt(cmd *cli.Command, name string) int { + return cmd.Value(name).(int) +} + +// +// +// + type NaturalTimeFlag = cli.FlagBase[nostr.Timestamp, struct{}, naturalTimeValue] type naturalTimeValue struct {