-
-
Notifications
You must be signed in to change notification settings - Fork 313
feat: add presences on ready_supplemental #1680
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,6 +50,8 @@ import { | |
| Intents, | ||
| OPCodes, | ||
| OrmUtils, | ||
| getMostRelevantSession, | ||
| Presence, | ||
| PresenceUpdateEvent, | ||
| ReadyEventData, | ||
| ReadyGuildDTO, | ||
|
|
@@ -58,7 +60,7 @@ import { | |
| TraceNode, | ||
| TraceRoot, | ||
| } from "@spacebar/util"; | ||
| import { ChannelType, DefaultUserGuildSettings, DMChannel, IdentifySchema, PrivateUserProjection, PublicUser, PublicUserProjection } from "@spacebar/schemas"; | ||
| import { ChannelType, DefaultUserGuildSettings, DMChannel, IdentifySchema, PrivateUserProjection, PublicUser, PublicUserProjection, RelationshipType } from "@spacebar/schemas"; | ||
| import { check } from "./instanceOf"; | ||
|
|
||
| // TODO: user sharding | ||
|
|
@@ -323,6 +325,46 @@ export async function onIdentify(this: WebSocket, data: Payload) { | |
|
|
||
| const userMetaQueryTime = taskSw.getElapsedAndReset(); | ||
|
|
||
| const friendPresenceUserIds = [...new Set(relationships.filter((relationship) => relationship.type === RelationshipType.friends).map((relationship) => relationship.to_id))]; | ||
| const { result: friendPresenceSessions, elapsed: friendPresenceSessionsQueryTime } = await timePromise(() => | ||
| friendPresenceUserIds.length === 0 | ||
| ? Promise.resolve([] as Session[]) | ||
| : getDatabase()!.getRepository(Session).find({ | ||
| where: { | ||
| user_id: In(friendPresenceUserIds), | ||
| is_admin_session: false, | ||
| status: Not(In(["offline", "invisible"])), | ||
| }, | ||
| relations: { user: true }, | ||
| select: { | ||
| user_id: true, | ||
| status: true, | ||
| activities: true, | ||
| client_status: true, | ||
| user: Object.fromEntries(PublicUserProjection.map((x) => [x, true])), | ||
| }, | ||
| }), | ||
| ); | ||
|
|
||
|
Comment on lines
+328
to
+348
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wouldn't this spam a bunch of db queries if you have a lot of friends? |
||
| const { result: friendPresences, elapsed: generateFriendPresencesTime } = timeFunction<Presence[]>(() => { | ||
| const sessionsByUserId = arrayGroupBy(friendPresenceSessions, (session) => session.user_id); | ||
|
|
||
| return friendPresenceUserIds.flatMap((userId) => { | ||
| const sessions = sessionsByUserId.get(userId); | ||
| if (!sessions?.length) return []; | ||
|
|
||
| const session = getMostRelevantSession(sessions); | ||
| return [ | ||
| { | ||
| user: session.user.toPublicUser(), | ||
| status: session.getPublicStatus(), | ||
| activities: session.activities, | ||
| client_status: session.client_status, | ||
| }, | ||
| ]; | ||
| }); | ||
| }); | ||
|
|
||
| const memberGuildIds = members.map((m) => m.guild_id); | ||
|
|
||
| // select relations | ||
|
|
@@ -688,7 +730,7 @@ export async function onIdentify(this: WebSocket, data: Payload) { | |
| version: 0, // TODO | ||
| }, | ||
| private_channels: channels, | ||
| presences: [], // TODO: Send actual data | ||
| presences: [], | ||
|
Comment on lines
-691
to
+733
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. another disappearing comment |
||
| session_id: this.session_id, | ||
| country_code: this.session?.last_seen_location_info?.country_code ?? user.settings!.locale, | ||
| users: Array.from(users), | ||
|
|
@@ -746,10 +788,12 @@ export async function onIdentify(this: WebSocket, data: Payload) { | |
| queryGuildsTime, | ||
| guildRelationQueryTime, | ||
| createUserSettingsTime, | ||
| friendPresenceSessionsQueryTime, | ||
| mergedMembersTime, | ||
| generateGuildsListTime, | ||
| generateUserGuildSettingsTime, | ||
| generateDmChannelsTime, | ||
| generateFriendPresencesTime, | ||
| appendRelationshipsTime, | ||
| findAndGenerateSessionReplaceTime, | ||
| emitSessionsReplaceTime, | ||
|
|
@@ -768,6 +812,7 @@ export async function onIdentify(this: WebSocket, data: Payload) { | |
| sessionSaveTime, | ||
| sessionQueryTime, | ||
| relationshipQueryTime, | ||
| friendPresenceSessionsQueryTime, | ||
| settingsQueryTime, | ||
| settingsProtosQueryTime, | ||
| applicationQueryTime, | ||
|
|
@@ -845,28 +890,32 @@ export async function onIdentify(this: WebSocket, data: Payload) { | |
| }), | ||
| ); | ||
|
|
||
| const readySupplementalGuilds = (guilds.filter((guild) => !guild.unavailable) as Guild[]).map((guild) => ({ | ||
| voice_states: guild.voice_states.map((state) => VoiceState.prototype.toPublicVoiceState.apply(state)), | ||
| id: guild.id, | ||
| embedded_activities: [], | ||
| })); | ||
| const readySupplementalGuilds = guilds.map((guild) => { | ||
| if (!("voice_states" in guild)) return { id: guild.id }; | ||
|
|
||
| const availableGuild = guild as Guild; | ||
| return { | ||
| id: availableGuild.id, | ||
| voice_states: availableGuild.voice_states.map((state) => VoiceState.prototype.toPublicVoiceState.apply(state)), | ||
| activity_instances: [], | ||
| }; | ||
|
Comment on lines
+897
to
+901
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is embedded_activities not required? |
||
| }); | ||
|
|
||
| // TODO: ready supplemental | ||
| await Send(this, { | ||
| op: OPCodes.DISPATCH, | ||
| t: EVENTEnum.ReadySupplemental, | ||
| s: this.sequence++, | ||
| d: { | ||
| guilds: readySupplementalGuilds, // { voice_states: [], id: string, embedded_activities: [] } | ||
| merged_members: guilds.map(() => []), // these merged members seem to be all users currently in vc in your guilds | ||
| merged_presences: { | ||
| guilds: [], | ||
| friends: [], | ||
| friends: friendPresences, | ||
| guilds: guilds.map(() => []), | ||
| }, | ||
| // these merged members seem to be all users currently in vc in your guilds | ||
| merged_members: [], | ||
| lazy_private_channels: [], | ||
| guilds: readySupplementalGuilds, // { voice_states: [], id: string, embedded_activities: [] } | ||
| // embedded_activities are users currently in an activity? | ||
| disclose: [], // Config.get().general.uniqueUsernames ? ["pomelo"] : [] | ||
|
leafusfluffy marked this conversation as resolved.
|
||
| game_invites: [], | ||
| }, | ||
| }); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"unknown" is one i usually put in there too for good measure