Update API [skip ci]

This commit is contained in:
Ilya Laktyushin
2025-01-17 18:13:42 +04:00
parent 1f8d34b0c0
commit a8fd8c6085
12 changed files with 223 additions and 79 deletions

View File

@@ -761,12 +761,13 @@ private enum UploadedMediaTransform {
private enum UploadedMediaThumbnailResult {
case file(Api.InputFile)
case photo(Api.InputPhoto)
case none
}
private enum UploadedMediaFileAndThumbnail {
case pending
case done(TelegramMediaFile, UploadedMediaThumbnailResult)
case done(TelegramMediaFile, UploadedMediaThumbnailResult, UploadedMediaThumbnailResult)
}
private func uploadedThumbnail(network: Network, postbox: Postbox, resourceReference: MediaResourceReference, forceNoBigParts: Bool = false) -> Signal<Api.InputFile?, PendingMessageUploadError> {
@@ -784,6 +785,43 @@ private func uploadedThumbnail(network: Network, postbox: Postbox, resourceRefer
}
}
private func uploadedVideoCover(network: Network, postbox: Postbox, resourceReference: MediaResourceReference, peerId: PeerId) -> Signal<Api.InputPhoto?, PendingMessageUploadError> {
return postbox.transaction { transaction -> Api.InputPeer? in
return transaction.getPeer(peerId).flatMap(apiInputPeer)
}
|> castError(PendingMessageUploadError.self)
|> mapToSignal { inputPeer -> Signal<Api.InputPhoto?, PendingMessageUploadError> in
guard let inputPeer else {
return .single(.inputPhotoEmpty)
}
return multipartUpload(network: network, postbox: postbox, source: .resource(resourceReference), encrypt: false, tag: TelegramMediaResourceFetchTag(statsCategory: .image, userContentType: .image), hintFileSize: nil, hintFileIsLarge: false, forceNoBigParts: true)
|> mapError { _ -> PendingMessageUploadError in return .generic }
|> mapToSignal { result -> Signal<Api.InputPhoto?, PendingMessageUploadError> in
switch result {
case .progress:
return .complete()
case let .inputFile(file):
return network.request(Api.functions.messages.uploadMedia(flags: 0, businessConnectionId: nil, peer: inputPeer, media: Api.InputMedia.inputMediaUploadedPhoto(flags: 0, file: file, stickers: [], ttlSeconds: nil)))
|> mapError { _ -> PendingMessageUploadError in return .generic }
|> map { uploadResult in
switch uploadResult {
case let .messageMediaPhoto(_, photo, _):
if case let .photo(_, id, accessHash, fileReference, _, _, _, _) = photo {
return .inputPhoto(id: id, accessHash: accessHash, fileReference: fileReference)
} else {
return .inputPhotoEmpty
}
default:
return .inputPhotoEmpty
}
}
case .inputSecretFile:
return .single(nil)
}
}
}
}
public func statsCategoryForFileWithAttributes(_ attributes: [TelegramMediaFileAttribute]) -> MediaResourceStatsCategory {
for attribute in attributes {
switch attribute {
@@ -956,29 +994,57 @@ private func uploadedMediaFileContent(network: Network, postbox: Postbox, auxili
case .pending:
return .single(.pending)
case let .done(media):
if let media = media as? TelegramMediaFile, let smallestThumbnail = smallestImageRepresentation(media.previewRepresentations) {
if peerId.namespace == Namespaces.Peer.SecretChat {
return .single(.done(media, .none))
} else {
let fileReference: AnyMediaReference
if let partialReference = media.partialReference {
fileReference = partialReference.mediaReference(media)
} else {
fileReference = .standalone(media: media)
}
return uploadedThumbnail(network: network, postbox: postbox, resourceReference: fileReference.resourceReference(smallestThumbnail.resource), forceNoBigParts: forceNoBigParts)
|> mapError { _ -> PendingMessageUploadError in return .generic }
|> map { result in
if let result = result {
return .done(media, .file(result))
if peerId.namespace == Namespaces.Peer.SecretChat {
return .single(.done(file, .none, .none))
} else {
var thumbnailSignal: Signal<UploadedMediaThumbnailResult, PendingMessageUploadError> = .single(.none)
var videoCoverSignal: Signal<UploadedMediaThumbnailResult, PendingMessageUploadError> = .single(.none)
if let media = media as? TelegramMediaFile {
if let smallestThumbnail = smallestImageRepresentation(media.previewRepresentations) {
let fileReference: AnyMediaReference
if let partialReference = media.partialReference {
fileReference = partialReference.mediaReference(media)
} else {
return .done(media, .none)
fileReference = .standalone(media: media)
}
thumbnailSignal = uploadedThumbnail(network: network, postbox: postbox, resourceReference: fileReference.resourceReference(smallestThumbnail.resource), forceNoBigParts: forceNoBigParts)
|> mapError { _ -> PendingMessageUploadError in return .generic }
|> map { result in
if let result = result {
return .file(result)
} else {
return .none
}
}
}
if let cover = media.videoCover, let resource = cover.representations.first?.resource {
let fileReference: AnyMediaReference
if let partialReference = media.partialReference {
fileReference = partialReference.mediaReference(media)
} else {
fileReference = .standalone(media: media)
}
videoCoverSignal = uploadedVideoCover(network: network, postbox: postbox, resourceReference: fileReference.resourceReference(resource), peerId: peerId)
|> mapError { _ -> PendingMessageUploadError in return .generic }
|> map { result in
if let result = result {
return .photo(result)
} else {
return .none
}
}
}
return combineLatest(
thumbnailSignal,
videoCoverSignal
)
|> map { thumbnail, videoCover in
return .done(media, thumbnail, videoCover)
}
} else {
return .single(.done(file, .none, .none))
}
} else {
return .single(.done(file, .none))
}
}
})
@@ -999,18 +1065,25 @@ private func uploadedMediaFileContent(network: Network, postbox: Postbox, auxili
}
return .single(.progress(PendingMessageUploadedContentProgress(progress: progress)))
case let .inputFile(inputFile):
if case let .done(file, thumbnail) = fileAndThumbnailResult {
if case let .done(file, thumbnail, videoCover) = fileAndThumbnailResult {
var flags: Int32 = 0
var hasSpoiler = false
var thumbnailFile: Api.InputFile?
var videoCoverPhoto: Api.InputPhoto?
if case let .file(file) = thumbnail {
thumbnailFile = file
}
if case let .photo(photo) = videoCover {
videoCoverPhoto = photo
}
if let _ = thumbnailFile {
flags |= 1 << 2
}
if let _ = videoCoverPhoto {
flags |= 1 << 6
}
var ttlSeconds: Int32?
for attribute in attributes {
@@ -1049,11 +1122,11 @@ private func uploadedMediaFileContent(network: Network, postbox: Postbox, auxili
}
if ttlSeconds != nil {
return .single(.content(PendingMessageUploadedContentAndReuploadInfo(content: .media(.inputMediaUploadedDocument(flags: flags, file: inputFile, thumb: thumbnailFile, mimeType: file.mimeType, attributes: inputDocumentAttributesFromFileAttributes(file.attributes), stickers: stickers, videoCover: nil, ttlSeconds: ttlSeconds), text), reuploadInfo: nil, cacheReferenceKey: referenceKey)))
return .single(.content(PendingMessageUploadedContentAndReuploadInfo(content: .media(.inputMediaUploadedDocument(flags: flags, file: inputFile, thumb: thumbnailFile, mimeType: file.mimeType, attributes: inputDocumentAttributesFromFileAttributes(file.attributes), stickers: stickers, videoCover: videoCoverPhoto, ttlSeconds: ttlSeconds), text), reuploadInfo: nil, cacheReferenceKey: referenceKey)))
}
if !isGrouped {
let resultInfo = PendingMessageUploadedContentAndReuploadInfo(content: .media(.inputMediaUploadedDocument(flags: flags, file: inputFile, thumb: thumbnailFile, mimeType: file.mimeType, attributes: inputDocumentAttributesFromFileAttributes(file.attributes), stickers: stickers, videoCover: nil, ttlSeconds: ttlSeconds), text), reuploadInfo: nil, cacheReferenceKey: referenceKey)
let resultInfo = PendingMessageUploadedContentAndReuploadInfo(content: .media(.inputMediaUploadedDocument(flags: flags, file: inputFile, thumb: thumbnailFile, mimeType: file.mimeType, attributes: inputDocumentAttributesFromFileAttributes(file.attributes), stickers: stickers, videoCover: videoCoverPhoto, ttlSeconds: ttlSeconds), text), reuploadInfo: nil, cacheReferenceKey: referenceKey)
return .single(.content(resultInfo))
}
@@ -1064,7 +1137,7 @@ private func uploadedMediaFileContent(network: Network, postbox: Postbox, auxili
|> mapError { _ -> PendingMessageUploadError in }
|> mapToSignal { inputPeer -> Signal<PendingMessageUploadedContentResult, PendingMessageUploadError> in
if let inputPeer = inputPeer {
return network.request(Api.functions.messages.uploadMedia(flags: 0, businessConnectionId: nil, peer: inputPeer, media: .inputMediaUploadedDocument(flags: flags, file: inputFile, thumb: thumbnailFile, mimeType: file.mimeType, attributes: inputDocumentAttributesFromFileAttributes(file.attributes), stickers: stickers, videoCover: nil, ttlSeconds: ttlSeconds)))
return network.request(Api.functions.messages.uploadMedia(flags: 0, businessConnectionId: nil, peer: inputPeer, media: .inputMediaUploadedDocument(flags: flags, file: inputFile, thumb: thumbnailFile, mimeType: file.mimeType, attributes: inputDocumentAttributesFromFileAttributes(file.attributes), stickers: stickers, videoCover: videoCoverPhoto, ttlSeconds: ttlSeconds)))
|> mapError { _ -> PendingMessageUploadError in return .generic }
|> mapToSignal { result -> Signal<PendingMessageUploadedContentResult, PendingMessageUploadError> in
switch result {
@@ -1079,8 +1152,11 @@ private func uploadedMediaFileContent(network: Network, postbox: Postbox, auxili
if hasSpoiler {
flags |= (1 << 2)
}
if let _ = videoCoverPhoto {
flags |= (1 << 3)
}
let result: PendingMessageUploadedContentResult = .content(PendingMessageUploadedContentAndReuploadInfo(content: .media(.inputMediaDocument(flags: flags, id: .inputDocument(id: resource.fileId, accessHash: resource.accessHash, fileReference: Buffer(data: fileReference)), videoCover: nil, ttlSeconds: ttlSeconds, query: nil), text), reuploadInfo: nil, cacheReferenceKey: nil))
let result: PendingMessageUploadedContentResult = .content(PendingMessageUploadedContentAndReuploadInfo(content: .media(.inputMediaDocument(flags: flags, id: .inputDocument(id: resource.fileId, accessHash: resource.accessHash, fileReference: Buffer(data: fileReference)), videoCover: videoCoverPhoto, ttlSeconds: ttlSeconds, query: nil), text), reuploadInfo: nil, cacheReferenceKey: nil))
if let _ = ttlSeconds {
return .single(result)
} else {