From 21bfda32e452c05b8906bf318df7415d6cd5acd0 Mon Sep 17 00:00:00 2001 From: Puyodead1 Date: Thu, 22 Dec 2022 10:05:51 -0500 Subject: add connections --- src/util/schemas/index.ts | 1 + 1 file changed, 1 insertion(+) (limited to 'src/util/schemas/index.ts') diff --git a/src/util/schemas/index.ts b/src/util/schemas/index.ts index 44909a3a..2831d42a 100644 --- a/src/util/schemas/index.ts +++ b/src/util/schemas/index.ts @@ -30,6 +30,7 @@ export * from "./ChannelModifySchema"; export * from "./ChannelPermissionOverwriteSchema"; export * from "./ChannelReorderSchema"; export * from "./CodesVerificationSchema"; +export * from "./ConnectionCallbackSchema"; export * from "./DmChannelCreateSchema"; export * from "./EmojiCreateSchema"; export * from "./EmojiModifySchema"; -- cgit 1.5.1 From 6a52e65e27d514273a4210dadafbee9692f23354 Mon Sep 17 00:00:00 2001 From: Puyodead1 Date: Thu, 22 Dec 2022 10:47:32 -0500 Subject: adding connection now works --- src/api/Server.ts | 5 +++++ .../connections/#connection_name/authorize.ts | 2 +- .../connections/#connection_name/callback.ts | 2 +- src/connections/BattleNet/index.ts | 7 +------ src/connections/GitHub/index.ts | 7 +------ src/util/connections/Connection.ts | 4 ++-- src/util/connections/ConnectionConfig.ts | 1 - src/util/connections/ConnectionLoader.ts | 7 ++++--- src/util/dtos/ConnectedAccountDTO.ts | 18 ++++++++-------- src/util/entities/ConnectedAccount.ts | 24 +++++++++++----------- src/util/index.ts | 1 + src/util/schemas/ConnectedAccountSchema.ts | 16 +++++++++++++++ src/util/schemas/index.ts | 1 + 13 files changed, 54 insertions(+), 41 deletions(-) create mode 100644 src/util/schemas/ConnectedAccountSchema.ts (limited to 'src/util/schemas/index.ts') diff --git a/src/api/Server.ts b/src/api/Server.ts index 49229494..dc3b66ef 100644 --- a/src/api/Server.ts +++ b/src/api/Server.ts @@ -25,6 +25,8 @@ import { registerRoutes, Sentry, WebAuthn, + ConnectionConfig, + ConnectionLoader } from "@fosscord/util"; import { Request, Response, Router } from "express"; import { Server, ServerOptions } from "lambert-server"; @@ -64,6 +66,7 @@ export class FosscordServer extends Server { await Config.init(); await initEvent(); await Email.init(); + await ConnectionConfig.init(); await initInstance(); await Sentry.init(this.app); WebAuthn.init(); @@ -130,6 +133,8 @@ export class FosscordServer extends Server { Sentry.errorHandler(this.app); + ConnectionLoader.loadConnections(); + if (logRequests) console.log( red( diff --git a/src/api/routes/connections/#connection_name/authorize.ts b/src/api/routes/connections/#connection_name/authorize.ts index 8e640a69..5ce420cf 100644 --- a/src/api/routes/connections/#connection_name/authorize.ts +++ b/src/api/routes/connections/#connection_name/authorize.ts @@ -6,7 +6,7 @@ import { route } from "../../../util"; const router = Router(); router.get("/", route({}), async (req: Request, res: Response) => { - const { connection_id: connection_name } = req.params; + const { connection_name } = req.params; const connection = ConnectionStore.connections.get(connection_name); if (!connection) throw FieldErrors({ diff --git a/src/api/routes/connections/#connection_name/callback.ts b/src/api/routes/connections/#connection_name/callback.ts index f158a037..80a5b784 100644 --- a/src/api/routes/connections/#connection_name/callback.ts +++ b/src/api/routes/connections/#connection_name/callback.ts @@ -13,7 +13,7 @@ router.post( "/", route({ body: "ConnectionCallbackSchema" }), async (req: Request, res: Response) => { - const { connection_id: connection_name } = req.params; + const { connection_name } = req.params; const connection = ConnectionStore.connections.get(connection_name); if (!connection) throw FieldErrors({ diff --git a/src/connections/BattleNet/index.ts b/src/connections/BattleNet/index.ts index 0fd0aa18..1b725afd 100644 --- a/src/connections/BattleNet/index.ts +++ b/src/connections/BattleNet/index.ts @@ -118,15 +118,10 @@ export default class BattleNetConnection extends Connection { if (exists) return false; await this.createConnection({ user_id: userId, - external_id: userInfo.id, + external_id: userInfo.id.toString(), friend_sync: params.friend_sync, name: userInfo.battletag, - revoked: false, - show_activity: false, type: this.id, - verified: true, - visibility: 0, - integrations: [], }); return true; } diff --git a/src/connections/GitHub/index.ts b/src/connections/GitHub/index.ts index a96ac68e..41806a67 100644 --- a/src/connections/GitHub/index.ts +++ b/src/connections/GitHub/index.ts @@ -99,15 +99,10 @@ export default class GitHubConnection extends Connection { if (exists) return false; await this.createConnection({ user_id: userId, - external_id: userInfo.id, + external_id: userInfo.id.toString(), friend_sync: params.friend_sync, name: userInfo.name, - revoked: false, - show_activity: false, type: this.id, - verified: true, - visibility: 0, - integrations: [], }); return true; } diff --git a/src/util/connections/Connection.ts b/src/util/connections/Connection.ts index 02104d39..e8d41c36 100644 --- a/src/util/connections/Connection.ts +++ b/src/util/connections/Connection.ts @@ -1,7 +1,7 @@ import crypto from "crypto"; import { ConnectedAccount } from "../entities"; import { OrmUtils } from "../imports"; -import { ConnectionCallbackSchema } from "../schemas"; +import { ConnectedAccountSchema, ConnectionCallbackSchema } from "../schemas"; import { DiscordApiErrors } from "../util"; export default abstract class Connection { @@ -54,7 +54,7 @@ export default abstract class Connection { this.states.delete(state); } - async createConnection(data: any): Promise { + async createConnection(data: ConnectedAccountSchema): Promise { const ca = OrmUtils.mergeDeep(new ConnectedAccount(), data); await ca.save(); } diff --git a/src/util/connections/ConnectionConfig.ts b/src/util/connections/ConnectionConfig.ts index 9b120c93..dd372ff7 100644 --- a/src/util/connections/ConnectionConfig.ts +++ b/src/util/connections/ConnectionConfig.ts @@ -21,7 +21,6 @@ export const ConnectionConfig = { set: function set(val: Partial) { if (!config || !val) return; config = val.merge(config); - console.debug("config", config); // TODO: if no more issues with sql, remove this or find the reason why it's happening return applyConfig(config); }, diff --git a/src/util/connections/ConnectionLoader.ts b/src/util/connections/ConnectionLoader.ts index d06a6446..7467739c 100644 --- a/src/util/connections/ConnectionLoader.ts +++ b/src/util/connections/ConnectionLoader.ts @@ -23,7 +23,6 @@ export class ConnectionLoader { dirs.forEach(async (x) => { let modPath = path.resolve(path.join(root, x)); - console.log(`Loading connection: ${modPath}`); const mod = new (require(modPath).default)() as Connection; ConnectionStore.connections.set(mod.id, mod); @@ -55,11 +54,13 @@ export class ConnectionLoader { console.log( `[ConnectionConfig/WARN] ${id} tried to set config=null!`, ); - await ConnectionConfig.set({ + + const a = { [id]: OrmUtils.mergeDeep( ConnectionLoader.getConnectionConfig(id) || {}, config, ), - }); + }; + await ConnectionConfig.set(a); } } diff --git a/src/util/dtos/ConnectedAccountDTO.ts b/src/util/dtos/ConnectedAccountDTO.ts index a0287086..36a4e6b3 100644 --- a/src/util/dtos/ConnectedAccountDTO.ts +++ b/src/util/dtos/ConnectedAccountDTO.ts @@ -4,17 +4,17 @@ export class ConnectedAccountDTO { id: string; user_id: string; access_token?: string; - friend_sync: boolean; + friend_sync?: boolean; name: string; - revoked: boolean; - show_activity: boolean; + revoked?: boolean; + show_activity?: boolean; type: string; - verified: boolean; - visibility: boolean; - integrations: string[]; - metadata_: any; - metadata_visibility: boolean; - two_way_link: boolean; + verified?: boolean; + visibility?: number; + integrations?: string[]; + metadata_?: any; + metadata_visibility?: number; + two_way_link?: boolean; constructor( connectedAccount: ConnectedAccount, diff --git a/src/util/entities/ConnectedAccount.ts b/src/util/entities/ConnectedAccount.ts index 70923d2c..25d5a0c7 100644 --- a/src/util/entities/ConnectedAccount.ts +++ b/src/util/entities/ConnectedAccount.ts @@ -40,39 +40,39 @@ export class ConnectedAccount extends BaseClass { }) user: User; - @Column({ select: false }) - access_token: string; + @Column({ select: false, nullable: true }) + access_token?: string; @Column({ select: false }) - friend_sync: boolean = false; + friend_sync?: boolean = false; @Column() name: string; @Column({ select: false }) - revoked: boolean = false; + revoked?: boolean = false; @Column({ select: false }) - show_activity: boolean = true; + show_activity?: boolean = true; @Column() type: string; @Column() - verified: boolean; + verified?: boolean = true; @Column({ select: false }) - visibility: boolean = true; + visibility?: number = 0; @Column({ type: "simple-array" }) - integrations: string[]; + integrations?: string[] = []; - @Column({ type: "simple-json", name: "metadata" }) - metadata_: any; + @Column({ type: "simple-json", name: "metadata", nullable: true }) + metadata_?: any; @Column() - metadata_visibility: boolean = true; + metadata_visibility?: number = 0; @Column() - two_way_link: boolean = false; + two_way_link?: boolean = false; } diff --git a/src/util/index.ts b/src/util/index.ts index a3495a0c..cb180ee8 100644 --- a/src/util/index.ts +++ b/src/util/index.ts @@ -25,3 +25,4 @@ export * from "./dtos/index"; export * from "./schemas"; export * from "./imports"; export * from "./config"; +export * from "./connections" \ No newline at end of file diff --git a/src/util/schemas/ConnectedAccountSchema.ts b/src/util/schemas/ConnectedAccountSchema.ts new file mode 100644 index 00000000..e00e4fa1 --- /dev/null +++ b/src/util/schemas/ConnectedAccountSchema.ts @@ -0,0 +1,16 @@ +export interface ConnectedAccountSchema { + external_id: string; + user_id: string; + access_token?: string; + friend_sync?: boolean; + name: string; + revoked?: boolean; + show_activity?: boolean; + type: string; + verified?: boolean; + visibility?: number; + integrations?: string[]; + metadata_?: any; + metadata_visibility?: number; + two_way_link?: boolean; +} diff --git a/src/util/schemas/index.ts b/src/util/schemas/index.ts index 2831d42a..71b4c2ce 100644 --- a/src/util/schemas/index.ts +++ b/src/util/schemas/index.ts @@ -30,6 +30,7 @@ export * from "./ChannelModifySchema"; export * from "./ChannelPermissionOverwriteSchema"; export * from "./ChannelReorderSchema"; export * from "./CodesVerificationSchema"; +export * from "./ConnectedAccountSchema"; export * from "./ConnectionCallbackSchema"; export * from "./DmChannelCreateSchema"; export * from "./EmojiCreateSchema"; -- cgit 1.5.1 From 2364096a97ba66949cc3b7fbc5b9fb68977ff3ca Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Fri, 23 Dec 2022 12:46:03 +1100 Subject: entities/ConnectionUpdateSchema -> schemas/ConnectionUpdateSchema --- assets/schemas.json | 597 ++++++++++++++++++++- .../connections/#connection_name/authorize.ts | 5 +- src/util/entities/ConnectionUpdateSchema.ts | 3 - src/util/entities/index.ts | 1 - src/util/schemas/ConnectionUpdateSchema.ts | 3 + src/util/schemas/index.ts | 1 + 6 files changed, 593 insertions(+), 17 deletions(-) delete mode 100644 src/util/entities/ConnectionUpdateSchema.ts create mode 100644 src/util/schemas/ConnectionUpdateSchema.ts (limited to 'src/util/schemas/index.ts') diff --git a/assets/schemas.json b/assets/schemas.json index 881560dd..fe4fcea3 100644 --- a/assets/schemas.json +++ b/assets/schemas.json @@ -36,16 +36,6 @@ ], "$schema": "http://json-schema.org/draft-07/schema#" }, - "ConnectionUpdateSchema": { - "type": "object", - "properties": { - "visibility": { - "type": "boolean" - } - }, - "additionalProperties": false, - "$schema": "http://json-schema.org/draft-07/schema#" - }, "SMTPConnection.CustomAuthenticationResponse": { "type": "object", "properties": { @@ -4035,6 +4025,593 @@ }, "$schema": "http://json-schema.org/draft-07/schema#" }, + "ConnectionUpdateSchema": { + "type": "object", + "properties": { + "visibility": { + "type": "boolean" + } + }, + "additionalProperties": false, + "definitions": { + "ChannelPermissionOverwriteType": { + "enum": [ + 0, + 1, + 2 + ], + "type": "number" + }, + "ChannelModifySchema": { + "type": "object", + "properties": { + "name": { + "maxLength": 100, + "type": "string" + }, + "type": { + "enum": [ + 0, + 1, + 10, + 11, + 12, + 13, + 14, + 15, + 2, + 255, + 3, + 33, + 34, + 35, + 4, + 5, + 6, + 64, + 7, + 8, + 9 + ], + "type": "number" + }, + "topic": { + "type": "string" + }, + "icon": { + "type": [ + "null", + "string" + ] + }, + "bitrate": { + "type": "integer" + }, + "user_limit": { + "type": "integer" + }, + "rate_limit_per_user": { + "type": "integer" + }, + "position": { + "type": "integer" + }, + "permission_overwrites": { + "type": "array", + "items": { + "type": "object", + "properties": { + "id": { + "type": "string" + }, + "type": { + "$ref": "#/definitions/ChannelPermissionOverwriteType" + }, + "allow": { + "type": "string" + }, + "deny": { + "type": "string" + } + }, + "additionalProperties": false, + "required": [ + "allow", + "deny", + "id", + "type" + ] + } + }, + "parent_id": { + "type": "string" + }, + "id": { + "type": "string" + }, + "nsfw": { + "type": "boolean" + }, + "rtc_region": { + "type": "string" + }, + "default_auto_archive_duration": { + "type": "integer" + }, + "default_reaction_emoji": { + "type": [ + "null", + "string" + ] + }, + "flags": { + "type": "integer" + }, + "default_thread_rate_limit_per_user": { + "type": "integer" + }, + "video_quality_mode": { + "type": "integer" + } + }, + "additionalProperties": false + }, + "ActivitySchema": { + "type": "object", + "properties": { + "afk": { + "type": "boolean" + }, + "status": { + "$ref": "#/definitions/Status" + }, + "activities": { + "type": "array", + "items": { + "$ref": "#/definitions/Activity" + } + }, + "since": { + "type": "integer" + } + }, + "additionalProperties": false, + "required": [ + "status" + ] + }, + "Status": { + "enum": [ + "dnd", + "idle", + "invisible", + "offline", + "online" + ], + "type": "string" + }, + "Activity": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "type": { + "$ref": "#/definitions/ActivityType" + }, + "url": { + "type": "string" + }, + "created_at": { + "type": "integer" + }, + "timestamps": { + "type": "object", + "properties": { + "start": { + "type": "integer" + }, + "end": { + "type": "integer" + } + }, + "additionalProperties": false, + "required": [ + "end", + "start" + ] + }, + "application_id": { + "type": "string" + }, + "details": { + "type": "string" + }, + "state": { + "type": "string" + }, + "emoji": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "id": { + "type": "string" + }, + "animated": { + "type": "boolean" + } + }, + "additionalProperties": false, + "required": [ + "animated", + "name" + ] + }, + "party": { + "type": "object", + "properties": { + "id": { + "type": "string" + }, + "size": { + "type": "array", + "items": [ + { + "type": "integer" + } + ], + "minItems": 1, + "maxItems": 1 + } + }, + "additionalProperties": false + }, + "assets": { + "type": "object", + "properties": { + "large_image": { + "type": "string" + }, + "large_text": { + "type": "string" + }, + "small_image": { + "type": "string" + }, + "small_text": { + "type": "string" + } + }, + "additionalProperties": false + }, + "secrets": { + "type": "object", + "properties": { + "join": { + "type": "string" + }, + "spectate": { + "type": "string" + }, + "match": { + "type": "string" + } + }, + "additionalProperties": false + }, + "instance": { + "type": "boolean" + }, + "flags": { + "type": "string" + }, + "id": { + "type": "string" + }, + "sync_id": { + "type": "string" + }, + "metadata": { + "type": "object", + "properties": { + "context_uri": { + "type": "string" + }, + "album_id": { + "type": "string" + }, + "artist_ids": { + "type": "array", + "items": { + "type": "string" + } + } + }, + "additionalProperties": false, + "required": [ + "album_id", + "artist_ids" + ] + }, + "session_id": { + "type": "string" + } + }, + "additionalProperties": false, + "required": [ + "flags", + "name", + "session_id", + "type" + ] + }, + "ActivityType": { + "enum": [ + 0, + 1, + 2, + 4, + 5 + ], + "type": "number" + }, + "Record": { + "type": "object", + "additionalProperties": false + }, + "Embed": { + "type": "object", + "properties": { + "title": { + "type": "string" + }, + "type": { + "enum": [ + "article", + "gifv", + "image", + "link", + "rich", + "video" + ], + "type": "string" + }, + "description": { + "type": "string" + }, + "url": { + "type": "string" + }, + "timestamp": { + "type": "string", + "format": "date-time" + }, + "color": { + "type": "integer" + }, + "footer": { + "type": "object", + "properties": { + "text": { + "type": "string" + }, + "icon_url": { + "type": "string" + }, + "proxy_icon_url": { + "type": "string" + } + }, + "additionalProperties": false, + "required": [ + "text" + ] + }, + "image": { + "$ref": "#/definitions/EmbedImage" + }, + "thumbnail": { + "$ref": "#/definitions/EmbedImage" + }, + "video": { + "$ref": "#/definitions/EmbedImage" + }, + "provider": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "url": { + "type": "string" + } + }, + "additionalProperties": false + }, + "author": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "url": { + "type": "string" + }, + "icon_url": { + "type": "string" + }, + "proxy_icon_url": { + "type": "string" + } + }, + "additionalProperties": false + }, + "fields": { + "type": "array", + "items": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "value": { + "type": "string" + }, + "inline": { + "type": "boolean" + } + }, + "additionalProperties": false, + "required": [ + "name", + "value" + ] + } + } + }, + "additionalProperties": false + }, + "EmbedImage": { + "type": "object", + "properties": { + "url": { + "type": "string" + }, + "proxy_url": { + "type": "string" + }, + "height": { + "type": "integer" + }, + "width": { + "type": "integer" + } + }, + "additionalProperties": false + }, + "Partial": { + "type": "object", + "properties": { + "message_notifications": { + "type": "integer" + }, + "mute_config": { + "$ref": "#/definitions/MuteConfig" + }, + "muted": { + "type": "boolean" + }, + "channel_id": { + "type": [ + "null", + "string" + ] + } + }, + "additionalProperties": false + }, + "MuteConfig": { + "type": "object", + "properties": { + "end_time": { + "type": "integer" + }, + "selected_time_window": { + "type": "integer" + } + }, + "additionalProperties": false, + "required": [ + "end_time", + "selected_time_window" + ] + }, + "CustomStatus": { + "type": "object", + "properties": { + "emoji_id": { + "type": "string" + }, + "emoji_name": { + "type": "string" + }, + "expires_at": { + "type": "integer" + }, + "text": { + "type": "string" + } + }, + "additionalProperties": false + }, + "FriendSourceFlags": { + "type": "object", + "properties": { + "all": { + "type": "boolean" + } + }, + "additionalProperties": false, + "required": [ + "all" + ] + }, + "GuildFolder": { + "type": "object", + "properties": { + "color": { + "type": "integer" + }, + "guild_ids": { + "type": "array", + "items": { + "type": "string" + } + }, + "id": { + "type": "integer" + }, + "name": { + "type": "string" + } + }, + "additionalProperties": false, + "required": [ + "color", + "guild_ids", + "id", + "name" + ] + }, + "Partial": { + "type": "object", + "properties": { + "password": { + "type": "string" + } + }, + "additionalProperties": false + }, + "Partial": { + "type": "object", + "properties": { + "credential": { + "type": "string" + }, + "name": { + "type": "string" + }, + "ticket": { + "type": "string" + } + }, + "additionalProperties": false + } + }, + "$schema": "http://json-schema.org/draft-07/schema#" + }, "DmChannelCreateSchema": { "type": "object", "properties": { diff --git a/src/api/routes/connections/#connection_name/authorize.ts b/src/api/routes/connections/#connection_name/authorize.ts index 5ce420cf..5c24dc7c 100644 --- a/src/api/routes/connections/#connection_name/authorize.ts +++ b/src/api/routes/connections/#connection_name/authorize.ts @@ -1,7 +1,6 @@ +import { route } from "@fosscord/api"; import { Request, Response, Router } from "express"; -import { FieldErrors } from "../../../../util"; -import { ConnectionStore } from "../../../../util/connections"; -import { route } from "../../../util"; +import { ConnectionStore, FieldErrors } from "../../../../util"; const router = Router(); diff --git a/src/util/entities/ConnectionUpdateSchema.ts b/src/util/entities/ConnectionUpdateSchema.ts deleted file mode 100644 index ac234e7e..00000000 --- a/src/util/entities/ConnectionUpdateSchema.ts +++ /dev/null @@ -1,3 +0,0 @@ -export interface ConnectionUpdateSchema { - visibility?: boolean; -} diff --git a/src/util/entities/index.ts b/src/util/entities/index.ts index b4e3ecd0..ad34f67b 100644 --- a/src/util/entities/index.ts +++ b/src/util/entities/index.ts @@ -28,7 +28,6 @@ export * from "./ClientRelease"; export * from "./Config"; export * from "./ConnectedAccount"; export * from "./ConnectionConfigEntity"; -export * from "./ConnectionUpdateSchema"; export * from "./EmbedCache"; export * from "./Emoji"; export * from "./Encryption"; diff --git a/src/util/schemas/ConnectionUpdateSchema.ts b/src/util/schemas/ConnectionUpdateSchema.ts new file mode 100644 index 00000000..ac234e7e --- /dev/null +++ b/src/util/schemas/ConnectionUpdateSchema.ts @@ -0,0 +1,3 @@ +export interface ConnectionUpdateSchema { + visibility?: boolean; +} diff --git a/src/util/schemas/index.ts b/src/util/schemas/index.ts index 71b4c2ce..e9f4268e 100644 --- a/src/util/schemas/index.ts +++ b/src/util/schemas/index.ts @@ -32,6 +32,7 @@ export * from "./ChannelReorderSchema"; export * from "./CodesVerificationSchema"; export * from "./ConnectedAccountSchema"; export * from "./ConnectionCallbackSchema"; +export * from "./ConnectionUpdateSchema"; export * from "./DmChannelCreateSchema"; export * from "./EmojiCreateSchema"; export * from "./EmojiModifySchema"; -- cgit 1.5.1