-
Notifications
You must be signed in to change notification settings - Fork 1
/
selfdestruct.go
50 lines (45 loc) · 1.49 KB
/
selfdestruct.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
package main
import (
"context"
"time"
"github.com/rs/zerolog"
"maunium.net/go/mautrix/id"
)
func restartSelfDestruct() {
log := globalLog.With().Str("action", "restart self-destruct").Logger()
ctx := log.WithContext(context.Background())
evts, err := db.GetSelfDestructingEvents(ctx)
if err != nil {
log.Err(err).Msg("Failed to get self-destructing events from database")
return
}
for _, evt := range evts {
log.Debug().
Str("event_id", evt.EventID.String()).
Time("delete_at", evt.DeleteAt).
Msg("Restarting self-destruct timer")
go doSelfDestruct(ctx, evt.RoomID, evt.EventID, evt.DeleteAt)
}
}
func selfDestruct(ctx context.Context, eventID id.EventID, after time.Duration) {
roomID := getEvent(ctx).RoomID
deleteAt := time.Now().Add(after)
err := db.SetSelfDestruct(ctx, roomID, eventID, deleteAt)
if err != nil {
zerolog.Ctx(ctx).Err(err).Msg("Failed to mark event as self-destructing in database")
}
go doSelfDestruct(ctx, roomID, eventID, deleteAt)
}
func doSelfDestruct(ctx context.Context, roomID id.RoomID, eventID id.EventID, at time.Time) {
time.Sleep(time.Until(at))
log := zerolog.Ctx(ctx).With().Str("target_event_id", eventID.String()).Logger()
if _, err := cli.RedactEvent(roomID, eventID); err != nil {
log.Err(err).Msg("Failed to self-destruct message")
} else {
log.Debug().Msg("Event self-destructed")
err = db.DoneSelfDestruct(ctx, eventID)
if err != nil {
log.Warn().Err(err).Msg("Failed to remove self-destruct marker from database")
}
}
}