Kaynağa Gözat

Add a conversion function to properly read native messages for jobs

jherve 1 yıl önce
ebeveyn
işleme
035f954ef7
2 değiştirilmiş dosya ile 23 ekleme ve 6 silme
  1. 8 0
      src/NativeMessage.js
  2. 15 6
      src/NativeMessage.purs

+ 8 - 0
src/NativeMessage.js

@@ -0,0 +1,8 @@
+export function toArrayOfObjects(field) {
+    return function (obj) {
+        return {
+            [field]: Object.entries(obj[field]).map(a => a[1]),
+            tag: obj.tag
+        };
+    }
+}

+ 15 - 6
src/NativeMessage.purs

@@ -35,15 +35,16 @@ data NativeMessage =
     flexibility :: String
   }
   | NativeMessageJobAlreadyExists {job_id :: String}
-  | NativeMessageJobAdded {job :: Unit}
-  | NativeMessageJobOfferList {job_offers :: Unit}
+  | NativeMessageJobAdded {job :: NativePythonJobOffer}
+  | NativeMessageJobOfferList {job_offers :: Array NativePythonJobOffer}
 
+type NativePythonJobOffer = {id :: String, title :: String, url :: String}
 type NativePythonMessage m = {tag :: String | m}
 type NativePythonMessageLog = NativePythonMessage (level :: String, content :: String)
 type NativePythonMessageInitialConfiguration = NativePythonMessage (jobsPath :: String)
 type NativePythonMessageJobAlreadyExists = NativePythonMessage (job_id :: String)
-type NativePythonMessageJobOfferList = NativePythonMessage (job_offers :: Json)
-type NativePythonMessageJobAdded = NativePythonMessage (job :: Json)
+type NativePythonMessageJobOfferList = NativePythonMessage (job_offers :: Array NativePythonJobOffer)
+type NativePythonMessageJobAdded = NativePythonMessage (job :: NativePythonJobOffer)
 
 derive instance Generic NativeMessage _
 instance Show NativeMessage where show = genericShow
@@ -52,6 +53,12 @@ instance EncodeJson NativeMessage where
   encodeJson (NativeMessageVisitedJobPage r) = encodeJson $ union {tag: "visited_linkedin_job_page"} r
   encodeJson a = genericEncodeJson a
 
+-- A function used to transform some messages sent by the native application that are in the form
+-- of an object with unknown keys to an array of objects. The long-term solution is probably to 
+-- change the format of the native message, but we'll probably need this function as well when
+-- we read data storage in storage.local which is stored as a giant object with unknown keys.
+foreign import toArrayOfObjects :: String -> Json -> Json
+
 instance DecodeJson NativeMessage where
   decodeJson json = case decodeJson @(NativePythonMessage ()) json of
     Right {tag: "log_message"} ->
@@ -59,9 +66,11 @@ instance DecodeJson NativeMessage where
     Right {tag: "job_already_exists"} ->
       map (\{job_id} -> NativeMessageJobAlreadyExists {job_id}) $ decodeJson @NativePythonMessageJobAlreadyExists json
     Right {tag: "job_offer_list"} ->
-      map (\_ -> NativeMessageJobOfferList {job_offers: unit}) $ decodeJson @NativePythonMessageJobOfferList json
+      map (\o -> NativeMessageJobOfferList {job_offers: o.job_offers}) $
+      decodeJson @NativePythonMessageJobOfferList $
+      toArrayOfObjects "job_offers" json
     Right {tag: "job_added"} ->
-      map (\_ -> NativeMessageJobAdded {job: unit}) $ decodeJson @NativePythonMessageJobAdded json
+      map (\{job} -> NativeMessageJobAdded {job}) $ decodeJson @NativePythonMessageJobAdded json
 
     Right _r -> Left $ UnexpectedValue json
     Left _ -> genericDecodeJson json