From ce38633766838f295c24623fb3ad51ea0d237c6b Mon Sep 17 00:00:00 2001 From: Pistasj Date: Mon, 29 May 2023 22:18:24 +0200 Subject: [PATCH] nesten --- Hubs/ChatHub.cs | 21 +-- .../20230327073724_FuckThisShitv2.Designer.cs | 40 ----- Migrations/20230327073724_FuckThisShitv2.cs | 34 ---- ...39_sqlite.local_migration_918.Designer.cs} | 21 +-- ...30529185839_sqlite.local_migration_918.cs} | 2 +- Migrations/ChattingContextModelSnapshot.cs | 17 -- Model.cs | 14 -- Pages/Chat.razor | 25 ++- Pages/Index.razor | 159 +++--------------- Program.cs | 1 + Shared/SurveyPrompt.razor | 16 -- 11 files changed, 49 insertions(+), 301 deletions(-) delete mode 100644 Migrations/20230327073724_FuckThisShitv2.Designer.cs delete mode 100644 Migrations/20230327073724_FuckThisShitv2.cs rename Migrations/{20230526180433_sqlite.local_migration_189.Designer.cs => 20230529185839_sqlite.local_migration_918.Designer.cs} (77%) rename Migrations/{20230526180433_sqlite.local_migration_189.cs => 20230529185839_sqlite.local_migration_918.cs} (97%) delete mode 100644 Shared/SurveyPrompt.razor diff --git a/Hubs/ChatHub.cs b/Hubs/ChatHub.cs index adbc10a..fd7ce0d 100644 --- a/Hubs/ChatHub.cs +++ b/Hubs/ChatHub.cs @@ -4,21 +4,6 @@ namespace LiveChat.Server.Hubs; public class ChatHub : Hub { - public async Task SendMessage(string user, string message, string room) - { - using (var db = new ChattingContext()) - { - var messageData = new ChatMessage - { - User = user, - Message = message, - }; - var dbSave = db.Messages.Add(messageData); - await Clients.AllExcept("LiveChat Room Users").SendAsync("ReceiveMessage", user, message); - db.SaveChanges(); - } - } - public async Task SendMessageRoom(string user, string message, string room) { using (var db = new ChattingContext()) @@ -27,9 +12,9 @@ public class ChatHub : Hub { User = user, Message = message, - RoomId = room + RoomId = room, }; - var dbSave = db.RoomChatMessages.Add(messageData); + db.RoomChatMessages.Add(messageData); await Clients.Group(room).SendAsync("ReceiveMessage", user, message); db.SaveChanges(); } @@ -38,7 +23,7 @@ public class ChatHub : Hub public async Task JoinRoom(string room, string user) { await Groups.AddToGroupAsync(Context.ConnectionId, room); - await Groups.AddToGroupAsync(Context.ConnectionId, "Folk i rom"); + await Groups.AddToGroupAsync(Context.ConnectionId, "Folk_i_rom"); await Clients.Caller.SendAsync("ConnectionIdReceive", Context.ConnectionId); } } diff --git a/Migrations/20230327073724_FuckThisShitv2.Designer.cs b/Migrations/20230327073724_FuckThisShitv2.Designer.cs deleted file mode 100644 index 6c3bf60..0000000 --- a/Migrations/20230327073724_FuckThisShitv2.Designer.cs +++ /dev/null @@ -1,40 +0,0 @@ -// -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace LiveChat.Migrations -{ - [DbContext(typeof(ChattingContext))] - [Migration("20230327073724_FuckThisShitv2")] - partial class FuckThisShitv2 - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "7.0.4"); - - modelBuilder.Entity("ChatMessage", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("TEXT"); - - b.Property("Message") - .HasColumnType("TEXT"); - - b.Property("User") - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.ToTable("Messages"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/Migrations/20230327073724_FuckThisShitv2.cs b/Migrations/20230327073724_FuckThisShitv2.cs deleted file mode 100644 index 6770f7b..0000000 --- a/Migrations/20230327073724_FuckThisShitv2.cs +++ /dev/null @@ -1,34 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace LiveChat.Migrations -{ - /// - public partial class FuckThisShitv2 : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "Messages", - columns: table => new - { - Id = table.Column(type: "TEXT", nullable: false), - User = table.Column(type: "TEXT", nullable: true), - Message = table.Column(type: "TEXT", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_Messages", x => x.Id); - }); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "Messages"); - } - } -} diff --git a/Migrations/20230526180433_sqlite.local_migration_189.Designer.cs b/Migrations/20230529185839_sqlite.local_migration_918.Designer.cs similarity index 77% rename from Migrations/20230526180433_sqlite.local_migration_189.Designer.cs rename to Migrations/20230529185839_sqlite.local_migration_918.Designer.cs index c283e9c..0052c5c 100644 --- a/Migrations/20230526180433_sqlite.local_migration_189.Designer.cs +++ b/Migrations/20230529185839_sqlite.local_migration_918.Designer.cs @@ -9,8 +9,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace LiveChat.Migrations { [DbContext(typeof(ChattingContext))] - [Migration("20230526180433_sqlite.local_migration_189")] - partial class sqlitelocal_migration_189 + [Migration("20230529185839_sqlite.local_migration_918")] + partial class sqlitelocal_migration_918 { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -18,23 +18,6 @@ namespace LiveChat.Migrations #pragma warning disable 612, 618 modelBuilder.HasAnnotation("ProductVersion", "7.0.4"); - modelBuilder.Entity("ChatMessage", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("TEXT"); - - b.Property("Message") - .HasColumnType("TEXT"); - - b.Property("User") - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.ToTable("Messages"); - }); - modelBuilder.Entity("Room", b => { b.Property("Id") diff --git a/Migrations/20230526180433_sqlite.local_migration_189.cs b/Migrations/20230529185839_sqlite.local_migration_918.cs similarity index 97% rename from Migrations/20230526180433_sqlite.local_migration_189.cs rename to Migrations/20230529185839_sqlite.local_migration_918.cs index 2b8c37a..60704bd 100644 --- a/Migrations/20230526180433_sqlite.local_migration_189.cs +++ b/Migrations/20230529185839_sqlite.local_migration_918.cs @@ -5,7 +5,7 @@ namespace LiveChat.Migrations { /// - public partial class sqlitelocal_migration_189 : Migration + public partial class sqlitelocal_migration_918 : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) diff --git a/Migrations/ChattingContextModelSnapshot.cs b/Migrations/ChattingContextModelSnapshot.cs index 231193d..310fe27 100644 --- a/Migrations/ChattingContextModelSnapshot.cs +++ b/Migrations/ChattingContextModelSnapshot.cs @@ -15,23 +15,6 @@ namespace LiveChat.Migrations #pragma warning disable 612, 618 modelBuilder.HasAnnotation("ProductVersion", "7.0.4"); - modelBuilder.Entity("ChatMessage", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("TEXT"); - - b.Property("Message") - .HasColumnType("TEXT"); - - b.Property("User") - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.ToTable("Messages"); - }); - modelBuilder.Entity("Room", b => { b.Property("Id") diff --git a/Model.cs b/Model.cs index bf00761..73805b3 100644 --- a/Model.cs +++ b/Model.cs @@ -7,8 +7,6 @@ using System.ComponentModel.DataAnnotations.Schema; public class ChattingContext : DbContext { - public DbSet Messages { get; set; } - public DbSet Rooms { get; set; } public DbSet RoomChatMessages { get; set; } @@ -37,17 +35,6 @@ public class Room // koble tilsammen RoomChatMessage til Room // https://www.entityframeworktutorial.net/efcore/one-to-many-conventions-entity-framework-core.aspx public RoomChatMessage? RoomChatMessage { get; set; } - -} - -public class ChatMessage -{ - [Key] - [DatabaseGenerated(DatabaseGeneratedOption.Identity)] - public string? Id { get; set; } - - public string? User { get; set; } - public string? Message { get; set; } } public class RoomChatMessage @@ -63,5 +50,4 @@ public class RoomChatMessage // koble tilsammen RoomChatMessage til Room // https://www.entityframeworktutorial.net/efcore/one-to-many-conventions-entity-framework-core.aspx public Room? Room { get; set; } - } \ No newline at end of file diff --git a/Pages/Chat.razor b/Pages/Chat.razor index 8ca4232..058ef7d 100644 --- a/Pages/Chat.razor +++ b/Pages/Chat.razor @@ -2,6 +2,7 @@ @using Microsoft.AspNetCore.SignalR.Client; @using LiveChat.Models; @inject NavigationManager Navigation +@inject IHttpContextAccessor httpContextAccessor; Chat room @@ -13,11 +14,13 @@

