fiatjaf / relay29

NIP-29 relay
MIT License
13 stars 4 forks source link

use Relay interface #6

Closed mattn closed 1 month ago

mattn commented 2 months ago

Remove dependencies to fiatjaf/khatru

mattn commented 2 months ago

This can be possible to use this relay29 on relayer.

fiatjaf commented 2 months ago

Would it work to move these functions to github.com/fiatjaf/relay29/core so they can be imported and used on relayer, while github.com/fiatjaf/relay29 would still have a khatru dependency?

mattn commented 2 months ago

Could you please explain more description?

fiatjaf commented 2 months ago

hmm, nevermind, what I was thinking doesn't work.

fiatjaf commented 2 months ago

But this PR also doesn't work, the khatru.GetAuthed() call is still being used and I don't know how to get rid of it.

mattn commented 1 month ago

You are right. We must separate the mechanism for GetAuthed from these functions.

mattn commented 1 month ago

Do you have any idea?

mattn commented 1 month ago

How about this ctx.Value(relay29.AuthKey).(string)

mattn commented 1 month ago

Or

ctx.Value(relay29.GetAuthedKey).(relay29.GetAutheder).GetAuthed(ctx)
mattn commented 1 month ago

This change make hkatru need to add small changes for relay29.

diff --git a/filter_policy.go b/filter_policy.go
index 1d9e5ef..dfb61a2 100644
--- a/filter_policy.go
+++ b/filter_policy.go
@@ -4,7 +4,6 @@ import (
    "context"
    "slices"

-   "github.com/fiatjaf/khatru"
    "github.com/nbd-wtf/go-nostr"
    "github.com/nbd-wtf/go-nostr/nip29"
 )
@@ -36,7 +35,7 @@ func (s *State) RequireKindAndSingleGroupIDOrSpecificEventReference(ctx context.
        }
    }

-   authed := khatru.GetAuthed(ctx)
+   authed := getAuthed(ctx)

    switch {
    case isNormal:
diff --git a/queries.go b/queries.go
index bf58ae4..6d5a642 100644
--- a/queries.go
+++ b/queries.go
@@ -3,7 +3,6 @@ package relay29
 import (
    "context"

-   "github.com/fiatjaf/khatru"
    "github.com/fiatjaf/set"
    "github.com/nbd-wtf/go-nostr"
    "github.com/nbd-wtf/go-nostr/nip29"
@@ -13,7 +12,7 @@ import (
 func (s *State) MetadataQueryHandler(ctx context.Context, filter nostr.Filter) (chan *nostr.Event, error) {
    ch := make(chan *nostr.Event, 1)

-   authed := khatru.GetAuthed(ctx)
+   authed := getAuthed(ctx)
    go func() {
        if slices.Contains(filter.Kinds, nostr.KindSimpleGroupMetadata) {
            if _, ok := filter.Tags["d"]; !ok {
@@ -56,7 +55,7 @@ func (s *State) MetadataQueryHandler(ctx context.Context, filter nostr.Filter) (
 func (s *State) AdminsQueryHandler(ctx context.Context, filter nostr.Filter) (chan *nostr.Event, error) {
    ch := make(chan *nostr.Event, 1)

-   authed := khatru.GetAuthed(ctx)
+   authed := getAuthed(ctx)
    go func() {
        if slices.Contains(filter.Kinds, nostr.KindSimpleGroupAdmins) {
            if _, ok := filter.Tags["d"]; !ok {
@@ -113,7 +112,7 @@ func (s *State) AdminsQueryHandler(ctx context.Context, filter nostr.Filter) (ch
 func (s *State) MembersQueryHandler(ctx context.Context, filter nostr.Filter) (chan *nostr.Event, error) {
    ch := make(chan *nostr.Event, 1)

-   authed := khatru.GetAuthed(ctx)
+   authed := getAuthed(ctx)
    go func() {
        if slices.Contains(filter.Kinds, nostr.KindSimpleGroupMembers) {
            if _, ok := filter.Tags["d"]; !ok {
@@ -174,7 +173,7 @@ func (s *State) NormalEventQuery(ctx context.Context, filter nostr.Filter) (chan
    }

    ch := make(chan *nostr.Event)
-   authed := khatru.GetAuthed(ctx)
+   authed := getAuthed(ctx)
    go func() {
        // now here in refE/refA/ids we have to check for each result if it is allowed
        var results chan *nostr.Event
diff --git a/state.go b/state.go
index 1e0d5a8..b9bf8ca 100644
--- a/state.go
+++ b/state.go
@@ -35,6 +35,26 @@ type Options struct {
    SecretKey string
 }

+const (
+   AuthedProviderKey = "nip29-authedprovider"
+)
+
+type AuthedProvider GetAuthed(context.Context) string
+
+func getAuthed(ctx context.Context) string {
+   v := ctx.Value(AuthedProviderKey)
+   if v != nil {
+       return ""
+   }
+   fn, ok := v.(AuthedProvider)
+   if !ok {
+       return ""
+   }
+   return fn(ctx)
+}
+
 func Init(opts Options) *State {
    pubkey, _ := nostr.GetPublicKey(opts.SecretKey)

and khatru

    ctx, cancel := context.WithCancel(
        context.WithValue(
            context.Background(),
            wsKey, ws,
            relay29.AuthProviderKey, GetAuthed,
        ),
    )
fiatjaf commented 1 month ago

Thank you. I will try it today.

fiatjaf commented 1 month ago

OK, I need something like this in order to make reuse this package to make a thing on top of strfry. Let me see what I can do.