Skip to content

Commit 3f5496e

Browse files
committed
Add stateful client interface for polls
1 parent e5d4321 commit 3f5496e

18 files changed

+500
-44
lines changed

Assets/Plugins/StreamChat/Core/IStreamChatClient.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ public interface IStreamChatClient : IDisposable, IStreamChatClientEventsListene
108108
/// </summary>
109109
IStreamChatLowLevelClient LowLevelClient { get; }
110110

111+
/// <summary>
112+
/// Polls API for creating, retrieving, and querying polls
113+
/// </summary>
114+
IStreamPollsApi Polls { get; }
115+
111116
/// <summary>
112117
/// Connect user to Stream Chat server.
113118
/// User authentication credentials:
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using StreamChat.Core.Requests;
4+
using StreamChat.Core.StatefulModels;
5+
6+
namespace StreamChat.Core
7+
{
8+
/// <summary>
9+
/// Polls API for creating, retrieving, and querying polls
10+
/// </summary>
11+
public interface IStreamPollsApi
12+
{
13+
/// <summary>
14+
/// Create a new poll
15+
/// </summary>
16+
/// <param name="createRequest">Request with poll creation data</param>
17+
/// <returns>The created poll</returns>
18+
Task<IStreamPoll> CreatePollAsync(StreamCreatePollRequest createRequest);
19+
20+
/// <summary>
21+
/// Get a poll by ID
22+
/// </summary>
23+
/// <param name="pollId">The poll ID</param>
24+
/// <returns>The poll with the specified ID</returns>
25+
Task<IStreamPoll> GetPollAsync(string pollId);
26+
27+
/// <summary>
28+
/// Query polls based on filters
29+
/// </summary>
30+
/// <param name="queryRequest">Request with query filters and parameters</param>
31+
/// <returns>List of polls matching the query</returns>
32+
Task<IEnumerable<IStreamPoll>> QueryPollsAsync(StreamQueryPollsRequest queryRequest);
33+
}
34+
}
35+

Assets/Plugins/StreamChat/Core/IStreamPollsApi.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
3+
namespace StreamChat.Core.QueryBuilders.Sort
4+
{
5+
/// <summary>
6+
/// Sort object for Poll query
7+
/// </summary>
8+
public sealed class PollsSortObject : QuerySort<PollsSortObject, PollSortFieldName>
9+
{
10+
protected override PollsSortObject Instance => this;
11+
12+
protected override string ToUnderlyingFieldName(PollSortFieldName fieldName)
13+
{
14+
switch (fieldName)
15+
{
16+
case PollSortFieldName.CreatedAt: return "created_at";
17+
case PollSortFieldName.UpdatedAt: return "updated_at";
18+
case PollSortFieldName.Id: return "id";
19+
case PollSortFieldName.Name: return "name";
20+
case PollSortFieldName.VoteCount: return "vote_count";
21+
default:
22+
throw new ArgumentOutOfRangeException(nameof(fieldName), fieldName, null);
23+
}
24+
}
25+
}
26+
27+
/// <summary>
28+
/// Sort field names for polls
29+
/// </summary>
30+
public enum PollSortFieldName
31+
{
32+
CreatedAt,
33+
UpdatedAt,
34+
Id,
35+
Name,
36+
VoteCount
37+
}
38+
}
39+

