mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-02-20 17:57:29 +00:00
Remove WebSocket to REST fallback for attachments.
This commit is contained in:
committed by
Greyson Parrelli
parent
c476278d4c
commit
e57b6bfe00
@@ -248,34 +248,6 @@ sealed class NetworkResult<T>(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides the ability to fallback to [fromFetch] if the current [NetworkResult] is non-successful.
|
||||
*
|
||||
* The [fallback] will only be triggered on non-[Success] results. You can provide a [unless] to limit what kinds of errors you fallback on
|
||||
* (the default is to fallback on every error).
|
||||
*
|
||||
* This primary usecase of this is to make a websocket request (see [fromWebSocketRequest]) and fallback to rest upon failure.
|
||||
*
|
||||
* ```kotlin
|
||||
* val user: NetworkResult<LocalUserModel> = NetworkResult
|
||||
* .fromWebSocketRequest(websocket, request, LocalUserMode.class.java)
|
||||
* .fallbackTo { result -> NetworkResult.fromFetch { http.getUser() } }
|
||||
* ```
|
||||
*
|
||||
* @param unless If this lamba returns true, the fallback will not be triggered.
|
||||
*/
|
||||
fun fallbackToFetch(unless: (NetworkResult<T>) -> Boolean = { false }, fallback: Fetcher<T>): NetworkResult<T> {
|
||||
if (this is Success) {
|
||||
return this
|
||||
}
|
||||
|
||||
return if (unless(this)) {
|
||||
fromFetch(fallback)
|
||||
} else {
|
||||
this
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes the output of one [NetworkResult] and passes it as the input to another if the operation is successful.
|
||||
* If it's non-successful, the [result] lambda is not run, and instead the original failure will be propagated.
|
||||
|
||||
@@ -11,6 +11,7 @@ import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentRemo
|
||||
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream
|
||||
import org.whispersystems.signalservice.api.websocket.SignalWebSocket
|
||||
import org.whispersystems.signalservice.internal.crypto.PaddingInputStream
|
||||
import org.whispersystems.signalservice.internal.get
|
||||
import org.whispersystems.signalservice.internal.push.AttachmentUploadForm
|
||||
import org.whispersystems.signalservice.internal.push.PushAttachmentData
|
||||
import org.whispersystems.signalservice.internal.push.PushServiceSocket
|
||||
@@ -18,7 +19,6 @@ import org.whispersystems.signalservice.internal.push.http.AttachmentCipherOutpu
|
||||
import org.whispersystems.signalservice.internal.push.http.ResumableUploadSpec
|
||||
import org.whispersystems.signalservice.internal.websocket.WebSocketRequestMessage
|
||||
import java.io.InputStream
|
||||
import java.security.SecureRandom
|
||||
import kotlin.jvm.optionals.getOrNull
|
||||
|
||||
/**
|
||||
@@ -28,29 +28,17 @@ class AttachmentApi(
|
||||
private val authWebSocket: SignalWebSocket.AuthenticatedWebSocket,
|
||||
private val pushServiceSocket: PushServiceSocket
|
||||
) {
|
||||
companion object {
|
||||
@JvmStatic
|
||||
fun create(authWebSocket: SignalWebSocket.AuthenticatedWebSocket, pushServiceSocket: PushServiceSocket): AttachmentApi {
|
||||
return AttachmentApi(authWebSocket, pushServiceSocket)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a v4 attachment upload form, which provides the necessary information to upload an attachment.
|
||||
*
|
||||
* GET /v4/attachments/form/upload
|
||||
* - 200: Success
|
||||
* - 413: Too many attempts
|
||||
* - 429: Too many attempts
|
||||
*/
|
||||
fun getAttachmentV4UploadForm(): NetworkResult<AttachmentUploadForm> {
|
||||
val request = WebSocketRequestMessage(
|
||||
id = SecureRandom().nextLong(),
|
||||
verb = "GET",
|
||||
path = "/v4/attachments/form/upload"
|
||||
)
|
||||
|
||||
return NetworkResult
|
||||
.fromWebSocketRequest(authWebSocket, request, AttachmentUploadForm::class)
|
||||
.fallbackToFetch(
|
||||
unless = { it is NetworkResult.StatusCodeError && it.code == 209 },
|
||||
fallback = { pushServiceSocket.attachmentV4UploadAttributes }
|
||||
)
|
||||
val request = WebSocketRequestMessage.get("/v4/attachments/form/upload")
|
||||
return NetworkResult.fromWebSocketRequest(authWebSocket, request, AttachmentUploadForm::class)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user