コード例 #1
0
        public async Task <PostViewModel> Handle(UpdatePostCommand request, CancellationToken cancellationToken)
        {
            var userId = Guid.Parse(_httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value);
            var user   = await _userRepository.GetByIdAsync(userId);

            user.CheckDisabled();

            var post = await _postRepository.GetPostWithAttachmentsById(request.PostId);

            if (post == null)
            {
                throw new ClientException("操作失败", new List <string> {
                    $"Post {request.PostId} does not exists."
                });
            }

            if (post.UserId != userId)
            {
                throw new ClientException("操作失败", new List <string> {
                    $"Post {post.Id} does not belong to user {userId}"
                });
            }

            var attachments = request.Attachments.Select(a => new PostAttachment(a.Name, a.Text, a.AttachmentType, a.IsPrivate)).ToList();

            post.Update(request.Text, request.Commentable, request.ForwardType, request.ShareType, request.Visibility,
                        request.ViewPassword, request.SystemTag, request.PublicTags, request.PrivateTag, request.CircleId, request.Latitude, request.Longitude, request.LocationName, request.Address,
                        request.CityCode, request.FriendIds, attachments, request.ShowOriginalText);

            #region arise内部用户更新帖子:无需审核
            var role = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.Role)?.Value ?? string.Empty;
            if (role == "internal")
            {
                post.Examine(PostAuthStatus.Authenticated);
            }
            #endregion

            _postRepository.Update(post);

            if (await _postRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken))
            {
                // 如果审核通过,给帖子中被@的用户发送被@通知
                if (post.PostAuthStatus == PostAuthStatus.Authenticated)
                {
                    var atUserIds = await _postQueries.GetAtUserIdsAsync(post);
                    await SendUserAtedEventAsync(post.UserId, post.Id, atUserIds);
                }

                return(await _postQueries.GetPostAsync(post.Id));
            }

            throw new ApplicationException("操作失败");
        }
コード例 #2
0
        public async Task <PostViewModel> Handle(PublishPostCommand request, CancellationToken cancellationToken)
        {
            var userId = Guid.Parse(_httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value);
            var user   = await _userRepository.GetByIdAsync(userId);

            user.CheckDisabled();

            // check if the private tag name exist
            if (!string.IsNullOrWhiteSpace(request.PrivateTag))
            {
                var tag = await _tagRepository.GetUserPrivateTagByName(userId, request.PrivateTag);

                if (tag == null)
                {
                    throw new ClientException("操作失败", new List <string> {
                        $"Tag {request.PrivateTag} does not exist."
                    });
                }
            }

            var score       = (DateTime.Now - DateTime.UnixEpoch.AddSeconds(user.CreatedTime)).TotalHours <= _scoreRewardSettings.NewUserHour ? user.PostScore + _scoreRewardSettings.NewUserPost : user.PostScore;
            var attachments = request.Attachments.Select(a => new PostAttachment(a.Name, a.Text, a.AttachmentType, a.IsPrivate)).ToList();
            var post        = Domain.AggregatesModel.PostAggregate.Post.CreatePost(request.Text, request.Commentable, request.ForwardType, request.ShareType,
                                                                                   request.Visibility, request.ViewPassword, request.SystemTag, request.PublicTags, request.PrivateTag, request.CircleId, request.Latitude, request.Longitude, request.LocationName,
                                                                                   request.Address, request.CityCode, request.FriendIds, attachments, score, userId);

            _postRepository.Add(post);

            #region arise内部用户发帖:1. 创建时间随机向前推1-5个月,2. 无需审核
            var role = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.Role)?.Value ?? string.Empty;
            if (role == "internal")
            {
                // 无需审核
                post.Examine(PostAuthStatus.Authenticated);

                // 创建时间随机向前推1-5个月
                var createdTime = DateTime.UnixEpoch.AddSeconds(post.CreatedTime);
                var months      = new Random().Next(1, 6);
                createdTime = createdTime.AddMonths(-months);
                post.SetCreatedTime((createdTime - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds);
            }
            #endregion

            if (await _postRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken))
            {
                #region 发布“帖子已发布事件”
                //取得附件中的第一张图片
                string image = null;
                var    first = request.Attachments.Where(a => !a.IsPrivate).FirstOrDefault();
                if (first.AttachmentType == AttachmentType.Image)
                {
                    image = first.Name;
                }
                else if (first.AttachmentType == AttachmentType.Video)
                {
                    image = first.Name?.Substring(0, first.Name.LastIndexOf('.')) + ".jpg";
                }

                // 如果帖子是审核通过状态的,取得帖子中被@的用户以便发送被@通知给他们
                IEnumerable <Guid> atUserIds = new List <Guid>();
                if (post.PostAuthStatus == PostAuthStatus.Authenticated)
                {
                    atUserIds = await _postQueries.GetAtUserIdsAsync(post);
                }

                await SendPostPublishedEventAsync(user.Id, user.Nickname, post.Id, image, atUserIds);

                #endregion

                return(await _postQueries.GetPostAsync(post.Id));
            }

            throw new ApplicationException("操作失败");
        }
コード例 #3
0
        public async Task <ActionResult <PostViewModel> > GetPostAsync(Guid postId)
        {
            var post = await _postQueries.GetPostAsync(postId);

            return(Ok(ResponseWrapper.CreateOkResponseWrapper(post)));
        }
コード例 #4
0
        public async Task <IActionResult> Get(Guid id)
        {
            var post = await postQueries.GetPostAsync(id);

            return(Ok(post));
        }