Exemplo n.º 1
0
        public async Task <FriendDto> CreateFriendshipRequest(CreateFriendshipRequestInput input)
        {
            var userIdentifier = AbpSession.ToUserIdentifier();
            var probableFriend = new UserIdentifier(input.TenantId, input.UserId);

            _chatFeatureChecker.CheckChatFeatures(userIdentifier.TenantId, probableFriend.TenantId);

            if (_friendshipManager.GetFriendshipOrNull(userIdentifier, probableFriend) != null)
            {
                throw new UserFriendlyException(L("YouAlreadySentAFriendshipRequestToThisUser"));
            }

            var user = await UserManager.FindByIdAsync(AbpSession.GetUserId());

            User probableFriendUser;

            using (CurrentUnitOfWork.SetTenantId(input.TenantId))
            {
                probableFriendUser = (await UserManager.FindByIdAsync(input.UserId));
            }

            var friendTenancyName = probableFriend.TenantId.HasValue ? _tenantCache.Get(probableFriend.TenantId.Value).TenancyName : null;
            var sourceFriendship  = new Friendship(userIdentifier, probableFriend, friendTenancyName, probableFriendUser.UserName, probableFriendUser.ProfilePictureId, FriendshipState.Accepted);

            _friendshipManager.CreateFriendship(sourceFriendship);

            var userTenancyName  = user.TenantId.HasValue ? _tenantCache.Get(user.TenantId.Value).TenancyName : null;
            var targetFriendship = new Friendship(probableFriend, userIdentifier, userTenancyName, user.UserName, user.ProfilePictureId, FriendshipState.Accepted);

            _friendshipManager.CreateFriendship(targetFriendship);

            var clients = _onlineClientManager.GetAllByUserId(probableFriend);

            if (clients.Any())
            {
                var isFriendOnline = _onlineClientManager.IsOnline(sourceFriendship.ToUserIdentifier());
                _chatCommunicator.SendFriendshipRequestToClient(clients, targetFriendship, false, isFriendOnline);
            }

            var senderClients = _onlineClientManager.GetAllByUserId(userIdentifier);

            if (senderClients.Any())
            {
                var isFriendOnline = _onlineClientManager.IsOnline(targetFriendship.ToUserIdentifier());
                _chatCommunicator.SendFriendshipRequestToClient(senderClients, sourceFriendship, true, isFriendOnline);
            }

            var sourceFriendshipRequest = sourceFriendship.MapTo <FriendDto>();

            sourceFriendshipRequest.IsOnline = _onlineClientManager.GetAllByUserId(probableFriend).Any();

            return(sourceFriendshipRequest);
        }
Exemplo n.º 2
0
        private void HandleSenderToReceiver(UserIdentifier senderIdentifier, UserIdentifier receiverIdentifier, string message)
        {
            var friendshipState = _friendshipManager.GetFriendshipOrNull(senderIdentifier, receiverIdentifier)?.State;

            if (friendshipState == null)
            {
                friendshipState = FriendshipState.Accepted;

                var receiverTenancyName = receiverIdentifier.TenantId.HasValue
                    ? _tenantCache.Get(receiverIdentifier.TenantId.Value).TenancyName
                    : null;

                var receiverUser = _userManager.GetUser(receiverIdentifier);
                _friendshipManager.CreateFriendship(
                    new Friendship(
                        senderIdentifier,
                        receiverIdentifier,
                        receiverTenancyName,
                        receiverUser.UserName,
                        receiverUser.ProfilePictureId,
                        friendshipState.Value)
                    );
            }

            if (friendshipState.Value == FriendshipState.Blocked)
            {
                //Do not send message if receiver banned the sender
                return;
            }

            var sentMessage = new ChatMessage(
                senderIdentifier,
                receiverIdentifier,
                ChatSide.Sender,
                message,
                ChatMessageReadState.Read
                );

            Save(sentMessage);

            _chatCommunicator.SendMessageToClient(
                _onlineClientManager.GetAllByUserId(senderIdentifier),
                sentMessage
                );
        }
