Skip to content
This repository was archived by the owner on Jul 20, 2025. It is now read-only.

Commit f43f259

Browse files
committed
Check if message is in archived or locked thread before deletion
1 parent 2247120 commit f43f259

File tree

2 files changed

+70
-60
lines changed

2 files changed

+70
-60
lines changed

client/client.go

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,17 @@ type Message struct {
5858
}
5959

6060
type Messages struct {
61-
TotalResults int `json:"total_results"`
62-
ContextMessages [][]Message `json:"messages"`
61+
TotalResults int `json:"total_results"`
62+
Messages [][]Message `json:"messages"`
63+
Threads []Thread `json:"threads"`
64+
}
65+
66+
type Thread struct {
67+
ID string
68+
Metadata struct {
69+
Archived bool
70+
Locked bool
71+
} `json:"thread_metadata"`
6372
}
6473

6574
type ServerWait struct {
@@ -131,7 +140,7 @@ func (c *Client) Delete() error {
131140
}
132141

133142
for _, channel := range channels {
134-
if err = c.DeleteFromChannel(me, &channel); err != nil {
143+
if err = c.DeleteFromChannel(me, channel); err != nil {
135144
return err
136145
}
137146
}
@@ -152,7 +161,7 @@ Relationships:
152161
}
153162
}
154163

155-
channel, err := c.ChannelRelationship(&relation.Recipient)
164+
channel, err := c.RelationshipChannel(relation.Recipient)
156165
if err != nil {
157166
return fmt.Errorf("error resolving relationship to channel: %w", err)
158167
}
@@ -169,7 +178,7 @@ Relationships:
169178
return fmt.Errorf("error fetching guilds: %w", err)
170179
}
171180
for _, guild := range guilds {
172-
if err = c.DeleteFromGuild(me, &guild); err != nil {
181+
if err = c.DeleteFromGuild(me, guild); err != nil {
173182
return err
174183
}
175184
}
@@ -179,7 +188,7 @@ Relationships:
179188
return nil
180189
}
181190

