eduapi-port 0.1.0

A library for unofficial API for EduPage.
Documentation
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
const debug = require("debug")("edupage:log");
const error = require("debug")("edupage:error");
const RawData = require("../lib/RawData");
const {iterate} = require("../lib/utils");
const Attachment = require("./Attachment");
const Class = require("./Class");
const Edupage = require("./Edupage");
const {ENDPOINT, ENTITY_TYPE, API_STATUS, TIMELINE_ITEM_TYPE} = require("./enums");
const {APIError, EdupageError, MessageError} = require("./exceptions");
const Parent = require("./Parent");
const Plan = require("./Plan");
const Student = require("./Student");
const Teacher = require("./Teacher");
const User = require("./User");
const Assignment = require("./Assignment");

debug.log = console.log.bind(console);

/**
 * @typedef {import("../lib/RawData").RawDataObject} RawDataObject
 */

class Message extends RawData {
	/**
	 * Creates an instance of Message.
	 * @param {RawDataObject} [data={}] Raw data to initialize the instance with.
	 * @param {Edupage} [edupage=null] Edupage instance to use.
	 * @memberof Message
	 */
	constructor(data = {}, edupage = null) {
		super(data);

		if(!data.data) data.data = {};
		if(typeof data.data === "string") data.data = JSON.parse(data.data) || {};

		/**
		 * Edupage instance associated to this object.
		 * @type {Edupage}
		 */
		this.edupage = edupage;


		/**
		 * Timeline item id
		 * @type {string}
		 */
		this.id = data.timelineid;

		/**
		 * Timeline item type
		 * @type {string}
		 */
		this.type = data.typ;

		/**
		 * Date when the message was created
		 * @type {Date}
		 */
		this.creationDate = data.cas_pridania ? new Date(data.cas_pridania) : null;

		/**
		 * Date when the message was last updated
		 * @type {Date}
		 */
		this.timelineDate = data.timestamp ? new Date(data.timestamp) : null;

		/**
		 * ID of the user who created the message
		 * @type {string}
		 */
		this.otherId = data.otherId || null;

		/**
		 * Number of replies to the message
		 * @type {number}
		 */
		this.repliesCount = +data.pocet_reakcii || 0;

		/**
		 * Date of the last reply to the message
		 * @type {Date}
		 */
		this.lastReplyDate = data.posledna_reakcia ? new Date(data.posledna_reakcia) : null;

		/**
		 * Flag indicating whether the message was removed
		 * @type {boolean}
		 */
		this.isRemoved = !!+data.removed;

		/**
		 * Flag indicating whether the message is reply
		 * @type {boolean}
		 */
		this.isReply = !!this._data.data.textReply;

		/**
		 * Flag indicating whether the message is sent as important
		 * @type {boolean}
		 */
		this.isImportant = !!+data.data.receipt || !!+data.data.importantReply;

		/**
		 * Date when the currently logged in user read the message. If the message is not important, this is same as `Message.creationDate`.
		 * @type {Date}
		 */
		this.seenDate = this.isImportant ? (data.data.myConfirmations?.receipt ? new Date(data.data.myConfirmations?.receipt) : null) : this.creationDate;

		/**
		 * Flag indicating whether the message was seen. If the message is not important, this will be always `true`.
		 * @type {boolean}
		 */
		this.isSeen = !!this.seenDate;

		/**
		 * Date when the currently logged in user liked the message.
		 * @type {Date}
		 */
		this.likedDate = data.data.myConfirmations?.like ? new Date(data.data.myConfirmations?.like) : null;

		/**
		 * Flag indicating whether the message was liked.
		 * @type {boolean}
		 */
		this.isLiked = !!this.likedDate;

		/**
		 * Date when the currently logged in user marked this message as done.
		 * @type {Date}
		 */
		this.doneDate = null;

		/**
		 * Flag indicating whether the message was marked as done.
		 * @type {boolean}
		 */
		this.isDone = false;

		/**
		 * Flag indicating whether the message was starred.
		 * @type {boolean}
		 */
		this.isStarred = false;

		/**
		 * Number of likes the message has.
		 * @type {number}
		 */
		this.likes = data.data.confirmations?.like || 0;

		/**
		 * The textual content of the message.
		 * @type {string}
		 */
		this.text = data.data.messageContent || data.text;

		/**
		 * Title of the message.
		 * @type {string}
		 */
		this.title = data.user_meno;


		/**
		 * Number of participants in the message.
		 * ! WARNING: This property is only accessible after calling `message.refresh()`!
		 * @type {number}
		 */
		this.participantsCount = null;

		/**
		 * List of participants in the message.
		 * ! WARNING: This property is only accessible after calling `message.refresh()`!
		 * @type {(User | Teacher | Student | Parent)[]}
		 */
		this.participants = [];

		/**
		 * List of users who liked the message.
		 * ! WARNING: This property is only accessible after calling `message.refresh()`!
		 * @type {({user: User | Teacher | Student | Parent, date: Date})[]}
		 */
		this.likedBy = [];

		/**
		 * List of users who have seen the message.
		 * ! WARNING: This property is only accessible after calling `message.refresh()`!
		 * @type {({user: User | Teacher | Student | Parent, date: Date})[]}
		 */
		this.seenBy = [];


		/**
		 * Author of the message.
		 * @type {User | Teacher | Student | Parent}
		 */
		this.owner = null;

		/**
		 * Recipient of the message.
		 * @type {User | Teacher | Student | Parent | Plan | Class}
		 */
		this.recipient = null;

		/**
		 * Recipient of the message as user string. This can be used, when the exact recipient is not known (e.g. when the recipient is everyone, this will be '*').
		 * @type {string}
		 */
		this.recipientUserString = null;

		/**
		 * Flag indicating whether the message has no exact recipient (e.g. userstring contains '*').
		 * @type {boolean}
		 */
		this.isWildcardRecipient = false;

		/**
		 * Root message of the reply.
		 * @type {Message}
		 */
		this.replyOf = null;


		/**
		 * Resources attached to the message.
		 * @type {Attachment[]}
		 */
		this.attachments = [];

		/**
		 * List of replies to the message.
		 * @type {Message[]}
		 */
		this.replies = [];

		/**
		 * Assignment attached to the message.
		 * @type {Assignment}
		 */
		this.assignment = null;

		if(this.edupage) Message.prototype.init.call(this);
	}

