Skip to content

Commit

Permalink
fix: duplicate presences
Browse files Browse the repository at this point in the history
  • Loading branch information
jouwdan committed Sep 12, 2023
1 parent b464387 commit 2a92b86
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
32 changes: 26 additions & 6 deletions app/src/lib/services/presence.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { RealtimeChannel, SupabaseClient } from "@supabase/supabase-js";
import { studentsOnline, studentsOnlineList } from "$lib/stores";
import type { Presence } from "$lib/services/types/presence";
import type { Presence, PresenceObject } from "$lib/services/types/presence";

let presenceChannel: RealtimeChannel;

Expand Down Expand Up @@ -55,15 +55,25 @@ export function subscribePresence(presence: Presence, courseid: string) {
const courseIDWithoutNetlify = courseid.replace(".netlify.app", "");
const courseIDWithNetlify = `${courseIDWithoutNetlify}.netlify.app`;

const onlineUsersObj = [];
const onlineUsersObj: PresenceObject[] = [];
for (const [key, value] of Object.entries(presenceState)) {
if (key === courseIDWithoutNetlify || key === courseIDWithNetlify) {
onlineUsersObj.push(...value);
}
}

studentsOnline.set(onlineUsersObj.length);
studentsOnlineList.set(onlineUsersObj);
const uniqueUsers = new Set();

const filteredUniquePresences: PresenceObject[] = onlineUsersObj.filter((presence) => {
if (!uniqueUsers.has(presence.studentEmail)) {
uniqueUsers.add(presence.studentEmail);
return true;
}
return false;
});

studentsOnline.set(filteredUniquePresences.length);
studentsOnlineList.set(filteredUniquePresences);
});

presenceChannel.on("presence", { event: "join" }, ({ newPresences }) => {
Expand All @@ -72,8 +82,18 @@ export function subscribePresence(presence: Presence, courseid: string) {

const filteredNewPresences = newPresences.filter((presence) => presence.channel === courseIDWithoutNetlify || presence.channel === courseIDWithNetlify);

studentsOnline.update((count) => count + filteredNewPresences.length);
studentsOnlineList.update((list) => [...list, ...filteredNewPresences]);
const uniqueUsers = new Set();

const filteredUniquePresences = filteredNewPresences.filter((presence) => {
if (!uniqueUsers.has(presence.studentEmail)) {
uniqueUsers.add(presence.studentEmail);
return true;
}
return false;
});

studentsOnline.update((count) => count + filteredUniquePresences.length);
studentsOnlineList.update((list) => [...list, ...filteredUniquePresences]);
});

presenceChannel.on("presence", { event: "leave" }, ({ leftPresences }) => {
Expand Down
4 changes: 4 additions & 0 deletions app/src/lib/services/types/presence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ export interface Presence {
loRoute: string;
loIcon?: IconType;
}

export type PresenceObject = {
[key: string]: any;
};
3 changes: 2 additions & 1 deletion app/src/lib/stores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { StudentLoEvent } from "$lib/services/types/metrics";
import type { User } from "$lib/services/types/auth";
import { localStorageStore } from "@skeletonlabs/skeleton";
import type { Lo, Course } from "$lib/services/models/lo-types";
import type { PresenceObject } from "./services/types/presence";

export const revealSidebar = writable(false);
export const revealOnline = writable(false);
Expand All @@ -19,4 +20,4 @@ export const authenticating: Writable<boolean> = writable(false);

const students: StudentLoEvent[] = [];
export const studentsOnline = writable(0);
export const studentsOnlineList = writable(students);
export const studentsOnlineList = writable<PresenceObject>(students);

0 comments on commit 2a92b86

Please sign in to comment.