-
Notifications
You must be signed in to change notification settings - Fork 0
/
purge.go
139 lines (120 loc) · 3.41 KB
/
purge.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
// Copyright 2021 The fallacy Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package fallacy
import (
"errors"
"log"
"strconv"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
)
// The max amount of messages to fetch at once—the server will only give about
// ~1000 events.
const fetchLimit = 1000
// RedactMessage only redacts message events, skipping redaction events, already
// redacted events, and state events.
func RedactMessage(ev event.Event) (err error) {
if ev.StateKey != nil {
return
}
if ev.Type != event.EventRedaction && ev.Unsigned.RedactedBecause == nil {
<-limit
_, err = Client.RedactEvent(ev.RoomID, ev.ID, mautrix.ReqRedact{})
return
}
return
}
func redactMessage(ev event.Event) {
if err := RedactMessage(ev); err != nil {
log.Println(err)
}
}
func validate(resp *mautrix.RespMessages, err error) (*mautrix.RespMessages, error) {
if err == nil && resp == nil {
return resp, errNilMsgResponse
}
return resp, err
}
// PurgeUser redacts optionally a limit or all messages sent by a specified
// user. This is implemented efficiently using a filter to only obtain the
// events sent by the user.
func PurgeUser(body []string, ev event.Event) {
user := id.UserID(body[0])
var max int
var limit bool
if len(body) > 1 {
i, err := strconv.Atoi(body[1])
if err != nil {
sendNotice(ev.RoomID, "not a valid integer of messages to purge")
return
}
max = i
limit = true
}
filter := userFilter(user)
msg, err := validate(Client.Messages(ev.RoomID, "", "", 'b', &filter, fetchLimit))
var prev string
var i int
for err == nil && msg.End != prev {
prev = msg.End
for _, e := range msg.Chunk {
if limit {
if i >= max {
sendNotice(ev.RoomID, "Purging messages done!")
return
}
i++
}
go redactMessage(*e)
}
msg, err = validate(Client.Messages(ev.RoomID, msg.End, "", 'b', &filter, fetchLimit))
}
log.Println("purging user messages failed with", err)
}
// PurgeMessages redacts all message events newer than the specified event ID.
// It's loosely inspired by Telegram's SophieBot mechanics.
func PurgeMessages(body []string, ev event.Event) {
relate := ev.Content.AsMessage().RelatesTo
if relate == nil {
sendNotice(ev.RoomID, "Reply to the message you want to purge!")
return
}
c, err := Client.Context(ev.RoomID, relate.EventID, purgeFilter, 1)
if err != nil {
log.Println("fetching context failed with error", err)
return
}
go RedactMessage(*c.Event)
msg, err := validate(Client.Messages(ev.RoomID, c.End, "", 'f', purgeFilter, fetchLimit))
if msg != nil {
msg.Chunk = append(c.EventsAfter, msg.Chunk...)
}
for err == nil {
for _, e := range msg.Chunk {
go redactMessage(*e)
if e.ID == ev.ID {
sendNotice(ev.RoomID, "Purging messages done!")
return
}
}
msg, err = validate(Client.Messages(ev.RoomID, msg.End, "", 'f', purgeFilter, fetchLimit))
}
log.Println("fetching messages failed with", err)
}
// CommandPurge is a simple function to be invoked by the purge keyword.
func CommandPurge(body []string, ev event.Event) {
if !hasPerms(ev.RoomID, event.EventRedaction) {
sendNotice(ev.RoomID, permsMessage)
return
}
if len(body) > 0 {
PurgeUser(body, ev)
return
}
PurgeMessages(body, ev)
}
var (
errNilMsgResponse = errors.New("/messages response was nil, server has nothing to send us")
)