changeset 20892:111ca700d6bd

push-notification: Add unseen count to OX driver.
author Steffen Templin <steffen.templin@open-xchange.com>
date Wed, 05 Oct 2016 13:28:49 +0300
parents d2872ec409ec
children 252651306e8c
files src/plugins/push-notification/push-notification-driver-ox.c
diffstat 1 files changed, 65 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/push-notification/push-notification-driver-ox.c	Mon Oct 10 20:38:31 2016 +0300
+++ b/src/plugins/push-notification/push-notification-driver-ox.c	Wed Oct 05 13:28:49 2016 +0300
@@ -315,6 +315,58 @@
     str_free(&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;
+    struct mailbox *box;
+    struct mailbox_transaction_context *mctx = NULL;
+    bool use_existing_txn = FALSE;
+
+    /* 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);
+        }
+    }
+
+    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. */
+        }
+    } else {
+        i_error(OX_LOG_LABEL "Begin mailbox transaction failed: %s", msg->mailbox);
+        return -1;
+    }
+
+    if (!use_existing_txn) {
+        mailbox_free(&box);
+    }
+
+    return 0;
+}
+
+
 static void push_notification_driver_ox_process_msg
 (struct push_notification_driver_txn *dtxn,
  struct push_notification_txn_msg *msg)
@@ -328,6 +380,12 @@
     struct push_notification_driver_ox_txn *txn =
         (struct push_notification_driver_ox_txn *)dtxn->context;
     struct mail_user *user = dtxn->ptxn->muser;
+    struct mailbox_status box_status;
+    bool status_success = TRUE;
+
+    if (push_notification_driver_ox_get_mailbox_status(dtxn, msg, &box_status) < 0) {
+        status_success = FALSE;
+    }
 
     messagenew = push_notification_txn_msg_get_eventdata(msg, "MessageNew");
     if (messagenew == NULL) {
@@ -364,7 +422,13 @@
 	str_append(str, "\",\"snippet\":\"");
 	json_append_escaped(str, messagenew->snippet);
     }
-    str_append(str, "\"}");
+    if (status_success) {
+    /* The current mail isn't counted yet, therefore +1. */
+    str_printfa(str, "\",\"unseen\":%u", box_status.unseen + 1);
+    } else {
+        str_append(str, "\"");
+    }
+    str_append(str, "}");
 
     push_notification_driver_debug(OX_LOG_LABEL, user,
                                    "Sending notification: %s", str_c(str));