Skip to content

Commit

Permalink
Add poll voters pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed Apr 13, 2024
1 parent 827de6c commit b336ec2
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/main/java/net/dv8tion/jda/api/entities/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import net.dv8tion.jda.api.requests.restaction.MessageCreateAction;
import net.dv8tion.jda.api.requests.restaction.MessageEditAction;
import net.dv8tion.jda.api.requests.restaction.ThreadChannelAction;
import net.dv8tion.jda.api.requests.restaction.pagination.PollVotersPaginationAction;
import net.dv8tion.jda.api.requests.restaction.pagination.ReactionPaginationAction;
import net.dv8tion.jda.api.utils.AttachedFile;
import net.dv8tion.jda.api.utils.AttachmentProxy;
Expand All @@ -61,6 +62,7 @@
import net.dv8tion.jda.internal.JDAImpl;
import net.dv8tion.jda.internal.entities.ReceivedMessage;
import net.dv8tion.jda.internal.requests.FunctionalCallback;
import net.dv8tion.jda.internal.requests.restaction.pagination.PollVotersPaginationActionImpl;
import net.dv8tion.jda.internal.utils.Checks;
import net.dv8tion.jda.internal.utils.Helpers;
import net.dv8tion.jda.internal.utils.IOUtil;
Expand Down Expand Up @@ -705,6 +707,21 @@ default String getGuildId()
@CheckReturnValue
AuditableRestAction<Message> expirePoll();

/**
* Paginate the users who voted for a poll answer.
*
* @param answerId
* The id of the poll answer, usually the ordinal position of the answer (first is 1)
*
* @return {@link PollVotersPaginationAction}
*/
@Nonnull
@CheckReturnValue
default PollVotersPaginationAction retrievePollVoters(long answerId)
{
return new PollVotersPaginationActionImpl(getJDA(), getChannelId(), getId(), answerId);
}

/**
* Rows of interactive components such as {@link Button Buttons}.
* <br>You can use {@link MessageRequest#setComponents(LayoutComponent...)} to update these.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import net.dv8tion.jda.api.requests.restaction.MessageEditAction;
import net.dv8tion.jda.api.requests.restaction.pagination.MessagePaginationAction;
import net.dv8tion.jda.api.requests.restaction.pagination.PaginationAction;
import net.dv8tion.jda.api.requests.restaction.pagination.PollVotersPaginationAction;
import net.dv8tion.jda.api.requests.restaction.pagination.ReactionPaginationAction;
import net.dv8tion.jda.api.utils.AttachedFile;
import net.dv8tion.jda.api.utils.FileUpload;
Expand All @@ -50,6 +51,7 @@
import net.dv8tion.jda.internal.requests.restaction.MessageCreateActionImpl;
import net.dv8tion.jda.internal.requests.restaction.MessageEditActionImpl;
import net.dv8tion.jda.internal.requests.restaction.pagination.MessagePaginationActionImpl;
import net.dv8tion.jda.internal.requests.restaction.pagination.PollVotersPaginationActionImpl;
import net.dv8tion.jda.internal.requests.restaction.pagination.ReactionPaginationActionImpl;
import net.dv8tion.jda.internal.utils.Checks;

Expand Down Expand Up @@ -1090,6 +1092,43 @@ default AuditableRestAction<Message> expirePollById(long messageId)
return expirePollById(Long.toUnsignedString(messageId));
}

/**
* Paginate the users who voted for a poll answer.
*
* @param messageId
* The message id for the poll
* @param answerId
* The id of the poll answer, usually the ordinal position of the answer (first is 1)
*
* @throws IllegalArgumentException
* If the message id is not a valid snowflake
*
* @return {@link PollVotersPaginationAction}
*/
@Nonnull
@CheckReturnValue
default PollVotersPaginationAction retrievePollVotersById(@Nonnull String messageId, long answerId)
{
return new PollVotersPaginationActionImpl(getJDA(), getId(), messageId, answerId);
}

