502 lines
20 KiB
Java
502 lines
20 KiB
Java
package com.discord.utilities.rest;
|
|
|
|
import a0.a.a.b;
|
|
import android.content.ContentResolver;
|
|
import android.content.Context;
|
|
import android.net.Uri;
|
|
import androidx.annotation.MainThread;
|
|
import androidx.annotation.VisibleForTesting;
|
|
import c.d.b.a.a;
|
|
import com.discord.R;
|
|
import com.discord.models.domain.ModelAuditLogEntry;
|
|
import com.discord.restapi.RestAPIParams;
|
|
import com.discord.utilities.attachments.AttachmentUtilsKt;
|
|
import com.discord.utilities.error.Error;
|
|
import com.lytefast.flexinput.model.Attachment;
|
|
import d0.f0.l;
|
|
import d0.g0.w;
|
|
import d0.o;
|
|
import d0.t.u;
|
|
import d0.z.d.m;
|
|
import j0.l.a.q;
|
|
import j0.l.e.j;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Objects;
|
|
import kotlin.Pair;
|
|
import kotlin.Unit;
|
|
import kotlin.jvm.functions.Function0;
|
|
import kotlin.jvm.functions.Function1;
|
|
import kotlin.jvm.internal.DefaultConstructorMarker;
|
|
import kotlinx.coroutines.CoroutineDispatcher;
|
|
import okhttp3.MultipartBody;
|
|
import rx.Emitter;
|
|
import rx.Observable;
|
|
import s.a.a.n;
|
|
import s.a.j0;
|
|
import s.a.w0;
|
|
/* compiled from: SendUtils.kt */
|
|
public final class SendUtils {
|
|
public static final SendUtils INSTANCE = new SendUtils();
|
|
public static final int MAX_MESSAGE_CHARACTER_COUNT = 2000;
|
|
public static final int MAX_MESSAGE_CHARACTER_COUNT_PREMIUM = 4000;
|
|
|
|
/* compiled from: SendUtils.kt */
|
|
public static final class FileUpload {
|
|
public static final Companion Companion = new Companion(null);
|
|
public static final long SIZE_UNKNOWN = -1;
|
|
private final Observable<Long> bytesWrittenObservable;
|
|
private final long contentLength;
|
|
private final String mimeType;
|
|
private final String name;
|
|
private final MultipartBody.Part part;
|
|
|
|
/* compiled from: SendUtils.kt */
|
|
public static final class Companion {
|
|
private Companion() {
|
|
}
|
|
|
|
public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) {
|
|
this();
|
|
}
|
|
}
|
|
|
|
public FileUpload(String str, long j, MultipartBody.Part part, String str2, Observable<Long> observable) {
|
|
m.checkNotNullParameter(str, ModelAuditLogEntry.CHANGE_KEY_NAME);
|
|
m.checkNotNullParameter(part, "part");
|
|
m.checkNotNullParameter(str2, "mimeType");
|
|
m.checkNotNullParameter(observable, "bytesWrittenObservable");
|
|
this.name = str;
|
|
this.contentLength = j;
|
|
this.part = part;
|
|
this.mimeType = str2;
|
|
this.bytesWrittenObservable = observable;
|
|
}
|
|
|
|
/* JADX DEBUG: Multi-variable search result rejected for r4v0, resolved type: com.discord.utilities.rest.SendUtils$FileUpload */
|
|
/* JADX WARN: Multi-variable type inference failed */
|
|
public static /* synthetic */ FileUpload copy$default(FileUpload fileUpload, String str, long j, MultipartBody.Part part, String str2, Observable observable, int i, Object obj) {
|
|
if ((i & 1) != 0) {
|
|
str = fileUpload.name;
|
|
}
|
|
if ((i & 2) != 0) {
|
|
j = fileUpload.contentLength;
|
|
}
|
|
if ((i & 4) != 0) {
|
|
part = fileUpload.part;
|
|
}
|
|
if ((i & 8) != 0) {
|
|
str2 = fileUpload.mimeType;
|
|
}
|
|
if ((i & 16) != 0) {
|
|
observable = fileUpload.bytesWrittenObservable;
|
|
}
|
|
return fileUpload.copy(str, j, part, str2, observable);
|
|
}
|
|
|
|
public final String component1() {
|
|
return this.name;
|
|
}
|
|
|
|
public final long component2() {
|
|
return this.contentLength;
|
|
}
|
|
|
|
public final MultipartBody.Part component3() {
|
|
return this.part;
|
|
}
|
|
|
|
public final String component4() {
|
|
return this.mimeType;
|
|
}
|
|
|
|
public final Observable<Long> component5() {
|
|
return this.bytesWrittenObservable;
|
|
}
|
|
|
|
public final FileUpload copy(String str, long j, MultipartBody.Part part, String str2, Observable<Long> observable) {
|
|
m.checkNotNullParameter(str, ModelAuditLogEntry.CHANGE_KEY_NAME);
|
|
m.checkNotNullParameter(part, "part");
|
|
m.checkNotNullParameter(str2, "mimeType");
|
|
m.checkNotNullParameter(observable, "bytesWrittenObservable");
|
|
return new FileUpload(str, j, part, str2, observable);
|
|
}
|
|
|
|
public boolean equals(Object obj) {
|
|
if (this == obj) {
|
|
return true;
|
|
}
|
|
if (!(obj instanceof FileUpload)) {
|
|
return false;
|
|
}
|
|
FileUpload fileUpload = (FileUpload) obj;
|
|
return m.areEqual(this.name, fileUpload.name) && this.contentLength == fileUpload.contentLength && m.areEqual(this.part, fileUpload.part) && m.areEqual(this.mimeType, fileUpload.mimeType) && m.areEqual(this.bytesWrittenObservable, fileUpload.bytesWrittenObservable);
|
|
}
|
|
|
|
public final Observable<Long> getBytesWrittenObservable() {
|
|
return this.bytesWrittenObservable;
|
|
}
|
|
|
|
public final long getContentLength() {
|
|
return this.contentLength;
|
|
}
|
|
|
|
public final String getMimeType() {
|
|
return this.mimeType;
|
|
}
|
|
|
|
public final String getName() {
|
|
return this.name;
|
|
}
|
|
|
|
public final MultipartBody.Part getPart() {
|
|
return this.part;
|
|
}
|
|
|
|
public int hashCode() {
|
|
String str = this.name;
|
|
int i = 0;
|
|
int a = (b.a(this.contentLength) + ((str != null ? str.hashCode() : 0) * 31)) * 31;
|
|
MultipartBody.Part part = this.part;
|
|
int hashCode = (a + (part != null ? part.hashCode() : 0)) * 31;
|
|
String str2 = this.mimeType;
|
|
int hashCode2 = (hashCode + (str2 != null ? str2.hashCode() : 0)) * 31;
|
|
Observable<Long> observable = this.bytesWrittenObservable;
|
|
if (observable != null) {
|
|
i = observable.hashCode();
|
|
}
|
|
return hashCode2 + i;
|
|
}
|
|
|
|
public String toString() {
|
|
StringBuilder P = a.P("FileUpload(name=");
|
|
P.append(this.name);
|
|
P.append(", contentLength=");
|
|
P.append(this.contentLength);
|
|
P.append(", part=");
|
|
P.append(this.part);
|
|
P.append(", mimeType=");
|
|
P.append(this.mimeType);
|
|
P.append(", bytesWrittenObservable=");
|
|
P.append(this.bytesWrittenObservable);
|
|
P.append(")");
|
|
return P.toString();
|
|
}
|
|
}
|
|
|
|
/* compiled from: SendUtils.kt */
|
|
public static abstract class SendPayload {
|
|
|
|
/* compiled from: SendUtils.kt */
|
|
public static final class Preprocessing extends SendPayload {
|
|
private final String mimeType;
|
|
private final String name;
|
|
private final int numFiles;
|
|
|
|
public Preprocessing(int i, String str, String str2) {
|
|
super(null);
|
|
this.numFiles = i;
|
|
this.name = str;
|
|
this.mimeType = str2;
|
|
}
|
|
|
|
public static /* synthetic */ Preprocessing copy$default(Preprocessing preprocessing, int i, String str, String str2, int i2, Object obj) {
|
|
if ((i2 & 1) != 0) {
|
|
i = preprocessing.numFiles;
|
|
}
|
|
if ((i2 & 2) != 0) {
|
|
str = preprocessing.name;
|
|
}
|
|
if ((i2 & 4) != 0) {
|
|
str2 = preprocessing.mimeType;
|
|
}
|
|
return preprocessing.copy(i, str, str2);
|
|
}
|
|
|
|
public final int component1() {
|
|
return this.numFiles;
|
|
}
|
|
|
|
public final String component2() {
|
|
return this.name;
|
|
}
|
|
|
|
public final String component3() {
|
|
return this.mimeType;
|
|
}
|
|
|
|
public final Preprocessing copy(int i, String str, String str2) {
|
|
return new Preprocessing(i, str, str2);
|
|
}
|
|
|
|
public boolean equals(Object obj) {
|
|
if (this == obj) {
|
|
return true;
|
|
}
|
|
if (!(obj instanceof Preprocessing)) {
|
|
return false;
|
|
}
|
|
Preprocessing preprocessing = (Preprocessing) obj;
|
|
return this.numFiles == preprocessing.numFiles && m.areEqual(this.name, preprocessing.name) && m.areEqual(this.mimeType, preprocessing.mimeType);
|
|
}
|
|
|
|
public final String getMimeType() {
|
|
return this.mimeType;
|
|
}
|
|
|
|
public final String getName() {
|
|
return this.name;
|
|
}
|
|
|
|
public final int getNumFiles() {
|
|
return this.numFiles;
|
|
}
|
|
|
|
public int hashCode() {
|
|
int i = this.numFiles * 31;
|
|
String str = this.name;
|
|
int i2 = 0;
|
|
int hashCode = (i + (str != null ? str.hashCode() : 0)) * 31;
|
|
String str2 = this.mimeType;
|
|
if (str2 != null) {
|
|
i2 = str2.hashCode();
|
|
}
|
|
return hashCode + i2;
|
|
}
|
|
|
|
public String toString() {
|
|
StringBuilder P = a.P("Preprocessing(numFiles=");
|
|
P.append(this.numFiles);
|
|
P.append(", name=");
|
|
P.append(this.name);
|
|
P.append(", mimeType=");
|
|
return a.H(P, this.mimeType, ")");
|
|
}
|
|
}
|
|
|
|
/* compiled from: SendUtils.kt */
|
|
public static final class ReadyToSend extends SendPayload {
|
|
private final RestAPIParams.Message message;
|
|
private final List<FileUpload> uploads;
|
|
|
|
/* JADX INFO: super call moved to the top of the method (can break code semantics) */
|
|
public ReadyToSend(RestAPIParams.Message message, List<FileUpload> list) {
|
|
super(null);
|
|
m.checkNotNullParameter(message, "message");
|
|
m.checkNotNullParameter(list, "uploads");
|
|
this.message = message;
|
|
this.uploads = list;
|
|
}
|
|
|
|
/* JADX DEBUG: Multi-variable search result rejected for r0v0, resolved type: com.discord.utilities.rest.SendUtils$SendPayload$ReadyToSend */
|
|
/* JADX WARN: Multi-variable type inference failed */
|
|
public static /* synthetic */ ReadyToSend copy$default(ReadyToSend readyToSend, RestAPIParams.Message message, List list, int i, Object obj) {
|
|
if ((i & 1) != 0) {
|
|
message = readyToSend.message;
|
|
}
|
|
if ((i & 2) != 0) {
|
|
list = readyToSend.uploads;
|
|
}
|
|
return readyToSend.copy(message, list);
|
|
}
|
|
|
|
public final RestAPIParams.Message component1() {
|
|
return this.message;
|
|
}
|
|
|
|
public final List<FileUpload> component2() {
|
|
return this.uploads;
|
|
}
|
|
|
|
public final ReadyToSend copy(RestAPIParams.Message message, List<FileUpload> list) {
|
|
m.checkNotNullParameter(message, "message");
|
|
m.checkNotNullParameter(list, "uploads");
|
|
return new ReadyToSend(message, list);
|
|
}
|
|
|
|
public boolean equals(Object obj) {
|
|
if (this == obj) {
|
|
return true;
|
|
}
|
|
if (!(obj instanceof ReadyToSend)) {
|
|
return false;
|
|
}
|
|
ReadyToSend readyToSend = (ReadyToSend) obj;
|
|
return m.areEqual(this.message, readyToSend.message) && m.areEqual(this.uploads, readyToSend.uploads);
|
|
}
|
|
|
|
public final RestAPIParams.Message getMessage() {
|
|
return this.message;
|
|
}
|
|
|
|
public final List<FileUpload> getUploads() {
|
|
return this.uploads;
|
|
}
|
|
|
|
public int hashCode() {
|
|
RestAPIParams.Message message = this.message;
|
|
int i = 0;
|
|
int hashCode = (message != null ? message.hashCode() : 0) * 31;
|
|
List<FileUpload> list = this.uploads;
|
|
if (list != null) {
|
|
i = list.hashCode();
|
|
}
|
|
return hashCode + i;
|
|
}
|
|
|
|
public String toString() {
|
|
StringBuilder P = a.P("ReadyToSend(message=");
|
|
P.append(this.message);
|
|
P.append(", uploads=");
|
|
return a.J(P, this.uploads, ")");
|
|
}
|
|
}
|
|
|
|
private SendPayload() {
|
|
}
|
|
|
|
public /* synthetic */ SendPayload(DefaultConstructorMarker defaultConstructorMarker) {
|
|
this();
|
|
}
|
|
}
|
|
|
|
private SendUtils() {
|
|
}
|
|
|
|
private final Observable<FileUpload> getPart(Attachment<?> attachment, ContentResolver contentResolver, String str) {
|
|
return Observable.n(new SendUtils$getPart$1(attachment, str, contentResolver), Emitter.BackpressureMode.BUFFER);
|
|
}
|
|
|
|
/* JADX DEBUG: Multi-variable search result rejected for r1v0, resolved type: com.discord.utilities.rest.SendUtils */
|
|
/* JADX WARN: Multi-variable type inference failed */
|
|
public static /* synthetic */ void handleSendError$default(SendUtils sendUtils, Error error, Context context, Function0 function0, Function0 function02, int i, Object obj) {
|
|
if ((i & 4) != 0) {
|
|
function0 = null;
|
|
}
|
|
if ((i & 8) != 0) {
|
|
function02 = null;
|
|
}
|
|
sendUtils.handleSendError(error, context, function0, function02);
|
|
}
|
|
|
|
private final Pair<String, String> splitFileExtension(String str) {
|
|
int lastIndexOf$default = w.lastIndexOf$default((CharSequence) str, '.', 0, false, 6, (Object) null);
|
|
if (lastIndexOf$default == -1) {
|
|
return o.to(str, null);
|
|
}
|
|
Objects.requireNonNull(str, "null cannot be cast to non-null type java.lang.String");
|
|
String substring = str.substring(0, lastIndexOf$default);
|
|
m.checkNotNullExpressionValue(substring, "(this as java.lang.Strin…ing(startIndex, endIndex)");
|
|
String substring2 = str.substring(lastIndexOf$default + 1);
|
|
m.checkNotNullExpressionValue(substring2, "(this as java.lang.String).substring(startIndex)");
|
|
return o.to(substring, substring2);
|
|
}
|
|
|
|
public final void compressImageAttachments(Context context, List<? extends Attachment<?>> list, Function1<? super List<? extends Attachment<?>>, Unit> function1) {
|
|
m.checkNotNullParameter(context, "context");
|
|
m.checkNotNullParameter(list, "attachments");
|
|
m.checkNotNullParameter(function1, "onCompressed");
|
|
w0 w0Var = w0.i;
|
|
CoroutineDispatcher coroutineDispatcher = j0.a;
|
|
c.q.a.k.a.G(w0Var, n.b, null, new SendUtils$compressImageAttachments$1(list, context, function1, null), 2, null);
|
|
}
|
|
|
|
public final Observable<SendPayload> getSendPayload(ContentResolver contentResolver, RestAPIParams.Message message, List<? extends Attachment<?>> list) {
|
|
SendPayload.Preprocessing preprocessing;
|
|
RestAPIParams.Message message2 = message;
|
|
m.checkNotNullParameter(contentResolver, "contentResolver");
|
|
m.checkNotNullParameter(message2, "apiParamMessage");
|
|
if (list == null) {
|
|
j jVar = new j(new SendPayload.ReadyToSend(message2, d0.t.n.emptyList()));
|
|
m.checkNotNullExpressionValue(jVar, "Observable.just(SendPayl…ramMessage, emptyList()))");
|
|
return jVar;
|
|
}
|
|
List<Attachment<?>> extractLinks = AttachmentUtilsKt.extractLinks(list, contentResolver);
|
|
String content = message.getContent();
|
|
if (content == null) {
|
|
content = "";
|
|
}
|
|
String appendLinks = AttachmentUtilsKt.appendLinks(content, extractLinks);
|
|
if (!m.areEqual(appendLinks, message.getContent())) {
|
|
message2 = RestAPIParams.Message.copy$default(message, appendLinks, null, null, null, null, null, null, 126, null);
|
|
}
|
|
List<Attachment<?>> uniqueifyNames = uniqueifyNames(u.minus((Iterable) list, (Iterable) extractLinks));
|
|
if (!uniqueifyNames.isEmpty()) {
|
|
if (uniqueifyNames.size() != 1) {
|
|
preprocessing = new SendPayload.Preprocessing(uniqueifyNames.size(), null, null);
|
|
} else {
|
|
Attachment attachment = (Attachment) u.first((List<? extends Object>) uniqueifyNames);
|
|
preprocessing = new SendPayload.Preprocessing(1, attachment.getDisplayName(), AttachmentUtilsKt.getMimeType(contentResolver, attachment.getUri(), attachment.getDisplayName()));
|
|
}
|
|
j jVar2 = new j(preprocessing);
|
|
ArrayList arrayList = new ArrayList(d0.t.o.collectionSizeOrDefault(uniqueifyNames, 10));
|
|
int i = 0;
|
|
for (Object obj : uniqueifyNames) {
|
|
int i2 = i + 1;
|
|
if (i < 0) {
|
|
d0.t.n.throwIndexOverflow();
|
|
}
|
|
arrayList.add(INSTANCE.getPart((Attachment) obj, contentResolver, a.l("file", i)));
|
|
i = i2;
|
|
}
|
|
Observable<SendPayload> m = Observable.m(jVar2, Observable.l(Observable.g0(new q(arrayList))).e0().F(new SendUtils$getSendPayload$2(message2)));
|
|
m.checkNotNullExpressionValue(m, "Observable.concat(\n …)\n }\n )");
|
|
return m;
|
|
}
|
|
j jVar3 = new j(new SendPayload.ReadyToSend(message2, d0.t.n.emptyList()));
|
|
m.checkNotNullExpressionValue(jVar3, "Observable.just(SendPayl…ramMessage, emptyList()))");
|
|
return jVar3;
|
|
}
|
|
|
|
@MainThread
|
|
public final void handleSendError(Error error, Context context, Function0<Unit> function0, Function0<Unit> function02) {
|
|
m.checkNotNullParameter(error, "error");
|
|
m.checkNotNullParameter(context, "context");
|
|
error.setShowErrorToasts(false);
|
|
Error.Response response = error.getResponse();
|
|
m.checkNotNullExpressionValue(response, "error.response");
|
|
Integer abortCodeMessageResId = RestAPIAbortMessages.getAbortCodeMessageResId(response.getCode());
|
|
if (abortCodeMessageResId != null) {
|
|
c.a.d.m.g(context, abortCodeMessageResId.intValue(), 0, null, 12);
|
|
} else if (error.getType() == Error.Type.REQUEST_TOO_LARGE) {
|
|
if (function0 != null) {
|
|
function0.mo1invoke();
|
|
}
|
|
} else if (error.getType() != Error.Type.FORBIDDEN_DISCORD) {
|
|
Error.Response response2 = error.getResponse();
|
|
m.checkNotNullExpressionValue(response2, "error.response");
|
|
if (response2.isKnownResponse() || error.getType() == Error.Type.NETWORK) {
|
|
error.setShowErrorToasts(true);
|
|
} else {
|
|
c.a.d.m.g(context, R.string.network_error_bad_request, 0, null, 12);
|
|
}
|
|
} else if (function02 != null) {
|
|
function02.mo1invoke();
|
|
}
|
|
error.showToasts(context);
|
|
}
|
|
|
|
@VisibleForTesting
|
|
public final <T> Attachment<T> renamedWithDuplicateCount(Attachment<? extends T> attachment, int i) {
|
|
String str;
|
|
m.checkNotNullParameter(attachment, "$this$renamedWithDuplicateCount");
|
|
long id2 = attachment.getId();
|
|
Uri uri = attachment.getUri();
|
|
Pair<String, String> splitFileExtension = splitFileExtension(attachment.getDisplayName());
|
|
String component1 = splitFileExtension.component1();
|
|
String component2 = splitFileExtension.component2();
|
|
if (component2 != null) {
|
|
str = component1 + '-' + i + '.' + component2;
|
|
} else {
|
|
str = component1 + '-' + i;
|
|
}
|
|
return new Attachment<>(id2, uri, str, attachment.getData());
|
|
}
|
|
|
|
@VisibleForTesting
|
|
public final List<Attachment<?>> uniqueifyNames(List<? extends Attachment<?>> list) {
|
|
m.checkNotNullParameter(list, "attachmentParts");
|
|
return d0.f0.q.toList(l.sequence(new SendUtils$uniqueifyNames$1(list, null)));
|
|
}
|
|
}
|