Ejemplo n.º 1
0
        public async Task SendCandidate(int roomId, string candidate)
        {
            // Validation
            if (roomId <= 0)
            {
                await ReturnError("Could not send candidate", "You must specify a room.");

                return;
            }

            if (candidate.IsNullOrWhitespace())
            {
                await ReturnError("Could not send candidate", "You must specify a candidate.");

                return;
            }

            // Permission
            Room room = null;
            await Task.Run(() => room = _roomService.GetRoom(roomId));

            if (room == null || !RoomPermissionHelper.CanAddMessage(_context.CurrentUser, room))
            {
                await ReturnError("Could not send candidate", "You do not have permission, the room may have been deleted.");

                return;
            }

            // Notify others in group
            await Clients.OthersInGroup(roomId.ToString()).SendAsync("ReceiveCandidate", candidate);
        }
Ejemplo n.º 2
0
        public IActionResult DeleteRoom(int roomId)
        {
            Room room = null;

            if (roomId > 0)
            {
                room = _roomService.GetRoom(roomId);
            }

            if (room == null || !RoomPermissionHelper.CanDeleteRoom(_context.CurrentUser, room))
            {
                return(LocalRedirect(UrlHelper.GetAccessDeniedUrl()));
            }
            else
            {
                try
                {
                    // Delete the room
                    _roomService.DeleteRoom(_context.CurrentUser, room);
                }
                catch (Exception e)
                {
                    _exceptionService.ReportException(e);
                    return(LocalRedirect(UrlHelper.GetErrorUrl()));
                }
            }

            // If we got here we were successful so redirect to rooms list
            return(LocalRedirect(UrlHelper.GetRoomsUrl()));
        }
Ejemplo n.º 3
0
        public async Task StopBroadcast(int roomId)
        {
            // Validation
            if (roomId <= 0)
            {
                await ReturnError("Could not stop broadcast", "You must specify a room.");

                return;
            }

            // Permission
            Room room = null;
            await Task.Run(() => room = _roomService.GetRoom(roomId));

            if (room == null || !RoomPermissionHelper.CanAddMessage(_context.CurrentUser, room))
            {
                await ReturnError("Could not stop broadcast", "You do not have permission, the room may have been deleted.");

                return;
            }

            // Notify all in group
            Models.Broadcast hubBroadcast = new Models.Broadcast
            {
                UserId   = _context.CurrentUser.Id,
                StreamId = string.Empty
            };
            await Clients.Group(roomId.ToString()).SendAsync("ReceiveDeleteBroadcast", hubBroadcast);
        }
