Example #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("操作失败");
        }
Example #2
0
        public async Task <bool> Handle(ExaminePostCommand request, CancellationToken cancellationToken)
        {
            var post = await _postRepository.GetByIdAsync(request.PostId);

            post.Examine(request.PostAuthStatus);

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

                return(true);
            }

            throw new ApplicationException("操作失败");
        }
        public async Task <IEnumerable <PostViewModel> > Handle(ForwardPostsCommand request, CancellationToken cancellationToken)
        {
            // 被转发的帖子对象list
            var toBeForwardedPosts = await _postRepository.GetPostsAsync(request.ForwardPostIds);

            // 被转发的帖子转发过的原始帖子对象list
            var originalPosts = await _postRepository.GetPostsAsync(toBeForwardedPosts.Where(p => p.ForwardedPostId != null).Select(p => p.ForwardedPostId.Value).ToList());

            var myId = Guid.Parse(_httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value);
            var me   = await _userRepository.GetByIdAsync(myId);

            var posts = new List <Domain.AggregatesModel.PostAggregate.Post>();

            toBeForwardedPosts.ForEach(toBeForwardedPost =>
            {
                // 创建新帖子
                var score = (DateTime.Now - DateTime.UnixEpoch.AddSeconds(me.CreatedTime)).TotalHours <= _scoreRewardSettings.NewUserHour ? me.PostScore + _scoreRewardSettings.NewUserPost : me.PostScore;
                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, null, request.Latitude, request.Longitude, request.LocationName,
                                                                                 request.Address, request.CityCode, request.FriendIds, null, score, myId, request.ShowOriginalText);

                post.SetForwardPostId(toBeForwardedPost);
                _postRepository.Add(post);
                posts.Add(post);

                // 增加被转发帖子的转发数和积分
                toBeForwardedPost.IncreaseForwardCount(_scoreRewardSettings.ForwardPost);

                // 被转发帖子是转发贴时,还要增加原始帖的转发数和积分
                if (toBeForwardedPost.ForwardedPostId != null)
                {
                    var originalPost = originalPosts.FirstOrDefault(p => p.Id == toBeForwardedPost.ForwardedPostId.Value);
                    originalPost.IncreaseForwardCount(_scoreRewardSettings.ForwardPost);
                }
            });

            if (await _postRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken))
            {
                var forwardInfos = new List <ForwardInfo>();

                foreach (var post in posts)
                {
                    var forwardedPost = toBeForwardedPosts.FirstOrDefault(p => p.Id == post.ForwardedPostId.Value);

                    if (forwardedPost == null)
                    {
                        forwardedPost = originalPosts.FirstOrDefault(p => p.Id == post.ForwardedPostId.Value);
                    }

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

                    forwardInfos.Add(new ForwardInfo
                    {
                        ForwardUserId      = myId,
                        OriginalPostUserId = forwardedPost.UserId,
                        OriginalPostId     = post.ForwardedPostId.Value,
                        NewPostId          = post.Id,
                        AtUserIds          = atUserIds
                    });
                }

                await SendPostForwardedEventAsync(forwardInfos);
            }

            return(await _postQueries.GetPostsAsync(posts.Select(p => p.Id).ToList()));
        }
Example #4
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("操作失败");
        }