Skip to content

Commit

Permalink
feat: handle hint_(gauche|droite) to build a dummy `MessageRecipien…
Browse files Browse the repository at this point in the history
…t` type
  • Loading branch information
Vexcited committed Feb 22, 2024
1 parent 1d73e3b commit 8bf00e2
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 38 deletions.
6 changes: 3 additions & 3 deletions examples/discussions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ import { authenticatePronoteCredentials, PronoteApiAccountId } from "../src";
console.log("\n--- Messages (most recent first)\n"); // Line break for contents.

for (const message of messages) {
const receiver = message.receiver ?? `(${message.amountOfRecipients} people)`;
const receiver = message.receiver?.name ?? `(${message.amountOfRecipients} people)`;
const recipients = await message.getRecipients();

console.log(`[${message.created.toLocaleString()}]`, message.author, "to", receiver);
console.log(`[${message.created.toLocaleString()}]`, message.author.name, "to", receiver);
console.log("Partial visibility:", message.partialVisibility ? "yes" : "no");
console.log("Actual recipients:", recipients.join(", ") || "(no recipient)");
console.log("Actual recipients:", recipients.map((x) => x.name).join(", ") || "(no recipient)");
console.log(message.content);

for (const file of message.files) {
Expand Down
2 changes: 1 addition & 1 deletion src/api/user/messageRecipients/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const callApiUserMessageRecipients = makeApiHandler<ApiUserMessageRecipie
}
});

