2024-12-18 14:08:14 +00:00
|
|
|
import Foundation
|
2024-12-20 20:58:06 +00:00
|
|
|
import KreeRequest
|
2024-12-18 14:08:14 +00:00
|
|
|
|
2024-12-23 14:42:59 +00:00
|
|
|
public struct DevRantRequest {
|
2024-12-20 20:58:06 +00:00
|
|
|
let request: KreeRequest
|
2024-12-15 19:52:03 +00:00
|
|
|
let backend = DevRantBackend()
|
|
|
|
|
2024-12-17 14:10:38 +00:00
|
|
|
public init(requestLogger: Logger) {
|
2024-12-20 20:58:06 +00:00
|
|
|
self.request = KreeRequest(encoder: .devRant, decoder: .devRant, logger: requestLogger)
|
2024-12-17 14:10:38 +00:00
|
|
|
}
|
|
|
|
|
2024-12-20 20:58:06 +00:00
|
|
|
private func makeConfig(_ method: KreeRequest.Method, path: String, urlParameters: [String: String] = [:], headers: [String: String] = [:], token: AuthToken? = nil) -> KreeRequest.Config {
|
2024-12-15 19:52:03 +00:00
|
|
|
var urlParameters = urlParameters
|
|
|
|
urlParameters["app"] = "3"
|
2024-12-20 15:19:17 +00:00
|
|
|
|
2024-12-15 19:52:03 +00:00
|
|
|
if let token {
|
|
|
|
urlParameters["token_id"] = String(token.id)
|
|
|
|
urlParameters["token_key"] = token.key
|
|
|
|
urlParameters["user_id"] = String(token.userId)
|
|
|
|
}
|
|
|
|
|
|
|
|
var headers = headers
|
|
|
|
headers["Content-Type"] = "application/x-www-form-urlencoded"
|
|
|
|
|
|
|
|
return .init(method: method, backend: backend, path: path, urlParameters: urlParameters, headers: headers)
|
|
|
|
}
|
|
|
|
|
2024-12-20 20:58:06 +00:00
|
|
|
private func makeMultipartConfig(_ method: KreeRequest.Method, path: String, parameters: [String: String] = [:], boundary: String, headers: [String: String] = [:], token: AuthToken? = nil) -> KreeRequest.Config {
|
2024-12-20 15:19:17 +00:00
|
|
|
var parameters = parameters
|
|
|
|
parameters["app"] = "3"
|
|
|
|
|
|
|
|
if let token {
|
|
|
|
parameters["token_id"] = String(token.id)
|
|
|
|
parameters["token_key"] = token.key
|
|
|
|
parameters["user_id"] = String(token.userId)
|
|
|
|
}
|
|
|
|
|
|
|
|
var headers = headers
|
|
|
|
headers["Content-Type"] = "multipart/form-data; boundary=\(boundary)"
|
|
|
|
|
|
|
|
return .init(method: method, backend: backend, path: path, urlParameters: parameters, headers: headers)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func multipartBody(parameters: [String: String], boundary: String, imageData: Data?) -> Data {
|
|
|
|
var body = Data()
|
|
|
|
|
|
|
|
let boundaryPrefix = "--\(boundary)\r\n"
|
|
|
|
|
|
|
|
for (key, value) in parameters {
|
|
|
|
body.appendString(boundaryPrefix)
|
|
|
|
body.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
|
|
|
|
body.appendString("\(value)\r\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
if let imageData {
|
|
|
|
//TODO: the image is not always jpeg. not sure if it matters here.
|
|
|
|
body.appendString(boundaryPrefix)
|
|
|
|
body.appendString("Content-Disposition: form-data; name=\"image\"; filename=\"image.jpeg\"\r\n")
|
|
|
|
body.appendString("Content-Type: image/jpeg\r\n\r\n")
|
|
|
|
body.append(imageData)
|
|
|
|
body.appendString("\r\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
body.appendString("--".appending(boundary.appending("--")))
|
|
|
|
|
|
|
|
return body
|
|
|
|
}
|
2024-12-23 14:42:59 +00:00
|
|
|
|
|
|
|
/// For endpoints with the POST method in the devRant API the url parameters need to be passed as a string in the http body rather than in the URL.
|
|
|
|
/// The url encoding works but it might also work with other encodings like json or multipart form data.
|
|
|
|
private func stringBody(fromUrlParameters urlParameters: [String: String]) -> String {
|
|
|
|
String(KreeRequest.urlEncodedQueryString(from: urlParameters).dropFirst()) // dropping the first character "?"
|
|
|
|
}
|
2024-12-20 15:19:17 +00:00
|
|
|
}
|
|
|
|
|
2024-12-23 14:42:59 +00:00
|
|
|
public extension DevRantRequest {
|
2024-12-20 20:26:49 +00:00
|
|
|
func logIn(username: String, password: String) async throws -> AuthToken {
|
2024-12-15 19:52:03 +00:00
|
|
|
var parameters: [String: String] = [:]
|
2024-12-17 14:10:38 +00:00
|
|
|
parameters["app"] = "3"
|
2024-12-15 19:52:03 +00:00
|
|
|
parameters["username"] = username
|
|
|
|
parameters["password"] = password
|
|
|
|
|
2024-12-17 14:10:38 +00:00
|
|
|
let config = makeConfig(.post, path: "users/auth-token")
|
|
|
|
|
2024-12-23 14:42:59 +00:00
|
|
|
let body = stringBody(fromUrlParameters: parameters)
|
2024-12-15 19:52:03 +00:00
|
|
|
|
2024-12-17 14:10:38 +00:00
|
|
|
let response: AuthToken.CodingData.Container = try await request.requestJson(config: config, string: body, apiError: DevRantApiError.CodingData.self)
|
2024-12-15 19:52:03 +00:00
|
|
|
|
|
|
|
return response.auth_token.decoded
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets a personalized feed of rants.
|
|
|
|
///
|
|
|
|
/// - Parameters:
|
|
|
|
/// - token: The token from the `logIn` call response.
|
|
|
|
/// - limit: The number of rants to get for pagination.
|
|
|
|
/// - skip: How many rants to skip for pagination.
|
|
|
|
/// - sessionHash: Pass the session hash value from the last rant feed response or `nil` if calling for the first time.
|
2024-12-20 20:26:49 +00:00
|
|
|
func getRantFeed(token: AuthToken, sort: RantFeed.Sort = .algorithm, limit: Int = 20, skip: Int, sessionHash: String?) async throws -> RantFeed {
|
2024-12-15 19:52:03 +00:00
|
|
|
var parameters: [String: String] = [:]
|
|
|
|
|
|
|
|
parameters["sort"] = switch sort {
|
|
|
|
case .algorithm: "algo"
|
|
|
|
case .recent: "recent"
|
|
|
|
case .top: "top"
|
|
|
|
}
|
|
|
|
|
|
|
|
switch sort {
|
|
|
|
case .top(range: let range):
|
|
|
|
parameters["range"] = switch range {
|
|
|
|
case .day: "day"
|
|
|
|
case .week: "week"
|
|
|
|
case .month: "month"
|
|
|
|
case .all: "all"
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
parameters["limit"] = String(limit)
|
|
|
|
parameters["skip"] = String(skip)
|
|
|
|
parameters["prev_set"] = sessionHash
|
|
|
|
|
|
|
|
parameters["plat"] = "1" // I don't know wtf that is.
|
|
|
|
parameters["nari"] = "1" // I don't know wtf that is.
|
|
|
|
|
2024-12-20 15:19:17 +00:00
|
|
|
let config = makeConfig(.get, path: "devrant/rants", urlParameters: parameters, token: token)
|
2024-12-15 19:52:03 +00:00
|
|
|
|
|
|
|
let response: RantFeed.CodingData = try await request.requestJson(config: config, apiError: DevRantApiError.CodingData.self)
|
|
|
|
|
|
|
|
return response.decoded
|
|
|
|
}
|
|
|
|
|
2024-12-18 14:08:14 +00:00
|
|
|
/// Gets all weeklies as a list.
|
2024-12-15 19:52:03 +00:00
|
|
|
///
|
|
|
|
/// - Parameters:
|
|
|
|
/// - token: The token from the `logIn` call response.
|
2024-12-20 20:26:49 +00:00
|
|
|
func getWeeklies(token: AuthToken) async throws -> [Weekly] {
|
2024-12-20 15:19:17 +00:00
|
|
|
let config = makeConfig(.get, path: "devrant/weekly-list", token: token)
|
2024-12-15 19:52:03 +00:00
|
|
|
|
|
|
|
let response: Weekly.CodingData.List = try await request.requestJson(config: config, apiError: DevRantApiError.CodingData.self)
|
|
|
|
|
|
|
|
return response.weeks.map(\.decoded)
|
|
|
|
}
|
2024-12-18 14:08:14 +00:00
|
|
|
|
|
|
|
/// 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.
|
2024-12-20 20:26:49 +00:00
|
|
|
func getWeeklyRants(token: AuthToken, week: Int?, limit: Int = 20, skip: Int) async throws -> RantFeed {
|
2024-12-18 14:08:14 +00:00
|
|
|
var parameters: [String: String] = [:]
|
|
|
|
|
|
|
|
parameters["week"] = week.flatMap { String($0) }
|
|
|
|
parameters["limit"] = String(limit)
|
|
|
|
parameters["skip"] = String(skip)
|
|
|
|
|
2024-12-20 15:19:17 +00:00
|
|
|
let config = makeConfig(.get, path: "devrant/weekly-rants", urlParameters: parameters, token: token)
|
2024-12-18 14:08:14 +00:00
|
|
|
|
|
|
|
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`.
|
2024-12-20 20:26:49 +00:00
|
|
|
func getNotificationFeed(token: AuthToken, lastChecked: Date?, category: NotificationFeed.Category) async throws -> NotificationFeed {
|
2024-12-18 14:08:14 +00:00
|
|
|
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.
|
|
|
|
|
2024-12-20 15:19:17 +00:00
|
|
|
let config = makeConfig(.get, path: "users/me/notif-feed\(category.rawValue)", urlParameters: parameters, token: token)
|
2024-12-18 14:08:14 +00:00
|
|
|
|
|
|
|
let response: NotificationFeed.CodingData.Container = try await request.requestJson(config: config, apiError: DevRantApiError.CodingData.self)
|
|
|
|
|
|
|
|
return response.data.decoded
|
|
|
|
}
|
2024-12-18 14:35:42 +00:00
|
|
|
|
|
|
|
/// Gets a single rant and its comments.
|
|
|
|
///
|
|
|
|
/// - Parameters:
|
|
|
|
/// - token: The token from the `logIn` call response.
|
|
|
|
/// - rantId: The id of the rant.
|
|
|
|
/// - lastCommentId: Only fetch the comments which were posted after the one corresponding to this id. Pass `nil` to get all comments.
|
2024-12-20 20:26:49 +00:00
|
|
|
func getRant(token: AuthToken, rantId: Int, lastCommentId: Int? = nil) async throws -> (rant: Rant, comments: [Comment]) {
|
2024-12-18 14:35:42 +00:00
|
|
|
var parameters: [String: String] = [:]
|
|
|
|
|
|
|
|
parameters["last_comment_id"] = lastCommentId.flatMap { String($0) }
|
|
|
|
|
2024-12-20 15:19:17 +00:00
|
|
|
let config = makeConfig(.get, path: "devrant/rants/\(rantId)", urlParameters: parameters, token: token)
|
2024-12-18 14:35:42 +00:00
|
|
|
|
|
|
|
struct Response: Codable {
|
|
|
|
let rant: Rant.CodingData
|
|
|
|
let comments: [Comment.CodingData]?
|
|
|
|
}
|
|
|
|
|
|
|
|
let response: Response = try await request.requestJson(config: config, apiError: DevRantApiError.CodingData.self)
|
|
|
|
|
|
|
|
return (rant: response.rant.decoded, comments: response.comments?.map(\.decoded) ?? [])
|
|
|
|
}
|
2024-12-20 15:19:17 +00:00
|
|
|
|
|
|
|
/// Gets a single comment.
|
|
|
|
///
|
|
|
|
/// - Parameters:
|
|
|
|
/// - token: The token from the `logIn` call response.
|
|
|
|
/// - commentId: The id of the comment.
|
2024-12-20 20:26:49 +00:00
|
|
|
func getComment(token: AuthToken, commentId: Int) async throws -> Comment {
|
2024-12-20 15:19:17 +00:00
|
|
|
let config = makeConfig(.get, path: "comments/\(commentId)", token: token)
|
|
|
|
|
|
|
|
let response: Comment.CodingData = try await request.requestJson(config: config, apiError: DevRantApiError.CodingData.self)
|
|
|
|
|
|
|
|
return response.decoded
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the id of a user.
|
|
|
|
///
|
|
|
|
/// - Parameters:
|
|
|
|
/// - username: The username of the user.
|
2024-12-20 20:26:49 +00:00
|
|
|
func getUserId(username: String) async throws -> Int {
|
2024-12-20 15:19:17 +00:00
|
|
|
var parameters: [String: String] = [:]
|
|
|
|
|
|
|
|
parameters["username"] = username
|
|
|
|
|
|
|
|
let config = makeConfig(.get, path: "get-user-id", urlParameters: parameters)
|
|
|
|
|
|
|
|
struct Response: Decodable {
|
|
|
|
let user_id: Int
|
|
|
|
}
|
|
|
|
|
|
|
|
let response: Response = try await request.requestJson(config: config, apiError: DevRantApiError.CodingData.self)
|
|
|
|
|
|
|
|
return response.user_id
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets a user's profile data.
|
|
|
|
///
|
|
|
|
/// - Parameters:
|
|
|
|
/// - token: The token from the `logIn` call response.
|
|
|
|
/// - userId: The id of the user.
|
|
|
|
/// - contentType: The type of content created by the user to be fetched.
|
|
|
|
/// - skip: The number of content items to skip for pagination.
|
2024-12-20 20:26:49 +00:00
|
|
|
func getProfile(token: AuthToken, userId: Int, contentType: Profile.ContentType, skip: Int) async throws -> Profile {
|
2024-12-20 15:19:17 +00:00
|
|
|
var parameters: [String: String] = [:]
|
|
|
|
|
|
|
|
parameters["skip"] = String(skip)
|
|
|
|
parameters["content"] = contentType.rawValue
|
|
|
|
|
|
|
|
let config = makeConfig(.get, path: "users/\(userId)", urlParameters: parameters, token: token)
|
|
|
|
|
2024-12-23 15:04:19 +00:00
|
|
|
let response: Profile.CodingData.Container = try await request.requestJson(config: config, apiError: DevRantApiError.CodingData.self)
|
2024-12-20 15:19:17 +00:00
|
|
|
|
|
|
|
return response.decoded
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Votes on a rant.
|
|
|
|
///
|
|
|
|
/// - Parameters:
|
|
|
|
/// - token: The token from the `logIn` call response.
|
|
|
|
/// - rantId: The id of the rant.
|
|
|
|
/// - vote: The vote for this rant.
|
2024-12-20 20:26:49 +00:00
|
|
|
func voteOnRant(token: AuthToken, rantId: Int, vote: VoteState, downvoteReason: DownvoteReason = .notForMe) async throws -> Rant {
|
2024-12-23 14:42:59 +00:00
|
|
|
let config = makeConfig(.post, path: "devrant/rants/\(rantId)/vote", token: token)
|
|
|
|
|
|
|
|
var parameters = config.urlParameters
|
2024-12-20 15:19:17 +00:00
|
|
|
|
|
|
|
parameters["vote"] = String(vote.rawValue)
|
|
|
|
|
2024-12-20 15:36:47 +00:00
|
|
|
if vote == .downvoted {
|
|
|
|
parameters["reason"] = String(downvoteReason.rawValue)
|
|
|
|
}
|
|
|
|
|
2024-12-23 14:42:59 +00:00
|
|
|
let body = stringBody(fromUrlParameters: parameters)
|
2024-12-20 15:19:17 +00:00
|
|
|
|
|
|
|
struct Response: Codable {
|
|
|
|
let rant: Rant.CodingData
|
|
|
|
}
|
|
|
|
|
2024-12-23 14:42:59 +00:00
|
|
|
let response: Response = try await request.requestJson(config: config, string: body, apiError: DevRantApiError.CodingData.self)
|
2024-12-20 15:19:17 +00:00
|
|
|
|
|
|
|
return response.rant.decoded
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Votes on a comment.
|
|
|
|
///
|
|
|
|
/// - Parameters:
|
|
|
|
/// - token: The token from the `logIn` call response.
|
|
|
|
/// - commentId: The id of the comment.
|
|
|
|
/// - vote: The vote for this comment.
|
2024-12-20 20:26:49 +00:00
|
|
|
func voteOnComment(token: AuthToken, commentId: Int, vote: VoteState, downvoteReason: DownvoteReason = .notForMe) async throws -> Comment {
|
2024-12-23 14:42:59 +00:00
|
|
|
let config = makeConfig(.post, path: "comments/\(commentId)/vote", token: token)
|
|
|
|
|
|
|
|
var parameters = config.urlParameters
|
2024-12-20 15:19:17 +00:00
|
|
|
|
|
|
|
parameters["vote"] = String(vote.rawValue)
|
|
|
|
|
2024-12-20 15:36:47 +00:00
|
|
|
if vote == .downvoted {
|
|
|
|
parameters["reason"] = String(downvoteReason.rawValue)
|
|
|
|
}
|
|
|
|
|
2024-12-23 14:42:59 +00:00
|
|
|
let body = stringBody(fromUrlParameters: parameters)
|
2024-12-20 15:19:17 +00:00
|
|
|
|
|
|
|
struct Response: Decodable {
|
|
|
|
let comment: Comment.CodingData
|
|
|
|
}
|
|
|
|
|
2024-12-23 14:42:59 +00:00
|
|
|
let response: Response = try await request.requestJson(config: config, string: body, apiError: DevRantApiError.CodingData.self)
|
2024-12-20 15:19:17 +00:00
|
|
|
|
|
|
|
return response.comment.decoded
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Updates the user profile.
|
|
|
|
///
|
|
|
|
/// - Parameters:
|
|
|
|
/// - token: The token from the `logIn` call response.
|
|
|
|
/// - about: The user's about text.
|
|
|
|
/// - skills: The user's list of skills.
|
|
|
|
/// - github: The user's GitHub link.
|
|
|
|
/// - location: The user's geographic location.
|
|
|
|
/// - website: The user's personal website.
|
2024-12-20 20:26:49 +00:00
|
|
|
func editUserProfile(token: AuthToken, about: String, skills: String, github: String, location: String, website: String) async throws {
|
2024-12-23 14:42:59 +00:00
|
|
|
let config = makeConfig(.post, path: "users/me/edit-profile", token: token)
|
|
|
|
|
|
|
|
var parameters = config.urlParameters
|
2024-12-20 15:19:17 +00:00
|
|
|
|
|
|
|
parameters["profile_about"] = about
|
|
|
|
parameters["profile_skills"] = skills
|
|
|
|
parameters["profile_github"] = github
|
|
|
|
parameters["profile_location"] = location
|
|
|
|
parameters["profile_website"] = website
|
|
|
|
|
2024-12-23 14:42:59 +00:00
|
|
|
let body = stringBody(fromUrlParameters: parameters)
|
2024-12-20 15:19:17 +00:00
|
|
|
|
2024-12-23 14:42:59 +00:00
|
|
|
try await request.requestJson(config: config, string: body, apiError: DevRantApiError.CodingData.self)
|
2024-12-20 15:19:17 +00:00
|
|
|
}
|
|
|
|
|
2024-12-20 18:52:21 +00:00
|
|
|
/// Creates and posts a rant.
|
2024-12-20 15:19:17 +00:00
|
|
|
///
|
|
|
|
/// - Parameters:
|
|
|
|
/// - token: The token from the `logIn` call response.
|
|
|
|
/// - kind: The type of rant.
|
|
|
|
/// - text: The text content of the rant.
|
|
|
|
/// - tags: The rant's associated tags.
|
|
|
|
/// - image: An image to attach to the rant.
|
|
|
|
/// - imageConversion: The image conversion methods for unsupported image formats.
|
|
|
|
/// - Returns:
|
|
|
|
/// The id of the posted rant.
|
2024-12-20 20:26:49 +00:00
|
|
|
func postRant(token: AuthToken, kind: Rant.Kind, text: String, tags: String, image: Data?, imageConversion: [ImageDataConverter] = [.unsupportedToJpeg]) async throws -> Int {
|
2024-12-20 15:19:17 +00:00
|
|
|
let boundary = UUID().uuidString
|
|
|
|
|
2024-12-23 14:42:59 +00:00
|
|
|
let config = makeMultipartConfig(.post, path: "devrant/rants", boundary: boundary, token: token)
|
2024-12-20 15:19:17 +00:00
|
|
|
|
|
|
|
var parameters = config.urlParameters
|
|
|
|
|
2024-12-23 15:25:34 +00:00
|
|
|
parameters["rant"] = text
|
2024-12-20 15:19:17 +00:00
|
|
|
parameters["tags"] = tags
|
|
|
|
parameters["type"] = String(kind.rawValue)
|
|
|
|
|
|
|
|
let convertedImage = image.flatMap { imageConversion.convert($0) }
|
|
|
|
|
|
|
|
let bodyData = multipartBody(parameters: parameters, boundary: boundary, imageData: convertedImage)
|
|
|
|
|
|
|
|
struct Response: Decodable {
|
|
|
|
let rant_id: Int
|
|
|
|
}
|
|
|
|
|
|
|
|
let response: Response = try await request.requestJson(config: config, data: bodyData, apiError: DevRantApiError.CodingData.self)
|
|
|
|
|
|
|
|
return response.rant_id
|
|
|
|
}
|
2024-12-20 18:52:21 +00:00
|
|
|
|
|
|
|
/// Deletes a rant.
|
|
|
|
///
|
|
|
|
/// - Parameters:
|
|
|
|
/// - token: The token from the `logIn` call response.
|
|
|
|
/// - rantId: The id of the rant.
|
2024-12-20 20:26:49 +00:00
|
|
|
func deleteRant(token: AuthToken, rantId: Int) async throws {
|
2024-12-20 18:52:21 +00:00
|
|
|
let config = makeConfig(.delete, path: "devrant/rants/\(rantId)", token: token)
|
|
|
|
|
|
|
|
try await request.requestJson(config: config, apiError: DevRantApiError.CodingData.self)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets or unsets a rant as a favorite.
|
|
|
|
///
|
|
|
|
/// - Parameters:
|
|
|
|
/// - token: The token from the `logIn` call response.
|
|
|
|
/// - rantId: The id of the rant.
|
|
|
|
/// - favorite: `true` sets the rant as favorite and `false` sets it as not favorite.
|
2024-12-20 20:26:49 +00:00
|
|
|
func favoriteRant(token: AuthToken, rantId: Int, favorite: Bool) async throws {
|
2024-12-20 18:52:21 +00:00
|
|
|
let favoritePath = favorite ? "favorite" : "unfavorite"
|
|
|
|
|
|
|
|
let config = makeConfig(.post, path: "devrant/rants/\(rantId)/\(favoritePath)", token: token)
|
|
|
|
|
2024-12-23 14:42:59 +00:00
|
|
|
let parameters = config.urlParameters
|
|
|
|
|
|
|
|
let body = stringBody(fromUrlParameters: parameters)
|
|
|
|
|
|
|
|
try await request.requestJson(config: config, string: body, apiError: DevRantApiError.CodingData.self)
|
2024-12-20 18:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Edits a posted rant.
|
|
|
|
///
|
|
|
|
/// - Parameters:
|
|
|
|
/// - token: The token from the `logIn` call response.
|
|
|
|
/// - rantId: The id of the rant.
|
|
|
|
/// - text: The text content of the rant.
|
|
|
|
/// - tags: The rants's associated tags.
|
|
|
|
/// - image: An image to attach to the rant.
|
2024-12-23 15:25:34 +00:00
|
|
|
func editRant(token: AuthToken, rantId: Int, text: String, tags: String, image: Data?, imageConversion: [ImageDataConverter] = [.unsupportedToJpeg]) async throws {
|
2024-12-20 18:52:21 +00:00
|
|
|
let boundary = UUID().uuidString
|
|
|
|
|
2024-12-23 14:42:59 +00:00
|
|
|
let config = makeMultipartConfig(.post, path: "devrant/rants/\(rantId)", boundary: boundary, token: token)
|
2024-12-20 18:52:21 +00:00
|
|
|
|
|
|
|
var parameters = config.urlParameters
|
|
|
|
|
|
|
|
parameters["rant"] = text
|
|
|
|
parameters["tags"] = tags
|
|
|
|
|
|
|
|
let convertedImage = image.flatMap { imageConversion.convert($0) }
|
|
|
|
|
|
|
|
let bodyData = multipartBody(parameters: parameters, boundary: boundary, imageData: convertedImage)
|
|
|
|
|
|
|
|
try await request.requestJson(config: config, data: bodyData, apiError: DevRantApiError.CodingData.self)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates and posts a comment for a specific rant.
|
|
|
|
///
|
|
|
|
/// - Parameters:
|
|
|
|
/// - token: The token from the `logIn` call response.
|
|
|
|
/// - rantId: The id of the rant that this comment should be posted for.
|
|
|
|
/// - text: The text content of the comment.
|
|
|
|
/// - image: An image to attach to the comment.
|
2024-12-20 20:26:49 +00:00
|
|
|
func postComment(token: AuthToken, rantId: Int, text: String, image: Data?, imageConversion: [ImageDataConverter] = [.unsupportedToJpeg]) async throws {
|
2024-12-20 18:52:21 +00:00
|
|
|
let boundary = UUID().uuidString
|
|
|
|
|
2024-12-23 14:42:59 +00:00
|
|
|
let config = makeMultipartConfig(.post, path: "devrant/rants/\(rantId)/comments", boundary: boundary, token: token)
|
2024-12-20 18:52:21 +00:00
|
|
|
|
|
|
|
var parameters = config.urlParameters
|
|
|
|
|
|
|
|
parameters["comment"] = text
|
|
|
|
|
|
|
|
let convertedImage = image.flatMap { imageConversion.convert($0) }
|
|
|
|
|
|
|
|
let bodyData = multipartBody(parameters: parameters, boundary: boundary, imageData: convertedImage)
|
|
|
|
|
|
|
|
try await request.requestJson(config: config, data: bodyData, apiError: DevRantApiError.CodingData.self)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Edits a posted comment.
|
|
|
|
///
|
|
|
|
/// - Parameters:
|
|
|
|
/// - token: The token from the `logIn` call response.
|
|
|
|
/// - commentId: The id of the comment.
|
|
|
|
/// - text: The text content of the comment.
|
|
|
|
/// - image: An image to attach to the comment.
|
2024-12-20 20:26:49 +00:00
|
|
|
func editComment(token: AuthToken, commentId: Int, text: String, image: Data?, imageConversion: [ImageDataConverter] = [.unsupportedToJpeg]) async throws {
|
2024-12-20 18:52:21 +00:00
|
|
|
let boundary = UUID().uuidString
|
|
|
|
|
2024-12-23 14:42:59 +00:00
|
|
|
let config = makeMultipartConfig(.post, path: "comments/\(commentId)", boundary: boundary, token: token)
|
2024-12-20 18:52:21 +00:00
|
|
|
|
|
|
|
var parameters = config.urlParameters
|
|
|
|
|
|
|
|
parameters["comment"] = text
|
|
|
|
|
|
|
|
let convertedImage = image.flatMap { imageConversion.convert($0) }
|
|
|
|
|
|
|
|
let bodyData = multipartBody(parameters: parameters, boundary: boundary, imageData: convertedImage)
|
|
|
|
|
|
|
|
try await request.requestJson(config: config, data: bodyData, apiError: DevRantApiError.CodingData.self)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Deletes a comment.
|
|
|
|
///
|
|
|
|
/// - Parameters:
|
|
|
|
/// - token: The token from the `logIn` call response.
|
|
|
|
/// - commentId: The id of the comment.
|
2024-12-20 20:26:49 +00:00
|
|
|
func deleteComment(token: AuthToken, commentId: Int) async throws {
|
2024-12-20 18:52:21 +00:00
|
|
|
let config = makeConfig(.delete, path: "comments/\(commentId)", token: token)
|
|
|
|
|
|
|
|
try await request.requestJson(config: config, apiError: DevRantApiError.CodingData.self)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Marks all notifications as read.
|
|
|
|
///
|
|
|
|
/// - Parameters:
|
|
|
|
/// - token: The token from the `logIn` call response.
|
2024-12-20 20:26:49 +00:00
|
|
|
func markAllNotificationsAsRead(token: AuthToken) async throws {
|
2024-12-20 18:52:21 +00:00
|
|
|
let config = makeConfig(.delete, path: "users/me/notif-feed", token: token)
|
|
|
|
|
|
|
|
try await request.requestJson(config: config, apiError: DevRantApiError.CodingData.self)
|
|
|
|
}
|
|
|
|
|
2024-12-23 14:42:59 +00:00
|
|
|
/// Subscribes to a user.
|
2024-12-20 18:52:21 +00:00
|
|
|
///
|
|
|
|
/// - Parameters:
|
|
|
|
/// - token: The token from the `logIn` call response.
|
2024-12-23 14:42:59 +00:00
|
|
|
/// - userId: The id of the user to subscribe to.
|
|
|
|
func subscribeToUser(token: AuthToken, userId: Int) async throws {
|
|
|
|
let config = makeConfig(.post, path: "users/\(userId)/subscribe", token: token)
|
|
|
|
|
|
|
|
let parameters = config.urlParameters
|
2024-12-20 18:52:21 +00:00
|
|
|
|
2024-12-23 14:42:59 +00:00
|
|
|
let body = stringBody(fromUrlParameters: parameters)
|
|
|
|
|
|
|
|
try await request.requestJson(config: config, string: body, apiError: DevRantApiError.CodingData.self)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Unsubscribes from a user.
|
|
|
|
///
|
|
|
|
/// - Parameters:
|
|
|
|
/// - token: The token from the `logIn` call response.
|
|
|
|
/// - userId: The id of the user to unsubscribe from.
|
|
|
|
func unsubscribeFromUser(token: AuthToken, userId: Int) async throws {
|
|
|
|
let config = makeConfig(.delete, path: "users/\(userId)/subscribe", token: token)
|
2024-12-20 18:52:21 +00:00
|
|
|
|
|
|
|
try await request.requestJson(config: config, apiError: DevRantApiError.CodingData.self)
|
|
|
|
}
|
2024-12-20 15:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private extension Data {
|
|
|
|
mutating func appendString(_ string: String) {
|
|
|
|
let data = string.data(using: .utf8, allowLossyConversion: false)
|
|
|
|
append(data!)
|
|
|
|
}
|
2024-12-10 16:33:57 +00:00
|
|
|
}
|