/**
* Paginate the users who voted for a poll answer.
*
* @param messageId
* The message id for the poll
* @param answerId
* The id of the poll answer, usually the ordinal position of the answer (first is 1)
*
* @return {@link PollVotersPaginationAction}
*/
@Nonnull
@CheckReturnValue
default PollVotersPaginationAction retrievePollVotersById(long messageId, long answerId)
{
return new PollVotersPaginationActionImpl(getJDA(), getId(), Long.toUnsignedString(messageId), answerId);
}

/**
* Creates a new {@link net.dv8tion.jda.api.entities.MessageHistory MessageHistory} object for each call of this method.
* <br>MessageHistory is <b>NOT</b> an internal message cache, but rather it queries the Discord servers for previously sent messages.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.dv8tion.jda.api.requests.restaction.pagination;

import net.dv8tion.jda.api.entities.User;

/**
* {@link PaginationAction PaginationAction} that paginates the votes for a poll answer.
*
* <p><b>Limits</b><br>
* Minimum - 1<br>
* Maximum - 1000
* <br>Default - 1000
*/
public interface PollVotersPaginationAction extends PaginationAction<User, PollVotersPaginationAction>
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.dv8tion.jda.internal.requests.restaction.pagination;

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.exceptions.ParsingException;
import net.dv8tion.jda.api.requests.Request;
import net.dv8tion.jda.api.requests.Response;
import net.dv8tion.jda.api.requests.Route;
import net.dv8tion.jda.api.requests.restaction.pagination.PollVotersPaginationAction;
import net.dv8tion.jda.api.utils.data.DataArray;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.internal.entities.EntityBuilder;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;

public class PollVotersPaginationActionImpl extends PaginationActionImpl<User, PollVotersPaginationAction> implements PollVotersPaginationAction
{
public PollVotersPaginationActionImpl(JDA jda, String channelId, String messageId, long answerId)
{
super(jda, Route.Messages.GET_POLL_ANSWER_VOTERS.compile(channelId, messageId, Long.toString(answerId)), 1, 1000, 1000);
this.order = PaginationOrder.FORWARD;
}

@NotNull
@Override
public EnumSet<PaginationOrder> getSupportedOrders()
{
return EnumSet.of(PaginationOrder.FORWARD);
}

@Override
protected long getKey(User it)
{
return it.getIdLong();
}

@Override
protected void handleSuccess(Response response, Request<List<User>> request)
{
DataArray array = response.getObject().getArray("users");
List<User> users = new ArrayList<>(array.length());
EntityBuilder builder = api.getEntityBuilder();
for (int i = 0; i < array.length(); i++)
{
try
{
DataObject object = array.getObject(i);
users.add(builder.createUser(object));
}
catch(ParsingException | NullPointerException e)
{
LOG.warn("Encountered an exception in PollVotersPaginationAction", e);
}
}

if (!users.isEmpty())
{
if (useCache)
cached.addAll(users);
last = users.get(users.size() - 1);
lastKey = last.getIdLong();
}

request.onSuccess(users);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.dv8tion.jda.test.entities.message;

import net.dv8tion.jda.api.requests.Method;
import net.dv8tion.jda.internal.requests.restaction.pagination.PollVotersPaginationActionImpl;
import net.dv8tion.jda.test.IntegrationTest;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;

public class PollVotersPaginationTest extends IntegrationTest
{
private PollVotersPaginationActionImpl newAction()
{
return new PollVotersPaginationActionImpl(jda, "381886978205155338", "1228092239079804968", 5);
}

@Test
void testDefaults()
{
assertThatRequestFrom(newAction())
.hasMethod(Method.GET)
.hasCompiledRoute("channels/381886978205155338/polls/1228092239079804968/answers/5?limit=1000&after=0")
.whenQueueCalled();
}

@Test
void testSkipTo()
{
long randomId = random.nextLong();
assertThatRequestFrom(newAction().skipTo(randomId))
.hasMethod(Method.GET)
.hasQueryParams("limit", "1000", "after", Long.toUnsignedString(randomId))
.whenQueueCalled();
}

@Test
void testOrder()
{
assertThatIllegalArgumentException()
.isThrownBy(() -> newAction().reverse())
.withMessage("Cannot use PaginationOrder.BACKWARD for this pagination endpoint.");
}
}

0 comments on commit b336ec2

Please sign in to comment.