const response = await createPronoteAPICall(fetcher, PronoteApiFunctions.DiscussionsRecipients, {
const response = await createPronoteAPICall(fetcher, PronoteApiFunctions.MessageRecipients, {
session_instance: input.session.instance,
payload: request_payload
});
Expand Down
16 changes: 3 additions & 13 deletions src/api/user/messageRecipients/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { PronoteValue } from "~/api/type";
import type { PronoteApiFunctions } from "~/constants/functions";
import type { PronoteApiOnglets } from "~/constants/onglets";
import type { PronoteApiUserMessageRecipientType } from "~/constants/recipients";
import type { PronoteApiUserMessageRecipient } from "~/constants/recipients";
import type { Session } from "~/session";

export interface PronoteApiUserMessageRecipients {
Expand All @@ -20,19 +20,9 @@ export interface PronoteApiUserMessageRecipients {
}

response: {
nom: PronoteApiFunctions.DiscussionsRecipients
nom: PronoteApiFunctions.MessageRecipients
donnees: {
listeDest: PronoteValue<24, Array<{
L: string
N: string
/**
* Type of the recipient.
* Tells whether the recipient is a teacher, student, ...
*/
G: PronoteApiUserMessageRecipientType
P: number
refusMess?: boolean
}>>
listeDest: PronoteValue<24, Array<PronoteApiUserMessageRecipient>>
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/client/Pronote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { callApiUserAttendance } from "~/api/user/attendance";
import { PronoteApiAttendanceItemType } from "~/constants/attendance";
import { StudentAbsence, StudentDelay, StudentPunishment } from "~/parser/attendance";
import { callApiUserMessageRecipients } from "~/api/user/messageRecipients";
import { FetchedMessageRecipient } from "~/parser/recipient";

export default class Pronote {
/**
Expand Down Expand Up @@ -494,17 +495,14 @@ export default class Pronote {
await this.getMessagesFromDiscussion(possessions, true);
}

/**
* Work in progress, should be updated asap.
*/
public async getRecipientsForMessage (messageID: string): Promise<string[]> {
public async getRecipientsForMessage (messageID: string): Promise<FetchedMessageRecipient[]> {
return this.queue.push(async () => {
const { data } = await callApiUserMessageRecipients(this.fetcher, {
session: this.session,
messageID
});

return data.donnees.listeDest.V.map((dest) => dest.L);
return data.donnees.listeDest.V.map((dest) => new FetchedMessageRecipient(dest));
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/constants/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ export enum PronoteApiFunctions {
Discussions = "ListeMessagerie",
Messages = "ListeMessages",
Attendance = "PagePresence",
DiscussionsRecipients = "SaisiePublicMessage"
MessageRecipients = "SaisiePublicMessage"
}
22 changes: 21 additions & 1 deletion src/constants/recipients.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
export enum PronoteApiUserMessageRecipientType {
Teacher = 3,
Student = 4
Student = 4,
Personal = 34
// TODO: Find other types
}

export interface PronoteApiUserMessageRecipient {
N: string

/** Name of the recipient. */
L: string

/**
* Type of the recipient.
* Tells whether the recipient is a teacher, student, ...
*/
G: PronoteApiUserMessageRecipientType

/** NOTE: Not sure what this is. */
P: number

/** Whether the recipient refuses messages. */
refusMess?: boolean
}
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export { PronoteApiLessonContentCategory } from "~/constants/lessonCategory";
export { PronoteApiAttachmentType } from "~/constants/attachments";
export { PronoteApiNewsQuestionType } from "~/constants/news";
export { PronoteApiAccountId } from "~/constants/accounts";
export { PronoteApiUserMessageRecipientType } from "~/constants/recipients";

// Exporting main classes.
export { default as Pronote } from "~/client/Pronote";
Expand All @@ -52,3 +53,5 @@ export { Period, OngletPeriod } from "~/parser/period";
export { StudentNews, StudentNewsCategory, StudentNewsItem, StudentNewsItemQuestion } from "~/parser/news";
export { StudentAbsence, StudentDelay, StudentPunishment } from "~/parser/attendance";
export { StudentDiscussionsOverview, StudentDiscussion, StudentDiscussionFolder } from "~/parser/discussion";
export { StudentMessage } from "~/parser/messages";
export { BaseMessageRecipient, FetchedMessageRecipient } from "~/parser/recipient";
27 changes: 13 additions & 14 deletions src/parser/messages.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import type { PronoteApiUserMessages } from "~/api/user/messages/types";
import { BaseMessageRecipient, FetchedMessageRecipient } from "~/parser/recipient";
import type Pronote from "~/client/Pronote";

import { readPronoteApiDate } from "~/pronote/dates";
import { StudentAttachment } from "~/parser/attachment";
import { PRONOTE_MESSAGE_MYSELF_NAME } from "~/constants/messages";
import { PronoteApiUserMessageRecipientType } from "~/constants/recipients";
import { makeDummyRecipient, parseHintToType } from "~/pronote/recipients";

export class StudentMessage {
readonly #client: Pronote;
readonly #myself: string;
readonly #myself: BaseMessageRecipient;

readonly #id: string;
readonly #content: string;
readonly #created: Date;

readonly #author: string;
readonly #receiver?: string;
readonly #author: BaseMessageRecipient;
readonly #receiver?: BaseMessageRecipient;

readonly #partialVisibility: boolean;
readonly #amountOfRecipients: number;
Expand All @@ -23,27 +26,23 @@ export class StudentMessage {

constructor (client: Pronote, data: PronoteApiUserMessages["response"]["donnees"]["listeMessages"]["V"][number]) {
this.#client = client;
// Reproduce the recipient format.
this.#myself = `${client.studentName} (${client.studentClass})`;
this.#myself = makeDummyRecipient(`${client.studentName} (${client.studentClass})`, PronoteApiUserMessageRecipientType.Student);

this.#id = data.N;
this.#content = data.estHTML ? data.contenu.V : data.contenu;
this.#created = readPronoteApiDate(data.date.V);

this.#author = data.public_gauche === PRONOTE_MESSAGE_MYSELF_NAME ? this.#myself : data.public_gauche;
this.#receiver = data.public_droite === PRONOTE_MESSAGE_MYSELF_NAME ? this.#myself : data.public_droite;
this.#author = data.public_gauche === PRONOTE_MESSAGE_MYSELF_NAME ? this.#myself : makeDummyRecipient(data.public_gauche, parseHintToType(data.hint_gauche));
if (data.public_droite === PRONOTE_MESSAGE_MYSELF_NAME) this.#receiver = this.#myself;
else if (typeof data.public_droite === "string") this.#receiver = makeDummyRecipient(data.public_droite, parseHintToType(data.hint_droite!));

this.#partialVisibility = data.estUnAparte;
this.#amountOfRecipients = (data.nbPublic ?? 1) + 1; // `+1` because the author is also a recipient.

this.#files = data.listeDocumentsJoints?.V.map((file) => new StudentAttachment(client, file)) ?? [];
}

public async getRecipients (): Promise<string[]> {
if (this.#amountOfRecipients === 2 && typeof this.#receiver === "string") {
return [this.#author, this.#receiver];
}

public async getRecipients (): Promise<FetchedMessageRecipient[]> {
return this.#client.getRecipientsForMessage(this.#id);
}

Expand All @@ -59,14 +58,14 @@ export class StudentMessage {
return this.#created;
}

get author (): string {
get author (): BaseMessageRecipient {
return this.#author;
}

/**
* @remark `undefined` when there's more than two recipients.
*/
get receiver (): string | undefined {
get receiver (): BaseMessageRecipient | undefined {
return this.#receiver;
}

Expand Down
38 changes: 38 additions & 0 deletions src/parser/recipient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { PronoteApiUserMessageRecipient, PronoteApiUserMessageRecipientType } from "~/constants/recipients";

export class BaseMessageRecipient {
readonly #name: string;
readonly #type: PronoteApiUserMessageRecipientType;

constructor (data: Omit<PronoteApiUserMessageRecipient, "N" | "P" | "refusMess">) {
this.#name = data.L;
this.#type = data.G;
}

public get name (): string {
return this.#name;
}

public get type (): PronoteApiUserMessageRecipientType {
return this.#type;
}
}

export class FetchedMessageRecipient extends BaseMessageRecipient {
readonly #id: string;
readonly #refuseMessages: boolean;

constructor (data: PronoteApiUserMessageRecipient) {
super(data);
this.#id = data.N;
this.#refuseMessages = data.refusMess ?? false;
}

public get id (): string {
return this.#id;
}

public get refuseMessages (): boolean {
return this.#refuseMessages;
}
}
26 changes: 26 additions & 0 deletions src/pronote/recipients.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { PronoteApiUserMessageRecipientType } from "~/constants/recipients";
import { BaseMessageRecipient } from "~/parser/recipient";

export const parseHintToType = (hint: string): PronoteApiUserMessageRecipientType => {
let type: PronoteApiUserMessageRecipientType;

switch (hint) {
case "Professeur":
type = PronoteApiUserMessageRecipientType.Teacher;
break;
case "Personnel":
type = PronoteApiUserMessageRecipientType.Personal;
break;
default:
type = PronoteApiUserMessageRecipientType.Student;
}

return type;
};

export const makeDummyRecipient = (name: string, type: PronoteApiUserMessageRecipientType): BaseMessageRecipient => {
return new BaseMessageRecipient({
L: name,
G: type
});
};

0 comments on commit 8bf00e2

Please sign in to comment.