Пример #1
0
        public async Task <PagedResponse <PhotoContract> > GetPagedAsync([FromUri] string continuationToken)
        {
            try
            {
                _telemetryClient.TrackEvent("UserPhotoController GetPagedAsync invoked");

                var registrationReference = await ValidateAndReturnCurrentUserId();

                var user = await _repository.GetUser(null, registrationReference);

                var stream = await _repository.GetUserPhotoStream(user.UserId, continuationToken);

                return(stream);
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
Пример #2
0
        public async Task <LeaderboardContract> GetAsync([FromUri] int mostGoldCategoriesCount,
                                                         [FromUri] int mostGoldPhotosCount,
                                                         [FromUri] int mostGoldUsersCount,
                                                         [FromUri] int mostGivingUsersCount)
        {
            try
            {
                _telemetryClient.TrackEvent("LeaderboardController GetAsync invoked");

                var leaderboardContract =
                    await
                    _repository.GetLeaderboard(mostGoldCategoriesCount, mostGoldPhotosCount, mostGoldUsersCount,
                                               mostGivingUsersCount);

                return(leaderboardContract);
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
        public async Task <CategoryContract> PostAsync(string name)
        {
            try
            {
                _telemetryClient.TrackEvent("CategoryController PostAsync invoked");
                await ValidateAndReturnCurrentUserId();

                var sanitizedName = name.TrimAndReplaceMultipleWhitespaces().ToTitleCase();

                return(await _repository.CreateCategory(sanitizedName));
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }
                if (ex.Error == DataLayerError.DuplicateKeyInsert)
                {
                    throw ServiceExceptions.DuplicateCategoryException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
        public async Task <ReportContract> PostAsync(ReportContract report)
        {
            try
            {
                var registrationReference = await ValidateAndReturnCurrentUserId();

                var reportContract = await _repository.InsertReport(report, registrationReference);

                var photoContract = await _repository.GetPhoto(reportContract.ContentId);

                if (photoContract.Status == PhotoStatus.UnderReview)
                {
                    await _notificationHandler.SendPushAsync(PushNotificationPlatform.Windows,
                                                             "user:"******"Your photo has been placed under review.",
                                                             photoContract.ThumbnailUrl, photoContract.Id);
                }

                return(reportContract);
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
Пример #5
0
        public async Task <UserContract> UpdateUserProfile(UserContract user)
        {
            try
            {
                _telemetryClient.TrackEvent("UserController UpdateUserProfile invoked");

                var currentUserId = await ValidateAndReturnCurrentUserId();

                if (!currentUserId.Equals(user.RegistrationReference))
                {
                    throw ServiceExceptions.NotAllowed();
                }

                var existingUser = await _repository.UpdateUser(user);

                return(existingUser);
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
Пример #6
0
        public async Task <UserContract> GetUser()
        {
            _telemetryClient.TrackEvent("UserController GetUser invoked");

            var registrationReference = await ValidateAndReturnCurrentUserId();

            try
            {
                var currentUser = await _repository.GetUser(null, registrationReference);

                // We need to add a record for the current authenticated user
                if (currentUser.RegistrationReference == null)
                {
                    _telemetryClient.TrackEvent("No user found, creating new user");

                    currentUser = await _repository.CreateUser(registrationReference);
                }

                return(currentUser);
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
        /// <summary>
        /// Extracts the User details accessing the service as a unique id in the form
        /// of "{authprovider}:{uniqueId}" using ProviderCrednetials for the logged
        /// in user.
        /// </summary>
        /// <param name="principal">The principal accessing the service.</param>
        /// <param name="request">The HttpRequest used to access the service.</param>
        /// <returns>The unique user id.</returns>
        public async Task <string> GetCurrentUserRegistrationReferenceAsync(ClaimsPrincipal principal, HttpRequestMessage request)
        {
            string provider = principal?.FindFirst("http://schemas.microsoft.com/identity/claims/identityprovider").Value;

            ProviderCredentials creds = null;

            if (string.Equals(provider, "facebook", StringComparison.OrdinalIgnoreCase))
            {
                creds = await principal.GetAppServiceIdentityAsync <FacebookCredentials>(request);
            }
            else if (string.Equals(provider, "google", StringComparison.OrdinalIgnoreCase))
            {
                creds = await principal.GetAppServiceIdentityAsync <GoogleCredentials>(request);
            }
            else if (string.Equals(provider, "twitter", StringComparison.OrdinalIgnoreCase))
            {
                creds = await principal.GetAppServiceIdentityAsync <TwitterCredentials>(request);
            }
            else if (string.Equals(provider, "microsoftaccount", StringComparison.OrdinalIgnoreCase))
            {
                creds = await principal.GetAppServiceIdentityAsync <MicrosoftAccountCredentials>(request);
            }

            if (creds == null)
            {
                throw ServiceExceptions.UserNullException();
            }

            // Format user details in the desired form of {authprovider}:{uniqueId}
            string authProvider   = creds.Provider;
            string uniqueId       = creds.UserClaims.FirstOrDefault(c => c.Type.Equals(ClaimTypes.NameIdentifier))?.Value;
            var    uniqueUserName = $"{authProvider}:{uniqueId}";

            return(uniqueUserName);
        }
Пример #8
0
        public async Task <PhotoContract> PutAsync(PhotoContract photo)
        {
            try
            {
                _telemetryClient.TrackEvent("Auth: PhotoController PutAsync invoked");

                var registrationReference = await ValidateAndReturnCurrentUserId();

                if (!await _photoValidation.IsUserPhotoOwner(registrationReference, photo.Id))
                {
                    throw ServiceExceptions.NotAllowed();
                }

                return(await _repository.UpdatePhoto(photo));
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
Пример #9
0
        public async Task <AnnotationContract> PostAsync(AnnotationContract annotation)
        {
            try
            {
                _telemetryClient.TrackEvent("AnnotationController PostAsync invoked");
                await ValidateAndReturnCurrentUserId();

                // Get the Gold gifting user.
                var fromUser = await _repository.GetUser(annotation.From.UserId);

                // Check to see if the gifting user has enough of a balance to support gift.
                if ((fromUser.GoldBalance - annotation.GoldCount) < 0)
                {
                    throw ServiceExceptions.UserBalanceTooLow();
                }

                var insertedAnnotation = await _repository.InsertAnnotation(annotation);

                var photoContract = await _repository.GetPhoto(annotation.PhotoId);

                var receiver = await _repository.GetUser(photoContract.User.UserId);

                var goldBalance = receiver.GoldBalance;

                try
                {
                    _telemetryClient.TrackEvent("Gold received Push notification invoked.");

                    // Send push notification to the user receiving Gold
                    await
                    _notificationHandler.SendPushAsync(PushNotificationPlatform.Windows,
                                                       "user:"******"You have received GOLD!",
                                                       photoContract.ThumbnailUrl, annotation.PhotoId, goldBalance);
                }
                catch (Exception e)
                {
                    _telemetryClient.TrackException(e);
                }

                return(insertedAnnotation);
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
Пример #10
0
        public ServiceExceptionReport(XElement node, string ns) : this()
        {
            var att = node.Attribute(XName.Get("version"));

            if (att != null)
            {
                Version = att.Value;
            }

            foreach (var serviceException in node.Elements(XName.Get("ServiceException", ns)))
            {
                ServiceExceptions.Add(new ServiceException(serviceException, ns));
            }
        }
Пример #11
0
        private void Clear()
        {
            ServiceExceptions?.Clear();

            IsOnlyDaily = false;

            Currently?.Dispose();
            Currently = null;

            if (Daily != null)
            {
                foreach (var daily in Daily)
                {
                    daily?.Dispose();
                }
                Daily = null;
            }
        }
        public async Task <ReportContract> PostAsync(ReportContract report)
        {
            try
            {
                var registrationReference = await ValidateAndReturnCurrentUserId();

                return(await _repository.InsertReport(report, registrationReference));
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
Пример #13
0
        public async Task <PhotoContract> GetAsync(string id)
        {
            try
            {
                _telemetryClient.TrackEvent("PhotoController GetAsync invoked");

                return(await _repository.GetPhoto(id));
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
Пример #14
0
        public async Task <UserContract> UpdateUserProfile(UserContract user)
        {
            try
            {
                _telemetryClient.TrackEvent("UserController UpdateUserProfile invoked");

                var currentUserId = await ValidateAndReturnCurrentUserId();

                // A user should only be able to update his/her own profile.
                if (!currentUserId.Equals(user.RegistrationReference))
                {
                    throw ServiceExceptions.NotAllowed();
                }

                // Check if the user owns the photo that is passed in as profile photo
                var photo = await _repository.GetPhoto(user.ProfilePhotoId);

                if (!photo.User.UserId.Equals(user.UserId))
                {
                    throw ServiceExceptions.NotAllowed();
                }

                // Refreshing profile photo url
                user.ProfilePhotoUrl = photo.ThumbnailUrl;

                var existingUser = await _repository.UpdateUser(user);

                return(existingUser);
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
        public async Task <IList <PhotoContract> > GetAsync([FromUri] int count)
        {
            try
            {
                _telemetryClient.TrackEvent("HeroPhotoController GetAsync invoked ");
                var daysOld = int.Parse(WebConfigurationManager.AppSettings["HeroImagesDaysOld"]);

                return(await _repository.GetHeroPhotos(count, daysOld));
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
Пример #16
0
        public async Task DeleteAsync(string id)
        {
            try
            {
                _telemetryClient.TrackEvent("PhotoController DeleteAsync invoked");
                var registrationReference = await ValidateAndReturnCurrentUserId();

                await _repository.DeletePhoto(id, registrationReference);
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
        public async Task <PagedResponse <PhotoContract> > GetAsync(string id, [FromUri] string continuationToken = null)
        {
            try
            {
                _telemetryClient.TrackEvent("CategoryController GetAsync(id, continuation) invoked");

                var stream = await _repository.GetCategoryPhotoStream(id, continuationToken);

                return(stream);
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
        public async Task <IList <CategoryContract> > GetAsync()
        {
            try
            {
                _telemetryClient.TrackEvent("CategoryController GetAsync invoked");

                var categories = await _repository.GetCategories();

                return(categories);
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
        public async Task <IList <CategoryPreviewContract> > GetAsync([FromUri] int numberOfThumbnails)
        {
            try
            {
                _telemetryClient.TrackEvent("CategoryController GetAsync(numberOfThumbnails) invoked");
                var categoriesPreviewList = await _repository.GetCategoriesPreview(numberOfThumbnails);

                // Order by categories with the latest photo added.
                IList <CategoryPreviewContract> orderedCategoriesPreviewList =
                    categoriesPreviewList.OrderByDescending(x => x.PhotoThumbnails[0].CreatedAt).ToList();
                return(orderedCategoriesPreviewList);
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
Пример #20
0
        public async Task <PhotoContract> PostAsync(PhotoContract photo)
        {
            try
            {
                _telemetryClient.TrackEvent("PhotoController PostAsync invoked");
                await ValidateAndReturnCurrentUserId();

                int goldIncrement;
                int.TryParse(WebConfigurationManager.AppSettings["NewPhotoAward"], out goldIncrement);

                return(await _repository.InsertPhoto(photo, goldIncrement));
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
        public async Task <UserContract> PostAsync(StringWrapper receipt)
        {
            _telemetryClient.TrackEvent("IapController PostAsync invoked");

            if (receipt == null)
            {
                throw ServiceExceptions.IapValidationException("IapController: Receipt is null");
            }

            var registrationReference = await ValidateAndReturnCurrentUserId();

            UserContract returnUser;

            try
            {
                int goldValue;

                var xmlReceipt = new XmlDocument();
                xmlReceipt.LoadXml(receipt.Data);

                var iapReceipt = _iapValidator.ValidateXmlSignature(xmlReceipt);

                var user = await _repository.GetUser(null, registrationReference);

                iapReceipt.UserId = user.UserId;

                var productKey = "Iap";

                if (!string.IsNullOrEmpty(iapReceipt.ProductId))
                {
                    productKey = "Iap" + iapReceipt.ProductId.Trim();
                    int.TryParse(WebConfigurationManager.AppSettings[productKey], out goldValue);
                }
                else
                {
                    throw ServiceExceptions.IapValidationException(
                              $"IapController: Product not found in configuration: {productKey}");
                }

                iapReceipt.GoldIncrement = goldValue;

                returnUser = await _repository.InsertIapPurchase(iapReceipt);
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
            catch (IapValidationException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == IapValidationError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.IapValidationException(ex.Message);
            }

            return(returnUser);
        }