Ejemplo n.º 4
0
        public async Task SendMessage(int roomId, string text)
        {
            // Validation
            if (roomId <= 0)
            {
                await ReturnError("Could not send message", "You must specify a room.");

                return;
            }

            if (text.IsNullOrWhitespace())
            {
                await ReturnError("Could not send message", "Text must not be null or whitespace.");

                return;
            }

            // Permission
            Room room = null;
            await Task.Run(() => room = _roomService.GetRoom(roomId));

            if (room == null || !RoomPermissionHelper.CanAddMessage(_context.CurrentUser, room))
            {
                await ReturnError("Could not send message", "You do not have permission, the room may have been deleted.");

                return;
            }

            // Trim message if necessary
            if (text.Length > 2000)
            {
                text = text.Substring(0, 2000);
            }

            // Add message to room
            int messageId = -1;
            await Task.Run(() => messageId = _roomService.AddMessage(_context.CurrentUser, room, text));

            // Html encode the text of the message
            text = HtmlEncoder.Default.Encode(text);

            // Notify all in group
            Models.Message hubMessage = new Models.Message
            {
                Id               = messageId,
                UserName         = _context.CurrentUser.UserName,
                UserId           = _context.CurrentUser.Id,
                Text             = text,
                IsDeleted        = false,
                ModifiedId       = _context.CurrentUser.Id,
                ModifiedUserName = _context.CurrentUser.UserName
            };
            await Clients.Group(roomId.ToString()).SendAsync("ReceiveMessage", hubMessage);
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> OnGet(int roomId)
        {
            if (roomId > 0)
            {
                await Task.Run(() => Room = _roomService.GetRoom(roomId, includeMessages: true));
            }

            if (Room == null || !RoomPermissionHelper.CanViewRoom(_context.CurrentUser, Room))
            {
                return(LocalRedirect(UrlHelper.GetAccessDeniedUrl()));
            }

            return(Page());
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> OnGet(int id)
        {
            if (id > 0)
            {
                Room = await Task.Run(() => _roomService.GetRoom(id));
            }

            if (Room == null || !RoomPermissionHelper.CanEditRoom(_context.CurrentUser, Room))
            {
                return(LocalRedirect(UrlHelper.GetAccessDeniedUrl()));
            }

            ContactUIDs = String.Join(",", Room.Contacts.Select(c => c.ContactUID).ToArray());
            return(Page());
        }
Ejemplo n.º 7
0
        public async Task GetMessages(int roomId)
        {
            if (roomId <= 0)
            {
                await ReturnError("Could not retrieve messages", "You must specify a room.");

                return;
            }

            Room room = null;
            await Task.Run(() => room = _roomService.GetRoom(roomId));

            if (room == null || !RoomPermissionHelper.CanViewRoom(_context.CurrentUser, room))
            {
                await ReturnError("Could not retrieve messages", "You do not have permission, the room may have been deleted.");

                return;
            }

            // Retrieve last 300 messages
            List <Models.Message> hubMessages = null;

            await Task.Run(() => {
                List <Message> messages  = _roomService.GetMessages(new MessageSearch()).ToList();
                List <User> messageUsers = _userService.GetUserByUID(messages.Select(m => m.CreatedUID).ToArray()).ToList();

                hubMessages =
                    messages.Select(m => new Models.Message {
                    Id               = m.Id,
                    UserName         = messageUsers.First(u => m.CreatedUID.Equals(u.Id)).UserName,
                    UserId           = m.CreatedUID,
                    Text             = m.IsDeleted ? string.Empty : m.Text,
                    IsDeleted        = m.IsDeleted,
                    ModifiedId       = m.ModifiedUID,
                    ModifiedUserName = messageUsers.First(u => u.Id.Equals(m.ModifiedUID)).UserName
                })
                    .ToList();
            });

            if (hubMessages == null)
            {
                await ReturnError("Could not retrieve messages", "Something went wrong, pleash refresh the page.");

                return;
            }

            await Clients.Caller.SendAsync("ReceiveMessages", hubMessages);
        }
Ejemplo n.º 8
0
        public IActionResult OnGet(int id)
        {
            if (id > 0)
            {
                Room = _roomService.GetRoom(id);
            }

            if (Room == null || !RoomPermissionHelper.CanViewRoom(_context.CurrentUser, Room))
            {
                return(LocalRedirect(UrlHelper.GetAccessDeniedUrl()));
            }
            else
            {
                return(Page());
            }
        }
Ejemplo n.º 9
0
        public async Task JoinRoom(int roomId)
        {
            if (roomId <= 0)
            {
                await ReturnError("Could not join room", "You must specify a room.");

                return;
            }

            Room room = null;
            await Task.Run(() => room = _roomService.GetRoom(roomId));

            if (room == null || !RoomPermissionHelper.CanViewRoom(_context.CurrentUser, room))
            {
                await ReturnError("Could not join room", "You do not have permission, the room may have been deleted.");

                return;
            }

            await Groups.AddToGroupAsync(Context.ConnectionId, roomId.ToString());

            List <Models.Contact> hubContacts = new List <Models.Contact>();
            await Task.Run(() => {
                hubContacts =
                    _userService.GetUserByUID(room.Contacts.Select(c => c.ContactUID).ToArray())
                    .Select(u => new Models.Contact
                {
                    Id       = room.Contacts.First(c => c.ContactUID.Equals(u.Id)).Id,
                    UserUID  = u.Id,
                    UserName = u.UserName
                })
                    .ToList();
            });

            await Clients.Caller.SendAsync("ReceiveContacts", hubContacts);

            Models.Contact caller = new Models.Contact
            {
                UserName = _context.CurrentUser.UserName,
                UserUID  = _context.CurrentUser.Id
            };

            await Clients.OthersInGroup(roomId.ToString()).SendAsync("ReceiveContact", caller);
        }
Ejemplo n.º 10
0
        public async Task SendOfferToUser(int roomId, string userId, string offer)
        {
            // Validation
            if (roomId <= 0)
            {
                await ReturnError("Could not send offer", "You must specify a room.");

                return;
            }

            if (userId.IsNullOrWhitespace())
            {
                await ReturnError("Could not send offer", "You must specify a user.");

                return;
            }

            if (offer.IsNullOrWhitespace())
            {
                await ReturnError("Could not send offer", "You must specify an offer.");

                return;
            }

            // Permission
            Room room = null;
            await Task.Run(() => room = _roomService.GetRoom(roomId));

            if (room == null || !RoomPermissionHelper.CanAddMessage(_context.CurrentUser, room))
            {
                await ReturnError("Could not send offer", "You do not have permission, the room may have been deleted.");

                return;
            }

            // Notify specified user
            await Clients.User(userId).SendAsync("ReceiveOffer", offer, _context.CurrentUser.Id);
        }
Ejemplo n.º 11
0
        public async Task <IActionResult> OnPost(int id)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    Models.Room retrievedRoom = null;

                    if (id > 0)
                    {
                        retrievedRoom = await Task.Run(() => _roomService.GetRoom(id));
                    }

                    if (retrievedRoom == null || !RoomPermissionHelper.CanEditRoom(_context.CurrentUser, retrievedRoom))
                    {
                        return(LocalRedirect(UrlHelper.GetAccessDeniedUrl()));
                    }

                    // Set the new property values and save
                    retrievedRoom.Name        = Room.Name;
                    retrievedRoom.Description = Room.Description;

                    await _context.SaveChangesAsync();

                    // Now add the room contacts (this will only add valid room contacts)
                    _roomService.SetRoomContacts(_context.CurrentUser, retrievedRoom, ContactUIDs != null ? ContactUIDs.Split(",") : new string[] { });

                    // If we got here we were successful, so redirect to view page
                    return(LocalRedirect(UrlHelper.GetViewRoomUrl(retrievedRoom.Id)));
                }
                catch (Exception e)
                {
                    _exceptionService.ReportException(e);
                }
            }

            return(Page());
        }
