Skip to content

Commit

Permalink
Fix inefficient deletions on API v8
Browse files Browse the repository at this point in the history
There can be multiple hits in a message context array.
  • Loading branch information
cedws committed Mar 31, 2021
1 parent 9597786 commit 4216353
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,23 +183,17 @@ func (c *Client) DeleteMessages(messages *Messages, seek *int) error {
const minSleep = 200

for _, ctx := range messages.ContextMessages {
var hit *Message

for _, msg := range ctx {
if msg.Hit {
hit = &msg
break
if !msg.Hit {
// This is a context message which may or may not be authored
// by the current user
log.Debugf("Skipping context message")
continue
}

// This is a context message which may or may not be authored
// by the current user.
log.Debugf("Skipping context message")
}

if hit != nil {
// The message might be an action rather than text. Actions aren't deletable.
// An example of an action is a call request.
if hit.Type != UserMessage {
if msg.Type != UserMessage {
log.Debugf("Found message of non-zero type, incrementing seek index")
(*seek)++
continue
Expand All @@ -210,27 +204,25 @@ func (c *Client) DeleteMessages(messages *Messages, seek *int) error {
// Entire channels should be skipped at the caller of this function
// We do it this way because guilds searches return a mix of messages
// from any channel
if c.skipChannel(hit.ChannelID) {
log.Infof("Skipping message deletion for channel %v", hit.ChannelID)
if c.skipChannel(msg.ChannelID) {
log.Infof("Skipping message deletion for channel %v", msg.ChannelID)
(*seek)++
continue
}

log.Infof("Deleting message %v from channel %v", hit.ID, hit.ChannelID)
log.Infof("Deleting message %v from channel %v", msg.ID, msg.ChannelID)
if c.dryRun {
// Move seek index forward to simulate message deletion on server's side
(*seek)++
} else {
err := c.DeleteMessage(hit)
err := c.DeleteMessage(&msg)
if err != nil {
return errors.Wrap(err, "Error deleting message")
}
time.Sleep(minSleep * time.Millisecond)
}
// Increment regardless of whether it's a dry run
c.deletedCount++

break
}
}

Expand Down

0 comments on commit 4216353

Please sign in to comment.