From ea32047abd4c3a9a80f0b3079c67dd680d997bcc Mon Sep 17 00:00:00 2001
From: delphvr <delphine.vanrossum@student.uclouvain.be>
Date: Mon, 16 Dec 2024 13:43:31 +0100
Subject: [PATCH] reply to message stay if edited previous mess

---
 backend/app/models.py                             | 8 ++++++--
 backend/app/schemas.py                            | 4 ++--
 frontend/src/lib/api/sessions.ts                  | 2 +-
 frontend/src/lib/types/message.ts                 | 8 ++++----
 frontend/src/lib/types/session.ts                 | 2 +-
 frontend/src/routes/sessions/[id]/Writebox.svelte | 7 ++++++-
 6 files changed, 20 insertions(+), 11 deletions(-)

diff --git a/backend/app/models.py b/backend/app/models.py
index 8222ec2..179b7ee 100644
--- a/backend/app/models.py
+++ b/backend/app/models.py
@@ -127,10 +127,14 @@ class Message(Base):
     user_id = Column(Integer, ForeignKey("users.id"))
     session_id = Column(Integer, ForeignKey("sessions.id"))
     created_at = Column(DateTime, default=datetime_aware)
-    reply_to_message_id = Column(Integer, ForeignKey("messages.id"), nullable=True)
+    reply_to_message_id = Column(
+        Integer, ForeignKey("messages.message_id"), nullable=True
+    )
 
     feedbacks = relationship("MessageFeedback", backref="message")
-    replies = relationship("Message", backref="parent_message", remote_side=[id])
+    replies = relationship(
+        "Message", backref="parent_message", remote_side=[message_id]
+    )
 
     def raw(self):
         return [
diff --git a/backend/app/schemas.py b/backend/app/schemas.py
index bc4a3c3..4bab4d1 100644
--- a/backend/app/schemas.py
+++ b/backend/app/schemas.py
@@ -154,7 +154,7 @@ class Message(BaseModel):
     user_id: int
     session_id: int
     created_at: NaiveDatetime
-    reply_to_message_id: int | None = None
+    reply_to_message_id: str | None = None
     feedbacks: list[MessageFeedback]
 
     class Config:
@@ -184,7 +184,7 @@ class MessageMetadataCreate(BaseModel):
 class MessageCreate(BaseModel):
     message_id: str | None = None
     content: str
-    reply_to_message_id: int | None = None
+    reply_to_message_id: str | None = None
     metadata: list[MessageMetadataCreate]
 
     class Config:
diff --git a/frontend/src/lib/api/sessions.ts b/frontend/src/lib/api/sessions.ts
index 9b86115..5e056ac 100644
--- a/frontend/src/lib/api/sessions.ts
+++ b/frontend/src/lib/api/sessions.ts
@@ -41,7 +41,7 @@ export async function createMessageAPI(
 	id: number,
 	content: string,
 	metadata: { message: string; date: number }[],
-	replyTo: number | null
+	replyTo: string | null
 ): Promise<any | null> {
 	const response = await fetch(`/api/sessions/${id}/messages`, {
 		method: 'POST',
diff --git a/frontend/src/lib/types/message.ts b/frontend/src/lib/types/message.ts
index 3e7bcd1..1c1cc25 100644
--- a/frontend/src/lib/types/message.ts
+++ b/frontend/src/lib/types/message.ts
@@ -16,7 +16,7 @@ export default class Message {
 	private _edited: boolean = false;
 	private _versions = writable([] as { content: string; date: Date }[]);
 	private _feedbacks = writable([] as Feedback[]);
-	private _replyTo: number;
+	private _replyTo: string;
 
 	public constructor(
 		id: number,
@@ -25,7 +25,7 @@ export default class Message {
 		created_at: Date,
 		user: User,
 		session: Session,
-		replyTo: number
+		replyTo: string
 	) {
 		this._id = id;
 		this._message_id = message_id;
@@ -77,7 +77,7 @@ export default class Message {
 		return `message-${this._message_id}`;
 	}
 
-	get replyTo(): number {
+	get replyTo(): string {
 		return this._replyTo;
 	}
 
@@ -85,7 +85,7 @@ export default class Message {
 		if (this._replyTo == null) return undefined;
 
 		return get(this._session.messages).find(
-			(m) => m instanceof Message && m.id == this._replyTo
+			(m) => m instanceof Message && m.message_id == this._replyTo
 		) as Message | undefined;
 	}
 
diff --git a/frontend/src/lib/types/session.ts b/frontend/src/lib/types/session.ts
index 04cfb8f..254dce9 100644
--- a/frontend/src/lib/types/session.ts
+++ b/frontend/src/lib/types/session.ts
@@ -201,7 +201,7 @@ export default class Session {
 		sender: User,
 		content: string,
 		metadata: { message: string; date: number }[],
-		replyTo: number | null
+		replyTo: string | null
 	): Promise<Message | null> {
 		const json = await createMessageAPI(fetch, this.id, content, metadata, replyTo);
 		if (json == null || json.id == null || json.message_id == null) {
diff --git a/frontend/src/routes/sessions/[id]/Writebox.svelte b/frontend/src/routes/sessions/[id]/Writebox.svelte
index 284d112..0c582ac 100644
--- a/frontend/src/routes/sessions/[id]/Writebox.svelte
+++ b/frontend/src/routes/sessions/[id]/Writebox.svelte
@@ -37,7 +37,12 @@
 		if (message.length == 0) return;
 
 		try {
-			const m = await session.sendMessage(user, message, [...metadata], replyTo?.id || null);
+			const m = await session.sendMessage(
+				user,
+				message,
+				[...metadata],
+				replyTo?.message_id || null
+			);
 
 			if (m === null) {
 				toastAlert($t('chatbox.sendError'));
-- 
GitLab