diff --git a/Mastodon/Scene/Compose/ComposeViewModel+PublishState.swift b/Mastodon/Scene/Compose/ComposeViewModel+PublishState.swift index 2da46b655..040ba26c7 100644 --- a/Mastodon/Scene/Compose/ComposeViewModel+PublishState.swift +++ b/Mastodon/Scene/Compose/ComposeViewModel+PublishState.swift @@ -53,6 +53,15 @@ extension ComposeViewModel.PublishState { let mediaIDs = attachmentServices.compactMap { attachmentService in attachmentService.attachment.value?.id } + let pollOptions: [String]? = { + guard viewModel.isPollComposing.value else { return nil } + return viewModel.pollAttributes.value.map { attribute in attribute.option.value } + }() + let pollExpiresIn: Int? = { + guard viewModel.isPollComposing.value else { return nil } + return viewModel.pollExpiresOptionAttribute.expiresOption.value.seconds + }() + let updateMediaQuerySubscriptions: [AnyPublisher, Error>] = { var subscriptions: [AnyPublisher, Error>] = [] for attachmentService in attachmentServices { @@ -81,7 +90,9 @@ extension ComposeViewModel.PublishState { .flatMap { attachments -> AnyPublisher, Error> in let query = Mastodon.API.Statuses.PublishStatusQuery( status: viewModel.composeStatusAttribute.composeContent.value, - mediaIDs: mediaIDs + mediaIDs: mediaIDs.isEmpty ? nil : mediaIDs, + pollOptions: pollOptions, + pollExpiresIn: pollExpiresIn ) return viewModel.context.apiService.publishStatus( domain: domain, diff --git a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Statuses.swift b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Statuses.swift index 6a7642609..e4bff7d54 100644 --- a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Statuses.swift +++ b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Statuses.swift @@ -96,15 +96,34 @@ extension Mastodon.API.Statuses { public struct PublishStatusQuery: Codable, PostQuery { public let status: String? public let mediaIDs: [String]? + public let pollOptions: [String]? + public let pollExpiresIn: Int? - enum CodingKeys: String, CodingKey { - case status - case mediaIDs = "media_ids" - } - - public init(status: String?, mediaIDs: [String]?) { + public init(status: String?, mediaIDs: [String]?, pollOptions: [String]?, pollExpiresIn: Int?) { self.status = status self.mediaIDs = mediaIDs + self.pollOptions = pollOptions + self.pollExpiresIn = pollExpiresIn + } + + var contentType: String? { + return Self.multipartContentType() + } + + var body: Data? { + var data = Data() + + status.flatMap { data.append(Data.multipart(key: "status", value: $0)) } + for mediaID in mediaIDs ?? [] { + data.append(Data.multipart(key: "media_ids[]", value: mediaID)) + } + for pollOption in pollOptions ?? [] { + data.append(Data.multipart(key: "poll[options][]", value: pollOption)) + } + pollExpiresIn.flatMap { data.append(Data.multipart(key: "poll[expires_in]", value: $0)) } + + data.append(Data.multipartEnd()) + return data } } diff --git a/MastodonSDK/Sources/MastodonSDK/Query/MultipartFormValue.swift b/MastodonSDK/Sources/MastodonSDK/Query/MultipartFormValue.swift index fd9a9c8f4..f86a71c8e 100644 --- a/MastodonSDK/Sources/MastodonSDK/Query/MultipartFormValue.swift +++ b/MastodonSDK/Sources/MastodonSDK/Query/MultipartFormValue.swift @@ -35,3 +35,12 @@ extension String: MultipartFormValue { var multipartContentType: String? { return nil } var multipartFilename: String? { return nil } } + + +extension Int: MultipartFormValue { + var multipartValue: Data { + return String(self).data(using: .utf8)! + } + var multipartContentType: String? { return nil } + var multipartFilename: String? { return nil } +}