182-
func (c *Client) DeleteFromChannel(me *Me, channel *Channel) error {
191+
func (c *Client) DeleteFromChannel(me Me, channel Channel) error {
183192
if c.skipChannel(channel.ID) {
184193
log.Infof("skipping message deletion for channel %v", channel.ID)
185194
return nil
@@ -192,7 +201,7 @@ func (c *Client) DeleteFromChannel(me *Me, channel *Channel) error {
192201
if err != nil {
193202
return fmt.Errorf("error fetching messages for channel: %w", err)
194203
}
195-
if len(results.ContextMessages) == 0 {
204+
if len(results.Messages) == 0 {
196205
log.Infof("no more messages to delete for channel %v", channel.ID)
197206
break
198207
}
@@ -205,7 +214,7 @@ func (c *Client) DeleteFromChannel(me *Me, channel *Channel) error {
205214
return nil
206215
}
207216

208-
func (c *Client) DeleteFromGuild(me *Me, channel *Channel) error {
217+
func (c *Client) DeleteFromGuild(me Me, channel Channel) error {
209218
if c.skipChannel(channel.ID) {
210219
log.Infof("skipping message deletion for guild '%v'", channel.Name)
211220
return nil
@@ -218,7 +227,7 @@ func (c *Client) DeleteFromGuild(me *Me, channel *Channel) error {
218227
if err != nil {
219228
return fmt.Errorf("error fetching messages for guild: %w", err)
220229
}
221-
if len(results.ContextMessages) == 0 {
230+
if len(results.Messages) == 0 {
222231
log.Infof("no more messages to delete for guild '%v'", channel.Name)
223232
break
224233
}
@@ -231,29 +240,45 @@ func (c *Client) DeleteFromGuild(me *Me, channel *Channel) error {
231240
return nil
232241
}
233242

234-
func (c *Client) DeleteMessages(messages *Messages, offset *int) error {
243+
func (c *Client) DeleteMessages(messages Messages, offset *int) error {
235244
// Milliseconds to wait between deleting messages
236245
// A delay which is too short will cause the server to return 429 and force us to wait a while
237246
// By preempting the server's delay, we can reduce the number of requests made to the server
238247
const minSleep = 200
239248

240-
for _, ctx := range messages.ContextMessages {
249+
archived := make(map[string]bool)
250+
for _, thread := range messages.Threads {
251+
archived[thread.ID] = thread.Metadata.Archived || thread.Metadata.Locked
252+
}
253+
254+
for _, ctx := range messages.Messages {
241255
for _, msg := range ctx {
242256
if !msg.Hit {
243-
// This is a context message which may or may not be authored
244-
// by the current user
257+
// message is for context but may not be authored by this user
245258
log.Debugf("skipping context message")
246259
continue
247260
}
248261

249-
// The message might be an action rather than text. Actions aren't deletable.
250-
// An example of an action is a call request.
262+
if archived[msg.ChannelID] {
263+
// TODO: try to unarchive the thread
264+
log.Debugf("message is in archived or locked thread %v", msg.ChannelID)
265+
(*offset)++
266+
continue
267+
}
268+
251269
if msg.Type != UserMessage && msg.Type != UserReply {
270+
// message is not text but could be an action for example
252271
log.Debugf("found message of type %v, seeking ahead", msg.Type)
253272
(*offset)++
254273
continue
255274
}
256275

276+
if c.skipPinned && msg.Pinned {
277+
log.Infof("found pinned message, skipping")
278+
(*offset)++
279+
continue
280+
}
281+
257282
// Check if this message is in our list of channels to skip
258283
// This will only skip this specific message and increment the seek index
259284
// Entire channels should be skipped at the caller of this function
@@ -265,12 +290,6 @@ func (c *Client) DeleteMessages(messages *Messages, offset *int) error {
265290
continue
266291
}
267292

268-
if c.skipPinned && msg.Pinned {
269-
log.Infof("found pinned message, skipping")
270-
(*offset)++
271-
continue
272-
}
273-
274293
log.Infof("deleting message %v from channel %v", msg.ID, msg.ChannelID)
275294
if c.dryRun {
276295
// Move seek index forward to simulate message deletion on server's side
@@ -281,6 +300,7 @@ func (c *Client) DeleteMessages(messages *Messages, offset *int) error {
281300
}
282301
time.Sleep(minSleep * time.Millisecond)
283302
}
303+
284304
// Increment regardless of whether it's a dry run
285305
c.deletedCount++
286306
}

client/request.go

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -122,35 +122,34 @@ func (c *Client) wait(res *http.Response, mult int) error {
122122
return fmt.Errorf("error decoding response: %w", err)
123123
}
124124

125-
// Multiply retry_after by the mult passed in
126125
millis := time.Duration(data.RetryAfter*float32(mult)) * time.Millisecond
127126
log.Infof("server asked us to sleep for %v", millis)
128127
time.Sleep(millis)
129128

130129
return nil
131130
}
132131

133-
func (c *Client) DeleteMessage(msg Message) error {
134-
endpoint := fmt.Sprintf("/channels/%v/messages/%v", msg.ChannelID, msg.ID)
135-
err := c.request("DELETE", endpoint, nil, nil)
136-
return err
132+
func (c *Client) DeleteMessage(msg Message) (err error) {
133+
endpoint := fmt.Sprintf(
134+
"/channels/%v/messages/%v",
135+
msg.ChannelID,
136+
msg.ID,
137+
)
138+
err = c.request("DELETE", endpoint, nil, nil)
139+
return
137140
}
138141

139-
func (c *Client) Me() (*Me, error) {
140-
var me Me
141-
err := c.request("GET", "/users/@me", nil, &me)
142-
143-
return &me, err
142+
func (c *Client) Me() (me Me, err error) {
143+
err = c.request("GET", "/users/@me", nil, &me)
144+
return
144145
}
145146

146-
func (c *Client) Channels() ([]Channel, error) {
147-
var channels []Channel
148-
149-
err := c.request("GET", "/users/@me/channels", nil, &channels)
150-
return channels, err
147+
func (c *Client) Channels() (channels []Channel, err error) {
148+
err = c.request("GET", "/users/@me/channels", nil, &channels)
149+
return
151150
}
152151

153-
func (c *Client) ChannelMessages(channel *Channel, me *Me, offset int) (*Messages, error) {
152+
func (c *Client) ChannelMessages(channel Channel, me Me, offset int) (messages Messages, err error) {
154153
endpoint := fmt.Sprintf(
155154
"/channels/%v/messages/search",
156155
channel.ID,
@@ -164,39 +163,32 @@ func (c *Client) ChannelMessages(channel *Channel, me *Me, offset int) (*Message
164163
MaxID: c.maxID,
165164
}
166165

167-
var results Messages
168-
err := c.request("GET", endpoint+args.MarshalText(), nil, &results)
169-
170-
return &results, err
166+
err = c.request("GET", endpoint+args.MarshalText(), nil, &messages)
167+
return
171168
}
172169

173-
func (c *Client) ChannelRelationship(relation *Recipient) (*Channel, error) {
170+
func (c *Client) RelationshipChannel(relation Recipient) (channel Channel, err error) {
174171
recipients := struct {
175172
Recipients []string `json:"recipients"`
176173
}{
177174
[]string{relation.ID},
178175
}
179-
var channel Channel
180176

181-
err := c.request("POST", "/users/@me/channels", recipients, &channel)
182-
return &channel, err
177+
err = c.request("POST", "/users/@me/channels", recipients, &channel)
178+
return
183179
}
184180

185-
func (c *Client) Relationships() ([]Relationship, error) {
186-
var relations []Relationship
187-
188-
err := c.request("GET", "/users/@me/relationships", nil, &relations)
189-
return relations, err
181+
func (c *Client) Relationships() (relations []Relationship, err error) {
182+
err = c.request("GET", "/users/@me/relationships", nil, &relations)
183+
return
190184
}
191185

192-
func (c *Client) Guilds() ([]Channel, error) {
193-
var channels []Channel
194-
195-
err := c.request("GET", "/users/@me/guilds", nil, &channels)
196-
return channels, err
186+
func (c *Client) Guilds() (channels []Channel, err error) {
187+
err = c.request("GET", "/users/@me/guilds", nil, &channels)
188+
return
197189
}
198190

199-
func (c *Client) GuildMessages(channel *Channel, me *Me, offset int) (*Messages, error) {
191+
func (c *Client) GuildMessages(channel Channel, me Me, offset int) (messages Messages, err error) {
200192
endpoint := fmt.Sprintf(
201193
"/guilds/%v/messages/search",
202194
channel.ID,
@@ -210,8 +202,6 @@ func (c *Client) GuildMessages(channel *Channel, me *Me, offset int) (*Messages,
210202
MaxID: c.maxID,
211203
}
212204

213-
var results Messages
214-
215-
err := c.request("GET", endpoint+args.MarshalText(), nil, &results)
216-
return &results, err
205+
err = c.request("GET", endpoint+args.MarshalText(), nil, &messages)
206+
return
217207
}

0 commit comments

Comments
 (0)