public async Task <IActionResult> Change([FromBody] UserChangeForm form) { try { Identity id = JsonSerializer.Deserialize <Identity>(User.Identity.Name); if (id.Guest != null) { return(Forbid()); } if (!(form.Name?.Length >= 4) && !(form.Password?.Length >= 6)) { return(BadRequest(Errors.EmptyRequest)); } if (form.Name?.Length >= 4 && !Helper.isRightName(form.Name)) { return(BadRequest(Errors.BadName)); } var user = await _context.Users.FindAsync(id.UserId); if (user == null) { throw new Exception("Invalid Token"); } if (form.Name?.Length >= 4) { user.Name = form.Name; } if (form.Password?.Length >= 6) { user.Password = form.Password; } _context.Users.Update(user); await _context.SaveChangesAsync(); var connections = _state.ChangeUser(id.UserId, user.Name); if (connections.Count() > 0) { await _hub.Clients.Clients(connections).SendAsync("usernameChanged", new { id = id.UserId, name = user.Name }); } id.Name = user.Name; return(Ok(Helper.GetToken(JsonSerializer.Serialize(id)))); } catch (Exception ex) { return(StatusCode(StatusCodes.Status500InternalServerError, ex)); } }
public async Task <IActionResult> Register([FromBody] RegForm data) { try { if (!Helper.isRightName(data.Name)) { return(BadRequest(Errors.BadName)); } if (_context.Users.FirstOrDefault(u => u.Email == data.Email) != null) { return(BadRequest(Errors.EmailTaken)); } var limit = DateTime.UtcNow.Subtract(new TimeSpan(1, 0, 0)).Ticks; var range = _context.RegQueue.Where(e => e.Date < limit); _context.RegQueue.RemoveRange(range); var rand = new Random(); string key; do { key = rand.Next(100000000, 999999999).ToString(); }while (_context.RegQueue.FirstOrDefault(e => e.Key == key) != null); await _context.RegQueue.AddAsync(new RegQueueEntity { Key = key, Date = DateTime.UtcNow.Ticks, Name = data.Name, Email = data.Email, Password = data.Password }); await _context.SaveChangesAsync(); string content = $"Hello {data.Name}. Please follow the link to confirm your email addresss.\n{Helper.Settings.EmailConfirmAddr + key}"; if (!await Helper.SendMail(data.Email, content)) { throw new Exception("Failed to send a confirmation email."); } return(Ok("ok")); } catch (Exception ex) { return(StatusCode(StatusCodes.Status500InternalServerError, ex)); } }
public async Task ClearMessages(long from, long till) { await Task.Run(async() => { Identity _id = JsonSerializer.Deserialize <Identity>(Context.User.Identity.Name); if (_id.UserId == 0) { return; } if (!_state.ClearMessages(_id.UserId, Context.ConnectionId, from, till)) { return; } var room = await _context.Rooms.Include(r => r.Messages).FirstOrDefaultAsync(r => r.UserId == _id.UserId); if (room == null) { return; } if (from == 0 && till == 0) { _context.Messages.RemoveRange(room.Messages); } else if (till == 0) { _context.Messages.RemoveRange(room.Messages.Where(m => m.TimeStamp > from)); } else { if (from > till) { return; } _context.Messages.RemoveRange(room.Messages.Where(m => m.TimeStamp > from && m.TimeStamp < till)); } await _context.SaveChangesAsync(); }); }
public async Task <IActionResult> Change([FromBody] RoomForm form) { try { Identity id = JsonSerializer.Deserialize <Identity>(User.Identity.Name); if (id.Guest != null) { return(Forbid()); } if (!Helper.isRighGrouptName(form.Name)) { return(BadRequest(Errors.BadName)); } if (!StaticData.CountryCodes.Contains(form.Country)) { return(BadRequest(Errors.WrongCountry)); } var room = await _context.Rooms.FirstOrDefaultAsync(r => r.UserId == id.UserId); var slug = Helper.Slugify(form.Name); string[] connectionIds = null; if (room == null) { if (await _context.Rooms.AnyAsync(r => r.Slug == slug)) { return(BadRequest(Errors.RoomNameExist)); } await _context.Rooms.AddAsync(new Room { Name = form.Name, Slug = slug, Description = form.Description, Country = form.Country, Password = form.Password, Limit = form.Limit, UserId = id.UserId }); } else { if (room.Slug != slug && await _context.Rooms.AnyAsync(r => r.Slug == slug)) { return(BadRequest(Errors.RoomNameExist)); } if (room.Name != form.Name || room.Country != form.Country) { connectionIds = _state.Connections(room.RoomId); } room.Name = form.Name; room.Slug = slug; room.Description = form.Description; room.Country = form.Country; room.Password = form.Password; room.Limit = form.Limit; _context.Rooms.Update(room); } await _context.SaveChangesAsync(); if (connectionIds != null) { await _hub.Clients.Clients(connectionIds).SendAsync("roomChanged", new { name = form.Name, flag = form.Country }); } return(Ok("ok")); } catch (Exception ex) { return(StatusCode(StatusCodes.Status500InternalServerError, ex)); } }