Skip to content

Commit

Permalink
Moved communities removing events code to the query package.
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-myles committed Dec 24, 2024
1 parent 4f69603 commit 4b74cc5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 46 deletions.
30 changes: 30 additions & 0 deletions database/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ func (db *dbClient) AcceptEvents(ctx context.Context, events ...*model.Event) er
}

if events[i].Kind == nostr.KindDeletion {
communityFilters, err := db.prepareCommunityDeleteFilters(ctx, events[i])
if err != nil {
return err
}
if len(communityFilters) > 0 {
req.Delete = append(req.Delete, communityFilters...)

continue
}
if err := req.Remove(events[i]); err != nil {
return err
}
Expand Down Expand Up @@ -497,3 +506,24 @@ func (db *dbClient) deleteExpiredEvents(ctx context.Context) error {
}
return err
}

func (db *dbClient) prepareCommunityDeleteFilters(ctx context.Context, incomingEvent *model.Event) (filters []databaseFilterDelete, err error) {
var ids []string
for _, eTag := range incomingEvent.Tags.GetAll([]string{"e"}) {
if eTag.Key() == "e" {
ids = append(ids, eTag.Value())
}
}
filters = make([]databaseFilterDelete, 0)
for ev := range db.SelectEvents(ctx, model.Filter{IDs: ids}) {
if hTag := ev.GetTag("h"); hTag == nil {
continue
}
filters = append(filters, databaseFilterDelete{
Author: ev.GetMasterPublicKey(),
IDs: []string{ev.GetID()},
})
}

return filters, nil
}
49 changes: 3 additions & 46 deletions server/ws/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"crypto/sha512"
"encoding/base64"
"encoding/hex"
"fmt"
"log"
"math"
"math/rand/v2"
Expand Down Expand Up @@ -187,31 +186,18 @@ func (h *handler) handleReq(ctx context.Context, respWriter Writer, sub *model.S
}

func (h *handler) handleEvents(ctx context.Context, respWriter Writer, events []*model.Event, cfg *Config) error {
var allEvents []*model.Event
for i := range events {
if err := h.validateIncomingEvent(ctx, events[i], cfg); err != nil {
return errors.Wrapf(err, "event %v: invalid", events[i])
}
if events[i].Kind == nostr.KindDeletion {
evs, err := prepareCommunityEventsForDeletion(ctx, events[i])
if err != nil {
return err
}
if len(evs) > 0 {
allEvents = append(allEvents, evs...)

continue
}
}
allEvents = append(allEvents, events[i])
}

if wsEventListener == nil {
log.Panic("wsEventListener is not set")
}

if eventMustAuth != nil {
if authRequired := eventMustAuth(ctx, allEvents...); authRequired {
if authRequired := eventMustAuth(ctx, events...); authRequired {
status, _ := h.connAuth.LoadOrCompute(respWriter, func() connAuthData {
return connAuthData{
Challenge: generateChallenge(),
Expand All @@ -229,11 +215,11 @@ func (h *handler) handleEvents(ctx context.Context, respWriter Writer, events []
}
}

if err := wsEventListener(ctx, allEvents...); err != nil {
if err := wsEventListener(ctx, events...); err != nil {
return errors.Wrap(err, "failed to store events")
}

if err := h.notifyListenersAboutNewEvents(ctx, allEvents...); err != nil {
if err := h.notifyListenersAboutNewEvents(ctx, events...); err != nil {
return errors.Wrap(ErrNotifyFailed, err.Error())
}

Expand Down Expand Up @@ -327,32 +313,3 @@ func (h *handler) handleCount(ctx context.Context, envelope *nostr.CountEnvelope

return nil
}

func prepareCommunityEventsForDeletion(ctx context.Context, incomingEvent *model.Event) (evs []*model.Event, err error) {
var ids []string
for _, eTag := range incomingEvent.Tags.GetAll([]string{"e"}) {
if eTag.Key() == "e" {
ids = append(ids, eTag.Value())
}
}
res := make([]*model.Event, 0)
for ev := range query.GetStoredEvents(ctx, &model.Subscription{Filters: model.Filters{nostr.Filter{IDs: ids}}}) {
if hTag := ev.GetTag("h"); hTag == nil {
continue
}
res = append(res, &model.Event{
Event: nostr.Event{
Kind: nostr.KindDeletion,
ID: ev.ID,
Tags: model.Tags{
{"k", fmt.Sprint(ev.Kind)},
{"e", fmt.Sprint(ev.ID)},
{"a", fmt.Sprintf("%v:%v:%v", ev.Kind, ev.GetMasterPublicKey(), ev.Tags.GetD())},
},
PubKey: ev.GetMasterPublicKey(),
},
})
}

return res, nil
}

0 comments on commit 4b74cc5

Please sign in to comment.