public async Task <IActionResult> PutChatRoom(Guid id, ChatRoom newProperties) { var chatRoom = await _context.ChatRooms.FindAsync(id); if (chatRoom == null) { return(NotFound()); } chatRoom.Name = newProperties.Name; await _context.SaveChangesAsync(); return(NoContent()); }
public async Task <IActionResult> PutChatRoomParticipant(Guid roomId, Guid id, ChatRoomParticipant newProperties) { var chatRoomParticipant = await _context.ChatRoomParticipants .Where(p => p.ChatRoomId == roomId) .Where(p => p.Id == id) .SingleOrDefaultAsync(); if (chatRoomParticipant == null) { return(NotFound()); } chatRoomParticipant.Name = newProperties.Name; await _context.SaveChangesAsync(); return(NoContent()); }
public async Task <IActionResult> Login(Guid chatRoomId) { try { Guid userId = Guid.NewGuid(); var user = await _context.ChatRoomParticipants.FindAsync(userId); if (user == null) { var room = await _context.ChatRooms .Where(r => r.Id == chatRoomId) .SingleOrDefaultAsync(); if (room == null) { room = new ChatRoom { Id = chatRoomId, Name = $"{chatRoomId}" }; } user = new ChatRoomParticipant { Id = userId, ChatRoom = room, Name = Request.Headers["User-Agent"] }; _context.ChatRoomParticipants.Add(user); await _context.SaveChangesAsync(); } if (user == null) { throw new Exception($"User with ID {userId} not found"); } var claims = User.Claims .Where(x => x.Type != $"Room{chatRoomId}") .Concat(new[] { new Claim($"Room{chatRoomId}", userId.ToString()) }) .ToList(); var claimsIdentity = new ClaimsIdentity( claims, CookieAuthenticationDefaults.AuthenticationScheme); await HttpContext.SignInAsync( CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity)); return(RedirectToAction("Index", "Chat", new { chatRoomId })); } catch (Exception ex) { _logger.LogError(ex, "Could not log user in"); return(StatusCode(500)); } }
public async Task SendMessage(Guid chatRoomId, string message) { try { Guid userId = Context.User.Claims .Where(x => x.Type == $"Room{chatRoomId}") .Select(x => x.Value) .Select(Guid.Parse) .Single(); string name = await _context.ChatRoomParticipants .Where(u => u.Id == userId) .Select(u => u.Name) .SingleAsync(); Guid id = Guid.NewGuid(); await Clients.Group($"ChatRoom:{chatRoomId}").SendAsync("ReceiveMessage", new { Id = id, TextContent = message, Timestamp = DateTimeOffset.UtcNow, User = new { Id = userId, Name = name } }); try { var chatRoom = await _context.ChatRooms.FindAsync(chatRoomId); if (chatRoom == null) { chatRoom = new ChatRoom { Id = chatRoomId, Name = "Chat Room #1" }; } _context.ChatMessages.Add(new ChatMessage { Id = id, UserId = userId, TextContent = message, Timestamp = DateTimeOffset.UtcNow }); await _context.SaveChangesAsync(); } catch (Exception ex) { _logger.LogWarning(ex, "Could not save chat message to database"); } } catch (Exception ex) { _logger.LogWarning(ex, "Could not send chat message via SignalR - discarding"); return; } }
public async Task <ActionResult> DeleteChatRoomMessage(Guid roomId, Guid id) { var chatRoomMessage = await _context.ChatMessages .Include(p => p.User) .Where(p => p.User.ChatRoomId == roomId) .Where(p => p.Id == id) .SingleOrDefaultAsync(); if (chatRoomMessage == null) { return(NotFound()); } _context.ChatMessages.Remove(chatRoomMessage); await _context.SaveChangesAsync(); return(NoContent()); }