forked from mautrix/signal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
disappearing.go
156 lines (143 loc) · 4.76 KB
/
disappearing.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// mautrix-signal - A Matrix-signal puppeting bridge.
// Copyright (C) 2023 Scott Weber
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"context"
"fmt"
"time"
"github.com/rs/zerolog"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/id"
"go.mau.fi/mautrix-signal/database"
)
type DisappearingMessagesManager struct {
DB *database.Database
Log zerolog.Logger
Bridge *SignalBridge
checkMessagesChan chan struct{}
}
func (dmm *DisappearingMessagesManager) ScheduleDisappearingForRoom(ctx context.Context, roomID id.RoomID) {
log := dmm.Log.With().Stringer("room_id", roomID).Logger()
disappearingMessages, err := dmm.DB.DisappearingMessage.GetUnscheduledForRoom(ctx, roomID)
if err != nil {
log.Err(err).Msg("Failed to get unscheduled disappearing messages")
return
}
for _, disappearingMessage := range disappearingMessages {
err = disappearingMessage.StartExpirationTimer(ctx)
if err != nil {
log.Err(err).Msg("Failed to schedule disappearing message")
} else {
log.Debug().
Stringer("event_id", disappearingMessage.EventID).
Time("expire_at", disappearingMessage.ExpireAt).
Msg("Scheduling disappearing message")
}
}
// Tell the disappearing messages loop to check again
dmm.checkMessagesChan <- struct{}{}
}
func (dmm *DisappearingMessagesManager) StartDisappearingLoop(ctx context.Context) {
dmm.checkMessagesChan = make(chan struct{}, 1)
go func() {
log := dmm.Log.With().Str("action", "loop").Logger()
ctx = log.WithContext(ctx)
for {
dmm.redactExpiredMessages(ctx)
duration := 10 * time.Minute // Check again in 10 minutes just in case
nextMsg, err := dmm.DB.DisappearingMessage.GetNextScheduledMessage(ctx)
if err != nil {
if ctx.Err() != nil {
return
}
log.Err(err).Msg("Failed to get next disappearing message")
continue
} else if nextMsg != nil {
duration = time.Until(nextMsg.ExpireAt)
}
select {
case <-time.After(duration):
case <-dmm.checkMessagesChan:
case <-ctx.Done():
return
}
}
}()
}
func (dmm *DisappearingMessagesManager) redactExpiredMessages(ctx context.Context) {
log := zerolog.Ctx(ctx)
expiredMessages, err := dmm.DB.DisappearingMessage.GetExpiredMessages(ctx)
if err != nil {
log.Err(err).Msg("Failed to get expired disappearing messages")
return
}
for _, msg := range expiredMessages {
portal := dmm.Bridge.GetPortalByMXID(msg.RoomID)
if portal == nil {
log.Warn().Stringer("event_id", msg.EventID).Stringer("room_id", msg.RoomID).Msg("Failed to redact message: portal not found")
err = msg.Delete(ctx)
if err != nil {
log.Err(err).
Stringer("event_id", msg.EventID).
Msg("Failed to delete disappearing message row in database")
}
continue
}
_, err = portal.MainIntent().RedactEvent(ctx, msg.RoomID, msg.EventID, mautrix.ReqRedact{
Reason: "Message expired",
TxnID: fmt.Sprintf("mxsg_disappear_%s", msg.EventID),
})
if err != nil {
log.Err(err).
Stringer("event_id", msg.EventID).
Stringer("room_id", msg.RoomID).
Msg("Failed to redact message")
} else {
log.Err(err).
Stringer("event_id", msg.EventID).
Stringer("room_id", msg.RoomID).
Msg("Redacted message")
}
err = msg.Delete(ctx)
if err != nil {
log.Err(err).
Stringer("event_id", msg.EventID).
Msg("Failed to delete disappearing message row in database")
}
}
}
func (dmm *DisappearingMessagesManager) AddDisappearingMessage(ctx context.Context, eventID id.EventID, roomID id.RoomID, expireIn time.Duration, startTimerNow bool) {
if expireIn == 0 {
return
}
var expireAt time.Time
if startTimerNow {
expireAt = time.Now().Add(expireIn)
}
disappearingMessage := dmm.DB.DisappearingMessage.NewWithValues(roomID, eventID, expireIn, expireAt)
err := disappearingMessage.Insert(ctx)
if err != nil {
zerolog.Ctx(ctx).Err(err).Stringer("event_id", eventID).
Msg("Failed to add disappearing message to database")
return
}
zerolog.Ctx(ctx).Debug().Stringer("event_id", eventID).
Msg("Added disappearing message row to database")
if startTimerNow {
// Tell the disappearing messages loop to check again
dmm.checkMessagesChan <- struct{}{}
}
}