Skip to content

Commit

Permalink
chore: code refactoring
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 7b97abf commit e1852de
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 123 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ For debugging you can create a `launch.json` file similar to the following (chan
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/main.go", // Update the path to main.go
"args": ["relay", "info", "bitcoinmaximalists.online"],
"program": "${workspaceFolder}/main.go",
"args": ["relay", "--info", "bitcoinmaximalists.online"],
"env": {},
"showLog": true
}
Expand All @@ -45,7 +45,7 @@ For debugging you can create a `launch.json` file similar to the following (chan
<summary>Retrieve relay info</summary>

```console
nostro relay info relay.nostrview.com
nostro relay --info relay.nostrview.com
####################### RELAY INFO #######################
NAME: relay.nostrview.com
DESCRIPTION: Nostrview relay
Expand All @@ -64,7 +64,7 @@ PAYMENTSURL: https://relay.nostrview.com/invoices
<summary>Retrieve from the specified relay the last 30 notes in which the specified user has been tagged</summary>

```console
nostro notes usertagged npub1rusgp3upyrtpsy2pcqznl6e8hejg9ne8u2eg05gzc4n2cctsugksvcx2np nostr.wine
nostro notes --usertagged npub1rusgp3upyrtpsy2pcqznl6e8hejg9ne8u2eg05gzc4n2cctsugksvcx2np nostr.wine
returned events saved to user_tagged_notes.json
```
</details>
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
var rootCmd = &cobra.Command{
Use: "nostro",
Short: "Nostr OSINT.",
Long: `NostrO enables you to do Open Source Intelligence on the Nostr protocol.`,
Long: `Welcome to NostrO 🔎 𓅦`,
Run: func(cmd *cobra.Command, args []string) {
// Display a message indicating that a subcommand is required
fmt.Println("################### Welcome to NostrO 🔎 𓅦 ###################")
Expand Down
76 changes: 75 additions & 1 deletion internal/commands/notes.go
Original file line number Diff line number Diff line change
@@ -1,10 +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 notesUserTagged bool

var NotesCmd = &cobra.Command{
Use: "notes",
Short: "Notes commands",
Short: "Operations on notes",
Long: `Search and retrieve notes on nostr`,
RunE: func(cmd *cobra.Command, args []string) error {
if notesUserTagged {
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{nostr.KindTextNote},
Tags: t,
Limit: 30,
}}
} 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_tagged_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()
}
return nil
},
}

func init() {
NotesCmd.Flags().BoolVarP(&notesUserTagged, "usertagged", "", false, "Retrieve from the specified relay all the notes in which the specified user has been tagged.")
}
39 changes: 38 additions & 1 deletion internal/commands/relay.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,47 @@
package commands

import (
"context"
"fmt"

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

var relayInfo bool

var RelayCmd = &cobra.Command{
Use: "relay",
Short: "Relay commands",
Short: "Operations on relays",
Long: `Retrieve data on nostr relays`,
RunE: func(cmd *cobra.Command, args []string) error {
if relayInfo {
if len(args) != 1 {
return fmt.Errorf("relay name is required")
}
relay := args[0]
data, err := nip11.Fetch(context.Background(), relay)
if err != nil {
return err
}
fmt.Println("####################### RELAY INFO #######################")
fmt.Println("NAME: ", data.Name)
fmt.Println("DESCRIPTION: ", data.Description)
fmt.Println("PUB KEY: ", data.PubKey)
fmt.Println("CONTACT: ", data.Contact)
fmt.Println("SUPPORTED NIPS: ", data.SupportedNIPs)
fmt.Println("SOFTWARE: ", data.Software)
fmt.Println("VERSION: ", data.Version)
fmt.Println("LIMITATION: ", data.Limitation)
fmt.Println("PAYMENTSURL: ", data.PaymentsURL)
fmt.Println("##########################################################")
} else {
cmd.Help()
}
return nil
},
}

func init() {
RelayCmd.Flags().BoolVarP(&relayInfo, "info", "", false, "Retrieve relay information document (nip-11)")
}
39 changes: 0 additions & 39 deletions internal/commands/relayinfo.go

This file was deleted.

77 changes: 0 additions & 77 deletions internal/commands/usertaggednotes.go

This file was deleted.

0 comments on commit e1852de

Please sign in to comment.