From 5672bd69dd490397bd35628cf5ad7135b87362ff Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Tue, 5 Apr 2022 21:49:03 +0800 Subject: Get announcements from course activity_stream --- contents/ui/main.qml | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) (limited to 'contents/ui/main.qml') diff --git a/contents/ui/main.qml b/contents/ui/main.qml index 33c12b6..e9529cf 100644 --- a/contents/ui/main.qml +++ b/contents/ui/main.qml @@ -17,28 +17,42 @@ Item { readonly property string oauth2Token: plasmoid.configuration.oauth2Token readonly property string authHeader: `Bearer ${oauth2Token}` - function syncCanvas() { + function callApi(path, amount, callback) { let xhr = new XMLHttpRequest() - xhr.open("GET", `${apiEndpoint}/courses`) + xhr.open("GET", `${apiEndpoint}${path}` + (amount ? `?per_page=${amount}` : "")) xhr.setRequestHeader("Authorization", authHeader) - xhr.onload = function () { + xhr.onload = () => { if (xhr.status == 200) { try { - let courses = JSON.parse(xhr.responseText) - for (let c of courses) { - announcementsModel.append({course: c.name, title: ""}) - } + let json = JSON.parse(xhr.responseText) + if (callback) { callback(json) } } catch (e) { - if (e instanceof SyntaxError) { console.error("Cannot parse JSON:") } - else { throw e } + if (e instanceof SyntaxError) { + console.error(`Cannot parse response for ${path} as JSON:\n${xhr.responseText}`) + } else { throw e } } } else { - console.error("XHR failed") + console.error(`XHR failed when retrieving ${path} (status ${xhr.status}):\n${xhr.responseText}`) } } xhr.send() } + function syncCanvas() { + // Get all courses + callApi("/courses", 20, courses => { + for (let course of courses) { + callApi(`/courses/${course.id}/activity_stream`, 10, activityStream => { + for (let activity of activityStream) { + if (activity.type == "Announcement") { + announcementsModel.append({course: course.course_code, title: activity.title}) + } + } + }) + } + }) + } + Timer { interval: 60 * 1000 running: true; @@ -46,10 +60,6 @@ Item { onTriggered: syncCanvas(); } - Plasmoid.preferredRepresentation: Plasmoid.fullRepresentation - - Plasmoid.fullRepresentation: main - ColumnLayout { id: main anchors.fill: parent @@ -75,6 +85,9 @@ Item { Layout.margins: PlasmaCore.Units.smallSpacing Layout.fillWidth: true Layout.fillHeight: true + + ScrollBar.horizontal.policy: ScollBar.AlwaysOff + ListView { Layout.fillWidth: true delegate: Text { -- cgit v1.2.3