From fc331b32c3856d46850c0c162e9a4331a07d077a Mon Sep 17 00:00:00 2001
From: Wilhelm Oks <oksw@MBP-von-Wilhelm.fritz.box>
Date: Thu, 12 Dec 2024 13:05:58 +0100
Subject: [PATCH] Models WIP: RantFeed

---
 Sources/SwiftDevRant/Models/Comment.swift  |  4 +--
 Sources/SwiftDevRant/Models/Rant.swift     |  4 +--
 Sources/SwiftDevRant/Models/RantFeed.swift | 41 +++++++++++++++++++---
 3 files changed, 40 insertions(+), 9 deletions(-)

diff --git a/Sources/SwiftDevRant/Models/Comment.swift b/Sources/SwiftDevRant/Models/Comment.swift
index fc50fe7..7f46e2b 100644
--- a/Sources/SwiftDevRant/Models/Comment.swift
+++ b/Sources/SwiftDevRant/Models/Comment.swift
@@ -12,7 +12,7 @@ public struct Comment: Identifiable, Hashable {
     public let voteState: VoteState
     
     /// The number of upvotes from other users.
-    public var score: Int
+    public let score: Int
     
     /// The user who wrote this comment.
     public let author: User
@@ -27,7 +27,7 @@ public struct Comment: Identifiable, Hashable {
     public let text: String
     
     /// The URLs and user mentions inside of the text of this comment.
-    public var linksInText: [Link]
+    public let linksInText: [Link]
     
     /// The optional image that the user has uploaded for this comment.
     public let image: AttachedImage?
diff --git a/Sources/SwiftDevRant/Models/Rant.swift b/Sources/SwiftDevRant/Models/Rant.swift
index 8fd923d..7b9118a 100644
--- a/Sources/SwiftDevRant/Models/Rant.swift
+++ b/Sources/SwiftDevRant/Models/Rant.swift
@@ -23,13 +23,13 @@ public struct Rant: Identifiable, Hashable {
     public let isEdited: Bool
     
     /// True if this rant has been marked as a favorite by the logged in user.
-    public var isFavorite: Bool
+    public let isFavorite: Bool
     
     /// The text contents of this rant.
     public let text: String
     
     /// The URLs and user mentions inside of the text of this rant.
-    public var linksInText: [Link]
+    public let linksInText: [Link]
     
     /// The optional image that the user has uploaded for this rant.
     public let image: AttachedImage?
diff --git a/Sources/SwiftDevRant/Models/RantFeed.swift b/Sources/SwiftDevRant/Models/RantFeed.swift
index 85eb892..0688609 100644
--- a/Sources/SwiftDevRant/Models/RantFeed.swift
+++ b/Sources/SwiftDevRant/Models/RantFeed.swift
@@ -1,9 +1,6 @@
 /// Contains the list of rants for the logged in user and other random things.
 public struct RantFeed: Hashable {
-    public var rants: [Rant]
-    
-    /// The notification settings for the logged-in user.
-    //public let settings: Settings
+    public let rants: [Rant]
     
     public let sessionHash: String?
     
@@ -12,11 +9,19 @@ public struct RantFeed: Hashable {
     
     /// True if the logged in user is subscribed to devRant++.
     public let devRantSupporter: Bool
-    //public let isUserDPP: Int
     
     public let numberOfUnreadNotifications: Int
     
     public let news: News?
+    
+    public init(rants: [Rant], sessionHash: String?, weeklyRantWeek: Int?, devRantSupporter: Bool, numberOfUnreadNotifications: Int, news: RantFeed.News?) {
+        self.rants = rants
+        self.sessionHash = sessionHash
+        self.weeklyRantWeek = weeklyRantWeek
+        self.devRantSupporter = devRantSupporter
+        self.numberOfUnreadNotifications = numberOfUnreadNotifications
+        self.news = news
+    }
 }
 
 public extension RantFeed {
@@ -45,3 +50,29 @@ public extension RantFeed {
         case all
     }
 }
+
+extension RantFeed {
+    struct CodingData: Codable {
+        let rants: [Rant.CodingData]
+        //let settings //not sure what the purpose is. probably not needed.
+        let set: String?
+        let wrw: Int?
+        let dpp: Int
+        let num_notifs: Int?
+        //let unread //probably the same info as already provided by num_notifs, so not needed.
+        let news: News.CodingData?
+    }
+}
+
+extension RantFeed.CodingData {
+    var decoded: RantFeed {
+        .init(
+            rants: rants.map(\.decoded),
+            sessionHash: `set`,
+            weeklyRantWeek: wrw,
+            devRantSupporter: dpp != 0,
+            numberOfUnreadNotifications: num_notifs ?? 0,
+            news: news?.decoded
+        )
+    }
+}