From 9f7638c7faafd50cea059b3074d3ae103a18fc8f Mon Sep 17 00:00:00 2001 From: peterdeme Date: Mon, 6 Dec 2021 17:01:50 +0100 Subject: [PATCH] feat: add options to truncate --- src/stream-chat-net-test/ChannelTests.cs | 28 +++++++++--- src/stream-chat-net/Channel.cs | 22 ++++++++-- src/stream-chat-net/ChannelObject.cs | 55 ++++++++++++++++++++++++ src/stream-chat-net/IChannel.cs | 3 +- src/stream-chat-net/MessageInput.cs | 4 +- 5 files changed, 100 insertions(+), 12 deletions(-) diff --git a/src/stream-chat-net-test/ChannelTests.cs b/src/stream-chat-net-test/ChannelTests.cs index c1b396e..5fa9848 100644 --- a/src/stream-chat-net-test/ChannelTests.cs +++ b/src/stream-chat-net-test/ChannelTests.cs @@ -665,14 +665,32 @@ public async Task TestTruncate() }; var msg3 = await channel.SendMessage(inMsg, user3.ID); - var chanState = await channel.Query(new ChannelQueryParams(false, true)); - Assert.AreEqual(3, chanState.Messages.Count); + await EnsureChannelMessageCount(channel, messageCount: 3); - await channel.Truncate(); + // Truncate without options + var response = await channel.Truncate(); + await EnsureChannelMessageCount(channel, messageCount: 0); - chanState = await channel.Query(new ChannelQueryParams(false, true)); + // Truncate with options + await channel.SendMessage(inMsg, user3.ID); + await EnsureChannelMessageCount(channel, messageCount: 1); - Assert.AreEqual(0, chanState.Messages.Count); + await channel.Truncate(new TruncateOptions + { + SkipPush = true, + Message = new MessageInput + { + Text = "This channel is getting truncated", + User = new User { ID = user1.ID }, + }, + }); + await EnsureChannelMessageCount(channel, messageCount: 1); + } + + private static async Task EnsureChannelMessageCount(IChannel channel, int messageCount) + { + var chanState = await channel.Query(new ChannelQueryParams(false, true)); + Assert.AreEqual(messageCount, chanState.Messages.Count); } [Test] diff --git a/src/stream-chat-net/Channel.cs b/src/stream-chat-net/Channel.cs index d81aacf..d5f0714 100644 --- a/src/stream-chat-net/Channel.cs +++ b/src/stream-chat-net/Channel.cs @@ -224,7 +224,7 @@ public async Task Update(GenericData customData, MessageI public async Task PartialUpdate(PartialUpdateChannelRequest partialUpdateChannelRequest) { var request = this._client.BuildAppRequest(this.Endpoint, HttpMethod.PATCH); - request.SetJsonBody(JsonConvert.SerializeObject(partialUpdateChannelRequest)); + request.SetJsonBody(partialUpdateChannelRequest.ToJObject().ToString()); var response = await this._client.MakeRequest(request); if (response.StatusCode == System.Net.HttpStatusCode.OK) @@ -252,12 +252,26 @@ public async Task Delete() throw StreamChatException.FromResponse(response); } - public async Task Truncate() + public async Task Truncate() + { + return await Truncate(null); + } + + public async Task Truncate(TruncateOptions truncateOptions) { var request = this._client.BuildAppRequest(this.Endpoint + "/truncate", HttpMethod.POST); + if (truncateOptions != null) + { + request.SetJsonBody(truncateOptions.ToJObject().ToString()); + } + var response = await this._client.MakeRequest(request); - if (response.StatusCode != System.Net.HttpStatusCode.Created) - throw StreamChatException.FromResponse(response); + if (response.StatusCode == System.Net.HttpStatusCode.Created) + { + var respObj = JObject.Parse(response.Content); + return TruncateResponse.FromJObject(respObj); + } + throw StreamChatException.FromResponse(response); } public async Task AddMembers(IEnumerable userIDs, MessageInput msg = null) diff --git a/src/stream-chat-net/ChannelObject.cs b/src/stream-chat-net/ChannelObject.cs index 899d0cb..2584885 100644 --- a/src/stream-chat-net/ChannelObject.cs +++ b/src/stream-chat-net/ChannelObject.cs @@ -173,6 +173,14 @@ public class PartialUpdateChannelRequest [JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "unset")] public IEnumerable Unset { get; set; } + + internal JObject ToJObject() + { + var root = JObject.FromObject(this); + if (this.User != null) + root.Add("user", this.User.ToJObject()); + return root; + } } public class PartialUpdateChannelResponse @@ -207,4 +215,51 @@ internal static PartialUpdateChannelResponse FromJObject(JObject jObj) return result; } } + + public class TruncateOptions + { + + [JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "hard_delete")] + public bool? HardDelete { get; set; } + + [JsonIgnore] + public MessageInput Message { get; set; } + + [JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "skip_push")] + public bool? SkipPush { get; set; } + + [JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "truncated_at")] + public DateTime? TruncatedAt { get; set; } + + internal JObject ToJObject() + { + var root = JObject.FromObject(this); + if (this.Message != null) + root.Add("message", this.Message.ToJObject()); + return root; + } + } + + public class TruncateResponse + { + public string Duration { get; set; } + public Message Message { get; set; } + public ChannelObjectWithInfo Channel { get; set; } + + internal static TruncateResponse FromJObject(JObject jObj) + { + var result = new TruncateResponse(); + + var data = JsonHelpers.FromJObject(result, jObj); + var msgObj = data.GetData("message"); + if (msgObj != null) + result.Message = Message.FromJObject(msgObj); + var chanObj = data.GetData("channel"); + if (chanObj != null) + result.Channel = ChannelObjectWithInfo.FromJObject(chanObj); + result.Duration = data.GetData("duration"); + + return result; + } + } } diff --git a/src/stream-chat-net/IChannel.cs b/src/stream-chat-net/IChannel.cs index 90c7af4..4574a40 100644 --- a/src/stream-chat-net/IChannel.cs +++ b/src/stream-chat-net/IChannel.cs @@ -25,7 +25,8 @@ public interface IChannel Task Update(GenericData customData, MessageInput msg = null, bool skipPush = false); Task PartialUpdate(PartialUpdateChannelRequest partialUpdateChannelRequest); Task Delete(); - Task Truncate(); + Task Truncate(); + Task Truncate(TruncateOptions truncateOptions); Task AddMembers(IEnumerable userIDs, MessageInput msg = null); Task RemoveMembers(IEnumerable userIDs, MessageInput msg = null); diff --git a/src/stream-chat-net/MessageInput.cs b/src/stream-chat-net/MessageInput.cs index 2165b90..6ce72d1 100644 --- a/src/stream-chat-net/MessageInput.cs +++ b/src/stream-chat-net/MessageInput.cs @@ -27,7 +27,7 @@ public class MessageInput : CustomDataBase public string ParentID { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "shown_in_channel")] - public bool ShownInChannel { get; set; } + public bool? ShownInChannel { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "created_at")] public DateTime? CreatedAt { get; set; } @@ -39,7 +39,7 @@ public class MessageInput : CustomDataBase public List MentionedUsers { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "silent")] - public bool Silent { get; set; } + public bool? Silent { get; set; } public MessageInput() { }