Skip to content

Commit

Permalink
feat: add specific event 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 7b2bd75 commit 972ee34
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 12 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Usage:
Available Commands:
completion Generate the autocompletion script for the specified shell
dm Operations on direct messages
event Operations on events
help Help about any command
notes Operations on notes
relay Operations on relays
Expand Down Expand Up @@ -128,6 +129,32 @@ Nip05 Valid: false
</details>


<details>
<summary>Retrieve the specified event from the specified relay</summary>

```console
nostro event --info note1se5g5crjxaaet4vzy3xtpurv4as3dsfd5dteglk4z3f2xafstl5qyry4m3 nos.lol

####################### EVENT INFO #######################
ID: 86688a6072377b95d582244cb0f06caf6116c12da357947ed51452a375305fe8
PubKey: 1f2080c78120d6181141c0053feb27be6482cf27e2b287d102c566ac6170e22d
Kind: 1
Created At: 1696529147
Tags: [[t osint] [t osint] [t Nostr] [t nostr] [t github] [t github] [r https://github.com/r3drun3/nostro] [r https://image.nostr.build/7a83f1b7b006bdecd047731c6b0fcec54d1a5186ae222f3e98e15953850712f4.jpg]]
Content: I believe that one of the best ways to learn a technology is to experiment and build upon it. That's why I've started developing a tool for performing #osint operations on #Nostr on my #github.
Feel free to collaborate if you want ☺
https://github.com/r3drun3/nostro


https://image.nostr.build/7a83f1b7b006bdecd047731c6b0fcec54d1a5186ae222f3e98e15953850712f4.jpg
Signature: 9b3b4af0bac8df5f62dd54b8f5be34bdee7545e0a6453fe6e3462861d29390282e95a4e85f8d2bf801d1f0da3ccc955b3ecff0ffbc6786ffa7d1c7017650b34a
##########################################################
```
</details>




<details>
<summary>Retrieve the user contact list from the specified relay</summary>

Expand Down Expand Up @@ -186,5 +213,7 @@ returned events saved to user_reacted_notes.json
```
</details>

<br/>

For all available command use the cli `help` function.

1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func init() {
rootCmd.AddCommand(commands.NotesCmd)
rootCmd.AddCommand(commands.DirectMessagesCmd)
rootCmd.AddCommand(commands.UserCmd)
rootCmd.AddCommand(commands.EventsCmd)
}

func Execute() {
Expand Down
95 changes: 95 additions & 0 deletions internal/commands/event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package commands

import (
"context"
"encoding/json"
"fmt"
"time"

"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip19"
"github.com/spf13/cobra"
)

type Event struct {
ID string `json:"id"`
PubKey string `json:"pubkey"`
Content string `json:"content"`
Kind int `json:"kind"`
Tags [][]string `json:"tags"`
Sig string `json:"sig"`
CreatedAt int64 `json:"created_at"`
}

var eventData bool

var EventsCmd = &cobra.Command{
Use: "event",
Short: "Operations on events",
Long: `Search and retrieve events on nostr`,
RunE: func(cmd *cobra.Command, args []string) error {
if eventData {
if len(args) != 2 {
return fmt.Errorf("user npbu key and relay name are required")
}
id := 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(id); err == nil {
filters = []nostr.Filter{{
IDs: []string{v.(string)},
}}
} 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)
}

fmt.Println("####################### EVENT INFO #######################")
//fmt.Println(evs[0].Tags)
var event Event
eventBytes, err := json.Marshal(evs[0])
if err != nil {
// Handle the error
panic("Error marshaling event")
}
if err := json.Unmarshal(eventBytes, &event); err != nil {
panic("Error decoding user JSON")
}
fmt.Println("ID:", event.ID)
fmt.Println("PubKey:", event.PubKey)
fmt.Println("Kind:", event.Kind)
fmt.Println("Created At:", event.CreatedAt)
fmt.Println("Tags:", event.Tags)
fmt.Println("Content:", event.Content)
fmt.Println("Signature:", event.Sig)
fmt.Println("##########################################################")
} else {
cmd.Help()
}
return nil
},
}

func init() {
EventsCmd.Flags().BoolVarP(&eventData, "info", "", false, "Retrieve the specified event body from the specified relay.")
}
12 changes: 0 additions & 12 deletions internal/commands/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,6 @@ var UserCmd = &cobra.Command{
fmt.Println("Created At:", user.CreatedAt)
fmt.Println("Nip05 Valid:", user.Nip05Valid)
fmt.Println("##########################################################")
// Uncomment to save user info into file
// filename := "user_data.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 if userContactList {
if len(args) != 2 {
return fmt.Errorf("user npbu key and relay name are required")
Expand Down

0 comments on commit 972ee34

Please sign in to comment.