From f4f487ee67a6365b5da09e58580cf2e2434602a0 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 | 24 ++++++++++--- src/stream-chat-net/Channel.cs | 20 +++++++++-- src/stream-chat-net/ChannelObject.cs | 46 ++++++++++++++++++++++++ src/stream-chat-net/IChannel.cs | 3 +- 4 files changed, 84 insertions(+), 9 deletions(-) diff --git a/src/stream-chat-net-test/ChannelTests.cs b/src/stream-chat-net-test/ChannelTests.cs index c1b396e..891e55b 100644 --- a/src/stream-chat-net-test/ChannelTests.cs +++ b/src/stream-chat-net-test/ChannelTests.cs @@ -665,14 +665,28 @@ 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" }, + }); + await EnsureChannelMessageCount(channel, messageCount: 0); + } + + 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..fd6bb47 100644 --- a/src/stream-chat-net/Channel.cs +++ b/src/stream-chat-net/Channel.cs @@ -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..bbd9ada 100644 --- a/src/stream-chat-net/ChannelObject.cs +++ b/src/stream-chat-net/ChannelObject.cs @@ -207,4 +207,50 @@ 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);