From ccc774168a0612c6892b7862141e58b28b029aec Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Mon, 21 Aug 2023 23:28:36 +1000 Subject: [PATCH 01/20] Close #1091 --- src/gateway/opcodes/Identify.ts | 58 ++++++++++++++++----------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/src/gateway/opcodes/Identify.ts b/src/gateway/opcodes/Identify.ts index 7610901a9..0f5469cf7 100644 --- a/src/gateway/opcodes/Identify.ts +++ b/src/gateway/opcodes/Identify.ts @@ -17,43 +17,43 @@ */ import { - WebSocket, - Payload, - setupListener, - Capabilities, CLOSECODES, + Capabilities, OPCODES, + Payload, Send, + WebSocket, + setupListener, } from "@spacebar/gateway"; import { - checkToken, + Application, + Config, + DMChannel, + DefaultUserGuildSettings, + EVENTEnum, + Guild, + GuildOrUnavailable, + IdentifySchema, Intents, Member, - ReadyEventData, - Session, - EVENTEnum, - Config, - PublicUser, - PrivateUserProjection, - ReadState, - Application, - emitEvent, - SessionsReplace, - PrivateSessionProjection, MemberPrivateProjection, - PresenceUpdateEvent, - IdentifySchema, - DefaultUserGuildSettings, - ReadyGuildDTO, - Guild, - PublicUserProjection, - ReadyUserGuildSettingsEntries, - UserSettings, - Permissions, - DMChannel, - GuildOrUnavailable, - Recipient, OPCodes, + Permissions, + PresenceUpdateEvent, + PrivateSessionProjection, + PrivateUserProjection, + PublicUser, + PublicUserProjection, + ReadState, + ReadyEventData, + ReadyGuildDTO, + ReadyUserGuildSettingsEntries, + Recipient, + Session, + SessionsReplace, + UserSettings, + checkToken, + emitEvent, } from "@spacebar/util"; import { check } from "./instanceOf"; @@ -332,7 +332,7 @@ export async function onIdentify(this: WebSocket, data: Payload) { // TODO how is active determined? // in our lazy request impl, we just pick the 'most relevant' session active: x.session_id == session.session_id, - activities: x.activities, + activities: x.activities ?? [], client_info: x.client_info, // TODO: what does all mean? session_id: x.session_id == session.session_id ? "all" : x.session_id, From 8a7ee8c8a93bff0e90f88105932f0015eeedbbaa Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Mon, 21 Aug 2023 23:57:44 +1000 Subject: [PATCH 02/20] what the fuck is session_id: "all"??? --- src/gateway/opcodes/Identify.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/gateway/opcodes/Identify.ts b/src/gateway/opcodes/Identify.ts index 0f5469cf7..e097e44cb 100644 --- a/src/gateway/opcodes/Identify.ts +++ b/src/gateway/opcodes/Identify.ts @@ -334,8 +334,7 @@ export async function onIdentify(this: WebSocket, data: Payload) { active: x.session_id == session.session_id, activities: x.activities ?? [], client_info: x.client_info, - // TODO: what does all mean? - session_id: x.session_id == session.session_id ? "all" : x.session_id, + session_id: x.session_id, // TODO: discord.com sends 'all', what is that??? status: x.status, })); From d158b11421c43303040b4022bda46a4185cd83c5 Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Fri, 25 Aug 2023 00:03:03 +1000 Subject: [PATCH 03/20] Update build.yml --- .github/workflows/build.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e3bcc81f6..d5ce87e24 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -2,9 +2,9 @@ name: Build on: push: - branches: [ "**" ] + branches: [ "master" ] pull_request: - branches: [ "**" ] + branches: [ "master" ] jobs: build: @@ -25,4 +25,4 @@ jobs: cache: 'npm' - run: npm ci - run: npm run build --if-present - - run: npm run test --if-present \ No newline at end of file + - run: npm run test --if-present From 3a561b5cf605e03a2a4222edbc3e1dbc496da60e Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Fri, 25 Aug 2023 00:03:15 +1000 Subject: [PATCH 04/20] Update style.yml --- .github/workflows/style.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/style.yml b/.github/workflows/style.yml index 4fd62bae6..8b18f6e61 100644 --- a/.github/workflows/style.yml +++ b/.github/workflows/style.yml @@ -2,9 +2,9 @@ name: Style on: push: - branches: [ "**" ] + branches: [ "master" ] pull_request: - branches: [ "**" ] + branches: [ "master" ] jobs: build: @@ -25,4 +25,4 @@ jobs: cache: 'npm' - run: npm i --only=dev - run: npx eslint . - - run: npx prettier --check . \ No newline at end of file + - run: npx prettier --check . From 3498ffdc013e82efec16aa7b985f1e3d08bdfe99 Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Sun, 27 Aug 2023 16:54:54 +1000 Subject: [PATCH 05/20] prevent put /guilds/id/members/id for others until we have oauth2 scopes impled --- src/api/routes/guilds/#guild_id/members/#member_id/index.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/api/routes/guilds/#guild_id/members/#member_id/index.ts b/src/api/routes/guilds/#guild_id/members/#member_id/index.ts index cafb922ec..c168f2dc5 100644 --- a/src/api/routes/guilds/#guild_id/members/#member_id/index.ts +++ b/src/api/routes/guilds/#guild_id/members/#member_id/index.ts @@ -18,6 +18,7 @@ import { route } from "@spacebar/api"; import { + DiscordApiErrors, emitEvent, Emoji, getPermission, @@ -198,7 +199,9 @@ router.put( member_id = req.user_id; rights.hasThrow("JOIN_GUILDS"); } else { - // TODO: join others by controller + // TODO: check oauth2 scope + + throw DiscordApiErrors.MISSING_REQUIRED_OAUTH2_SCOPE; } const guild = await Guild.findOneOrFail({ From c311edc99c4dfc2a275ab99b887769660c29bbf0 Mon Sep 17 00:00:00 2001 From: Puyodead1 Date: Sun, 27 Aug 2023 20:16:49 -0400 Subject: [PATCH 06/20] make guild voice a text based channel --- src/util/entities/Channel.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/entities/Channel.ts b/src/util/entities/Channel.ts index 19952bc21..6268d735f 100644 --- a/src/util/entities/Channel.ts +++ b/src/util/entities/Channel.ts @@ -506,7 +506,6 @@ export interface DMChannel extends Omit { export function isTextChannel(type: ChannelType): boolean { switch (type) { case ChannelType.GUILD_STORE: - case ChannelType.GUILD_VOICE: case ChannelType.GUILD_STAGE_VOICE: case ChannelType.GUILD_CATEGORY: case ChannelType.GUILD_FORUM: @@ -515,6 +514,7 @@ export function isTextChannel(type: ChannelType): boolean { case ChannelType.DM: case ChannelType.GROUP_DM: case ChannelType.GUILD_NEWS: + case ChannelType.GUILD_VOICE: case ChannelType.GUILD_NEWS_THREAD: case ChannelType.GUILD_PUBLIC_THREAD: case ChannelType.GUILD_PRIVATE_THREAD: From ed7fd34f46ffd1322f7d0e6ea81e4612afb0ec4a Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Sat, 2 Sep 2023 01:01:58 +1000 Subject: [PATCH 07/20] DB_LOGGING env var --- src/util/util/Database.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/util/util/Database.ts b/src/util/util/Database.ts index a6b24b3e3..3a45eea0e 100644 --- a/src/util/util/Database.ts +++ b/src/util/util/Database.ts @@ -16,12 +16,12 @@ along with this program. If not, see . */ -import { DataSource } from "typeorm"; -import { yellow, green, red } from "picocolors"; -import { Migration } from "../entities/Migration"; -import { ConfigEntity } from "../entities/Config"; import { config } from "dotenv"; import path from "path"; +import { green, red, yellow } from "picocolors"; +import { DataSource } from "typeorm"; +import { ConfigEntity } from "../entities/Config"; +import { Migration } from "../entities/Migration"; // UUID extension option is only supported with postgres // We want to generate all id's with Snowflakes that's why we have our own BaseEntity class @@ -50,7 +50,7 @@ const DataSourceOptions = new DataSource({ database: isSqlite ? dbConnectionString : undefined, entities: [path.join(__dirname, "..", "entities", "*.js")], synchronize: !!process.env.DB_SYNC, - logging: false, + logging: !!process.env.DB_LOGGING, bigNumberStrings: false, supportBigNumbers: true, name: "default", @@ -129,7 +129,7 @@ export async function initDatabase(): Promise { return dbConnection; } -export { dbConnection, DataSourceOptions, DatabaseType }; +export { DataSourceOptions, DatabaseType, dbConnection }; export async function closeDatabase() { await dbConnection?.destroy(); From 2f48212e6e6813f90eb13cf8ed79e0ebb3440b31 Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Sat, 2 Sep 2023 02:21:42 +1000 Subject: [PATCH 08/20] typeorm inverse relations of guild relations --- src/util/entities/Channel.ts | 5 +++-- src/util/entities/Emoji.ts | 2 +- src/util/entities/Invite.ts | 2 +- src/util/entities/Member.ts | 1 + src/util/entities/Role.ts | 4 ++-- src/util/entities/Sticker.ts | 4 ++-- src/util/entities/VoiceState.ts | 4 ++-- 7 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/util/entities/Channel.ts b/src/util/entities/Channel.ts index 6268d735f..9f7041d42 100644 --- a/src/util/entities/Channel.ts +++ b/src/util/entities/Channel.ts @@ -97,10 +97,11 @@ export class Channel extends BaseClass { guild_id?: string; @JoinColumn({ name: "guild_id" }) - @ManyToOne(() => Guild, { + @ManyToOne(() => Guild, (guild) => guild.channels, { onDelete: "CASCADE", + nullable: true, }) - guild: Guild; + guild?: Guild; @Column({ nullable: true }) @RelationId((channel: Channel) => channel.parent) diff --git a/src/util/entities/Emoji.ts b/src/util/entities/Emoji.ts index 0bc2f4231..4d851698f 100644 --- a/src/util/entities/Emoji.ts +++ b/src/util/entities/Emoji.ts @@ -33,7 +33,7 @@ export class Emoji extends BaseClass { guild_id: string; @JoinColumn({ name: "guild_id" }) - @ManyToOne(() => Guild, { + @ManyToOne(() => Guild, (guild) => guild.emojis, { onDelete: "CASCADE", }) guild: Guild; diff --git a/src/util/entities/Invite.ts b/src/util/entities/Invite.ts index 7970c4f00..f7e54fbe8 100644 --- a/src/util/entities/Invite.ts +++ b/src/util/entities/Invite.ts @@ -53,7 +53,7 @@ export class Invite extends BaseClassWithoutId { guild_id: string; @JoinColumn({ name: "guild_id" }) - @ManyToOne(() => Guild, { + @ManyToOne(() => Guild, (guild) => guild.invites, { onDelete: "CASCADE", }) guild: Guild; diff --git a/src/util/entities/Member.ts b/src/util/entities/Member.ts index d305e4f5c..0535313e2 100644 --- a/src/util/entities/Member.ts +++ b/src/util/entities/Member.ts @@ -327,6 +327,7 @@ export class Member extends BaseClassWithoutId { id: guild_id, }, relations: PublicGuildRelations, + relationLoadStrategy: "query", }); const memberCount = await Member.count({ where: { guild_id } }); diff --git a/src/util/entities/Role.ts b/src/util/entities/Role.ts index 9a601f31c..e8e5fedaf 100644 --- a/src/util/entities/Role.ts +++ b/src/util/entities/Role.ts @@ -23,12 +23,12 @@ import { Guild } from "./Guild"; @Entity("roles") export class Role extends BaseClass { - @Column({ nullable: true }) + @Column() @RelationId((role: Role) => role.guild) guild_id: string; @JoinColumn({ name: "guild_id" }) - @ManyToOne(() => Guild, { + @ManyToOne(() => Guild, (guild) => guild.roles, { onDelete: "CASCADE", }) guild: Guild; diff --git a/src/util/entities/Sticker.ts b/src/util/entities/Sticker.ts index cd07e65a2..e9294f921 100644 --- a/src/util/entities/Sticker.ts +++ b/src/util/entities/Sticker.ts @@ -17,9 +17,9 @@ */ import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm"; -import { User } from "./User"; import { BaseClass } from "./BaseClass"; import { Guild } from "./Guild"; +import { User } from "./User"; export enum StickerType { STANDARD = 1, @@ -62,7 +62,7 @@ export class Sticker extends BaseClass { guild_id?: string; @JoinColumn({ name: "guild_id" }) - @ManyToOne(() => Guild, { + @ManyToOne(() => Guild, (guild) => guild.stickers, { onDelete: "CASCADE", }) guild?: Guild; diff --git a/src/util/entities/VoiceState.ts b/src/util/entities/VoiceState.ts index b291c4d3b..84b0ca717 100644 --- a/src/util/entities/VoiceState.ts +++ b/src/util/entities/VoiceState.ts @@ -20,8 +20,8 @@ import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm"; import { BaseClass } from "./BaseClass"; import { Channel } from "./Channel"; import { Guild } from "./Guild"; -import { User } from "./User"; import { Member } from "./Member"; +import { User } from "./User"; //https://gist.github.com/vassjozsef/e482c65df6ee1facaace8b3c9ff66145#file-voice_state-ex @Entity("voice_states") @@ -31,7 +31,7 @@ export class VoiceState extends BaseClass { guild_id: string; @JoinColumn({ name: "guild_id" }) - @ManyToOne(() => Guild, { + @ManyToOne(() => Guild, (guild) => guild.voice_states, { onDelete: "CASCADE", }) guild?: Guild; From eebd61b19d9d21c32685e72552748874c10c8dfb Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Sat, 2 Sep 2023 14:10:32 +1000 Subject: [PATCH 09/20] update sentry --- package-lock.json | Bin 292535 -> 292984 bytes package.json | 5 ++--- src/util/util/Sentry.ts | 35 +++++++++++++++++++++++------------ 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0576c58949d073bf23327b7e96645d878b011568..aabad6442f02484fe32b8693c9832791e1448531 100644 GIT binary patch delta 1184 zcmc(d&u`LT7{|kOCK`Y3NA#f1jcg~(M(IzC2U}VOrDZR)l)}MeKu4hrN}&|EEQ>K- zJoz3L<7xlFl4WCCbeqwGdh+1G#P}!7%$pYzL6!xh_r1ycCQshO_wzi@r*ky&^LS)~ zcDadJulwGmi{0aXSkuaNt2I}y28gztBzT?8C?=Ytzt!`l$wWTvi38b?|gwJDjS_W(D)u9$ zC9f7E1YxUcR!|jDRoW%9UKQ(AMB}+=z-BeA9V>7_D@v0{#1PoL*XwYS=@A4^ri41C zA~Uo}TV^ImB)EB8hpVO=e%l@GHoW@z1~hk4@a5jEf%d=eUGKLKn$>VsurNu-n^|^= z3lx|b2JAH1pjvWX&==!|WhbpPNHt?tmJb5M$pj5&E?hI(N?DhEilZi$wImlV#l={J z;k|VVg>SA-!7r~T?*7M>Y{mNHN>3HIvfbNLe_JE3-VXOf>TT=6De80|6Kw?Y0iPZ~ zWehjVINFj~B^x(G6<%hVT8RZ9t<(x8ok^D~3koobWSPiCmsq`shEc;w1{koY14CWr zJaMe46ylUp<@?4M+hg6dL6Vb^ozjd$Pv`b2(J)-;oD451(j16VL{tm#NLa=wp;~BT zqJ-yjY>{yQCK9L$u;p61>|eBt(vmNhA;cDL%M?lTmcPx^^9Cih_?jKEP(0`bXcbwT o`Ze^o$N%!2iOU1?I8&FU=RYnyK017Qbm;2dq0Z6axsO}F0V@}x2><{9 delta 1047 zcmZvbU2oe|7{}G68skMm1;K>I$`%O;6gvCFj-7A;bz<2Zg#oC~D9IF6m#aU9#J zBXPwALIMeNB=QBgr2Dh?cHQgCm_sM>jr3>aE#iN!!=Tf%J?ps_w|i^9--h(@ z2a5;kO8}ny@)G>v_`St^Z??&R7toGb9Flh;=`0OK8dk{`x|ZI(uj4CNLIh`8ezTqBowH5D1k zQqZ(2Nr7beQ|jodykDN?}9&8O|FoK$viQ|LVUG z-|n9~$%y%iFe$f%Dv~HjP3*?nK%*JiGsm)^1rxu}N#fYgcP4mG4TEC8PG(Dn#b&{{ zLi9o#&nikC=rnJ%$F8bl%#Ju${tGcb_!|Co=iI_4cdxDtgi1XbNJ4kuw7@JMb96h5 zn`Vc`B?)QcbG?bEd~3iXT)WJ&qp4`oZFj==%Nc=F@UBu(wH#+xZEcAA#<0%dZq*Ws zi_`n58;k97Y8^hldu^%VwWI4V^ksN-9}35t7dqZLpCe=oIX`VJ=j|z@S^$EztG zA$81MK0vrRXtB$LnxXec-IkPydB@2YU5bbb9DMNUO^DumbJ>&C|4^X#$K@~f&c56` PTU+i+hkIwK-}e6jkaKk% diff --git a/package.json b/package.json index 70e4ae849..3994670b5 100644 --- a/package.json +++ b/package.json @@ -66,9 +66,8 @@ }, "dependencies": { "@aws-sdk/client-s3": "^3.385.0", - "@sentry/integrations": "^7.61.1", - "@sentry/node": "^7.61.1", - "@sentry/tracing": "^7.61.1", + "@sentry/integrations": "^7.66.0", + "@sentry/node": "^7.66.0", "ajv": "8.6.2", "ajv-formats": "2.1.1", "amqplib": "^0.10.3", diff --git a/src/util/util/Sentry.ts b/src/util/util/Sentry.ts index e302da0c2..74a23a1ec 100644 --- a/src/util/util/Sentry.ts +++ b/src/util/util/Sentry.ts @@ -16,13 +16,12 @@ along with this program. If not, see . */ -import { Config } from "./Config"; import { yellow } from "picocolors"; +import { Config } from "./Config"; -import express from "express"; -import * as SentryNode from "@sentry/node"; -import * as Tracing from "@sentry/tracing"; import * as Integrations from "@sentry/integrations"; +import * as SentryNode from "@sentry/node"; +import express from "express"; // Work around for when bundle calls api/etc let errorHandlersUsed = false; @@ -46,16 +45,28 @@ export const Sentry = { ); } + const integrations = [ + new SentryNode.Integrations.Http({ tracing: true }), + new Integrations.RewriteFrames({ + root: __dirname, + }), + new SentryNode.Integrations.Http({ + tracing: true, + breadcrumbs: true, + }), + ...SentryNode.autoDiscoverNodePerformanceMonitoringIntegrations(), + ]; + + if (app) + integrations.push( + new SentryNode.Integrations.Express({ + app, + }), + ); + SentryNode.init({ dsn: endpoint, - integrations: [ - new SentryNode.Integrations.Http({ tracing: true }), - new Tracing.Integrations.Express({ app }), - new Tracing.Integrations.Mysql(), - new Integrations.RewriteFrames({ - root: __dirname, - }), - ], + integrations, tracesSampleRate: traceSampleRate, // naming? environment, }); From 2fc3c5dbd4910a18d5a0e18065236067c25c8918 Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Sat, 2 Sep 2023 20:18:38 +1000 Subject: [PATCH 10/20] sentry db instrumentation for gateway events --- src/gateway/events/Message.ts | 55 ++++++++++++++++------------------- 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/src/gateway/events/Message.ts b/src/gateway/events/Message.ts index 457901464..52d9edd8f 100644 --- a/src/gateway/events/Message.ts +++ b/src/gateway/events/Message.ts @@ -16,15 +16,15 @@ along with this program. If not, see . */ -import { WebSocket, Payload, CLOSECODES, OPCODES } from "@spacebar/gateway"; -import OPCodeHandlers from "../opcodes"; -import { check } from "../opcodes/instanceOf"; -import WS from "ws"; -import { PayloadSchema, ErlpackType } from "@spacebar/util"; import * as Sentry from "@sentry/node"; +import { CLOSECODES, OPCODES, Payload, WebSocket } from "@spacebar/gateway"; +import { ErlpackType, PayloadSchema } from "@spacebar/util"; +import fs from "fs/promises"; import BigIntJson from "json-bigint"; import path from "path"; -import fs from "fs/promises"; +import WS from "ws"; +import OPCodeHandlers from "../opcodes"; +import { check } from "../opcodes/instanceOf"; const bigIntJson = BigIntJson({ storeAsString: true }); let erlpack: ErlpackType | null = null; @@ -88,33 +88,28 @@ export async function Message(this: WebSocket, buffer: WS.Data) { return; } - const transaction = - data.op != 1 - ? Sentry.startTransaction({ - op: OPCODES[data.op], - name: `GATEWAY ${OPCODES[data.op]}`, - data: { - ...data.d, - token: data?.d?.token ? "[Redacted]" : undefined, - }, - }) - : undefined; - try { - const ret = await OPCodeHandler.call(this, data); - Sentry.withScope((scope) => { - scope.setSpan(transaction); - scope.setUser({ id: this.user_id }); - transaction?.finish(); - }); - return ret; + return await Sentry.startActiveSpan( + { + op: "websocket.server", + name: `GATEWAY ${OPCODES[data.op]}`, + data: { + ...data.d, + token: data?.d?.token ? "[Redacted]" : undefined, + }, + }, + async () => { + const ret = await OPCodeHandler.call(this, data); + Sentry.setUser({ id: this.user_id }); + return ret; + }, + ); } catch (error) { - Sentry.withScope((scope) => { - scope.setSpan(transaction); - if (this.user_id) scope.setUser({ id: this.user_id }); - Sentry.captureException(error); + Sentry.captureException(error, { + user: { + id: this.user_id, + }, }); - transaction?.finish(); console.error(`Error: Op ${data.op}`, error); // if (!this.CLOSED && this.CLOSING) return this.close(CLOSECODES.Unknown_error); From 09f5b4f0bbb4d88e97d75dc014f549f2571cc135 Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Sat, 2 Sep 2023 21:05:27 +1000 Subject: [PATCH 11/20] Fix identify throwing on invalid token instead of closing with auth failed close code --- src/gateway/opcodes/Identify.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/gateway/opcodes/Identify.ts b/src/gateway/opcodes/Identify.ts index e097e44cb..9a3128d9f 100644 --- a/src/gateway/opcodes/Identify.ts +++ b/src/gateway/opcodes/Identify.ts @@ -60,6 +60,14 @@ import { check } from "./instanceOf"; // TODO: user sharding // TODO: check privileged intents, if defined in the config +const tryGetUserFromToken = async (...args: Parameters) => { + try { + return (await checkToken(...args)).user; + } catch (e) { + return null; + } +}; + export async function onIdentify(this: WebSocket, data: Payload) { if (this.user_id) { // we've already identified @@ -74,7 +82,7 @@ export async function onIdentify(this: WebSocket, data: Payload) { this.capabilities = new Capabilities(identify.capabilities || 0); - const { user } = await checkToken(identify.token, { + const user = await tryGetUserFromToken(identify.token, { relations: ["relationships", "relationships.to", "settings"], select: [...PrivateUserProjection, "relationships"], }); From c64a11572911ec520bcde2477ff15d4c0d65c235 Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Sat, 2 Sep 2023 23:47:40 +1000 Subject: [PATCH 12/20] xkcd embed handler and improved generic handler --- src/api/util/utility/EmbedHandlers.ts | 73 +++++++++++++++++++-------- 1 file changed, 53 insertions(+), 20 deletions(-) diff --git a/src/api/util/utility/EmbedHandlers.ts b/src/api/util/utility/EmbedHandlers.ts index 15e3f67f3..e8f394077 100644 --- a/src/api/util/utility/EmbedHandlers.ts +++ b/src/api/util/utility/EmbedHandlers.ts @@ -17,11 +17,11 @@ */ import { Config, Embed, EmbedType } from "@spacebar/util"; -import fetch, { RequestInit } from "node-fetch"; import * as cheerio from "cheerio"; -import probe from "probe-image-size"; import crypto from "crypto"; +import fetch, { RequestInit } from "node-fetch"; import { yellow } from "picocolors"; +import probe from "probe-image-size"; export const DEFAULT_FETCH_OPTIONS: RequestInit = { redirect: "follow", @@ -85,6 +85,7 @@ export const getMetaDescriptions = (text: string) => { const $ = cheerio.load(text); return { + type: getMeta($, "og:type"), title: getMeta($, "og:title") || $("title").first().text(), provider_name: getMeta($, "og:site_name"), author: getMeta($, "article:author"), @@ -96,6 +97,8 @@ export const getMetaDescriptions = (text: string) => { height: parseInt(getMeta($, "og:image:height") || "0"), url: getMeta($, "og:url"), youtube_embed: getMeta($, "og:video:secure_url"), + + $, }; }; @@ -116,7 +119,7 @@ const genericImageHandler = async (url: URL): Promise => { method: "HEAD", }); - let width, height, image; + let width: number, height: number, image: string | undefined; if (type.headers.get("content-type")?.indexOf("image") !== -1) { const result = await probe(url.href); @@ -181,9 +184,16 @@ export const EmbedHandlers: { return null; } + let embedType = EmbedType.link; + if (metas.type == "article") embedType = EmbedType.article; + if (metas.type == "object") embedType = EmbedType.article; // github + if (metas.type == "rich") embedType = EmbedType.rich; + + if (metas.width < 400) embedType = EmbedType.link; + return { url: url.href, - type: EmbedType.link, + type: embedType, title: metas.title, thumbnail: { width: metas.width, @@ -210,9 +220,7 @@ export const EmbedHandlers: { // TODO: facebook // have to use their APIs or something because they don't send the metas in initial html - "twitter.com": (url: URL) => { - return EmbedHandlers["www.twitter.com"](url); - }, + "twitter.com": (url) => EmbedHandlers["www.twitter.com"](url), "www.twitter.com": async (url: URL) => { const token = Config.get().external.twitter; if (!token) return null; @@ -345,15 +353,15 @@ export const EmbedHandlers: { }; }, - "pixiv.net": (url: URL) => { - return EmbedHandlers["www.pixiv.net"](url); - }, + // TODO: docs: Pixiv won't work without Imagor + "pixiv.net": (url) => EmbedHandlers["www.pixiv.net"](url), "www.pixiv.net": async (url: URL) => { const response = await doFetch(url); if (!response) return null; const metas = getMetaDescriptions(await response.text()); - // TODO: doesn't show images. think it's a bug in the cdn + if (!metas.image) return null; + return { url: url.href, type: EmbedType.image, @@ -407,9 +415,7 @@ export const EmbedHandlers: { }; }, - "reddit.com": (url: URL) => { - return EmbedHandlers["www.reddit.com"](url); - }, + "reddit.com": (url) => EmbedHandlers["www.reddit.com"](url), "www.reddit.com": async (url: URL) => { const res = await EmbedHandlers["default"](url); return { @@ -420,12 +426,9 @@ export const EmbedHandlers: { }, }; }, - "youtu.be": (url: URL) => { - return EmbedHandlers["www.youtube.com"](url); - }, - "youtube.com": (url: URL) => { - return EmbedHandlers["www.youtube.com"](url); - }, + + "youtu.be": (url) => EmbedHandlers["www.youtube.com"](url), + "youtube.com": (url) => EmbedHandlers["www.youtube.com"](url), "www.youtube.com": async (url: URL): Promise => { const response = await doFetch(url); if (!response) return null; @@ -466,6 +469,36 @@ export const EmbedHandlers: { }; }, + "www.xkcd.com": (url) => EmbedHandlers["xkcd.com"](url), + "xkcd.com": async (url) => { + const response = await doFetch(url); + if (!response) return null; + + const metas = getMetaDescriptions(await response.text()); + const hoverText = metas.$("#comic img").attr("title"); + + if (!metas.image) return null; + + const { width, height } = await probe(metas.image); + + return { + url: url.href, + type: EmbedType.rich, + title: `xkcd: ${metas.title}`, + image: { + width, + height, + url: metas.image, + proxy_url: getProxyUrl(new URL(metas.image), width, height), + }, + footer: hoverText + ? { + text: hoverText, + } + : undefined, + }; + }, + // the url is an image from this instance self: async (url: URL): Promise => { const result = await probe(url.href); From fb7409947cf022c864fdbb07be6c2ba6cba296c1 Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Sun, 3 Sep 2023 13:41:45 +1000 Subject: [PATCH 13/20] Fix bug in embed handler where getMeta would not return undefined --- src/api/util/utility/EmbedHandlers.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/api/util/utility/EmbedHandlers.ts b/src/api/util/utility/EmbedHandlers.ts index e8f394077..af01a32f1 100644 --- a/src/api/util/utility/EmbedHandlers.ts +++ b/src/api/util/utility/EmbedHandlers.ts @@ -78,7 +78,8 @@ export const getProxyUrl = ( const getMeta = ($: cheerio.CheerioAPI, name: string): string | undefined => { let elem = $(`meta[property="${name}"]`); if (!elem.length) elem = $(`meta[name="${name}"]`); - return elem.attr("content") || elem.text(); + const ret = elem.attr("content") || elem.text(); + return ret.trim().length == 0 ? undefined : ret; }; export const getMetaDescriptions = (text: string) => { @@ -168,7 +169,8 @@ export const EmbedHandlers: { const response = await doFetch(url); if (!response) return null; - const metas = getMetaDescriptions(await response.text()); + const text = await response.text(); + const metas = getMetaDescriptions(text); // TODO: handle video @@ -189,7 +191,7 @@ export const EmbedHandlers: { if (metas.type == "object") embedType = EmbedType.article; // github if (metas.type == "rich") embedType = EmbedType.rich; - if (metas.width < 400) embedType = EmbedType.link; + if (metas.width && metas.width < 400) embedType = EmbedType.link; return { url: url.href, From e64c34adea6ad918511f26c8c22ff76500d51ba3 Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Sun, 3 Sep 2023 14:17:11 +1000 Subject: [PATCH 14/20] Rewrite thumbnail/image generation for embeds --- src/api/util/utility/EmbedHandlers.ts | 148 +++++++++++--------------- 1 file changed, 65 insertions(+), 83 deletions(-) diff --git a/src/api/util/utility/EmbedHandlers.ts b/src/api/util/utility/EmbedHandlers.ts index af01a32f1..b1e6b8665 100644 --- a/src/api/util/utility/EmbedHandlers.ts +++ b/src/api/util/utility/EmbedHandlers.ts @@ -16,7 +16,7 @@ along with this program. If not, see . */ -import { Config, Embed, EmbedType } from "@spacebar/util"; +import { Config, Embed, EmbedImage, EmbedType } from "@spacebar/util"; import * as cheerio from "cheerio"; import crypto from "crypto"; import fetch, { RequestInit } from "node-fetch"; @@ -35,6 +35,20 @@ export const DEFAULT_FETCH_OPTIONS: RequestInit = { method: "GET", }; +const makeEmbedImage = ( + url: string | undefined, + width: number | undefined, + height: number | undefined, +): Required | undefined => { + if (!url || !width || !height) return undefined; + return { + url, + width, + height, + proxy_url: getProxyUrl(new URL(url), width, height), + }; +}; + let hasWarnedAboutImagor = false; export const getProxyUrl = ( @@ -82,6 +96,15 @@ const getMeta = ($: cheerio.CheerioAPI, name: string): string | undefined => { return ret.trim().length == 0 ? undefined : ret; }; +const tryParseInt = (str: string | undefined) => { + if (!str) return undefined; + try { + return parseInt(str); + } catch (e) { + return undefined; + } +}; + export const getMetaDescriptions = (text: string) => { const $ = cheerio.load(text); @@ -94,8 +117,8 @@ export const getMetaDescriptions = (text: string) => { image: getMeta($, "og:image") || getMeta($, "twitter:image"), image_fallback: $(`image`).attr("src"), video_fallback: $(`video`).attr("src"), - width: parseInt(getMeta($, "og:image:width") || "0"), - height: parseInt(getMeta($, "og:image:height") || "0"), + width: tryParseInt(getMeta($, "og:image:width")), + height: tryParseInt(getMeta($, "og:image:height")), url: getMeta($, "og:url"), youtube_embed: getMeta($, "og:video:secure_url"), @@ -120,13 +143,11 @@ const genericImageHandler = async (url: URL): Promise => { method: "HEAD", }); - let width: number, height: number, image: string | undefined; + let image; if (type.headers.get("content-type")?.indexOf("image") !== -1) { const result = await probe(url.href); - width = result.width; - height = result.height; - image = url.href; + image = makeEmbedImage(url.href, result.width, result.height); } else if (type.headers.get("content-type")?.indexOf("video") !== -1) { // TODO return null; @@ -135,22 +156,19 @@ const genericImageHandler = async (url: URL): Promise => { const response = await doFetch(url); if (!response) return null; const metas = getMetaDescriptions(await response.text()); - width = metas.width; - height = metas.height; - image = metas.image || metas.image_fallback; + image = makeEmbedImage( + metas.image || metas.image_fallback, + metas.width, + metas.height, + ); } - if (!width || !height || !image) return null; + if (!image) return null; return { url: url.href, type: EmbedType.image, - thumbnail: { - width: width, - height: height, - url: url.href, - proxy_url: getProxyUrl(new URL(image), width, height), - }, + thumbnail: image, }; }; @@ -176,13 +194,15 @@ export const EmbedHandlers: { if (!metas.image) metas.image = metas.image_fallback; + let image: Required | undefined; + if (metas.image && (!metas.width || !metas.height)) { const result = await probe(metas.image); - metas.width = result.width; - metas.height = result.height; + image = makeEmbedImage(metas.image, result.width, result.height); } - if (!metas.image && (!metas.title || !metas.description)) { + if (!image && (!metas.title || !metas.description)) { + // we don't have any content to display return null; } @@ -191,24 +211,11 @@ export const EmbedHandlers: { if (metas.type == "object") embedType = EmbedType.article; // github if (metas.type == "rich") embedType = EmbedType.rich; - if (metas.width && metas.width < 400) embedType = EmbedType.link; - return { url: url.href, type: embedType, title: metas.title, - thumbnail: { - width: metas.width, - height: metas.height, - url: metas.image, - proxy_url: metas.image - ? getProxyUrl( - new URL(metas.image), - metas.width, - metas.height, - ) - : undefined, - }, + thumbnail: image, description: metas.description, }; }, @@ -340,14 +347,7 @@ export const EmbedHandlers: { type: EmbedType.link, title: metas.title, description: metas.description, - thumbnail: { - width: 640, - height: 640, - proxy_url: metas.image - ? getProxyUrl(new URL(metas.image), 640, 640) - : undefined, - url: metas.image, - }, + thumbnail: makeEmbedImage(metas.image, 640, 640), provider: { url: "https://spotify.com", name: "Spotify", @@ -369,18 +369,11 @@ export const EmbedHandlers: { type: EmbedType.image, title: metas.title, description: metas.description, - image: { - width: metas.width, - height: metas.height, - url: url.href, - proxy_url: metas.image - ? getProxyUrl( - new URL(metas.image), - metas.width, - metas.height, - ) - : undefined, - }, + image: makeEmbedImage( + metas.image || metas.image_fallback, + metas.width, + metas.height, + ), provider: { url: "https://pixiv.net", name: "Pixiv", @@ -437,37 +430,31 @@ export const EmbedHandlers: { const metas = getMetaDescriptions(await response.text()); return { - video: { - // TODO: does this adjust with aspect ratio? - width: metas.width, - height: metas.height, - url: metas.youtube_embed, - }, + video: makeEmbedImage( + metas.youtube_embed, + metas.width, + metas.height, + ), url: url.href, - type: EmbedType.video, + type: metas.youtube_embed ? EmbedType.video : EmbedType.link, title: metas.title, - thumbnail: { - width: metas.width, - height: metas.height, - url: metas.image, - proxy_url: metas.image - ? getProxyUrl( - new URL(metas.image), - metas.width, - metas.height, - ) - : undefined, - }, + thumbnail: makeEmbedImage( + metas.image || metas.image_fallback, + metas.width, + metas.height, + ), provider: { url: "https://www.youtube.com", name: "YouTube", }, description: metas.description, color: 16711680, - author: { - name: metas.author, - // TODO: author channel url - }, + author: metas.author + ? { + name: metas.author, + // TODO: author channel url + } + : undefined, }; }, @@ -487,12 +474,7 @@ export const EmbedHandlers: { url: url.href, type: EmbedType.rich, title: `xkcd: ${metas.title}`, - image: { - width, - height, - url: metas.image, - proxy_url: getProxyUrl(new URL(metas.image), width, height), - }, + image: makeEmbedImage(metas.image, width, height), footer: hoverText ? { text: hoverText, From 1612832fc4f782a29988d2c9a1b928cfc00429e3 Mon Sep 17 00:00:00 2001 From: Puyodead1 Date: Sun, 3 Sep 2023 15:28:33 -0400 Subject: [PATCH 15/20] add facebook, and add steam fields --- assets/schemas.json | Bin 18227560 -> 18223848 bytes src/api/util/utility/EmbedHandlers.ts | 57 ++++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/assets/schemas.json b/assets/schemas.json index 3891cee7668f40df7180c76ce32c0799cd05621a..77df48bdd982b605f534c41710d69acf2e49ec93 100644 GIT binary patch delta 4568 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 J@J`aT{{s<9@T&j- diff --git a/src/api/util/utility/EmbedHandlers.ts b/src/api/util/utility/EmbedHandlers.ts index b1e6b8665..430ca4aa4 100644 --- a/src/api/util/utility/EmbedHandlers.ts +++ b/src/api/util/utility/EmbedHandlers.ts @@ -226,8 +226,23 @@ export const EmbedHandlers: { "c.tenor.com": genericImageHandler, "media.tenor.com": genericImageHandler, - // TODO: facebook - // have to use their APIs or something because they don't send the metas in initial html + "facebook.com": (url) => EmbedHandlers["www.facebook.com"](url), + "www.facebook.com": async (url: URL) => { + const response = await doFetch(url); + if (!response) return null; + const metas = getMetaDescriptions(await response.text()); + + return { + url: url.href, + type: EmbedType.link, + title: metas.title, + description: metas.description, + thumbnail: makeEmbedImage(metas.image, 640, 640), + provider: { + name: "Facebook", + }, + }; + }, "twitter.com": (url) => EmbedHandlers["www.twitter.com"](url), "www.twitter.com": async (url: URL) => { @@ -385,6 +400,42 @@ export const EmbedHandlers: { const response = await doFetch(url); if (!response) return null; const metas = getMetaDescriptions(await response.text()); + const numReviews = metas.$("#review_summary_num_reviews").val() as + | string + | undefined; + const price = metas + .$(".game_purchase_price.price") + .data("price-final") as number | undefined; + const releaseDate = metas + .$(".release_date") + .find("div.date") + .text() + .trim(); + const isReleased = new Date(releaseDate) < new Date(); + + const fields: Embed["fields"] = []; + + if (numReviews) + fields.push({ + name: "Reviews", + value: numReviews, + inline: true, + }); + + if (price) + fields.push({ + name: "Price", + value: `$${price / 100}`, + inline: true, + }); + + // if the release date is in the past, it's already out + if (releaseDate && !isReleased) + fields.push({ + name: "Release Date", + value: releaseDate, + inline: true, + }); return { url: url.href, @@ -405,7 +456,7 @@ export const EmbedHandlers: { url: "https://store.steampowered.com", name: "Steam", }, - // TODO: fields for release date + fields, // TODO: Video }; }, From 8a35664ae630e960bf44f01497100e318231effb Mon Sep 17 00:00:00 2001 From: Puyodead1 Date: Sun, 3 Sep 2023 15:48:55 -0400 Subject: [PATCH 16/20] [fb] remove provider obj, add color --- src/api/util/utility/EmbedHandlers.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/api/util/utility/EmbedHandlers.ts b/src/api/util/utility/EmbedHandlers.ts index 430ca4aa4..ab953613f 100644 --- a/src/api/util/utility/EmbedHandlers.ts +++ b/src/api/util/utility/EmbedHandlers.ts @@ -238,9 +238,7 @@ export const EmbedHandlers: { title: metas.title, description: metas.description, thumbnail: makeEmbedImage(metas.image, 640, 640), - provider: { - name: "Facebook", - }, + color: 16777215, }; }, From add16ab26eb37cace5663980cb3c3042871b7693 Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Tue, 5 Sep 2023 23:29:22 +1000 Subject: [PATCH 17/20] * allow limit=1 * fix GET messages?around order --- .../channels/#channel_id/messages/index.ts | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/api/routes/channels/#channel_id/messages/index.ts b/src/api/routes/channels/#channel_id/messages/index.ts index c384a05bf..a5bfcfd73 100644 --- a/src/api/routes/channels/#channel_id/messages/index.ts +++ b/src/api/routes/channels/#channel_id/messages/index.ts @@ -40,7 +40,13 @@ import { import { Request, Response, Router } from "express"; import { HTTPError } from "lambert-server"; import multer from "multer"; -import { FindManyOptions, FindOperator, LessThan, MoreThan } from "typeorm"; +import { + FindManyOptions, + FindOperator, + LessThan, + MoreThan, + MoreThanOrEqual, +} from "typeorm"; import { URL } from "url"; const router: Router = Router(); @@ -122,12 +128,24 @@ router.get( if (around) { query.take = Math.floor(limit / 2); - const [right, left] = await Promise.all([ - Message.find({ ...query, where: { id: LessThan(around) } }), - Message.find({ ...query, where: { id: MoreThan(around) } }), - ]); - right.push(...left); - messages = right; + if (query.take != 0) { + const [right, left] = await Promise.all([ + Message.find({ ...query, where: { id: LessThan(around) } }), + Message.find({ + ...query, + where: { id: MoreThanOrEqual(around) }, + }), + ]); + left.push(...right); + messages = left; + } else { + query.take = 1; + const message = await Message.findOne({ + ...query, + where: { id: around }, + }); + messages = message ? [message] : []; + } } else { if (after) { if (BigInt(after) > BigInt(Snowflake.generate())) From d55f755d6c8018cb4608f7e4315a11ad5bafc6bf Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Wed, 13 Sep 2023 19:43:54 +1000 Subject: [PATCH 18/20] Add simple about and clients to readme #1098 --- README.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c990c1d16..9e1d94908 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,9 @@ ## [About](https://spacebar.chat) +Spacebar/server is a Discord backend re-implementation and extension. +We aim to reverse engineer and add additional features to the Discord backend, while remaining completely backwards compatible with existing bots, applications, and clients. + This repository contains: - [Spacebar HTTP API Server](/src/api) @@ -23,6 +26,13 @@ This repository contains: - [HTTP CDN Server](/src/cdn) - [Utility and Database Models](/src/util) +## [Documentation](https://docs.spacebar.chat) + ## [Contributing](https://docs.spacebar.chat/contributing/) -## [Setup](https://docs.spacebar.chat/setup/server/) +## Clients + +You *should* be able to use any client designed for Discord.com to connect to a Spacebar instance. +However, some incompatibilities still exist between Spacebar and Discord. For this reason, not every client will connect. +The [WIP official Spacebar client](https://github.com/spacebarchat/client) will always work. +You can find a [live version here](https://app.spacebar.chat). From e355be8ea3f82552ca021a918efbaa86d3a1ba88 Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Sat, 16 Sep 2023 23:43:23 +1000 Subject: [PATCH 19/20] Fix regression where generic embed handler wouldn't properly send image results --- src/api/util/utility/EmbedHandlers.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/api/util/utility/EmbedHandlers.ts b/src/api/util/utility/EmbedHandlers.ts index ab953613f..f39db9c3a 100644 --- a/src/api/util/utility/EmbedHandlers.ts +++ b/src/api/util/utility/EmbedHandlers.ts @@ -194,14 +194,13 @@ export const EmbedHandlers: { if (!metas.image) metas.image = metas.image_fallback; - let image: Required | undefined; - if (metas.image && (!metas.width || !metas.height)) { const result = await probe(metas.image); - image = makeEmbedImage(metas.image, result.width, result.height); + metas.width = result.width; + metas.height = result.height; } - if (!image && (!metas.title || !metas.description)) { + if (!metas.image && (!metas.title || !metas.description)) { // we don't have any content to display return null; } @@ -215,7 +214,7 @@ export const EmbedHandlers: { url: url.href, type: embedType, title: metas.title, - thumbnail: image, + thumbnail: makeEmbedImage(metas.image, metas.width, metas.height), description: metas.description, }; }, From c802bb9747bedcfd1ca4e2483e9fe53d6678d4fd Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Sat, 16 Sep 2023 23:47:32 +1000 Subject: [PATCH 20/20] Embeds: handle og:sitename --- src/api/util/utility/EmbedHandlers.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/api/util/utility/EmbedHandlers.ts b/src/api/util/utility/EmbedHandlers.ts index f39db9c3a..0f1e88a50 100644 --- a/src/api/util/utility/EmbedHandlers.ts +++ b/src/api/util/utility/EmbedHandlers.ts @@ -121,6 +121,7 @@ export const getMetaDescriptions = (text: string) => { height: tryParseInt(getMeta($, "og:image:height")), url: getMeta($, "og:url"), youtube_embed: getMeta($, "og:video:secure_url"), + site_name: getMeta($, "og:site_name"), $, }; @@ -216,6 +217,12 @@ export const EmbedHandlers: { title: metas.title, thumbnail: makeEmbedImage(metas.image, metas.width, metas.height), description: metas.description, + provider: metas.site_name + ? { + name: metas.site_name, + url: url.origin, + } + : undefined, }; },