feat: implement poll supports for status compose
This commit is contained in:
parent
d05f97951b
commit
0e84b4c164
|
@ -53,6 +53,15 @@ extension ComposeViewModel.PublishState {
|
||||||
let mediaIDs = attachmentServices.compactMap { attachmentService in
|
let mediaIDs = attachmentServices.compactMap { attachmentService in
|
||||||
attachmentService.attachment.value?.id
|
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>] = {
|
let updateMediaQuerySubscriptions: [AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Attachment>, Error>] = {
|
||||||
var subscriptions: [AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Attachment>, Error>] = []
|
var subscriptions: [AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Attachment>, Error>] = []
|
||||||
for attachmentService in attachmentServices {
|
for attachmentService in attachmentServices {
|
||||||
|
@ -81,7 +90,9 @@ extension ComposeViewModel.PublishState {
|
||||||
.flatMap { attachments -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Status>, Error> in
|
.flatMap { attachments -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Status>, Error> in
|
||||||
let query = Mastodon.API.Statuses.PublishStatusQuery(
|
let query = Mastodon.API.Statuses.PublishStatusQuery(
|
||||||
status: viewModel.composeStatusAttribute.composeContent.value,
|
status: viewModel.composeStatusAttribute.composeContent.value,
|
||||||
mediaIDs: mediaIDs
|
mediaIDs: mediaIDs.isEmpty ? nil : mediaIDs,
|
||||||
|
pollOptions: pollOptions,
|
||||||
|
pollExpiresIn: pollExpiresIn
|
||||||
)
|
)
|
||||||
return viewModel.context.apiService.publishStatus(
|
return viewModel.context.apiService.publishStatus(
|
||||||
domain: domain,
|
domain: domain,
|
||||||
|
|
|
@ -96,15 +96,34 @@ extension Mastodon.API.Statuses {
|
||||||
public struct PublishStatusQuery: Codable, PostQuery {
|
public struct PublishStatusQuery: Codable, PostQuery {
|
||||||
public let status: String?
|
public let status: String?
|
||||||
public let mediaIDs: [String]?
|
public let mediaIDs: [String]?
|
||||||
|
public let pollOptions: [String]?
|
||||||
|
public let pollExpiresIn: Int?
|
||||||
|
|
||||||
enum CodingKeys: String, CodingKey {
|
public init(status: String?, mediaIDs: [String]?, pollOptions: [String]?, pollExpiresIn: Int?) {
|
||||||
case status
|
|
||||||
case mediaIDs = "media_ids"
|
|
||||||
}
|
|
||||||
|
|
||||||
public init(status: String?, mediaIDs: [String]?) {
|
|
||||||
self.status = status
|
self.status = status
|
||||||
self.mediaIDs = mediaIDs
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -35,3 +35,12 @@ extension String: MultipartFormValue {
|
||||||
var multipartContentType: String? { return nil }
|
var multipartContentType: String? { return nil }
|
||||||
var multipartFilename: 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 }
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue