Skip to content

Commit

Permalink
fix: update presence
Browse files Browse the repository at this point in the history
  • Loading branch information
jouwdan committed Aug 9, 2023
1 parent bc7155e commit add243a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 16 deletions.
15 changes: 15 additions & 0 deletions apps/web/src/lib/services/presence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ export function setupPresence(supabase: SupabaseClient, courseid: string) {
});
}

export function updatePresence(presence: Presence) {
presenceChannel.track({
online_at: new Date().toISOString(),
studentName: presence.studentName,
studentEmail: presence.studentEmail,
studentImg: presence.studentImg,
courseTitle: presence.courseTitle,
loTitle: presence.loTitle,
loImage: presence.loImage,
loRoute: presence.loRoute,
loIcon: presence.loIcon
});
}

export function subscribePresence(presence: Presence, courseid: string) {
try {
presenceChannel.subscribe(async (status) => {
Expand All @@ -23,6 +37,7 @@ export function subscribePresence(presence: Presence, courseid: string) {
studentName: presence.studentName,
studentEmail: presence.studentEmail,
studentImg: presence.studentImg,
courseTitle: presence.courseTitle,
loTitle: presence.loTitle,
loImage: presence.loImage,
loRoute: presence.loRoute,
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/lib/types/presence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface Presence {
studentName: string;
studentEmail: string;
studentImg: string;
courseTitle: string;
loTitle: string;
loImage: string;
loRoute: string;
Expand Down
49 changes: 33 additions & 16 deletions apps/web/src/routes/(course-reader)/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
import { analyticsService } from '$lib/services/analytics';
import { initServices } from '$lib/tutors-startup';
import type { RealtimeChannel } from '@supabase/supabase-js';
import { setupPresence, subscribePresence, unsubscribePresence } from '$lib/services/presence';
import {
setupPresence,
subscribePresence,
unsubscribePresence,
updatePresence
} from '$lib/services/presence';
let currentRoute = '';
Expand All @@ -34,20 +39,24 @@
}
}
let currentStatus: boolean;
onlineStatus.subscribe((value) => {
currentStatus = value;
});
let presenceSetup: boolean = false;
$: currentLo;
onMount(() => {
!presenceSetup && setupPresence(supabase, $page.params.courseid);
presenceSetup = true;
setInitialClassState();
initServices(data.session);
setInterval(updatePageCount, 30 * 1000);
$: {
if (currentStatus) {
if ($onlineStatus) {
subscribePresence(
{
studentName: session.user.user_metadata.full_name,
studentEmail: session.user.user_metadata.email,
studentImg: session.user.user_metadata.avatar_url,
courseTitle: get(currentLo).parentLo
? get(currentLo).parentLo.title
: get(currentLo).title,
loTitle: get(currentLo).title,
loImage: get(currentLo).img,
loRoute: get(currentLo).route,
Expand All @@ -56,19 +65,27 @@
$page.params.courseid
);
console.log('subscribed');
} else if (!currentStatus) {
console.log(get(studentsOnlineList));
} else if (!$onlineStatus) {
unsubscribePresence();
console.log('unsubscribed');
}
}
onMount(() => {
setupPresence(supabase, $page.params.courseid);
setInitialClassState();
initServices(data.session);
setInterval(updatePageCount, 30 * 1000);
});
$: $currentLo &&
$onlineStatus &&
presenceSetup &&
updatePresence({
studentName: session.user.user_metadata.full_name,
studentEmail: session.user.user_metadata.email,
studentImg: session.user.user_metadata.avatar_url,
courseTitle: get(currentLo).parentLo ? get(currentLo).parentLo.title : get(currentLo).title,
loTitle: get(currentLo).title,
loImage: get(currentLo).img,
loRoute: get(currentLo).route,
loIcon: get(currentLo).icon
});
page.subscribe((path) => {
if (path.route.id) {
currentRoute = path.route.id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const load: PageLoad = async ({ params }) => {
}

return {
course: topic.course,
topic: topic,
lo: topic.lo,
unitId: unitId
Expand Down

0 comments on commit add243a

Please sign in to comment.