Skip to content

Commit

Permalink
feat: add options to truncate
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdeme committed Dec 6, 2021
1 parent b23f1d8 commit f4f487e
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 9 deletions.
24 changes: 19 additions & 5 deletions src/stream-chat-net-test/ChannelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
20 changes: 17 additions & 3 deletions src/stream-chat-net/Channel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,26 @@ public async Task Delete()
throw StreamChatException.FromResponse(response);
}

public async Task Truncate()
public async Task<TruncateResponse> Truncate()
{
return await Truncate(null);
}

public async Task<TruncateResponse> 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<string> userIDs, MessageInput msg = null)
Expand Down
46 changes: 46 additions & 0 deletions src/stream-chat-net/ChannelObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<JObject>("message");
if (msgObj != null)
result.Message = Message.FromJObject(msgObj);
var chanObj = data.GetData<JObject>("channel");
if (chanObj != null)
result.Channel = ChannelObjectWithInfo.FromJObject(chanObj);
result.Duration = data.GetData<string>("duration");

return result;
}
}
}
3 changes: 2 additions & 1 deletion src/stream-chat-net/IChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public interface IChannel
Task<UpdateChannelResponse> Update(GenericData customData, MessageInput msg = null, bool skipPush = false);
Task<PartialUpdateChannelResponse> PartialUpdate(PartialUpdateChannelRequest partialUpdateChannelRequest);
Task Delete();
Task Truncate();
Task<TruncateResponse> Truncate();
Task<TruncateResponse> Truncate(TruncateOptions truncateOptions);

Task AddMembers(IEnumerable<string> userIDs, MessageInput msg = null);
Task RemoveMembers(IEnumerable<string> userIDs, MessageInput msg = null);
Expand Down

0 comments on commit f4f487e

Please sign in to comment.