Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Upstream] some code cleanup #71

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"start": "NODE_ENV=production node dist/index.js",
"build": "tsc",
"lint": "eslint --fix 'src/**/*.ts'",
"format": "prettier --write 'src/*.ts'",
"format": "prettier --write 'src/**/*.ts'",
"dev": "NODE_ENV=development nodemon src/index.ts",
"dev:migrate": "DATABASE_URL= yarn run db-migrate -e dev up"
},
Expand Down
2 changes: 1 addition & 1 deletion api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function createApp(postgraphileOptions: PostGraphileOptions) {
app.use(
"/uploads",
express.static("uploads", {
setHeaders: function (res, path, stat) {
setHeaders: function (res) {
res.set("Content-Disposition", "attachment");
},
})
Expand Down
140 changes: 72 additions & 68 deletions api/src/plugins/importCtf.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,57 @@

import { makeExtendSchemaPlugin, gql } from "graphile-utils";
import axios from "axios"
import savepointWrapper from "./savepointWrapper"
import axios from "axios";
import savepointWrapper from "./savepointWrapper";

interface CTFTimeResponse {
title: string;
weight: number;
url: string,
logo: string,
ctftime_url: string,
description: string,
start: string,
finish: string
title: string;
weight: number;
url: string;
logo: string;
ctftime_url: string;
description: string;
start: string;
finish: string;
}


async function fetchFromCtftime(id: number): Promise<CTFTimeResponse> {
const url = `https://ctftime.org/api/v1/events/${id}/`;
const response = await axios.get(url, {
headers: { "User-Agent": "CTFNote" }, // The default axios user-agent is blacklisted by ctftime :/
});
return response.data
const url = `https://ctftime.org/api/v1/events/${id}/`;
const response = await axios.get(url, {
headers: { "User-Agent": "CTFNote" }, // The default axios user-agent is blacklisted by ctftime :/
});
return response.data;
}

export default makeExtendSchemaPlugin((build) => {
const { pgSql: sql } = build;
return {
typeDefs: gql`
input ImportCtfInput {
ctftimeId: Int!
}

export default makeExtendSchemaPlugin(build => {
const { pgSql: sql } = build;
return {
typeDefs: gql`

input ImportCtfInput {
ctftimeId: Int!
}

type ImportCtfPayload {
ctf: Ctf @pgField
query: Query
}
type ImportCtfPayload {
ctf: Ctf @pgField
query: Query
}

extend type Mutation {
importCtf(input: ImportCtfInput) : ImportCtfPayload
}
`,
resolvers: {
Mutation: {
importCtf: async (_query, { input: { ctftimeId } }, { pgClient }, resolveInfo) => {
const ctf = await fetchFromCtftime(ctftimeId)
await savepointWrapper(pgClient, async () => {
const { rows: [newCtf] } = await pgClient.query(
`INSERT INTO ctfnote.ctf(
extend type Mutation {
importCtf(input: ImportCtfInput): ImportCtfPayload
}
`,
resolvers: {
Mutation: {
importCtf: async (
_query,
{ input: { ctftimeId } },
{ pgClient },
resolveInfo
) => {
const ctf = await fetchFromCtftime(ctftimeId);
await savepointWrapper(pgClient, async () => {
const {
rows: [newCtf],
} = await pgClient.query(
`INSERT INTO ctfnote.ctf(
title,
weight,
ctf_url,
Expand All @@ -60,32 +63,33 @@ export default makeExtendSchemaPlugin(build => {
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
RETURNING *;
`,
[
ctf.title,
ctf.weight,
ctf.url,
ctf.logo,
ctf.ctftime_url,
ctf.description,
ctf.start,
ctf.finish,
]
)
const [row] = await resolveInfo.graphile.selectGraphQLResultFromTable(
sql.fragment`ctfnote.ctf`,
(tableAlias, queryBuilder) => {
queryBuilder.where(
sql.fragment`${tableAlias}.id = ${sql.value(newCtf.id)}`
);
}
)
return {
data: row,
query: build.$$isQuery,
};
})
},
},
[
ctf.title,
ctf.weight,
ctf.url,
ctf.logo,
ctf.ctftime_url,
ctf.description,
ctf.start,
ctf.finish,
]
);
const [row] =
await resolveInfo.graphile.selectGraphQLResultFromTable(
sql.fragment`ctfnote.ctf`,
(tableAlias, queryBuilder) => {
queryBuilder.where(
sql.fragment`${tableAlias}.id = ${sql.value(newCtf.id)}`
);
}
);
return {
data: row,
query: build.$$isQuery,
};
});
},
};
},
},
};
});
29 changes: 16 additions & 13 deletions api/src/plugins/savepointWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { Client } from "pg"
import { Client } from "pg";

async function savepointWrapper(pgClient: Client, f: () => void): Promise<void> {
const name = `"CHECKPOINT-${Math.floor(Math.random() * 0xffff)}"`
await pgClient.query(`SAVEPOINT ${name}`);
try {
await f()
} catch (e) {
await pgClient.query(`ROLLBACK TO SAVEPOINT ${name}`);
throw e;
} finally {
await pgClient.query(`RELEASE SAVEPOINT ${name}`);
}
async function savepointWrapper(
pgClient: Client,
f: () => void
): Promise<void> {
const name = `"CHECKPOINT-${Math.floor(Math.random() * 0xffff)}"`;
await pgClient.query(`SAVEPOINT ${name}`);
try {
await f();
} catch (e) {
await pgClient.query(`ROLLBACK TO SAVEPOINT ${name}`);
throw e;
} finally {
await pgClient.query(`RELEASE SAVEPOINT ${name}`);
}
}

export default savepointWrapper;
export default savepointWrapper;
7 changes: 4 additions & 3 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ services:
hedgedoc:
image: quay.io/hedgedoc/hedgedoc:1.9.0-alpine
environment:
- CMD_DB_URL=postgres://ctfnote:ctfnote@db:5432/hedgedoc
- CMD_URL_PATH=pad
- CMD_IMAGE_UPLOAD_TYPE=filesystem
CMD_DB_URL: 'postgres://ctfnote:ctfnote@db:5432/hedgedoc'
CMD_URL_PATH: 'pad'
CMD_IMAGE_UPLOAD_TYPE: 'filesystem'
CMD_CSP_ENABLE: 'false'
depends_on:
- db
restart: always
Expand Down
6 changes: 5 additions & 1 deletion front/src/components/CTF/Guests.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ export default defineComponent({
},
computed: {
guests() {
return this.team.filter((p) => p.role == Role.UserGuest || (p.role == Role.UserFriend && this.ctf.endTime > this.now));
return this.team.filter(
(p) =>
p.role == Role.UserGuest ||
(p.role == Role.UserFriend && this.ctf.endTime > this.now)
);
},
guestsWithInvitation() {
return this.guests.map((g) => {
Expand Down