Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webhooks (and misc fixes)! #52

Merged
merged 8 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

78 changes: 78 additions & 0 deletions ReplayBrowser/Data/Migrations/20240826120303_AccountWebhooks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

namespace Server.Migrations
{
/// <inheritdoc />
public partial class AccountWebhooks : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Webhook",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Url = table.Column<string>(type: "text", nullable: false),
Type = table.Column<byte>(type: "smallint", nullable: false),
AccountId = table.Column<int>(type: "integer", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Webhook", x => x.Id);
table.ForeignKey(
name: "FK_Webhook_Accounts_AccountId",
column: x => x.AccountId,
principalTable: "Accounts",
principalColumn: "Id");
});

migrationBuilder.CreateTable(
name: "WebhookHistory",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
SentAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
ResponseCode = table.Column<int>(type: "integer", nullable: false),
ResponseBody = table.Column<string>(type: "text", nullable: false),
WebhookId = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_WebhookHistory", x => x.Id);
table.ForeignKey(
name: "FK_WebhookHistory_Webhook_WebhookId",
column: x => x.WebhookId,
principalTable: "Webhook",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});

migrationBuilder.CreateIndex(
name: "IX_Webhook_AccountId",
table: "Webhook",
column: "AccountId");

migrationBuilder.CreateIndex(
name: "IX_WebhookHistory_WebhookId",
table: "WebhookHistory",
column: "WebhookId");
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "WebhookHistory");

migrationBuilder.DropTable(
name: "Webhook");
}
}
}
78 changes: 78 additions & 0 deletions ReplayBrowser/Data/Migrations/ReplayDbContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,59 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.ToTable("HistoryEntry");
});

modelBuilder.Entity("ReplayBrowser.Data.Models.Account.Webhook", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");

NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));

b.Property<int?>("AccountId")
.HasColumnType("integer");

b.Property<byte>("Type")
.HasColumnType("smallint");

b.Property<string>("Url")
.IsRequired()
.HasColumnType("text");

b.HasKey("Id");

b.HasIndex("AccountId");

b.ToTable("Webhook");
});

modelBuilder.Entity("ReplayBrowser.Data.Models.Account.WebhookHistory", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");

NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));

b.Property<string>("ResponseBody")
.IsRequired()
.HasColumnType("text");

b.Property<int>("ResponseCode")
.HasColumnType("integer");

b.Property<DateTime>("SentAt")
.HasColumnType("timestamp with time zone");

b.Property<int>("WebhookId")
.HasColumnType("integer");

b.HasKey("Id");

b.HasIndex("WebhookId");

b.ToTable("WebhookHistory");
});

modelBuilder.Entity("ReplayBrowser.Data.Models.CharacterData", b =>
{
b.Property<int>("Id")
Expand Down Expand Up @@ -473,6 +526,24 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Navigation("Account");
});

modelBuilder.Entity("ReplayBrowser.Data.Models.Account.Webhook", b =>
{
b.HasOne("ReplayBrowser.Data.Models.Account.Account", null)
.WithMany("Webhooks")
.HasForeignKey("AccountId");
});

