Пример #1
0
        private async void Disconnect()
        {
            ChatControl    chatWindow     = new ChatControl(ChatsControl);
            MainWindow     mainWindow     = new MainWindow();
            SignalRManager signalRManager = new SignalRManager();
            string         chatName       = Command["objectName"];
            string         userName       = User.Name;

            if (chatName == string.Empty)
            {
                chatName = ChatSelected;
            }
            if (chatName != string.Empty)
            {
                await ApiManager.Delete("api/chat", $"removeUserFromChat/{chatName}/{userName}");

                chatWindow.DeleteTabItem(chatName);

                signalRManager.RemoveUserFromChat(chatName, userName);
            }
            else
            {
                MessageBox.Show("Подключитесь к комнате или введите корректное название.");
            }
        }
Пример #2
0
        private async void Delete()
        {
            ChatControl chatWindow = new ChatControl(ChatsControl);
            string      chatName   = Command["objectName"];
            bool        hasRight   = Convert.ToBoolean(await ApiManager.Read($"api/chat/hasHighRightInChat/{chatName}/{User.Name}"));

            if (hasRight)
            {
                if (chatName != string.Empty)
                {
                    SignalRManager signalRManager = new SignalRManager();

                    ApiManager.Delete("api/chat", $"deleteChat/{chatName}/{User.Name}");

                    chatWindow.DeleteTabItem(chatName);

                    signalRManager.RemoveChat(chatName);
                }
                else
                {
                    MessageBox.Show("Некорректное имя.");
                }
            }
            else
            {
                MessageBox.Show("У вас нет прав на это действие.");
            }
        }
Пример #3
0
        private async void Create()
        {
            if (Command["objectName"] != string.Empty)
            {
                ChatControl    chatControl    = new ChatControl(ChatsControl);
                MainWindow     mainWindow     = new MainWindow();
                SignalRManager signalRManager = new SignalRManager();
                string         chatName       = Command["objectName"];
                string         userName       = User.Name;

                bool isChatExists = Convert.ToBoolean(await ApiManager.Read($"api/chat/isChatExists/{chatName}"));
                if (!isChatExists)
                {
                    await ApiManager.Create("api/chat/create", $"{{'Name':'{chatName}', 'Creator':'{userName}'}}");

                    await ApiManager.Create("api/chat/user", $"{{ 'Chat':{{'Name':'{chatName}'}}, 'User':{{'Name':'{userName}'}} }}");

                    chatControl.AddTabItem(chatName);
                    mainWindow.UpdateUsersBox();

                    signalRManager.AddUserToChat(chatName, userName);
                }
                else
                {
                    MessageBox.Show("Чат с таким названием уже существует.");
                }
            }
            else
            {
                MessageBox.Show("Некорректное имя.");
            }
        }
Пример #4
0
        private async void LoadPreviouslyOpenedChatsItems()
        {
            ChatControl   chatControl = new ChatControl(ChatsControl);
            List <string> chatsNames  = new List <string>();

            string chatsJson = await ApiManager.Read($"api/chat/getChats/{User.Name}");

            chatsNames = GetListValuesFromJson(chatsJson, "name");

            foreach (string chatName in chatsNames)
            {
                chatControl.AddTabItem(chatName);
                await UpdateUsersBox();
            }
        }
Пример #5
0
        public async Task UpdateUsersBox()
        {
            if (ChatSelected != null)
            {
                List <string> usersNames = new List <string>();

                string usersJson = await ApiManager.Read($"api/chat/getUsers/{ChatSelected}");

                usersNames = GetListValuesFromJson(usersJson, "name");

                ChatControl.UsersBox[ChatSelected].Items.Clear();
                foreach (string user in usersNames)
                {
                    ChatControl.CreateListBoxItem(ChatSelected, user);
                }
            }
        }
Пример #6
0
        public async void OnConnect()
        {
            connection.On <string, string, string>("ReceiveMessage", (chatName, userName, message) =>
            {
                Dispatcher.Invoke(() =>
                {
                    string newMessage = $"{userName}: {message}";
                    if (ChatControl.ChatBox.Count != 0)
                    {
                        if (ChatControl.HasTabItemToDictionary(chatName))
                        {
                            ChatControl.ChatBox[chatName].Items.Add(newMessage);
                        }
                    }
                });
            });
            connection.On <string, string>("ReceiveUser", (chatName, userName) =>
            {
                Dispatcher.Invoke(() =>
                {
                    if (ChatControl.UsersBox.Count != 0)
                    {
                        if (!ChatControl.HasUserToUsersBox(chatName, userName))
                        {
                            ChatControl.CreateListBoxItem(chatName, userName);
                        }
                        else
                        {
                            ChatControl.UpdateListBoxItem(chatName, userName);
                        }
                    }
                });
            });
            connection.On <string, string>("RenameUser", (oldName, newName) =>
            {
                Dispatcher.Invoke(() =>
                {
                    if (User.Name == oldName)
                    {
                        User.Name = newName;
                    }

                    UpdateUsersBox();
                });
            });
            connection.On("ChangeRoleUser", () =>
            {
                Dispatcher.Invoke(() =>
                {
                    UpdateUsersBox();
                });
            });
            connection.On <string, string>("RenameChat", (oldName, newName) =>
            {
                Dispatcher.Invoke(() =>
                {
                    ChatControl chatWindow = new ChatControl(ChatsControl);

                    if (ChatSelected == oldName)
                    {
                        ChatSelected = newName;
                    }

                    chatWindow.RenameTabItem(oldName, newName);
                });
            });
            connection.On("RemoveUser", () =>
            {
                Dispatcher.Invoke(() =>
                {
                    UpdateUsersBox();
                });
            });
            connection.On <string>("RemoveChat", (chatName) =>
            {
                Dispatcher.Invoke(() =>
                {
                    ChatControl chatWindow = new ChatControl(ChatsControl);

                    chatWindow.DeleteTabItem(chatName);
                });
            });
            connection.On <string, string>("BanUser", (chatName, userName) =>
            {
                Dispatcher.Invoke(() =>
                {
                    if (User.Name == userName && chatName == "allChats")
                    {
                        ChatControl chatWindow = new ChatControl(ChatsControl);
                        chatWindow.DeleteAllTabItem();
                    }
                    else if (User.Name == userName)
                    {
                        ChatControl chatWindow = new ChatControl(ChatsControl);
                        chatWindow.DeleteTabItem(chatName);
                    }
                    else
                    {
                        ChatControl.UpdateListBoxItem(chatName, userName);
                    }
                });
            });

            await connection.StartAsync();
        }