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] Search for Tasks and CTFs #74

Open
wants to merge 17 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: 2 additions & 0 deletions api/migrations/39-search-tasks-n-ctfs.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CREATE INDEX ON ctfnote.ctf (title);
CREATE INDEX ON ctfnote.task (title);
6 changes: 3 additions & 3 deletions 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 All @@ -27,8 +27,8 @@
"graphile-utils": "^4.11.2",
"graphql": "^15.6.1",
"graphql-upload": "^12.0.0",
"postgraphile": "^4.11.0",
"postgraphile-plugin-connection-filter": "^2.1.1",
"postgraphile": "^4.12.8",
"postgraphile-plugin-connection-filter": "^2.2.2",
"postgres-migrations": "^5.3.0"
},
"devDependencies": {
Expand Down
12 changes: 11 additions & 1 deletion api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import createTasKPlugin from "./plugins/createTask";
import importCtfPlugin from "./plugins/importCtf";
import uploadLogoPlugin from "./plugins/uploadLogo";
import uploadScalar from "./plugins/uploadScalar";
import ConnectionFilterPlugin from "postgraphile-plugin-connection-filter";

function getDbUrl(role: "user" | "admin") {
const login = config.db[role].login;
Expand Down Expand Up @@ -43,6 +44,7 @@ function createOptions() {
importCtfPlugin,
uploadLogoPlugin,
createTasKPlugin,
ConnectionFilterPlugin,
],
ownerConnectionString: getDbUrl("admin"),
enableQueryBatching: true,
Expand All @@ -60,6 +62,14 @@ function createOptions() {
postgraphileOptions.jwtSecret = "DEV";
postgraphileOptions.showErrorStack = "json" as const;
postgraphileOptions.extendedErrors = ["hint", "detail", "errcode"];

postgraphileOptions.graphileBuildOptions = {
connectionFilterAllowedOperators: ["includesInsensitive"],
connectionFilterAllowedFieldTypes: ["String"],
connectionFilterComputedColumns: false,
connectionFilterSetofFunctions: false,
connectionFilterArrays: false,
};
}
return postgraphileOptions;
}
Expand All @@ -70,7 +80,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;
69 changes: 39 additions & 30 deletions api/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1345,24 +1345,24 @@ graceful-fs@^4.1.2:
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.8.tgz#e412b8d33f5e006593cbd3cee6df9f2cebbe802a"
integrity sha512-qkIilPUYcNhJpd33n0GBXTB1MMPp14TxEsEs0pTrsSVucApsYzW5V+Q8Qxhik6KU3evy+qkAAowTByymK0avdg==

[email protected].1:
version "4.12.1"
resolved "https://registry.yarnpkg.com/graphile-build-pg/-/graphile-build-pg-4.12.1.tgz#f6102a60968076beedfefeb102e8558255e65921"
integrity sha512-Rd9QBtbyLJ425VUeHggFCvh3s3oKR58mUU6JYK0sQTRjYbxrE/eWDNAQZQLuWeemfSHvgS5W0EzppkHjkoiWiQ==
[email protected].2:
version "4.12.2"
resolved "https://registry.yarnpkg.com/graphile-build-pg/-/graphile-build-pg-4.12.2.tgz#b816824488ba97f797f61c7d129e10e21fe64638"
integrity sha512-4zWS7yb2L3afNpzADX9iBc2do4UOd3abiHd/WG0ao8lharU4YxEDS5qKlE2/2s+gSNqW2okKXEuI1/ci9DXVbw==
dependencies:
"@graphile/lru" "4.11.0"
chalk "^2.4.2"
debug "^4.1.1"
graphile-build "4.12.0"
graphile-build "4.12.2"
jsonwebtoken "^8.5.1"
lodash ">=4 <5"
lru-cache ">=4 <5"
pg-sql2 "4.12.1"

[email protected].0:
version "4.12.0"
resolved "https://registry.yarnpkg.com/graphile-build/-/graphile-build-4.12.0.tgz#726bdb1338f13b30cdec35ee3b0c9af8aa7f7dab"
integrity sha512-P4urOvOf4C8uzuuCq8BjFb+qffQvWUnZamrEyRC/0BfKKPkZhQ/HYqe9M7JkwiH8uFekHaTXDBrdR+OPoZEdhw==
[email protected].2:
version "4.12.2"
resolved "https://registry.yarnpkg.com/graphile-build/-/graphile-build-4.12.2.tgz#156b0af43ebd2f60622bda573f0aa34b01735e2a"
integrity sha512-UqomiSnWPj4pjO6Q6PzT1YeH96k7e0JzCBI3X8kkELG+PP2BOQCNE5e+xLJvohJmUr0YBTgflPQo7P1ZESPwww==
dependencies:
"@graphile/lru" "4.11.0"
chalk "^2.4.2"
Expand All @@ -1374,7 +1374,7 @@ [email protected]:
pluralize "^7.0.0"
semver "^6.0.0"

graphile-utils@^4.11.2, graphile-utils@^4.12.1:
graphile-utils@^4.11.2:
version "4.12.1"
resolved "https://registry.yarnpkg.com/graphile-utils/-/graphile-utils-4.12.1.tgz#52039718d1f8a30bf73c60a6d14c619c58006b91"
integrity sha512-+yfKs2W59lVgl/KcZrcGIZ3CQ/eyitZ+HHkbgAVRonA4PaWTSvaqLH4xNvvJu4X7sIGFxb4GWzCxTLnyjHRrmg==
Expand All @@ -1383,6 +1383,15 @@ graphile-utils@^4.11.2, graphile-utils@^4.12.1:
graphql ">=0.9 <0.14 || ^14.0.2 || ^15.4.0"
tslib "^2.0.1"

graphile-utils@^4.12.2:
version "4.12.2"
resolved "https://registry.yarnpkg.com/graphile-utils/-/graphile-utils-4.12.2.tgz#2858462672eecdb53c5327ffc22abb61f4e30e06"
integrity sha512-2UcTWWMFLFkKwbDLqlN0mF5sxLqz9y0p7I3zNOJpXtHVjrHyp7oQZsWComPsit/PWIrIgtDUagP+HPkypBRrqA==
dependencies:
debug "^4.1.1"
graphql ">=0.9 <0.14 || ^14.0.2 || ^15.4.0"
tslib "^2.0.1"

[email protected]:
version "4.12.0"
resolved "https://registry.yarnpkg.com/graphql-parse-resolve-info/-/graphql-parse-resolve-info-4.12.0.tgz#b5e83c1f56236660dee2cee9541ba70463e859a9"
Expand All @@ -1409,10 +1418,10 @@ graphql-upload@^12.0.0:
isobject "^4.0.0"
object-path "^0.11.5"

graphql-ws@^5.1.2:
version "5.5.0"
resolved "https://registry.yarnpkg.com/graphql-ws/-/graphql-ws-5.5.0.tgz#79f10248d23d104369eaef93acb9f887276a2c42"
integrity sha512-WQepPMGQQoqS2VsrI2I3RMLCVz3CW4/6ZqGV6ABDOwH4R62DzjxwMlwZbj6vhSI/7IM3/C911yITwgs77iO/hw==
graphql-ws@^5.5.5:
version "5.5.5"
resolved "https://registry.yarnpkg.com/graphql-ws/-/graphql-ws-5.5.5.tgz#f375486d3f196e2a2527b503644693ae3a8670a9"
integrity sha512-hvyIS71vs4Tu/yUYHPvGXsTgo0t3arU820+lT5VjZS2go0ewp2LqyCgxEN56CzOG7Iys52eRhHBiD1gGRdiQtw==

"graphql@>=0.9 <0.14 || ^14.0.2 || ^15.4.0", "graphql@^0.6.0 || ^0.7.0 || ^0.8.0-b || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.2 || ^15.0.0", graphql@^15.3.0, graphql@^15.6.1:
version "15.6.1"
Expand Down Expand Up @@ -2259,26 +2268,26 @@ pluralize@^7.0.0:
resolved "https://registry.yarnpkg.com/pluralize/-/pluralize-7.0.0.tgz#298b89df8b93b0221dbf421ad2b1b1ea23fc6777"
integrity sha512-ARhBOdzS3e41FbkW/XWrTEtukqqLoK5+Z/4UeDaLuSW+39JPeFgs4gCGqsrJHVZX0fUrx//4OF0K1CUGwlIFow==

[email protected].1:
version "4.12.1"
resolved "https://registry.yarnpkg.com/postgraphile-core/-/postgraphile-core-4.12.1.tgz#fcee9d54b02a56666a8026df56e0b0540e712cf4"
integrity sha512-BukrJ3j+H4dtEaCCZOPjLZ6+DJnAYHPOvFpDC85w9T4xlpLFCx7/E8ZlZFm7z/P6f/s8SS8EpL7lazms6uB8FQ==
[email protected].2:
version "4.12.2"
resolved "https://registry.yarnpkg.com/postgraphile-core/-/postgraphile-core-4.12.2.tgz#a15107cf297ed8091004621e73ba4a53d31e1d2a"
integrity sha512-+2OWlPVsMAVjYRMBSI/CT4GUB0mkSmPKGopKapfvhW40SCUBiPB/kqTylX2viRRnN8FuZtS3cRaTPiWr1K+DIg==
dependencies:
graphile-build "4.12.0"
graphile-build-pg "4.12.1"
graphile-build "4.12.2"
graphile-build-pg "4.12.2"
tslib "^2.0.1"

postgraphile-plugin-connection-filter@^2.1.1:
postgraphile-plugin-connection-filter@^2.2.2:
version "2.2.2"
resolved "https://registry.yarnpkg.com/postgraphile-plugin-connection-filter/-/postgraphile-plugin-connection-filter-2.2.2.tgz#c2444a01a4a6506f9e8a485f18b6cf5bcd17e43b"
integrity sha512-/PkdMwaIfGM++C8G5QUAF1iosAVEyIbPaaYteCMgfNWjW215SP9+8L+ewfi6Dm9Weaf+qKnNERWx7xlY8kuWOw==
dependencies:
tslib "^2.3.0"

postgraphile@^4.11.0:
version "4.12.4"
resolved "https://registry.yarnpkg.com/postgraphile/-/postgraphile-4.12.4.tgz#c07395618e12f3e35f401ec7c75d7e34e48de69b"
integrity sha512-ylx3w9MQ9tSOXCIPIzqseyTX57R6PJOQrQYdNn3SMrj6Glrt7mpzNNLW5BDwQvuViLnIIOaUEkDdHG1IaHHMmA==
postgraphile@^4.12.8:
version "4.12.8"
resolved "https://registry.yarnpkg.com/postgraphile/-/postgraphile-4.12.8.tgz#f42f2114e69e074c3ab11bad4431f8f893edd403"
integrity sha512-n8QqLkTtLmVItXeXAdEby4Qv8d6l7/AcDDqHy0BsMojVZgpwfmAIlF8lMOF4xOqqeUydnO0HCiVZjqaBhxjVJw==
dependencies:
"@graphile/lru" "4.11.0"
"@types/json5" "^0.0.30"
Expand All @@ -2290,11 +2299,11 @@ postgraphile@^4.11.0:
commander "^2.19.0"
debug "^4.1.1"
finalhandler "^1.0.6"
graphile-build "4.12.0"
graphile-build-pg "4.12.1"
graphile-utils "^4.12.1"
graphile-build "4.12.2"
graphile-build-pg "4.12.2"
graphile-utils "^4.12.2"
graphql "^0.6.0 || ^0.7.0 || ^0.8.0-b || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.2 || ^15.0.0"
graphql-ws "^5.1.2"
graphql-ws "^5.5.5"
http-errors "^1.5.1"
iterall "^1.0.2"
json5 "^2.1.1"
Expand All @@ -2303,7 +2312,7 @@ postgraphile@^4.11.0:
pg ">=6.1.0 <9"
pg-connection-string "^2.0.0"
pg-sql2 "4.12.1"
postgraphile-core "4.12.1"
postgraphile-core "4.12.2"
subscriptions-transport-ws "^0.9.18"
tslib "^2.1.0"
ws "^7.4.2"
Expand Down
Loading