-
Notifications
You must be signed in to change notification settings - Fork 21
/
deleting.go
81 lines (73 loc) · 1.65 KB
/
deleting.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package khatru
import (
"context"
"fmt"
"strconv"
"strings"
"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 {
var f nostr.Filter
switch tag[0] {
case "e":
f = nostr.Filter{IDs: []string{tag[1]}}
case "a":
spl := strings.Split(tag[1], ":")
if len(spl) != 3 {
continue
}
kind, err := strconv.Atoi(spl[0])
if err != nil {
continue
}
author := spl[1]
identifier := spl[2]
f = nostr.Filter{
Kinds: []int{kind},
Authors: []string{author},
Tags: nostr.TagMap{"d": []string{identifier}},
Until: &evt.CreatedAt,
}
default:
continue
}
for _, query := range rl.QueryEvents {
ch, err := query(ctx, f)
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 {
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 {
if err := del(ctx, target); err != nil {
return err
}
}
} else {
// fail and stop here
return fmt.Errorf("blocked: %s", msg)
}
// don't try to query this same event again
break
}
}
}
return nil
}