	/**
	 * Initializes instance.
	 * @param {Edupage} [edupage=null] Edupage instance to use.
	 * @memberof Message
	 */
	init(edupage = null) {
		if(edupage) this.edupage = edupage;

		//Update attributes
		if("item" in this._data) {
			const data = this._data.item;

			this.timelineDate = data.timestamp ? new Date(data.timestamp) : null;
			this.repliesCount = +data.pocet_reakcii || 0;
			this.lastReplyDate = data.posledna_reakcia ? new Date(data.posledna_reakcia) : null;
			this.isRemoved = !!+data.removed;
			this.seenDate = this.isImportant ? (this._data.reakcie[0]?.data?.myConfirmations?.receipt ? new Date(this._data.reakcie[0]?.data?.myConfirmations?.receipt) : null) : this.creationDate;
			this.isSeen = !!this.seenDate;
			this.likedDate = this._data.reakcie[0]?.data?.myConfirmations?.like ? new Date(this._data.reakcie[0]?.data?.myConfirmations?.like) : null;
			this.isLiked = !!this.likedDate;
			this.likes = data.data.confirmations?.like || 0;
		}

		//Add more data
		if("numParticipants" in this._data) this.participantsCount = this._data.numParticipants;
		if("participants" in this._data) this.participants = iterate(this._data.participants).map(([i, user, data]) => this.edupage.getUserByUserString(user) || User.from(user, data, this.edupage));
		if("reakcie" in this._data) {
			this._data.reakcie
				.forEach(e => {
					if(e.pomocny_zaznam) return;
					if(e.timelineid == this._data.item?.timelineid) return;
					if(e.cas_pridania == this._data.cas_pridania) return;
					if(e.typ == TIMELINE_ITEM_TYPE.CONFIRMATION) {
						const data = Message.parseUsername(e.vlastnik_meno);
						const user = this.edupage.getUserByUserString(e.vlastnik) || User.from(e.vlastnik, data, this.edupage);

						if(e.data.like) this.likedBy.push({user, date: new Date(e.data.like)});
						if(this.isImportant) this.seenBy.push({user, date: new Date(e.cas_pridania)});
					} else {
						if(this.replies.some(t => t.id == e.timelineid)) return;

						//After init it should get pushed automatically
						const msg = new Message(e, this.edupage);

						//If it wasn't pushed into replies, push it manually
						if(!this.replies.includes(msg)) this.replies.push(msg);
					}
				});

			this.repliesCount = this.replies.length;
		}

		//Setup flags
		const props = this._data.userProps || this.edupage._data.timelineUserProps[this.id] || {};
		this.doneDate = props.doneMaxCas ? new Date(props.doneMaxCas) : null;
		this.isDone = !!this.doneDate;
		this.isStarred = !!+props.starred;

		//Add owner object
		if(this._data.vlastnik) {
			//Parse name of the owner
			const data = Message.parseUsername(this._data.vlastnik_meno);

			//Assign owner object
			this.owner = this.edupage.getUserByUserString(this._data.vlastnik) ||
				User.from(this._data.vlastnik, data, this.edupage);
		}

		//Add recipient object
		if(this._data.user) {
			const {recipient, wildcard} = this.getRecipient(this._data.user);

			this.recipient = recipient;
			this.isWildcardRecipient = wildcard;
			this.recipientUserString = this._data.user;
		}

		//Add reply
		if(this._data.reakcia_na && this._data.typ != TIMELINE_ITEM_TYPE.CONFIRMATION) {
			//Find root message
			const message = this.edupage.timelineItems.find(e => e.id == this._data.reakcia_na);

			if(message) {
				message.replies.push(this);
				this.replyOf = message;
			} else {
				//Cannot find root message
			}
		}

		//Add attachments
		if(this._data.data.attachements) {
			const attchs = this._data.data.attachements;
			this.attachments = Object.keys(attchs).map(e => new Attachment({src: e, name: attchs[e]}, this.edupage));
		}

		//Add homework
		if(this._data.data.superid) {
			this.assignment = this.edupage.assignments.find(e => e.superId == this._data.data.superid);
		}

		//TODO: implement this._data.participantGroups = {
		//TODO: 	hasParticipants: 1
		//TODO: 	id: "CustPlan3610"
		//TODO: 	name: "Telesná a športová výchova · I.BP - I"
		//TODO: 	spec: "Rodičia a žiaci"
		//TODO: 	type: "group"
		//TODO: }[]

	}

