Fix double posting in poor network conditions

Closes #421
This commit is contained in:
Shadowfacts 2023-07-08 15:24:36 -07:00
parent 3c9692d5b2
commit 0eb000224e
3 changed files with 22 additions and 14 deletions

View File

@ -77,7 +77,8 @@ class PostService: ObservableObject {
pollOptions: draft.poll?.pollOptions.map(\.text), pollOptions: draft.poll?.pollOptions.map(\.text),
pollExpiresIn: draft.poll == nil ? nil : Int(draft.poll!.duration), pollExpiresIn: draft.poll == nil ? nil : Int(draft.poll!.duration),
pollMultiple: draft.poll?.multiple, pollMultiple: draft.poll?.multiple,
localOnly: mastodonController.instanceFeatures.localOnlyPosts ? draft.localOnly : nil localOnly: mastodonController.instanceFeatures.localOnlyPosts ? draft.localOnly : nil,
idempotencyKey: draft.id.uuidString
) )
} }

View File

@ -113,6 +113,9 @@ public class Client {
var urlRequest = URLRequest(url: url, timeoutInterval: timeoutInterval) var urlRequest = URLRequest(url: url, timeoutInterval: timeoutInterval)
urlRequest.httpMethod = request.method.name urlRequest.httpMethod = request.method.name
urlRequest.httpBody = request.body.data urlRequest.httpBody = request.body.data
for (name, value) in request.headers {
urlRequest.setValue(value, forHTTPHeaderField: name)
}
if let mimeType = request.body.mimeType { if let mimeType = request.body.mimeType {
urlRequest.setValue(mimeType, forHTTPHeaderField: "Content-Type") urlRequest.setValue(mimeType, forHTTPHeaderField: "Content-Type")
} }
@ -397,19 +400,22 @@ public class Client {
pollOptions: [String]? = nil, pollOptions: [String]? = nil,
pollExpiresIn: Int? = nil, pollExpiresIn: Int? = nil,
pollMultiple: Bool? = nil, pollMultiple: Bool? = nil,
localOnly: Bool? = nil /* hometown only, not glitch */) -> Request<Status> { localOnly: Bool? = nil, /* hometown only, not glitch */
return Request<Status>(method: .post, path: "/api/v1/statuses", body: ParametersBody([ idempotencyKey: String) -> Request<Status> {
"status" => text, var req = Request<Status>(method: .post, path: "/api/v1/statuses", body: ParametersBody([
"content_type" => contentType.mimeType, "status" => text,
"in_reply_to_id" => inReplyTo, "content_type" => contentType.mimeType,
"sensitive" => sensitive, "in_reply_to_id" => inReplyTo,
"spoiler_text" => spoilerText, "sensitive" => sensitive,
"visibility" => visibility?.rawValue, "spoiler_text" => spoilerText,
"language" => language, "visibility" => visibility?.rawValue,
"poll[expires_in]" => pollExpiresIn, "language" => language,
"poll[multiple]" => pollMultiple, "poll[expires_in]" => pollExpiresIn,
"local_only" => localOnly, "poll[multiple]" => pollMultiple,
] + "media_ids" => mediaIDs + "poll[options]" => pollOptions)) "local_only" => localOnly,
] + "media_ids" => mediaIDs + "poll[options]" => pollOptions))
req.headers["Idempotency-Key"] = idempotencyKey
return req
} }
public static func editStatus( public static func editStatus(

View File

@ -13,6 +13,7 @@ public struct Request<ResultType: Decodable>: Sendable {
let endpoint: Endpoint let endpoint: Endpoint
let body: Body let body: Body
var queryParameters: [Parameter] var queryParameters: [Parameter]
var headers: [String: String] = [:]
var additionalAcceptableHTTPCodes: [Int] = [] var additionalAcceptableHTTPCodes: [Int] = []
init(method: Method, path: Endpoint, body: Body = EmptyBody(), queryParameters: [Parameter] = []) { init(method: Method, path: Endpoint, body: Body = EmptyBody(), queryParameters: [Parameter] = []) {