diff options
author | Christoph Wurst <ChristophWurst@users.noreply.github.com> | 2022-09-08 18:07:24 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-08 18:07:24 +0300 |
commit | edb7ada8fe52a90898a973b923223798409dfd50 (patch) | |
tree | a48b82b743302b464d4c4b2084b553ff298a8ff0 | |
parent | 4c16eb0bdbd80c9163c8d4f4f45fd664035cd457 (diff) | |
parent | 396fe081a324346b421771aee3fb0fa0f7718f67 (diff) |
Merge pull request #6800 from sazanof/enh/clear-folder-feature
Users can clear their mailboxes (Updated)
-rw-r--r-- | appinfo/routes.php | 5 | ||||
-rw-r--r-- | lib/Contracts/IMailManager.php | 8 | ||||
-rw-r--r-- | lib/Controller/MailboxesController.php | 19 | ||||
-rw-r--r-- | lib/Service/MailManager.php | 33 | ||||
-rw-r--r-- | src/components/NavigationMailbox.vue | 35 | ||||
-rw-r--r-- | src/service/MailboxService.js | 8 | ||||
-rw-r--r-- | src/store/actions.js | 6 | ||||
-rw-r--r-- | src/store/mutations.js | 3 |
8 files changed, 117 insertions, 0 deletions
diff --git a/appinfo/routes.php b/appinfo/routes.php index 6da97e398..03b3fcb4d 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -165,6 +165,11 @@ return [ 'verb' => 'DELETE' ], [ + 'name' => 'mailboxes#clearMailbox', + 'url' => '/api/mailboxes/{id}/clear', + 'verb' => 'POST' + ], + [ 'name' => 'mailboxes#markAllAsRead', 'url' => '/api/mailboxes/{id}/read', 'verb' => 'POST' diff --git a/lib/Contracts/IMailManager.php b/lib/Contracts/IMailManager.php index 83982a86d..27b4bd24c 100644 --- a/lib/Contracts/IMailManager.php +++ b/lib/Contracts/IMailManager.php @@ -204,6 +204,14 @@ interface IMailManager { /** * @param Account $account * @param Mailbox $mailbox + * + * @throws ServiceException + */ + public function clearMailbox(Account $account, Mailbox $mailbox): void; + + /** + * @param Account $account + * @param Mailbox $mailbox * @param bool $subscribed * * @return Mailbox diff --git a/lib/Controller/MailboxesController.php b/lib/Controller/MailboxesController.php index 230cf6c30..c0a4f17a5 100644 --- a/lib/Controller/MailboxesController.php +++ b/lib/Controller/MailboxesController.php @@ -283,4 +283,23 @@ class MailboxesController extends Controller { $this->mailManager->deleteMailbox($account, $mailbox); return new JSONResponse(); } + + /** + * @NoAdminRequired + * @TrapError + * + * @param int $id + * + * @return JSONResponse + * @throws ClientException + * @throws ServiceException + * @throws \OCP\AppFramework\Db\DoesNotExistException + */ + public function clearMailbox(int $id): JSONResponse { + $mailbox = $this->mailManager->getMailbox($this->currentUserId, $id); + $account = $this->accountService->find($this->currentUserId, $mailbox->getAccountId()); + + $this->mailManager->clearMailbox($account, $mailbox); + return new JSONResponse(); + } } diff --git a/lib/Service/MailManager.php b/lib/Service/MailManager.php index 883d98554..0f66b8b5d 100644 --- a/lib/Service/MailManager.php +++ b/lib/Service/MailManager.php @@ -584,6 +584,39 @@ class MailManager implements IMailManager { } /** + * Clear messages in folder + * + * @param Account $account + * @param Mailbox $mailbox + * + * @throws DoesNotExistException + * @throws Horde_Imap_Client_Exception + * @throws Horde_Imap_Client_Exception_NoSupportExtension + * @throws ServiceException + */ + public function clearMailbox(Account $account, + Mailbox $mailbox): void { + $client = $this->imapClientFactory->getClient($account); + $trashMailboxId = $account->getMailAccount()->getTrashMailboxId(); + $currentMailboxId = $mailbox->getId(); + try { + if (($currentMailboxId !== $trashMailboxId) && !is_null($trashMailboxId)) { + $trash = $this->mailboxMapper->findById($trashMailboxId); + $client->copy($mailbox->getName(), $trash->getName(), [ + 'move' => true + ]); + } else { + $client->expunge($mailbox->getName(), [ + 'delete' => true + ]); + } + $this->dbMessageMapper->deleteAll($mailbox); + } finally { + $client->logout(); + } + } + + /** * @param Account $account * @param Mailbox $mailbox * @param Message $message diff --git a/src/components/NavigationMailbox.vue b/src/components/NavigationMailbox.vue index fcd8c2366..0fbf8c8ce 100644 --- a/src/components/NavigationMailbox.vue +++ b/src/components/NavigationMailbox.vue @@ -169,6 +169,16 @@ {{ t('mail', 'Sync in background') }} </ActionCheckbox> + <ActionButton + v-if="mailbox.specialRole !== 'flagged' && !account.isUnified" + :close-after-click="true" + @click="clearMailbox"> + <template #icon> + <EraserVariant :size="20" /> + </template> + {{ t('mail', 'Clear mailbox') }} + </ActionButton> + <ActionButton v-if="!account.isUnified && !mailbox.specialRole && !hasSubMailboxes" @click="deleteMailbox"> <template #icon> <IconDelete @@ -220,6 +230,7 @@ import IconDraft from 'vue-material-design-icons/Pencil' import IconArchive from 'vue-material-design-icons/PackageDown' import IconInbox from 'vue-material-design-icons/Home' import IconAllInboxes from 'vue-material-design-icons/InboxMultiple' +import EraserVariant from 'vue-material-design-icons/EraserVariant' import ImportantIcon from './icons/ImportantIcon' import IconSend from 'vue-material-design-icons/Send' import IconLoading from '@nextcloud/vue/dist/Components/NcLoadingIcon' @@ -258,6 +269,7 @@ export default { IconDraft, IconArchive, IconInbox, + EraserVariant, ImportantIcon, IconLoading, MoveMailboxModal, @@ -532,6 +544,29 @@ export default { this.clearCache = false } }, + clearMailbox() { + const id = this.mailbox.databaseId + OC.dialogs.confirmDestructive( + t('mail', 'All messages in mailbox will be deleted.'), + t('mail', 'Clear mailbox {name}', { name: this.mailbox.displayName }), + { + type: OC.dialogs.YES_NO_BUTTONS, + confirm: t('mail', 'Clear mailbox'), + confirmClasses: 'error', + cancel: t('mail', 'Cancel'), + }, + (result) => { + if (result) { + return this.$store + .dispatch('clearMailbox', { mailbox: this.mailbox }) + .then(() => { + logger.info(`mailbox ${id} cleared`) + }) + .catch((error) => logger.error('could not clear mailbox', { error })) + } + } + ) + }, deleteMailbox() { const id = this.mailbox.databaseId logger.info('delete mailbox', { mailbox: this.mailbox }) diff --git a/src/service/MailboxService.js b/src/service/MailboxService.js index 7c6c82df8..b0305cab5 100644 --- a/src/service/MailboxService.js +++ b/src/service/MailboxService.js @@ -54,3 +54,11 @@ export async function patchMailbox(id, data) { const response = await axios.patch(url, data) return response.data } + +export const clearMailbox = async (id) => { + const url = generateUrl('/apps/mail/api/mailboxes/{id}/clear', { + id, + }) + + await axios.post(url) +} diff --git a/src/store/actions.js b/src/store/actions.js index a221c92cc..05ec0107c 100644 --- a/src/store/actions.js +++ b/src/store/actions.js @@ -55,6 +55,7 @@ import { } from '../service/AccountService' import { create as createMailbox, + clearMailbox, deleteMailbox, fetchAll as fetchAllMailboxes, markMailboxRead, @@ -195,6 +196,11 @@ export default { await deleteMailbox(mailbox.databaseId) commit('removeMailbox', { id: mailbox.databaseId }) }, + async clearMailbox({ commit }, { mailbox }) { + await clearMailbox(mailbox.databaseId) + commit('removeEnvelopes', { id: mailbox.databaseId }) + commit('setMailboxUnreadCount', { id: mailbox.databaseId }) + }, async createMailbox({ commit }, { account, name }) { const prefixed = (account.personalNamespace && !name.startsWith(account.personalNamespace)) ? account.personalNamespace + name diff --git a/src/store/mutations.js b/src/store/mutations.js index 0d2af8254..b23931edc 100644 --- a/src/store/mutations.js +++ b/src/store/mutations.js @@ -345,6 +345,9 @@ export default { Vue.delete(state.envelopes, id) }, + removeEnvelopes(state, { id }) { + Vue.set(state.mailboxes[id], 'envelopeLists', []) + }, addMessage(state, { message }) { Vue.set(state.messages, message.databaseId, message) }, |