	/**
	 * @typedef {Object} MessageReplyOptions
	 * @prop {string} text
	 * @prop {User | Teacher | Student | Parent} [recipient=null]
	 * @prop {boolean} [parents=false]
	 * @prop {Attachment[]} [attachments=[]]
	 */

	/**
	 * Creates a new reply to the message
	 * @param {MessageReplyOptions} options
	 * @memberof Message
	 */
	async reply(options) {
		const {
			text,
			recipient = null,
			parents = false,
			attachments = []
		} = options;

		const res = await this.edupage.api({
			url: ENDPOINT.TIMELINE_CREATE_REPLY,
			data: {
				"groupid": this.id,
				"recipient": this.isReply
					? this.owner.getUserString(parents)
					: (recipient ? recipient.getUserString(parents) : ""),
				"text": text,
				"moredata": JSON.stringify({attachements: attachments})
			}
		});

		//Request failed
		if(res.status !== API_STATUS.OK) {
			error(`[Reply] Received invalid status from the server '${res.status}'`);
			throw new APIError(`Failed to send message: Invalid status received '${res.status}'`, res);
		}

		//Add reply to root message
		const _len = this.replies.length;
		this._data.reakcie = res.data.reakcie;
		Message.prototype.init.call(this);

		//Check for successful creation of the reply
		if(this.replies.length > _len) {
			if(this.replies.length > _len + 1) debug(`[Reply] Message contains multiple new replies (${_len} -> ${this.replies.length})`);
			return this.replies[this.replies.length - 1];
		} else {
			error(`[Reply] Message had ${_len} replies, and now has ${this.replies.length}`, res, this.replies);
			throw new MessageError(`Failed to get sent message: No new replies were created`);
		}
	}

	/**
	 * Marks the message as liked
	 * @param {boolean} [state=true] State to use (like/unlike)
	 * @return {Promise<boolean>} 
	 * @memberof Message
	 */
	async markAsLiked(state = true) {
		if(!this.edupage) throw new EdupageError(`Message does not have assigned Edupage instance yet`);

		//Load data
		const res = await this.edupage.api({
			url: ENDPOINT.TIMELINE_CREATE_CONFIRMATION,
			data: {
				"groupid": this.id,
				"confirmType": "like",
				"val": (+state).toString()
			}
		});

		await this.refresh(res);

		return state;
	}

	/**
	 * Marks the message as seen
	 * @return {Promise<void>} 
	 * @memberof Message
	 */
	async markAsSeen() {
		if(!this.edupage) throw new EdupageError(`Message does not have assigned Edupage instance yet`);

		//Load data
		const res = await this.edupage.api({
			url: ENDPOINT.TIMELINE_CREATE_CONFIRMATION,
			data: {
				"groupid": this.id,
				"confirmType": "receipt",
				"val": ""
			}
		});

		await this.refresh(res);
	}

