diff --git a/gateway/src/opcodes/Heartbeat.ts b/src/gateway/opcodes/Heartbeat.ts
index 50394130..42b72d4b 100644
--- a/gateway/src/opcodes/Heartbeat.ts
+++ b/src/gateway/opcodes/Heartbeat.ts
@@ -2,7 +2,7 @@ import { Payload, WebSocket } from "@fosscord/gateway";
import { setHeartbeat } from "../util/Heartbeat";
import { Send } from "../util/Send";
-export async function onHeartbeat(this: WebSocket, data: Payload) {
+export async function onHeartbeat(this: WebSocket, _data: Payload) {
// TODO: validate payload
setHeartbeat(this);
diff --git a/gateway/src/opcodes/Identify.ts b/src/gateway/opcodes/Identify.ts
index 860000da..44db598c 100644
--- a/gateway/src/opcodes/Identify.ts
+++ b/src/gateway/opcodes/Identify.ts
@@ -18,16 +18,18 @@ import {
PrivateSessionProjection,
MemberPrivateProjection,
PresenceUpdateEvent,
+ UserSettings,
+ IdentifySchema,
} from "@fosscord/util";
import { Send } from "../util/Send";
import { CLOSECODES, OPCODES } from "../util/Constants";
import { genSessionId } from "../util/SessionUtils";
import { setupListener } from "../listener/listener";
-import { IdentifySchema } from "../schema/Identify";
// import experiments from "./experiments.json";
const experiments: any = [];
import { check } from "./instanceOf";
import { Recipient } from "@fosscord/util";
+import { OrmUtils } from "@fosscord/util";
// TODO: user sharding
// TODO: check privileged intents, if defined in the config
@@ -50,15 +52,15 @@ export async function onIdentify(this: WebSocket, data: Payload) {
const session_id = genSessionId();
this.session_id = session_id; //Set the session of the WebSocket object
-
+
const [user, read_states, members, recipients, session, application] =
await Promise.all([
User.findOneOrFail({
where: { id: this.user_id },
- relations: ["relationships", "relationships.to"],
+ relations: ["relationships", "relationships.to", "settings"],
select: [...PrivateUserProjection, "relationships"],
}),
- ReadState.find({ user_id: this.user_id }),
+ ReadState.find({ where: { user_id: this.user_id } }),
Member.find({
where: { id: this.user_id },
select: MemberPrivateProjection,
@@ -83,7 +85,7 @@ export async function onIdentify(this: WebSocket, data: Payload) {
// TODO: public user selection
}),
// save the session and delete it when the websocket is closed
- new Session({
+ await OrmUtils.mergeDeep(new Session(), {
user_id: this.user_id,
session_id: session_id,
// TODO: check if status is only one of: online, dnd, offline, idle
@@ -96,12 +98,17 @@ export async function onIdentify(this: WebSocket, data: Payload) {
},
activities: [],
}).save(),
- Application.findOne({ id: this.user_id }),
+ Application.findOne({ where: { id: this.user_id } }),
]);
if (!user) return this.close(CLOSECODES.Authentication_failed);
+ if (!user.settings) { //settings may not exist after updating...
+ user.settings = new UserSettings();
+ user.settings.id = user.id;
+ //await (user.settings as UserSettings).save();
+ }
- if (!identify.intents) identify.intents = BigInt("0x6ffffffff");
+ if (!identify.intents) identify.intents = "30064771071";
this.intents = new Intents(identify.intents);
if (identify.shard) {
this.shard_id = identify.shard[0];
@@ -117,7 +124,7 @@ export async function onIdentify(this: WebSocket, data: Payload) {
return this.close(CLOSECODES.Invalid_shard);
}
}
- var users: PublicUser[] = [];
+ let users: PublicUser[] = [];
const merged_members = members.map((x: Member) => {
return [
@@ -231,7 +238,7 @@ export async function onIdentify(this: WebSocket, data: Payload) {
const d: ReadyEventData = {
v: 8,
- application,
+ application: {id: application?.id??'', flags: application?.flags??0}, //TODO: check this code!
user: privateUser,
user_settings: user.settings,
// @ts-ignore
diff --git a/gateway/src/opcodes/LazyRequest.ts b/src/gateway/opcodes/LazyRequest.ts
index 7503ee61..74996f5b 100644
--- a/gateway/src/opcodes/LazyRequest.ts
+++ b/src/gateway/opcodes/LazyRequest.ts
@@ -1,12 +1,9 @@
-import { getPermission, listenEvent, Member, Role } from "@fosscord/util";
-import { LazyRequest } from "../schema/LazyRequest";
+import { getPermission, listenEvent, Member, Role, getOrInitialiseDatabase, LazyRequest } from "@fosscord/util";
import { Send } from "../util/Send";
import { OPCODES } from "../util/Constants";
import { WebSocket, Payload, handlePresenceUpdate } from "@fosscord/gateway";
import { check } from "./instanceOf";
-import "missing-native-js-functions";
import { getRepository } from "typeorm";
-import "missing-native-js-functions";
// TODO: only show roles/members that have access to this channel
// TODO: config: to list all members (even those who are offline) sorted by role, or just those who are online
@@ -17,8 +14,9 @@ async function getMembers(guild_id: string, range: [number, number]) {
throw new Error("range is not a valid array");
}
// TODO: wait for typeorm to implement ordering for .find queries https://github.com/typeorm/typeorm/issues/2620
+ // TODO: rewrite this, released in 0.3.0
- let members = await getRepository(Member)
+ let members: Member[] = await (await getOrInitialiseDatabase()).getRepository(Member)
.createQueryBuilder("member")
.where("member.guild_id = :guild_id", { guild_id })
.leftJoinAndSelect("member.roles", "role")
@@ -122,7 +120,7 @@ export async function onLazyRequest(this: WebSocket, { d }: Payload) {
const ranges = channels![channel_id];
if (!Array.isArray(ranges)) throw new Error("Not a valid Array");
- const member_count = await Member.count({ guild_id });
+ const member_count = await Member.count({ where: { guild_id } });
const ops = await Promise.all(ranges.map((x) => getMembers(guild_id, x)));
// TODO: unsubscribe member_events that are not in op.members
diff --git a/gateway/src/opcodes/PresenceUpdate.ts b/src/gateway/opcodes/PresenceUpdate.ts
index 415df6ee..f31c9161 100644
--- a/gateway/src/opcodes/PresenceUpdate.ts
+++ b/src/gateway/opcodes/PresenceUpdate.ts
@@ -1,6 +1,5 @@
import { WebSocket, Payload } from "@fosscord/gateway";
-import { emitEvent, PresenceUpdateEvent, Session, User } from "@fosscord/util";
-import { ActivitySchema } from "../schema/Activity";
+import { ActivitySchema, emitEvent, PresenceUpdateEvent, Session, User } from "@fosscord/util";
import { check } from "./instanceOf";
export async function onPresenceUpdate(this: WebSocket, { d }: Payload) {
diff --git a/gateway/src/opcodes/RequestGuildMembers.ts b/src/gateway/opcodes/RequestGuildMembers.ts
index b80721dc..b80721dc 100644
--- a/gateway/src/opcodes/RequestGuildMembers.ts
+++ b/src/gateway/opcodes/RequestGuildMembers.ts
diff --git a/gateway/src/opcodes/Resume.ts b/src/gateway/opcodes/Resume.ts
index 42dc586d..42dc586d 100644
--- a/gateway/src/opcodes/Resume.ts
+++ b/src/gateway/opcodes/Resume.ts
diff --git a/gateway/src/opcodes/VoiceStateUpdate.ts b/src/gateway/opcodes/VoiceStateUpdate.ts
index 321e6b17..c4297a68 100644
--- a/gateway/src/opcodes/VoiceStateUpdate.ts
+++ b/src/gateway/opcodes/VoiceStateUpdate.ts
@@ -1,4 +1,3 @@
-import { VoiceStateUpdateSchema } from "../schema/VoiceStateUpdateSchema";
import { Payload, WebSocket } from "@fosscord/gateway";
import { genVoiceToken } from "../util/SessionUtils";
import { check } from "./instanceOf";
@@ -7,11 +6,13 @@ import {
emitEvent,
Guild,
Member,
- Region,
VoiceServerUpdateEvent,
VoiceState,
VoiceStateUpdateEvent,
+ VoiceStateUpdateSchema,
} from "@fosscord/util";
+import { OrmUtils } from "@fosscord/util";
+import { Region } from "@fosscord/util";
// TODO: check if a voice server is setup
// Notice: Bot users respect the voice channel's user limit, if set. When the voice channel is full, you will not receive the Voice State Update or Voice Server Update events in response to your own Voice State Update. Having MANAGE_CHANNELS permission bypasses this limit and allows you to join regardless of the channel being full or not.
@@ -19,6 +20,11 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
check.call(this, VoiceStateUpdateSchema, data.d);
const body = data.d as VoiceStateUpdateSchema;
+ if(body.guild_id == null) {
+ console.log(`[Gateway] VoiceStateUpdate called with guild_id == null by user ${this.user_id}!`);
+ return;
+ }
+
let voiceState: VoiceState;
try {
voiceState = await VoiceState.findOneOrFail({
@@ -47,9 +53,9 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
//The event send by Discord's client on channel leave has both guild_id and channel_id as null
if (body.guild_id === null) body.guild_id = voiceState.guild_id;
- voiceState.assign(body);
+ voiceState = OrmUtils.mergeDeep(voiceState, body);
} catch (error) {
- voiceState = new VoiceState({
+ voiceState = OrmUtils.mergeDeep(new VoiceState(), {
...body,
user_id: this.user_id,
deaf: false,
@@ -84,7 +90,7 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
//If it's null it means that we are leaving the channel and this event is not needed
if (voiceState.channel_id !== null) {
- const guild = await Guild.findOne({ id: voiceState.guild_id });
+ const guild = await Guild.findOne({ where: { id: voiceState.guild_id } });
const regions = Config.get().regions;
let guildRegion: Region;
if (guild && guild.region) {
diff --git a/gateway/src/opcodes/experiments.json b/src/gateway/opcodes/experiments.json
index 0370b5da..0370b5da 100644
--- a/gateway/src/opcodes/experiments.json
+++ b/src/gateway/opcodes/experiments.json
diff --git a/gateway/src/opcodes/index.ts b/src/gateway/opcodes/index.ts
index 027739db..027739db 100644
--- a/gateway/src/opcodes/index.ts
+++ b/src/gateway/opcodes/index.ts
diff --git a/gateway/src/opcodes/instanceOf.ts b/src/gateway/opcodes/instanceOf.ts
index 6fd50852..eb6f6ea1 100644
--- a/gateway/src/opcodes/instanceOf.ts
+++ b/src/gateway/opcodes/instanceOf.ts
@@ -1,4 +1,4 @@
-import { instanceOf } from "lambert-server";
+import { instanceOf } from "@fosscord/util";
import { WebSocket } from "@fosscord/gateway";
import { CLOSECODES } from "../util/Constants";
|