Skip to content

Commit

Permalink
Hold strong reference to user from private channel (#1176)
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed Dec 27, 2019
1 parent 4426e2e commit c1e4242
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/main/java/net/dv8tion/jda/internal/JDAImpl.java
Expand Up @@ -502,6 +502,7 @@ public ExecutorService getCallbackPool()

@Nonnull
@Override
@SuppressWarnings("ConstantConditions") // this can't really happen unless you pass bad configs
public OkHttpClient getHttpClient()
{
return sessionConfig.getHttpClient();
Expand Down
Expand Up @@ -974,11 +974,18 @@ public VoiceChannel createVoiceChannel(GuildImpl guild, DataObject json, long gu

public PrivateChannel createPrivateChannel(DataObject json)
{
final long channelId = json.getUnsignedLong("id");
PrivateChannel channel = api.getPrivateChannelById(channelId);
if (channel == null)
channel = api.getFakePrivateChannelMap().get(channelId);
if (channel != null)
return channel;

DataObject recipient = json.hasKey("recipients") ?
json.getArray("recipients").getObject(0) :
json.getObject("recipient");
final long userId = recipient.getLong("id");
UserImpl user = (UserImpl) getJDA().getUsersView().get(userId);
UserImpl user = (UserImpl) getJDA().getUserById(userId);
if (user == null)
{ //The getJDA() can give us private channels connected to Users that we can no longer communicate with.
// As such, make a fake user and fake private channel.
Expand All @@ -1002,7 +1009,7 @@ public PrivateChannel createPrivateChannel(DataObject json, UserImpl user)
getJDA().getFakePrivateChannelMap().put(channelId, priv);
getJDA().getFakeUserMap().put(user.getIdLong(), user);
}
else
else if (api.isGuildSubscriptions())
{
SnowflakeCacheViewImpl<PrivateChannel> privateView = getJDA().getPrivateChannelsView();
try (UnlockHook hook = privateView.writeLock())
Expand Down
Expand Up @@ -22,10 +22,8 @@
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.requests.restaction.MessageAction;
import net.dv8tion.jda.api.utils.AttachmentOption;
import net.dv8tion.jda.internal.JDAImpl;
import net.dv8tion.jda.internal.requests.RestActionImpl;
import net.dv8tion.jda.internal.requests.Route;
import net.dv8tion.jda.internal.utils.cache.SnowflakeReference;

import javax.annotation.Nonnull;
import java.io.InputStream;
Expand All @@ -36,24 +34,21 @@
public class PrivateChannelImpl implements PrivateChannel
{
private final long id;
private final JDAImpl api;
private final SnowflakeReference<User> user;

private final User user;
private long lastMessageId;
private boolean fake = false;

public PrivateChannelImpl(long id, User user)
{
this.id = id;
this.api = (JDAImpl) user.getJDA();
this.user = new SnowflakeReference<>(user, api::getUserById);
this.user = user;
}

@Nonnull
@Override
public User getUser()
{
return user.resolve();
return user;
}

@Override
Expand Down Expand Up @@ -89,7 +84,7 @@ public ChannelType getType()
@Override
public JDA getJDA()
{
return api;
return user.getJDA();
}

@Nonnull
Expand Down
Expand Up @@ -126,6 +126,6 @@ public EnumSet<ConfigFlag> getFlags()
@Nonnull
public static SessionConfig getDefault()
{
return new SessionConfig(null, null, null, null, ConfigFlag.getDefault(), 900, 250);
return new SessionConfig(null, new OkHttpClient(), null, null, ConfigFlag.getDefault(), 900, 250);
}
}
Expand Up @@ -76,6 +76,6 @@ public IAudioSendFactory getAudioSendFactory()
@Nonnull
public static ShardingSessionConfig getDefault()
{
return new ShardingSessionConfig(null, null, null, null, null, null, ConfigFlag.getDefault(), ShardingConfigFlag.getDefault(), 900, 250);
return new ShardingSessionConfig(null, null, new OkHttpClient(), null, null, null, ConfigFlag.getDefault(), ShardingConfigFlag.getDefault(), 900, 250);
}
}

0 comments on commit c1e4242

Please sign in to comment.