From adc8deffa90b467bb75ec872df8ea5ec14c95d51 Mon Sep 17 00:00:00 2001
From: Kima <kimavideos97@gmail.com>
Date: Sun, 10 Dec 2023 20:37:40 +0100
Subject: [PATCH] startend msg sending api thingie

---
 filcnaplo_kreta_api/lib/client/api.dart       | 43 ++++++++---------
 filcnaplo_kreta_api/lib/client/client.dart    |  3 ++
 .../lib/providers/message_provider.dart       | 46 ++++++++++++++++---
 3 files changed, 64 insertions(+), 28 deletions(-)

diff --git a/filcnaplo_kreta_api/lib/client/api.dart b/filcnaplo_kreta_api/lib/client/api.dart
index 6dfb28a..5060149 100644
--- a/filcnaplo_kreta_api/lib/client/api.dart
+++ b/filcnaplo_kreta_api/lib/client/api.dart
@@ -47,26 +47,26 @@ class KretaAPI {
       KretaApiEndpoints.downloadHomeworkAttachments(uid, type);
   static String subjects(String iss, String uid) =>
       "${BaseKreta.kreta(iss)}${KretaApiEndpoints.subjects}?oktatasiNevelesiFeladatUid=$uid";
-      // Structure:
-      // {
-      //   "Uid": 000,
-      //   "Tantargy": {
-      //       "Uid": 000,
-      //       "Nev": "Irodalom",
-      //       "Kategoria": {
-      //          "Uid": "000,magyar_nyelv_es_irodalom",
-      //          "Nev": "magyar_nyelv_es_irodalom",
-      //          "Leiras": "Magyar nyelv és irodalom"
-      //       },
-      //       "SortIndex": 0,
-      //    },
-      //    "Atlag": null, // float
-      //    "AtlagAlakulasaIdoFuggvenyeben": Array[], // no idea what this is
-      //    "SulyozottOsztalyzatOsszege": null, // int | float
-      //    "SulyozottOsztalyzatSzama": null, // int | float
-      //    "SortIndex": 0
-      // }
-      // refer to https://discord.com/channels/1111649116020285532/1111798771513303102/1148368925969612920
+  // Structure:
+  // {
+  //   "Uid": 000,
+  //   "Tantargy": {
+  //       "Uid": 000,
+  //       "Nev": "Irodalom",
+  //       "Kategoria": {
+  //          "Uid": "000,magyar_nyelv_es_irodalom",
+  //          "Nev": "magyar_nyelv_es_irodalom",
+  //          "Leiras": "Magyar nyelv és irodalom"
+  //       },
+  //       "SortIndex": 0,
+  //    },
+  //    "Atlag": null, // float
+  //    "AtlagAlakulasaIdoFuggvenyeben": Array[], // no idea what this is
+  //    "SulyozottOsztalyzatOsszege": null, // int | float
+  //    "SulyozottOsztalyzatSzama": null, // int | float
+  //    "SortIndex": 0
+  // }
+  // refer to https://discord.com/channels/1111649116020285532/1111798771513303102/1148368925969612920
 
   // ADMIN API
   static const sendMessage =
@@ -118,7 +118,8 @@ class KretaApiEndpoints {
   static const capabilities = "/ellenorzo/V3/Sajat/Intezmenyek";
   static String downloadHomeworkAttachments(String uid, String type) =>
       "/ellenorzo/V3/Sajat/Csatolmany/$uid";
-  static const subjects = "/ellenorzo/V3/Sajat/Ertekelesek/Atlagok/TantargyiAtlagok";
+  static const subjects =
+      "/ellenorzo/V3/Sajat/Ertekelesek/Atlagok/TantargyiAtlagok";
 }
 
 class KretaAdminEndpoints {
diff --git a/filcnaplo_kreta_api/lib/client/client.dart b/filcnaplo_kreta_api/lib/client/client.dart
index d4ed845..97aef58 100644
--- a/filcnaplo_kreta_api/lib/client/client.dart
+++ b/filcnaplo_kreta_api/lib/client/client.dart
@@ -139,6 +139,9 @@ class KretaClient {
           if (!headerMap.containsKey("content-type")) {
             headerMap["content-type"] = "application/json";
           }
+          if (url.contains('kommunikacio/uzenetek')) {
+            headerMap["X-Uzenet-Lokalizacio"] = "hu-HU";
+          }
         }
 
         res = await client.post(Uri.parse(url), headers: headerMap, body: body);
diff --git a/filcnaplo_kreta_api/lib/providers/message_provider.dart b/filcnaplo_kreta_api/lib/providers/message_provider.dart
index 8499b85..5e15574 100644
--- a/filcnaplo_kreta_api/lib/providers/message_provider.dart
+++ b/filcnaplo_kreta_api/lib/providers/message_provider.dart
@@ -27,27 +27,33 @@ class MessageProvider with ChangeNotifier {
 
     // Load messages from the database
     if (userId != null) {
-      var dbMessages = await Provider.of<DatabaseProvider>(_context, listen: false).userQuery.getMessages(userId: userId);
+      var dbMessages =
+          await Provider.of<DatabaseProvider>(_context, listen: false)
+              .userQuery
+              .getMessages(userId: userId);
       _messages = dbMessages;
       notifyListeners();
     }
   }
 
   // Fetches all types of Messages
-  Future<void> fetchAll() => Future.forEach(MessageType.values, (MessageType v) => fetch(type: v));
+  Future<void> fetchAll() =>
+      Future.forEach(MessageType.values, (MessageType v) => fetch(type: v));
 
   // Fetches Messages from the Kreta API then stores them in the database
   Future<void> fetch({MessageType type = MessageType.inbox}) async {
     // Check Message Type
     if (type == MessageType.draft) return;
-    String messageType = ["beerkezett", "elkuldott", "torolt"].elementAt(type.index);
+    String messageType =
+        ["beerkezett", "elkuldott", "torolt"].elementAt(type.index);
 
     // Check User
     User? user = Provider.of<UserProvider>(_context, listen: false).user;
     if (user == null) throw "Cannot fetch Messages for User null";
 
     // Get messages
-    List? messagesJson = await Provider.of<KretaClient>(_context, listen: false).getAPI(KretaAPI.messages(messageType));
+    List? messagesJson = await Provider.of<KretaClient>(_context, listen: false)
+        .getAPI(KretaAPI.messages(messageType));
     if (messagesJson == null) throw "Cannot fetch Messages for User ${user.id}";
 
     // Parse messages
@@ -55,8 +61,12 @@ class MessageProvider with ChangeNotifier {
     await Future.wait(List.generate(messagesJson.length, (index) {
       return () async {
         Map message = messagesJson.cast<Map>()[index];
-        Map? messageJson = await Provider.of<KretaClient>(_context, listen: false).getAPI(KretaAPI.message(message["azonosito"].toString()));
-        if (messageJson != null) messages.add(Message.fromJson(messageJson, forceType: type));
+        Map? messageJson =
+            await Provider.of<KretaClient>(_context, listen: false)
+                .getAPI(KretaAPI.message(message["azonosito"].toString()));
+        if (messageJson != null) {
+          messages.add(Message.fromJson(messageJson, forceType: type));
+        }
       }();
     }));
 
@@ -73,8 +83,30 @@ class MessageProvider with ChangeNotifier {
     if (user == null) throw "Cannot store Messages for User null";
 
     String userId = user.id;
-    await Provider.of<DatabaseProvider>(_context, listen: false).userStore.storeMessages(_messages, userId: userId);
+    await Provider.of<DatabaseProvider>(_context, listen: false)
+        .userStore
+        .storeMessages(_messages, userId: userId);
 
     notifyListeners();
   }
+
+  // fetch recipients
+  Future<void> fetchRecipients() async {
+    // check user
+    User? user = Provider.of<UserProvider>(_context, listen: false).user;
+    if (user == null) throw "Cannot fetch Messages for User null";
+
+    // get recipients
+    List? recipientsJson =
+        await Provider.of<KretaClient>(_context, listen: false)
+            .getAPI(KretaAPI.recipientsTeacher);
+    if (recipientsJson == null) {
+      throw "Cannot fetch Recipients for User ${user.id}";
+    }
+
+    print(recipientsJson);
+  }
+
+  // send message
+  Future<void> sendMessage() async {}
 }