From 06e0bea84f4542cb6c8202d3160f2b3318097b1b Mon Sep 17 00:00:00 2001 From: r3drun3 Date: Fri, 6 Oct 2023 10:26:36 +0200 Subject: [PATCH] feat: add user received direct messages retrieval Signed-off-by: r3drun3 --- .gitignore | 3 +- README.md | 14 +++++++ cmd/root.go | 1 + internal/commands/dm.go | 84 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 internal/commands/dm.go diff --git a/.gitignore b/.gitignore index 62fd8e1..5ec27ad 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ #dev example_output.json user_tagged_notes.json -user_reposted_notes.json \ No newline at end of file +user_reposted_notes.json +user_received_direct_messages.json \ No newline at end of file diff --git a/README.md b/README.md index c8476ed..a0b0da5 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,7 @@ Usage: Available Commands: completion Generate the autocompletion script for the specified shell + dm Operations on direct messages help Help about any command notes Operations on notes relay Operations on relays @@ -99,6 +100,19 @@ PAYMENTSURL: https://relay.nostrview.com/invoices ``` + +
+ Retrieve from the specified relay the last 300 direct messages that the specified user received + +```console +nostro dm --userreceived npub1rusgp3upyrtpsy2pcqznl6e8hejg9ne8u2eg05gzc4n2cctsugksvcx2np nostr.wine +returned events saved to user_received_direct_messages.json +``` +
+ + + +
Retrieve from the specified relay the last 300 notes in which the specified user has been tagged diff --git a/cmd/root.go b/cmd/root.go index 1bb21a2..0dac739 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -22,6 +22,7 @@ var rootCmd = &cobra.Command{ func init() { rootCmd.AddCommand(commands.RelayCmd) rootCmd.AddCommand(commands.NotesCmd) + rootCmd.AddCommand(commands.DirectMessagesCmd) } func Execute() { diff --git a/internal/commands/dm.go b/internal/commands/dm.go new file mode 100644 index 0000000..e1870e2 --- /dev/null +++ b/internal/commands/dm.go @@ -0,0 +1,84 @@ +package commands + +import ( + "context" + "encoding/json" + "fmt" + "os" + "time" + + "github.com/nbd-wtf/go-nostr" + "github.com/nbd-wtf/go-nostr/nip19" + "github.com/spf13/cobra" +) + +var userReceivedDm bool + +var DirectMessagesCmd = &cobra.Command{ + Use: "dm", + Short: "Operations on direct messages", + Long: `Search and retrieve direct messages on nostr`, + RunE: func(cmd *cobra.Command, args []string) error { + if userReceivedDm { + if len(args) != 2 { + return fmt.Errorf("user npbu key and relay name are required") + } + npub := args[0] + url := args[1] + // connect to relay + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + relay, err := nostr.RelayConnect(ctx, url) + if err != nil { + panic(err) + } + // create filters + var filters nostr.Filters + if _, v, err := nip19.Decode(npub); err == nil { + t := make(map[string][]string) + // making a "p" tag for the above public key. + // this filters for messages tagged with the user, mainly replies. + t["p"] = []string{v.(string)} + filters = []nostr.Filter{{ + Kinds: []int{int(nostr.KindEncryptedDirectMessage)}, + Tags: t, + Limit: 300, + }} + } else { + panic("not a valid npub!") + } + // create a subscription and submit to relay + // results will be returned on the sub.Events channel + sub, _ := relay.Subscribe(ctx, filters) + + // we will append the returned events to this slice + evs := make([]nostr.Event, 0) + + go func() { + <-sub.EndOfStoredEvents + cancel() + }() + for ev := range sub.Events { + evs = append(evs, *ev) + } + + filename := "user_received_direct_messages.json" + if f, err := os.Create(filename); err == nil { + fmt.Fprintf(os.Stderr, "returned events saved to %s\n", filename) + // encode the returned events in a file + enc := json.NewEncoder(f) + enc.SetIndent("", " ") + enc.Encode(evs) + f.Close() + } else { + panic(err) + } + } else { + cmd.Help() + } + return nil + }, +} + +func init() { + DirectMessagesCmd.Flags().BoolVarP(&userReceivedDm, "userreceived", "", false, "Retrieve from the specified relay the last 300 direct messages that the specified user received.") +}