public async Task <IActionResult> Create([FromForm] string name, [FromForm] string notes, [FromForm] string password)
        {
            try
            {
                var username = HttpContext.User.Identity.Name;
                if (username == null)
                {
                    return(Forbid());
                }
                var owner = await Db.GetUser(username);

                if (owner == null)
                {
                    return(Forbid());
                }
                var session = await Db.InsertSession(name, notes, owner, password);

                return(Json(session));
            }
            catch (Exception e)
            {
                Logger.LogError($"{nameof(Create)} failed: {e.Message}");
                return(Forbid());
            }
        }
        public async Task <IActionResult> AddToken([FromForm] string name, [FromForm] string description, [FromForm] bool isprivate, [FromForm] IFormFile icon)
        {
            try
            {
                var currentusername = HttpContext.User.Identity.Name;
                if (currentusername == null)
                {
                    return(Forbid());
                }
                var currentuser = await Db.GetUser(currentusername);

                if (!(currentuser is User _))
                {
                    throw new Exception($"No user called {currentusername} found");
                }
                using var stream = StreamManager.GetStream();
                await icon.CopyToAsync(stream);

                var iconbin = stream.ToArray();
                var token   = await Db.AddTokenToUser(currentuser.Id, name, description, isprivate, iconbin);

                if (token == null)
                {
                    throw new Exception($"Tokencreate failed");
                }
                return(Json(token.UUID));
            }
            catch (Exception e)
            {
                Logger.LogError($"{nameof(AddToken)} failed: {e.Message}");
                throw e;
            }
        }
Exemplo n.º 3
0
        public async Task <IActionResult> List()
        {
            try
            {
                var username = HttpContext.User.Identity.Name;
                var user     = await Db.GetUser(username);

                var tokens = await Db.GetTokens(user.Id);

                return(Json(tokens));
            }
            catch (Exception e)
            {
                Logger.LogError($"{nameof(List)} failed: {e.Message}");
                return(Forbid());
            }
        }
Exemplo n.º 4
0
        public async Task <IActionResult> GetRecent([FromForm] long id) //TODO Weird stuff
        {
            var currentusername = HttpContext.User.Identity.Name;

            if (currentusername == null)
            {
                throw new System.Exception($"User not logged in");
            }
            var currentuser = await Db.GetUser(currentusername);

            if (!(currentuser is User _))
            {
                throw new System.Exception($"No user called {currentusername} found");
            }
            var session = await Db.GetSession(id, currentuser.Id);

            if (!(session is ExtendedSessionView _))
            {
                throw new System.ArgumentException("SessionId not valid or User not in Session");
            }
            ChatMessageView[] messages = await Db.GetChatMessages(id);

            return(Json(messages));
        }