From b336ec2bfb978c9c0e00525cd66d5e82748daa97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Spie=C3=9F?= Date: Sat, 13 Apr 2024 22:26:21 +0200 Subject: [PATCH] Add poll voters pagination --- .../net/dv8tion/jda/api/entities/Message.java | 17 ++++ .../channel/middleman/MessageChannel.java | 39 +++++++++ .../PollVotersPaginationAction.java | 31 +++++++ .../PollVotersPaginationActionImpl.java | 85 +++++++++++++++++++ .../message/PollVotersPaginationTest.java | 59 +++++++++++++ 5 files changed, 231 insertions(+) create mode 100644 src/main/java/net/dv8tion/jda/api/requests/restaction/pagination/PollVotersPaginationAction.java create mode 100644 src/main/java/net/dv8tion/jda/internal/requests/restaction/pagination/PollVotersPaginationActionImpl.java create mode 100644 src/test/java/net/dv8tion/jda/test/entities/message/PollVotersPaginationTest.java diff --git a/src/main/java/net/dv8tion/jda/api/entities/Message.java b/src/main/java/net/dv8tion/jda/api/entities/Message.java index 4679a4624b..5bd0b770d0 100644 --- a/src/main/java/net/dv8tion/jda/api/entities/Message.java +++ b/src/main/java/net/dv8tion/jda/api/entities/Message.java @@ -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; @@ -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; @@ -705,6 +707,21 @@ default String getGuildId() @CheckReturnValue AuditableRestAction 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}. *
You can use {@link MessageRequest#setComponents(LayoutComponent...)} to update these. diff --git a/src/main/java/net/dv8tion/jda/api/entities/channel/middleman/MessageChannel.java b/src/main/java/net/dv8tion/jda/api/entities/channel/middleman/MessageChannel.java index 84b1aedf07..074a671ad8 100644 --- a/src/main/java/net/dv8tion/jda/api/entities/channel/middleman/MessageChannel.java +++ b/src/main/java/net/dv8tion/jda/api/entities/channel/middleman/MessageChannel.java @@ -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; @@ -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; @@ -1090,6 +1092,43 @@ default AuditableRestAction 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. *
MessageHistory is NOT an internal message cache, but rather it queries the Discord servers for previously sent messages. diff --git a/src/main/java/net/dv8tion/jda/api/requests/restaction/pagination/PollVotersPaginationAction.java b/src/main/java/net/dv8tion/jda/api/requests/restaction/pagination/PollVotersPaginationAction.java new file mode 100644 index 0000000000..79d0aa28ab --- /dev/null +++ b/src/main/java/net/dv8tion/jda/api/requests/restaction/pagination/PollVotersPaginationAction.java @@ -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. + * + *

Limits
+ * Minimum - 1
+ * Maximum - 1000 + *
Default - 1000 + */ +public interface PollVotersPaginationAction extends PaginationAction +{ +} diff --git a/src/main/java/net/dv8tion/jda/internal/requests/restaction/pagination/PollVotersPaginationActionImpl.java b/src/main/java/net/dv8tion/jda/internal/requests/restaction/pagination/PollVotersPaginationActionImpl.java new file mode 100644 index 0000000000..5ac181ee7b --- /dev/null +++ b/src/main/java/net/dv8tion/jda/internal/requests/restaction/pagination/PollVotersPaginationActionImpl.java @@ -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 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 getSupportedOrders() + { + return EnumSet.of(PaginationOrder.FORWARD); + } + + @Override + protected long getKey(User it) + { + return it.getIdLong(); + } + + @Override + protected void handleSuccess(Response response, Request> request) + { + DataArray array = response.getObject().getArray("users"); + List 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); + } +} diff --git a/src/test/java/net/dv8tion/jda/test/entities/message/PollVotersPaginationTest.java b/src/test/java/net/dv8tion/jda/test/entities/message/PollVotersPaginationTest.java new file mode 100644 index 0000000000..57010d81c6 --- /dev/null +++ b/src/test/java/net/dv8tion/jda/test/entities/message/PollVotersPaginationTest.java @@ -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."); + } +}