about summary refs log tree commit diff
path: root/BugMine.Sdk/BugMineProject.cs
blob: babb6801426d1d969c0c644fc4898184c84dbf45 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System.Text.Json.Nodes;
using LibMatrix.EventTypes.Spec.State;
using LibMatrix.Homeservers;
using LibMatrix.RoomTypes;

namespace BugMine.Web.Classes;

public class BugMineProject(GenericRoom room) {
    public const string RoomType = "gay.rory.bugmine.project";
    public GenericRoom Room { get; } = room;
    public ProjectInfo Info { get; set; }
    public string ProjectSlug { get; set; }

    public async Task<BugMineProject> InitializeAsync() {
        Info = (await Room.GetStateAsync<ProjectInfo>(ProjectInfo.EventId))!;
        var alias = await room.GetCanonicalAliasAsync();

        if (alias != null)
            ProjectSlug = alias.Alias?[1..] ?? room.RoomId;
        else ProjectSlug = room.RoomId;

        return this;
    }

    public async Task<BugMineIssue> CreateIssue(BugMineIssueData issue) {
        // add relation to room creation event
        issue.RelatesTo = new() {
            EventId = await room.GetStateEventIdAsync(RoomCreateEventContent.EventId),
            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);
        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)) {
            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();
    }
}