feat: add Idempotency-Key` header for status

This commit is contained in:
CMK 2021-08-09 17:54:11 +08:00
parent 3570c7108c
commit d3c77ee6cf
7 changed files with 69 additions and 3 deletions

View File

@ -70,7 +70,7 @@ extension ComposeStatusPollItem {
hasher.combine(id)
}
enum ExpiresOption: Equatable, Hashable, CaseIterable {
enum ExpiresOption: String, Equatable, Hashable, CaseIterable {
case thirtyMinutes
case oneHour
case sixHours

View File

@ -107,6 +107,8 @@ extension ComposeViewModel.PublishState {
return subscriptions
}()
let idempotencyKey = viewModel.idempotencyKey.value
publishingSubscription = Publishers.MergeMany(updateMediaQuerySubscriptions)
.collect()
.flatMap { attachments -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Status>, Error> in
@ -122,6 +124,7 @@ extension ComposeViewModel.PublishState {
)
return viewModel.context.apiService.publishStatus(
domain: domain,
idempotencyKey: idempotencyKey,
query: query,
mastodonAuthenticationBox: mastodonAuthenticationBox
)

View File

@ -59,6 +59,9 @@ final class ComposeViewModel: NSObject {
private(set) lazy var publishStateMachinePublisher = CurrentValueSubject<PublishState?, Never>(nil)
private(set) var publishDate = Date() // update it when enter Publishing state
// TODO: group post material into Hashable class
var idempotencyKey = CurrentValueSubject<String, Never>(UUID().uuidString)
// UI & UX
let title: CurrentValueSubject<String, Never>
let shouldDismiss = CurrentValueSubject<Bool, Never>(true)
@ -383,6 +386,56 @@ final class ComposeViewModel: NSObject {
self.isPollToolbarButtonEnabled.value = !shouldPollDisable
})
.store(in: &disposeBag)
// calculate `Idempotency-Key`
let content = Publishers.CombineLatest3(
composeStatusAttribute.isContentWarningComposing,
composeStatusAttribute.contentWarningContent,
composeStatusAttribute.composeContent
)
.map { isContentWarningComposing, contentWarningContent, composeContent -> String in
if isContentWarningComposing {
return contentWarningContent + (composeContent ?? "")
} else {
return composeContent ?? ""
}
}
let attachmentIDs = attachmentServices.map { attachments -> String in
let attachmentIDs = attachments.compactMap { $0.attachment.value?.id }
return attachmentIDs.joined(separator: ",")
}
let pollOptionsAndDuration = Publishers.CombineLatest3(
isPollComposing,
pollOptionAttributes,
pollExpiresOptionAttribute.expiresOption
)
.map { isPollComposing, pollOptionAttributes, expiresOption -> String in
guard isPollComposing else {
return ""
}
let pollOptions = pollOptionAttributes.map { $0.option.value }.joined(separator: ",")
return pollOptions + expiresOption.rawValue
}
Publishers.CombineLatest4(
content,
attachmentIDs,
pollOptionsAndDuration,
selectedStatusVisibility
)
.map { content, attachmentIDs, pollOptionsAndDuration, selectedStatusVisibility -> String in
var hasher = Hasher()
hasher.combine(content)
hasher.combine(attachmentIDs)
hasher.combine(pollOptionsAndDuration)
hasher.combine(selectedStatusVisibility.visibility.rawValue)
let hashValue = hasher.finalize()
return "\(hashValue)"
}
.assign(to: \.value, on: idempotencyKey)
.store(in: &disposeBag)
}
deinit {

View File

@ -16,6 +16,7 @@ extension APIService {
func publishStatus(
domain: String,
idempotencyKey: String?,
query: Mastodon.API.Statuses.PublishStatusQuery,
mastodonAuthenticationBox: MastodonAuthenticationBox
) -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Status>, Error> {
@ -24,6 +25,7 @@ extension APIService {
return Mastodon.API.Statuses.publishStatus(
session: session,
domain: domain,
idempotencyKey: idempotencyKey,
query: query,
authorization: authorization
)

View File

@ -56,8 +56,11 @@ final class SendPostIntentHandler: NSObject, SendPostIntentHandling {
visibility: visibility
)
let idempotencyKey = UUID().uuidString
APIService.shared.publishStatus(
domain: box.domain,
idempotencyKey: idempotencyKey,
query: query,
mastodonAuthenticationBox: box
)

View File

@ -77,14 +77,18 @@ extension Mastodon.API.Statuses {
public static func publishStatus(
session: URLSession,
domain: String,
idempotencyKey: String?,
query: PublishStatusQuery,
authorization: Mastodon.API.OAuth.Authorization?
) -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Status>, Error> {
let request = Mastodon.API.post(
var request = Mastodon.API.post(
url: publishNewStatusEndpointURL(domain: domain),
query: query,
authorization: authorization
)
if let idempotencyKey = idempotencyKey {
request.setValue(idempotencyKey, forHTTPHeaderField: "Idempotency-Key")
}
return session.dataTaskPublisher(for: request)
.tryMap { data, response in
let value = try Mastodon.API.decode(type: Mastodon.Entity.Status.self, from: data, response: response)

View File

@ -346,6 +346,7 @@ extension ShareViewModel {
)
return APIService.shared.publishStatus(
domain: domain,
idempotencyKey: nil, // FIXME:
query: query,
mastodonAuthenticationBox: mastodonAuthenticationBox
)