Example #1
0
        // Deletes the notification subscription for the user
        public static async Task UnsubscribeForInboxUpdates(string userId)
        {
            var idFilter = Builders <User> .Filter.Eq("Id", new ObjectId(userId));

            var user = await userCollection.Find(idFilter).FirstOrDefaultAsync();

            if (null != user && !string.IsNullOrEmpty(user.SubscriptionId))
            {
                OutlookHelper outlook = new OutlookHelper();
                outlook.AnchorMailbox = user.Email;

                await outlook.DeleteSubscription(user.Email, user.AccessToken, user.SubscriptionId);

                var update = Builders <User> .Update.Set("SubscriptionId", string.Empty)
                             .Set("SubscriptionExpires", DateTime.UtcNow);

                var result = await userCollection.UpdateOneAsync(idFilter, update);
            }
        }
Example #2
0
        // Creates a notification subscription on the user's inbox and
        // saves the subscription ID on the user in the database
        public static async Task SubscribeForInboxUpdates(string userId, string notificationUrl)
        {
            var idFilter = Builders <User> .Filter.Eq("Id", new ObjectId(userId));

            var user = await userCollection.Find(idFilter).FirstOrDefaultAsync();

            if (null != user && string.IsNullOrEmpty(user.SubscriptionId))
            {
                OutlookHelper outlook = new OutlookHelper();
                outlook.AnchorMailbox = user.Email;

                JObject subscription = await outlook.CreateInboxSubscription(user.Email, user.AccessToken, notificationUrl);

                var update = Builders <User> .Update.Set("SubscriptionId", (string)subscription["Id"])
                             .Set("SubscriptionExpires", DateTime.Parse((string)subscription["SubscriptionExpirationDateTime"]));

                var result = await userCollection.UpdateOneAsync(idFilter, update);
            }
        }
Example #3
0
        public static async Task SyncUsersInbox(string userId)
        {
            User user = await GetUserById(userId);

            if (null != user)
            {
                OutlookHelper outlook = new OutlookHelper();
                outlook.AnchorMailbox = user.Email;

                // If we have no prior sync state, do an intial sync. This sync always
                // returns a deltatoken, even if there are more results on the server.
                // You must always do another sync after the initial sync.
                bool   isInitialSync = string.IsNullOrEmpty(user.SyncState);
                bool   syncComplete  = false;
                string newSyncState  = user.SyncState;
                string newSkipToken  = string.Empty;

                while (!syncComplete)
                {
                    var syncResults = await outlook.SyncInbox(user.Email, user.AccessToken, newSyncState, newSkipToken);

                    // Do updates (add/delete/update)
                    await ParseSyncItems(user.Id, (JArray)syncResults["value"]);

                    // Is there a skip token?
                    if (null != syncResults["@odata.nextLink"])
                    {
                        // There are more results, need to call again
                        // nextLink is a URL which has a $skiptoken parameter
                        string nextLink = (string)syncResults["@odata.nextLink"];
                        string query    = new UriBuilder(nextLink).Query;
                        NameValueCollection queryParams = HttpUtility.ParseQueryString(query);

                        if (string.IsNullOrEmpty(queryParams["$skiptoken"]))
                        {
                            throw new Exception(
                                      "Failed to find $skiptoken in nextLink value in sync response. Try resetting sync state.");
                        }

                        newSyncState = string.Empty;
                        newSkipToken = queryParams["$skiptoken"];
                    }

                    else if (null != syncResults["@odata.deltaLink"])
                    {
                        // Delta link is a URL which has a $deltatoken parameter
                        string deltaLink = (string)syncResults["@odata.deltaLink"];
                        string query     = new UriBuilder(deltaLink).Query;
                        NameValueCollection queryParams = HttpUtility.ParseQueryString(query);

                        if (string.IsNullOrEmpty(queryParams["$deltatoken"]))
                        {
                            throw new Exception(
                                      "Failed to find $deltatoken in deltaLink value in sync response. Try resetting sync state.");
                        }

                        newSkipToken = string.Empty;
                        newSyncState = queryParams["$deltatoken"];
                        if (isInitialSync)
                        {
                            isInitialSync = false;
                        }
                        else
                        {
                            syncComplete = true;
                        }
                    }
                    else
                    {
                        // No deltaLink or nextLink, something's wrong
                        throw new Exception("Failed to find either a deltaLink or nextLink value in sync response. Try resetting sync state.");
                    }
                }

                // Save new sync state to the user's record in database
                var idFilter = Builders <User> .Filter.Eq("Id", new ObjectId(userId));

                var userUpdate = Builders <User> .Update.Set("SyncState", newSyncState);

                var result = await userCollection.UpdateOneAsync(idFilter, userUpdate);
            }
        }