Skip to content

Commit

Permalink
get rid of changes from main
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-scales committed Jan 21, 2024
1 parent ed22170 commit d473e96
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions FU.API/FU.API/Hubs/ChatHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ChatHub : Hub
/// <summary>
/// List of the connected users names.
/// </summary>
private static List<string> connectedUsers;
public static readonly List<string> ConnectedUsers = new();

Check warning on line 19 in FU.API/FU.API/Hubs/ChatHub.cs

View workflow job for this annotation

GitHub Actions / test


/// <summary>
/// The app db context.
Expand All @@ -30,62 +30,61 @@ public class ChatHub : Hub
public ChatHub(AppDbContext context)
{
_context = context;
connectedUsers = new List<string>();
}

/// <summary>
/// Connect the user to the hub.
/// </summary>
/// <returns>Task.</returns>
public async override Task OnConnectedAsync()
public override Task OnConnectedAsync()
{
var userId = UserId;
var user = _context.Users.Find(userId);

if (user is null || user.Username is null)
{
return;
return Task.CompletedTask;
}

if (!connectedUsers.Contains(user.Username))
if (!ConnectedUsers.Contains(user.Username))
{
connectedUsers.Add(user.Username);
ConnectedUsers.Add(user.Username);
}

// Update the online status of the user
user.IsOnline = true;
_context.Users.Update(user);
await _context.SaveChangesAsync();
_context.SaveChanges();

await base.OnConnectedAsync();
return base.OnConnectedAsync();
}

/// <summary>
/// Disconnect the user from the hub.
/// </summary>
/// <param name="exception">Exception.</param>
/// <returns>Task.</returns>
public override async Task OnDisconnectedAsync(Exception? exception)
public override Task OnDisconnectedAsync(Exception? exception)
{
var userId = UserId;
var user = _context.Users.Find(userId);

if (user is null || user.Username is null)
{
return;
return Task.CompletedTask;
}

if (connectedUsers.Contains(user.Username))
if (ConnectedUsers.Contains(user.Username))
{
connectedUsers.Remove(user.Username);
ConnectedUsers.Remove(user.Username);
}

// Update the online status of the user
user.IsOnline = false;
_context.Users.Update(user);
await _context.SaveChangesAsync();
_context.SaveChanges();

base.OnDisconnectedAsync(exception);
return base.OnDisconnectedAsync(exception);
}

/// <summary>
Expand Down Expand Up @@ -114,4 +113,4 @@ public async Task LeaveChatGroup(int chatId)
private int UserId => Context?.User?.Claims?.FirstOrDefault(c => c.Type == CustomClaimTypes.UserId)?.Value is not null
? int.Parse(Context.User.Claims.FirstOrDefault(c => c.Type == CustomClaimTypes.UserId)?.Value ?? string.Empty)
: -1;
}
}

0 comments on commit d473e96

Please sign in to comment.