Skip to content

Commit 5d0db0f

Browse files
GwFreak01GwFreak01
authored andcommitted
[NT] Implement Create polls basic functionality
1 parent 096af4c commit 5d0db0f

File tree

3 files changed

+444
-0
lines changed

3 files changed

+444
-0
lines changed
Lines changed: 335 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,335 @@
1+
package io.getstream.chat.java.models;
2+
3+
import com.fasterxml.jackson.annotation.*;
4+
import io.getstream.chat.java.exceptions.StreamException;
5+
import io.getstream.chat.java.models.Poll.CreatePollRequestData.CreatePollRequest;
6+
import io.getstream.chat.java.models.User.UserRequestObject;
7+
import io.getstream.chat.java.models.framework.RequestObjectBuilder;
8+
import io.getstream.chat.java.models.framework.StreamRequest;
9+
import io.getstream.chat.java.models.framework.StreamResponseObject;
10+
import io.getstream.chat.java.services.PollService;
11+
import io.getstream.chat.java.services.framework.Client;
12+
import java.util.Date;
13+
import java.util.HashMap;
14+
import java.util.List;
15+
import java.util.Map;
16+
import lombok.*;
17+
import org.jetbrains.annotations.NotNull;
18+
import org.jetbrains.annotations.Nullable;
19+
import retrofit2.Call;
20+
21+
/** Represents poll functionality in Stream Chat. */
22+
@Data
23+
@NoArgsConstructor
24+
public class Poll {
25+
@Nullable
26+
@JsonProperty("id")
27+
private String id;
28+
29+
@Nullable
30+
@JsonProperty("name")
31+
private String name;
32+
33+
@Nullable
34+
@JsonProperty("description")
35+
private String description;
36+
37+
@Nullable
38+
@JsonProperty("voting_visibility")
39+
private VotingVisibility votingVisibility;
40+
41+
@Nullable
42+
@JsonProperty("enforce_unique_vote")
43+
private Boolean enforceUniqueVote;
44+
45+
@Nullable
46+
@JsonProperty("max_votes_allowed")
47+
private Integer maxVotesAllowed;
48+
49+
@Nullable
50+
@JsonProperty("allow_user_suggested_options")
51+
private Boolean allowUserSuggestedOptions;
52+
53+
@Nullable
54+
@JsonProperty("allow_answers")
55+
private Boolean allowAnswers;
56+
57+
@Nullable
58+
@JsonProperty("is_closed")
59+
private Boolean isClosed;
60+
61+
@Nullable
62+
@JsonProperty("options")
63+
private List<PollOption> options;
64+
65+
@Nullable
66+
@JsonProperty("vote_count")
67+
private Integer voteCount;
68+
69+
@Nullable
70+
@JsonProperty("vote_counts_by_option")
71+
private Map<String, Integer> voteCountsByOption;
72+
73+
@Nullable
74+
@JsonProperty("answers_count")
75+
private Integer answersCount;
76+
77+
@Nullable
78+
@JsonProperty("created_by_id")
79+
private String createdById;
80+
81+
@Nullable
82+
@JsonProperty("created_by")
83+
private User createdBy;
84+
85+
@Nullable
86+
@JsonProperty("created_at")
87+
private Date createdAt;
88+
89+
@Nullable
90+
@JsonProperty("updated_at")
91+
private Date updatedAt;
92+
93+
@NotNull @JsonIgnore private Map<String, Object> additionalFields = new HashMap<>();
94+
95+
@JsonAnyGetter
96+
public Map<String, Object> getAdditionalFields() {
97+
return this.additionalFields;
98+
}
99+
100+
@JsonAnySetter
101+
public void setAdditionalField(String name, Object value) {
102+
this.additionalFields.put(name, value);
103+
}
104+
105+
/** Voting visibility options for a poll. */
106+
public enum VotingVisibility {
107+
@JsonProperty("public")
108+
PUBLIC,
109+
@JsonProperty("anonymous")
110+
ANONYMOUS,
111+
@JsonEnumDefaultValue
112+
UNKNOWN
113+
}
114+
115+
/** A poll option. */
116+
@Data
117+
@NoArgsConstructor
118+
public static class PollOption {
119+
@Nullable
120+
@JsonProperty("id")
121+
private String id;
122+
123+
@Nullable
124+
@JsonProperty("text")
125+
private String text;
126+
127+
@Nullable
128+
@JsonProperty("position")
129+
private Integer position;
130+
131+
@Nullable
132+
@JsonProperty("vote_count")
133+
private Integer voteCount;
134+
135+
@NotNull @JsonIgnore private Map<String, Object> additionalFields = new HashMap<>();
136+
137+
@JsonAnyGetter
138+
public Map<String, Object> getAdditionalFields() {
139+
return this.additionalFields;
140+
}
141+
142+
@JsonAnySetter
143+
public void setAdditionalField(String name, Object value) {
144+
this.additionalFields.put(name, value);
145+
}
146+
}
147+
148+
/** Request object for poll options. */
149+
@Builder
150+
@Setter
151+
@Getter
152+
@EqualsAndHashCode
153+
public static class PollOptionRequestObject {
154+
@Nullable
155+
@JsonProperty("id")
156+
private String id;
157+
158+
@Nullable
159+
@JsonProperty("text")
160+
private String text;
161+
162+
@Nullable
163+
@JsonProperty("position")
164+
private Integer position;
165+
166+
@Singular @Nullable @JsonIgnore private Map<String, Object> additionalFields;
167+
168+
@JsonAnyGetter
169+
public Map<String, Object> getAdditionalFields() {
170+
return this.additionalFields;
171+
}
172+
173+
@JsonAnySetter
174+
public void setAdditionalField(String name, Object value) {
175+
this.additionalFields.put(name, value);
176+
}
177+
178+
@Nullable
179+
public static PollOptionRequestObject buildFrom(@Nullable PollOption pollOption) {
180+
return RequestObjectBuilder.build(PollOptionRequestObject.class, pollOption);
181+
}
182+
}
183+
184+
/** Request object for polls. */
185+
@Builder
186+
@Setter
187+
@Getter
188+
@EqualsAndHashCode
189+
public static class PollRequestObject {
190+
@Nullable
191+
@JsonProperty("id")
192+
private String id;
193+
194+
@Nullable
195+
@JsonProperty("name")
196+
private String name;
197+
198+
@Nullable
199+
@JsonProperty("description")
200+
private String description;
201+
202+
@Nullable
203+
@JsonProperty("voting_visibility")
204+
private VotingVisibility votingVisibility;
205+
206+
@Nullable
207+
@JsonProperty("enforce_unique_vote")
208+
private Boolean enforceUniqueVote;
209+
210+
@Nullable
211+
@JsonProperty("max_votes_allowed")
212+
private Integer maxVotesAllowed;
213+
214+
@Nullable
215+
@JsonProperty("allow_user_suggested_options")
216+
private Boolean allowUserSuggestedOptions;
217+
218+
@Nullable
219+
@JsonProperty("allow_answers")
220+
private Boolean allowAnswers;
221+
222+
@Singular
223+
@Nullable
224+
@JsonProperty("options")
225+
private List<PollOptionRequestObject> options;
226+
227+
@Nullable
228+
@JsonProperty("user_id")
229+
private String userId;
230+
231+
@Nullable
232+
@JsonProperty("user")
233+
private UserRequestObject user;
234+
235+
@Singular @Nullable @JsonIgnore private Map<String, Object> additionalFields;
236+
237+
@JsonAnyGetter
238+
public Map<String, Object> getAdditionalFields() {
239+
return this.additionalFields;
240+
}
241+
242+
@JsonAnySetter
243+
public void setAdditionalField(String name, Object value) {
244+
this.additionalFields.put(name, value);
245+
}
246+
247+
@Nullable
248+
public static PollRequestObject buildFrom(@Nullable Poll poll) {
249+
return RequestObjectBuilder.build(PollRequestObject.class, poll);
250+
}
251+
}
252+
253+
/** Response for poll creation. */
254+
@Data
255+
@NoArgsConstructor
256+
@EqualsAndHashCode(callSuper = true)
257+
public static class CreatePollResponse extends StreamResponseObject {
258+
@NotNull
259+
@JsonProperty("poll")
260+
private Poll poll;
261+
}
262+
263+
/** Request data for creating a poll. */
264+
@Builder(
265+
builderClassName = "CreatePollRequest",
266+
builderMethodName = "",
267+
buildMethodName = "internalBuild")
268+
@Getter
269+
@EqualsAndHashCode
270+
public static class CreatePollRequestData {
271+
@Nullable
272+
@JsonProperty("id")
273+
private String id;
274+
275+
@Nullable
276+
@JsonProperty("name")
277+
private String name;
278+
279+
@Nullable
280+
@JsonProperty("description")
281+
private String description;
282+
283+
@Nullable
284+
@JsonProperty("voting_visibility")
285+
private VotingVisibility votingVisibility;
286+
287+
@Nullable
288+
@JsonProperty("enforce_unique_vote")
289+
private Boolean enforceUniqueVote;
290+
291+
@Nullable
292+
@JsonProperty("max_votes_allowed")
293+
private Integer maxVotesAllowed;
294+
295+
@Nullable
296+
@JsonProperty("allow_user_suggested_options")
297+
private Boolean allowUserSuggestedOptions;
298+
299+
@Nullable
300+
@JsonProperty("allow_answers")
301+
private Boolean allowAnswers;
302+
303+
@Singular
304+
@Nullable
305+
@JsonProperty("options")
306+
private List<PollOptionRequestObject> options;
307+
308+
@Nullable
309+
@JsonProperty("user_id")
310+
private String userId;
311+
312+
@Nullable
313+
@JsonProperty("user")
314+
private UserRequestObject user;
315+
316+
public static class CreatePollRequest extends StreamRequest<CreatePollResponse> {
317+
public CreatePollRequest() {}
318+
319+
@Override
320+
protected Call<CreatePollResponse> generateCall(Client client) throws StreamException {
321+
return client.create(PollService.class).create(this.internalBuild());
322+
}
323+
}
324+
}
325+
326+
/**
327+
* Creates a poll creation request.
328+
*
329+
* @return the created request
330+
*/
331+
@NotNull
332+
public static CreatePollRequest create() {
333+
return new CreatePollRequest();
334+
}
335+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.getstream.chat.java.services;
2+
3+
import io.getstream.chat.java.models.Poll.CreatePollRequestData;
4+
import io.getstream.chat.java.models.Poll.CreatePollResponse;
5+
import org.jetbrains.annotations.NotNull;
6+
import retrofit2.Call;
7+
import retrofit2.http.Body;
8+
import retrofit2.http.POST;
9+
10+
/** Service interface for Poll API endpoints. */
11+
public interface PollService {
12+
13+
/**
14+
* Creates a new poll.
15+
*
16+
* @param request The poll creation request data
17+
* @return A response with the created poll
18+
*/
19+
@POST("polls")
20+
Call<CreatePollResponse> create(@NotNull @Body CreatePollRequestData request);
21+
}

0 commit comments

Comments
 (0)