import assert from "assert" ; import q from "../util/q" ; import { CONFIRMATIONS } from "../util/consts" ; import { ObjectExt } from "./inbox" ; import { OutboxCtrl } from "./outbox" ; import { createElement as h , createMention , render } from "../util/html" ; import { sample } from "../util/misc" ; import { Pg , insertOrBumpChallengeBoard , removeFromChallengeBoard , } from "../util/model" ; export interface ChallengeBoardCtrl { handleRequest ( object ) : Promise < void > ; handleRemove ( object ) : Promise < void > ; } export default async ( { actorUrl , outbox , pg , } ) : Promise < ChallengeBoardCtrl > => { const handleRequest = async ( object ) => { await q . transact ( pg , async ( ) => { const { id , preferredUsername : name = "???" } = object . actor ; const now = new Date ( ) ; const { rows } = await insertOrBumpChallengeBoard ( pg , id , name , now ) ; assert . strictEqual ( rows . length , 1 ) ; const replyContent = render ( [ h ( "p" , { } , [ createMention ( id , name ) , " " , sample ( CONFIRMATIONS ) , now . valueOf ( ) === rows [ 0 ] . createdAt . valueOf ( ) ? " You are now on the challenge board." : " You have been bumped to the top of the challenge board." , ] ) , ] ) ; await outbox . createObject ( pg , { type : "Note" , published : now . toISOString ( ) , attributedTo : actorUrl , inReplyTo : object . id , to : [ object . actor . id ] , content : replyContent , tag : [ { type : "Mention" , href : object . actor . id } ] , } ) ; } ) ; } ; const handleRemove = async ( object ) => { await q . transact ( pg , async ( ) => { const { id , preferredUsername : name = "???" } = object . actor ; const { rowCount } = await removeFromChallengeBoard ( pg , object . actor . id ) ; let replyText ; if ( rowCount === 0 ) { replyText = "You are not on the challenge board." ; } else { assert . strictEqual ( rowCount , 1 ) ; replyText = "You've been removed from the challenge board." ; } const replyContent = render ( [ h ( "p" , { } , [ createMention ( id , name ) , " " , replyText ] ) , ] ) ; await outbox . createObject ( pg , { type : "Note" , published : new Date ( ) . toISOString ( ) , attributedTo : actorUrl , inReplyTo : object . id , to : [ object . actor . id ] , content : replyContent , tag : [ { type : "Mention" , href : object . actor . id } ] , } ) ; } ) ; } ; return { handleRequest , handleRemove } ; } ;