Ejemplo n.º 12
0
        public async Task DeleteMessage(int roomId, int messageId)
        {
            // Validation
            if (roomId <= 0 || messageId <= 0)
            {
                await ReturnError("Could not delete message", "You must specify a room and message.");

                return;
            }

            // Retrieve message
            Message message = null;
            await Task.Run(() => message = _roomService.GetMessage(messageId));

            // Permission
            if (message == null || !RoomPermissionHelper.CanDeleteMessage(_context.CurrentUser, message))
            {
                await ReturnError("Could not delete message", "You do not have permission, the room may have been deleted.");

                return;
            }

            // Delete
            await Task.Run(() => _roomService.DeleteMessage(_context.CurrentUser, message));

            // Notify all in group
            Models.Message hubMessage = new Models.Message {
                Id               = message.Id,
                UserName         = _userService.GetUserNameByUID(message.CreatedUID),
                UserId           = message.CreatedUID,
                Text             = string.Empty,
                IsDeleted        = true,
                ModifiedId       = _context.CurrentUser.Id,
                ModifiedUserName = _context.CurrentUser.UserName
            };
            await Clients.Group(roomId.ToString()).SendAsync("ReceiveDeleteMessage", hubMessage);
        }
Ejemplo n.º 13
0
        public JsonResult AddRoomContacts(int roomId, string[] UIDs)
        {
            GenericResponse result = new GenericResponse()
            {
                IsSuccessful = false
            };

            try
            {
                if (roomId < 1 || UIDs.IsNullOrEmpty())
                {
                    result.ErrorMessage = "Bad request.";
                    return(new JsonResult(result));
                }

                Room room = _roomService.GetRoom(roomId);

                if (room == null || !RoomPermissionHelper.CanAddRoomContact(_context.CurrentUser, room))
                {
                    result.ErrorMessage = "You do not have permission to add a contact to this room.";
                    return(new JsonResult(result));
                }

                // Now add the contacts to the room (UIDs not found in the actionUser's contacts will be filtered out)
                _roomService.AddRoomContacts(_context.CurrentUser, room, UIDs);

                // If we got this far we are successful
                result.IsSuccessful = true;
            }
            catch (Exception e)
            {
                result.ErrorMessage = "An exception occurred.";
                _exceptionService.ReportException(e);
            }

            return(new JsonResult(result));
        }