Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(llc): add support for polls api #2040

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions packages/stream_chat/lib/src/client/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import 'package:stream_chat/src/core/models/filter.dart';
import 'package:stream_chat/src/core/models/member.dart';
import 'package:stream_chat/src/core/models/message.dart';
import 'package:stream_chat/src/core/models/own_user.dart';
import 'package:stream_chat/src/core/models/poll.dart';
import 'package:stream_chat/src/core/models/poll_option.dart';
import 'package:stream_chat/src/core/models/poll_vote.dart';
import 'package:stream_chat/src/core/models/user.dart';
import 'package:stream_chat/src/core/platform_detector/platform_detector.dart';
import 'package:stream_chat/src/core/util/utils.dart';
Expand Down Expand Up @@ -1200,6 +1203,124 @@ class StreamChatClient {
messageId,
);

/// Creates a new Poll
Future<CreatePollResponse> createPoll(Poll poll) =>
_chatApi.polls.createPoll(poll);

/// Retrieves a Poll by [pollId]
Future<GetPollResponse> getPoll(String pollId) =>
_chatApi.polls.getPoll(pollId);

/// Updates a Poll
Future<UpdatePollResponse> updatePoll(Poll poll) =>
_chatApi.polls.updatePoll(poll);

/// Partially updates a Poll by [pollId].
///
/// Use [set] to define values to be set.
/// Use [unset] to define values to be unset.
Future<UpdatePollResponse> partialUpdatePoll(
String pollId, {
Map<String, Object?>? set,
List<String>? unset,
}) =>
_chatApi.polls.partialUpdatePoll(
pollId,
set: set,
unset: unset,
);

/// Deletes the Poll by [pollId].
Future<EmptyResponse> deletePoll(String pollId) =>
_chatApi.polls.deletePoll(pollId);

/// Marks the Poll [pollId] as closed.
Future<UpdatePollResponse> closePoll(String pollId) =>
partialUpdatePoll(pollId, set: {
'is_closed': true,
});

/// Creates a new Poll Option for the Poll [pollId].
Future<CreatePollOptionResponse> createPollOption(
String pollId,
PollOption option,
) =>
_chatApi.polls.createPollOption(pollId, option);

/// Retrieves a Poll Option by [optionId] for the Poll [pollId].
Future<GetPollOptionResponse> getPollOption(
String pollId,
String optionId,
) =>
_chatApi.polls.getPollOption(pollId, optionId);

/// Updates a Poll Option for the Poll [pollId].
Future<UpdatePollOptionResponse> updatePollOption(
String pollId,
PollOption option,
) =>
_chatApi.polls.updatePollOption(pollId, option);

/// Deletes a Poll Option by [optionId] for the Poll [pollId].
Future<EmptyResponse> deletePollOption(
String pollId,
String optionId,
) =>
_chatApi.polls.deletePollOption(pollId, optionId);

/// Cast a [vote] for the Poll [pollId].
Future<CastPollVoteResponse> castPollVote(
String messageId,
String pollId,
PollVote vote,
) =>
_chatApi.polls.castPollVote(messageId, pollId, vote);

/// Adds a answer with [answerText] for the Poll [pollId].
Future<CastPollVoteResponse> addPollAnswer(
String messageId,
String pollId, {
required String answerText,
}) {
final vote = PollVote(answerText: answerText);
return castPollVote(messageId, pollId, vote);
}

/// Removes a vote by [voteId] for the Poll [pollId].
Future<RemovePollVoteResponse> removePollVote(
String messageId,
String pollId,
String voteId,
) =>
_chatApi.polls.removePollVote(messageId, pollId, voteId);

/// Queries Polls with the given [filter] and [sort] options.
Future<QueryPollsResponse> queryPolls({
Filter? filter,
List<SortOption>? sort,
PaginationParams pagination = const PaginationParams(),
}) =>
_chatApi.polls.queryPolls(
filter: filter,
sort: sort,
pagination: pagination,
);

/// Queries Poll Votes for the Poll [pollId] with the given [filter]
/// and [sort] options.
Future<QueryPollVotesResponse> queryPollVotes(
String pollId, {
Filter? filter,
List<SortOption>? sort,
PaginationParams pagination = const PaginationParams(),
}) =>
_chatApi.polls.queryPollVotes(
pollId,
filter: filter,
sort: sort,
pagination: pagination,
);

/// Update or Create the given user object.
Future<UpdateUsersResponse> updateUser(User user) => updateUsers([user]);

Expand Down
198 changes: 198 additions & 0 deletions packages/stream_chat/lib/src/core/api/polls_api.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
import 'dart:convert';

