Prettier u8

This commit is contained in:
Rory& committed 2025-12-17 09:23:55 +01:00
1 parent b843219907
commit 45ee84aca9
13 files changed
+71 -287

No files matched your search

@@ -3,10 +3,7 @@ import { onLazyRequest } from "./LazyRequest";
import { GuildSubscriptionsBulkSchema } from "@spacebar/schemas";
import { check } from "./instanceOf";
export async function onGuildSubscriptionsBulk(
this: WebSocket,
payload: Payload,
) {
export async function onGuildSubscriptionsBulk(this: WebSocket, payload: Payload) {
const startTime = Date.now();
check.call(this, GuildSubscriptionsBulkSchema, payload.d);
const body = payload.d as GuildSubscriptionsBulkSchema;
@@ -22,7 +19,5 @@ export async function onGuildSubscriptionsBulk(
},
});
}
console.log(
`[Gateway] GuildSubscriptionsBulk processed ${Object.keys(body.subscriptions).length} subscriptions for user ${this.user_id} in ${Date.now() - startTime}ms`,
);
console.log(`[Gateway] GuildSubscriptionsBulk processed ${Object.keys(body.subscriptions).length} subscriptions for user ${this.user_id} in ${Date.now() - startTime}ms`);
}
+7 -27
View File
@@ -16,18 +16,11 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {
getDatabase,
getPermission,
GuildMembersChunkEvent,
Member,
Presence,
Session,
} from "@spacebar/util";
import { getDatabase, getPermission, GuildMembersChunkEvent, Member, Presence, Session } from "@spacebar/util";
import { WebSocket, Payload, OPCODES, Send } from "@spacebar/gateway";
import { check } from "./instanceOf";
import { FindManyOptions, ILike, In } from "typeorm";
import { RequestGuildMembersSchema } from "@spacebar/schemas"
import { RequestGuildMembersSchema } from "@spacebar/schemas";
export async function onRequestGuildMembers(this: WebSocket, { d }: Payload) {
const startTime = Date.now();
@@ -39,11 +32,7 @@ export async function onRequestGuildMembers(this: WebSocket, { d }: Payload) {
check.call(this, RequestGuildMembersSchema, d);
const {
presences,
nonce,
query: requestQuery,
} = d as RequestGuildMembersSchema;
const { presences, nonce, query: requestQuery } = d as RequestGuildMembersSchema;
let { limit, user_ids, guild_id } = d as RequestGuildMembersSchema;
// some discord libraries send empty string as query when they meant to send undefined, which was leading to errors being thrown in this handler
@@ -63,8 +52,7 @@ export async function onRequestGuildMembers(this: WebSocket, { d }: Payload) {
}
// TODO: Configurable limit?
if ((query || (user_ids && user_ids.length > 0)) && (!limit || limit > 100))
limit = 100;
if ((query || (user_ids && user_ids.length > 0)) && (!limit || limit > 100)) limit = 100;
const permissions = await getPermission(this.user_id, guild_id);
permissions.hasThrow("VIEW_CHANNEL");
@@ -107,10 +95,7 @@ export async function onRequestGuildMembers(this: WebSocket, { d }: Payload) {
.leftJoinAndSelect("member.roles", "role")
.leftJoinAndSelect("member.user", "user")
.leftJoinAndSelect("user.sessions", "session")
.andWhere(
"',' || member.roles || ',' NOT LIKE :everyoneRoleIdList",
{ everyoneRoleIdList: "%," + guild_id + ",%" },
)
.andWhere("',' || member.roles || ',' NOT LIKE :everyoneRoleIdList", { everyoneRoleIdList: "%," + guild_id + ",%" })
.addOrderBy("user.username", "ASC")
.limit(memberFind.take);
@@ -145,10 +130,7 @@ export async function onRequestGuildMembers(this: WebSocket, { d }: Payload) {
const chunkCount = Math.ceil(members.length / 1000);
let notFound: string[] = [];
if (user_ids && user_ids.length > 0)
notFound = user_ids.filter(
(id) => !members.some((member) => member.id == id),
);
if (user_ids && user_ids.length > 0) notFound = user_ids.filter((id) => !members.some((member) => member.id == id));
const chunks: GuildMembersChunkEvent["data"][] = [];
while (members.length > 0) {
@@ -202,7 +184,5 @@ export async function onRequestGuildMembers(this: WebSocket, { d }: Payload) {
});
});
console.log(
`[Gateway] REQUEST_GUILD_MEMBERS took ${Date.now() - startTime}ms for guild ${guild_id} with ${members.length} members`,
);
console.log(`[Gateway] REQUEST_GUILD_MEMBERS took ${Date.now() - startTime}ms for guild ${guild_id} with ${members.length} members`);
}
+6 -24
View File
@@ -1,9 +1,4 @@
import {
genVoiceToken,
Payload,
WebSocket,
generateStreamKey,
} from "@spacebar/gateway";
import { genVoiceToken, Payload, WebSocket, generateStreamKey } from "@spacebar/gateway";
import {
Channel,
Config,
@@ -18,7 +13,7 @@ import {
VoiceStateUpdateEvent,
} from "@spacebar/util";
import { check } from "./instanceOf";
import { StreamCreateSchema } from "@spacebar/schemas"
import { StreamCreateSchema } from "@spacebar/schemas";
export async function onStreamCreate(this: WebSocket, data: Payload) {
const startTime = Date.now();
@@ -47,17 +42,11 @@ export async function onStreamCreate(this: WebSocket, data: Payload) {
where: { id: body.channel_id },
});
if (
!channel ||
(body.type === "guild" && channel.guild_id != body.guild_id)
)
return this.close(4000, "invalid channel");
if (!channel || (body.type === "guild" && channel.guild_id != body.guild_id)) return this.close(4000, "invalid channel");
// TODO: actually apply preferred_region from the event payload
const regions = Config.get().regions;
const guildRegion = regions.available.filter(
(r) => r.id === regions.default,
)[0];
const guildRegion = regions.available.filter((r) => r.id === regions.default)[0];
// first make sure theres no other streams for this user that somehow didnt get cleared
await Stream.delete({
@@ -85,12 +74,7 @@ export async function onStreamCreate(this: WebSocket, data: Payload) {
await streamSession.save();
const streamKey = generateStreamKey(
body.type,
body.guild_id,
body.channel_id,
this.user_id,
);
const streamKey = generateStreamKey(body.type, body.guild_id, body.channel_id, this.user_id);
await emitEvent({
event: "STREAM_CREATE",
@@ -125,9 +109,7 @@ export async function onStreamCreate(this: WebSocket, data: Payload) {
channel_id: voiceState.channel_id,
} as VoiceStateUpdateEvent);
console.log(
`[Gateway] STREAM_CREATE for user ${this.user_id} in channel ${body.channel_id} with stream key ${streamKey} in ${Date.now() - startTime}ms`,
);
console.log(`[Gateway] STREAM_CREATE for user ${this.user_id} in channel ${body.channel_id} with stream key ${streamKey} in ${Date.now() - startTime}ms`);
}
//stream key:
+3 -11
View File
@@ -1,13 +1,7 @@
import { parseStreamKey, Payload, WebSocket } from "@spacebar/gateway";
import {
emitEvent,
Stream,
StreamDeleteEvent,
VoiceState,
VoiceStateUpdateEvent,
} from "@spacebar/util";
import { emitEvent, Stream, StreamDeleteEvent, VoiceState, VoiceStateUpdateEvent } from "@spacebar/util";
import { check } from "./instanceOf";
import { StreamDeleteSchema } from "@spacebar/schemas"
import { StreamDeleteSchema } from "@spacebar/schemas";
export async function onStreamDelete(this: WebSocket, data: Payload) {
const startTime = Date.now();
@@ -75,7 +69,5 @@ export async function onStreamDelete(this: WebSocket, data: Payload) {
channel_id: channelId,
} as StreamDeleteEvent);
console.log(
`[Gateway] STREAM_DELETE for user ${this.user_id} in channel ${channelId} with stream key ${body.stream_key} in ${Date.now() - startTime}ms`,
);
console.log(`[Gateway] STREAM_DELETE for user ${this.user_id} in channel ${channelId} with stream key ${body.stream_key} in ${Date.now() - startTime}ms`);
}
+6 -23
View File
@@ -1,20 +1,8 @@
import {
genVoiceToken,
parseStreamKey,
Payload,
WebSocket,
} from "@spacebar/gateway";
import {
Config,
emitEvent,
Stream,
StreamCreateEvent,
StreamServerUpdateEvent,
StreamSession,
} from "@spacebar/util";
import { genVoiceToken, parseStreamKey, Payload, WebSocket } from "@spacebar/gateway";
import { Config, emitEvent, Stream, StreamCreateEvent, StreamServerUpdateEvent, StreamSession } from "@spacebar/util";
import { check } from "./instanceOf";
import { Not } from "typeorm";
import { StreamWatchSchema } from "@spacebar/schemas"
import { StreamWatchSchema } from "@spacebar/schemas";
export async function onStreamWatch(this: WebSocket, data: Payload) {
const startTime = Date.now();
@@ -45,13 +33,10 @@ export async function onStreamWatch(this: WebSocket, data: Payload) {
if (!stream) return this.close(4000, "Invalid stream key");
if (type === "guild" && stream.channel.guild_id != guildId)
return this.close(4000, "Invalid stream key");
if (type === "guild" && stream.channel.guild_id != guildId) return this.close(4000, "Invalid stream key");
const regions = Config.get().regions;
const guildRegion = regions.available.find(
(r) => r.endpoint === stream.endpoint,
);
const guildRegion = regions.available.find((r) => r.endpoint === stream.endpoint);
if (!guildRegion) return this.close(4000, "Unknown region");
@@ -97,7 +82,5 @@ export async function onStreamWatch(this: WebSocket, data: Payload) {
user_id: this.user_id,
} as StreamServerUpdateEvent);
console.log(
`[Gateway] STREAM_WATCH for user ${this.user_id} in channel ${channelId} with stream key ${body.stream_key} in ${Date.now() - startTime}ms`,
);
console.log(`[Gateway] STREAM_WATCH for user ${this.user_id} in channel ${channelId} with stream key ${body.stream_key} in ${Date.now() - startTime}ms`);
}
+10 -40
View File
@@ -17,19 +17,10 @@
*/
import { Payload, WebSocket } from "@spacebar/gateway";
import {
Config,
emitEvent,
Guild,
Member,
Region,
VoiceServerUpdateEvent,
VoiceState,
VoiceStateUpdateEvent,
} from "@spacebar/util";
import { Config, emitEvent, Guild, Member, Region, VoiceServerUpdateEvent, VoiceState, VoiceStateUpdateEvent } from "@spacebar/util";
import { genVoiceToken } from "../util/SessionUtils";
import { check } from "./instanceOf";
import { VoiceStateUpdateSchema } from "@spacebar/schemas"
import { VoiceStateUpdateSchema } from "@spacebar/schemas";
// TODO: check if a voice server is setup
// Notice: Bot users respect the voice channel's user limit, if set.
@@ -50,10 +41,7 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
voiceState = await VoiceState.findOneOrFail({
where: { user_id: this.user_id },
});
if (
voiceState.session_id !== this.session_id &&
body.channel_id === null
) {
if (voiceState.session_id !== this.session_id && body.channel_id === null) {
//Should we also check guild_id === null?
//changing deaf or mute on a client that's not the one with the same session of the voicestate in the database should be ignored
return;
@@ -62,11 +50,7 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
if (voiceState.channel_id !== body.channel_id) isChanged = true;
//If a user change voice channel between guild we should send a left event first
if (
voiceState.guild_id &&
voiceState.guild_id !== body.guild_id &&
voiceState.session_id === this.session_id
) {
if (voiceState.guild_id && voiceState.guild_id !== body.guild_id && voiceState.session_id === this.session_id) {
await emitEvent({
event: "VOICE_STATE_UPDATE",
data: { ...voiceState.toPublicVoiceState(), channel_id: null },
@@ -89,12 +73,7 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
}
// if user left voice channel, send an update to previous channel/guild to let other people know that the user left
if (
voiceState.session_id === this.session_id &&
body.guild_id == null &&
body.channel_id == null &&
(prevState?.guild_id || prevState?.channel_id)
) {
if (voiceState.session_id === this.session_id && body.guild_id == null && body.channel_id == null && (prevState?.guild_id || prevState?.channel_id)) {
await emitEvent({
event: "VOICE_STATE_UPDATE",
data: {
@@ -118,8 +97,7 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
}
//If the session changed we generate a new token
if (voiceState.session_id !== this.session_id)
voiceState.token = genVoiceToken();
if (voiceState.session_id !== this.session_id) voiceState.token = genVoiceToken();
voiceState.session_id = this.session_id;
const { member } = voiceState;
@@ -146,13 +124,9 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
const regions = Config.get().regions;
let guildRegion: Region;
if (guild && guild.region) {
guildRegion = regions.available.filter(
(r) => r.id === guild.region,
)[0];
guildRegion = regions.available.filter((r) => r.id === guild.region)[0];
} else {
guildRegion = regions.available.filter(
(r) => r.id === regions.default,
)[0];
guildRegion = regions.available.filter((r) => r.id === regions.default)[0];
}
await emitEvent({
@@ -161,15 +135,11 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
token: voiceState.token,
guild_id: voiceState.guild_id,
endpoint: guildRegion.endpoint,
channel_id: voiceState.guild_id
? undefined
: voiceState.channel_id, // only DM voice calls have this set, and DM channel is one where guild_id is null
channel_id: voiceState.guild_id ? undefined : voiceState.channel_id, // only DM voice calls have this set, and DM channel is one where guild_id is null
},
user_id: voiceState.user_id,
} as VoiceServerUpdateEvent);
}
console.log(
`[Gateway] VOICE_STATE_UPDATE for user ${this.user_id} in channel ${voiceState.channel_id} in guild ${voiceState.guild_id} in ${Date.now() - startTime}ms`,
);
console.log(`[Gateway] VOICE_STATE_UPDATE for user ${this.user_id} in channel ${voiceState.channel_id} in guild ${voiceState.guild_id} in ${Date.now() - startTime}ms`);
}