diff --git a/Sources/SwiftDevRant/Models/Comment.swift b/Sources/SwiftDevRant/Models/Comment.swift index d097b02..0529968 100644 --- a/Sources/SwiftDevRant/Models/Comment.swift +++ b/Sources/SwiftDevRant/Models/Comment.swift @@ -45,3 +45,47 @@ public struct Comment: Identifiable, Hashable { self.image = image } } + +extension Comment { + struct CodingData: Codable { + let id: Int + let rant_id: Int + let body: String + let score: Int + let created_time: Int + let vote_state: Int + let links: [Link.CodingData]? + let user_id: Int + let user_username: String + let user_score: Int + let user_avatar: User.Avatar.CodingData + //let user_avatar_lg: User.Avatar.CodingData //TODO: check if this exists in the JSON data + let user_dpp: Int? + let attached_image: AttachedImage.CodingData? + let edited: Bool? + } +} + +extension Comment.CodingData { + var decoded: Comment { + .init( + id: id, + rantId: rant_id, + voteState: .init(rawValue: vote_state) ?? .unvoted, + score: score, + author: .init( + id: user_id, + name: user_username, + score: user_score, + devRantSupporter: (user_dpp ?? 0) != 0, + avatar: user_avatar.decoded, + avatarLarge: nil + ), + created: Date(timeIntervalSince1970: TimeInterval(created_time)), + isEdited: edited ?? false, + text: body, + linksInText: links?.map(\.decoded) ?? [], + image: attached_image?.decoded + ) + } +} diff --git a/Sources/SwiftDevRant/Models/User.Avatar.swift b/Sources/SwiftDevRant/Models/User.Avatar.swift index c4dd8a9..8349165 100644 --- a/Sources/SwiftDevRant/Models/User.Avatar.swift +++ b/Sources/SwiftDevRant/Models/User.Avatar.swift @@ -7,6 +7,11 @@ public extension User { public var imageUrl: String? { imageUrlPath.flatMap { "https://avatars.devrant.com/\($0)" } } + + public init(colorHex: String, imageUrlPath: String?) { + self.colorHex = colorHex + self.imageUrlPath = imageUrlPath + } } } diff --git a/Sources/SwiftDevRant/Models/User.swift b/Sources/SwiftDevRant/Models/User.swift index 2729399..e9662e6 100644 --- a/Sources/SwiftDevRant/Models/User.swift +++ b/Sources/SwiftDevRant/Models/User.swift @@ -8,5 +8,14 @@ public struct User: Identifiable, Hashable { public let avatar: Avatar /// A large avatar for the profile view. - public let avatarLarge: Avatar + public let avatarLarge: Avatar? + + public init(id: Int, name: String, score: Int, devRantSupporter: Bool, avatar: User.Avatar, avatarLarge: User.Avatar?) { + self.id = id + self.name = name + self.score = score + self.devRantSupporter = devRantSupporter + self.avatar = avatar + self.avatarLarge = avatarLarge + } }