diff --git a/gateway/src/opcodes/Heartbeat.ts b/gateway/src/opcodes/Heartbeat.ts
index 015257b9..46caee56 100644
--- a/gateway/src/opcodes/Heartbeat.ts
+++ b/gateway/src/opcodes/Heartbeat.ts
@@ -1,7 +1,4 @@
-import { CLOSECODES, Payload } from "../util/Constants";
-import { Send } from "../util/Send";
-import { setHeartbeat } from "../util/setHeartbeat";
-import WebSocket from "../util/WebSocket";
+import { Payload, Send, setHeartbeat, WebSocket } from "@fosscord/gateway";
export async function onHeartbeat(this: WebSocket, data: Payload) {
// TODO: validate payload
diff --git a/gateway/src/opcodes/Identify.ts b/gateway/src/opcodes/Identify.ts
index 95db7f3d..a58583ee 100644
--- a/gateway/src/opcodes/Identify.ts
+++ b/gateway/src/opcodes/Identify.ts
@@ -1,5 +1,10 @@
-import { CLOSECODES, Payload, OPCODES } from "../util/Constants";
-import WebSocket from "../util/WebSocket";
+import {
+ WebSocket,
+ CLOSECODES,
+ Payload,
+ OPCODES,
+ genSessionId,
+} from "@fosscord/gateway";
import {
Channel,
checkToken,
@@ -14,15 +19,16 @@ import {
dbConnection,
PublicMemberProjection,
PublicMember,
+ ChannelType,
+ PublicUser,
} from "@fosscord/util";
import { setupListener } from "../listener/listener";
import { IdentifySchema } from "../schema/Identify";
-import { Send } from "../util/Send";
+import { Send } from "@fosscord/gateway/util/Send";
// import experiments from "./experiments.json";
const experiments: any = [];
import { check } from "./instanceOf";
-import { Recipient } from "../../../util/dist/entities/Recipient";
-import { genSessionId } from "../util/SessionUtils";
+import { Recipient } from "@fosscord/util";
// TODO: bot sharding
// TODO: check priviliged intents
@@ -48,15 +54,17 @@ export async function onIdentify(this: WebSocket, data: Payload) {
this.shard_id = identify.shard[0];
this.shard_count = identify.shard[1];
if (
- !this.shard_count ||
- !this.shard_id ||
+ this.shard_count == null ||
+ this.shard_id == null ||
this.shard_id >= this.shard_count ||
this.shard_id < 0 ||
this.shard_count <= 0
) {
+ console.log(identify.shard);
return this.close(CLOSECODES.Invalid_shard);
}
}
+ var users: PublicUser[] = [];
const members = await Member.find({
where: { id: this.user_id },
@@ -84,13 +92,42 @@ export async function onIdentify(this: WebSocket, data: Payload) {
const user_guild_settings_entries = members.map((x) => x.settings);
const recipients = await Recipient.find({
- where: { user_id: this.user_id },
- relations: ["channel", "channel.recipients"],
+ where: { user_id: this.user_id, closed: false },
+ relations: ["channel", "channel.recipients", "channel.recipients.user"],
+ // TODO: public user selection
+ });
+ const channels = recipients.map((x) => {
+ // @ts-ignore
+ x.channel.recipients = x.channel.recipients?.map((x) => x.user);
+ //TODO is this needed? check if users in group dm that are not friends are sent in the READY event
+ //users = users.concat(x.channel.recipients);
+ if (x.channel.isDm()) {
+ x.channel.recipients = x.channel.recipients!.filter(
+ (x) => x.id !== this.user_id
+ );
+ }
+ return x.channel;
+ });
+ const user = await User.findOneOrFail({
+ where: { id: this.user_id },
+ relations: ["relationships", "relationships.to"],
});
- const channels = recipients.map((x) => x.channel);
- const user = await User.findOneOrFail({ id: this.user_id });
if (!user) return this.close(CLOSECODES.Authentication_failed);
+ for (let relation of user.relationships) {
+ const related_user = relation.to;
+ const public_related_user = {
+ username: related_user.username,
+ discriminator: related_user.discriminator,
+ id: related_user.id,
+ public_flags: related_user.public_flags,
+ avatar: related_user.avatar,
+ bot: related_user.bot,
+ bio: related_user.bio,
+ };
+ users.push(public_related_user);
+ }
+
const session_id = genSessionId();
this.session_id = session_id; //Set the session of the WebSocket object
const session = new Session({
@@ -108,16 +145,6 @@ export async function onIdentify(this: WebSocket, data: Payload) {
//We save the session and we delete it when the websocket is closed
await session.save();
- const public_user = {
- username: user.username,
- discriminator: user.discriminator,
- id: user.id,
- public_flags: user.public_flags,
- avatar: user.avatar,
- bot: user.bot,
- bio: user.bio,
- };
-
const privateUser = {
avatar: user.avatar,
mobile: user.mobile,
@@ -154,7 +181,7 @@ export async function onIdentify(this: WebSocket, data: Payload) {
}),
guild_experiments: [], // TODO
geo_ordered_rtc_regions: [], // TODO
- relationships: user.relationships,
+ relationships: user.relationships.map((x) => x.toPublicRelationship()),
read_state: {
// TODO
entries: [],
@@ -180,7 +207,7 @@ export async function onIdentify(this: WebSocket, data: Payload) {
// @ts-ignore
experiments: experiments, // TODO
guild_join_requests: [], // TODO what is this?
- users: [public_user].unique(), // TODO
+ users: users.unique(),
merged_members: merged_members,
// shard // TODO: only for bots sharding
// application // TODO for applications
diff --git a/gateway/src/opcodes/LazyRequest.ts b/gateway/src/opcodes/LazyRequest.ts
index b7ee9a96..db00157f 100644
--- a/gateway/src/opcodes/LazyRequest.ts
+++ b/gateway/src/opcodes/LazyRequest.ts
@@ -2,13 +2,10 @@ import {
getPermission,
Member,
PublicMemberProjection,
- PublicUserProjection,
Role,
} from "@fosscord/util";
import { LazyRequest } from "../schema/LazyRequest";
-import { OPCODES, Payload } from "../util/Constants";
-import { Send } from "../util/Send";
-import WebSocket from "../util/WebSocket";
+import { WebSocket, Send, OPCODES, Payload } from "@fosscord/gateway";
import { check } from "./instanceOf";
import "missing-native-js-functions";
diff --git a/gateway/src/opcodes/PresenceUpdate.ts b/gateway/src/opcodes/PresenceUpdate.ts
index 3760f8a3..53d7b9d2 100644
--- a/gateway/src/opcodes/PresenceUpdate.ts
+++ b/gateway/src/opcodes/PresenceUpdate.ts
@@ -1,5 +1,4 @@
-import { CLOSECODES, Payload } from "../util/Constants";
-import WebSocket from "../util/WebSocket";
+import { WebSocket, Payload } from "@fosscord/gateway";
export function onPresenceUpdate(this: WebSocket, data: Payload) {
// return this.close(CLOSECODES.Unknown_error);
diff --git a/gateway/src/opcodes/RequestGuildMembers.ts b/gateway/src/opcodes/RequestGuildMembers.ts
index 2701d978..b80721dc 100644
--- a/gateway/src/opcodes/RequestGuildMembers.ts
+++ b/gateway/src/opcodes/RequestGuildMembers.ts
@@ -1,6 +1,4 @@
-import { CLOSECODES, Payload } from "../util/Constants";
-
-import WebSocket from "../util/WebSocket";
+import { Payload, WebSocket } from "@fosscord/gateway";
export function onRequestGuildMembers(this: WebSocket, data: Payload) {
// return this.close(CLOSECODES.Unknown_error);
diff --git a/gateway/src/opcodes/Resume.ts b/gateway/src/opcodes/Resume.ts
index 4efde9b0..398cce25 100644
--- a/gateway/src/opcodes/Resume.ts
+++ b/gateway/src/opcodes/Resume.ts
@@ -1,7 +1,4 @@
-import { CLOSECODES, Payload } from "../util/Constants";
-import { Send } from "../util/Send";
-
-import WebSocket from "../util/WebSocket";
+import { WebSocket, Payload, Send } from "@fosscord/gateway";
export async function onResume(this: WebSocket, data: Payload) {
console.log("Got Resume -> cancel not implemented");
diff --git a/gateway/src/opcodes/VoiceStateUpdate.ts b/gateway/src/opcodes/VoiceStateUpdate.ts
index 95a01608..084c5766 100644
--- a/gateway/src/opcodes/VoiceStateUpdate.ts
+++ b/gateway/src/opcodes/VoiceStateUpdate.ts
@@ -1,9 +1,16 @@
import { VoiceStateUpdateSchema } from "../schema/VoiceStateUpdateSchema";
-import { Payload } from "../util/Constants";
-import WebSocket from "../util/WebSocket";
+import { Payload, WebSocket, genVoiceToken } from "@fosscord/gateway";
import { check } from "./instanceOf";
-import { Config, emitEvent, Guild, Member, Region, VoiceServerUpdateEvent, VoiceState, VoiceStateUpdateEvent } from "@fosscord/util";
-import { genVoiceToken } from "../util/SessionUtils";
+import {
+ Config,
+ emitEvent,
+ Guild,
+ Member,
+ Region,
+ VoiceServerUpdateEvent,
+ VoiceState,
+ VoiceStateUpdateEvent,
+} 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.
@@ -14,21 +21,27 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
let voiceState: VoiceState;
try {
voiceState = await VoiceState.findOneOrFail({
- where: { user_id: this.user_id }
+ 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;
}
//If a user change voice channel between guild we should send a left event first
- if (voiceState.guild_id !== body.guild_id && voiceState.session_id === this.session_id) {
+ if (
+ voiceState.guild_id !== body.guild_id &&
+ voiceState.session_id === this.session_id
+ ) {
await emitEvent({
event: "VOICE_STATE_UPDATE",
data: { ...voiceState, channel_id: null },
guild_id: voiceState.guild_id,
- })
+ });
}
//The event send by Discord's client on channel leave has both guild_id and channel_id as null
@@ -50,10 +63,11 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
voiceState.member = await Member.findOneOrFail({
where: { id: voiceState.user_id, guild_id: voiceState.guild_id },
relations: ["user", "roles"],
- })
+ });
//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 { id, ...newObj } = voiceState;
@@ -69,13 +83,17 @@ 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({ id: voiceState.guild_id });
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({
diff --git a/gateway/src/opcodes/index.ts b/gateway/src/opcodes/index.ts
index fa57f568..027739db 100644
--- a/gateway/src/opcodes/index.ts
+++ b/gateway/src/opcodes/index.ts
@@ -1,5 +1,4 @@
-import { Payload } from "../util/Constants";
-import WebSocket from "../util/WebSocket";
+import { WebSocket, Payload } from "@fosscord/gateway";
import { onHeartbeat } from "./Heartbeat";
import { onIdentify } from "./Identify";
import { onLazyRequest } from "./LazyRequest";
@@ -21,5 +20,6 @@ export default {
8: onRequestGuildMembers,
// 9: Invalid Session
// 10: Hello
+ // 13: Dm_update
14: onLazyRequest,
};
diff --git a/gateway/src/opcodes/instanceOf.ts b/gateway/src/opcodes/instanceOf.ts
index c4ee5ee6..37d513ad 100644
--- a/gateway/src/opcodes/instanceOf.ts
+++ b/gateway/src/opcodes/instanceOf.ts
@@ -1,6 +1,5 @@
import { instanceOf } from "lambert-server";
-import { CLOSECODES } from "../util/Constants";
-import WebSocket from "../util/WebSocket";
+import { WebSocket, CLOSECODES } from "@fosscord/gateway";
export function check(this: WebSocket, schema: any, data: any) {
try {
|