This repository was archived by the owner on Dec 18, 2023. It is now read-only.
forked from mautrix/signal
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdisappearing.go
120 lines (106 loc) · 4.12 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
// 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(roomID id.RoomID) {
dmm.Log.Debug().Msgf("Scheduling disappearing messages for %s", roomID)
disappearingMessages := dmm.DB.DisappearingMessage.GetUnscheduledForRoom(roomID)
for _, disappearingMessage := range disappearingMessages {
dmm.Log.Debug().Msgf("Scheduling disappearing message %s", disappearingMessage.EventID)
disappearingMessage.StartExpirationTimer()
}
// 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() {
for {
dmm.redactExpiredMessages()
duration := 10 * time.Minute // Check again in 10 minutes just in case
nextMsg := dmm.DB.DisappearingMessage.GetNextScheduledMessage()
if nextMsg != nil {
dmm.Log.Debug().Msgf("Next message to expire is %s in %s", nextMsg.EventID, nextMsg.ExpireAt.Sub(time.Now()))
duration = nextMsg.ExpireAt.Sub(time.Now())
}
select {
case <-time.After(duration):
// We should have at least one expired message now, so we should check again
dmm.Log.Debug().Msgf("Duration (%s) is up, checking for expired messages", duration)
case <-dmm.checkMessagesChan:
// There are new messages in the DB, so we should check again
dmm.Log.Debug().Msg("New messages in DB, checking again")
case <-ctx.Done():
// We've been told to stop
dmm.Log.Debug().Msg("Stopping disappearing messages loop")
return
}
}
}()
}
func (dmm *DisappearingMessagesManager) redactExpiredMessages() {
// Get all expired messages and redact them
expiredMessages := dmm.DB.DisappearingMessage.GetExpiredMessages()
for _, msg := range expiredMessages {
portal := dmm.Bridge.GetPortalByMXID(msg.RoomID)
if portal == nil {
dmm.Log.Warn().Msgf("Failed to redact message %s in room %s: portal not found", msg.EventID, msg.RoomID)
return
}
// Redact the message
_, err := portal.MainIntent().RedactEvent(msg.RoomID, msg.EventID, mautrix.ReqRedact{
Reason: "Message expired",
TxnID: fmt.Sprintf("mxsig_disappear_%s", msg.EventID),
})
if err != nil {
portal.log.Warn().Msgf("Failed to make %s disappear: %v", msg.EventID, err)
} else {
portal.log.Debug().Msgf("Disappeared %s", msg.EventID)
}
msg.Delete()
}
}
func (dmm *DisappearingMessagesManager) AddDisappearingMessage(eventID id.EventID, roomID id.RoomID, expireInSeconds int64, startTimerNow bool) {
if expireInSeconds == 0 {
dmm.Log.Debug().Msgf("Not adding disappearing message %s: expireIn is 0", eventID)
return
}
dmm.Log.Debug().Msgf("Adding disappearing message %s", eventID)
expireAt := time.Time{}
if startTimerNow {
expireAt = time.Now().Add(time.Duration(expireInSeconds) * time.Second)
}
disappearingMessage := dmm.DB.DisappearingMessage.NewWithValues(roomID, eventID, expireInSeconds, expireAt)
disappearingMessage.Insert(nil)
if startTimerNow {
// Tell the disappearing messages loop to check again
dmm.checkMessagesChan <- struct{}{}
}
}