discord-jadx/app/src/main/java/com/discord/utilities/permissions/PermissionUtils.java

314 lines
13 KiB
Java

package com.discord.utilities.permissions;
import c.d.b.a.a;
import com.airbnb.lottie.parser.AnimatableValueParser;
import com.discord.api.channel.Channel;
import com.discord.api.permission.Permission;
import com.discord.api.permission.PermissionOverwrite;
import com.discord.api.role.GuildRole;
import com.discord.api.stageinstance.StageInstance;
import com.discord.api.stageinstance.StageInstancePrivacyLevel;
import com.discord.models.domain.ModelAuditLogEntry;
import com.discord.models.member.GuildMember;
import com.discord.stores.StoreSlowMode;
import com.discord.utilities.PermissionOverwriteUtilsKt;
import com.discord.utilities.guildmember.GuildMemberUtilsKt;
import com.discord.widgets.chat.list.NewThreadsPermissionsFeatureFlag;
import d0.t.h0;
import d0.t.n;
import d0.z.d.m;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/* compiled from: PermissionUtils.kt */
public final class PermissionUtils {
public static final PermissionUtils INSTANCE = new PermissionUtils();
private PermissionUtils() {
}
private final long applyEveryone(long j, Map<Long, GuildRole> map) {
GuildRole guildRole = map != null ? map.get(Long.valueOf(j)) : null;
return guildRole != null ? guildRole.h() : Permission.DEFAULT;
}
private final long applyEveryoneOverwrites(long j, Collection<PermissionOverwrite> collection, long j2) {
Object obj;
boolean z2;
Iterator<T> it = collection.iterator();
while (true) {
if (!it.hasNext()) {
obj = null;
break;
}
obj = it.next();
if (((PermissionOverwrite) obj).e() == j) {
z2 = true;
continue;
} else {
z2 = false;
continue;
}
if (z2) {
break;
}
}
PermissionOverwrite permissionOverwrite = (PermissionOverwrite) obj;
return permissionOverwrite != null ? ((permissionOverwrite.d() & j2) ^ j2) | permissionOverwrite.c() : j2;
}
private final long applyRoleOverwrites(GuildMember guildMember, Collection<PermissionOverwrite> collection, long j, long j2) {
long j3;
boolean z2;
Object obj;
boolean z3;
Object obj2 = null;
long j4 = 0;
if (guildMember != null) {
long j5 = 0;
for (Long l : guildMember.getRoles()) {
long longValue = l.longValue();
Iterator<T> it = collection.iterator();
while (true) {
if (!it.hasNext()) {
obj = null;
break;
}
obj = it.next();
if (((PermissionOverwrite) obj).e() == longValue) {
z3 = true;
continue;
} else {
z3 = false;
continue;
}
if (z3) {
break;
}
}
PermissionOverwrite permissionOverwrite = (PermissionOverwrite) obj;
if (permissionOverwrite != null) {
j4 |= permissionOverwrite.c();
j5 |= permissionOverwrite.d();
}
}
j4 = j5;
j3 = j4;
} else {
j3 = 0;
}
long j6 = (j ^ (j & j4)) | j3;
Iterator<T> it2 = collection.iterator();
while (true) {
if (!it2.hasNext()) {
break;
}
Object next = it2.next();
if (((PermissionOverwrite) next).e() == j2) {
z2 = true;
continue;
} else {
z2 = false;
continue;
}
if (z2) {
obj2 = next;
break;
}
}
PermissionOverwrite permissionOverwrite2 = (PermissionOverwrite) obj2;
return permissionOverwrite2 != null ? ((permissionOverwrite2.d() & j6) ^ j6) | permissionOverwrite2.c() : j6;
}
private final long applyRoles(GuildMember guildMember, Map<Long, GuildRole> map, long j) {
if (guildMember != null) {
for (Long l : guildMember.getRoles()) {
GuildRole guildRole = map != null ? map.get(Long.valueOf(l.longValue())) : null;
if (guildRole != null) {
j |= guildRole.h();
}
}
}
return j;
}
public static final boolean can(long j, Long l) {
return ((l != null ? l.longValue() : 0) & j) == j;
}
public static final boolean canAndIsElevated(long j, Long l, boolean z2, int i) {
return can(j, l) && isElevated(j, z2, i);
}
public static final boolean canEveryone(long j, Channel channel, Channel channel2, Map<Long, GuildRole> map) {
m.checkNotNullParameter(channel, "channel");
m.checkNotNullParameter(map, "roles");
if (!AnimatableValueParser.B1(channel)) {
GuildRole guildRole = (GuildRole) a.t0(channel, map);
if (!(guildRole == null || (guildRole.h() & j) == j)) {
return false;
}
List<PermissionOverwrite> s2 = channel.s();
if (s2 == null) {
return true;
}
for (PermissionOverwrite permissionOverwrite : s2) {
if (PermissionOverwriteUtilsKt.denies(permissionOverwrite, j)) {
return false;
}
}
return true;
} else if (channel2 == null || AnimatableValueParser.u1(channel)) {
return false;
} else {
return canEveryone(j, channel2, null, map);
}
}
public static /* synthetic */ boolean canEveryone$default(long j, Channel channel, Channel channel2, Map map, int i, Object obj) {
if ((i & 4) != 0) {
channel2 = null;
}
return canEveryone(j, channel, channel2, map);
}
public static final boolean canManageGuildMembers(boolean z2, boolean z3, int i, Long l) {
return z2 || canAndIsElevated(8, l, z3, i) || canAndIsElevated(2, l, z3, i) || canAndIsElevated(4, l, z3, i) || canAndIsElevated(Permission.MANAGE_ROLES, l, z3, i) || can(Permission.MANAGE_NICKNAMES, l);
}
public static final long computeNonThreadPermissions(long j, long j2, long j3, GuildMember guildMember, Map<Long, GuildRole> map, Collection<PermissionOverwrite> collection) {
if (j3 == j) {
return Permission.ALL;
}
PermissionUtils permissionUtils = INSTANCE;
long applyRoles = permissionUtils.applyRoles(guildMember, map, permissionUtils.applyEveryone(j2, map));
return (applyRoles & 8) == 8 ? Permission.ALL : (collection == null || !(collection.isEmpty() ^ true)) ? applyRoles : permissionUtils.applyRoleOverwrites(guildMember, collection, permissionUtils.applyEveryoneOverwrites(j2, collection, applyRoles), j);
}
public static final long computePermissions(long j, Channel channel, Channel channel2, long j2, GuildMember guildMember, Map<Long, GuildRole> map, Map<Long, StageInstance> map2, boolean z2) {
long j3;
long j4;
m.checkNotNullParameter(channel, "channel");
if (AnimatableValueParser.B1(channel)) {
j3 = channel2 == null ? 0 : computeThreadPermissions(j, channel, channel2, j2, guildMember, map, z2);
} else {
long f = channel.f();
List<PermissionOverwrite> s2 = channel.s();
if (s2 == null) {
s2 = n.emptyList();
}
j3 = computeNonThreadPermissions(j, f, j2, guildMember, map, s2);
}
if (!GuildMemberUtilsKt.isLurker(guildMember)) {
return j3;
}
Map<Long, StageInstance> emptyMap = map2 != null ? map2 : h0.emptyMap();
m.checkNotNullParameter(channel, "$this$computeLurkerPermissionsAllowList");
m.checkNotNullParameter(emptyMap, "stageInstances");
if (AnimatableValueParser.x1(channel)) {
StageInstance stageInstance = (StageInstance) a.c(channel, emptyMap);
if ((stageInstance != null ? stageInstance.e() : null) == StageInstancePrivacyLevel.PUBLIC) {
j4 = Permission.AllowList.LURKER_STAGE_CHANNEL;
return j3 & j4;
}
}
j4 = Permission.AllowList.LURKER_DEFAULT;
return j3 & j4;
}
public static final long computeThreadPermissions(long j, Channel channel, Channel channel2, long j2, GuildMember guildMember, Map<Long, GuildRole> map, boolean z2) {
m.checkNotNullParameter(channel, "thread");
m.checkNotNullParameter(channel2, "parentChannel");
long f = channel2.f();
List<PermissionOverwrite> s2 = channel2.s();
if (s2 == null) {
s2 = n.emptyList();
}
long computeNonThreadPermissions = computeNonThreadPermissions(j, f, j2, guildMember, map, s2);
if (NewThreadsPermissionsFeatureFlag.Companion.getINSTANCE().isEnabled(channel.f())) {
if (AnimatableValueParser.u1(channel) && !z2) {
can(Permission.MANAGE_THREADS, Long.valueOf(computeNonThreadPermissions));
}
return can(Permission.SEND_MESSAGES_IN_THREADS, Long.valueOf(computeNonThreadPermissions)) ? computeNonThreadPermissions | Permission.SEND_MESSAGES : -2049 & computeNonThreadPermissions;
}
if (AnimatableValueParser.u1(channel)) {
if (can(Permission.CREATE_PRIVATE_THREADS, Long.valueOf(computeNonThreadPermissions))) {
computeNonThreadPermissions |= Permission.SEND_MESSAGES;
}
if (!z2 && !can(Permission.MANAGE_THREADS, Long.valueOf(computeNonThreadPermissions))) {
return 0;
}
} else if (can(Permission.CREATE_PUBLIC_THREADS, Long.valueOf(computeNonThreadPermissions))) {
computeNonThreadPermissions |= Permission.SEND_MESSAGES;
}
return computeNonThreadPermissions;
}
public static final boolean hasAccess(Channel channel, Map<Long, Long> map) {
m.checkNotNullParameter(channel, "channel");
m.checkNotNullParameter(map, ModelAuditLogEntry.CHANGE_KEY_PERMISSIONS);
return INSTANCE.hasAccess(channel, (Long) a.c(channel, map));
}
public static final boolean isElevated(long j, boolean z2, int i) {
if ((Permission.ELEVATED & j) != j) {
return true;
}
return isElevated(z2, i);
}
public static final boolean isElevated(boolean z2, int i) {
if (i == 0) {
return true;
}
return z2;
}
public final boolean canEveryoneRole(long j, Channel channel, Map<Long, GuildRole> map) {
m.checkNotNullParameter(channel, "channel");
m.checkNotNullParameter(map, "roles");
long f = channel.f();
GuildRole guildRole = map.get(Long.valueOf(f));
return canRole(j, guildRole, guildRole != null ? AnimatableValueParser.F0(channel, f) : null);
}
public final boolean canRole(long j, GuildRole guildRole, PermissionOverwrite permissionOverwrite) {
if (guildRole == null) {
return false;
}
long h = guildRole.h();
long j2 = 0;
long c2 = permissionOverwrite != null ? permissionOverwrite.c() : 0;
if (permissionOverwrite != null) {
j2 = permissionOverwrite.d();
}
return ((((h & j2) ^ h) | c2) & j) == j;
}
public final long computeChannelOverwrite(long j, long j2, GuildMember guildMember, Collection<PermissionOverwrite> collection) {
if (collection == null || !(!collection.isEmpty())) {
return 0;
}
return applyRoleOverwrites(guildMember, collection, applyEveryoneOverwrites(j2, collection, 0), j);
}
public final boolean hasAccess(Channel channel, Long l) {
m.checkNotNullParameter(channel, "channel");
if (AnimatableValueParser.t1(channel)) {
return true;
}
return can((AnimatableValueParser.l1(channel) || AnimatableValueParser.f1(channel)) ? Permission.VIEW_CHANNEL : Permission.CONNECT, l);
}
public final boolean hasAccessWrite(Channel channel, Long l) {
m.checkNotNullParameter(channel, "channel");
return AnimatableValueParser.t1(channel) || can(3072, l);
}
public final boolean hasBypassSlowmodePermissions(Long l, StoreSlowMode.Type type) {
m.checkNotNullParameter(type, "type");
return m.areEqual(type, StoreSlowMode.Type.MessageSend.INSTANCE) ? can(16, l) || can(Permission.MANAGE_MESSAGES, l) : can(Permission.MANAGE_THREADS, l);
}
}