public async Task <IHttpActionResult> FetchStatisticsAsync()
        {
            var cookies = Request.Headers.GetCookies("session").FirstOrDefault();

            if (cookies == null)
            {
                return(JsonResponseEx.Create(HttpStatusCode.Unauthorized, new { message = "Session cookie is missing." }));
            }
            var sessionCookieValue = cookies["session"].Values;
            var account            = await AuthorizationController.AccountFromCookie(sessionCookieValue, false);

            if (null == account)
            {
                return(JsonResponseEx.Create(HttpStatusCode.Unauthorized, new { message = "Failed to locate an account for the auth cookie." }));
            }

            var client = await SharedConfig.GetOneDriveClientForAccountAsync(account);

            var cameraRollFolder = await client.Drive.Special["cameraroll"].Request().GetAsync();


            var responseObj = new
            {
                itemCount    = cameraRollFolder.Folder.ChildCount,
                totalSize    = cameraRollFolder.Size,
                lastModified = cameraRollFolder.LastModifiedDateTime
            };

            return(JsonResponseEx.Create(HttpStatusCode.OK, responseObj));
        }
        public async Task <IHttpActionResult> CreateFile()
        {
            var cookies = Request.Headers.GetCookies("session").FirstOrDefault();

            if (cookies == null)
            {
                return(JsonResponseEx.Create(HttpStatusCode.Unauthorized, new { message = "Session cookie is missing." }));
            }
            var sessionCookieValue = cookies["session"].Values;
            var account            = await AuthorizationController.AccountFromCookie(sessionCookieValue, false);

            if (null == account)
            {
                return(JsonResponseEx.Create(HttpStatusCode.Unauthorized, new { message = "Failed to locate an account for the auth cookie." }));
            }

            var client = await SharedConfig.GetOneDriveClientForAccountAsync(account);

            var item = await client.Drive.Special[account.SourceFolder].ItemWithPath("test_file.txt").Content.Request().PutAsync <Item>(this.TestFileStream());

            await AzureStorage.InsertActivityAsync(
                new Activity
            {
                UserId  = account.Id,
                Type    = ActivityEventCode.FileChanged,
                Message = string.Format("Creating test file test_file.txt with resource id: {0}", item.Id)
            });

            return(JsonResponseEx.Create(HttpStatusCode.OK, item));
        }
 public IHttpActionResult LastWebhook()
 {
     if (LastWebhookReceived != null)
     {
         return(JsonResponseEx.Create(HttpStatusCode.OK, LastWebhookReceived));
     }
     else
     {
         return(NotFound());
     }
 }
        public async Task <IHttpActionResult> AuthRedirect(string code)
        {
            // Redeem authorization code for account information

            OAuthHelper helper = new OAuthHelper(SharedConfig.TokenService,
                                                 SharedConfig.AppClientID,
                                                 SharedConfig.AppClientSecret,
                                                 SharedConfig.RedirectUri);

            var token = await helper.RedeemAuthorizationCodeAsync(code);

            if (null == token)
            {
                return(JsonResponseEx.Create(HttpStatusCode.InternalServerError, new { message = "Invalid response from token service.", code = "tokenServiceNullResponse" }));
            }

            Account account = new Account(token);
            var     client  = await SharedConfig.GetOneDriveClientForAccountAsync(account);

            var rootDrive = await client.Drive.Request().GetAsync();

            account.Id          = rootDrive.Id;
            account.DisplayName = rootDrive.Owner.User.DisplayName;

            var existingAccount = await AzureStorage.LookupAccountAsync(rootDrive.Id);

            if (null == existingAccount)
            {
                await AzureStorage.InsertAccountAsync(account);

                existingAccount = account;
            }
            else
            {
                existingAccount.SetTokenResponse(token);
                await AzureStorage.UpdateAccountAsync(existingAccount);
            }

            var authCookie = CookieForAccount(existingAccount);

            return(RedirectResponse.Create("/default.aspx", authCookie));
        }
        public async Task <IHttpActionResult> RecentActivity(string cid = null)
        {
            var cookies = Request.Headers.GetCookies("session").FirstOrDefault();

            if (cookies == null)
            {
                return(JsonResponseEx.Create(HttpStatusCode.Unauthorized, new { message = "Session cookie is missing." }));
            }
            var sessionCookieValue = cookies["session"].Values;
            var account            = await AuthorizationController.AccountFromCookie(sessionCookieValue, false);

            if (null == account)
            {
                return(JsonResponseEx.Create(HttpStatusCode.Unauthorized, new { message = "Failed to locate an account for the auth cookie." }));
            }

            var activity = await AzureStorage.RecentActivityAsync(cid ?? account.Id);

            return(JsonResponseEx.Create(HttpStatusCode.OK, new { value = activity }));
        }
        public async Task <IHttpActionResult> CreateTestWebhook()
        {
            var cookies = Request.Headers.GetCookies("session").FirstOrDefault();

            if (cookies == null)
            {
                return(JsonResponseEx.Create(HttpStatusCode.Unauthorized, new { message = "Session cookie is missing." }));
            }
            var sessionCookieValue = cookies["session"].Values;
            var account            = await AuthorizationController.AccountFromCookie(sessionCookieValue, false);

            if (null == account)
            {
                return(JsonResponseEx.Create(HttpStatusCode.Unauthorized, new { message = "Failed to locate an account for the auth cookie." }));
            }

            OneDriveNotification notification = new OneDriveNotification {
                UserId = account.Id
            };
            await AzureStorage.AddToPendingSubscriptionQueueAsync(notification);

            return(Ok());
        }