-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathcreateTask.ts
More file actions
161 lines (142 loc) · 3.88 KB
/
createTask.ts
File metadata and controls
161 lines (142 loc) · 3.88 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { makeExtendSchemaPlugin, gql } from "graphile-utils";
import axios from "axios";
import savepointWrapper from "./savepointWrapper";
import config from "../config";
import { registerAndLoginUser } from "../utils/hedgedoc";
function buildNoteContent(
title: string,
description?: string,
tags?: string[]
): string {
let note = "";
note += `# ${title}`;
if (tags) {
for (const tag of tags) {
note += ` - ${tag}`;
}
}
note += "\n\n";
if (description) {
note += `## Description\n`;
note += "\n";
note += `${description}\n`;
note += "\n";
note += "----\n";
}
return note;
}
export async function createPad(
setCookieHeader: string[],
title: string,
description?: string,
tags?: string[]
): Promise<string> {
const options = {
headers: {
"Content-Type": "text/markdown",
Cookie: setCookieHeader.join("; "),
},
maxRedirects: 0,
validateStatus: (status: number) => status === 302,
};
try {
const res = await axios.post(
config.pad.createUrl,
buildNoteContent(title, description, tags),
options
);
return res.headers.location;
} catch (e) {
throw Error(
`Call to ${
config.pad.createUrl
} during task creation failed. Length of note: ${
buildNoteContent(title, description, tags).length
}`
);
}
}
export default makeExtendSchemaPlugin((build) => {
const { pgSql: sql } = build;
return {
typeDefs: gql`
input CreateTaskInput {
ctfId: Int!
title: String!
tags: [String]
description: String
flag: String
}
type CreateTaskPayload {
task: Task @pgField
query: Query
}
extend type Mutation {
createTask(input: CreateTaskInput): CreateTaskPayload
}
`,
resolvers: {
Mutation: {
createTask: async (
_query,
{ input: { title, description, tags, flag, ctfId } },
{ pgClient },
resolveInfo
) => {
try {
let cookie: string[] | undefined = undefined;
if (config.pad.nonpublicPads) {
cookie = await registerAndLoginUser();
}
const {
rows: [isAllowed],
} = await pgClient.query(
`SELECT ctfnote_private.can_play_ctf($1)`,
[ctfId]
);
if (isAllowed.can_play_ctf !== true) {
return {};
}
const padPathOrUrl = await createPad(
cookie ?? [],
title,
description,
tags
);
let padPath: string;
if (padPathOrUrl.startsWith("/")) {
padPath = padPathOrUrl.slice(1);
} else {
padPath = new URL(padPathOrUrl).pathname.slice(1);
}
const padUrl = `${config.pad.showUrl}${padPath}`;
return await savepointWrapper(pgClient, async () => {
const {
rows: [newTask],
} = await pgClient.query(
`SELECT * FROM ctfnote_private.create_task($1, $2, $3, $4, $5)`,
[title, description ?? "", flag ?? "", padUrl, ctfId]
);
const [row] =
await resolveInfo.graphile.selectGraphQLResultFromTable(
sql.fragment`ctfnote.task`,
(tableAlias, queryBuilder) => {
queryBuilder.where(
sql.fragment`${tableAlias}.id = ${sql.value(newTask.id)}`
);
}
);
return {
data: row,
query: build.$$isQuery,
};
});
} catch (error) {
console.error("error:", error);
throw new Error(`Creating task failed: ${error}`);
}
},
},
},
};
});