Пример #1
0
        public async Task <IActionResult> JoinGroup(string roomId, JoinRoomForm joinRoomForm)
        {
            var user = await accountRepository.GetUser(joinRoomForm.UserId);

            if (user == null)
            {
                return(BadRequest(new { message = "Couldn't find the user in the database" }));
            }
            var roomExist = user.Rooms.Find(r => r.Id == roomId);

            if (roomExist != null)
            {
                return(BadRequest(new { message = "Cannot join the group twice" }));
            }

            var newJoinRoom = await userRepository.AddRoomToUser(joinRoomForm.UserId, roomId, joinRoomForm.RoomName);

            await roomRepository.AddUserToRoom(roomId, joinRoomForm.UserId, joinRoomForm.Username);

            var senderConnection = await userConnectedRepository.GetConnectedUserByUserId(joinRoomForm.UserId);

            await hubContext.Groups.AddToGroupAsync(senderConnection.ConnectionId, joinRoomForm.RoomName);

            await hubContext.Clients.Client(senderConnection.ConnectionId).SendAsync(KeyConstants.joinRoomSuccess, newJoinRoom);

            await hubContext.Clients.Group(joinRoomForm.RoomName).SendAsync(KeyConstants.newJoinRoom, senderConnection.ConnectionId, joinRoomForm.UserId);

            return(NoContent());
        }
Пример #2
0
        public override async Task OnConnectedAsync()
        {
            var connectionId = GetConnectionId();
            var userId       = Context.GetHttpContext().Request.Query["userId"];

            var checkIfExists = await userConnectedRepository.GetConnectedUserByUserId(userId);

            if (checkIfExists != null)
            {
                await userConnectedRepository.UpdateUserByUserId(userId, connectionId);

                var updatedConnectedUser = await userConnectedRepository.GetConnectedUserByUserId(userId);

                await Clients.All.SendAsync(KeyConstants.updatedConnectedUser, updatedConnectedUser);
            }
            else
            {
                await userConnectedRepository.AddUser(userId, connectionId);
            }

            var connectedUserObject = await accountRepository.GetUser(userId);

            var roomList = connectedUserObject.Rooms;

            foreach (var room in roomList)
            {
                await Groups.AddToGroupAsync(connectionId, room.RoomName);
            }

            var userConnectedList = await userConnectedRepository.GetAllConnectedUsers();

            await Clients.Caller.SendAsync(KeyConstants.userConnectedList, userConnectedList);

            var connectedUser = await userConnectedRepository.GetConnectedUserByUserId(userId);

            await Clients.All.SendAsync(KeyConstants.newConnectedUser, connectedUser);

            await base.OnConnectedAsync();
        }
Пример #3
0
        public async Task <ActionResult <Contact> > CreateContact(string userId, [FromBody] NewContactForm newContact)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new { message = "Contact form is not valid" }));
            }

            var contactValid = await accountRepository.GetUser(newContact.ContactId);

            if (contactValid == null)
            {
                return(BadRequest(new { message = "Cannot find the contact in the database" }));
            }
            var senderDocument = await accountRepository.GetUser(userId);

            if (senderDocument == null)
            {
                return(BadRequest(new { message = "Cannot find the user in the database" }));
            }

            if (senderDocument.Contacts.Any(c => c.ContactId == newContact.ContactId))
            {
                return(BadRequest(new { message = "Cannot add a contact twice" }));
            }

            var senderConnection = await userConnectedRepository.GetConnectedUserByUserId(userId);

            if (senderConnection == null)
            {
                return(BadRequest(new { message = "Verify your connection to the SignalR Hub" }));
            }
            var senderConnectionId = senderConnection.ConnectionId;

            var senderUpdatedDocument = await userRepository.AddContactToUser(userId, newContact.ContactId, newContact.ContactName);

            if (senderUpdatedDocument == null)
            {
                return(NotFound(new { message = "Contact was not added to the database" }));
            }

            var newSenderContact = senderUpdatedDocument.Contacts.Find(c => c.ContactId == newContact.ContactId);

            if (newSenderContact == null)
            {
                return(NotFound(new { message = "Couldn't find the new contact in the database" }));
            }

            var receiverUpdatedDocument = await userRepository.AddContactToUser(newContact.ContactId, userId, newContact.Username);

            if (receiverUpdatedDocument == null)
            {
                return(NotFound(new { message = "Contact was not added to the database" }));
            }

            var newReceiverContact = receiverUpdatedDocument.Contacts.Find(c => c.ContactId == userId);

            if (newReceiverContact == null)
            {
                return(NotFound(new { message = "Couldn't find the new contact in the database" }));
            }

            var receiverConnection = await userConnectedRepository.GetConnectedUserByUserId(newContact.ContactId);

            if (receiverConnection == null)
            {
                await hubContext.Clients.Client(senderConnectionId).SendAsync(KeyConstants.addedContactOffline, newSenderContact);

                return(CreatedAtRoute("GetContact", new { userId = userId.ToString(), contactId = newSenderContact.ContactId }, newSenderContact));
            }
            var receiverConnectionId = receiverConnection.ConnectionId;
            await hubContext.Clients.Client(senderConnectionId).SendAsync(KeyConstants.receiveNewContact, newSenderContact);

            await hubContext.Clients.Client(receiverConnectionId).SendAsync(KeyConstants.receiveNewContact, newReceiverContact);

            return(CreatedAtRoute("GetContact", new { userId = userId.ToString(), contactId = newSenderContact.ContactId }, newSenderContact));
        }