コード例 #1
0
        public byte[] GetMyAvatar(string username)
        {
            var filter = Builders <Account> .Filter.Eq("_id", username);

            var acc    = accounts.Find(filter).FirstOrDefault();
            var avatar = Encoding.UTF8.GetBytes(encryptService.Decrypt(Encoding.UTF8.GetString(acc.avatar)));

            return(avatar);
        }
コード例 #2
0
        public async Task <List <Message> > GetMessages(string roomid, string username)
        {
            var filter = Builders <Message> .Filter.Empty;
            var task   = memoryService.ChatRooms[roomid].GetCollection <Message>(username).DeleteManyAsync(filter);
            var res    = memoryService.RoomMessages[roomid].Find(filter).ToList();

            res.Sort();
            foreach (Message m in res)
            {
                m.content = encryptService.Decrypt(m.content);
            }
            await task;

            if (res is not null)
            {
                return(res);
            }
            return(res);
        }
コード例 #3
0
        public async Task <SignResult> SignIn([FromBody] Account acc)
        {
            var filter = Builders <Account> .Filter.Eq("_id", acc.username);

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

            var myacc = accounts.Find(filter).FirstOrDefault();

            if (myacc is not null)
            {
                if (encryptService.Decrypt(myacc.password) == acc.password)
                {
                    if (myacc.connected)
                    {
                        return(new SignResult()
                        {
                            success = false, err = "Your account has already been online"
                        });
                    }
                    var task = accounts.UpdateOneAsync(filter, update);
                    memoryService.OnlineTrack[acc.username]          = new(300000);
                    memoryService.OnlineTrack[acc.username].Elapsed += (s, e) =>
                    {
                        var filter = Builders <Account> .Filter.Eq("_id", acc.username);

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

                        accounts.UpdateOne(filter, update);
                        memoryService.OnlineTrack[acc.username].Stop();
                        memoryService.OnlineTrack.Remove(acc.username);
                        memoryService.OnlineTrack[acc.username].Close();
                    };
                    memoryService.OnlineTrack[acc.username].Enabled = true;
                    await task;
                    return(new SignResult()
                    {
                        success = true, avatar = myacc.avatar, rooms = myacc.rooms
                    });
                }
                return(new SignResult()
                {
                    success = false, err = "Incorrect password"
                });
            }
            return(new SignResult()
            {
                success = false, err = "Incorrect username"
            });
        }