feat: implement poll supports for status compose

This commit is contained in:
CMK 2021-03-24 15:08:00 +08:00
parent d05f97951b
commit 0e84b4c164
3 changed files with 46 additions and 7 deletions

View File

@ -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<Mastodon.Response.Content<Mastodon.Entity.Attachment>, Error>] = {
var subscriptions: [AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Attachment>, Error>] = []
for attachmentService in attachmentServices {
@ -81,7 +90,9 @@ extension ComposeViewModel.PublishState {
.flatMap { attachments -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Status>, 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,

View File

@ -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
}
}

View File

@ -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 }
}