Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
Added TextChannel#getCachedHistory
Browse files Browse the repository at this point in the history
modified the version
  • Loading branch information
JoshiCodes committed Jun 9, 2023
1 parent 294aafb commit 8a8b6e3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>de.joshicodes</groupId>
<artifactId>rja</artifactId>
<version>1.0-alpha.9</version>
<version>1.0-alpha.9a</version>

<description>
Simple Java API for Revolt.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public MessageReceivedEvent handle(RJA rja, JsonObject object) {
}
rja.getChannelCache().stream().filter(c -> c.getId().equals(message.getChannelId())).findFirst().ifPresent(c -> {
if(c instanceof TextChannel tc) {
TextChannel.updateLastMessageId(tc, message.getId());
tc.getCachedHistory().add(message.getId());
}
});
return new MessageReceivedEvent(rja, author, message, textChannel);
Expand Down
28 changes: 17 additions & 11 deletions src/main/java/de/joshicodes/rja/object/channel/TextChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import de.joshicodes.rja.util.JsonUtil;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;

public abstract class TextChannel extends ServerChannel {

Expand Down Expand Up @@ -57,16 +59,14 @@ public String getId() {
return id;
}
};
tc.setLastMessageId(lastMessageId);
tc.cachedHistory = new ArrayList<>();
if(lastMessageId != null)
tc.cachedHistory.add(lastMessageId);
return tc;

}

private String lastMessageId;

public static void updateLastMessageId(TextChannel tc, String id) {
tc.setLastMessageId(id);
}
private List<String> cachedHistory;

abstract public String getServerId();
abstract public String getName();
Expand All @@ -76,7 +76,17 @@ public static void updateLastMessageId(TextChannel tc, String id) {

@Nullable
public String lastMessageId() {
return lastMessageId;
if(getCachedHistory().isEmpty()) {
return null;
}
if(getCachedHistory().size() > 1) {
return getCachedHistory().get(getCachedHistory().size() - 2);
}
return getCachedHistory().get(0);
}

public List<String> getCachedHistory() {
return cachedHistory;
}

abstract public boolean isNsfw();
Expand All @@ -99,8 +109,4 @@ public ChannelType getType() {
return ChannelType.TEXT_CHANNEL;
}

void setLastMessageId(String id) {
this.lastMessageId = id;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public Message fetch(RJA rja, int responseCode, JsonElement data) {
rja.cacheMessage(m);
rja.getChannelCache().stream().filter(c -> c.getId().equals(m.getChannelId())).findFirst().ifPresent(c -> {
if(c instanceof TextChannel tc) {
TextChannel.updateLastMessageId(tc, m.getId());
tc.getCachedHistory().add(m.getId());
}
});
return m;
Expand Down

0 comments on commit 8a8b6e3

Please sign in to comment.