modelBuilder.Entity("ReplayBrowser.Data.Models.Account.WebhookHistory", b =>
{
b.HasOne("ReplayBrowser.Data.Models.Account.Webhook", "Webhook")
.WithMany("Logs")
.HasForeignKey("WebhookId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();

b.Navigation("Webhook");
});

modelBuilder.Entity("ReplayBrowser.Data.Models.CharacterData", b =>
{
b.HasOne("ReplayBrowser.Data.Models.CollectedPlayerData", null)
Expand Down Expand Up @@ -529,6 +600,13 @@ protected override void BuildModel(ModelBuilder modelBuilder)
modelBuilder.Entity("ReplayBrowser.Data.Models.Account.Account", b =>
{
b.Navigation("History");

b.Navigation("Webhooks");
});

modelBuilder.Entity("ReplayBrowser.Data.Models.Account.Webhook", b =>
{
b.Navigation("Logs");
});

modelBuilder.Entity("ReplayBrowser.Data.Models.CollectedPlayerData", b =>
Expand Down
1 change: 1 addition & 0 deletions ReplayBrowser/Data/Models/Account/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class Account : IEntityTypeConfiguration<Account>
public List<Guid> SavedProfiles { get; set; } = new();
public List<HistoryEntry> History { get; set; } = new();

public List<Webhook> Webhooks { get; set; } = new();
public bool Protected { get; set; } = false;

public void Configure(EntityTypeBuilder<Account> builder)
Expand Down
1 change: 1 addition & 0 deletions ReplayBrowser/Data/Models/Account/History.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public enum Action
// Account actions
AccountSettingsChanged,
Login,
WebhooksChanged,

// Site actions
SearchPerformed,
Expand Down
10 changes: 10 additions & 0 deletions ReplayBrowser/Data/Models/Account/Webhook.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace ReplayBrowser.Data.Models.Account;

public class Webhook
{
public int Id { get; set; }

public string Url { get; set; }

Check warning on line 7 in ReplayBrowser/Data/Models/Account/Webhook.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Url' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 7 in ReplayBrowser/Data/Models/Account/Webhook.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Url' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public WebhookType Type { get; set; }
public List<WebhookHistory> Logs { get; set; } = new();
}
17 changes: 17 additions & 0 deletions ReplayBrowser/Data/Models/Account/WebhookHistory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace ReplayBrowser.Data.Models.Account;

/// <summary>
/// Represents a history entry for a webhook. Contains information about when something was sent and the response.
/// </summary>
public class WebhookHistory
{
public int Id { get; set; }

public DateTime SentAt { get; set; }
public int ResponseCode { get; set; }
// ReSharper disable once EntityFramework.ModelValidation.UnlimitedStringLength // This is a log, it can be as long as it wants.
public string ResponseBody { get; set; } = string.Empty;

public Webhook? Webhook { get; set; } = null!;
public int WebhookId { get; set; }
}
14 changes: 14 additions & 0 deletions ReplayBrowser/Data/Models/Account/WebhookType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace ReplayBrowser.Data.Models.Account;

public enum WebhookType : byte
{
/// <summary>
/// Will attempt to send the replay data to a Discord webhook.
/// </summary>
Discord,

/// <summary>
/// Will send collected data to a URL.
/// </summary>
Json,
}
6 changes: 5 additions & 1 deletion ReplayBrowser/Data/Models/Player.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
using Microsoft.EntityFrameworkCore;
using System.Text.Json.Serialization;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ReplayBrowser.Models.Ingested;

namespace ReplayBrowser.Data.Models;

public class Player : IEntityTypeConfiguration<Player>
{
[JsonIgnore]
public int Id { get; set; }

public List<string> AntagPrototypes { get; set; } = null!;
public List<string> JobPrototypes { get; set; } = null!;
public required string PlayerIcName { get; set; }
public bool Antag { get; set; }

[JsonIgnore]
public ReplayParticipant Participant { get; set; } = null!;
[JsonIgnore]
public int ParticipantId { get; set; }

public JobDepartment? EffectiveJob { get; set; }
Expand Down
1 change: 1 addition & 0 deletions ReplayBrowser/Data/Models/Replay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace ReplayBrowser.Data.Models;

public class Replay : IEntityTypeConfiguration<Replay>
{
[JsonIgnore]
public int Id { get; set; }

public required string Link { get; set; }
Expand Down
4 changes: 4 additions & 0 deletions ReplayBrowser/Data/Models/ReplayParticipant.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text.Json.Serialization;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

Expand All @@ -8,11 +9,14 @@ namespace ReplayBrowser.Data.Models;
/// </summary>
public class ReplayParticipant : IEntityTypeConfiguration<ReplayParticipant>
{
[JsonIgnore]
public int Id { get; set; }
public Guid PlayerGuid { get; set; }
public string Username { get; set; } = null!;

[JsonIgnore]
public Replay Replay { get; set; } = null!;
[JsonIgnore]
public int ReplayId { get; set; }

public List<Player>? Players { get; set; }
Expand Down
Loading
Loading