Skip to content

Commit

Permalink
removed db stuff in signalR
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabefire committed Jun 12, 2024
1 parent 8ef08df commit 00b209d
Show file tree
Hide file tree
Showing 8 changed files with 8 additions and 140 deletions.
62 changes: 5 additions & 57 deletions TalkWaveApi.WebSocket/Hubs/ChatHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,8 @@ namespace TalkWaveApi.WebSocket.Hubs
[Authorize]
public class ChatHub : Hub
{
private readonly DatabaseContext _context;
public ChatHub(DatabaseContext dbContext)
{
_context = dbContext;
}
public async Task JoinGroup(string groupId)
{
string? userId = Context.UserIdentifier;

if (!int.TryParse(userId, out int userParsedId))
{
Context.Abort();
return;
}

await ValidateChannel(groupId, userParsedId, Context);
await Groups.AddToGroupAsync(Context.ConnectionId, groupId);

}
Expand All @@ -40,59 +26,21 @@ public async Task LeaveGroup(string groupId)
}
public async Task SendMessage(string groupId, string message)
{
string? userId = Context.UserIdentifier;
if (!int.TryParse(userId, out int userParsedId))
{
Context.Abort();
return;
}
User? user = await _context.Users.FindAsync(userParsedId);
if (user == null)
string? userEmail = Context.UserIdentifier;

if (userEmail == null)
{
Context.Abort();
return;
}

Message dbMessage = new()
{
UserId = userParsedId,
ChannelId = int.Parse(groupId),
Content = message,
CreatedAt = DateTime.UtcNow
};

MessageDto messageDto = new()
{
Author = user.UserName,
Author = userEmail,
Content = message,
CreatedAt = DateTime.UtcNow,
};
await _context.Messages.AddAsync(dbMessage);
await _context.SaveChangesAsync();

await Clients.Group(groupId).SendAsync("ReceiveMessage", user.UserId, messageDto);
}
public async Task ValidateChannel(string Id, int UserId, HubCallerContext context)
{
//Validate Id can be casted as int
if (!int.TryParse(Id, out int ChannelId))
{
context.Abort();
}

//Validate channel exists
var channel = await _context.Channels.FindAsync(ChannelId);
if (channel == null)
{
context.Abort();
}

//Validate user is in channel
var userTest = await _context.ChannelUsersStatuses.Where(x => x.ChannelId == ChannelId).Where(x => x.UserId == UserId).FirstOrDefaultAsync();
if (userTest == null)
{
context.Abort();
}
await Clients.Group(groupId).SendAsync("ReceiveMessage", userEmail, messageDto);
}
};
}
Expand Down
14 changes: 0 additions & 14 deletions TalkWaveApi.WebSocket/Models/Channel.cs

This file was deleted.

14 changes: 0 additions & 14 deletions TalkWaveApi.WebSocket/Models/ChannelUserStatus.cs

This file was deleted.

15 changes: 0 additions & 15 deletions TalkWaveApi.WebSocket/Models/Message.cs

This file was deleted.

18 changes: 0 additions & 18 deletions TalkWaveApi.WebSocket/Models/User.cs

This file was deleted.

13 changes: 2 additions & 11 deletions TalkWaveApi.WebSocket/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");

builder.Services.AddSingleton<IUserIdProvider, UserIdProvider>();
builder.Services.AddSingleton<IUserIdProvider, EmailBasedUserIdProvider>();

builder.Services.AddCors(p => p.AddPolicy("dev", builder =>
{
Expand All @@ -29,15 +29,6 @@
builder.WithOrigins("https://talkwaveapp.com").AllowAnyMethod().AllowAnyHeader().AllowCredentials();
}));


builder.Services.AddDbContext<DatabaseContext>(options =>
options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection"), x =>
{
x.EnableRetryOnFailure();
}));

builder.Services.AddDatabaseDeveloperPageExceptionFilter();

builder.Services.AddHealthChecks();

builder.Services.AddAuthentication(options =>
Expand Down Expand Up @@ -113,6 +104,6 @@ public class EmailBasedUserIdProvider : IUserIdProvider
{
public virtual string GetUserId(HubConnectionContext connection)
{
return connection.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value!;
return connection.User?.FindFirst(ClaimTypes.Email)?.Value!;
}
}
11 changes: 0 additions & 11 deletions TalkWaveApi.WebSocket/Services/UserIdProvider.cs

This file was deleted.

1 change: 1 addition & 0 deletions TalkWaveApi/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ private string CreateToken(User user)
List<Claim> claims =
[
new(ClaimTypes.NameIdentifier, user.UserId.ToString()),
new(ClaimTypes.Email, user.Email),
];

var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(
Expand Down

0 comments on commit 00b209d

Please sign in to comment.