Requests WIP: getWeeklyRants, getNotificationFeed
This commit is contained in:
parent
edd526d2aa
commit
b6fd0219fb
3 changed files with 52 additions and 3 deletions
@ -2,7 +2,7 @@ import Foundation
|
|||||||
|
|
||||||
/// Contains a list of all notifications for the logged in user and the numbers of unread notifications.
|
/// Contains a list of all notifications for the logged in user and the numbers of unread notifications.
|
||||||
public struct NotificationFeed: Hashable {
|
public struct NotificationFeed: Hashable {
|
||||||
public enum Categories: String, CaseIterable {
|
public enum Category: String, CaseIterable {
|
||||||
case all = ""
|
case all = ""
|
||||||
case upvotes = "upvotes"
|
case upvotes = "upvotes"
|
||||||
case mentions = "mentions"
|
case mentions = "mentions"
|
||||||
@ -32,6 +32,10 @@ public struct NotificationFeed: Hashable {
|
|||||||
|
|
||||||
extension NotificationFeed {
|
extension NotificationFeed {
|
||||||
struct CodingData: Decodable {
|
struct CodingData: Decodable {
|
||||||
|
struct Container: Decodable {
|
||||||
|
let data: NotificationFeed.CodingData
|
||||||
|
}
|
||||||
|
|
||||||
let check_time: Int
|
let check_time: Int
|
||||||
let items: [Notification.CodingData]
|
let items: [Notification.CodingData]
|
||||||
let unread: UnreadNumbers.CodingData
|
let unread: UnreadNumbers.CodingData
|
||||||
|
@ -71,7 +71,7 @@ extension Rant {
|
|||||||
let text: String
|
let text: String
|
||||||
let score: Int
|
let score: Int
|
||||||
let created_time: Int
|
let created_time: Int
|
||||||
let attached_image: AttachedImage.CodingData?
|
let attached_image: AttachedImage.CodingData? // this value can also be of type String. See the custom decoding code.
|
||||||
let num_comments: Int
|
let num_comments: Int
|
||||||
let tags: [String]
|
let tags: [String]
|
||||||
let vote_state: Int
|
let vote_state: Int
|
||||||
@ -104,8 +104,10 @@ extension Rant {
|
|||||||
created_time = try values.decode(Int.self, forKey: .created_time)
|
created_time = try values.decode(Int.self, forKey: .created_time)
|
||||||
|
|
||||||
do {
|
do {
|
||||||
|
// If the value is an object, decode it into an attached image.
|
||||||
attached_image = try values.decode(AttachedImage.CodingData.self, forKey: .attached_image)
|
attached_image = try values.decode(AttachedImage.CodingData.self, forKey: .attached_image)
|
||||||
} catch {
|
} catch {
|
||||||
|
// Otherwise it was an empty string. Treat is as no attached image.
|
||||||
attached_image = nil
|
attached_image = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
import Foundation
|
||||||
|
|
||||||
public struct SwiftDevRant {
|
public struct SwiftDevRant {
|
||||||
let request: Request
|
let request: Request
|
||||||
let backend = DevRantBackend()
|
let backend = DevRantBackend()
|
||||||
@ -79,7 +81,7 @@ public struct SwiftDevRant {
|
|||||||
return response.decoded
|
return response.decoded
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get all weeklies as a list.
|
/// Gets all weeklies as a list.
|
||||||
///
|
///
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - token: The token from the `logIn` call response.
|
/// - token: The token from the `logIn` call response.
|
||||||
@ -90,4 +92,45 @@ public struct SwiftDevRant {
|
|||||||
|
|
||||||
return response.weeks.map(\.decoded)
|
return response.weeks.map(\.decoded)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Gets a specific week's weekly rants.
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - token: The token from the `logIn` call response.
|
||||||
|
/// - week: The number of the week. Pass `nil` to get the latest week's rants.
|
||||||
|
/// - limit: The number of rants for pagination.
|
||||||
|
/// - skip: How many rants to skip for pagination.
|
||||||
|
public func getWeeklyRants(token: AuthToken, week: Int?, limit: Int = 20, skip: Int) async throws -> RantFeed {
|
||||||
|
var parameters: [String: String] = [:]
|
||||||
|
|
||||||
|
parameters["week"] = week.flatMap { String($0) }
|
||||||
|
parameters["limit"] = String(limit)
|
||||||
|
parameters["skip"] = String(skip)
|
||||||
|
|
||||||
|
//parameters["sort"] = "algo" //TODO: This seems wrong. Check if this is needed or not.
|
||||||
|
|
||||||
|
let config = makeConfig(.get, path: "devrant/weekly-rants", urlParameters: parameters)
|
||||||
|
|
||||||
|
let response: RantFeed.CodingData = try await request.requestJson(config: config, apiError: DevRantApiError.CodingData.self)
|
||||||
|
|
||||||
|
return response.decoded
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets the list of notifications and numbers for each notification type.
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - token: The token from the `logIn` call response.
|
||||||
|
/// - lastChecked: Pass the value from the last response or `nil`.
|
||||||
|
public func getNotificationFeed(token: AuthToken, lastChecked: Date?, category: NotificationFeed.Category) async throws -> NotificationFeed {
|
||||||
|
var parameters: [String: String] = [:]
|
||||||
|
|
||||||
|
parameters["last_time"] = lastChecked.flatMap { String(Int($0.timeIntervalSince1970)) } ?? "0"
|
||||||
|
parameters["ext_prof"] = "1" // I don't know wtf that is.
|
||||||
|
|
||||||
|
let config = makeConfig(.get, path: "users/me/notif-feed\(category.rawValue)", urlParameters: parameters)
|
||||||
|
|
||||||
|
let response: NotificationFeed.CodingData.Container = try await request.requestJson(config: config, apiError: DevRantApiError.CodingData.self)
|
||||||
|
|
||||||
|
return response.data.decoded
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user