Skip to content
refs
95 lines · 3.7 KBRaw
1import type { TimelineItem } from "../contract/events.ts";
2import type { Task } from "../contract/handles.ts";
3import type { TaskStatus } from "../contract/views.ts";
4import { readStream, taskStream } from "../events.ts";
5import { firstInvalid, invalid, ok, raise } from "../outcomes.ts";
6import { Policy } from "../policy.ts";
7import { type RepoRecord } from "../repos.ts";
8import * as tasks from "../tasks.ts";
9import type { Deps } from "./context.ts";
10
11export async function openTask(deps: Deps, policy: Policy, record: tasks.TaskRecord, visibleRepo: (name: string) => Promise<RepoRecord>): Promise<Task> {
12 const db = deps.env.DB;
13 const actor = policy.actor;
14 const person = policy.person!;
15 const extras = await tasks.taskExtras(db, record.id);
16
17 const commentAuthor = (item: TimelineItem) => item.by.onBehalfOf?.id ?? item.by.actor.id;
18 const detail = tasks.toDetail(record, extras.labels, extras.comments);
19 return {
20 detail,
21
22 async timeline() {
23 const moderator = policy.canModerateComments();
24 const events = await readStream(db, taskStream(record.id));
25 return events.map((event) =>
26 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,
27 );
28 },
29
30 async comment(body, then) {
31 const text = body.trim();
32 const bodyProblem = firstInvalid({ body: tasks.requiredBodyError(text, "Comment", "Write a comment first.") });
33 if (bodyProblem) return bodyProblem;
34 await tasks.addComment(db, actor, record, text, then);
35 return ok();
36 },
37
38 async setStatus(status: TaskStatus) {
39 await tasks.setStatus(db, actor, record, status);
40 return ok();
41 },
42
43 async rename(title) {
44 const text = title.trim();
45 const titleProblem = firstInvalid({ title: tasks.titleError(text) });
46 if (titleProblem) return titleProblem;
47 await tasks.rename(db, actor, record, text);
48 return ok();
49 },
50
51 async describe(body) {
52 const text = body.trim();
53 const bodyProblem = firstInvalid({ body: tasks.bodyError(text) });
54 if (bodyProblem) return bodyProblem;
55 await tasks.describe(db, record, text);
56 return ok();
57 },
58
59 async assign(id) {
60 if (id === null) {
61 await tasks.assign(db, actor, record, null);
62 return ok();
63 }
64 const name = await tasks.memberName(db, record.repo_id, id);
65 if (!name) return invalid("Choose a member to assign.", "assignee");
66 await tasks.assign(db, actor, record, { id, name });
67 return ok();
68 },
69
70 async transfer(repoName) {
71 const target = await visibleRepo(repoName).catch(() => null);
72 if (!target) return invalid("Choose a repository.", "repo");
73 if (target.id === record.repo_id) return ok({ repo: record.repo_name, number: record.number });
74 return ok(await tasks.transfer(db, actor, record, { id: target.id, name: target.full_name }));
75 },
76
77 async setLabels(labels) {
78 await tasks.setLabels(db, actor, record, labels);
79 return ok();
80 },
81
82 async editComment(comment, body) {
83 const text = body.trim();
84 const bodyProblem = firstInvalid({ body: tasks.requiredBodyError(text, "Comment", "Comments can't be empty.") });
85 if (bodyProblem) return bodyProblem;
86 if (!(await tasks.editComment(db, record, Number(comment), person.id, text))) raise("not_found", "No such comment, or it isn't yours.");
87 return ok();
88 },
89
90 async deleteComment(comment) {
91 if (!(await tasks.deleteComment(db, record, Number(comment), person.id, policy.canModerateComments()))) raise("not_found", "No such comment, or you can't delete it.");
92 return ok();
93 },
94 };
95}