	/**
	 * Marks the message as done
	 * @param {boolean} [state=true] State to use (done/not done)
	 * @return {Promise<boolean>} 
	 * @memberof Message
	 */
	async markAsDone(state = true) {
		if(!this.edupage) throw new EdupageError(`Message does not have assigned Edupage instance yet`);

		//Load data
		const res = await this.edupage.api({
			url: ENDPOINT.TIMELINE_FLAG_HOMEWORK,
			data: {
				"homeworkid": "timeline:" + this.id,
				"flag": "done",
				"value": (+state).toString()
			}
		});

		if(!("timelineUserProps" in res)) throw new APIError(`Failed to refresh message: Invalid status received '${res.status}'`, this, res);

		this._data.userProps = null;
		this.edupage._data.timelineUserProps = res.timelineUserProps;
		Message.prototype.init.call(this);

		return state;
	}

	/**
	 * Marks the message as starred
	 * @param {boolean} [state=true] State to use (starred/not starred)
	 * @return {Promise<boolean>} 
	 * @memberof Message
	 */
	async markAsStarred(state = true) {
		if(!this.edupage) throw new EdupageError(`Message does not have assigned Edupage instance yet`);

		//Load data
		const res = await this.edupage.api({
			url: ENDPOINT.TIMELINE_FLAG_HOMEWORK,
			data: {
				"homeworkid": "timeline:" + this.id,
				"flag": "important",
				"value": (+state).toString()
			}
		});

		if(!("timelineUserProps" in res)) throw new APIError(`Failed to refresh message: Invalid status received '${res.status}'`, this, res);

		this._data.userProps = null;
		this.edupage._data.timelineUserProps = res.timelineUserProps;
		Message.prototype.init.call(this);

		return state;
	}

	/**
	 * Refreshes message replies and some other fields
	 * @param {RawDataObject} [data=null] Raw data to use instead of requesting from the server
	 * @memberof Message
	 */
	async refresh(data = null) {
		if(!this.edupage) throw new EdupageError(`Message does not have assigned Edupage instance yet`);

		//Load data
		const res = data || await this.edupage.api({
			url: ENDPOINT.TIMELINE_GET_REPLIES,
			data: {
				groupid: this.id,
				lastsync: ""
			}
		});

		//Invalid response
		if(res.status !== API_STATUS.OK) throw new APIError(`Failed to refresh message: Invalid status received '${res.status}'`, this, res);

		//Parse message data of each reply
		res.data.reakcie.forEach(e => {
			if(typeof e.data === "string") e.data = JSON.parse(e.data);
		});

		//Assign data
		this._data = {...this._data, ...res.data};

		//Init Message object
		Message.prototype.init.call(this);
	}

	/**
	 * Parses name of the user
	 * @static
	 * @param {string} name Name to parse
	 * @return {{firstname: string, lastname: string}} Parsed name
	 * @memberof Message
	 */
	static parseUsername(name) {
		const {firstname = "", lastname = ""} = name.match(/(?<firstname>.*?)\s?(?<lastname>\S+)(?:\s\(|$)/)?.groups || {};

		return {firstname, lastname};
	}

	/**
	 * Searches for the user object from userstring. If user is not found, the parsed userstring is returned.
	 * @private
	 * @param {string} userString Userstring to search for
	 * @return {{recipient: (User | Teacher | Student | Parent | Plan | Class), wildcard: boolean}} User object and wildcard flag
	 * @memberof Message
	 */
	getRecipient(userString) {
		//Ignore parents just for now
		userString = userString.replace("Only", "");

		//Parse userString
		const {type, id, wildcard} = User.parseUserString(userString);
		let recipient = null;

		if(type == ENTITY_TYPE.TEACHER) recipient = this.edupage.teachers.find(e => e.id == id);
		if(type == ENTITY_TYPE.STUDENT) recipient = this.edupage.students.find(e => e.id == id);
		if(type == ENTITY_TYPE.PARENT) recipient = this.edupage.parents.find(e => e.id == id);
		if(type == ENTITY_TYPE.STUD_PLAN || type == ENTITY_TYPE.CUST_PLAN) recipient = this.edupage.plans.find(e => e.id == id);
		if(type == ENTITY_TYPE.STUD_CLASS) recipient = this.edupage.classes.find(e => e.id == id);
		if(type == ENTITY_TYPE.CLASS) recipient = this.edupage.plans.find(e => e.customClassId == id);

		return {recipient, wildcard};
	}
}

module.exports = Message;