Skip to content

Commit

Permalink
feat: add user written 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 c6e5a23 commit 100524a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#dev
example_output.json
user_written_notes.json
user_tagged_notes.json
user_reposted_notes.json
user_received_direct_messages.json
Expand Down
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,26 @@ PAYMENTSURL: https://relay.nostrview.com/invoices
<summary>Retrieve from the specified relay the last 300 direct messages that the specified user received</summary>

```console
nostro dm --userreceived npub1rusgp3upyrtpsy2pcqznl6e8hejg9ne8u2eg05gzc4n2cctsugksvcx2np nostr.wine
nostro dm --userreceived npub1rusgp3upyrtpsy2pcqznl6e8hejg9ne8u2eg05gzc4n2cctsugksvcx2np nos.lol
returned events saved to user_received_direct_messages.json
```
</details>

<details>
<summary>Retrieve from the specified relay the last 300 notes that the specified user wrote</summary>

```console
nostro notes --userwritten npub1rusgp3upyrtpsy2pcqznl6e8hejg9ne8u2eg05gzc4n2cctsugksvcx2np nos.lol
returned events saved to user_written_notes.json
```
</details>


<details>
<summary>Retrieve from the specified relay the last 300 notes in which the specified user has been tagged</summary>

```console
nostro notes --usertagged npub1rusgp3upyrtpsy2pcqznl6e8hejg9ne8u2eg05gzc4n2cctsugksvcx2np nostr.wine
nostro notes --usertagged npub1rusgp3upyrtpsy2pcqznl6e8hejg9ne8u2eg05gzc4n2cctsugksvcx2np nos.lol
returned events saved to user_tagged_notes.json
```
</details>
Expand All @@ -126,7 +133,7 @@ returned events saved to user_tagged_notes.json
<summary>Retrieve from the specified relay the last 300 notes from the specified user that have been reposted</summary>

```console
nostro notes --userreposted npub1rusgp3upyrtpsy2pcqznl6e8hejg9ne8u2eg05gzc4n2cctsugksvcx2np nostr.wine
nostro notes --userreposted npub1rusgp3upyrtpsy2pcqznl6e8hejg9ne8u2eg05gzc4n2cctsugksvcx2np nos.lol
returned events saved to user_reposted_notes.json
```
</details>
Expand All @@ -136,7 +143,7 @@ returned events saved to user_reposted_notes.json
<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
nostro notes --userreacted npub1rusgp3upyrtpsy2pcqznl6e8hejg9ne8u2eg05gzc4n2cctsugksvcx2np nos.lol
returned events saved to user_reacted_notes.json
```
</details>
Expand Down
56 changes: 56 additions & 0 deletions internal/commands/notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
var notesUserTagged bool
var notesUserReposted bool
var notesUserReacted bool
var notesUserWritten bool

var NotesCmd = &cobra.Command{
Use: "notes",
Expand Down Expand Up @@ -180,6 +181,60 @@ var NotesCmd = &cobra.Command{
} else {
panic(err)
}
} else if notesUserWritten {
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.KindTextNote)},
Authors: []string{v.(string)},
//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_written_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 @@ -191,4 +246,5 @@ 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.")
NotesCmd.Flags().BoolVarP(&notesUserWritten, "userwritten", "", false, "Retrieve from the specified relay the last 300 notes written by the specified user.")
}

0 comments on commit 100524a

Please sign in to comment.