From 55094d804144e335530e9638b194ebc82f0ef485 Mon Sep 17 00:00:00 2001 From: Jordan Eldredge Date: Sun, 20 Feb 2022 01:41:19 -0800 Subject: [PATCH] Extend the graphql schema more --- packages/skin-database/api/graphql/index.ts | 33 +++++++++++++++++-- .../graphql/resolvers/ArchiveFileResolver.ts | 6 ++++ .../skin-database/data/ArchiveFileModel.ts | 14 ++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/packages/skin-database/api/graphql/index.ts b/packages/skin-database/api/graphql/index.ts index bfc80eca..6704461e 100644 --- a/packages/skin-database/api/graphql/index.ts +++ b/packages/skin-database/api/graphql/index.ts @@ -3,7 +3,9 @@ import { graphqlHTTP } from "express-graphql"; import { buildSchema } from "graphql"; import SkinModel from "../../data/SkinModel"; +import TweetModel from "../../data/TweetModel"; import SkinResolver from "./resolvers/SkinResolver"; +import TweetResolver from "./resolvers/TweetResolver"; import UserResolver from "./resolvers/UserResolver"; import SkinsConnection from "./SkinsConnection"; import TweetsConnection from "./TweetsConnection"; @@ -11,7 +13,6 @@ import TweetsConnection from "./TweetsConnection"; const router = Router(); const schema = buildSchema(` - """A classic Winamp skin""" type Skin { """Database ID of the skin""" @@ -97,11 +98,29 @@ type Review { type ArchiveFile { """Filename of the file within the archive""" filename: String, + + """ + A URL to download the file. **Note:** This is powered by a little + serverless Cloudflare function which tries to exctact the file on the fly. + It may not work for all files. + """ + url: String + + """ + The date on the file inside the archive. Given in simplified extended ISO + format (ISO 8601). + """ + date: String + } """A tweet made by @winampskins mentioning a Winamp skin""" type Tweet { - """URL of the tweet""" + """ + URL of the tweet. **Note:** Early on in the bot's life we just recorded + _which_ skins were tweeted, not any info about the actual tweet. This means we + don't always know the URL of the tweet. + """ url: String """Number of likes the tweet has received. Updated nightly. (Note: Recent likes on older tweets may not be reflected here)""" @@ -178,6 +197,9 @@ type Query { """Get a skin by its MD5 hash""" fetch_skin_by_md5(md5: String!): Skin + """Get a tweet by its URL""" + fetch_tweet_by_url(url: String!): Tweet + """ All skins in the database @@ -208,6 +230,13 @@ const root = { } return new SkinResolver(skin); }, + async fetch_tweet_by_url({ url }, { ctx }) { + const tweet = await TweetModel.fromAnything(ctx, url); + if (tweet == null) { + return null; + } + return new TweetResolver(tweet); + }, async skins({ first, offset, sort, filter }) { if (first > 1000) { throw new Error("Maximum limit is 1000"); diff --git a/packages/skin-database/api/graphql/resolvers/ArchiveFileResolver.ts b/packages/skin-database/api/graphql/resolvers/ArchiveFileResolver.ts index 229f6c1f..5b3b9d65 100644 --- a/packages/skin-database/api/graphql/resolvers/ArchiveFileResolver.ts +++ b/packages/skin-database/api/graphql/resolvers/ArchiveFileResolver.ts @@ -8,4 +8,10 @@ export default class ArchiveFileResolver { filename() { return this._model.getFileName(); } + url() { + return this._model.getUrl(); + } + date() { + return this._model.getFileDate().toISOString(); + } } diff --git a/packages/skin-database/data/ArchiveFileModel.ts b/packages/skin-database/data/ArchiveFileModel.ts index 353e0a73..6639510a 100644 --- a/packages/skin-database/data/ArchiveFileModel.ts +++ b/packages/skin-database/data/ArchiveFileModel.ts @@ -2,6 +2,7 @@ import UserContext, { ctxWeakMapMemoize } from "./UserContext"; import { ArchiveFileRow } from "../types"; import DataLoader from "dataloader"; import { knex } from "../db"; +import SkinModel from "./SkinModel"; export type ArchiveFileDebugData = { row: ArchiveFileRow; @@ -18,6 +19,10 @@ export default class ArchiveFileModel { return rows.map((row) => new ArchiveFileModel(ctx, row)); } + getMd5(): string { + return this.row.skin_md5; + } + getFileName(): string { return this.row.file_name; } @@ -26,6 +31,15 @@ export default class ArchiveFileModel { return new Date(this.row.file_date); } + getUrl(): string { + const filename = encodeURIComponent(this.getFileName()); + return `https://zip-worker.jordan1320.workers.dev/zip/${this.getMd5()}/${filename}`; + } + + async getSkin(): Promise { + return SkinModel.fromMd5Assert(this.ctx, this.getMd5()); + } + async debug(): Promise { return { row: this.row,