Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/api/routes/users/@me/relationships.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/

import { route } from "@spacebar/api";
import { Config, DiscordApiErrors, Relationship, RelationshipAddEvent, RelationshipRemoveEvent, RelationshipUpdateEvent, User, emitEvent } from "@spacebar/util";
import { Channel, Config, DiscordApiErrors, Relationship, RelationshipAddEvent, RelationshipRemoveEvent, RelationshipUpdateEvent, User, emitEvent } from "@spacebar/util";
import { Request, Response, Router } from "express";
import { HTTPError } from "lambert-server";
import { PublicUserProjection, RelationshipType, RelationshipPatchSchema } from "@spacebar/schemas";
Expand Down Expand Up @@ -315,5 +315,14 @@ async function updateRelationship(req: Request, res: Response, friend: User, typ
} as RelationshipAddEvent),
]);


if (
incoming_relationship.type === RelationshipType.friends &&
outgoing_relationship.type === RelationshipType.friends
) {
await Channel.createDMChannel([id], req.user_id);
await Channel.createDMChannel([req.user_id], id);
}

return res.sendStatus(204);
}
21 changes: 20 additions & 1 deletion src/gateway/events/Close.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,26 @@ export async function Close(this: WebSocket, code: number, reason: Buffer) {
this.removeAllListeners();

if (this.session_id) {
await Session.delete({ session_id: this.session_id });
const authSessionId = this.session?.session_id;
const closedAt = Date.now();

setTimeout(async () => {
try {
if (authSessionId && this.user_id) {
const s = await Session.findOne({
where: { user_id: this.user_id, session_id: authSessionId },
});
if (s && (s.last_seen?.getTime() ?? 0) <= closedAt) {
await Session.update(
{ user_id: this.user_id, session_id: authSessionId },
{ status: "offline", activities: [], client_status: {} }
);
}
}
} catch(e) {
console.error("[WebSocket] Close session cleanup failed", code, e);
}
}, 10_000);

const voiceState = await VoiceState.findOne({
where: { user_id: this.user_id },
Expand Down
18 changes: 13 additions & 5 deletions src/gateway/opcodes/Identify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,12 +529,20 @@ export async function onIdentify(this: WebSocket, data: Payload) {
let channelUsers = channel.recipients?.map((recipient) => recipient.user.toPublicUser());

if (channelUsers && channelUsers.length > 0) channelUsers.forEach((user) => users.add(user));
// HACK: insert self into recipients for DMs with users that no longer exist
// HACK: insert "Deleted User" into recipients for DMs with users that no longer exist
else if (channel.type === ChannelType.DM) {
const selfUser = user.toPublicUser();
users.add(selfUser);
channelUsers ??= [];
channelUsers.push(selfUser);
const deletedUser = {
avatar: null,
discriminator: "0000",
id: "0",
public_flags: 0,
username: "Deleted User",
badge_ids: null,
};

users.add(deletedUser as any);
channelUsers ??= [];
channelUsers.push(deletedUser as any);
}

return {
Expand Down
14 changes: 14 additions & 0 deletions src/util/dtos/DmChannelDTO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ export class DmChannelDTO {
}) || [],
)
).map((u) => new MinimalPublicUserDTO(u));

if (obj.type === 1 && obj.recipients.length === 0) {
obj.recipients = [
new MinimalPublicUserDTO({
avatar: null,
discriminator: "0000",
id: "0",
public_flags: 0,
username: "Deleted User",
badge_ids: null,
} as any),
];
}

return obj;
}

Expand Down
5 changes: 2 additions & 3 deletions src/util/entities/Channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,13 +494,12 @@ export class Channel extends BaseClass {
} else {
await emitEvent({
event: "CHANNEL_CREATE",
data: channel_dto,
data: channel_dto.excludedRecipients([creator_user_id]),
user_id: creator_user_id,
});
}

if (recipients.length === 1) return channel_dto;
else return channel_dto.excludedRecipients([creator_user_id]);
if (recipients.length === 1) return channel_dto.excludedRecipients([creator_user_id]);
}

static async removeRecipientFromChannel(channel: Channel, user_id: string) {
Expand Down