mirror of
https://github.com/fiatjaf/khatru.git
synced 2025-09-17 04:04:09 +02:00
add relay.BroadcastEvent() and rename files.
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/nbd-wtf/go-nostr"
|
"github.com/nbd-wtf/go-nostr"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// AddEvent sends an event through then normal add pipeline, as if it was received from a websocket.
|
||||||
func (rl *Relay) AddEvent(ctx context.Context, evt *nostr.Event) error {
|
func (rl *Relay) AddEvent(ctx context.Context, evt *nostr.Event) error {
|
||||||
if evt == nil {
|
if evt == nil {
|
||||||
return errors.New("error: event is nil")
|
return errors.New("error: event is nil")
|
||||||
@@ -82,46 +83,3 @@ func (rl *Relay) AddEvent(ctx context.Context, evt *nostr.Event) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rl *Relay) handleDeleteRequest(ctx context.Context, evt *nostr.Event) error {
|
|
||||||
// event deletion -- nip09
|
|
||||||
for _, tag := range evt.Tags {
|
|
||||||
if len(tag) >= 2 && tag[0] == "e" {
|
|
||||||
// first we fetch the event
|
|
||||||
for _, query := range rl.QueryEvents {
|
|
||||||
ch, err := query(ctx, nostr.Filter{IDs: []string{tag[1]}})
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
target := <-ch
|
|
||||||
if target == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// got the event, now check if the user can delete it
|
|
||||||
acceptDeletion := target.PubKey == evt.PubKey
|
|
||||||
var msg string
|
|
||||||
if acceptDeletion == false {
|
|
||||||
msg = "you are not the author of this event"
|
|
||||||
}
|
|
||||||
// but if we have a function to overwrite this outcome, use that instead
|
|
||||||
for _, odo := range rl.OverwriteDeletionOutcome {
|
|
||||||
acceptDeletion, msg = odo(ctx, target, evt)
|
|
||||||
}
|
|
||||||
if acceptDeletion {
|
|
||||||
// delete it
|
|
||||||
for _, del := range rl.DeleteEvent {
|
|
||||||
del(ctx, target)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// fail and stop here
|
|
||||||
return fmt.Errorf("blocked: %s", msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
// don't try to query this same event again
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
11
broadcasting.go
Normal file
11
broadcasting.go
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package khatru
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/nbd-wtf/go-nostr"
|
||||||
|
)
|
||||||
|
|
||||||
|
// BroadcastEvent emits an event to all listeners whose filters' match, skipping all filters and actions
|
||||||
|
// it also doesn't attempt to store the event or trigger any reactions or callbacks
|
||||||
|
func (rl *Relay) BroadcastEvent(evt *nostr.Event) {
|
||||||
|
notifyListeners(evt)
|
||||||
|
}
|
51
deleting.go
Normal file
51
deleting.go
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
package khatru
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/nbd-wtf/go-nostr"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (rl *Relay) handleDeleteRequest(ctx context.Context, evt *nostr.Event) error {
|
||||||
|
// event deletion -- nip09
|
||||||
|
for _, tag := range evt.Tags {
|
||||||
|
if len(tag) >= 2 && tag[0] == "e" {
|
||||||
|
// first we fetch the event
|
||||||
|
for _, query := range rl.QueryEvents {
|
||||||
|
ch, err := query(ctx, nostr.Filter{IDs: []string{tag[1]}})
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
target := <-ch
|
||||||
|
if target == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// got the event, now check if the user can delete it
|
||||||
|
acceptDeletion := target.PubKey == evt.PubKey
|
||||||
|
var msg string
|
||||||
|
if acceptDeletion == false {
|
||||||
|
msg = "you are not the author of this event"
|
||||||
|
}
|
||||||
|
// but if we have a function to overwrite this outcome, use that instead
|
||||||
|
for _, odo := range rl.OverwriteDeletionOutcome {
|
||||||
|
acceptDeletion, msg = odo(ctx, target, evt)
|
||||||
|
}
|
||||||
|
if acceptDeletion {
|
||||||
|
// delete it
|
||||||
|
for _, del := range rl.DeleteEvent {
|
||||||
|
del(ctx, target)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// fail and stop here
|
||||||
|
return fmt.Errorf("blocked: %s", msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// don't try to query this same event again
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
Reference in New Issue
Block a user