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 = '