public async Task <ChatroomApplication> NewChatroomAsync(IEnumerable <long> peopleIds, string name = "")
        {
            peopleIds = peopleIds.ToArray();
            if (OperatorId != 0)
            {
                var user = await _userRepo.GetByIdAsync(OperatorId);

                if (!peopleIds.Contains(user.Id))
                {
                    throw new ArgumentException("All users must contain yourself.");
                }
                if (!peopleIds.All(id => id == user.Id || user.IsFriend(id)))
                {
                    throw new ArgumentException("All users must be your friend.");
                }
            }

            var chatroom = await _chatroomRepo.NewEmptyChatroomAsync(name, OperatorId);

            foreach (var userId in peopleIds)
            {
                chatroom.AddPeople(userId, 0);
            }
            if (OperatorId != 0)
            {
                chatroom.SetRole(OperatorId, UserChatroom.UserRole.Admin, 0);
            }
            await _chatroomRepo.SaveChangesAsync();

            return(new ChatroomApplication(_provider)
            {
                ChatroomId = chatroom.Id,
                OperatorId = OperatorId
            });
        }
Example #2
0
        internal async Task <Chatroom> GetOrAddP2PChatroom(long user1Id, long user2Id)
        {
            var room = await _chatroomRepo.FindP2PChatroomAsync(user1Id, user2Id);

            if (room != null)
            {
                return(room);
            }
            var user1Exist = await _userRepo.ContainsIdAsync(user1Id);

            var user2Exist = await _userRepo.ContainsIdAsync(user2Id);

            if (!user1Exist || !user2Exist)
            {
                throw new ArgumentException("User not exist.");
            }
            room = await _chatroomRepo.NewEmptyChatroomAsync("", creatorId : 0);

            room.AddPeople(user1Id, operatorId: 0);
            room.AddPeople(user2Id, operatorId: 0);
            room.P2PUser1Id = Math.Min(user1Id, user2Id);
            room.P2PUser2Id = Math.Max(user1Id, user2Id);
            await _chatroomRepo.SaveChangesAsync();

            return(room);
        }