concord 2.5.0

A terminal user interface client for Discord
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
use crate::discord::ids::{
    Id,
    marker::{ChannelMarker, GuildMarker},
};
use crate::discord::{AppCommand, DiscordAction, MuteDuration};
use crate::tui::keybindings::KeyChord;

use super::super::model::{ActionAvailability, FocusPane, MUTE_ACTION_DURATIONS};
use super::super::{
    DashboardState, MuteActionDurationItem, ThreadActionItem, ThreadActionKind,
    ThreadNotificationItem,
};
use super::{
    ActiveModalPopupKind, ModalPopup, ThreadActionMenuState, ThreadDeleteConfirmationState,
};

impl DashboardState {
    /// Open the action menu for the focused thread (a regular thread or a forum
    /// post). Returns `false` (so the caller can fall back to other action
    /// contexts) when a thread is not the current focus.
    pub fn open_selected_thread_actions(&mut self) -> bool {
        let Some((guild_id, channel_id)) = self.focused_thread_action_target() else {
            return false;
        };
        self.popups.modal = Some(ModalPopup::ThreadActionMenu(
            ThreadActionMenuState::Actions {
                guild_id,
                channel_id,
                selection: Default::default(),
            },
        ));
        true
    }

    /// The parent guild and thread id of the focused thread, whether it is a
    /// forum post selected in the messages pane (the forum's post list) or any
    /// thread shown in the channel pane.
    fn focused_thread_action_target(&self) -> Option<(Id<GuildMarker>, Id<ChannelMarker>)> {
        match self.navigation.focus {
            FocusPane::Messages if self.message_pane_uses_forum_posts() => {
                let (guild_id, _) = self.selected_forum_channel()?;
                let item = self
                    .selected_forum_post_items()
                    .get(self.selected_forum_post())?
                    .clone();
                Some((guild_id, item.channel_id))
            }
            FocusPane::Channels => {
                let entries = self.channel_pane_entries();
                let channel = entries.get(self.selected_channel())?.channel_state()?;
                let guild_id = channel.guild_id?;
                // Any focused thread opens the action menu. A forum post is just a
                // thread whose parent happens to be a forum; the forum-specific
                // rows gate on that further down.
                channel.is_thread().then_some((guild_id, channel.id))
            }
            _ => None,
        }
    }

    pub fn is_thread_action_menu_active(&self) -> bool {
        self.popups.thread_action_menu().is_some()
    }

    pub fn is_thread_action_mute_duration_phase(&self) -> bool {
        matches!(
            self.popups.thread_action_menu(),
            Some(ThreadActionMenuState::MuteDuration { .. })
        )
    }

    pub fn is_thread_action_notification_phase(&self) -> bool {
        matches!(
            self.popups.thread_action_menu(),
            Some(ThreadActionMenuState::NotificationSettings { .. })
        )
    }

