// TODO: logging
        public static async Task Ban(SausageConnection user)
        {
            try
            {
                if (!(user.Socket.Connected || MainSocket.Connected))
                {
                    return;
                }
                // user exists
                if (ConnectedUsers.Any(x => x.UserInfo.Guid == user.UserInfo.Guid))
                {
                    Blacklisted.Add(user.Ip.Address);
                    PacketFormat packet = new PacketFormat(PacketOption.UserBanned)
                    {
                        Guid    = user.UserInfo.Guid,
                        Content = "Place-holder reason"
                    };
                    Log(packet);
                    await Task.Delay(1000);

                    // delay for waiting on the client to recieve a message
                    user.Disconnect();
                    UiCtx.Send(x => ConnectedUsers.Remove(user));
                }
                else
                {
                    MessageBox.Show("User not found", "Ban result");
                }
            }
            catch (ArgumentNullException e)
            {
                MessageBox.Show($"User returned null {e}", "Exception Caught");
            }
        }
 public static async Task Mute(SausageConnection user)
 {
     try
     {
         // user exists
         if (ConnectedUsers.Any(x => x.UserInfo.Guid == user.UserInfo.Guid))
         {
             PacketFormat packet = new PacketFormat(PacketOption.UserMuted)
             {
                 Guid    = user.UserInfo.Guid,
                 Content = "Place-holder reason"
             };
             user.UserInfo.IsMuted = true;
             await Log(packet);
         }
         else
         {
             MessageBox.Show("User not found", "Kick result");
         }
     }
     catch (ArgumentNullException e)
     {
         MessageBox.Show($"User returned null {e}", "Exception Caught");
     }
 }
Example #3
0
        public async Task <IActionResult> SendPrivateMessage(MessageDetail message)
        {
            if (!ConnectedUsers.Any(x => x.UserId.Equals(message.FromUserId)))
            {
                throw new InvalidOperationException(Resources.UserIsDisconnected);
            }

            var toUser = ConnectedUsers.FirstOrDefault(x => x.UserId == message.ToUserId);

            if (toUser is null)
            {
                throw new InvalidOperationException(Resources.UserIsDisconnected);
            }

            await Clients.AllExcept(Context.ConnectionId)
            .SendAsync(ChatHubMethods.UpdateUnreadMessages, message.FromUserId);

            // send a private message to listener user
            await Clients.Client(toUser.ConnectionId)
            .SendAsync(ChatHubMethods.ReceiveMessage, message);

            AddMessageInCache(message);

            return(new OkResult());
        }
Example #4
0
        public IActionResult Connect()
        {
            if (ConnectedUsers.Any(x => x.ConnectionId.Equals(Context.ConnectionId)))
            {
                throw new InvalidOperationException(Resources.UserIsConnected);
            }

            var user = CallApiGET <UserResponseDTO>(uri: "api/users", isSecured: true);

            user.ConnectionId = Context.ConnectionId;

            ConnectedUsers.Add(user);

            return(new OkResult());
        }
Example #5
0
        public async Task <IActionResult> CacheMessage(MessageDetail message)
        {
            if (!ConnectedUsers.Any(x => x.UserId.Equals(message.FromUserId)))
            {
                throw new InvalidOperationException(Resources.UserIsDisconnected);
            }

            // update notification indicator
            await Clients.AllExcept(Context.ConnectionId)
            .SendAsync(ChatHubMethods.UpdateUnreadMessages, message.FromUserId);

            AddMessageInCache(message);

            return(new OkResult());
        }
Example #6
0
        public async Task <IActionResult> RequestConversation(string fromId, string toId)
        {
            Guid fromUserId = Guid.Parse(fromId);
            Guid toUserId   = Guid.Parse(toId);

            if (!ConnectedUsers.Any(x => x.UserId.Equals(fromUserId)))
            {
                throw new InvalidOperationException(Resources.UserIsDisconnected);
            }

            var toUser = ConnectedUsers.FirstOrDefault(x =>
                                                       x.UserId == toUserId &&
                                                       (x.IsOnHold || x.ConnectedWith == fromUserId));

            if (toUser is null)
            {
                throw new InvalidOperationException(Resources.UserIsDisconnected);
            }

            if (toUser.IsOnHold)
            {
                toUser.IsOnHold      = false;
                toUser.ConnectedWith = fromUserId;
                // set all cached messages to the requesting user
                CurrentMessages.Where(u => u.FromUserId == toUserId)
                .ToList()
                .ForEach(message => message.ToUserId = fromUserId);
            }

            // get all cached messages
            var cachedConversation = CurrentMessages
                                     .Where(u =>
                                            (u.FromUserId == fromUserId && u.ToUserId == toUserId) ||
                                            (u.FromUserId == toUserId && u.ToUserId == fromUserId));

            // hide user to others
            await Clients.AllExcept(Context.ConnectionId)
            .SendAsync(ChatHubMethods.UserNotAvailable, toUserId);

            await Clients.Client(toUser.ConnectionId)
            .SendAsync(ChatHubMethods.ConnectWith, fromUserId);

            await Clients.Caller.SendAsync(ChatHubMethods.ReceiveConversation, cachedConversation);

            return(new OkResult());
        }
Example #7
0
        public async Task <IActionResult> Connect(string userName, bool isHolding)
        {
            if (string.IsNullOrEmpty(userName) || string.IsNullOrWhiteSpace(userName))
            {
                throw new InvalidDataException(Resources.InvalidUserName);
            }

            if (ConnectedUsers.Any(x => x.ConnectionId.Equals(Context.ConnectionId)))
            {
                throw new InvalidOperationException(Resources.UserIsConnected);
            }

            var user = new UserDetail
            {
                UserId       = Guid.NewGuid(),
                ConnectionId = Context.ConnectionId,
                UserName     = userName,
                IsOnHold     = isHolding
            };

            ConnectedUsers.Add(user);

            if (user.IsOnHold)
            {
                // send to all except caller client
                await Clients.AllExcept(user.ConnectionId)
                .SendAsync(ChatHubMethods.NewUserAvailable, new
                {
                    userId       = user.UserId,
                    userName     = user.UserName,
                    messageCount = 0
                });
            }

            return(new OkObjectResult(user.UserId));
        }
Example #8
0
 public bool HasUser(string username) => ConnectedUsers.Any(x => x.Username == username);