Skip to content

Commit

Permalink
Add ChannelCacheViewTest
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed Dec 30, 2023
1 parent fd1017b commit 0394756
Show file tree
Hide file tree
Showing 4 changed files with 383 additions and 72 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ dependencies {

testImplementation("org.junit.jupiter:junit-jupiter:5.8.2")
testImplementation("org.reflections:reflections:0.10.2")
testImplementation("org.mockito:mockito-core:5.8.0")
}

val compileJava: JavaCompile by tasks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@

package net.dv8tion.jda.internal.entities.channel.middleman;

import net.dv8tion.jda.api.entities.channel.attribute.ICategorizableChannel;
import net.dv8tion.jda.api.entities.channel.attribute.IPositionableChannel;
import net.dv8tion.jda.api.entities.channel.concrete.Category;
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
import net.dv8tion.jda.internal.entities.GuildImpl;
import net.dv8tion.jda.internal.entities.channel.AbstractChannelImpl;
import net.dv8tion.jda.internal.entities.channel.mixin.middleman.GuildChannelMixin;
import net.dv8tion.jda.internal.utils.Checks;
import net.dv8tion.jda.internal.utils.ChannelUtil;

import javax.annotation.Nonnull;

Expand All @@ -48,72 +44,6 @@ public GuildImpl getGuild()
@Override
public int compareTo(@Nonnull GuildChannel o)
{
Checks.notNull(o, "Channel");

// Check thread positions
ThreadChannel thisThread = this instanceof ThreadChannel ? (ThreadChannel) this : null;
ThreadChannel otherThread = o instanceof ThreadChannel ? (ThreadChannel) o : null;

if (thisThread != null && otherThread == null)
return thisThread.getParentChannel().compareTo(o);
if (thisThread == null && otherThread != null)
return this.compareTo(otherThread.getParentChannel());
if (thisThread != null)
{
// If they are threads on the same channel
if (thisThread.getParentChannel().equals(otherThread.getParentChannel()))
return Long.compare(o.getIdLong(), id); // threads are ordered ascending by age
// If they are threads on different channels
return thisThread.getParentChannel().compareTo(otherThread.getParentChannel());
}

// Check category positions
Category thisParent = this instanceof ICategorizableChannel ? ((ICategorizableChannel) this).getParentCategory() : null;
Category otherParent = o instanceof ICategorizableChannel ? ((ICategorizableChannel) o).getParentCategory() : null;

if (thisParent != null && otherParent == null)
{
if (o instanceof Category)
{
// The other channel is the parent category of this channel
if (o.equals(thisParent))
return 1;
// The other channel is another category
return thisParent.compareTo(o);
}
return 1;
}
if (thisParent == null && otherParent != null)
{
if (this instanceof Category)
{
// This channel is parent of other channel
if (this.equals(otherParent))
return -1;
// This channel is a category higher than the other channel's parent category
return this.compareTo(otherParent); //safe use of recursion since no circular parents exist
}
return -1;
}
// Both channels are in different categories, compare the categories instead
if (thisParent != null && !thisParent.equals(otherParent))
return thisParent.compareTo(otherParent);

// Check sort bucket (text/message is above audio)
if (getType().getSortBucket() != o.getType().getSortBucket())
return Integer.compare(getType().getSortBucket(), o.getType().getSortBucket());

// Check actual position
if (o instanceof IPositionableChannel && this instanceof IPositionableChannel)
{
IPositionableChannel oPositionableChannel = (IPositionableChannel) o;
IPositionableChannel thisPositionableChannel = (IPositionableChannel) this;

if (thisPositionableChannel.getPositionRaw() != oPositionableChannel.getPositionRaw())
return Integer.compare(thisPositionableChannel.getPositionRaw(), oPositionableChannel.getPositionRaw());
}

// last resort by id
return Long.compareUnsigned(id, o.getIdLong());
return ChannelUtil.compare(this, o);
}
}
88 changes: 88 additions & 0 deletions src/main/java/net/dv8tion/jda/internal/utils/ChannelUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

