@robertying/thu-learn-lib 0.0.0-stage → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +324 -2
  3. package/lib/auth.d.ts +11 -0
  4. package/lib/auth.d.ts.map +1 -0
  5. package/lib/auth.js +99 -0
  6. package/lib/auth.js.map +1 -0
  7. package/lib/calendar.d.ts +16 -0
  8. package/lib/calendar.d.ts.map +1 -0
  9. package/lib/calendar.js +81 -0
  10. package/lib/calendar.js.map +1 -0
  11. package/lib/contents.d.ts +9 -0
  12. package/lib/contents.d.ts.map +1 -0
  13. package/lib/contents.js +51 -0
  14. package/lib/contents.js.map +1 -0
  15. package/lib/courses.d.ts +11 -0
  16. package/lib/courses.d.ts.map +1 -0
  17. package/lib/courses.js +94 -0
  18. package/lib/courses.js.map +1 -0
  19. package/lib/discussions.d.ts +11 -0
  20. package/lib/discussions.d.ts.map +1 -0
  21. package/lib/discussions.js +56 -0
  22. package/lib/discussions.js.map +1 -0
  23. package/lib/favorites.d.ts +35 -0
  24. package/lib/favorites.d.ts.map +1 -0
  25. package/lib/favorites.js +150 -0
  26. package/lib/favorites.js.map +1 -0
  27. package/lib/files.d.ts +13 -0
  28. package/lib/files.d.ts.map +1 -0
  29. package/lib/files.js +167 -0
  30. package/lib/files.js.map +1 -0
  31. package/lib/homework.d.ts +17 -0
  32. package/lib/homework.d.ts.map +1 -0
  33. package/lib/homework.js +194 -0
  34. package/lib/homework.js.map +1 -0
  35. package/lib/index.d.ts +134 -0
  36. package/lib/index.d.ts.map +1 -0
  37. package/lib/index.js +219 -0
  38. package/lib/index.js.map +1 -0
  39. package/lib/notices.d.ts +14 -0
  40. package/lib/notices.d.ts.map +1 -0
  41. package/lib/notices.js +92 -0
  42. package/lib/notices.js.map +1 -0
  43. package/lib/questionnaires.d.ts +7 -0
  44. package/lib/questionnaires.d.ts.map +1 -0
  45. package/lib/questionnaires.js +62 -0
  46. package/lib/questionnaires.js.map +1 -0
  47. package/lib/request.d.ts +57 -0
  48. package/lib/request.d.ts.map +1 -0
  49. package/lib/request.js +197 -0
  50. package/lib/request.js.map +1 -0
  51. package/lib/types.d.ts +433 -0
  52. package/lib/types.d.ts.map +1 -0
  53. package/lib/types.js +98 -0
  54. package/lib/types.js.map +1 -0
  55. package/lib/urls.d.ts +78 -0
  56. package/lib/urls.d.ts.map +1 -0
  57. package/lib/urls.js +160 -0
  58. package/lib/urls.js.map +1 -0
  59. package/lib/user.d.ts +7 -0
  60. package/lib/user.d.ts.map +1 -0
  61. package/lib/user.js +22 -0
  62. package/lib/user.js.map +1 -0
  63. package/lib/utils.d.ts +16 -0
  64. package/lib/utils.d.ts.map +1 -0
  65. package/lib/utils.js +117 -0
  66. package/lib/utils.js.map +1 -0
  67. package/package.json +67 -4
  68. package/src/auth.ts +121 -0
  69. package/src/calendar.ts +102 -0
  70. package/src/contents.ts +63 -0
  71. package/src/courses.ts +111 -0
  72. package/src/discussions.ts +80 -0
  73. package/src/favorites.ts +167 -0
  74. package/src/files.ts +197 -0
  75. package/src/homework.ts +284 -0
  76. package/src/index.ts +345 -0
  77. package/src/notices.ts +115 -0
  78. package/src/questionnaires.ts +76 -0
  79. package/src/request.ts +214 -0
  80. package/src/types.ts +494 -0
  81. package/src/urls.ts +257 -0
  82. package/src/user.ts +28 -0
  83. package/src/utils.ts +125 -0
