diff --git a/src/gateway/events/Message.ts b/src/gateway/events/Message.ts
index 45790146..52d9edd8 100644
--- a/src/gateway/events/Message.ts
+++ b/src/gateway/events/Message.ts
@@ -16,15 +16,15 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
-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);
diff --git a/src/gateway/opcodes/Identify.ts b/src/gateway/opcodes/Identify.ts
index 7610901a..9a3128d9 100644
--- a/src/gateway/opcodes/Identify.ts
+++ b/src/gateway/opcodes/Identify.ts
@@ -17,49 +17,57 @@
*/
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,
+ OPCodes,
+ Permissions,
PresenceUpdateEvent,
- IdentifySchema,
- DefaultUserGuildSettings,
- ReadyGuildDTO,
- Guild,
+ PrivateSessionProjection,
+ PrivateUserProjection,
+ PublicUser,
PublicUserProjection,
+ ReadState,
+ ReadyEventData,
+ ReadyGuildDTO,
ReadyUserGuildSettingsEntries,
- UserSettings,
- Permissions,
- DMChannel,
- GuildOrUnavailable,
Recipient,
- OPCodes,
+ Session,
+ SessionsReplace,
+ UserSettings,
+ checkToken,
+ emitEvent,
} from "@spacebar/util";
import { check } from "./instanceOf";
// TODO: user sharding
// TODO: check privileged intents, if defined in the config
+const tryGetUserFromToken = async (...args: Parameters<typeof checkToken>) => {
+ 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"],
});
@@ -332,10 +340,9 @@ 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,
+ session_id: x.session_id, // TODO: discord.com sends 'all', what is that???
status: x.status,
}));
|