Skip to content

Commit 223f2ff

Browse files
committed
Handle invisible connections and return userids of visible connected folks
1 parent 93af33c commit 223f2ff

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

api/abstractplay.ts

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ type MeData = {
173173
standingChallenges?: FullChallenge[];
174174
realStanding?: StandingChallenge[];
175175
connections: number;
176+
connected: string[]
176177
}
177178

178179
type Rating = {
@@ -1779,7 +1780,7 @@ async function me(claim: PartialClaims, pars: { size: string, vars: string, upda
17791780
}));
17801781

17811782
// Get number of current connections
1782-
const connections = await countByPk("wsConnections");
1783+
const { totalCount: connections, visibleUserIds: connected } = await countAndVisibleUserIds("wsConnections");
17831784

17841785
let data = null;
17851786
console.log(`Fetching challenges`);
@@ -1847,6 +1848,7 @@ async function me(claim: PartialClaims, pars: { size: string, vars: string, upda
18471848
"standingChallenges": (data[3] as any[]).map(d => d.Item),
18481849
"realStanding": realStanding,
18491850
"connections": connections,
1851+
"connected": connected,
18501852
} as MeData, Set_toJSON),
18511853
headers
18521854
};
@@ -1869,6 +1871,7 @@ async function me(claim: PartialClaims, pars: { size: string, vars: string, upda
18691871
palettes,
18701872
"realStanding": realStanding,
18711873
"connections": connections,
1874+
"connected": connected,
18721875
} as MeData, Set_toJSON),
18731876
headers
18741877
}
@@ -8718,28 +8721,70 @@ const getAllUsers = async (): Promise<FullUser[]> => {
87188721
/**
87198722
* Count all items for a given partition key, paginating until complete.
87208723
*/
8721-
async function countByPk(pkValue: string): Promise<number> {
8722-
let total = 0;
8724+
// async function countByPk(pkValue: string): Promise<number> {
8725+
// let total = 0;
8726+
// let lastKey: Record<string, any> | undefined = undefined;
8727+
8728+
// do {
8729+
// const params: QueryCommandInput = {
8730+
// TableName: process.env.ABSTRACT_PLAY_TABLE,
8731+
// KeyConditionExpression: `pk = :pk`,
8732+
// ExpressionAttributeValues: {
8733+
// ":pk": pkValue,
8734+
// },
8735+
// Select: "COUNT",
8736+
// ExclusiveStartKey: lastKey,
8737+
// };
8738+
8739+
// const result = await ddbDocClient.send(new QueryCommand(params));
8740+
8741+
// total += result.Count ?? 0;
8742+
// lastKey = result.LastEvaluatedKey;
8743+
// } while (lastKey);
8744+
8745+
// return total;
8746+
// }
8747+
8748+
async function countAndVisibleUserIds(pkValue: string): Promise<{
8749+
totalCount: number;
8750+
visibleUserIds: string[];
8751+
}> {
8752+
let totalCount = 0;
8753+
const visibleUserIds: string[] = [];
87238754
let lastKey: Record<string, any> | undefined = undefined;
87248755

87258756
do {
87268757
const params: QueryCommandInput = {
87278758
TableName: process.env.ABSTRACT_PLAY_TABLE,
8728-
KeyConditionExpression: `pk = :pk`,
8759+
KeyConditionExpression: "pk = :pk",
87298760
ExpressionAttributeValues: {
87308761
":pk": pkValue,
87318762
},
8732-
Select: "COUNT",
8763+
// Only fetch what we need
8764+
ProjectionExpression: "userId, invisible",
87338765
ExclusiveStartKey: lastKey,
87348766
};
87358767

87368768
const result = await ddbDocClient.send(new QueryCommand(params));
8769+
const items = result.Items ?? [];
8770+
8771+
// Count all items
8772+
totalCount += items.length;
8773+
8774+
// Collect userIds where invisible is missing or false
8775+
for (const item of items) {
8776+
if (item.invisible !== true) {
8777+
// invisible is undefined OR false
8778+
if (item.userId) {
8779+
visibleUserIds.push(item.userId);
8780+
}
8781+
}
8782+
}
87378783

8738-
total += result.Count ?? 0;
87398784
lastKey = result.LastEvaluatedKey;
87408785
} while (lastKey);
87418786

8742-
return total;
8787+
return { totalCount, visibleUserIds };
87438788
}
87448789

87458790
async function wsBroadcast (verb: string, payload: any, exclude?: string[]): Promise<SendMessageCommandOutput> {

api/sockets/authHandler.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export const handler = async (event: WebSocketEvent) => {
4444
// Parse the incoming message body
4545
const body = JSON.parse(event.body ?? "{}");
4646
const token: string | undefined = body.token;
47+
const invisible: boolean = body.invisible ?? false;
4748

4849
if (!token) {
4950
console.error("Missing token in auth message");
@@ -69,6 +70,7 @@ export const handler = async (event: WebSocketEvent) => {
6970

7071
connectionId,
7172
userId,
73+
invisible,
7274

7375
// Optional TTL for auto-cleanup
7476
ttl: Math.floor(Date.now() / 1000) + 3600,

0 commit comments

Comments
 (0)