/
index.ts
409 lines (338 loc) · 13.7 KB
/
index.ts
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import AsyncStorage from '@react-native-async-storage/async-storage';
// Wrapper types.
import type { PapillonUser } from './types/user';
import type { PapillonNews } from './types/news';
import type { PapillonGrades } from './types/grades';
import type { PapillonLesson } from './types/timetable';
import type { PapillonHomework } from './types/homework';
import type { PapillonDiscussion, PapillonDiscussionMessage, PapillonRecipient } from './types/discussions';
// Pronote related imports.
import { Pronote } from 'pawnote';
import { loadPronoteConnector } from './PronoteData/connector';
import { userHandler as pronoteUserHandler } from './PronoteData/user';
import { gradesHandler as pronoteGradesHandler } from './PronoteData/grades';
import { timetableHandler as pronoteTimetableHandler } from './PronoteData/timetable';
import { evaluationsHandler as pronoteEvaluationsHandler } from './PronoteData/evaluations';
import { vieScolaireHandler as pronoteVieScolaireHandler } from './PronoteData/vie_scolaire';
import { newsHandler as pronoteNewsHandler, newsStateHandler as pronoteNewsStateHandler } from './PronoteData/news';
import { homeworkPatchHandler as pronoteHomeworkPatchHandler, homeworkHandler as pronoteHomeworkHandler, homeworkUploadFileHandler as pronoteHomeworkUploadFileHandler, homeworkRemoveFileHandler as pronoteHomeworkRemoveFileHandler } from './PronoteData/homework';
import { discussionsHandler as pronoteDiscussionsHandler, discussionsRecipientsHandler as pronoteDiscussionsRecipientsHandler, discussionsCreationRecipientsHandler as pronoteDiscussionsCreationRecipientsHandler, discussionsCreationHandler as pronoteDiscussionsCreationHandler, discussionReplyHandler as pronoteDiscussionReplyHandler } from './PronoteData/discussions';
// Skolengo related imports.
import type { SkolengoDatas } from './SkolengoData/SkolengoDatas';
import { PapillonVieScolaire } from './types/vie_scolaire';
import { PapillonEvaluation } from './types/evaluations';
// EcoleDirecte related imports.
import { EDCore } from '@papillonapp/ed-core';
import { loadEcoleDirecteConnector } from './EcoleDirecteData/connector';
import { userInformations as EcoleDirecteUser } from './EcoleDirecteData/user';
import { EDtimetableHandler as EcoleDirecteTimetable } from './EcoleDirecteData/timetable';
import { EDvieScolaireHandler } from './EcoleDirecteData/vie_scolaire';
import { Alert } from 'react-native';
export type ServiceName = 'pronote' | 'skolengo' | 'ecoledirecte';
export class IndexDataInstance {
public initialized = false;
public initializing = false;
public isNetworkFailing = false;
public service?: ServiceName;
public skolengoInstance?: SkolengoDatas;
public pronoteInstance?: Pronote;
public ecoledirecteInstance?: EDCore;
/**
* Internal function that waits for the initialization
* of the service to be finished so we can use it.
*/
private async waitInit(): Promise<boolean> {
if (this.initialized) return true;
if (!this.initializing) {
if (this.service === 'skolengo' && !this.skolengoInstance) {
await this.init('skolengo');
}
else if (this.service === 'pronote' && !this.pronoteInstance) {
await this.init('pronote');
} else if (this.service === 'ecoledirecte' && !this.ecoledirecteInstance) {
await this.init('ecoledirecte');
}
}
return new Promise((resolve) => {
const interval = setInterval(() => {
if (!this.initializing) {
clearInterval(interval);
resolve(true);
}
}, 250);
});
}
public async init (service: 'pronote' | 'skolengo' | 'ecoledirecte', instance?: Pronote | EDCore): Promise<void> {
if (this.initializing) return;
this.service = service;
if (!this.service) return;
if (this.initialized) { // ...was already initialized.
// Prevent to do any more presence request.
if (this.pronoteInstance) this.pronoteInstance.stopPresenceRequests();
this.initialized = false;
}
this.initializing = true;
if (this.service === 'skolengo') {
const skolengo = await import('./SkolengoData/SkolengoDatas.js');
this.skolengoInstance = await skolengo.SkolengoDatas.initSkolengoDatas();
// TODO: Let's say for now that it never fails...
this.isNetworkFailing = false;
this.initialized = this.skolengoInstance ? true : false;
} else if(this.service === 'ecoledirecte') {
if (instance && instance instanceof EDCore) this.ecoledirecteInstance = instance;
else {
try {
const connector = await loadEcoleDirecteConnector();
if (connector) this.ecoledirecteInstance = connector;
this.isNetworkFailing = false;
} catch {
this.isNetworkFailing = true;
}
}
this.initialized = this.ecoledirecteInstance ? true : false;
}
else if (this.service === 'pronote') {
if (instance && instance instanceof Pronote) this.pronoteInstance = instance;
else {
try {
const connector = await loadPronoteConnector();
if (connector) this.pronoteInstance = connector;
this.isNetworkFailing = false;
} catch { // any error not handled in the connector; so the network fail.
this.isNetworkFailing = true;
}
}
this.initialized = this.pronoteInstance ? true : false;
}
this.initializing = false;
}
public async getGrades (selectedPeriodName: string, force = false): Promise<PapillonGrades | null> {
let grades: PapillonGrades | null | undefined;
await this.waitInit();
if (this.service === 'skolengo') {
// TODO: Typings and stuff for Skolengo.
// grades = await this.skolengoInstance.getGrades(selectedPeriodName, force) as unknown as PapillonGrades;
}
else if (this.service === 'pronote') {
grades = await pronoteGradesHandler(selectedPeriodName, this.pronoteInstance, force);
}
if (!grades) {
throw new Error('Unknown service.');
}
return grades;
}
public async getEvaluations (selectedPeriodName: string, force = false): Promise<PapillonEvaluation[] | null>{
await this.waitInit();
if (this.service === 'skolengo') {
// TODO: Skolengo Evaluation
}
else if (this.service === 'pronote') {
return pronoteEvaluationsHandler(selectedPeriodName, this.pronoteInstance, force);
}
return [];
}
/**
* Get all the homeworks from first day to end of the year.
*/
public async getHomeworks (force = false): Promise<PapillonHomework[]> {
await this.waitInit();
if (this.service === 'skolengo') {
// TODO
// return this.skolengoInstance.getHomeworks(day, force, day2) || [];
}
else if (this.service === 'pronote') {
return pronoteHomeworkHandler(force, this.pronoteInstance);
}
return [];
}
public async changeHomeworkState (homework: PapillonHomework, isDone: boolean): Promise<boolean> {
await this.waitInit();
if (this.service === 'skolengo') {
// TODO
// return this.skolengoInstance.patchHomeworkAssignment(id, isDone);
}
else if (this.service === 'pronote') {
return pronoteHomeworkPatchHandler(homework, isDone, this.pronoteInstance);
}
return false;
}
public async getNews (force = false): Promise<PapillonNews[]> {
await this.waitInit();
if (this.service === 'skolengo') {
// return this.skolengoInstance.getNews(force);
}
else if (this.service === 'pronote') {
return pronoteNewsHandler(force, this.pronoteInstance);
}
return [];
}
/**
* Change the `read` state of a news.
* Applies to information and surveys.
*
* @returns `true` when operation is successful.
*/
public async changeNewsState (localID: string, forceState: boolean = true): Promise<boolean> {
await this.waitInit();
if (this.service === 'skolengo') {
// TODO
}
else if (this.service === 'pronote') {
return pronoteNewsStateHandler(localID, forceState, this.pronoteInstance);
}
return false;
}
public async getUniqueNews (id: string, force = false): Promise<PapillonNews> {
await this.waitInit();
if (this.service === 'skolengo') {
// TODO
// return this.skolengoInstance.getUniqueNews(id, force);
}
throw new Error('Method only works for Skolengo');
}
/**
* Get a list of user's lessons for a week.
* The week is calculated from the given day.
* A week goes from the Monday to the Sunday.
*
* If the user is offline and/or the cache fails, an
* empty list is returned.
*/
public async getTimetable (day: Date, force = false): Promise<PapillonLesson[]> {
await this.waitInit();
// JS dates are starting from Sunday, we do `+1` to be on Monday;
const mondayIndex = day.getDate() - day.getDay() + 1;
const sundayIndex = mondayIndex + 6;
const monday = new Date(day);
monday.setDate(mondayIndex);
monday.setHours(0, 0, 0, 0);
const sunday = new Date(day);
sunday.setDate(sundayIndex);
sunday.setHours(23, 59, 59, 999);
if (this.service === 'skolengo') {
return this.skolengoInstance!.getTimetable(day, force);
}
else if (this.service === 'pronote') {
const timetable = await pronoteTimetableHandler([monday, sunday], this.pronoteInstance, force);
if (timetable) return timetable;
} else if(this.service === 'ecoledirecte') {
const timetable = await EcoleDirecteTimetable([monday, sunday], this.ecoledirecteInstance, force);
if(timetable) return timetable;
}
return [];
}
/**
* Retrieves information about the user and also
* about the school and periods.
*/
public async getUser (force = false): Promise<PapillonUser> {
await this.waitInit();
let user: PapillonUser | undefined | null;
if (this.service === 'skolengo') {
// TODO: Implement typings for skolengo.
// user = await this.skolengoInstance.getUser(force);
}
else if (this.service === 'pronote') {
user = await pronoteUserHandler(this.pronoteInstance, force);
} else if(this.service === 'ecoledirecte') {
user = await EcoleDirecteUser(this.ecoledirecteInstance, force);
}
if (!user) {
throw Alert.alert('Aucun cache n\'a été trouvé, il se peut que vous avez été déconnecté de votre session.');
}
return runUserMiddleware(user);
}
public async getViesco(force = false): Promise<PapillonVieScolaire> {
await this.waitInit();
if (this.service === 'skolengo') {
// TODO
// return this.skolengoInstance.getViesco(force);
}
else if (this.service === 'pronote') {
return pronoteVieScolaireHandler(this.pronoteInstance, force);
} else if (this.service === 'ecoledirecte') {
return EDvieScolaireHandler(this.ecoledirecteInstance, force);
}
return { absences: [], delays: [], punishments: [], observations: [] } as PapillonVieScolaire;
}
/**
* Get every conversation registered.
* Also get the recipients of each conversation and read all the messages.
*/
public async getConversations (): Promise<PapillonDiscussion[]> {
await this.waitInit();
if (this.service === 'pronote') {
return pronoteDiscussionsHandler(this.pronoteInstance);
}
return [];
}
/**
* Reply to a conversation and returns the
* whole conversation with updated messages.
*/
async replyToConversation (localDiscussionID: string, messageContent: string): Promise<PapillonDiscussionMessage[] | null> {
await this.waitInit();
if (this.service === 'pronote') {
return pronoteDiscussionReplyHandler(localDiscussionID, messageContent, this.pronoteInstance);
}
return null;
}
/**
* Create a discussion with the given `subject` and `content` as first message.
* If possible, add `recipients` to the discussion.
*/
async createDiscussion (subject: string, content: string, recipients: PapillonRecipient[]): Promise<void> {
await this.waitInit();
if (this.service === 'pronote') {
await pronoteDiscussionsCreationHandler(subject, content, recipients, this.pronoteInstance);
}
}
/**
* Get the recipients attached to a discussion
* using the local ID of it.
*/
public async getRecipients (localDiscussionID: string): Promise<string[]> {
await this.waitInit();
if (this.service === 'pronote') {
return pronoteDiscussionsRecipientsHandler(localDiscussionID, this.pronoteInstance);
}
return [];
}
/**
* Get the recipients that can be added to a new discussion.
*/
public async getCreationRecipients (): Promise<PapillonRecipient[]> {
await this.waitInit();
if (this.service === 'pronote') {
return pronoteDiscussionsCreationRecipientsHandler(this.pronoteInstance);
}
return [];
}
public async uploadHomework (homework: PapillonHomework, file: { name: string, uri: string, size: number, type: string }): Promise<void> {
await this.waitInit();
if (this.service === 'pronote') {
return pronoteHomeworkUploadFileHandler(homework, file, this.pronoteInstance);
}
}
public async removeUploadedHomework (homework: PapillonHomework): Promise<void> {
await this.waitInit();
if (this.service === 'pronote') {
return pronoteHomeworkRemoveFileHandler(homework, this.pronoteInstance);
}
}
}
/**
* Middleware mainly here to add the custom
* profile picture and custom name from settings.
*/
const runUserMiddleware = async (user: PapillonUser): Promise<PapillonUser> => {
const customProfilePictureB64 = await AsyncStorage.getItem('custom_profile_picture');
if (customProfilePictureB64) {
user.profile_picture = customProfilePictureB64;
}
const customName = await AsyncStorage.getItem('custom_name');
if (customName) {
user.name = customName;
}
return user;
};