Skip to content

Commit e5d4321

Browse files
committed
Add stateful poll object
1 parent f325487 commit e5d4321

13 files changed

Lines changed: 836 additions & 38 deletions
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System.Collections.Generic;
2+
using StreamChat.Core.InternalDTO.Models;
3+
using StreamChat.Core.InternalDTO.Responses;
4+
using StreamChat.Core.State;
5+
using StreamChat.Core.State.Caches;
6+
7+
namespace StreamChat.Core.Models
8+
{
9+
/// <summary>
10+
/// Represents an option in a poll
11+
/// </summary>
12+
public class StreamPollOption : IStateLoadableFrom<PollOptionInternalDTO, StreamPollOption>,
13+
IStateLoadableFrom<PollOptionResponseDataInternalDTO, StreamPollOption>
14+
{
15+
/// <summary>
16+
/// Custom data associated with this option
17+
/// </summary>
18+
public IReadOnlyDictionary<string, object> Custom { get; private set; }
19+
20+
/// <summary>
21+
/// Unique option ID
22+
/// </summary>
23+
public string Id { get; private set; }
24+
25+
/// <summary>
26+
/// Text displayed for this option
27+
/// </summary>
28+
public string Text { get; private set; }
29+
30+
/// <summary>
31+
/// Additional custom properties
32+
/// </summary>
33+
public IReadOnlyDictionary<string, object> AdditionalProperties { get; private set; }
34+
35+
StreamPollOption IStateLoadableFrom<PollOptionInternalDTO, StreamPollOption>.LoadFromDto(PollOptionInternalDTO dto, ICache cache)
36+
{
37+
Custom = dto.Custom;
38+
Id = dto.Id;
39+
Text = dto.Text;
40+
AdditionalProperties = dto.AdditionalProperties;
41+
42+
return this;
43+
}
44+
45+
StreamPollOption IStateLoadableFrom<PollOptionResponseDataInternalDTO, StreamPollOption>.LoadFromDto(PollOptionResponseDataInternalDTO dto, ICache cache)
46+
{
47+
Custom = dto.Custom;
48+
Id = dto.Id;
49+
Text = dto.Text;
50+
AdditionalProperties = dto.AdditionalProperties;
51+
52+
return this;
53+
}
54+
}
55+
}
56+

Assets/Plugins/StreamChat/Core/Models/StreamPollOption.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: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using StreamChat.Core.InternalDTO.Models;
4+
using StreamChat.Core.InternalDTO.Responses;
5+
using StreamChat.Core.State;
6+
using StreamChat.Core.State.Caches;
7+
using StreamChat.Core.StatefulModels;
8+
9+
namespace StreamChat.Core.Models
10+
{
11+
/// <summary>
12+
/// Represents a vote cast on a poll
13+
/// </summary>
14+
public class StreamPollVote : IStateLoadableFrom<PollVoteInternalDTO, StreamPollVote>,
15+
IStateLoadableFrom<PollVoteResponseDataInternalDTO, StreamPollVote>
16+
{
17+
/// <summary>
18+
/// Text answer for the vote (if poll allows answers)
19+
/// </summary>
20+
public string AnswerText { get; private set; }
21+
22+
/// <summary>
23+
/// Date/time of creation
24+
/// </summary>
25+
public DateTimeOffset? CreatedAt { get; private set; }
26+
27+
/// <summary>
28+
/// Unique vote ID
29+
/// </summary>
30+
public string Id { get; private set; }
31+
32+
/// <summary>
33+
/// Whether this is an answer (vs a vote for an option)
34+
/// </summary>
35+
public bool? IsAnswer { get; private set; }
36+
37+
/// <summary>
38+
/// ID of the option that was voted for
39+
/// </summary>
40+
public string OptionId { get; private set; }
41+
42+
/// <summary>
43+
/// ID of the poll this vote belongs to
44+
/// </summary>
45+
public string PollId { get; private set; }
46+
47+
/// <summary>
48+
/// Date/time of the last update
49+
/// </summary>
50+
public DateTimeOffset? UpdatedAt { get; private set; }
51+
52+
/// <summary>
53+
/// User who cast the vote
54+
/// </summary>
55+
public IStreamUser User { get; private set; }
56+
57+
/// <summary>
58+
/// ID of the user who cast the vote
59+
/// </summary>
60+
public string UserId { get; private set; }
61+
62+
/// <summary>
63+
/// Additional custom properties
64+
/// </summary>
65+
public IReadOnlyDictionary<string, object> AdditionalProperties { get; private set; }
66+
67+
StreamPollVote IStateLoadableFrom<PollVoteInternalDTO, StreamPollVote>.LoadFromDto(PollVoteInternalDTO dto, ICache cache)
68+
{
69+
AnswerText = dto.AnswerText;
70+
CreatedAt = dto.CreatedAt;
71+
Id = dto.Id;
72+
IsAnswer = dto.IsAnswer;
73+
OptionId = dto.OptionId;
74+
PollId = dto.PollId;
75+
UpdatedAt = dto.UpdatedAt;
76+
User = cache.TryCreateOrUpdate(dto.User);
77+
UserId = dto.UserId;
78+
AdditionalProperties = dto.AdditionalProperties;
79+
80+
return this;
81+
}
82+
83+
StreamPollVote IStateLoadableFrom<PollVoteResponseDataInternalDTO, StreamPollVote>.LoadFromDto(PollVoteResponseDataInternalDTO dto, ICache cache)
84+
{
85+
AnswerText = dto.AnswerText;
86+
CreatedAt = dto.CreatedAt;
87+
Id = dto.Id;
88+
IsAnswer = dto.IsAnswer;
89+
OptionId = dto.OptionId;
90+
PollId = dto.PollId;
91+
UpdatedAt = dto.UpdatedAt;
92+
User = cache.TryCreateOrUpdate(dto.User);
93+
UserId = dto.UserId;
94+
AdditionalProperties = dto.AdditionalProperties;
95+
96+
return this;
97+
}
98+
}
99+
}
100+

