1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
import QtQuick 2.15
import QtQuick.Layouts 1.15
import QtQuick.Controls 2.15
import org.kde.plasma.plasmoid 2.0
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.components 2.0 as PlasmaComponents
import org.kde.plasma.components 3.0 as PlasmaComponents3
import org.kde.plasma.extras 2.0 as PlasmaExtras
Item {
width: PlasmaCore.Units.gridUnit * 20
height: PlasmaCore.Units.gridUnit * 40
clip: true
readonly property string canvasUrl: plasmoid.configuration.canvasUrl
readonly property string apiEndpoint: `${canvasUrl.replace(/\/$/, "")}/api/v1`
readonly property string oauth2Token: plasmoid.configuration.oauth2Token
readonly property string authHeader: `Bearer ${oauth2Token}`
function callApi(path, amount, callback) {
let xhr = new XMLHttpRequest()
xhr.open("GET", `${apiEndpoint}${path}` + (amount ? `?per_page=${amount}` : ""))
xhr.setRequestHeader("Authorization", authHeader)
xhr.onload = () => {
if (xhr.status == 200) {
try {
let json = JSON.parse(xhr.responseText)
if (callback) { callback(json) }
} catch (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 when retrieving ${path} (status ${xhr.status}):\n${xhr.responseText}`)
}
}
xhr.send()
}
function syncCanvas() {
let courses = plasmoid.configuration.courses.split("\n").map(
// each line in the "courses" config consists of
// a numeric course id, a space, and a course code
line => { return line.split(" ", 2) }
)
announcementsModel.clear()
assignmentsModel.clear()
for (let course of courses) {
callApi(`/courses/${course[0]}/activity_stream`, 10, activityStream => {
// Get activity stream for each course
let announcementCount = 0
let assignmentCount = 0
activityStream.forEach(activity => {
if (activity.type == "Announcement") {
announcementsModel.append({
type: "announcement",
activityId: activity.id,
index: announcementCount,
course: course[1],
title: activity.title,
url: activity.html_url,
important: plasmoid.configuration.importantActivities.includes(
activity.id.toString()
),
})
announcementCount++
} else if (activity.type == "Submission") {
assignmentsModel.append({
type: "assignment",
activityId: activity.id,
index: assignmentCount,
course: course[1],
title: activity.title,
due: activity.assignment.due_at,
url: activity.html_url,
important: plasmoid.configuration.importantActivities.includes(
activity.id.toString()
),
})
assignmentCount++
}
})
})
}
}
Timer {
interval: 60 * 1000
running: true;
repeat: true;
onTriggered: syncCanvas();
}
ColumnLayout {
id: main
anchors.fill: parent
PlasmaExtras.Heading {
level: 1
text: "Kanvas"
}
PlasmaExtras.Heading {
level: 2
text: "Announcements"
}
ListModel {
id: announcementsModel
ListElement {
type: "announcement"
index: 0
course: "CS101"
title: "Title"
url: "https://xkcd.com"
important: true
activityId: 0
}
}
ScrollView {
implicitHeight: PlasmaCore.Units.gridUnit * 20
Layout.margins: PlasmaCore.Units.smallSpacing
Layout.fillWidth: true
Layout.fillHeight: true
ScrollBar.horizontal.policy: ScrollBar.AlwaysOff
ListView {
Layout.fillWidth: true
delegate: ActivityView {}
model: announcementsModel
}
}
PlasmaExtras.Heading {
level: 2
text: "Assignments"
}
ListModel {
id: assignmentsModel
ListElement {
type: "assignment"
index: 0
course: "EE210"
title: "Title"
due: "2022-12-31T23:59:59Z"
url: "https://xkcd.com"
important: true
activityId: 1
}
}
ScrollView {
implicitHeight: PlasmaCore.Units.gridUnit * 20
Layout.margins: PlasmaCore.Units.smallSpacing
Layout.fillWidth: true
Layout.fillHeight: true
ScrollBar.horizontal.policy: ScrollBar.AlwaysOff
ListView {
Layout.fillWidth: true
delegate: ActivityView {}
model: assignmentsModel
}
}
PlasmaComponents3.Button {
icon.name: "view-refresh"
text: i18n("Refresh")
onClicked: syncCanvas()
}
}
}
|