Rom @Room

-

Din tilkoblings ID: @connectionId. Brukernavn: @username

+

Del @Room til vennene dine for å snakke med dem fra her. Ditt brukernavn: @username.

@foreach (var msg in msgs) { -
@msg
+
+
@msg
+
}
@@ -49,17 +52,16 @@ private List msgs = new List(); - private bool ErViInne = false; - - private string connectionId = ""; - private string username = ""; private MessageModel messageModel = new MessageModel() { User = "Something", Message = "" }; + private string? host { get; set; } protected override async Task OnInitializedAsync() { + //host = httpContextAccessor.HttpContext.Request.Headers["Host"]; + hubConnection = new HubConnectionBuilder() .WithUrl(Navigation.ToAbsoluteUri("/chathub")) .Build(); @@ -89,9 +91,18 @@ // bli med i rom await hubConnection.SendAsync("JoinRoom", Room, username); - ErViInne = true; messageModel.User = username; + using (var db = new ChattingContext()) + { + var messages = db.RoomChatMessages.Where(m => m.RoomId == Room).ToList(); + foreach (var message in messages) + { + msgs.Add($"{message.User}: {message.Message}"); + } + await InvokeAsync(StateHasChanged); + } + hubConnection.On("ReceiveMessage", (user, message) => { var encodedMsg = $"{user}: {message}"; diff --git a/Pages/Index.razor b/Pages/Index.razor index baf05ff..8ced93c 100644 --- a/Pages/Index.razor +++ b/Pages/Index.razor @@ -1,155 +1,44 @@ @page "/" @using LiveChat.Models; -@using Microsoft.AspNetCore.SignalR.Client @inject Blazored.LocalStorage.ILocalStorageService localStorage @inject NavigationManager Navigation -@implements IAsyncDisposable LiveChat app
- - - - - - -
- -@if (msgs != null) -{ - -} -else -{ -

Ingen meldinger tilgjengelig.

-} - - @code { - private HubConnection? hubConnection; + private string? romkode; - private bool ModalIsVisible = true; - private MessageModel messageModel = new MessageModel() { User = "", Message = "" }; - private UserModel userModel = new UserModel() { User = "" }; - private List msgs = new List(); - - - protected override async Task OnInitializedAsync() + private void LagTilfeldigRom() { - hubConnection = new HubConnectionBuilder() - .WithUrl(Navigation.ToAbsoluteUri("/chathub")) - .Build(); - - using (var db = new ChattingContext()) - { - var messages = db.Messages.ToList(); - - foreach (var msg in messages) - { - var encodedMsg = $"{msg.User}: {msg.Message}"; - msgs.Add(encodedMsg); - } - await InvokeAsync(StateHasChanged); - } - - - hubConnection.On("ReceiveMessage", (user, message) => - { - var encodedMsg = $"{user}: {message}"; - msgs.Add(encodedMsg); - InvokeAsync(StateHasChanged); - }); - - await hubConnection.StartAsync(); + // lag tilfeldig nummer + Random random = new Random(); + var nummer = random.Next(0, 1000000); + // send bruker til rommet + Navigation.NavigateTo("/chat/" + nummer); } - private async Task SetUser() + private void BliMedIRom() { - ModalIsVisible = false; - messageModel.User = userModel.User; - await localStorage.SetItemAsync("user", userModel.User); - } - - private async Task HandleSubmit() - { - var filter = new ProfanityFilter.ProfanityFilter(); - - // Kjempegøy - filter.AddProfanity("faen"); - filter.AddProfanity("jævla"); - filter.AddProfanity("jævel"); - filter.AddProfanity("homse"); - filter.AddProfanity("homo"); - filter.AddProfanity("neger"); - filter.AddProfanity("transe"); - filter.AddProfanity("dritt"); - - messageModel.User = filter.CensorString(messageModel.User); - messageModel.Message = filter.CensorString(messageModel.Message); - - if (hubConnection is not null) - { - await hubConnection.SendAsync("SendMessage", messageModel.User, messageModel.Message); - } - } - - public bool IsConnected => - hubConnection?.State == HubConnectionState.Connected; - - public async ValueTask DisposeAsync() - { - if (hubConnection is not null) - { - await hubConnection.DisposeAsync(); - } + // send bruker til rommet + Navigation.NavigateTo("/chat/" + romkode); } } diff --git a/Program.cs b/Program.cs index c269145..600e2e5 100644 --- a/Program.cs +++ b/Program.cs @@ -10,6 +10,7 @@ var builder = WebApplication.CreateBuilder(args); builder.Services.AddRazorPages(); builder.Services.AddServerSideBlazor(); builder.Services.AddBlazoredLocalStorage(); +builder.Services.AddHttpContextAccessor(); builder.Services.AddResponseCompression(opts => { opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat( diff --git a/Shared/SurveyPrompt.razor b/Shared/SurveyPrompt.razor deleted file mode 100644 index ec64baa..0000000 --- a/Shared/SurveyPrompt.razor +++ /dev/null @@ -1,16 +0,0 @@ -
- - @Title - - - Please take our - brief survey - - and tell us what you think. -
- -@code { - // Demonstrates how a parent component can supply parameters - [Parameter] - public string? Title { get; set; } -}