예제 #1
0
        private void CheckProject(string userId, ProjectEntity projectEntity)
        {
            if (projectEntity == null)
            {
                throw new NotFoundException();
            }

            if (!_projectAccess.Contains(projectEntity.Access))
            {
                if (string.IsNullOrEmpty(userId))
                {
                    throw new UnauthorizedException();
                }

                if (projectEntity.UserId != userId)
                {
                    throw new ForbiddenException();
                }
            }
        }
예제 #2
0
        private void CheckProject(ProjectEntity projectEntity, string userId)
        {
            if (projectEntity == null)
            {
                throw new NotFoundException();
            }

            // If comments are disabled returns forbidden
            // for both own and other user projects
            if (!projectEntity.EnableComments)
            {
                throw new ForbiddenException();
            }

            // If it's own project skip access checking
            if (projectEntity.UserId == userId)
            {
                return;
            }

            // If it's other user project check access
            if (!_projectAccess.Contains(projectEntity.Access))
            {
                throw new ForbiddenException();
            }
        }
예제 #3
0
        private Watch AggregateProject(ProjectEntity projectEntity, string userId, Dictionary<string, UserEntity> users, Dictionary<string, int> commentsCounts,
            Dictionary<string, List<CommentEntity>> comments)
        {
            DomainProject project = _mapper.Map<ProjectEntity, DomainProject>(projectEntity);

            // 1. Aggregate Data

            UserEntity projectUser = users.ContainsKey(project.UserId) ? users[project.UserId] : null;
            int commentsCount = commentsCounts.ContainsKey(project.Id) ? commentsCounts[project.Id] : 0;
            List<CommentEntity> projectComments = comments.ContainsKey(project.Id) ? comments[project.Id] : new List<CommentEntity>();


            // Processed Videos
            var watchVideos = new List<WatchVideo>();
            watchVideos.AddRange(
                from @group in project.EncodedVideos.GroupBy(q => q.Width)
                select @group.ToList()
                into processedVideos
                where processedVideos.Count == 2
                from v in processedVideos
                select new WatchVideo
                {
                    ContentType = v.ContentType,
                    Width = v.Width,
                    Height = v.Height,
                    Uri = _uriProvider.CreateUri(v.FileId)
                });

            // Processed Screenshots
            List<WatchScreenshot> watchScreenshots = project.EncodedScreenshots.Select(
                s => new WatchScreenshot
                {
                    ContentType = s.ContentType,
                    Uri = _uriProvider.CreateUri(s.FileId)
                }).ToList();

            // External Video
            ExternalVideo externalVideo = !string.IsNullOrEmpty(project.VideoSource)
                ? new ExternalVideo
                {
                    ProductName = project.VideoSourceProductName,
                    VideoUri = project.VideoSource,
                    AcsNamespace = _settings.AcsNamespace
                }
                : null;


            // 2. Calculate Video State
            WatchState state;
            if (string.IsNullOrEmpty(project.AvsxFileId) || (string.IsNullOrEmpty(project.OriginalVideoFileId) && externalVideo == null))
            {
                state = WatchState.Uploading;
            }
            else if (externalVideo == null && watchVideos.Count == 0)
            {
                state = WatchState.Encoding;
            }
            else
            {
                state = WatchState.Ready;
            }

            // 3. Map comments
            var watchComments = new List<Comment>();
            foreach (CommentEntity projectComment in projectComments)
            {
                UserEntity commentAuthor = users.ContainsKey(projectComment.UserId) ? users[projectComment.UserId] : null;
                DomainComment domainComment = _mapper.Map<Tuple<CommentEntity, UserEntity>, DomainComment>(new Tuple<CommentEntity, UserEntity>(projectComment, commentAuthor));

                Comment comment = _mapper.Map<DomainComment, Comment>(domainComment);
                if (commentAuthor != null)
                {
                    comment.AvatarUrl = _userAvatarProvider.GetAvatar(commentAuthor.Email);
                }

                watchComments.Add(comment);
            }
            watchComments.Reverse();

            // Result
            return new Watch
            {
                Name = project.Name,
                UserId = project.UserId,
                UserName = projectUser != null ? projectUser.Name : null,
                UserAvatarUrl = projectUser != null ? _userAvatarProvider.GetAvatar(new DomainUser { Email = projectUser.Email }) : null,
                Description = project.Description,
                Created = project.Created,
                Avsx = !string.IsNullOrEmpty(project.AvsxFileId) ? _uriProvider.CreateUri(project.AvsxFileId) : null,
                Screenshots = watchScreenshots,
                Videos = watchVideos,
                PublicUrl = _projectUriProvider.GetUri(project.Id),
                Id = project.Id,
                Access = project.Access,
                IsEditable = project.UserId == userId,
                HitsCount = project.HitsCount,
                External = externalVideo,
                ScreenshotUrl = !string.IsNullOrEmpty(project.ScreenshotFileId) ? _uriProvider.CreateUri(project.ScreenshotFileId) : null,
                State = state,
                Generator = (int)project.ProductType,
                ProjectType = project.ProjectType,
                ProjectSubtype = project.ProjectSubtype,
                EnableComments = project.EnableComments,
                CommentsCount = commentsCount,
                LikesCount = project.LikesCount,
                DislikesCount = project.DislikesCount,
                Comments = watchComments
            };
        }
예제 #4
0
        private async Task<DomainProjectForAdmin> GetProjectDataAsync(ProjectEntity project)
        {
            UserEntity user = await _userRepository.GetAsync(project.UserId);

            return new DomainProjectForAdmin
            {
                ProjectId = project.Id,
                Name = project.Name,
                Description = project.Description,
                Created = project.Created,
                Modified = project.Modified,
                UserId = project.UserId,
                UserName = user != null ? user.Name : null,
                ProductType = (ProductType)project.ProductId,
                Product = _productWriterForAdmin.WriteProduct(project.ProductId)
            };
        }