mirror of
https://github.com/believethehype/nostdress.git
synced 2026-06-14 13:13:35 -07:00
Store Nostr Profile Picture in DB, get it only on Profile update when GET_NOSTR_PROFILE is true
This commit is contained in:
@@ -31,6 +31,11 @@ type Params struct {
|
||||
NotifyZaps bool `json:"notifyzaps"`
|
||||
NotifyZapComment bool `json:"notifycomments"`
|
||||
NotifyNonZap bool `json:"notifynonzaps"`
|
||||
Image struct {
|
||||
DataURI string
|
||||
Bytes []byte
|
||||
Ext string
|
||||
}
|
||||
}
|
||||
|
||||
func SaveName(
|
||||
@@ -44,6 +49,17 @@ func SaveName(
|
||||
name = strings.ToLower(name)
|
||||
domain = strings.ToLower(domain)
|
||||
|
||||
if params.Npub != "" && s.GetNostrProfile {
|
||||
NostrProfile, err := GetNostrProfileMetaData(params.Npub)
|
||||
if err == nil {
|
||||
err = addImageToProfile(params, NostrProfile.Picture)
|
||||
if err != nil {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
key := []byte(getID(name, domain))
|
||||
|
||||
pin = ComputePIN(name, domain)
|
||||
|
||||
+14
-11
@@ -29,25 +29,26 @@ func metaData(params *Params) lnurl.Metadata {
|
||||
LightningAddress: fmt.Sprintf("%s@%s", params.Name, params.Domain),
|
||||
}
|
||||
|
||||
/* if params.Npub != "" && s.GetNostrProfile {
|
||||
NostrProfile, err := GetNostrProfileMetaData(params.Npub)
|
||||
if err == nil {
|
||||
addImageToMetaData(&metadata, NostrProfile.Picture)
|
||||
if params.Npub != "" && s.GetNostrProfile {
|
||||
if params.Image.DataURI != "" {
|
||||
metadata.Image.Bytes = params.Image.Bytes
|
||||
metadata.Image.Ext = params.Image.Ext
|
||||
metadata.Image.DataURI = params.Image.DataURI
|
||||
}
|
||||
|
||||
} */
|
||||
}
|
||||
|
||||
return metadata
|
||||
|
||||
}
|
||||
|
||||
// addImageToMetaData adds an image to the LNURL metadata
|
||||
func addImageToMetaData(metadata *lnurl.Metadata, imageurl string) {
|
||||
func addImageToProfile(params *Params, imageurl string) (err error) {
|
||||
// Download and resize profile picture
|
||||
picture, err := DownloadProfilePicture(imageurl)
|
||||
if err != nil {
|
||||
log.Debug().Str("Downloading profile picture", err.Error()).Msg("Error")
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
// Determine image format
|
||||
@@ -59,13 +60,15 @@ func addImageToMetaData(metadata *lnurl.Metadata, imageurl string) {
|
||||
ext = "png"
|
||||
} else {
|
||||
log.Debug().Str("Detecting image format", "unknown format").Msg("Error")
|
||||
return
|
||||
return fmt.Errorf("Detecting image format: unknown format")
|
||||
}
|
||||
|
||||
// Set image metadata in LNURL metadata
|
||||
metadata.Image.Ext = ext
|
||||
metadata.Image.DataURI = "data:" + contentType + ";base64," + base64.StdEncoding.EncodeToString(picture)
|
||||
metadata.Image.Bytes = picture
|
||||
params.Image.Ext = ext
|
||||
params.Image.DataURI = "data:" + contentType + ";base64," + base64.StdEncoding.EncodeToString(picture)
|
||||
params.Image.Bytes = picture
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func DownloadProfilePicture(url string) ([]byte, error) {
|
||||
|
||||
@@ -150,10 +150,13 @@ func GetNostrProfileMetaData(npub string) (nostr.ProfileMetadata, error) {
|
||||
ctx, _ := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
|
||||
var metadata *nostr.ProfileMetadata
|
||||
// connect to any relay
|
||||
url := "wss://relay.damus.io"
|
||||
// connect to first relay, todo, check on all/for errors
|
||||
rel := Relays[0]
|
||||
log.Printf("Get Image from: %s . If you receive an error use another relay on first position in RELAYS option", rel)
|
||||
url := rel
|
||||
relay, err := nostr.RelayConnect(ctx, url)
|
||||
if err != nil {
|
||||
log.Printf("Could not get Image")
|
||||
return *metadata, err
|
||||
}
|
||||
|
||||
@@ -191,6 +194,7 @@ func GetNostrProfileMetaData(npub string) (nostr.ProfileMetadata, error) {
|
||||
} else {
|
||||
err = fmt.Errorf("no profile found for npub %s on relay %s", npub, url)
|
||||
}
|
||||
log.Printf("Success getting Nostr Profile")
|
||||
return *metadata, err
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user