diff options
author | Frederick Yin <fkfd@fkfd.me> | 2022-04-05 21:49:03 +0800 |
---|---|---|
committer | Frederick Yin <fkfd@fkfd.me> | 2022-04-05 21:49:03 +0800 |
commit | 5672bd69dd490397bd35628cf5ad7135b87362ff (patch) | |
tree | 61265a5de953fc8995a2e17998312421ba51ce2b /contents/ui | |
parent | 10add64d4f3f78fe9aa38dda808bdc8ba41ff3e6 (diff) |
Get announcements from course activity_stream
Diffstat (limited to 'contents/ui')
-rw-r--r-- | contents/ui/main.qml | 41 |
1 files changed, 27 insertions, 14 deletions
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 { |