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

279 lines
14 KiB
Java

package com.discord.utilities.threads;
import android.content.Context;
import com.airbnb.lottie.parser.AnimatableValueParser;
import com.discord.R;
import com.discord.api.channel.Channel;
import com.discord.api.guild.GuildFeature;
import com.discord.api.permission.Permission;
import com.discord.api.thread.ThreadMetadata;
import com.discord.models.domain.ModelAuditLogEntry;
import com.discord.models.domain.ModelNotificationSettings;
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.SnowflakeUtils;
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.NewThreadsPermissionsFeatureFlag;
import com.discord.widgets.chat.list.ViewThreadsFeatureFlag;
import d0.o;
import d0.t.g0;
import d0.t.h0;
import d0.t.n;
import d0.t.t;
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.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;
private static final LinkedHashMap<Integer, Integer> THREAD_AUTO_ARCHIVE_DURATION_NAMES = h0.linkedMapOf(o.to(60, Integer.valueOf((int) R.string.auto_archive_duration_1_hour)), o.to(1440, Integer.valueOf((int) R.string.auto_archive_duration_24_hours)), o.to(Integer.valueOf((int) ThreadArchiveDurations.THREE_DAYS_IN_MINUTES), Integer.valueOf((int) R.string.auto_archive_duration_3_days)), o.to(Integer.valueOf((int) ThreadArchiveDurations.SEVEN_DAYS_IN_MINUTES), Integer.valueOf((int) R.string.auto_archive_duration_1_week)));
/* compiled from: ThreadUtils.kt */
public static final class ThreadArchiveDurations {
public static final ThreadArchiveDurations INSTANCE = new ThreadArchiveDurations();
public static final int ONE_DAY_IN_MINUTES = 1440;
public static final int ONE_HOUR_IN_MINUTES = 60;
public static final int SEVEN_DAYS_IN_MINUTES = 10080;
public static final int THREE_DAYS_IN_MINUTES = 4320;
private ThreadArchiveDurations() {
}
}
private ThreadUtils() {
}
public static /* synthetic */ boolean canCreatePrivateThread$default(ThreadUtils threadUtils, Long l, Channel channel, Guild guild, boolean z2, int i, Object obj) {
if ((i & 8) != 0) {
z2 = true;
}
return threadUtils.canCreatePrivateThread(l, channel, guild, z2);
}
public static /* synthetic */ Observable createThread$default(ThreadUtils threadUtils, long j, Long l, int i, String str, Integer num, String str2, int i2, Object obj) {
return threadUtils.createThread(j, l, i, str, (i2 & 16) != 0 ? null : num, str2);
}
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(R.plurals.duration_hours_hours, i / 60);
m.checkNotNullExpressionValue(quantityString, "context.resources.getQua…ours_hours, minutes / 60)");
return quantityString;
}
public final boolean canCreatePrivateThread(Long l, Channel channel, Guild guild, boolean z2) {
if (guild == null || channel == null) {
return false;
}
boolean hasAccessWrite = PermissionUtils.INSTANCE.hasAccessWrite(channel, l);
boolean can = PermissionUtils.can(Permission.CREATE_PRIVATE_THREADS, l);
if (!NewThreadsPermissionsFeatureFlag.Companion.getINSTANCE().isEnabled(guild.getId())) {
can = hasAccessWrite && can;
}
return isThreadsEnabled(guild.getId()) && can && (AnimatableValueParser.B1(channel) ^ true) && (!z2 || guild.hasFeature(GuildFeature.PRIVATE_THREADS)) && !AnimatableValueParser.d1(channel);
}
public final boolean canCreatePublicThread(Long l, Channel channel, Message message, Guild guild) {
if (guild == null || channel == null) {
return false;
}
boolean hasAccessWrite = PermissionUtils.INSTANCE.hasAccessWrite(channel, l);
boolean can = PermissionUtils.can(Permission.CREATE_PUBLIC_THREADS, l);
boolean z2 = message == null || PermissionUtils.can(Permission.READ_MESSAGE_HISTORY, l);
return isThreadsEnabled(guild.getId()) && (!NewThreadsPermissionsFeatureFlag.Companion.getINSTANCE().isEnabled(guild.getId()) ? !(!hasAccessWrite || !can || !z2) : !(!can || !z2)) && ((message == null || !message.isSystemMessage()) && !AnimatableValueParser.B1(channel) && (message == null || !message.hasThread()));
}
public final boolean canCreateThread(Long l, Channel channel, Message message, Guild guild) {
return canCreatePublicThread(l, channel, message, guild) || canCreatePrivateThread$default(this, l, channel, guild, false, 8, null);
}
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(Permission.MANAGE_THREADS, 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(Permission.MANAGE_THREADS, l) && PermissionUtils.INSTANCE.hasAccessWrite(channel, l) : PermissionUtils.INSTANCE.hasAccessWrite(channel, l);
}
public final boolean canViewAllPrivateThreads(Long l) {
return PermissionUtils.can(Permission.MANAGE_THREADS, l) && PermissionUtils.can(Permission.READ_MESSAGE_HISTORY, l);
}
public final boolean canViewAllPublicArchivedThreads(Channel channel, Long l) {
return channel != null && PermissionUtils.INSTANCE.hasAccess(channel, l) && PermissionUtils.can(Permission.READ_MESSAGE_HISTORY, l);
}
public final long computeThreadAutoArchiveTimeMs(Channel channel) {
String a;
m.checkNotNullParameter(channel, "channel");
long i = (channel.i() >>> 22) + SnowflakeUtils.DISCORD_EPOCH;
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 num) {
Integer valueOf = joinedThread != null ? Integer.valueOf(joinedThread.getFlags()) : null;
if (valueOf != null) {
if ((valueOf.intValue() & 2) == 0) {
if ((valueOf.intValue() & 4) != 0) {
return 4;
}
if ((valueOf.intValue() & 8) == 0 && !z2) {
int i = ModelNotificationSettings.FREQUENCY_MENTIONS;
if (num != null && num.intValue() == i) {
return 4;
}
int i2 = ModelNotificationSettings.FREQUENCY_NOTHING;
if (num != null && num.intValue() == i2) {
}
}
}
return 2;
}
return 8;
}
public final Observable<Channel> createThread(long j, Long l, int i, String str, Integer num, String str2) {
m.checkNotNullParameter(str, ModelAuditLogEntry.CHANGE_KEY_NAME);
RestAPIParams.ThreadCreationSettings threadCreationSettings = new RestAPIParams.ThreadCreationSettings(str, i, num);
return l == null ? RestAPI.Companion.getApi().createThread(j, str2, threadCreationSettings) : RestAPI.Companion.getApi().createThreadFromMessage(j, l.longValue(), str2, threadCreationSettings);
}
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 isThreadModerator(Long l) {
return PermissionUtils.can(Permission.MANAGE_THREADS, l);
}
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.B1(channel)) ? sortThreadsByJoinedDate : prependSelectedThread(sortThreadsByJoinedDate, channel);
}
}