From 82e26623acbfd7ecd5c602f368be78b0a7ab7048 Mon Sep 17 00:00:00 2001 From: Ben Holt Date: Thu, 18 Mar 2021 11:57:28 -0400 Subject: [PATCH] Added timeOffsetMillis to outline API; not yet tested --- src/course-home/data/api.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/course-home/data/api.js b/src/course-home/data/api.js index 0c1ead2a..8e566d89 100644 --- a/src/course-home/data/api.js +++ b/src/course-home/data/api.js @@ -157,8 +157,12 @@ export async function getProctoringInfoData(courseId) { export async function getOutlineTabData(courseId) { const url = `${getConfig().LMS_BASE_URL}/api/course_home/v1/outline/${courseId}`; let { tabData } = {}; + let requestTime = Date.now(); + let responseTime = requestTime; try { + requestTime = Date.now(); tabData = await getAuthenticatedHttpClient().get(url); + responseTime = Date.now(); } catch (error) { const { httpErrorStatus } = error && error.customAttributes; if (httpErrorStatus === 404) { @@ -168,6 +172,18 @@ export async function getOutlineTabData(courseId) { throw error; } + // Time offset computation should move down into the HttpClient wrapper to maintain a global time correction reference + // Requires 'Access-Control-Expose-Headers: Date' on the server response per https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#access-control-expose-headers + const headerDate = tabData.headers.date; + + let timeOffsetMillis = 0; + if (headerDate !== undefined) { + const headerTime = Date.parse(headerDate); + const roundTripMillis = requestTime - responseTime; + const localTime = responseTime - (roundTripMillis / 2); // Roughly compensate for transit time + timeOffsetMillis = headerTime - localTime; + } + const { data, } = tabData; @@ -187,6 +203,7 @@ export async function getOutlineTabData(courseId) { const welcomeMessageHtml = data.welcome_message_html; return { + timeOffsetMillis, // This should move to a global time correction reference accessExpiration, canShowUpgradeSock, courseBlocks, @@ -201,7 +218,6 @@ export async function getOutlineTabData(courseId) { resumeCourse, verifiedMode, welcomeMessageHtml, - timeOffsetMillis: 0, }; }