    /// The noun for the active action menu's target: "post" for a forum post,
    /// "thread" otherwise. Used by the renderer for the menu titles.
    pub fn thread_action_menu_noun(&self) -> &'static str {
        let channel_id = match self.popups.thread_action_menu() {
            Some(
                ThreadActionMenuState::Actions { channel_id, .. }
                | ThreadActionMenuState::MuteDuration { channel_id, .. }
                | ThreadActionMenuState::NotificationSettings { channel_id, .. },
            ) => *channel_id,
            None => return "thread",
        };
        let is_forum_post = self
            .discord
            .cache
            .channel(channel_id)
            .and_then(|channel| channel.parent_id)
            .and_then(|parent_id| self.discord.cache.channel(parent_id))
            .is_some_and(|parent| parent.is_forum());
        if is_forum_post { "post" } else { "thread" }
    }

    pub fn close_thread_action_menu(&mut self) {
        if self.is_thread_action_menu_active() {
            self.popups.clear_modal();
        }
    }

    /// Step back from a submenu to the top-level actions. Returns `false` when
    /// already at the top level so the caller can close the menu instead.
    pub fn back_thread_action_menu(&mut self) -> bool {
        match self.popups.thread_action_menu() {
            Some(
                ThreadActionMenuState::MuteDuration {
                    guild_id,
                    channel_id,
                    ..
                }
                | ThreadActionMenuState::NotificationSettings {
                    guild_id,
                    channel_id,
                    ..
                },
            ) => {
                let (guild_id, channel_id) = (*guild_id, *channel_id);
                if let Some(menu) = self.popups.thread_action_menu_mut() {
                    *menu = ThreadActionMenuState::Actions {
                        guild_id,
                        channel_id,
                        selection: Default::default(),
                    };
                }
                true
            }
            _ => false,
        }
    }

    pub fn selected_thread_action_items(&self) -> Vec<ThreadActionItem> {
        let channel_id = match self.popups.thread_action_menu() {
            Some(ThreadActionMenuState::Actions { channel_id, .. }) => *channel_id,
            _ => return Vec::new(),
        };

        // A forum post is a thread whose parent is a forum channel. Pin is the
        // only forum-only action, and the labels read "post" for forum posts and
        // "thread" for regular threads.
        let is_forum_post = self
            .discord
            .cache
            .channel(channel_id)
            .and_then(|channel| channel.parent_id)
            .and_then(|parent_id| self.discord.cache.channel(parent_id))
            .is_some_and(|parent| parent.is_forum());
        let noun = if is_forum_post { "post" } else { "thread" };

        // A thread is "unread" when there is an ack target sitting ahead of the
        // last read message.
        let mark_as_read_enabled = self.discord.cache.channel_ack_target(channel_id).is_some();
        // Discord only offers muting once you follow the thread, so the mute row
        // is gated on membership (and unmute stays available while still followed).
        let followed = self.is_thread_followed(channel_id);
        let follow_label = format!("{} {noun}", if followed { "Unfollow" } else { "Follow" });
        let mute_label = format!(
            "{} {noun}",
            if self.discord.cache.channel_notification_muted(channel_id) {
                "Unmute"
            } else {
                "Mute"
            }
        );

        let can_moderate = self.can_moderate_thread(channel_id);
        let can_edit = self.can_edit_thread(channel_id);
        let can_change_archive_state = self.can_change_thread_archive_state(channel_id);
        let mutation_reason = |kind: ThreadActionKind| {
            kind.discord_action()
                .and_then(|action| self.discord_action_block_reason_in_channel(channel_id, action))
        };
        let close_action = if self.is_thread_archived(channel_id) {
            DiscordAction::ReopenThread
        } else {
            DiscordAction::ArchiveThread
        };
        let close_policy_reason =
            self.discord_action_block_reason_in_channel(channel_id, close_action);
        let close_label = format!(
            "{} {noun}",
            if self.is_thread_archived(channel_id) {
                "Reopen"
            } else {
                "Close"
            }
        );
        let lock_label = format!(
            "{} {noun}",
            if self.is_thread_locked(channel_id) {
                "Unlock"
            } else {
                "Lock"
            }
        );

        let mut items = vec![
            ThreadActionItem::new(
                ThreadActionKind::MarkAsRead,
                "Mark as read",
                (!mark_as_read_enabled).then(|| "no unread messages".to_owned()),
            ),
            ThreadActionItem::new(
                ThreadActionKind::ToggleFollow,
                follow_label,
                mutation_reason(ThreadActionKind::ToggleFollow).or_else(|| {
                    self.is_thread_archived(channel_id)
                        .then(|| "thread archived".to_owned())
                }),
            ),
            ThreadActionItem::new(
                ThreadActionKind::Close,
                close_label,
                close_policy_reason.or_else(|| {
                    (!can_change_archive_state).then(|| {
                        if self.is_thread_archived(channel_id) && !self.is_thread_locked(channel_id)
                        {
                            "Send Messages or Manage Threads required".to_owned()
                        } else {
                            "Thread Creator or Manage Threads required".to_owned()
                        }
                    })
                }),
            ),
            ThreadActionItem::new(
                ThreadActionKind::Lock,
                lock_label,
                mutation_reason(ThreadActionKind::Lock)
                    .or_else(|| (!can_moderate).then(|| "Manage Threads required".to_owned())),
            ),
            ThreadActionItem::new(
                ThreadActionKind::Edit,
                format!("Edit {noun}"),
                mutation_reason(ThreadActionKind::Edit).or_else(|| {
                    (!can_edit).then(|| "Thread Creator or Manage Threads required".to_owned())
                }),
            ),
            ThreadActionItem::new(
                ThreadActionKind::CopyLink,
                "Copy link",
                ActionAvailability::Enabled,
            ),
            ThreadActionItem::new(
                ThreadActionKind::ToggleMute,
                mute_label,
                (!followed).then(|| "follow thread first".to_owned()),
            ),
            ThreadActionItem::new(
                ThreadActionKind::NotificationSettings,
                "Notification settings",
                (!followed).then(|| "follow thread first".to_owned()),
            ),
        ];
        // Pinning only exists within a parent forum, so the row is forum-only.
        if is_forum_post {
            let pin_label = if self.is_thread_pinned(channel_id) {
                "Unpin post"
            } else {
                "Pin post"
            };
            items.push(ThreadActionItem::new(
                ThreadActionKind::Pin,
                pin_label,
                mutation_reason(ThreadActionKind::Pin)
                    .or_else(|| (!can_moderate).then(|| "Manage Threads required".to_owned())),
            ));
        }
        // Deleting removes the whole thread (moderator-only); the author's
        // body-only "delete message" is a separate action we do not offer.
        items.push(ThreadActionItem::new(
            ThreadActionKind::Delete,
            format!("Delete {noun}"),
            mutation_reason(ThreadActionKind::Delete)
                .or_else(|| (!can_moderate).then(|| "Manage Threads required".to_owned())),
        ));
        items.push(ThreadActionItem::new(
            ThreadActionKind::CopyId,
            "Copy thread ID",
            ActionAvailability::Enabled,
        ));
        items
    }

    /// Move the highlight to `row` within the current phase's list. Returns
    /// `false` when the row is out of range.
    pub fn select_thread_action_row(&mut self, row: usize) -> bool {
        if row >= self.thread_action_row_count() {
            return false;
        }
        if let Some(selection) = self.thread_action_selection_mut() {
            selection.select(row);
            return true;
        }
        false
    }

    /// Activate the row bound to `shortcut`: action-specific shortcuts in the
    /// top-level list, indexed shortcuts in the submenus. Mirrors the message
    /// action menu's shortcuts.
    pub fn activate_thread_action_shortcut(&mut self, shortcut: KeyChord) -> Option<AppCommand> {
        let index = match self.popups.thread_action_menu()? {
            ThreadActionMenuState::Actions { .. } => {
                let actions = self.selected_thread_action_items();
                self.key_bindings().matching_action_shortcut_index(
                    &actions,
                    shortcut,
                    |key_bindings, actions, index| {
                        key_bindings.thread_action_shortcuts(actions, index)
                    },
                    |action| action.is_enabled(),
                )?
            }
            ThreadActionMenuState::MuteDuration { .. } => {
                self.key_bindings().matching_indexed_shortcut_index(
                    shortcut,
                    self.selected_thread_mute_duration_items().len(),
                )?
            }
            ThreadActionMenuState::NotificationSettings { .. } => {
                self.key_bindings().matching_indexed_shortcut_index(
                    shortcut,
                    self.selected_thread_notification_items().len(),
                )?
            }
        };
        self.select_thread_action_row(index);
        self.activate_selected_thread_action()
    }

    pub fn selected_thread_mute_duration_items(&self) -> &'static [MuteActionDurationItem] {
        &MUTE_ACTION_DURATIONS
    }

    /// Build the three notification-level rows for the submenu, marking the
    /// current level with `[x]`. Unknown current level defaults to `4`
    /// (Only @mentions, Discord's default).
    pub fn selected_thread_notification_items(&self) -> Vec<ThreadNotificationItem> {
        let channel_id = match self.popups.thread_action_menu() {
            Some(ThreadActionMenuState::NotificationSettings { channel_id, .. }) => *channel_id,
            _ => return Vec::new(),
        };
        let current_flags = self
            .discord
            .cache
            .channel(channel_id)
            .and_then(|channel| channel.thread_notification_level_flags())
            .unwrap_or(4);
        vec![
            ThreadNotificationItem::new("All messages", 2, current_flags),
            ThreadNotificationItem::new("Only @mentions", 4, current_flags),
            ThreadNotificationItem::new("Nothing", 8, current_flags),
        ]
    }

    pub fn selected_thread_action_index(&self) -> Option<usize> {
        match self.popups.thread_action_menu()? {
            ThreadActionMenuState::Actions { selection, .. } => {
                Some(selection.selected_for_len(self.selected_thread_action_items().len()))
            }
            ThreadActionMenuState::MuteDuration { selection, .. } => {
                Some(selection.selected_for_len(self.selected_thread_mute_duration_items().len()))
            }
            ThreadActionMenuState::NotificationSettings { selection, .. } => {
                Some(selection.selected_for_len(self.selected_thread_notification_items().len()))
            }
        }
    }

    pub(in crate::tui) fn thread_action_row_count(&self) -> usize {
        match self.popups.thread_action_menu() {
            Some(ThreadActionMenuState::Actions { .. }) => {
                self.selected_thread_action_items().len()
            }
            Some(ThreadActionMenuState::MuteDuration { .. }) => {
                self.selected_thread_mute_duration_items().len()
            }
            Some(ThreadActionMenuState::NotificationSettings { .. }) => {
                self.selected_thread_notification_items().len()
            }
            None => 0,
        }
    }

    pub(super) fn thread_action_selection_mut(
        &mut self,
    ) -> Option<&mut super::SelectablePopupState> {
        match self.popups.thread_action_menu_mut()? {
            ThreadActionMenuState::Actions { selection, .. }
            | ThreadActionMenuState::MuteDuration { selection, .. }
            | ThreadActionMenuState::NotificationSettings { selection, .. } => Some(selection),
        }
    }

    pub fn move_thread_action_down(&mut self) {
        let len = self.thread_action_row_count();
        if let Some(selection) = self.thread_action_selection_mut() {
            selection.move_down(len);
        }
    }

    pub fn move_thread_action_up(&mut self) {
        if let Some(selection) = self.thread_action_selection_mut() {
            selection.move_up();
        }
    }

    pub fn activate_selected_thread_action(&mut self) -> Option<AppCommand> {
        let menu = self.popups.thread_action_menu().cloned()?;
        match menu {
            ThreadActionMenuState::Actions {
                guild_id,
                channel_id,
                selection,
            } => {
                let items = self.selected_thread_action_items();
                let item = items.get(selection.selected_for_len(items.len()))?.clone();
                if !item.is_enabled() {
                    return None;
                }
                match item.kind {
                    ThreadActionKind::MarkAsRead => {
                        self.mark_channel_as_read(channel_id);
                        self.close_thread_action_menu();
                        None
                    }
                    ThreadActionKind::CopyLink => {
                        let url = format!("https://discord.com/channels/{guild_id}/{channel_id}");
                        self.runtime.copy_text_requested = Some((url, "Link copied"));
                        self.close_thread_action_menu();
                        None
                    }
                    ThreadActionKind::CopyId => {
                        self.runtime.copy_text_requested =
                            Some((channel_id.get().to_string(), "Thread ID copied"));
                        self.close_thread_action_menu();
                        None
                    }
                    ThreadActionKind::ToggleFollow => {
                        self.close_thread_action_menu();
                        self.toggle_thread_follow(channel_id)
                    }
                    ThreadActionKind::ToggleMute => {
                        if self.discord.cache.channel_notification_muted(channel_id) {
                            self.close_thread_action_menu();
                            self.toggle_thread_mute(channel_id, None)
                        } else {
                            if let Some(menu) = self.popups.thread_action_menu_mut() {
                                *menu = ThreadActionMenuState::MuteDuration {
                                    guild_id,
                                    channel_id,
                                    selection: Default::default(),
                                };
                            }
                            None
                        }
                    }
                    ThreadActionKind::Close => {
                        self.close_thread_action_menu();
                        self.toggle_thread_archived(channel_id)
                    }
                    ThreadActionKind::Lock => {
                        self.close_thread_action_menu();
                        self.toggle_thread_locked(channel_id)
                    }
                    ThreadActionKind::Pin => {
                        self.close_thread_action_menu();
                        self.toggle_thread_pinned(channel_id)
                    }
                    ThreadActionKind::Delete => {
                        self.close_thread_action_menu();
                        self.open_thread_delete_confirmation(channel_id);
                        None
                    }
                    ThreadActionKind::Edit => {
                        self.close_thread_action_menu();
                        self.open_thread_edit(channel_id);
                        None
                    }
                    ThreadActionKind::NotificationSettings => {
                        if let Some(menu) = self.popups.thread_action_menu_mut() {
                            *menu = ThreadActionMenuState::NotificationSettings {
                                guild_id,
                                channel_id,
                                selection: Default::default(),
                            };
                        }
                        None
                    }
                }
            }
            ThreadActionMenuState::MuteDuration {
                channel_id,
                selection,
                ..
            } => {
                let items = self.selected_thread_mute_duration_items();
                let item = items.get(selection.selected_for_len(items.len()))?;
                let duration = item.duration;
                self.close_thread_action_menu();
                self.toggle_thread_mute(channel_id, Some(duration))
            }
            ThreadActionMenuState::NotificationSettings {
                channel_id,
                selection,
                ..
            } => {
                let items = self.selected_thread_notification_items();
                let item = items.get(selection.selected_for_len(items.len()))?.clone();
                self.close_thread_action_menu();
                Some(AppCommand::SetThreadNotificationLevel {
                    channel_id,
                    flags: item.flags,
                    label: self.channel_label(channel_id),
                })
            }
        }
    }

    /// Whether the current user is a member of the post thread (i.e. following
    /// it). Discord gates muting on this.
    fn is_thread_followed(&self, channel_id: Id<ChannelMarker>) -> bool {
        self.discord
            .cache
            .channel(channel_id)
            .map(|channel| channel.current_user_joined_thread)
            .unwrap_or(false)
    }

    fn toggle_thread_follow(&self, channel_id: Id<ChannelMarker>) -> Option<AppCommand> {
        let followed = self.is_thread_followed(channel_id);
        Some(AppCommand::SetThreadFollowed {
            channel_id,
            followed: !followed,
            label: self.channel_label(channel_id),
        })
    }

    /// Build the thread-member mute command for a thread. Unlike a regular
    /// channel, this targets the thread-member settings endpoint.
    fn toggle_thread_mute(
        &self,
        channel_id: Id<ChannelMarker>,
        duration: Option<MuteDuration>,
    ) -> Option<AppCommand> {
        let channel = self.discord.cache.channel(channel_id)?;
        let muted = !self.discord.cache.channel_notification_muted(channel_id);
        Some(AppCommand::SetThreadMuted {
            guild_id: channel.guild_id,
            channel_id,
            muted,
            duration,
            label: self.channel_label(channel_id),
        })
    }

    fn is_thread_archived(&self, channel_id: Id<ChannelMarker>) -> bool {
        self.discord
            .cache
            .channel(channel_id)
            .and_then(|channel| channel.thread_archived())
            .unwrap_or(false)
    }

    fn is_thread_locked(&self, channel_id: Id<ChannelMarker>) -> bool {
        self.discord
            .cache
            .channel(channel_id)
            .and_then(|channel| channel.thread_locked())
            .unwrap_or(false)
    }

    fn is_thread_pinned(&self, channel_id: Id<ChannelMarker>) -> bool {
        self.discord
            .cache
            .channel(channel_id)
            .and_then(|channel| channel.thread_pinned())
            .unwrap_or(false)
    }

    /// Whether the user can moderate the post (lock/pin/delete). Resolves the
    /// manage permission against the thread, which inherits from its parent.
    fn can_moderate_thread(&self, channel_id: Id<ChannelMarker>) -> bool {
        self.discord
            .cache
            .channel(channel_id)
            .is_some_and(|channel| self.discord.cache.can_manage_threads_in_channel(channel))
    }

    fn can_change_thread_archive_state(&self, channel_id: Id<ChannelMarker>) -> bool {
        self.discord
            .cache
            .channel(channel_id)
            .is_some_and(|channel| {
                let permission = if channel.thread_archived().unwrap_or(false) {
                    crate::discord::DiscordPermission::ReopenThread
                } else {
                    crate::discord::DiscordPermission::EditOwnThread
                };
                self.discord
                    .cache
                    .channel_permission_decision(channel, permission)
                    .is_allowed()
            })
    }

    fn can_edit_thread(&self, channel_id: Id<ChannelMarker>) -> bool {
        self.discord
            .cache
            .channel(channel_id)
            .is_some_and(|channel| {
                self.discord
                    .cache
                    .channel_permission_decision(
                        channel,
                        crate::discord::DiscordPermission::EditOwnThread,
                    )
                    .is_allowed()
            })
    }

    fn toggle_thread_archived(&self, channel_id: Id<ChannelMarker>) -> Option<AppCommand> {
        Some(AppCommand::SetThreadArchived {
            channel_id,
            archived: !self.is_thread_archived(channel_id),
            label: self.channel_label(channel_id),
        })
    }

    fn toggle_thread_locked(&self, channel_id: Id<ChannelMarker>) -> Option<AppCommand> {
        Some(AppCommand::SetThreadLocked {
            channel_id,
            locked: !self.is_thread_locked(channel_id),
            label: self.channel_label(channel_id),
        })
    }

    fn toggle_thread_pinned(&self, channel_id: Id<ChannelMarker>) -> Option<AppCommand> {
        let current_flags = self
            .discord
            .cache
            .channel(channel_id)
            .and_then(|channel| channel.flags)
            .unwrap_or(0);
        Some(AppCommand::SetThreadPinned {
            channel_id,
            pinned: !self.is_thread_pinned(channel_id),
            current_flags,
            label: self.channel_label(channel_id),
        })
    }

    fn open_thread_delete_confirmation(&mut self, channel_id: Id<ChannelMarker>) {
        let name = self.channel_label(channel_id);
        let is_forum_post = self
            .discord
            .cache
            .channel(channel_id)
            .and_then(|channel| channel.parent_id)
            .and_then(|parent_id| self.discord.cache.channel(parent_id))
            .is_some_and(|parent| parent.is_forum());
        self.popups.confirmation_button = super::ConfirmationButton::default();
        self.popups.modal = Some(ModalPopup::ThreadDeleteConfirmation(
            ThreadDeleteConfirmationState {
                channel_id,
                name,
                is_forum_post,
            },
        ));
    }

    pub fn close_thread_delete_confirmation(&mut self) {
        if self.is_active_modal_popup(ActiveModalPopupKind::ThreadDeleteConfirmation) {
            self.popups.clear_modal();
        }
    }

    pub fn confirm_thread_delete(&mut self) -> Option<AppCommand> {
        let confirmation = self.popups.take_thread_delete_confirmation()?;
        Some(AppCommand::DeleteThread {
            channel_id: confirmation.channel_id,
            label: confirmation.name,
        })
    }

    /// The display name and noun ("post" for a forum post, "thread" otherwise)
    /// for the open delete confirmation gate.
    pub fn thread_delete_confirmation_target(&self) -> Option<(String, &'static str)> {
        self.popups
            .thread_delete_confirmation()
            .map(|confirmation| {
                let noun = if confirmation.is_forum_post {
                    "post"
                } else {
                    "thread"
                };
                (confirmation.name.clone(), noun)
            })
    }
}