-
Notifications
You must be signed in to change notification settings - Fork 1
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
7bd4bab
commit e797c10
Showing
14 changed files
with
779 additions
and
103 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
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 |
---|---|---|
|
@@ -8,3 +8,7 @@ | |
color: black; | ||
padding-left:8px; | ||
} | ||
|
||
.page { | ||
height: calc(100vh - 61px); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,7 +46,7 @@ public void Initialize() | |
|
||
ApplicationUser user = new() | ||
{ | ||
UserName = "[email protected]", | ||
UserName = "AdminKrystian", | ||
Email = "[email protected]", | ||
EmailConfirmed = true, | ||
}; | ||
|
@@ -56,7 +56,7 @@ public void Initialize() | |
|
||
user = new() | ||
{ | ||
UserName = "player@example.com", | ||
UserName = "player", | ||
Email = "[email protected]", | ||
EmailConfirmed = true, | ||
}; | ||
|
@@ -66,7 +66,7 @@ public void Initialize() | |
|
||
user = new() | ||
{ | ||
UserName = "gm@example.com", | ||
UserName = "gm", | ||
Email = "[email protected]", | ||
EmailConfirmed = true, | ||
}; | ||
|
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,228 @@ | ||
@using Microsoft.AspNetCore.SignalR.Client | ||
@inject IChatManager _chatManager | ||
@inject IJSRuntime _jsRuntime | ||
@inject NavigationManager _navigationManager | ||
@inject AuthenticationStateProvider _stateProvider | ||
|
||
@attribute [Authorize(Roles = SD.Role_Admin + "," + SD.Role_HeroPlayer + "," + SD.Role_DukePlayer + "," + SD.Role_GameMaster)] | ||
|
||
<div class="d-flex flex-grow-1 flex-row"> | ||
<MudPaper Elevation="25" Class="py-4 flex-grow-1"> | ||
<MudToolBar Dense="true"> | ||
@if (string.IsNullOrEmpty(ContactId)) | ||
{ | ||
<MudIcon Icon="@Icons.Material.Outlined.Person" Style="margin-right:10px"></MudIcon> | ||
<MudText Typo="Typo.h6">chat</MudText> | ||
} | ||
else | ||
{ | ||
<MudIcon Icon="@Icons.Material.Outlined.ChatBubble" Style="margin-right:10px"></MudIcon> | ||
<MudText Typo="Typo.h6">@ContactEmail</MudText> | ||
} | ||
</MudToolBar> | ||
<div class="d-flex flex-column px-4" style="max-height:65vh;min-height:65vh; overflow:scroll;" id="chatContainer"> | ||
@foreach (var message in messages) | ||
{ | ||
@if (message.FromUser.Email == CurrentUserEmail) | ||
{ | ||
<div class="d-flex flex-row my-4"> | ||
<div class="mr-4"> | ||
<MudAvatar Color="Color.Secondary" Style="height:50px; width:50px;">@message.FromUser.Email.ToUpper().FirstOrDefault()</MudAvatar> | ||
</div> | ||
<div> | ||
<MudText Typo="Typo.body1">@message.FromUser.Email</MudText> | ||
<MudText Typo="Typo.caption" Style="font-size: xx-small!important;">@message.CreatedDate.ToString("dd MMM, yyyy hh:mm tt")</MudText> | ||
<MudText Typo="Typo.body2" Style=" padding: 15px;background-color: var(--mud-palette-background-grey);border-radius: 12px;margin-top:5px">@message.Message</MudText> | ||
</div> | ||
</div> | ||
} | ||
else | ||
{ | ||
<div class="d-flex flex-row-reverse my-4"> | ||
<div class="mr-4"> | ||
<MudAvatar Color="Color.Primary" Style="height:50px; width:50px;">@message.FromUser.Email.ToUpper().FirstOrDefault()</MudAvatar> | ||
</div> | ||
<div> | ||
<MudText Typo="Typo.body1">@message.FromUser.Email</MudText> | ||
<MudText Typo="Typo.caption" Style="font-size: xx-small!important;">@message.CreatedDate.ToString("dd MMM, yyyy hh:mm tt")</MudText> | ||
<MudText Typo="Typo.body2" Style=" padding: 15px;background-color: var(--mud-palette-background-grey);border-radius: 12px;margin-top:5px">@message.Message</MudText> | ||
</div> | ||
</div> | ||
} | ||
|
||
|
||
} | ||
</div> | ||
<MudPaper Elevation="25" Class="d-flex flex-row px-2 mx-4" Style=""> | ||
<MudTextField Disabled="!IsUserChosen" @ref="ChatTextField" Immediate="true" T="string" Placeholder="Enter your message..." DisableUnderLine="true" Class="mt-n2 mx-4" | ||
@bind-Value="CurrentMessage" For="@(()=> CurrentMessage)" OnKeyDown="Enter" /> | ||
<MudButton Disabled="!IsUserChosen" OnClick="SubmitAsync" StartIcon="@Icons.Material.Outlined.Send" Color="Color.Secondary" ButtonType="MudBlazor.ButtonType.Button">Send</MudButton> | ||
</MudPaper> | ||
</MudPaper> | ||
<MudPaper Elevation="25" Class="pa-3 ml-6" MinWidth="350px"> | ||
<MudToolBar Dense="true"> | ||
<MudText Typo="Typo.h6" Inline="true" Class="mr-2">#</MudText> | ||
<MudText Typo="Typo.h6">contacts</MudText> | ||
</MudToolBar> | ||
<div class="d-flex flex-column px-4" style="max-height:70vh;min-height:70vh; overflow:scroll;"> | ||
<MudList Clickable="true"> | ||
@foreach (var user in ChatUsers) | ||
{ | ||
<MudListItem Class="pa-0 px-2" OnClick="@(() => LoadUserChat(user.Id))"> | ||
<div class="d-flex flex-row mt-n1 mb-n1"> | ||
<div class="mr-4"> | ||
<MudBadge Class="my-2"> | ||
@if (user.Id == ContactId) | ||
{ | ||
<MudAvatar Color="Color.Secondary" Style="height:50px; width:50px;">@user.Email.ToUpper().FirstOrDefault()</MudAvatar> | ||
} | ||
else | ||
{ | ||
<MudAvatar Color="Color.Dark" Style="height:50px; width:50px;">@user.Email.ToUpper().FirstOrDefault()</MudAvatar> | ||
} | ||
</MudBadge> | ||
</div> | ||
<div> | ||
<MudText Typo="Typo.body2" Class="mt-3 mb-n2">@user.Email</MudText>@* | ||
<MudText Typo="Typo.caption" Style="font-size: xx-small!important;">@user.Id</MudText> *@ | ||
</div> | ||
</div> | ||
</MudListItem> | ||
} | ||
</MudList> | ||
</div> | ||
</MudPaper> | ||
</div> | ||
|
||
@code{ | ||
[CascadingParameter] public HubConnection hubConnection { get; set; } | ||
[Parameter] public string CurrentMessage { get; set; } | ||
[Parameter] public string CurrentUserId { get; set; } | ||
[Parameter] public string CurrentUserEmail { get; set; } | ||
|
||
MudTextField<string> ChatTextField = new(); | ||
private string HubUrl; | ||
private List<ChatMessage> messages = new List<ChatMessage>(); | ||
private async Task SubmitAsync() | ||
{ | ||
if(IsUserChosen==false) | ||
return; | ||
if (!string.IsNullOrEmpty(CurrentMessage) && !string.IsNullOrEmpty(ContactId)) | ||
{ | ||
//Save Message to DB | ||
var chatHistory = new ChatMessage() | ||
{ | ||
Message = CurrentMessage, | ||
ToUserId = ContactId, | ||
CreatedDate = DateTime.Now | ||
|
||
}; | ||
|
||
chatHistory.FromUserId = CurrentUserId; | ||
await hubConnection.SendAsync("SendMessage", CurrentUserEmail, chatHistory); | ||
CurrentMessage = string.Empty; | ||
await _chatManager.SaveMessageAsync(chatHistory); | ||
|
||
} | ||
} | ||
protected override async Task OnAfterRenderAsync(bool firstRender) | ||
{ | ||
await _jsRuntime.ScrollToBottom("chatContainer"); | ||
} | ||
protected override async Task OnInitializedAsync() | ||
{ | ||
try | ||
{ | ||
if (hubConnection == null) | ||
{ | ||
hubConnection = new HubConnectionBuilder().WithUrl(_navigationManager.ToAbsoluteUri(ChatHub.HubUrl)).Build(); | ||
} | ||
if (hubConnection.State == HubConnectionState.Disconnected) | ||
{ | ||
await hubConnection.StartAsync(); | ||
} | ||
|
||
hubConnection.On<string, ChatMessage>("SendMessage", ReceiveMessage); | ||
|
||
await GetUsersAsync(); | ||
var state = await _stateProvider.GetAuthenticationStateAsync(); | ||
var user = state.User; | ||
CurrentUserId = user.FindFirst(u => u.Type.Contains("nameidentifier"))?.Value; | ||
CurrentUserEmail = user.FindFirst(u => u.Type.Contains("emailaddress"))?.Value; | ||
if (!string.IsNullOrEmpty(ContactId)) | ||
{ | ||
await LoadUserChat(ContactId); | ||
} | ||
|
||
// await SendAsync($"[Notice] {_username} joined chat room."); | ||
} | ||
catch (Exception e) | ||
{ | ||
; | ||
// _message = $"ERROR: Failed to start chat client: {e.Message}"; | ||
// _isChatting = false; | ||
} | ||
|
||
} | ||
|
||
private async Task ReceiveMessage(string name, ChatMessage message) | ||
{ | ||
if ((ContactId == message.ToUserId && CurrentUserId == message.FromUserId) || (ContactId == message.FromUserId && CurrentUserId == message.ToUserId)) | ||
{ | ||
|
||
if ((ContactId == message.ToUserId && CurrentUserId == message.FromUserId)) | ||
{ | ||
messages.Add(new ChatMessage { Message = message.Message, CreatedDate = message.CreatedDate, FromUser = new ApplicationUser() { Email = CurrentUserEmail } }); | ||
//await hubConnection.SendAsync("ChatNotificationAsync", $"New Message From {name}", ContactId, CurrentUserId); | ||
} | ||
else if ((ContactId == message.FromUserId && CurrentUserId == message.ToUserId)) | ||
{ | ||
messages.Add(new ChatMessage { Message = message.Message, CreatedDate = message.CreatedDate, FromUser = new ApplicationUser() { Email = ContactEmail } }); | ||
} | ||
|
||
// Inform blazor the UI needs updating | ||
InvokeAsync(StateHasChanged); | ||
|
||
await Task.Delay(1000); | ||
|
||
await _jsRuntime.ScrollToBottom("chatContainer"); | ||
} | ||
} | ||
|
||
public async Task Enter(KeyboardEventArgs e) | ||
{ | ||
if (e.Code == "Enter" || e.Code == "NumpadEnter") | ||
{ | ||
ChatTextField.TextUpdateSuppression = false; | ||
await SubmitAsync(); | ||
CurrentMessage = string.Empty; | ||
StateHasChanged(); | ||
await Task.Run(async () => | ||
{ | ||
await Task.Delay(150); | ||
ChatTextField.TextUpdateSuppression = true; | ||
}); | ||
} | ||
} | ||
|
||
public List<ApplicationUser> ChatUsers = new List<ApplicationUser>(); | ||
public string ContactEmail { get; set; } | ||
public string ContactId { get; set; } | ||
private bool IsUserChosen { get; set; } = false; | ||
async Task LoadUserChat(string userId) | ||
{ | ||
var contact = await _chatManager.GetUserDetailsAsync(userId); | ||
ContactId = contact.Id; | ||
ContactEmail = contact.Email; | ||
_navigationManager.NavigateTo($"chat/{ContactId}"); | ||
messages = new List<ChatMessage>(); | ||
messages = await _chatManager.GetConversationAsync(ContactId); | ||
IsUserChosen = true; | ||
} | ||
private async Task GetUsersAsync() | ||
{ | ||
ChatUsers = await _chatManager.GetUsersAsync(); | ||
} | ||
|
||
} |
Oops, something went wrong.