| 1 | use gpui_kit::*; |
| 2 | use store::MessageRecord; |
| 3 | |
| 4 | use super::ConversationView; |
| 5 | use crate::notice::short_error; |
| 6 | use crate::restorable_deletes::RESTORE_FAILED_NOTICE; |
| 7 | use crate::runtime; |
| 8 | |
| 9 | impl ConversationView { |
| 10 | pub(super) fn delete(&mut self, message_id: &str, cx: &mut Context<Self>) { |
| 11 | let Some(record) = self.own_record(message_id, cx) else { |
| 12 | return; |
| 13 | }; |
| 14 | let state = self.app.read(cx); |
| 15 | let (mode, engine) = (state.mode, state.engine.clone()); |
| 16 | if mode.demo { |
| 17 | self.remember_delete(&record, cx); |
| 18 | self.store_deleted(record, true, cx); |
| 19 | return; |
| 20 | } |
| 21 | let Some(engine) = engine.filter(|_| !mode.read_only) else { |
| 22 | self.show_notice("Read-only mode: message not deleted", cx); |
| 23 | return; |
| 24 | }; |
| 25 | self.remember_delete(&record, cx); |
| 26 | let (conversation_id, message_id) = (record.conversation_id, record.message_id); |
| 27 | let (failed_conversation, failed_message) = (conversation_id.clone(), message_id.clone()); |
| 28 | let receiver = runtime::spawn(async move { |
| 29 | engine |
| 30 | .soft_delete_message(&conversation_id, &message_id) |
| 31 | .await |
| 32 | }); |
| 33 | cx.spawn(async move |this, cx| { |
| 34 | let error = match receiver.await { |
| 35 | Ok(Ok(())) => return, |
| 36 | Ok(Err(error)) => short_error(&error), |
| 37 | Err(_) => "cancelled".to_owned(), |
| 38 | }; |
| 39 | this.update(cx, |this, cx| { |
| 40 | this.app.update(cx, |state, _| { |
| 41 | state |
| 42 | .restorable_deletes |
| 43 | .forget(&failed_conversation, &failed_message) |
| 44 | }); |
| 45 | this.show_notice(&format!("Delete failed: {error}"), cx); |
| 46 | }) |
| 47 | .ok(); |
| 48 | }) |
| 49 | .detach(); |
| 50 | } |
| 51 | |
| 52 | pub(super) fn undo_delete(&mut self, message_id: &str, cx: &mut Context<Self>) { |
| 53 | let Some(record) = self.deleted_own_record(message_id, cx) else { |
| 54 | return; |
| 55 | }; |
| 56 | let state = self.app.read(cx); |
| 57 | let (mode, engine) = (state.mode, state.engine.clone()); |
| 58 | if mode.read_only && !mode.demo { |
| 59 | return; |
| 60 | } |
| 61 | let forgotten = self.app.update(cx, |state, _| { |
| 62 | state |
| 63 | .restorable_deletes |
| 64 | .forget(&record.conversation_id, &record.message_id) |
| 65 | }); |
| 66 | if !forgotten { |
| 67 | return; |
| 68 | } |
| 69 | let engine = engine.filter(|_| !mode.demo); |
| 70 | self.store_deleted(record.clone(), false, cx); |
| 71 | let Some(engine) = engine else { |
| 72 | return; |
| 73 | }; |
| 74 | let (conversation_id, message_id) = |
| 75 | (record.conversation_id.clone(), record.message_id.clone()); |
| 76 | let receiver = runtime::spawn(async move { |
| 77 | engine |
| 78 | .undo_soft_delete_message(&conversation_id, &message_id) |
| 79 | .await |
| 80 | }); |
| 81 | cx.spawn(async move |this, cx| { |
| 82 | if matches!(receiver.await, Ok(Ok(()))) { |
| 83 | return; |
| 84 | } |
| 85 | this.update(cx, |this, cx| { |
| 86 | this.remember_delete(&record, cx); |
| 87 | this.store_deleted(record, true, cx); |
| 88 | this.show_notice(RESTORE_FAILED_NOTICE, cx); |
| 89 | }) |
| 90 | .ok(); |
| 91 | }) |
| 92 | .detach(); |
| 93 | } |
| 94 | |
| 95 | fn remember_delete(&mut self, record: &MessageRecord, cx: &mut Context<Self>) { |
| 96 | self.app.update(cx, |state, _| { |
| 97 | state |
| 98 | .restorable_deletes |
| 99 | .remember(&record.conversation_id, &record.message_id) |
| 100 | }); |
| 101 | } |
| 102 | |
| 103 | fn store_deleted(&mut self, mut record: MessageRecord, deleted: bool, cx: &mut Context<Self>) { |
| 104 | record.deleted = deleted; |
| 105 | let _ = self |
| 106 | .app |
| 107 | .read(cx) |
| 108 | .store |
| 109 | .upsert_messages(std::slice::from_ref(&record)); |
| 110 | self.rebuild(false, cx); |
| 111 | } |
| 112 | |
| 113 | fn deleted_own_record(&self, message_id: &str, cx: &App) -> Option<MessageRecord> { |
| 114 | let conversation_id = self.conversation_id()?; |
| 115 | let my_user_id = self.row_context(cx).my_user_id?; |
| 116 | self.app |
| 117 | .read(cx) |
| 118 | .store |
| 119 | .messages_by_id(&conversation_id, &[message_id.to_owned()]) |
| 120 | .ok()? |
| 121 | .remove(message_id) |
| 122 | .filter(|record| record.deleted && record.sender_id.as_deref() == Some(&my_user_id)) |
| 123 | } |
| 124 | } |