-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2478c49
commit c4b8fa4
Showing
12 changed files
with
219 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 113 additions & 3 deletions
116
Sharecode.Backend.Application/Features/Live/Snippet/JoinedSnippetLiveHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,121 @@ | ||
using MediatR; | ||
using Sharecode.Backend.Application.Client; | ||
using Sharecode.Backend.Application.Service; | ||
|
||
namespace Sharecode.Backend.Application.Features.Live.Snippet; | ||
|
||
public class JoinedSnippetLiveHandler : IRequestHandler<JoinedSnippetEvent, JoinedSnippetResponse?> | ||
public class JoinedSnippetLiveHandler(IHttpClientContext clientContext, IGroupStateManager groupStateManager, ISnippetService snippetService, IUserService userService) : IRequestHandler<JoinedSnippetEvent, JoinedSnippetResponse?> | ||
{ | ||
public Task<JoinedSnippetResponse?> Handle(JoinedSnippetEvent request, CancellationToken cancellationToken) | ||
public async Task<JoinedSnippetResponse?> Handle(JoinedSnippetEvent request, CancellationToken cancellationToken) | ||
{ | ||
throw new NotImplementedException(); | ||
var requestingUser = await clientContext.GetUserIdentifierAsync() ?? Guid.Empty; | ||
//If the requests doesn't have an authorization token. | ||
//Scenario - Public Snippets | ||
bool isRequestAnonymous = requestingUser == Guid.Empty; | ||
// Get the users access to work on snippets | ||
//If there is no authorization token, the request would be sent with Empty Guid, which won't match the select | ||
//statement with in the access control table. | ||
var snippetAccess = await snippetService.GetSnippetAccess(request.SnippetId, requestingUser , false); | ||
//If the user doesn't have any access to that particular snippet, then return null. If the snippet is public | ||
//the fn will return read access | ||
if (!snippetAccess.Any()) | ||
{ | ||
return null; | ||
} | ||
|
||
//Get all the existing members in the signalR group | ||
var currentMembers = await groupStateManager.Members(request.SnippetId.ToString(), cancellationToken); | ||
//The way we store it in SignalR is, if there is a validated user, he/she will be a Guid, if its a string, it will | ||
//be an anonymous user. | ||
var loggedInUsers = currentMembers.Values.Select(x => | ||
{ | ||
bool success = Guid.TryParse(x, out var parsed); | ||
return new { success, parsed }; | ||
}) | ||
.Where(x => x.success) | ||
.Select(x => x.parsed) | ||
.ToHashSet(); | ||
//If the requesting user is logged in, then also add him/her too, so that we can get the logo and all of this newly | ||
//joined user too | ||
if (!isRequestAnonymous) | ||
{ | ||
loggedInUsers.Add(requestingUser); | ||
} | ||
|
||
//Get User Profile Information | ||
var userEnumerable = await userService.GetUsersProfileInformationAsync(loggedInUsers, cancellationToken); | ||
var activeUsers = userEnumerable | ||
.Select(x => | ||
new ActiveSnippetUsersDto() | ||
{ | ||
Id = x.Id, | ||
FullName = x.FullName, | ||
ProfilePicture = x.ProfilePicture | ||
}) | ||
.ToDictionary(x => x.Id!.Value, x => x); | ||
|
||
List<ActiveSnippetUsersDto> responseUsers = []; | ||
//Loop through the current members in the group | ||
foreach (var (connectionId, userIdentifier) in currentMembers) | ||
{ | ||
//If the current member in the group is a Guid, that means he/she was a logged in user | ||
if (Guid.TryParse(userIdentifier, out var uniqueUserId)) | ||
{ | ||
//Check whether such a user was there in the response returned from Db | ||
if (activeUsers.TryGetValue(uniqueUserId, out var activeUser)) | ||
{ | ||
//If there is attach connectionId and also add to the return array | ||
activeUser.ConnectionId = connectionId; | ||
responseUsers.Add(activeUser); | ||
} | ||
} | ||
else | ||
{ | ||
//In this else case, it means the user is a non-logged in user | ||
//In this case, only attach the name and connection Id we have | ||
var anonymousUser = new ActiveSnippetUsersDto() | ||
{ | ||
ConnectionId = connectionId, | ||
FullName = userIdentifier, | ||
Id = null, | ||
ProfilePicture = null | ||
}; | ||
responseUsers.Add(anonymousUser); | ||
} | ||
} | ||
|
||
var response = new JoinedSnippetResponse() | ||
{ | ||
JoinedUserAccesses = snippetAccess.ToControlModel(), | ||
JoinedUserId = Guid.Empty, | ||
ActiveUsers = responseUsers | ||
}; | ||
|
||
//If the requesting user was not logged in or if there is no data | ||
//associated with the requested user in the dbResponse | ||
//Set him as an anonymous User, also add him in the array of current users | ||
if (isRequestAnonymous || !activeUsers.ContainsKey(requestingUser)) | ||
{ | ||
response.JoinedUserName = "Anonymous User"; | ||
var anonymousUser = new ActiveSnippetUsersDto() | ||
{ | ||
ConnectionId = request.ConnectionId, | ||
FullName = response.JoinedUserName, | ||
Id = null, | ||
ProfilePicture = null | ||
}; | ||
responseUsers.Add(anonymousUser); | ||
} | ||
else | ||
{ | ||
//If there is data, Set the UserName and UserId for the newly joined user | ||
//Also add him in the current users array | ||
var activeSnippetUsersDto = activeUsers[requestingUser]; | ||
responseUsers.Add(activeSnippetUsersDto); | ||
response.JoinedUserName = activeSnippetUsersDto.FullName; | ||
response.JoinedUserId = activeSnippetUsersDto.Id; | ||
} | ||
|
||
return response; | ||
} | ||
} |
13 changes: 8 additions & 5 deletions
13
Sharecode.Backend.Application/Features/Live/Snippet/JoinedSnippetResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,20 @@ | ||
namespace Sharecode.Backend.Application.Features.Live.Snippet; | ||
using Sharecode.Backend.Domain.Dto.Snippet; | ||
|
||
namespace Sharecode.Backend.Application.Features.Live.Snippet; | ||
|
||
public class JoinedSnippetResponse | ||
{ | ||
public Guid SnippetId { get; init; } | ||
public Guid? JoinedUserId { get; init; } | ||
public string JoinedUserName { get; init; } | ||
public List<ActiveSnippetUsersDto> ActiveUsers { get; } = []; | ||
public Guid? JoinedUserId { get; set; } | ||
public string JoinedUserName { get; set; } | ||
public SnippetAccessControlDto JoinedUserAccesses { get; set; } | ||
public List<ActiveSnippetUsersDto> ActiveUsers { get; set; } = []; | ||
} | ||
|
||
public sealed record ActiveSnippetUsersDto | ||
{ | ||
public Guid? Id { get; init; } | ||
public string ConnectionId { get; init; } | ||
public string ConnectionId { get; set; } | ||
public string FullName { get; init; } | ||
public string? ProfilePicture { get; init; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Sharecode.Backend.Domain.Enums; | ||
|
||
public enum SnippetAccess | ||
{ | ||
Read, | ||
Write, | ||
Manage | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters