Example #1
0
        protected virtual async Task StoreUserMessageAsync(
            ChatMessage chatMessage,
            CancellationToken cancellationToken = default)
        {
            // 检查接收用户
            if (!chatMessage.ToUserId.HasValue)
            {
                throw new BusinessException(MessageServiceErrorCodes.UseNotFount);
            }

            var myFriend = await _friendStore
                           .GetMemberAsync(chatMessage.TenantId, chatMessage.ToUserId.Value, chatMessage.FormUserId, cancellationToken);

            var userChatSetting = await _userChatSettingRepository
                                  .FindByUserIdAsync(chatMessage.ToUserId.Value, cancellationToken);

            if (userChatSetting != null)
            {
                if (!userChatSetting.AllowReceiveMessage)
                {
                    // 当前发送的用户不接收消息
                    throw new BusinessException(MessageServiceErrorCodes.UserHasRejectAllMessage);
                }

                if (myFriend == null && !chatMessage.IsAnonymous)
                {
                    throw new BusinessException(MessageServiceErrorCodes.UserHasRejectNotFriendMessage);
                }

                if (chatMessage.IsAnonymous && !userChatSetting.AllowAnonymous)
                {
                    // 当前用户不允许匿名发言
                    throw new BusinessException(MessageServiceErrorCodes.UserNotAllowedToSpeakAnonymously);
                }
            }
            else
            {
                if (myFriend == null)
                {
                    throw new BusinessException(MessageServiceErrorCodes.UserHasRejectNotFriendMessage);
                }
            }
            if (myFriend?.Black == true)
            {
                throw new BusinessException(MessageServiceErrorCodes.UserHasBlack);
            }

            var message = new UserMessage(
                long.Parse(chatMessage.MessageId),
                chatMessage.FormUserId,
                chatMessage.FormUserName,
                chatMessage.Content,
                chatMessage.MessageType);

            message.SendToUser(chatMessage.ToUserId.Value);
            message.SetProperty(nameof(ChatMessage.IsAnonymous), chatMessage.IsAnonymous);

            await _messageRepository.InsertUserMessageAsync(message, cancellationToken);
        }
        public virtual async Task <UserAddFriendResult> AddRequestAsync(
            Guid?tenantId,
            Guid userId,
            Guid friendId,
            string remarkName  = "",
            string description = "",
            CancellationToken cancellationToken = default)
        {
            using (_currentTenant.Change(tenantId))
            {
                if (await _userChatFriendRepository.IsAddedAsync(userId, friendId))
                {
                    throw new BusinessException(MessageServiceErrorCodes.UseHasBeenAddedTheFriendOrSendAuthorization);
                }

                var status          = UserFriendStatus.NeedValidation;
                var userChatSetting = await _userChatSettingRepository.FindByUserIdAsync(friendId, cancellationToken);

                if (userChatSetting != null)
                {
                    if (!userChatSetting.AllowAddFriend)
                    {
                        throw new BusinessException(MessageServiceErrorCodes.UseRefuseToAddFriend);
                    }

                    status = userChatSetting.RequireAddFriendValition
                        ? UserFriendStatus.NeedValidation
                        : UserFriendStatus.Added;
                }

                var userChatFriend = new UserChatFriend(userId, friendId, remarkName, description, tenantId)
                {
                    CreationTime = _clock.Now,
                    CreatorId    = userId,
                };
                userChatFriend.SetStatus(status);

                await _userChatFriendRepository.InsertAsync(userChatFriend, cancellationToken : cancellationToken);

                return(new UserAddFriendResult(status));
            }
        }