Skip to content

Commit

Permalink
feat: add user reacted notes retrieval
Browse files Browse the repository at this point in the history
Signed-off-by: r3drun3 <simone.ragonesi@sighup.io>
  • Loading branch information
R3DRUN3 committed Oct 6, 2023
1 parent 06e0bea commit c6e5a23
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
example_output.json
user_tagged_notes.json
user_reposted_notes.json
user_received_direct_messages.json
user_received_direct_messages.json
user_reacted_notes.json
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,14 @@ returned events saved to user_reposted_notes.json
</details>


<details>
<summary>Retrieve from the specified relay the last 300 reaction received by notes from the specified user</summary>

```console
nostro notes --userreacted npub1rusgp3upyrtpsy2pcqznl6e8hejg9ne8u2eg05gzc4n2cctsugksvcx2np nostr.wine
returned events saved to user_reacted_notes.json
```
</details>



55 changes: 55 additions & 0 deletions internal/commands/notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

var notesUserTagged bool
var notesUserReposted bool
var notesUserReacted bool

var NotesCmd = &cobra.Command{
Use: "notes",
Expand Down Expand Up @@ -126,6 +127,59 @@ var NotesCmd = &cobra.Command{
} else {
panic(err)
}
} else if notesUserReacted {
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.KindReaction)},
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_reacted_notes.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()
}
Expand All @@ -136,4 +190,5 @@ var NotesCmd = &cobra.Command{
func init() {
NotesCmd.Flags().BoolVarP(&notesUserTagged, "usertagged", "", false, "Retrieve from the specified relay the last 300 notes in which the specified user has been tagged.")
NotesCmd.Flags().BoolVarP(&notesUserReposted, "userreposted", "", false, "Retrieve from the specified relay the last 300 notes from the specified user that have been reposted.")
NotesCmd.Flags().BoolVarP(&notesUserReacted, "userreacted", "", false, "Retrieve from the specified relay the last 300 reaction received by notes from the specified user.")
}

0 comments on commit c6e5a23

Please sign in to comment.