import type { TimelineItem } from "../contract/events.ts"; import type { Task } from "../contract/handles.ts"; import type { TaskStatus } from "../contract/views.ts"; import { readStream, taskStream } from "../events.ts"; import { firstInvalid, invalid, ok, raise } from "../outcomes.ts"; import { Policy } from "../policy.ts"; import { type RepoRecord } from "../repos.ts"; import * as tasks from "../tasks.ts"; import type { Deps } from "./context.ts"; export async function openTask(deps: Deps, policy: Policy, record: tasks.TaskRecord, visibleRepo: (name: string) => Promise): Promise { const db = deps.env.DB; const actor = policy.actor; const person = policy.person!; const extras = await tasks.taskExtras(db, record.id); const commentAuthor = (item: TimelineItem) => item.by.onBehalfOf?.id ?? item.by.actor.id; const detail = tasks.toDetail(record, extras.labels, extras.comments); return { detail, async timeline() { const moderator = policy.canModerateComments(); const events = await readStream(db, taskStream(record.id)); return events.map((event) => event.kind === "task.commented" ? { ...event, can: { edit: event.by.actor.kind === "human" && event.by.actor.id === person.id, delete: moderator || commentAuthor(event) === person.id } } : event, ); }, async comment(body, then) { const text = body.trim(); const bodyProblem = firstInvalid({ body: tasks.requiredBodyError(text, "Comment", "Write a comment first.") }); if (bodyProblem) return bodyProblem; await tasks.addComment(db, actor, record, text, then); return ok(); }, async setStatus(status: TaskStatus) { await tasks.setStatus(db, actor, record, status); return ok(); }, async rename(title) { const text = title.trim(); const titleProblem = firstInvalid({ title: tasks.titleError(text) }); if (titleProblem) return titleProblem; await tasks.rename(db, actor, record, text); return ok(); }, async describe(body) { const text = body.trim(); const bodyProblem = firstInvalid({ body: tasks.bodyError(text) }); if (bodyProblem) return bodyProblem; await tasks.describe(db, record, text); return ok(); }, async assign(id) { if (id === null) { await tasks.assign(db, actor, record, null); return ok(); } const name = await tasks.memberName(db, record.repo_id, id); if (!name) return invalid("Choose a member to assign.", "assignee"); await tasks.assign(db, actor, record, { id, name }); return ok(); }, async transfer(repoName) { const target = await visibleRepo(repoName).catch(() => null); if (!target) return invalid("Choose a repository.", "repo"); if (target.id === record.repo_id) return ok({ repo: record.repo_name, number: record.number }); return ok(await tasks.transfer(db, actor, record, { id: target.id, name: target.full_name })); }, async setLabels(labels) { await tasks.setLabels(db, actor, record, labels); return ok(); }, async editComment(comment, body) { const text = body.trim(); const bodyProblem = firstInvalid({ body: tasks.requiredBodyError(text, "Comment", "Comments can't be empty.") }); if (bodyProblem) return bodyProblem; if (!(await tasks.editComment(db, record, Number(comment), person.id, text))) raise("not_found", "No such comment, or it isn't yours."); return ok(); }, async deleteComment(comment) { if (!(await tasks.deleteComment(db, record, Number(comment), person.id, policy.canModerateComments()))) raise("not_found", "No such comment, or you can't delete it."); return ok(); }, }; }