Skip to content

Commit

Permalink
push-notification: Re-open mailbox and sync it to get unseen count.
Browse files Browse the repository at this point in the history
  • Loading branch information
Steffen Templin authored and GitLab committed Oct 17, 2016
1 parent 5e55914 commit 282323a
Showing 1 changed file with 19 additions and 41 deletions.
60 changes: 19 additions & 41 deletions src/plugins/push-notification/push-notification-driver-ox.c
Expand Up @@ -300,53 +300,32 @@ static void str_free_i(string_t *str)

static int push_notification_driver_ox_get_mailbox_status
(struct push_notification_driver_txn *dtxn,
struct push_notification_txn_msg *msg,
struct mailbox_status *r_box_status)
{
struct mail_user *user = dtxn->ptxn->muser;
struct mail_namespace *ns;
/* The already opened mailbox. We cannot use or sync it, because we are within a save transaction. */
struct mailbox *mbox = dtxn->ptxn->mbox;
struct mailbox *box;
struct mailbox_transaction_context *mctx = NULL;
bool use_existing_txn = FALSE;
int ret;

/* Get current mailbox */
if ((dtxn->ptxn->t != NULL) && dtxn->ptxn->mbox->inbox_user) {
/* Use the currently open transaction. */
box = dtxn->ptxn->mbox;
mctx = dtxn->ptxn->t;
use_existing_txn = TRUE;
} else {
ns = mail_namespace_find(user->namespaces, msg->mailbox);
box = mailbox_alloc(ns->list, msg->mailbox, MAILBOX_FLAG_READONLY);
if (mailbox_open(box) < 0) {
i_error(OX_LOG_LABEL "Unable to open mailbox to get status: %s",
mailbox_get_last_error(box, NULL));
return -1;
} else {
mctx = mailbox_transaction_begin(box, 0);
}
/* open and sync new instance of the same mailbox to get most recent status */
box = mailbox_alloc(mailbox_get_namespace(mbox)->list, mailbox_get_name(mbox), MAILBOX_FLAG_READONLY);
mailbox_open(box);
if (mailbox_sync(box, 0) < 0) {
i_warning("mailbox_sync(%s): %s", mailbox_get_vname(mbox), mailbox_get_last_error(box, NULL));
}

if (mctx != NULL) {
mailbox_get_open_status(box, STATUS_UNSEEN, r_box_status);
push_notification_driver_debug(OX_LOG_LABEL, user, "Got mailbox status: %s",
mailbox_get_name(box));

if (!use_existing_txn && (mailbox_transaction_commit(&mctx) < 0)) {
i_error(OX_LOG_LABEL "Transaction commit failed: %s",
mailbox_get_last_error(box, NULL));
/* the commit doesn't matter though. */
}
/* only 'unseen' is needed at the moment */
ret = mailbox_get_status(box, STATUS_UNSEEN, r_box_status);
if (ret < 0) {
i_error("Could not get mailbox '%s' status: %s",
mailbox_get_vname(box), mailbox_get_last_error(box, NULL));
} else {
i_error(OX_LOG_LABEL "Begin mailbox transaction failed: %s", msg->mailbox);
return -1;
push_notification_driver_debug(OX_LOG_LABEL, dtxn->ptxn->muser, "Got status of mailbox '%s': (unseen: %u)",
mailbox_get_vname(box), r_box_status->unseen);
}

if (!use_existing_txn) {
mailbox_free(&box);
}

return 0;
mailbox_free(&box);
return ret;
}


Expand All @@ -366,7 +345,7 @@ static void push_notification_driver_ox_process_msg
struct mailbox_status box_status;
bool status_success = TRUE;

if (push_notification_driver_ox_get_mailbox_status(dtxn, msg, &box_status) < 0) {
if (push_notification_driver_ox_get_mailbox_status(dtxn, &box_status) < 0) {
status_success = FALSE;
}

Expand Down Expand Up @@ -406,8 +385,7 @@ static void push_notification_driver_ox_process_msg
json_append_escaped(str, messagenew->snippet);
}
if (status_success) {
/* The current mail isn't counted yet, therefore +1. */
str_printfa(str, "\",\"unseen\":%u", box_status.unseen + 1);
str_printfa(str, "\",\"unseen\":%u", box_status.unseen);
} else {
str_append(str, "\"");
}
Expand Down

0 comments on commit 282323a

Please sign in to comment.