|
| 1 | +import { Memorize, longTermCache } from "../cache.js"; |
| 2 | +import type { ContentData, ServiceInterface, ServiceMetadata } from "./abstract.js"; |
| 3 | + |
| 4 | +export const TYPE = "guild" as const; |
| 5 | + |
| 6 | +export class GuildService implements ServiceInterface<{ cover: string; members: number }> { |
| 7 | + canHandle(metadata: ServiceMetadata): Promise<boolean> { |
| 8 | + return Promise.resolve(metadata.type === TYPE); |
| 9 | + } |
| 10 | + async getInformation(metadata: ServiceMetadata): Promise<(ContentData & { cover: string; members: number }) | never> { |
| 11 | + const guilds = await this.getGuilds(); |
| 12 | + let guild: GuildHostGuildNodeResponse | undefined = guilds.data.nodeBySlugId.guilds.edges.find( |
| 13 | + (guild) => guild.node.slugId === metadata.identifier, |
| 14 | + )?.node; |
| 15 | + |
| 16 | + if (guild === undefined) { |
| 17 | + guild = (await this.getOneGuild(metadata.identifier))?.data.nodeBySlugId; |
| 18 | + } |
| 19 | + |
| 20 | + if (guild === undefined) { |
| 21 | + throw new Error("Invalid data service"); |
| 22 | + } |
| 23 | + |
| 24 | + return { |
| 25 | + name: guild.name, |
| 26 | + description: guild.description, |
| 27 | + type: TYPE, |
| 28 | + author: "guild.host", |
| 29 | + keywords: [], |
| 30 | + members: guild.networkMembers.totalCount, |
| 31 | + cover: guild.backgroundPhoto |
| 32 | + ? `https://ik.imagekit.io/guild/prod/tr:w-600,dpr-1/${guild.backgroundPhoto?.rowId}.${guild.backgroundPhoto?.contentType.toLowerCase()}` |
| 33 | + : "", |
| 34 | + }; |
| 35 | + } |
| 36 | + |
| 37 | + @Memorize(longTermCache) |
| 38 | + private getOneGuild(id: string): Promise<GuildHostOneResponse> { |
| 39 | + return fetch("https://guild.host/graphql/1bab259132f26a669a0dbe5184260401be37f259bf888010711abab5438e8e51", { |
| 40 | + headers: { |
| 41 | + "X-Gqlvars": `{"id":"${id}"}`, |
| 42 | + }, |
| 43 | + }).then((response) => response.json()); |
| 44 | + } |
| 45 | + |
| 46 | + @Memorize(longTermCache) |
| 47 | + private getGuilds(): Promise<GuildHostResponse> { |
| 48 | + return fetch("https://guild.host/graphql/932d20dc08db4fb8d9f86e17908ba8ccb8b1de931ad51718689570c942c82c71", { |
| 49 | + headers: { |
| 50 | + "X-Gqlvars": '{"id":"svelte-society"}', |
| 51 | + }, |
| 52 | + }).then((response) => response.json()); |
| 53 | + } |
| 54 | + |
| 55 | + getAllServiceMetadata(): Promise<Array<ServiceMetadata>> { |
| 56 | + return this.getGuilds().then((response) => |
| 57 | + response.data.nodeBySlugId.guilds.edges.map((guild) => ({ |
| 58 | + type: TYPE, |
| 59 | + identifier: guild.node.slugId, |
| 60 | + })), |
| 61 | + ); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +type GuildHostResponse = { |
| 66 | + data: { |
| 67 | + nodeBySlugId: { |
| 68 | + __typename: "Guild"; |
| 69 | + name: string; |
| 70 | + slugId: string; |
| 71 | + description: string; |
| 72 | + networks: { edges: []; pageInfo: { endCursor: null; hasNextPage: boolean } }; |
| 73 | + id: string; |
| 74 | + guilds: { |
| 75 | + edges: Array<{ |
| 76 | + node: GuildHostGuildNodeResponse; |
| 77 | + cursor: string; |
| 78 | + }>; |
| 79 | + pageInfo: { endCursor: string; hasNextPage: boolean }; |
| 80 | + }; |
| 81 | + __isNode: "Guild"; |
| 82 | + }; |
| 83 | + }; |
| 84 | +}; |
| 85 | + |
| 86 | +type GuildHostGuildNodeResponse = { |
| 87 | + __typename: "Guild"; |
| 88 | + id: string; |
| 89 | + slugId: string; |
| 90 | + type: "NETWORK" | "GUILD"; |
| 91 | + myMembership: unknown; |
| 92 | + minimumDonationAmount?: number; |
| 93 | + donationCurrency?: "EUR"; |
| 94 | + rowId: string; |
| 95 | + name: string; |
| 96 | + description: string; |
| 97 | + networkMembers: { |
| 98 | + totalCount: number; |
| 99 | + }; |
| 100 | + primaryPhoto: { |
| 101 | + rowId: string; |
| 102 | + contentType: "PNG" | "WEBP" | "JPEG"; |
| 103 | + id: string; |
| 104 | + } | null; |
| 105 | + backgroundPhoto: { |
| 106 | + rowId: string; |
| 107 | + contentType: "PNG" | "WEBP" | "JPEG"; |
| 108 | + id: string; |
| 109 | + } | null; |
| 110 | + totalNetworks?: { |
| 111 | + edges: Array<unknown>; |
| 112 | + }; |
| 113 | + totalGuilds?: { |
| 114 | + edges: Array<{ |
| 115 | + cursor: string; |
| 116 | + }>; |
| 117 | + }; |
| 118 | + totalEvents?: { |
| 119 | + edges: Array<{ |
| 120 | + cursor: string; |
| 121 | + }>; |
| 122 | + }; |
| 123 | + totalPresentations?: { |
| 124 | + edges: Array<{ |
| 125 | + cursor: string; |
| 126 | + }>; |
| 127 | + }; |
| 128 | + __isNode?: "Guild"; |
| 129 | +}; |
| 130 | + |
| 131 | +type GuildHostOneResponse = { |
| 132 | + data: { |
| 133 | + nodeBySlugId: GuildHostGuildNodeResponse; |
| 134 | + }; |
| 135 | +}; |
0 commit comments