Skip to content

Conversation

@rafaelmf3
Copy link
Contributor

@rafaelmf3 rafaelmf3 commented Jan 16, 2026

Add Poll Support to Java SDK

Overview

This PR adds comprehensive support for the Polls feature in the Stream Chat Java SDK, allowing users to create, manage, and interact with polls in their chat applications.

Features Added

Poll Management

  • Create Polls: Create polls with multiple options, voting visibility settings, and custom configurations
  • Get Polls: Retrieve poll details by ID
  • Update Polls: Full and partial updates to poll properties
  • Delete Polls: Remove polls from the system
  • Query Polls: Advanced querying with filtering and sorting capabilities

Poll Options Management

  • Create Options: Add new options to existing polls
  • Get Options: Retrieve specific poll option details
  • Update Options: Modify option text and properties
  • Delete Options: Remove options from polls

Poll Voting

  • Query Votes: Retrieve votes for a poll with filtering and pagination
  • Cast Votes: Allow users to vote on poll options via messages
  • Delete Votes: Remove votes from polls

Channel Type Support

  • Added polls field to ChannelType model to enable polls at the channel type level
  • Support for enabling polls via channel type configuration and channel-level config overrides

API Endpoints Covered

Poll CRUD

  • POST /polls - Create poll
  • GET /polls/{poll_id} - Get poll
  • PUT /polls - Update poll
  • PATCH /polls/{poll_id} - Partial update poll
  • DELETE /polls/{poll_id} - Delete poll
  • POST /polls/query - Query polls

Poll Options

  • POST /polls/{poll_id}/options - Create option
  • GET /polls/{poll_id}/options/{option_id} - Get option
  • PUT /polls/{poll_id}/options - Update option
  • DELETE /polls/{poll_id}/options/{option_id} - Delete option

Poll Votes

  • POST /polls/{poll_id}/votes - Query votes
  • POST /messages/{message_id}/polls/{poll_id}/vote - Cast vote
  • DELETE /messages/{message_id}/polls/{poll_id}/vote/{vote_id} - Delete vote

Implementation Details

New Files

  • src/main/java/io/getstream/chat/java/models/Poll.java - Complete poll model with all request/response types
  • src/main/java/io/getstream/chat/java/services/PollService.java - Retrofit service interface for poll operations
  • src/test/java/io/getstream/chat/java/PollTest.java - Comprehensive unit tests

Modified Files

  • src/main/java/io/getstream/chat/java/models/ChannelType.java - Added polls field support for channel type configuration

Key Features

Poll Configuration

  • Voting Visibility: Public or anonymous voting
  • Unique Votes: Enforce one vote per user
  • Max Votes: Configure maximum votes allowed per user
  • User-Suggested Options: Allow users to suggest new options
  • Answers: Enable text answers alongside votes
  • Poll Status: Open/closed state management

Type Safety

  • Full type safety with @NotNull and @Nullable annotations
  • Jackson annotations for JSON serialization/deserialization
  • Lombok builders for fluent API usage

Authentication

  • Proper handling of user_id parameter for server-side authentication
  • Support for both user object and user_id string in requests

Testing

Comprehensive test coverage including:

  • ✅ Poll CRUD operations
  • ✅ Poll option CRUD operations
  • ✅ Poll querying with filters and sorting
  • ✅ Vote querying
  • ✅ Auto-ID generation
  • ✅ Channel type polls configuration
  • ✅ Channel config overrides for polls

All tests follow existing patterns and use JUnit 5.

Usage Example

// Create a poll
PollOptionInput option1 = new PollOptionInput();
option1.setText("Option 1");
PollOptionInput option2 = new PollOptionInput();
option2.setText("Option 2");

Poll poll = Poll.create()
    .userId("user123")
    .name("What's your favorite color?")
    .description("Please vote for your favorite")
    .options(List.of(option1, option2))
    .votingVisibility(Poll.VotingVisibility.PUBLIC)
    .enforceUniqueVote(true)
    .request()
    .getPoll();

// Enable polls on channel type
ChannelType.update("messaging")
    .polls(true)
    .request();

// Attach poll to a message
MessageRequestObject messageRequest = MessageRequestObject.builder()
    .text("Check out this poll!")
    .userId("user123")
    .additionalField("poll_id", poll.getId())
    .build();

Message.send("messaging", "channel123")
    .message(messageRequest)
    .request();

// Query votes
List<Poll.PollVote> votes = Poll.queryVotes(poll.getId())
    .userId("user123")
    .limit(10)
    .request()
    .getVotes();

CLA

  • I have signed the Stream CLA (required).
  • The code changes follow best practices
  • Code changes are tested (add some information if not applicable)

Description of the pull request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants