import { IConnection } from "./IConnection" ; import { Appservice } from "matrix-bot-sdk" ; import LogWrapper from "../LogWrapper" ; import { ProjectsGetResponseData } from "../Github/Types" ; import { BaseConnection } from "./BaseConnection" ; export interface GitHubProjectConnectionState { project_id : number ; state : "open" | "closed" ; } const log = new LogWrapper ( "GitHubProjectConnection" ) ; /**
 * Handles rooms connected to a github repo.
 */ export class GitHubProjectConnection extends BaseConnection implements IConnection { static readonly CanonicalEventType = "uk.half-shot.matrix-hookshot.github.project" ; static readonly LegacyCanonicalEventType = "uk.half-shot.matrix-github.project" ; static readonly EventTypes = [ GitHubProjectConnection . CanonicalEventType , GitHubProjectConnection . LegacyCanonicalEventType , ] ; static async onOpenProject ( project , as , inviteUser ) { log . info ( `Fetching ${ project . name } ${ project . id }` ) ; const state = { project_id : project . id , state : project . state as "open" | "closed" , } ; const roomId = await as . botClient . createRoom ( { visibility : "private" , name : `${ project . name }` , topic : project . body , preset : "private_chat" , invite : [ inviteUser ] , initial_state : [ { type : this . CanonicalEventType , content : state , state_key : project . url , } , ] , } ) ; return new GitHubProjectConnection ( roomId , as , state , project . url ) ; } get projectId ( ) { return this . state . project_id ; } constructor ( public readonly roomId , as , private state , stateKey ) { super ( roomId , stateKey , GitHubProjectConnection . CanonicalEventType ) ; } public isInterestedInStateEvent ( eventType , stateKey ) { return GitHubProjectConnection . EventTypes . includes ( eventType ) && this . stateKey === stateKey ; } public toString ( ) { return `GitHubProjectConnection ${ this . state . project_id }}` ; } }