package/src/index.ts ADDED
@@ -0,0 +1,345 @@
1
+ import * as auth from './auth.ts';
2
+ import * as calendar from './calendar.ts';
3
+ import * as contents from './contents.ts';
4
+ import * as courses from './courses.ts';
5
+ import * as discussions from './discussions.ts';
6
+ import * as favorites from './favorites.ts';
7
+ import * as files from './files.ts';
8
+ import * as homework from './homework.ts';
9
+ import * as notices from './notices.ts';
10
+ import * as questionnaires from './questionnaires.ts';
11
+ import { Session } from './request.ts';
12
+ import type {
13
+ AllContentsOptions,
14
+ AllContentsResult,
15
+ CalendarOptions,
16
+ CalendarResult,
17
+ CourseListOptions,
18
+ ExcellentHomework,
19
+ HomeworkListOptions,
20
+ IHomeworkDetail,
21
+ NotificationListOptions,
22
+ RemoteFile,
23
+ CommentItem,
24
+ CourseInfo,
25
+ Discussion,
26
+ FavoriteItem,
27
+ File,
28
+ FileCategory,
29
+ HelperConfig,
30
+ Homework,
31
+ HomeworkTA,
32
+ IHomeworkSubmitAttachment,
33
+ Language,
34
+ Notification,
35
+ Question,
36
+ Questionnaire,
37
+ SemesterInfo,
38
+ UserInfo,
39
+ ContentType,
40
+ } from './types.ts';
41
+ import { CourseType } from './types.ts';
42
+ import * as URLS from './urls.ts';
43
+ import * as user from './user.ts';
44
+
45
+ export { addCSRFTokenToUrl } from './request.ts';
46
+ export * from './types.ts';
47
+
48
+ /** the main helper class */
49
+ export class Learn2018Helper {
50
+ readonly #session: Session;
51
+
52
+ /** you can provide a CookieJar and / or CredentialProvider in the configuration */
53
+ constructor(config?: HelperConfig) {
54
+ this.#session = new Session(config, () => auth.login(this.#session));
55
+ }
56
+
57
+ /** whether preview URLs open the first page only (`HelperConfig.generatePreviewUrlForFirstPage`) */
58
+ public get previewFirstPage(): boolean {
59
+ return this.#session.previewFirstPage;
60
+ }
61
+
62
+ public set previewFirstPage(value: boolean) {
63
+ this.#session.previewFirstPage = value;
64
+ }
65
+
66
+ /** fetch CSRF token from helper (invalid after login / re-login), might be '' if not logged in */
67
+ public getCSRFToken(): string {
68
+ return this.#session.csrfToken;
69
+ }
70
+
71
+ /** manually set CSRF token (useful when you want to reuse previous token) */
72
+ public setCSRFToken(csrfToken: string): void {
73
+ this.#session.csrfToken = csrfToken;
74
+ }
75
+
76
+ /**
77
+ * If using alternative cookie management systems,
78
+ * be sure to clear id.tsinghua.edu.cn cookies before calling this function
79
+ */
80
+ public async getRoamingTicket(
81
+ username: string,
82
+ password: string,
83
+ fingerPrint: string,
84
+ fingerGenPrint: string = '',
85
+ fingerGenPrint3: string = '',
86
+ ): Promise<string> {
87
+ return auth.getRoamingTicket(this.#session, username, password, fingerPrint, fingerGenPrint, fingerGenPrint3);
88
+ }
89
+
90
+ /**
91
+ * Login is necessary if you do not provide a `CredentialProvider`.
92
+ * Without credentials it uses the provider, and concurrent calls share one SSO login.
93
+ */
94
+ public async login(
95
+ username?: string,
96
+ password?: string,
97
+ fingerPrint?: string,
98
+ fingerGenPrint?: string,
99
+ fingerGenPrint3?: string,
100
+ ): Promise<void> {
101
+ if (!username || !password || !fingerPrint) return this.#session.relogin();
102
+ return auth.login(this.#session, username, password, fingerPrint, fingerGenPrint, fingerGenPrint3);
103
+ }
104
+
105
+ /**
106
+ * Make sure the session is usable without a full SSO login when possible: with a CSRF token (from an earlier login
107
+ * or `setCSRFToken`), make one cheap request and log in again only if the session has expired; without one, log in.
108
+ * Use it when resuming an app instead of `login()`.
109
+ */
110
+ public async ensureSession(): Promise<void> {
111
+ return this.#session.ensureSession(URLS.LEARN_CURRENT_SEMESTER());
112
+ }
113
+
114
+ /** logout (to make everyone happy) */
115
+ public async logout(): Promise<void> {
116
+ return auth.logout(this.#session);
117
+ }
118
+
119
+ /** get user's name and department */
120
+ public async getUserInfo(courseType = CourseType.STUDENT): Promise<UserInfo> {
121
+ return user.getUserInfo(this.#session, courseType);
122
+ }
123
+
124
+ /**
125
+ * Get calendar items during the specified period (in yyyymmdd format, both inclusive).
126
+ * Ranges longer than 28 days are fetched in windows and merged; windows that fail even after a retry are listed in
127
+ * `failedRanges`. Rejects only when every window fails (usually with `FailReason.INVALID_RESPONSE`).
128
+ */
129
+ public async getCalendar(startDate: string, endDate: string, options?: CalendarOptions): Promise<CalendarResult> {
130
+ return calendar.getCalendar(this.#session, startDate, endDate, options);
131
+ }
132
+
133
+ public async getSemesterIdList(): Promise<string[]> {
134
+ return courses.getSemesterIdList(this.#session);
135
+ }
136
+
137
+ public async getCurrentSemester(): Promise<SemesterInfo> {
138
+ return courses.getCurrentSemester(this.#session);
139
+ }
140
+
141
+ /**
142
+ * Get all courses in the specified semester.
143
+ * `timeAndLocation` is only fetched (one request per course) with `options.timeAndLocation`.
144
+ */
145
+ public async getCourseList(
146
+ semesterID: string,
147
+ courseType: CourseType = CourseType.STUDENT,
148
+ lang: Language = this.#session.lang,
149
+ options?: CourseListOptions,
150
+ ): Promise<CourseInfo[]> {
151
+ return courses.getCourseList(this.#session, semesterID, courseType, lang, options);
152
+ }
153
+
154
+ /** Get the time and location of one course. */
155
+ public async getCourseTimeAndLocation(courseID: string): Promise<string[]> {
156
+ return courses.getCourseTimeAndLocation(this.#session, courseID);
157
+ }
158
+
159
+ /**
160
+ * Get certain type of content of all specified courses.
161
+ * It wraps around the `getXXX` list functions; a course that fails is reported in `errors` instead of failing
162
+ * the rest. `options.details` is passed on as homework `details` or notification `attachments`.
163
+ */
164
+ public async getAllContents<T extends ContentType>(
165
+ courseIDs: string[],
166
+ type: T,
167
+ options?: AllContentsOptions<T>,
168
+ ): Promise<AllContentsResult<T>> {
169
+ return contents.getAllContents(this.#session, courseIDs, type, options);
170
+ }
171
+
172
+ /**
173
+ * Get all notifications (课程公告) of the specified course.
174
+ * Attachments are only fetched (one request per notice) for the notices `options.attachments` selects;
175
+ * use `getNotificationAttachment` for the others.
176
+ */
177
+ public async getNotificationList(
178
+ courseID: string,
179
+ courseType: CourseType = CourseType.STUDENT,
180
+ options?: NotificationListOptions,
181
+ ): Promise<Notification[]> {
182
+ return notices.getNotificationList(this.#session, courseID, courseType, options);
183
+ }
184
+
185
+ /** Get the attachment of a notice (one request), `undefined` if it has none. */
186
+ public async getNotificationAttachment(
187
+ notification: Pick<Notification, 'url' | 'attachmentName'>,
188
+ courseType: CourseType = CourseType.STUDENT,
189
+ ): Promise<RemoteFile | undefined> {
190
+ return notices.getNotificationAttachment(this.#session, notification, courseType);
191
+ }
192
+
193
+ /** Get all files (课程文件) of the specified course. */
194
+ public async getFileList(courseID: string, courseType: CourseType = CourseType.STUDENT): Promise<File[]> {
195
+ return files.getFileList(this.#session, courseID, courseType);
196
+ }
197
+
198
+ /** Get file categories of the specified course. */
199
+ public async getFileCategoryList(
200
+ courseID: string,
201
+ courseType: CourseType = CourseType.STUDENT,
202
+ ): Promise<FileCategory[]> {
203
+ return files.getFileCategoryList(this.#session, courseID, courseType);
204
+ }
205
+
206
+ /**
207
+ * Get all files of the specified category of the specified course.
208
+ * Note: this cannot get correct `visitCount` and `downloadCount` for student
209
+ */
210
+ public async getFileListByCategory(
211
+ courseID: string,
212
+ categoryId: string,
213
+ courseType: CourseType = CourseType.STUDENT,
214
+ ): Promise<File[]> {
215
+ return files.getFileListByCategory(this.#session, courseID, categoryId, courseType);
216
+ }
217
+
218
+ /**
219
+ * Get all homeworks (课程作业) of the specified course, with the excellent homework list (without details).
220
+ * Details (page + description, two requests per homework) are only fetched for the homework `options.details`
221
+ * selects; use `getHomeworkDetail` for the others.
222
+ */
223
+ public async getHomeworkList(courseID: string): Promise<Homework[]>;
224
+ public async getHomeworkList(
225
+ courseID: string,
226
+ courseType: CourseType.STUDENT,
227
+ options?: HomeworkListOptions,
228
+ ): Promise<Homework[]>;
229
+ public async getHomeworkList(courseID: string, courseType: CourseType.TEACHER): Promise<HomeworkTA[]>;
230
+ public async getHomeworkList(
231
+ courseID: string,
232
+ courseType: CourseType = CourseType.STUDENT,
233
+ options?: HomeworkListOptions,
234
+ ): Promise<Homework[] | HomeworkTA[]> {
235
+ return courseType === CourseType.TEACHER
236
+ ? homework.getHomeworkList(this.#session, courseID, CourseType.TEACHER)
237
+ : homework.getHomeworkList(this.#session, courseID, CourseType.STUDENT, options);
238
+ }
239
+
240
+ /** Get the detail (page content, attachments and description) of one homework. */
241
+ public async getHomeworkDetail(item: Pick<Homework, 'url' | 'baseId'>): Promise<IHomeworkDetail> {
242
+ return homework.getHomeworkDetail(this.#session, item);
243
+ }
244
+
245
+ /** Get the detail (page content, attachments and description) of one excellent homework. */
246
+ public async getExcellentHomeworkDetail(item: Pick<ExcellentHomework, 'url' | 'baseId'>): Promise<IHomeworkDetail> {
247
+ return homework.getHomeworkDetail(this.#session, item);
248
+ }
249
+
250
+ /** Get all discussions (课程讨论) of the specified course. */
251
+ public async getDiscussionList(courseID: string, courseType: CourseType = CourseType.STUDENT): Promise<Discussion[]> {
252
+ return discussions.getDiscussionList(this.#session, courseID, courseType);
253
+ }
254
+
255
+ /**
256
+ * Get all notifications (课程答疑) of the specified course.
257
+ * The student version supports only answered questions, while the teacher version supports all questions.
258
+ */
259
+ public async getAnsweredQuestionList(
260
+ courseID: string,
261
+ courseType: CourseType = CourseType.STUDENT,
262
+ ): Promise<Question[]> {
263
+ return discussions.getAnsweredQuestionList(this.#session, courseID, courseType);
264
+ }
265
+
266
+ /**
267
+ * Get all questionnaires (课程问卷/QNR) of the specified course.
268
+ */
269
+ public async getQuestionnaireList(courseID: string): Promise<Questionnaire[]> {
270
+ return questionnaires.getQuestionnaireList(this.#session, courseID);
271
+ }
272
+
273
+ /**
274
+ * Add an item to favorites. (收藏)
275
+ */
276
+ public async addToFavorites(type: ContentType, id: string): Promise<void> {
277
+ return favorites.addToFavorites(this.#session, type, id);
278
+ }
279
+
280
+ /**
281
+ * Remove an item from favorites. (取消收藏)
282
+ */
283
+ public async removeFromFavorites(id: string): Promise<void> {
284
+ return favorites.removeFromFavorites(this.#session, id);
285
+ }
286
+
287
+ /**
288
+ * Get favorites. (我的收藏)
289
+ * If `courseID` or `type` is specified, only return favorites of that course or type.
290
+ */
291
+ public async getFavorites(courseID?: string, type?: ContentType): Promise<FavoriteItem[]> {
292
+ return favorites.getFavorites(this.#session, courseID, type);
293
+ }
294
+
295
+ /**
296
+ * Pin a favorite item. (置顶)
297
+ */
298
+ public async pinFavoriteItem(id: string): Promise<void> {
299
+ return favorites.pinFavoriteItem(this.#session, id);
300
+ }
301
+
302
+ /**
303
+ * Unpin a favorite item. (取消置顶)
304
+ */
305
+ public async unpinFavoriteItem(id: string): Promise<void> {
306
+ return favorites.unpinFavoriteItem(this.#session, id);
307
+ }
308
+
309
+ /**
310
+ * Set a comment. (备注)
311
+ * Set an empty string to remove the comment.
312
+ */
313
+ public async setComment(type: ContentType, id: string, content: string): Promise<void> {
314
+ return favorites.setComment(this.#session, type, id, content);
315
+ }
316
+
317
+ /**
318
+ * Get comments. (我的备注)
319
+ * If `courseID` or `type` is specified, only return favorites of that course or type.
320
+ */
321
+ public async getComments(courseID?: string, type?: ContentType): Promise<CommentItem[]> {
322
+ return favorites.getComments(this.#session, courseID, type);
323
+ }
324
+
325
+ public async sortCourses(courseIDs: string[]): Promise<void> {
326
+ return courses.sortCourses(this.#session, courseIDs);
327
+ }
328
+
329
+ public async submitHomework(
330
+ id: string,
331
+ content = '',
332
+ attachment?: IHomeworkSubmitAttachment,
333
+ removeAttachment = false,
334
+ ): Promise<void> {
335
+ return homework.submitHomework(this.#session, id, content, attachment, removeAttachment);
336
+ }
337
+
338
+ public async setLanguage(lang: Language): Promise<void> {
339
+ return user.setLanguage(this.#session, lang);
340
+ }
341
+
342
+ public getCurrentLanguage(): Language {
343
+ return this.#session.lang;
344
+ }
345
+ }
package/src/notices.ts ADDED
@@ -0,0 +1,115 @@
1
+ // Course notifications (课程公告).
2
+
3
+ import { Base64 } from 'js-base64';
4
+
5
+ import type { Session } from './request.ts';
6
+ import { $, YES } from './request.ts';
7
+ import type { INotification, Notification, NotificationListOptions, RemoteFile } from './types.ts';
8
+ import { ContentType, CourseType, FailReason } from './types.ts';
9
+ import * as URLS from './urls.ts';
10
+ import { decodeHTML, isSelected, trimAndDefine } from './utils.ts';
11
+
12
+ /**
13
+ * Get all notifications (课程公告) of the specified course: current and expired lists.
14
+ * Attachments only for the notices `options.attachments` selects.
15
+ */
16
+ export async function getNotificationList(
17
+ s: Session,
18
+ courseID: string,
19
+ courseType: CourseType = CourseType.STUDENT,
20
+ options: NotificationListOptions = {},
21
+ ): Promise<Notification[]> {
22
+ const lists = await Promise.all([
23
+ getNotificationListKind(s, courseID, courseType, false),
24
+ getNotificationListKind(s, courseID, courseType, true),
25
+ ]);
26
+ return Promise.all(
27
+ lists.flat().map(async (n): Promise<Notification> => {
28
+ if (!n.attachmentName || !isSelected(options.attachments, n)) return n;
29
+ return { ...n, attachment: await getNotificationAttachment(s, n, courseType) };
30
+ }),
31
+ );
32
+ }
33
+
34
+ async function getNotificationListKind(
35
+ s: Session,
36
+ courseID: string,
37
+ courseType: CourseType,
38
+ expired: boolean,
39
+ ): Promise<INotification[]> {
40
+ const json = await (
41
+ await s.fetchWithToken(URLS.LEARN_NOTIFICATION_LIST(courseType, expired), {
42
+ method: 'POST',
43
+ body: URLS.LEARN_PAGE_LIST_FORM_DATA(courseID),
44
+ })
45
+ ).json();
46
+ if (json.result !== 'success') {
47
+ return Promise.reject({
48
+ reason: FailReason.INVALID_RESPONSE,
49
+ extra: json,
50
+ });
51
+ }
52
+
53
+ const result = (json.object?.aaData ?? json.object?.resultsList ?? []) as any[];
54
+
55
+ return result.map((n) => {
56
+ const attachmentName: unknown = courseType === CourseType.STUDENT ? n.fjmc : n.fjbt;
57
+ return {
58
+ id: n.ggid,
59
+ content: Base64.decode(n.ggnr ?? ''),
60
+ title: decodeHTML(n.bt),
61
+ url: URLS.LEARN_NOTIFICATION_DETAIL(courseID, n.ggid, courseType),
62
+ publisher: n.fbrxm,
63
+ hasRead: n.sfyd === YES,
64
+ markedImportant: Number(n.sfqd) === 1, // n.sfqd could be string '1' (teacher mode) or number 1 (student mode)
65
+ publishTime: n.fbsj && typeof n.fbsj === 'string' ? n.fbsj : n.fbsjStr,
66
+ expireTime: n.jzsj ?? undefined,
67
+ isFavorite: n.sfsc === YES,
68
+ comment: n.bznr ?? undefined,
69
+ attachmentName: typeof attachmentName === 'string' && attachmentName ? attachmentName : undefined,
70
+ } satisfies INotification;
71
+ });
72
+ }
73
+
74
+ /**
75
+ * Get the attachment of a notice from its detail page (1 request), or `undefined` without a request if
76
+ * `attachmentName` says it has none.
77
+ */
78
+ export async function getNotificationAttachment(
79
+ s: Session,
80
+ notification: Pick<INotification, 'url' | 'attachmentName'>,
81
+ courseType: CourseType = CourseType.STUDENT,
82
+ ): Promise<RemoteFile | undefined> {
83
+ if (!notification.attachmentName) return undefined;
84
+ /// from JSON (backup, currently not used)
85
+ // const postParams = new FormData();
86
+ // postParams.append('id', id);
87
+ // postParams.append('wlkcid', courseID);
88
+ // const metadata = await (await s.fetchWithToken(URL.LEARN_NOTIFICATION_EDIT(courseType), {
89
+ // 'method': 'POST',
90
+ // 'body': postParams,
91
+ // })).json();
92
+ // const attachmentId = metadata.ggfjid as string;
93
+ /// parsed from HTML
94
+ const response = await s.fetchWithToken(notification.url);
95
+ const result = $(await response.text());
96
+ let path = '';
97
+ if (courseType === CourseType.STUDENT) {
98
+ path = result('.ml-10').attr('href')!;
99
+ } else {
100
+ path = result('#wjid').attr('href')!;
101
+ }
102
+ const size = trimAndDefine(result('div#attachment > div.fl > span[class^="color"]').first().text())!;
103
+ const params = new URLSearchParams(path.split('?').slice(-1)[0]);
104
+ const attachmentId = params.get('wjid')!;
105
+ if (!path.startsWith(URLS.LEARN_PREFIX)) {
106
+ path = URLS.LEARN_PREFIX + path;
107
+ }
108
+ return {
109
+ name: notification.attachmentName,
110
+ id: attachmentId,
111
+ downloadUrl: path,
112
+ previewUrl: URLS.LEARN_FILE_PREVIEW(ContentType.NOTIFICATION, attachmentId, courseType, s.previewFirstPage),
113
+ size,
114
+ };
115
+ }
@@ -0,0 +1,76 @@
1
+ // Questionnaires (课程问卷).
2
+
3
+ import type { Session } from './request.ts';
4
+ import { YES } from './request.ts';
5
+ import type { Questionnaire, QuestionnaireDetail } from './types.ts';
6
+ import { FailReason, QuestionnaireType } from './types.ts';
7
+ import * as URLS from './urls.ts';
8
+ import { QNR_TYPE_MAP, decodeHTML } from './utils.ts';
9
+
10
+ /**
11
+ * Get all questionnaires (课程问卷/QNR) of the specified course.
12
+ */
13
+ export async function getQuestionnaireList(s: Session, courseID: string): Promise<Questionnaire[]> {
14
+ return Promise.all([
15
+ getQuestionnaireListAtUrl(s, courseID, URLS.LEARN_QNR_LIST_ONGOING),
16
+ getQuestionnaireListAtUrl(s, courseID, URLS.LEARN_QNR_LIST_ENDED),
17
+ ]).then((r) => r.flat());
18
+ }
19
+
20
+ async function getQuestionnaireListAtUrl(s: Session, courseID: string, url: string): Promise<Questionnaire[]> {
21
+ const json = await (
22
+ await s.fetchWithToken(url, { method: 'POST', body: URLS.LEARN_PAGE_LIST_FORM_DATA(courseID) })
23
+ ).json();
24
+ if (json.result !== 'success') {
25
+ return Promise.reject({
26
+ reason: FailReason.INVALID_RESPONSE,
27
+ extra: json,
28
+ });
29
+ }
30
+ const result = (json.object?.aaData ?? []) as any[];
31
+ return Promise.all(
32
+ result.map(async (e) => {
33
+ const type = QNR_TYPE_MAP.get(e.wjlx) ?? QuestionnaireType.SURVEY;
34
+ return {
35
+ id: e.wjid,
36
+ type,
37
+ title: decodeHTML(e.wjbt),
38
+ startTime: e.kssj,
39
+ endTime: e.jssj,
40
+ uploadTime: e.scsj,
41
+ uploaderId: e.scr,
42
+ uploaderName: e.scrxm,
43
+ submitTime: e.tjsj ? e.tjsj : undefined,
44
+ isFavorite: e.sfsc === YES,
45
+ comment: e.bznr ?? undefined,
46
+ url: URLS.LEARN_QNR_SUBMIT_PAGE(e.wlkcid, e.wjid, type),
47
+ detail: await getQuestionnaireDetail(s, courseID, e.wjid),
48
+ } satisfies Questionnaire;
49
+ }),
50
+ );
51
+ }
52
+
53
+ async function getQuestionnaireDetail(s: Session, courseID: string, qnrID: string): Promise<QuestionnaireDetail[]> {
54
+ const json = await (
55
+ await s.fetchWithToken(URLS.LEARN_QNR_DETAIL, {
56
+ method: 'POST',
57
+ body: URLS.LEARN_QNR_DETAIL_FORM(courseID, qnrID),
58
+ })
59
+ ).json();
60
+ return (json as any[]).map(
61
+ (e) =>
62
+ ({
63
+ id: e.wtid,
64
+ index: Number(e.wtbh),
65
+ type: e.type,
66
+ required: e.require === YES,
67
+ title: decodeHTML(e.wtbt),
68
+ score: e.wtfz ? Number(e.wtfz) : undefined, // unsure about original type
69
+ options: (e.list as any[])?.map((o) => ({
70
+ id: o.xxid,
71
+ index: Number(o.xxbh),
72
+ title: decodeHTML(o.xxbt),
73
+ })),
74
+ }) satisfies QuestionnaireDetail,
75
+ );
76
+ }