discord-jadx/app/src/main/java/com/discord/utilities/threads/ThreadUtils.java

275 lines
12 KiB
Java

package com.discord.utilities.threads;
import android.content.Context;
import com.airbnb.lottie.parser.AnimatableValueParser;
import com.discord.api.channel.Channel;
import com.discord.api.guild.GuildFeature;
import com.discord.api.thread.ThreadMetadata;
import com.discord.models.guild.Guild;
import com.discord.models.message.Message;
import com.discord.models.user.User;
import com.discord.restapi.RestAPIParams;
import com.discord.stores.StoreThreadsActiveJoined;
import com.discord.stores.StoreThreadsJoined;
import com.discord.utilities.permissions.PermissionUtils;
import com.discord.utilities.rest.RestAPI;
import com.discord.utilities.time.Clock;
import com.discord.utilities.time.ClockFactory;
import com.discord.utilities.time.TimeUtils;
import com.discord.widgets.chat.list.CreateThreadsFeatureFlag;
import com.discord.widgets.chat.list.ViewThreadsFeatureFlag;
import d0.d0.f;
import d0.o;
import d0.t.g0;
import d0.t.h0;
import d0.t.n;
import d0.t.t;
import d0.t.u;
import d0.z.d.m;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import rx.Observable;
/* compiled from: ThreadUtils.kt */
public final class ThreadUtils {
public static final int DEFAULT_AUTO_ARCHIVE_DURATION = 1440;
public static final ThreadUtils INSTANCE = new ThreadUtils();
public static final int MAX_DISPLAYED_THREAD_BROWSER_ICON_COUNT = 99;
public static final int MAX_THREAD_NAME_LENGTH = 30;
private static final List<Integer> THREAD_AUTO_ARCHIVE_DURATIONS;
private static final LinkedHashMap<Integer, Integer> THREAD_AUTO_ARCHIVE_DURATION_NAMES;
static {
LinkedHashMap<Integer, Integer> linkedMapOf = h0.linkedMapOf(o.to(60, 2131886778), o.to(1440, 2131886780), o.to(4320, 2131886781), o.to(10080, 2131886779));
THREAD_AUTO_ARCHIVE_DURATION_NAMES = linkedMapOf;
Set<Integer> keySet = linkedMapOf.keySet();
m.checkNotNullExpressionValue(keySet, "THREAD_AUTO_ARCHIVE_DURATION_NAMES.keys");
THREAD_AUTO_ARCHIVE_DURATIONS = u.toList(keySet);
}
private ThreadUtils() {
}
public static /* synthetic */ Observable createThread$default(ThreadUtils threadUtils, long j, Long l, int i, String str, Integer num, int i2, Object obj) {
if ((i2 & 16) != 0) {
num = null;
}
return threadUtils.createThread(j, l, i, str, num);
}
public static /* synthetic */ long getThreadAckMessageTimestamp$default(ThreadUtils threadUtils, Channel channel, Long l, Long l2, Clock clock, int i, Object obj) {
if ((i & 4) != 0) {
clock = ClockFactory.get();
}
return threadUtils.getThreadAckMessageTimestamp(channel, l, l2, clock);
}
private final Comparator<StoreThreadsActiveJoined.ActiveJoinedThread> getThreadTimestampComparator() {
return ThreadUtils$getThreadTimestampComparator$1.INSTANCE;
}
/* JADX DEBUG: Multi-variable search result rejected for r10v0, resolved type: java.util.Map<java.lang.Long, ? extends java.util.Collection<com.discord.api.channel.Channel>> */
/* JADX WARN: Multi-variable type inference failed */
private final Map<Long, Collection<Channel>> prependSelectedThread(Map<Long, ? extends Collection<Channel>> map, Channel channel) {
boolean z2;
boolean z3;
Collection collection = (Collection) map.get(Long.valueOf(channel.r()));
if (collection == null) {
collection = n.emptyList();
}
if (!(collection instanceof Collection) || !collection.isEmpty()) {
Iterator it = collection.iterator();
while (true) {
if (!it.hasNext()) {
break;
}
if (((Channel) it.next()).h() == channel.h()) {
z3 = true;
continue;
} else {
z3 = false;
continue;
}
if (z3) {
z2 = true;
break;
}
}
}
z2 = false;
if (z2) {
return map;
}
Map<Long, Collection<Channel>> mutableMap = h0.toMutableMap(map);
Long valueOf = Long.valueOf(channel.r());
List mutableListOf = n.mutableListOf(channel);
mutableListOf.addAll(collection);
mutableMap.put(valueOf, mutableListOf);
return mutableMap;
}
private final Map<Long, Collection<Channel>> sortThreadsByJoinedDate(Map<Long, ? extends Map<Long, StoreThreadsActiveJoined.ActiveJoinedThread>> map) {
LinkedHashMap linkedHashMap = new LinkedHashMap(g0.mapCapacity(map.size()));
Iterator<T> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getKey();
SortedSet<StoreThreadsActiveJoined.ActiveJoinedThread> sortedSet = t.toSortedSet(((Map) entry.getValue()).values(), INSTANCE.getThreadTimestampComparator());
ArrayList arrayList = new ArrayList(d0.t.o.collectionSizeOrDefault(sortedSet, 10));
for (StoreThreadsActiveJoined.ActiveJoinedThread activeJoinedThread : sortedSet) {
arrayList.add(activeJoinedThread.getChannel());
}
linkedHashMap.put(key, arrayList);
}
return linkedHashMap;
}
public final String autoArchiveDurationName(Context context, int i) {
m.checkNotNullParameter(context, "context");
Integer num = THREAD_AUTO_ARCHIVE_DURATION_NAMES.get(Integer.valueOf(i));
if (num != null) {
m.checkNotNullExpressionValue(num, "it");
String string = context.getString(num.intValue());
if (string != null) {
return string;
}
}
String quantityString = context.getResources().getQuantityString(2131755065, i / 60);
m.checkNotNullExpressionValue(quantityString, "context.resources.getQua…ours_hours, minutes / 60)");
return quantityString;
}
public final boolean canCreatePrivateThread(Long l, Channel channel, Guild guild) {
if (guild == null || channel == null) {
return false;
}
return isThreadsEnabled(guild.getId()) && (PermissionUtils.INSTANCE.hasAccessWrite(channel, l) && PermissionUtils.can(68719476736L, l) && PermissionUtils.can(65536, l)) && (AnimatableValueParser.z1(channel) ^ true) && guild.hasFeature(GuildFeature.PRIVATE_THREADS);
}
public final boolean canCreatePublicThread(Long l, Channel channel, Message message, Guild guild) {
if (guild == null || channel == null) {
return false;
}
return isThreadsEnabled(guild.getId()) && (PermissionUtils.INSTANCE.hasAccessWrite(channel, l) && PermissionUtils.can(34359738368L, l)) && ((message == null || !message.isSystemMessage()) && !AnimatableValueParser.z1(channel) && (message == null || !message.hasThread()));
}
public final boolean canCreateThread(Long l, Channel channel, Message message, Guild guild) {
return canCreatePublicThread(l, channel, message, guild) || canCreatePrivateThread(l, channel, guild);
}
public final boolean canManageThread(User user, Channel channel, Long l) {
m.checkNotNullParameter(user, "user");
m.checkNotNullParameter(channel, "channel");
if (channel.q() != user.getId() || !PermissionUtils.INSTANCE.hasAccessWrite(channel, l)) {
return PermissionUtils.can(17179869184L, l) && PermissionUtils.INSTANCE.hasAccess(channel, l);
}
return true;
}
public final boolean canUnarchiveThread(Channel channel, Long l) {
m.checkNotNullParameter(channel, "channel");
ThreadMetadata y2 = channel.y();
return m.areEqual(y2 != null ? Boolean.valueOf(y2.d()) : null, Boolean.TRUE) ? PermissionUtils.can(17179869184L, l) && PermissionUtils.INSTANCE.hasAccessWrite(channel, l) : PermissionUtils.INSTANCE.hasAccessWrite(channel, l);
}
public final boolean canViewAllPrivateThreads(Long l) {
return PermissionUtils.can(17179869184L, l) && PermissionUtils.can(65536, l);
}
public final boolean canViewAllPublicArchivedThreads(Channel channel, Long l) {
return channel != null && PermissionUtils.INSTANCE.hasAccess(channel, l) && PermissionUtils.can(65536, l);
}
public final long computeThreadAutoArchiveTimeMs(Channel channel) {
String a;
m.checkNotNullParameter(channel, "channel");
long i = (channel.i() >>> 22) + 1420070400000L;
ThreadMetadata y2 = channel.y();
int c2 = (y2 != null ? y2.c() : 0) * 60 * 1000;
ThreadMetadata y3 = channel.y();
return Math.max(i, (y3 == null || (a = y3.a()) == null) ? 0 : TimeUtils.parseUTCDate(a)) + ((long) c2);
}
public final int computeThreadNotificationSetting(StoreThreadsJoined.JoinedThread joinedThread, boolean z2) {
Integer valueOf = joinedThread != null ? Integer.valueOf(joinedThread.getFlags()) : null;
if (valueOf == null) {
return 8;
}
if ((valueOf.intValue() & 2) == 0) {
if ((valueOf.intValue() & 4) == 0) {
if ((valueOf.intValue() & 8) != 0) {
return 8;
}
if (!z2 && (valueOf.intValue() & 1) != 0) {
}
}
return 4;
}
return 2;
}
public final Observable<Channel> createThread(long j, Long l, int i, String str, Integer num) {
m.checkNotNullParameter(str, "name");
RestAPIParams.ThreadCreationSettings threadCreationSettings = new RestAPIParams.ThreadCreationSettings(str, i, num);
return l == null ? RestAPI.Companion.getApi().createThread(j, threadCreationSettings) : RestAPI.Companion.getApi().createThreadFromMessage(j, l.longValue(), threadCreationSettings);
}
public final String generateDefaultName(String str, String str2) {
m.checkNotNullParameter(str, "messageText");
String str3 = "";
if (d0.g0.t.isBlank(str)) {
str = str2 != null ? str2 : str3;
}
StringBuilder sb = new StringBuilder();
String substring = str.substring(0, f.coerceAtMost(30, str.length()));
m.checkNotNullExpressionValue(substring, "(this as java.lang.Strin…ing(startIndex, endIndex)");
sb.append(substring);
if (str.length() > 30) {
str3 = "...";
}
sb.append(str3);
return sb.toString();
}
public final List<Integer> getTHREAD_AUTO_ARCHIVE_DURATIONS() {
return THREAD_AUTO_ARCHIVE_DURATIONS;
}
public final LinkedHashMap<Integer, Integer> getTHREAD_AUTO_ARCHIVE_DURATION_NAMES() {
return THREAD_AUTO_ARCHIVE_DURATION_NAMES;
}
public final long getThreadAckMessageTimestamp(Channel channel, Long l, Long l2, Clock clock) {
String a;
m.checkNotNullParameter(channel, "$this$getThreadAckMessageTimestamp");
m.checkNotNullParameter(clock, "clock");
long longValue = l != null ? l.longValue() : clock.currentTimeMillis();
long longValue2 = l2 != null ? l2.longValue() - ((long) 5000) : 0;
ThreadMetadata y2 = channel.y();
long max = Math.max(longValue2, (y2 == null || (a = y2.a()) == null) ? 0 : TimeUtils.parseUTCDate(a));
if (max == 0) {
max = TimeUtils.parseSnowflake(Long.valueOf(channel.h()));
}
return Math.max(longValue, max);
}
public final boolean isThreadsEnabled(long j) {
return ViewThreadsFeatureFlag.Companion.getINSTANCE().isEnabled() && CreateThreadsFeatureFlag.Companion.getINSTANCE().isEnabled(j);
}
public final Map<Long, Collection<Channel>> sortThreadsForChannelList(Map<Long, ? extends Map<Long, StoreThreadsActiveJoined.ActiveJoinedThread>> map, Channel channel) {
m.checkNotNullParameter(map, "threadsMap");
if (!ViewThreadsFeatureFlag.Companion.getINSTANCE().isEnabled()) {
return h0.emptyMap();
}
Map<Long, Collection<Channel>> sortThreadsByJoinedDate = sortThreadsByJoinedDate(map);
return (channel == null || !AnimatableValueParser.z1(channel)) ? sortThreadsByJoinedDate : prependSelectedThread(sortThreadsByJoinedDate, channel);
}
}