Skip to content

Commit

Permalink
feat: add user info 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 100524a commit b3f8acd
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ user_written_notes.json
user_tagged_notes.json
user_reposted_notes.json
user_received_direct_messages.json
user_reacted_notes.json
user_reacted_notes.json
user_data.json
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Available Commands:
help Help about any command
notes Operations on notes
relay Operations on relays
user Operations on users

Flags:
-h, --help help for nostro
Expand Down Expand Up @@ -101,6 +102,33 @@ PAYMENTSURL: https://relay.nostrview.com/invoices
</details>



<details>
<summary>Retrieve user info from the specified relay</summary>

```console
nostro user --info npub1rusgp3upyrtpsy2pcqznl6e8hejg9ne8u2eg05gzc4n2cctsugksvcx2np nos.lol
####################### USER INFO #######################
[[i twitter:AIXI282589933 1708101078024343815] [i github:R3DRUN3 0f954e6fada304dacdb8e7389eefaf2b]]
Name: r3drun3
Picture: https://i.postimg.cc/rwTgJm0G/symbol-1.gif
Username: r3drun3
Display Name: r3drun3
Banner: https://i.postimg.cc/90FYS0D7/1327483.png
Website:
About: Just a collection of quantum bits,
constantly phasing between cyberspace and meatspace.
Nip05: r3drun3@Nostr-Check.com
Lud16: smallfiction78@walletofsatoshi.com
Lud06:
Created At: 1689593935
Nip05 Valid: false
##########################################################
```
</details>



<details>
<summary>Retrieve from the specified relay the last 300 direct messages that the specified user received</summary>

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

func Execute() {
Expand Down
118 changes: 118 additions & 0 deletions internal/commands/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
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 User struct {
Name string `json:"name"`
Picture string `json:"picture"`
Username string `json:"username"`
DisplayName string `json:"display_name"`
Banner string `json:"banner"`
Website string `json:"website"`
About string `json:"about"`
Nip05 string `json:"nip05"`
Lud16 string `json:"lud16"`
Lud06 string `json:"lud06"`
CreatedAt int64 `json:"created_at"`
Nip05Valid bool `json:"nip05valid"`
}

var userData bool

var UserCmd = &cobra.Command{
Use: "user",
Short: "Operations on users",
Long: `Search and retrieve users info`,
RunE: func(cmd *cobra.Command, args []string) error {
if userData {
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.KindSetMetadata)},
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)
}
fmt.Println("####################### USER INFO #######################")
fmt.Println(evs[0].Tags)
var user User
if err := json.Unmarshal([]byte(evs[0].Content), &user); err != nil {
panic("Error decoding user JSON")
}
fmt.Println("Name:", user.Name)
fmt.Println("Picture:", user.Picture)
fmt.Println("Username:", user.Username)
fmt.Println("Display Name:", user.DisplayName)
fmt.Println("Banner:", user.Banner)
fmt.Println("Website:", user.Website)
fmt.Println("About:", user.About)
fmt.Println("Nip05:", user.Nip05)
fmt.Println("Lud16:", user.Lud16)
fmt.Println("Lud06:", user.Lud06)
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 {
cmd.Help()
}
return nil
},
}

func init() {
UserCmd.Flags().BoolVarP(&userData, "info", "", false, "Retrieve user info from the specified relay.")
}

0 comments on commit b3f8acd

Please sign in to comment.