Assets/Plugins/StreamChat/Core/Models/StreamPollVote.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.

Assets/Plugins/StreamChat/Core/State/Caches/Cache.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public Cache(StreamChatClient stateClient, ISerializer serializer, ILogs logs)
1717
Users = new CacheRepository<StreamUser>(trackedObjectsFactory.CreateStreamUser, cache: this);
1818
LocalUser = new CacheRepository<StreamLocalUserData>(trackedObjectsFactory.CreateStreamLocalUser, cache: this);
1919
ChannelMembers = new CacheRepository<StreamChannelMember>(trackedObjectsFactory.CreateStreamChannelMember, cache: this);
20+
Polls = new CacheRepository<StreamPoll>(trackedObjectsFactory.CreateStreamPoll, cache: this);
2021

2122
Channels.RegisterDtoIdMapping<StreamChannel, ChannelStateResponseInternalDTO>(dto => dto.Channel.Cid);
2223
Channels.RegisterDtoIdMapping<StreamChannel, ChannelResponseInternalDTO>(dto => dto.Cid);
@@ -44,6 +45,8 @@ public Cache(StreamChatClient stateClient, ISerializer serializer, ILogs logs)
4445

4546
Messages.RegisterDtoIdMapping<StreamMessage, MessageInternalDTO>(dto => dto.Id);
4647
Messages.RegisterDtoIdMapping<StreamMessage, MessageResponseInternalDTO>(dto => dto.Id);
48+
49+
Polls.RegisterDtoIdMapping<StreamPoll, PollResponseDataInternalDTO>(dto => dto.Id);
4750
}
4851

4952
public ICacheRepository<StreamChannel> Channels { get; }
@@ -55,5 +58,7 @@ public Cache(StreamChatClient stateClient, ISerializer serializer, ILogs logs)
5558
public ICacheRepository<StreamLocalUserData> LocalUser { get; }
5659

5760
public ICacheRepository<StreamChannelMember> ChannelMembers { get; }
61+
62+
public ICacheRepository<StreamPoll> Polls { get; }
5863
}
5964
}

Assets/Plugins/StreamChat/Core/State/Caches/ICache.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ internal interface ICache
99
ICacheRepository<StreamUser> Users { get; }
1010
ICacheRepository<StreamLocalUserData> LocalUser { get; }
1111
ICacheRepository<StreamChannelMember> ChannelMembers { get; }
12+
ICacheRepository<StreamPoll> Polls { get; }
1213
}
1314
}

Assets/Plugins/StreamChat/Core/State/Caches/ICacheExt.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,8 @@ public static StreamUser TryCreateOrUpdate(this ICache cache, FullUserResponseIn
7676

7777
public static StreamUser TryCreateOrUpdate(this ICache cache, UserEventPayloadInternalDTO dto)
7878
=> dto == null ? null : cache.Users.CreateOrUpdate<StreamUser, UserEventPayloadInternalDTO>(dto, out _);
79+
80+
public static StreamPoll TryCreateOrUpdate(this ICache cache, PollResponseDataInternalDTO dto)
81+
=> dto == null ? null : cache.Polls.CreateOrUpdate<StreamPoll, PollResponseDataInternalDTO>(dto, out _);
7982
}
8083
}

Assets/Plugins/StreamChat/Core/State/StatefulModelsFactory.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public StreamMessage CreateStreamMessage(string uniqueId)
3535
public StreamUser CreateStreamUser(string uniqueId)
3636
=> new StreamUser(uniqueId, _cache.Users, _context);
3737

38+
public StreamPoll CreateStreamPoll(string uniqueId)
39+
=> new StreamPoll(uniqueId, _cache.Polls, _context);
40+
3841
private readonly ILogs _logs;
3942
private readonly StreamChatClient _streamChatClient;
4043
private readonly IStatefulModelContext _context;

0 commit comments

Comments
 (0)