Skip to content

Commit

Permalink
Allow toggeling of subscription status of mailboxes
Browse files Browse the repository at this point in the history
Co-authored-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Signed-off-by: Holger Dehnhardt <holger@dehnhardt.org>
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
  • Loading branch information
dehnhardt and ChristophWurst committed Sep 2, 2020
1 parent 81831da commit 367f051
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 1 deletion.
13 changes: 13 additions & 0 deletions lib/Contracts/IMailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,17 @@ public function renameMailbox(Account $account, Mailbox $mailbox, string $name):
* @throws ServiceException
*/
public function deleteMailbox(Account $account, Mailbox $mailbox): void;

/**
* @param Account $account
* @param string $mailbox
* @param bool $subscribed
*
* @return Mailbox
* @throws ClientException
* @throws ServiceException
*/
public function updateSubscription(Account $account,
Mailbox $mailbox,
bool $subscribed): Mailbox;
}
10 changes: 9 additions & 1 deletion lib/Controller/MailboxesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ public function index(int $accountId): JSONResponse {
* @return JSONResponse
*/
public function patch(int $id,
?string $name = null): JSONResponse {
?string $name = null,
?bool $subscribed = null): JSONResponse {
$mailbox = $this->mailManager->getMailbox($this->currentUserId, $id);
$account = $this->accountService->find($this->currentUserId, $mailbox->getAccountId());

Expand All @@ -119,6 +120,13 @@ public function patch(int $id,
$name
);
}
if ($subscribed !== null) {
$mailbox = $this->mailManager->updateSubscription(
$account,
$mailbox,
$subscribed
);
}

return new JSONResponse($mailbox);
}
Expand Down
22 changes: 22 additions & 0 deletions lib/Service/MailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,28 @@ public function markFolderAsRead(Account $account, Mailbox $mailbox): void {
$this->imapMessageMapper->markAllRead($client, $mailbox->getName());
}

public function updateSubscription(Account $account, Mailbox $mailbox, bool $subscribed): Mailbox {
/**
* 1. Change subscription on IMAP
*/
$client = $this->imapClientFactory->getClient($account);
try {
$client->subscribeMailbox($mailbox->getName(), $subscribed);
} catch (Horde_Imap_Client_Exception $e) {
throw new ServiceException("Could not set subscription status for mailbox $mailbox on IMAP: " . $e->getMessage(), $e->getCode(), $e);
}

/**
* 2. Pull changes into the mailbox database cache
*/
$this->mailboxSync->sync($account, true);

/**
* 3. Return the updated object
*/
return $this->mailboxMapper->find($account, $mailbox->getName());
}

public function flagMessage(Account $account, string $mailbox, int $uid, string $flag, bool $value): void {
$client = $this->imapClientFactory->getClient($account);
try {
Expand Down
33 changes: 33 additions & 0 deletions src/components/NavigationMailbox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@
@click="clearCache">
{{ t('mail', 'Clear locally cached data, in case there are issues with synchronization.') }}
</ActionButton>

<ActionCheckbox
v-if="notVirtual"
:checked="isSubscribed"
:disabled="changeSubscription"
@update:checked="changeFolderSubscription">
{{ t('mail', 'Subscribed') }}
</ActionCheckbox>

<ActionButton v-if="!account.isUnified && !mailbox.specialRole && !hasSubMailboxes" icon="icon-delete" @click="deleteMailbox">
{{ t('mail', 'Delete folder') }}
</ActionButton>
Expand All @@ -103,6 +112,7 @@
import AppNavigationItem from '@nextcloud/vue/dist/Components/AppNavigationItem'
import AppNavigationCounter from '@nextcloud/vue/dist/Components/AppNavigationCounter'
import ActionButton from '@nextcloud/vue/dist/Components/ActionButton'
import ActionCheckbox from '@nextcloud/vue/dist/Components/ActionCheckbox'
import ActionInput from '@nextcloud/vue/dist/Components/ActionInput'
import ActionText from '@nextcloud/vue/dist/Components/ActionText'
Expand All @@ -120,6 +130,7 @@ export default {
AppNavigationCounter,
ActionText,
ActionButton,
ActionCheckbox,
ActionInput,
},
props: {
Expand Down Expand Up @@ -148,6 +159,7 @@ export default {
loadingMarkAsRead: false,
clearingCache: false,
showSaving: false,
changeSubscription: false,
editing: false,
showSubMailboxes: false,
menuOpen: false,
Expand All @@ -164,6 +176,9 @@ export default {
|| (this.mailbox.attributes && this.mailbox.attributes.includes('\\subscribed'))
)
},
notVirtual() {
return !this.account.isUnified && this.mailbox.specialRole !== 'flagged'
},
title() {
if (this.filter === 'starred') {
// Little hack to trick the translation logic into a different path
Expand Down Expand Up @@ -218,6 +233,9 @@ export default {
}
return t('mail', 'Loading …')
},
isSubscribed() {
return this.mailbox.attributes && this.mailbox.attributes.includes('\\subscribed')
},
},
methods: {
/**
Expand Down Expand Up @@ -295,6 +313,21 @@ export default {
.catch((error) => logger.error(`could not mark mailbox ${this.mailbox.databaseId} as read`, { error }))
.then(() => (this.loadingMarkAsRead = false))
},
async changeFolderSubscription(subscribed) {
try {
this.changeSubscription = true
await this.$store.dispatch('changeMailboxSubscription', {
mailbox: this.mailbox,
subscribed,
})
} catch (error) {
logger.error(`could not update subscription of mailbox ${mailbox.databaseId}`, { error })
throw error
} finally {
this.changeSubscription = false
}
},
async clearCache() {
try {
this.clearingCache = true
Expand Down
15 changes: 15 additions & 0 deletions src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,21 @@ export default {
})
)
},
async changeMailboxSubscription({ commit }, { mailbox, subscribed }) {
logger.debug(`toggle subscription for mailbox ${mailbox.databaseId}`, {
mailbox,
subscribed,
})
const updated = await patchMailbox(mailbox.databaseId, { subscribed })

commit('updateMailbox', {
mailbox: updated,
})
logger.debug(`subscription for mailbox ${mailbox.databaseId} updated`, {
mailbox,
updated,
})
},
fetchEnvelope({ commit, getters }, id) {
const cached = getters.getEnvelope(id)
if (cached) {
Expand Down
3 changes: 3 additions & 0 deletions src/store/mutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ export default {
addMailbox(state, { account, mailbox }) {
addMailboxToState(state, account, mailbox)
},
updateMailbox(state, { mailbox }) {
Vue.set(state.mailboxes, mailbox.databaseId, mailbox)
},
removeMailbox(state, { id }) {
const mailbox = state.mailboxes[id]
if (mailbox === undefined) {
Expand Down

0 comments on commit 367f051

Please sign in to comment.