From b1d0ae9feed62e4119437f5fc789a9d9370fa204 Mon Sep 17 00:00:00 2001 From: FreeScout Date: Fri, 3 Jan 2020 01:03:36 -0800 Subject: [PATCH] Delete single conversation forever --- app/Attachment.php | 22 +++++++++++++++- app/Conversation.php | 21 +++++++++++++++ .../Controllers/ConversationsController.php | 26 +++++++++++++++++++ public/js/main.js | 9 +++++-- resources/views/conversations/view.blade.php | 6 ++++- 5 files changed, 80 insertions(+), 4 deletions(-) diff --git a/app/Attachment.php b/app/Attachment.php index df77e3ff..3de5ca4b 100644 --- a/app/Attachment.php +++ b/app/Attachment.php @@ -238,13 +238,33 @@ class Attachment extends Model } $attachments = self::whereIn('id', $attachment_ids)->get(); + // Delete from disk + self::deleteForever($attachments); + } + + /** + * Delete attachments by thread IDs. + */ + public static function deleteByThreadIds($thread_ids) + { + if (!count($thread_ids)) { + return; + } + $attachments = self::whereIn('thread_id', $thread_ids)->get(); + + // Delete from disk + self::deleteForever($attachments); + } + + public static function deleteForever($attachments) + { // Delete from disk foreach ($attachments as $attachment) { Storage::delete($attachment->getStorageFilePath()); } // Delete from DB - self::whereIn('id', $attachment_ids)->delete(); + self::whereIn('id', $attachments->pluck('id')->toArray())->delete(); } /** diff --git a/app/Conversation.php b/app/Conversation.php index 6ac359e6..83d7b4ef 100644 --- a/app/Conversation.php +++ b/app/Conversation.php @@ -1343,6 +1343,27 @@ class Conversation extends Model \Eventy::action('conversation.status_changed', $this, $user, $changed_on_reply = false, $prev_status); } + public function deleteForever() + { + self::deleteConversationsForever([$this->id]); + } + + public static function deleteConversationsForever($conversation_ids) + { + \Eventy::action('conversations.before_delete_forever', $conversation_ids); + + //$conversation_ids = $conversations->pluck('id')->toArray(); + + // Delete attachments. + $thread_ids = Thread::whereIn('conversation_id', $conversation_ids)->pluck('id')->toArray(); + Attachment::deleteByThreadIds($thread_ids); + + // Delete threads. + Thread::whereIn('conversation_id', $conversation_ids)->delete(); + // Delete conversations. + Conversation::whereIn('id', $conversation_ids)->delete(); + } + // /** // * Get conversation meta data as array. // */ diff --git a/app/Http/Controllers/ConversationsController.php b/app/Http/Controllers/ConversationsController.php index 7741b7cd..18935cc2 100644 --- a/app/Http/Controllers/ConversationsController.php +++ b/app/Http/Controllers/ConversationsController.php @@ -1431,6 +1431,32 @@ class ConversationsController extends Controller } break; + // Delete conversation forever + case 'delete_conversation_forever': + $conversation = Conversation::find($request->conversation_id); + if (!$conversation) { + $response['msg'] = __('Conversation not found'); + } elseif (!$user->can('delete', $conversation)) { + $response['msg'] = __('Not enough permissions'); + } + + if (!$response['msg']) { + $folder_id = $conversation->getCurrentFolder(); + $mailbox = $conversation->mailbox; + + $conversation->deleteForever(); + + // Recalculate only old and new folders + $mailbox->updateFoldersCounters(); + + $response['redirect_url'] = route('mailboxes.view.folder', ['id' => $conversation->mailbox_id, 'folder_id' => $folder_id]); + + $response['status'] = 'success'; + + \Session::flash('flash_success_floating', __('Conversation deleted')); + } + break; + // Restore conversation case 'restore_conversation': $conversation = Conversation::find($request->conversation_id); diff --git a/public/js/main.js b/public/js/main.js index 59bf4107..66d42589 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -955,7 +955,7 @@ function initConversation() }); // Delete conversation - jQuery(".conv-delete").click(function(e){ + jQuery(".conv-delete,.conv-delete-forever").click(function(e){ var confirm_html = '
'+ '
'+ '
'+Lang.get("messages.confirm_delete_conversation")+'
'+ @@ -966,13 +966,18 @@ function initConversation() '
'+ '
'; + var action = 'delete_conversation'; + if ($(this).hasClass('conv-delete-forever')) { + action = 'delete_conversation_forever'; + } + showModalDialog(confirm_html, { on_show: function(modal) { modal.children().find('.delete-conversation-ok:first').click(function(e) { modal.modal('hide'); fsAjax( { - action: 'delete_conversation', + action: action, conversation_id: getGlobalAttr('conversation_id') }, laroute.route('conversations.ajax'), diff --git a/resources/views/conversations/view.blade.php b/resources/views/conversations/view.blade.php index ed2a9ca9..e424ddc2 100644 --- a/resources/views/conversations/view.blade.php +++ b/resources/views/conversations/view.blade.php @@ -31,7 +31,11 @@
  • {{ __("Move") }}
  • @endif -
  • {{ __("Delete") }}
  • + @if ($conversation->state != App\Conversation::STATE_DELETED) +
  • {{ __("Delete") }}
  • + @else +
  • {{ __("Delete Forever") }}
  • + @endif @action('conversation.append_action_buttons', $conversation, $mailbox)