khatru: OnEventDeleted hook.
This commit is contained in:
@@ -78,6 +78,9 @@ func (rl *Relay) handleDeleteRequest(ctx context.Context, evt nostr.Event) error
|
|||||||
}
|
}
|
||||||
|
|
||||||
haveDeletedSomething = true
|
haveDeletedSomething = true
|
||||||
|
if rl.OnEventDeleted != nil {
|
||||||
|
rl.OnEventDeleted(ctx, target)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+15
-3
@@ -39,9 +39,15 @@ type expirationManager struct {
|
|||||||
events expiringEventHeap
|
events expiringEventHeap
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
|
|
||||||
|
// a function to query the relay database, generally the same as relay.queryStored
|
||||||
queryStored func(ctx context.Context, filter nostr.Filter) iter.Seq[nostr.Event]
|
queryStored func(ctx context.Context, filter nostr.Filter) iter.Seq[nostr.Event]
|
||||||
|
|
||||||
|
// a function to delete an event from the relay database, generally the same as relay.DeleteEvent
|
||||||
deleteEvent func(ctx context.Context, id nostr.ID) error
|
deleteEvent func(ctx context.Context, id nostr.ID) error
|
||||||
|
|
||||||
|
// a function to call after an event has been deleted, generally the same as relay.OnEventDeleted
|
||||||
|
deleteCallback func(ctx context.Context, id nostr.Event)
|
||||||
|
|
||||||
interval time.Duration
|
interval time.Duration
|
||||||
initialScanDone bool
|
initialScanDone bool
|
||||||
kill chan struct{} // used for manually killing this
|
kill chan struct{} // used for manually killing this
|
||||||
@@ -109,7 +115,11 @@ func (em *expirationManager) checkExpiredEvents(ctx context.Context) {
|
|||||||
heap.Pop(&em.events)
|
heap.Pop(&em.events)
|
||||||
|
|
||||||
ctx := context.WithValue(ctx, internalCallKey, struct{}{})
|
ctx := context.WithValue(ctx, internalCallKey, struct{}{})
|
||||||
em.deleteEvent(ctx, next.id)
|
if nil == em.deleteEvent(ctx, next.id) && em.deleteCallback != nil {
|
||||||
|
for evt := range em.queryStored(ctx, nostr.Filter{IDs: []nostr.ID{next.id}}) {
|
||||||
|
em.deleteCallback(ctx, evt)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,12 +152,14 @@ func (em *expirationManager) removeEvent(id nostr.ID) {
|
|||||||
func (rl *Relay) StartExpirationManager(
|
func (rl *Relay) StartExpirationManager(
|
||||||
queryStored func(ctx context.Context, filter nostr.Filter) iter.Seq[nostr.Event],
|
queryStored func(ctx context.Context, filter nostr.Filter) iter.Seq[nostr.Event],
|
||||||
deleteEvent func(ctx context.Context, id nostr.ID) error,
|
deleteEvent func(ctx context.Context, id nostr.ID) error,
|
||||||
|
onDeleteCallback func(ctx context.Context, evt nostr.Event),
|
||||||
) {
|
) {
|
||||||
rl.expirationManager = &expirationManager{
|
rl.expirationManager = &expirationManager{
|
||||||
events: make(expiringEventHeap, 0),
|
events: make(expiringEventHeap, 0),
|
||||||
|
|
||||||
queryStored: queryStored,
|
queryStored: queryStored,
|
||||||
deleteEvent: deleteEvent,
|
deleteEvent: deleteEvent,
|
||||||
|
deleteCallback: onDeleteCallback,
|
||||||
|
|
||||||
interval: time.Hour,
|
interval: time.Hour,
|
||||||
kill: make(chan struct{}),
|
kill: make(chan struct{}),
|
||||||
|
|||||||
+10
-1
@@ -69,6 +69,7 @@ type Relay struct {
|
|||||||
ReplaceEvent func(ctx context.Context, event nostr.Event) error
|
ReplaceEvent func(ctx context.Context, event nostr.Event) error
|
||||||
DeleteEvent func(ctx context.Context, id nostr.ID) error
|
DeleteEvent func(ctx context.Context, id nostr.ID) error
|
||||||
OnEventSaved func(ctx context.Context, event nostr.Event)
|
OnEventSaved func(ctx context.Context, event nostr.Event)
|
||||||
|
OnEventDeleted func(ctx context.Context, deleted nostr.Event)
|
||||||
OnEphemeralEvent func(ctx context.Context, event nostr.Event)
|
OnEphemeralEvent func(ctx context.Context, event nostr.Event)
|
||||||
OnRequest func(ctx context.Context, filter nostr.Filter) (reject bool, msg string)
|
OnRequest func(ctx context.Context, filter nostr.Filter) (reject bool, msg string)
|
||||||
OnCount func(ctx context.Context, filter nostr.Filter) (reject bool, msg string)
|
OnCount func(ctx context.Context, filter nostr.Filter) (reject bool, msg string)
|
||||||
@@ -155,7 +156,15 @@ func (rl *Relay) UseEventstore(store eventstore.Store, maxQueryLimit int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// only when using the eventstore we automatically set up the expiration manager
|
// only when using the eventstore we automatically set up the expiration manager
|
||||||
rl.StartExpirationManager(rl.QueryStored, rl.DeleteEvent)
|
rl.StartExpirationManager(func(ctx context.Context, filter nostr.Filter) iter.Seq[nostr.Event] {
|
||||||
|
return rl.QueryStored(ctx, filter)
|
||||||
|
}, func(ctx context.Context, id nostr.ID) error {
|
||||||
|
return rl.DeleteEvent(ctx, id)
|
||||||
|
}, func(ctx context.Context, evt nostr.Event) {
|
||||||
|
if rl.OnEventDeleted != nil {
|
||||||
|
rl.OnEventDeleted(ctx, evt)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rl *Relay) getBaseURL(r *http.Request) string {
|
func (rl *Relay) getBaseURL(r *http.Request) string {
|
||||||
|
|||||||
Reference in New Issue
Block a user