import 'package:stream_chat/src/core/api/requests.dart';
import 'package:stream_chat/src/core/api/responses.dart';
import 'package:stream_chat/src/core/http/stream_http_client.dart';
import 'package:stream_chat/src/core/models/filter.dart';
import 'package:stream_chat/src/core/models/poll.dart';
import 'package:stream_chat/src/core/models/poll_option.dart';
import 'package:stream_chat/src/core/models/poll_vote.dart';

/// Defines the api dedicated to polls operations
class PollsApi {
/// Initialize a new polls api
const PollsApi(this._client);

final StreamHttpClient _client;

/// Creates a new poll with the given [poll] data.
Future<CreatePollResponse> createPoll(
Poll poll,
) async {
final response = await _client.post(
'/polls',
data: jsonEncode(poll),
);

return CreatePollResponse.fromJson(response.data);
}

/// Retrieves a poll by its [id].
Future<GetPollResponse> getPoll(
String id,
) async {
final response = await _client.get(
'/polls/$id',
);

return GetPollResponse.fromJson(response.data);
}

/// Updates the poll with the given [poll] data.
Future<UpdatePollResponse> updatePoll(
Poll poll,
) async {
final response = await _client.put(
'/polls',
data: jsonEncode(poll),
);

return UpdatePollResponse.fromJson(response.data);
}

/// Partially updates the poll with the given [id].
///
/// The [set] and [unset] parameters are used to update the poll.
Future<UpdatePollResponse> partialUpdatePoll(
String id, {
Map<String, Object?>? set,
List<String>? unset,
}) async {
final response = await _client.patch(
'/polls/$id',
data: jsonEncode({
if (set != null) 'set': set,
if (unset != null) 'unset': unset,
}),
);

return UpdatePollResponse.fromJson(response.data);
}

/// Deletes the poll with the given [id].
Future<EmptyResponse> deletePoll(String id) async {
final response = await _client.delete(
'/polls/$id',
);

return EmptyResponse.fromJson(response.data);
}

/// Creates a new poll option with the given [option] data.
Future<CreatePollOptionResponse> createPollOption(
String pollId,
PollOption option,
) async {
final response = await _client.post(
'/polls/$pollId/options',
data: jsonEncode(option),
);

return CreatePollOptionResponse.fromJson(response.data);
}

/// Retrieves a poll option by its [optionId].
Future<GetPollOptionResponse> getPollOption(
String pollId,
String optionId,
) async {
final response = await _client.get(
'/polls/$pollId/options/$optionId',
);

return GetPollOptionResponse.fromJson(response.data);
}

/// Updates the poll option with the given [option] data.
Future<UpdatePollOptionResponse> updatePollOption(
String pollId,
PollOption option,
) async {
final response = await _client.put(
'/polls/$pollId/options',
data: jsonEncode(option),
);

return UpdatePollOptionResponse.fromJson(response.data);
}

/// Removes the poll option with the given [optionId].
Future<EmptyResponse> deletePollOption(
String pollId,
String optionId,
) async {
final response = await _client.delete(
'/polls/$pollId/options/$optionId',
);

return EmptyResponse.fromJson(response.data);
}

/// Casts a vote on the poll with the given [pollId] and [vote] data.
Future<CastPollVoteResponse> castPollVote(
String messageId,
String pollId,
PollVote vote,
) async {
final response = await _client.post(
'/messages/$messageId/polls/$pollId/vote',
data: jsonEncode({
'vote': vote,
}),
);

return CastPollVoteResponse.fromJson(response.data);
}

/// Removes the vote with the given [voteId] from the poll with the
/// given [pollId].
Future<RemovePollVoteResponse> removePollVote(
String messageId,
String pollId,
String voteId,
) async {
final response = await _client.delete(
'/messages/$messageId/polls/$pollId/vote/$voteId',
);

return RemovePollVoteResponse.fromJson(response.data);
}

/// Queries polls with the given [filter], [sort], and [pagination]
/// parameters.
Future<QueryPollsResponse> queryPolls({
Filter? filter,
List<SortOption>? sort,
PaginationParams pagination = const PaginationParams(),
}) async {
final response = await _client.post(
'/polls/query',
data: jsonEncode({
if (filter != null) 'filter': filter.toJson(),
if (sort != null) 'sort': sort.map((e) => e.toJson()).toList(),
...pagination.toJson(),
}),
);

return QueryPollsResponse.fromJson(response.data);
}

/// Queries poll votes with the given [pollId], [filter], [sort], and
Future<QueryPollVotesResponse> queryPollVotes(
String pollId, {
Filter? filter,
List<SortOption>? sort,
PaginationParams pagination = const PaginationParams(),
}) async {
final response = await _client.post(
'/polls/$pollId/votes',
data: jsonEncode({
if (filter != null) 'filter': filter.toJson(),
if (sort != null) 'sort': sort.map((e) => e.toJson()).toList(),
...pagination.toJson(),
}),
);

return QueryPollVotesResponse.fromJson(response.data);
}
}
Loading
Loading