about summary refs log tree commit diff
path: root/BugMine.Sdk/BugMineProject.cs
diff options
context:
space:
mode:
Diffstat (limited to 'BugMine.Sdk/BugMineProject.cs')
-rw-r--r--BugMine.Sdk/BugMineProject.cs28
1 files changed, 17 insertions, 11 deletions
diff --git a/BugMine.Sdk/BugMineProject.cs b/BugMine.Sdk/BugMineProject.cs
index babb680..1f56659 100644
--- a/BugMine.Sdk/BugMineProject.cs
+++ b/BugMine.Sdk/BugMineProject.cs
@@ -1,6 +1,4 @@
-using System.Text.Json.Nodes;
-using LibMatrix.EventTypes.Spec.State;
-using LibMatrix.Homeservers;
+using ArcaneLibs.Extensions;
 using LibMatrix.RoomTypes;
 
 namespace BugMine.Web.Classes;
@@ -9,43 +7,51 @@ public class BugMineProject(GenericRoom room) {
     public const string RoomType = "gay.rory.bugmine.project";
     public GenericRoom Room { get; } = room;
     public ProjectInfo Info { get; set; }
+    public BugMineRoomMetadata Metadata { get; set; }
     public string ProjectSlug { get; set; }
 
     public async Task<BugMineProject> InitializeAsync() {
-        Info = (await Room.GetStateAsync<ProjectInfo>(ProjectInfo.EventId))!;
+        var infoTask = Room.GetStateAsync<ProjectInfo>(ProjectInfo.EventId);
+        var metadataTask = Room.GetStateAsync<BugMineRoomMetadata>(BugMineRoomMetadata.EventId)!;
         var alias = await room.GetCanonicalAliasAsync();
 
         if (alias != null)
             ProjectSlug = alias.Alias?[1..] ?? room.RoomId;
         else ProjectSlug = room.RoomId;
 
+        Info = (await infoTask)!;
+        Metadata = (await metadataTask)!;
         return this;
     }
 
     public async Task<BugMineIssue> CreateIssue(BugMineIssueData issue) {
         // add relation to room creation event
         issue.RelatesTo = new() {
-            EventId = await room.GetStateEventIdAsync(RoomCreateEventContent.EventId),
+            EventId = Metadata.RoomCreationEventId,
             RelationType = "gay.rory.bugmine.issue"
         };
         var eventId = await Room.SendTimelineEventAsync(BugMineIssueData.EventId, issue);
 
-        // return new BugMineIssueAccessor(Room, await Room.GetEventAsync<>(eventId));
         var evt = await room.GetEventAsync(eventId.EventId);
-        Console.WriteLine(evt);
+        Console.WriteLine(evt.ToJson(indent: false));
         return new BugMineIssue(Room, evt);
     }
 
     public async IAsyncEnumerable<BugMineIssue> GetIssues() {
-        var creationEventId = await room.GetStateEventIdAsync(RoomCreateEventContent.EventId);
-        await foreach (var evt in room.GetRelatedEventsAsync(creationEventId, "gay.rory.bugmine.issue", BugMineIssueData.EventId)) {
+        await foreach (var evt in room.GetRelatedEventsAsync(Metadata.RoomCreationEventId, "gay.rory.bugmine.issue", BugMineIssueData.EventId)) {
             yield return new BugMineIssue(Room, evt);
         }
     }
 }
 
 public static class ProjectRoomExtensions {
-    public static async Task<BugMineProject> AsBugMineProject(this GenericRoom room) {
-        return await new BugMineProject(room).InitializeAsync();
+    public static async Task<BugMineProject> AsBugMineProject(this GenericRoom room, SemaphoreSlim? semaphore = null) {
+        try {
+            await (semaphore?.WaitAsync() ?? Task.CompletedTask);
+            return await new BugMineProject(room).InitializeAsync();
+        }
+        finally {
+            semaphore?.Release();
+        }
     }
 }
\ No newline at end of file