@@ -58,8 +58,17 @@ type Message struct {
5858}
5959
6060type 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
6574type 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 }
0 commit comments