Assets/Plugins/StreamChat/Core/QueryBuilders/Sort/PollsSortObject.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
using System.Collections.Generic;
2+
using StreamChat.Core.Helpers;
3+
using StreamChat.Core.InternalDTO.Models;
4+
using StreamChat.Core.InternalDTO.Requests;
5+
using StreamChat.Core.LowLevelClient;
6+
using StreamChat.Core.LowLevelClient.Models;
7+
8+
namespace StreamChat.Core.Requests
9+
{
10+
/// <summary>
11+
/// Request to create a poll
12+
/// </summary>
13+
public class StreamCreatePollRequest : ISavableTo<CreatePollRequestInternalDTO>
14+
{
15+
/// <summary>
16+
/// Custom data for the poll
17+
/// </summary>
18+
public Dictionary<string, object> Custom { get; set; }
19+
20+
/// <summary>
21+
/// Whether to allow answers
22+
/// </summary>
23+
public bool? AllowAnswers { get; set; }
24+
25+
/// <summary>
26+
/// Whether to allow user suggested options
27+
/// </summary>
28+
public bool? AllowUserSuggestedOptions { get; set; }
29+
30+
/// <summary>
31+
/// Poll description
32+
/// </summary>
33+
public string Description { get; set; }
34+
35+
/// <summary>
36+
/// Whether to enforce unique votes
37+
/// </summary>
38+
public bool? EnforceUniqueVote { get; set; }
39+
40+
/// <summary>
41+
/// Poll unique ID
42+
/// </summary>
43+
public string Id { get; set; }
44+
45+
/// <summary>
46+
/// Whether the poll is closed
47+
/// </summary>
48+
public bool? IsClosed { get; set; }
49+
50+
/// <summary>
51+
/// Maximum number of votes allowed per user
52+
/// </summary>
53+
public int? MaxVotesAllowed { get; set; }
54+
55+
/// <summary>
56+
/// Poll name
57+
/// </summary>
58+
public string Name { get; set; }
59+
60+
/// <summary>
61+
/// Poll options
62+
/// </summary>
63+
public List<StreamPollOptionRequest> Options { get; set; }
64+
65+
/// <summary>
66+
/// Voting visibility setting
67+
/// </summary>
68+
public VotingVisibility? VotingVisibility { get; set; }
69+
70+
CreatePollRequestInternalDTO ISavableTo<CreatePollRequestInternalDTO>.SaveToDto()
71+
{
72+
var dto = new CreatePollRequestInternalDTO
73+
{
74+
Custom = Custom,
75+
AllowAnswers = AllowAnswers,
76+
AllowUserSuggestedOptions = AllowUserSuggestedOptions,
77+
Description = Description,
78+
EnforceUniqueVote = EnforceUniqueVote,
79+
Id = Id,
80+
IsClosed = IsClosed,
81+
MaxVotesAllowed = MaxVotesAllowed,
82+
Name = Name,
83+
VotingVisibility = VotingVisibility.HasValue ? (string)VotingVisibility.Value : null
84+
};
85+
86+
if (Options != null && Options.Count > 0)
87+
{
88+
dto.Options = new List<PollOptionInputInternalDTO>();
89+
foreach (var option in Options)
90+
{
91+
dto.Options.Add(option.TrySaveToDto<PollOptionInputInternalDTO>());
92+
}
93+
}
94+
95+
return dto;
96+
}
97+
}
98+
}
99+

Assets/Plugins/StreamChat/Core/Requests/StreamCreatePollRequest.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Collections.Generic;
2+
using StreamChat.Core.InternalDTO.Models;
3+
using StreamChat.Core.InternalDTO.Requests;
4+
using StreamChat.Core.LowLevelClient;
5+
6+
namespace StreamChat.Core.Requests
7+
{
8+
/// <summary>
9+
/// Request to create a poll option
10+
/// </summary>
11+
public class StreamPollOptionRequest : ISavableTo<PollOptionRequestInternalDTO>, ISavableTo<PollOptionInputInternalDTO>
12+
{
13+
/// <summary>
14+
/// Custom data for the option
15+
/// </summary>
16+
public Dictionary<string, object> Custom { get; set; }
17+
18+
/// <summary>
19+
/// Option text
20+
/// </summary>
21+
public string Text { get; set; }
22+
23+
PollOptionRequestInternalDTO ISavableTo<PollOptionRequestInternalDTO>.SaveToDto()
24+
{
25+
return new PollOptionRequestInternalDTO
26+
{
27+
Custom = Custom,
28+
Text = Text
29+
};
30+
}
31+
32+
PollOptionInputInternalDTO ISavableTo<PollOptionInputInternalDTO>.SaveToDto()
33+
{
34+
return new PollOptionInputInternalDTO
35+
{
36+
Custom = Custom,
37+
Text = Text
38+
};
39+
}
40+
}
41+
}

Assets/Plugins/StreamChat/Core/Requests/StreamPollOptionRequest.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using StreamChat.Core.InternalDTO.Requests;
4+
using StreamChat.Core.LowLevelClient;
5+
using StreamChat.Core.QueryBuilders.Filters;
6+
using StreamChat.Core.QueryBuilders.Sort;
7+
8+
namespace StreamChat.Core.Requests
9+
{
10+
/// <summary>
11+
/// Request to query polls
12+
/// </summary>
13+
public class StreamQueryPollsRequest : ISavableTo<QueryPollsRequestInternalDTO>
14+
{
15+
/// <summary>
16+
/// Filter conditions
17+
/// </summary>
18+
public IEnumerable<IFieldFilterRule> Filter { get; set; }
19+
20+
/// <summary>
21+
/// Number of results to return
22+
/// </summary>
23+
public int? Limit { get; set; }
24+
25+
public string Next { get; set; }
26+
27+
public string Prev { get; set; }
28+
29+
/// <summary>
30+
/// Sort parameters
31+
/// </summary>
32+
public PollsSortObject Sort { get; set; }
33+
34+
QueryPollsRequestInternalDTO ISavableTo<QueryPollsRequestInternalDTO>.SaveToDto()
35+
{
36+
return new QueryPollsRequestInternalDTO
37+
{
38+
Filter = Filter?.Select(_ => _.GenerateFilterEntry()).ToDictionary(x => x.Key, x => x.Value),
39+
Limit = Limit,
40+
Next = Next,
41+
Prev = Prev,
42+
Sort = Sort?.ToSortParamRequestList()
43+
};
44+
}
45+
}
46+
}
47+

0 commit comments

Comments
 (0)