Exemple #1
0
        private async void CreateRoom(string id, string pass, string name)
        {
            try
            {
                ChatRoom myRoom;
                if (checkBox2.Checked)
                {
                    myRoom = new ChatRoom()
                    {
                        Id = id, name = name, password = null, state = true
                    };
                }
                else
                {
                    myRoom = new ChatRoom()
                    {
                        Id = id, name = name, password = Encrypt(Convert.ToBase64String(Encoding.UTF8.GetBytes(pass)))
                    };
                }
                var task1 = chatRooms.InsertOneAsync(myRoom);
                account.rooms.Add(id);
                var filterRooms = Builders <Account> .Filter.Eq("_id", account.username);

                var updateRooms = Builders <Account> .Update.Set("rooms", account.rooms);

                var task2     = accounts.UpdateOneAsync(filterRooms, updateRooms);
                var newRoomdb = client.GetDatabase($"#{id}");
                newRoomdb.CreateCollection("accounts");
                newRoomdb.CreateCollection("messages");
                newRoomdb.CreateCollection($"{account.username} receives");
                var roomAccs  = newRoomdb.GetCollection <Account>("accounts");
                var temprooms = account.rooms;
                var tempava   = account.avatar;
                account.avatar = null;
                account.rooms  = null;
                var task3    = roomAccs.InsertOneAsync(account);
                var ChatRoom = new NewRoom()
                {
                    username = account.username, database = newRoomdb, name = $"#{myRoom.Id}: {myRoom.name}", label = myRoom.name, controlAccounts = accounts, Id = myRoom.Id, controldb = database
                };
                await task1;
                await task2;
                await task3;
                account.avatar = tempava;
                account.rooms  = temprooms;
                ChatRoom.Show();
                this.FormClosing += new FormClosingEventHandler((object sender, FormClosingEventArgs e) => { ChatRoom.Close(); });
            }
            catch (Exception)
            {
                MessageBox.Show("The room's ID has been taken, please re-type your room's ID", "Invalid room's ID");
                return;
            }
        }
Exemple #2
0
        private async void JoinRoom(string id, string pass)
        {
            var room = chatRooms.Find(s => s.Id == id).FirstOrDefault();

            if (room != null)
            {
                if (!room.state && room.password != Encrypt(Convert.ToBase64String(Encoding.UTF8.GetBytes(pass))))
                {
                    MessageBox.Show("Your room's password is incorrect, please re-type the room's password", "Incorrect room's password");
                    return;
                }
                else
                {
                    joinRoomdb = client.GetDatabase($"#{id}");
                    var roomAccounts = joinRoomdb.GetCollection <Account>("accounts");
                    var myAcc        = roomAccounts.Find(s => s.username == account.username).FirstOrDefault();
                    if (myAcc != null)
                    {
                        if (myAcc.connected)
                        {
                            MessageBox.Show("You are already in this room.", "Warning");
                        }
                        else
                        {
                            var filter = Builders <Account> .Filter.Eq("_id", myAcc.username);

                            var update = Builders <Account> .Update.Set("connected", true);

                            roomAccounts.UpdateOne(filter, update);
                            var ChatRoom = new NewRoom()
                            {
                                username = myAcc.username, database = joinRoomdb, name = $"#{room.Id}: {room.name}", label = room.name, controlAccounts = accounts, Id = room.Id, controldb = database
                            };
                            ChatRoom.Show();
                            this.FormClosing += new FormClosingEventHandler((object sender, FormClosingEventArgs e) => { ChatRoom.Close(); });
                        }
                    }
                    else
                    {
                        account.rooms.Add(id);
                        var temprooms   = account.rooms;
                        var tempava     = account.avatar;
                        var filterRooms = Builders <Account> .Filter.Eq("_id", account.username);

                        var updateRooms = Builders <Account> .Update.Set("rooms", account.rooms);

                        var task1 = accounts.UpdateOneAsync(filterRooms, updateRooms);
                        account.avatar = null;
                        account.rooms  = null;
                        var task2       = roomAccounts.InsertOneAsync(account);
                        var contactAccs = roomAccounts.Find(s => s.username != account.username).ToList();
                        var myContacts  = new HashSet <string>();
                        Parallel.ForEach(contactAccs, acc =>
                        {
                            acc.contacts.Add(account.username);
                            var filterAcc = Builders <Account> .Filter.Eq("_id", acc.username);
                            var updateAcc = Builders <Account> .Update.Set("contacts", acc.contacts);
                            roomAccounts.UpdateOneAsync(filterAcc, updateAcc);
                            myContacts.Add(acc.username);
                        });
                        var filter = Builders <Account> .Filter.Eq("_id", account.username);

                        var update = Builders <Account> .Update.Set("contacts", myContacts);

                        var task3 = roomAccounts.UpdateOneAsync(filter, update);
                        joinRoomdb.CreateCollection($"{account.username} receives");
                        var ChatRoom = new NewRoom()
                        {
                            username = account.username, database = joinRoomdb, name = $"#{room.Id}: {room.name}", label = room.name, controlAccounts = accounts, Id = room.Id, controldb = database
                        };
                        await task1;
                        await task2;
                        await task3;
                        account.avatar = tempava;
                        account.rooms  = temprooms;
                        ChatRoom.Show();
                        this.FormClosing += new FormClosingEventHandler((object sender, FormClosingEventArgs e) => { ChatRoom.Close(); });
                    }
                }
            }
            else
            {
                MessageBox.Show("Your room's ID does not exist, please re-type the room's ID", "Incorrect room's ID");
                return;
            }
        }