Exemplo n.º 3
0
        /// <summary>
        /// 内部生成
        /// </summary>
        /// <param name="tenant">商户对象</param>
        /// <returns></returns>
        private async Task BuildForInternalAsync(Tenant tenant)
        {
            //Create Organization Units

            var organizationUnits = new List <OrganizationUnit>();

            var producing = await CreateAndSaveOrganizationUnit(organizationUnits, tenant, "Producing");

            var researchAndDevelopment = await CreateAndSaveOrganizationUnit(organizationUnits, tenant, "Research & Development", producing);

            var ivrProducts = await CreateAndSaveOrganizationUnit(organizationUnits, tenant, "IVR Related Products", researchAndDevelopment);

            var voiceTech = await CreateAndSaveOrganizationUnit(organizationUnits, tenant, "Voice Technologies", researchAndDevelopment);

            var inhouseProjects = await CreateAndSaveOrganizationUnit(organizationUnits, tenant, "Inhouse Projects", researchAndDevelopment);

            var qualityManagement = await CreateAndSaveOrganizationUnit(organizationUnits, tenant, "Quality Management", producing);

            var testing = await CreateAndSaveOrganizationUnit(organizationUnits, tenant, "Testing", producing);

            var selling = await CreateAndSaveOrganizationUnit(organizationUnits, tenant, "Selling");

            var marketing = await CreateAndSaveOrganizationUnit(organizationUnits, tenant, "Marketing", selling);

            var sales = await CreateAndSaveOrganizationUnit(organizationUnits, tenant, "Sales", selling);

            var custRelations = await CreateAndSaveOrganizationUnit(organizationUnits, tenant, "Customer Relations", selling);

            var supporting = await CreateAndSaveOrganizationUnit(organizationUnits, tenant, "Supporting");

            var buying = await CreateAndSaveOrganizationUnit(organizationUnits, tenant, "Buying", supporting);

            var humanResources = await CreateAndSaveOrganizationUnit(organizationUnits, tenant, "Human Resources", supporting);

            //Create users

            var users = _randomUserGenerator.GetRandomUsers(RandomHelper.GetRandom(12, 26), tenant.Id);

            foreach (var user in users)
            {
                //Create the user
                await _userManager.CreateAsync(user);

                await CurrentUnitOfWork.SaveChangesAsync();

                //Add to roles
                _userManager.AddToRole(user.Id, StaticRoleNames.Tenants.User);

                //Add to OUs
                var randomOus = RandomHelper.GenerateRandomizedList(organizationUnits).Take(RandomHelper.GetRandom(0, 3));
                foreach (var ou in randomOus)
                {
                    await _userManager.AddToOrganizationUnitAsync(user, ou);
                }

                //Set profile picture
                if (RandomHelper.GetRandom(100) < 70) //A user will have a profile picture in 70% probability.
                {
                    await SetRandomProfilePictureAsync(user);
                }
            }

            //Set a picture to admin!
            var admin = _userManager.FindByName(User.AdminUserName);

            await SetRandomProfilePictureAsync(admin);

            //Create Friendships
            var friends = RandomHelper.GenerateRandomizedList(users).Take(3).ToList();

            foreach (var friend in friends)
            {
                _friendshipManager.CreateFriendship(
                    new Friendship(
                        admin.ToUserIdentifier(),
                        friend.ToUserIdentifier(),
                        tenant.TenancyName,
                        friend.UserName,
                        friend.ProfilePictureId,
                        FriendshipState.Accepted)
                    );

                _friendshipManager.CreateFriendship(
                    new Friendship(
                        friend.ToUserIdentifier(),
                        admin.ToUserIdentifier(),
                        tenant.TenancyName,
                        admin.UserName,
                        admin.ProfilePictureId,
                        FriendshipState.Accepted)
                    );
            }

            //Create chat message
            var friendWithMessage = RandomHelper.GenerateRandomizedList(friends).First();

            _chatMessageRepository.InsertAndGetId(
                new ChatMessage(
                    friendWithMessage.ToUserIdentifier(),
                    admin.ToUserIdentifier(),
                    ChatSide.Sender,
                    L("Demo_SampleChatMessage"),
                    ChatMessageReadState.Read
                    )
                );

            _chatMessageRepository.InsertAndGetId(
                new ChatMessage(
                    admin.ToUserIdentifier(),
                    friendWithMessage.ToUserIdentifier(),
                    ChatSide.Receiver,
                    L("Demo_SampleChatMessage"),
                    ChatMessageReadState.Unread
                    )
                );
        }