Skip to content

Commit f92947f

Browse files
committed
policyeval/eventhandle: add note if change is over 5 minutes old
1 parent 7e9d781 commit f92947f

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

policyeval/eventhandle.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"context"
55
"fmt"
66
"strings"
7+
"time"
78

89
"github.com/rs/zerolog"
10+
"go.mau.fi/util/exfmt"
911
"maunium.net/go/mautrix/event"
1012
"maunium.net/go/mautrix/id"
1113

@@ -111,6 +113,14 @@ func removeActionString(rec event.PolicyRecommendation) string {
111113

112114
func noopSendNotice(_ context.Context, _ string, _ ...any) id.EventID { return "" }
113115

116+
func oldEventNotice(timestamp int64) string {
117+
age := time.Since(time.UnixMilli(timestamp))
118+
if age > 5*time.Minute {
119+
return fmt.Sprintf(" %s ago", exfmt.DurationCustom(age, nil, exfmt.Day, time.Hour, time.Minute))
120+
}
121+
return ""
122+
}
123+
114124
func (pe *PolicyEvaluator) HandlePolicyListChange(ctx context.Context, policyRoom id.RoomID, added, removed *policylist.Policy) {
115125
policyRoomMeta := pe.GetWatchedListMeta(policyRoom)
116126
if policyRoomMeta == nil {
@@ -129,30 +139,35 @@ func (pe *PolicyEvaluator) HandlePolicyListChange(ctx context.Context, policyRoo
129139
if removedAndAddedAreEquivalent {
130140
if removed.Reason == added.Reason {
131141
sendNotice(ctx,
132-
"[%s] [%s](%s) re-%s ||`%s`|| for `%s`",
142+
"[%s] [%s](%s) re-%s ||`%s`|| for `%s`%s",
133143
policyRoomMeta.Name, added.Sender, added.Sender.URI().MatrixToURL(),
134-
addActionString(added.Recommendation), added.EntityOrHash(), added.Reason)
144+
addActionString(added.Recommendation), added.EntityOrHash(), added.Reason,
145+
oldEventNotice(added.Timestamp),
146+
)
135147
} else {
136148
sendNotice(ctx,
137-
"[%s] [%s](%s) changed the %s reason for ||`%s`|| from `%s` to `%s`",
149+
"[%s] [%s](%s) changed the %s reason for ||`%s`|| from `%s` to `%s`%s",
138150
policyRoomMeta.Name, added.Sender, added.Sender.URI().MatrixToURL(),
139-
changeActionString(added.Recommendation), added.EntityOrHash(), removed.Reason, added.Reason)
151+
changeActionString(added.Recommendation), added.EntityOrHash(), removed.Reason, added.Reason,
152+
oldEventNotice(added.Timestamp),
153+
)
140154
}
141155
} else {
142156
if removed != nil {
143157
sendNotice(ctx,
144-
"[%s] [%s](%s) %s %ss matching ||`%s`|| for `%s`",
158+
"[%s] [%s](%s) %s %ss matching ||`%s`|| for `%s`%s",
145159
policyRoomMeta.Name, removed.Sender, removed.Sender.URI().MatrixToURL(),
146160
removeActionString(removed.Recommendation), removed.EntityType, removed.EntityOrHash(), removed.Reason,
161+
oldEventNotice(removed.Timestamp),
147162
)
148163
if !policyRoomMeta.DontApply {
149164
pe.EvaluateRemovedRule(ctx, removed)
150165
}
151166
}
152167
if added != nil {
153-
var suffix string
168+
suffix := oldEventNotice(added.Timestamp)
154169
if added.Ignored {
155-
suffix = " (rule was ignored)"
170+
suffix += " (rule was ignored)"
156171
}
157172
sendNotice(ctx,
158173
"[%s] [%s](%s) %s %ss matching ||`%s`|| for `%s`%s",

policylist/room.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ func (r *Room) Update(evt *event.Event) (added, removed *Policy) {
112112
}
113113
if added != removed && removed != nil {
114114
removed.Sender = evt.Sender
115+
removed.Timestamp = evt.Timestamp
115116
}
116117
return
117118
}

0 commit comments

Comments
 (0)