import net.dv8tion.jda.api.entities.channel.Channel;
import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.channel.attribute.ICategorizableChannel;
import net.dv8tion.jda.api.entities.channel.attribute.IPositionableChannel;
import net.dv8tion.jda.api.entities.channel.concrete.Category;
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;

import java.util.EnumSet;

Expand Down Expand Up @@ -45,4 +50,87 @@ public static <T extends Channel> T safeChannelCast(Object instance, Class<T> to
String cleanedClassName = instance.getClass().getSimpleName().replace("Impl", "");
throw new IllegalStateException(Helpers.format("Cannot convert channel of type %s to %s!", cleanedClassName, toObjectClass.getSimpleName()));
}

public static int compare(GuildChannel a, GuildChannel b)
{
Checks.notNull(b, "Channel");

// Check thread positions
ThreadChannel thisThread = a instanceof ThreadChannel ? (ThreadChannel) a : null;
ThreadChannel otherThread = b instanceof ThreadChannel ? (ThreadChannel) b : null;

if (thisThread != null && otherThread == null)
{
// Thread should be below its parent
if (thisThread.getParentChannel().getIdLong() == b.getIdLong())
return 1;
// Otherwise compare parents
return thisThread.getParentChannel().compareTo(b);
}
if (thisThread == null && otherThread != null)
{
// Thread should be below its parent
if (otherThread.getParentChannel().getIdLong() == a.getIdLong())
return -1;
// Otherwise compare parents
return a.compareTo(otherThread.getParentChannel());
}
if (thisThread != null)
{
// If they are threads on the same channel
if (thisThread.getParentChannel().getIdLong() == otherThread.getParentChannel().getIdLong())
return Long.compare(b.getIdLong(), a.getIdLong()); // threads are ordered ascending by age
// If they are threads on different channels
return thisThread.getParentChannel().compareTo(otherThread.getParentChannel());
}

// Check category positions
Category thisParent = a instanceof ICategorizableChannel ? ((ICategorizableChannel) a).getParentCategory() : null;
Category otherParent = b instanceof ICategorizableChannel ? ((ICategorizableChannel) b).getParentCategory() : null;

if (thisParent != null && otherParent == null)
{
if (b instanceof Category)
{
// The other channel is the parent category of this channel
if (b.getIdLong() == thisParent.getIdLong())
return 1;
// The other channel is another category
return thisParent.compareTo(b);
}
return 1;
}
if (thisParent == null && otherParent != null)
{
if (a instanceof Category)
{
// This channel is parent of other channel
if (a.getIdLong() == otherParent.getIdLong())
return -1;
// This channel is a category higher than the other channel's parent category
return a.compareTo(otherParent); //safe use of recursion since no circular parents exist
}
return -1;
}
// Both channels are in different categories, compare the categories instead
if (thisParent != null && !thisParent.equals(otherParent))
return thisParent.compareTo(otherParent);

// Check sort bucket (text/message is above audio)
if (a.getType().getSortBucket() != b.getType().getSortBucket())
return Integer.compare(a.getType().getSortBucket(), b.getType().getSortBucket());

// Check actual position
if (b instanceof IPositionableChannel && a instanceof IPositionableChannel)
{
IPositionableChannel oPositionableChannel = (IPositionableChannel) b;
IPositionableChannel thisPositionableChannel = (IPositionableChannel) a;

if (thisPositionableChannel.getPositionRaw() != oPositionableChannel.getPositionRaw())
return Integer.compare(thisPositionableChannel.getPositionRaw(), oPositionableChannel.getPositionRaw());
}

// last resort by id
return Long.compareUnsigned(a.getIdLong(), b.getIdLong());
}
}
Loading

0 comments on commit 0394756

Please sign in to comment.