Skip to content

Commit

Permalink
[PBE-537] Possibility to send system messages (#126)
Browse files Browse the repository at this point in the history
* Possibility to send system messages

* wip: created MessageRequestType enum

* Remove invalid MessageRequestType values + Add StringEnumConverter

* Add default regular message type + ignore default values during serialization

---------

Co-authored-by: Daniel Sierpiński <33436839+sierpinskid@users.noreply.github.com>
  • Loading branch information
totalimmersion and sierpinskid authored Jul 10, 2023
1 parent 0e0b195 commit 44e5cb4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Models/MessageRequest.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace StreamChat.Models
{
[JsonConverter(typeof(StringEnumConverter))]
public enum MessageRequestType
{
[EnumMember(Value = @"regular")]
Regular = 0,

[EnumMember(Value = @"system")]
System = 1,
}

public class MessageRequest : CustomDataBase
{
public string Id { get; set; }
public MessageRequestType Type { get; set; }
public string Text { get; set; }
public string Mml { get; set; }
public UserRequest User { get; set; }
Expand Down
1 change: 1 addition & 0 deletions src/Utils/JsonSerializerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal static class StreamJsonConverter
NamingStrategy = new SnakeCaseNamingStrategy(),
},
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore,
};

internal static string SerializeObject(object obj) => JsonConvert.SerializeObject(obj, Settings);
Expand Down
24 changes: 24 additions & 0 deletions src/Utils/Utils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;

namespace StreamChat.Utils
{
public static class Utils
{
/// <summary>
/// Return enum value string representation respecting optional <see cref="EnumMemberAttribute"/> that overrides string representation
/// </summary>
public static string ToEnumMemberString<T>(this T value)
where T : Enum
{
return typeof(T)
.GetTypeInfo()
.DeclaredMembers
.SingleOrDefault(x => x.Name == value.ToString())
?.GetCustomAttribute<EnumMemberAttribute>(false)
?.Value ?? value.ToString();
}
}
}
14 changes: 14 additions & 0 deletions tests/MessageClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using StreamChat.Clients;
using StreamChat.Exceptions;
using StreamChat.Models;
using StreamChat.Utils;

namespace StreamChatTests
{
Expand Down Expand Up @@ -74,6 +75,19 @@ public async Task TestSendMessageAsync()
actualMsg.Attachments[0].GetData<string>("baz").Should().BeEquivalentTo("bazky");
}

[Test]
public async Task TestSendSystemMessageAsync()
{
var expectedMessage = new MessageRequest { Text = Guid.NewGuid().ToString(), Type = MessageRequestType.System };

var msgResp = await _messageClient.SendMessageAsync(_channel.Type, _channel.Id, expectedMessage, _user.Id);

var channel = await _channelClient.GetOrCreateAsync(_channel.Type, _channel.Id, new ChannelGetRequest { Watch = false, State = true });
var actualMsg = channel.Messages.First(m => m.Id == msgResp.Message.Id);
actualMsg.User.Id.Should().BeEquivalentTo(_user.Id);
actualMsg.Type.Should().BeEquivalentTo(MessageRequestType.System.ToEnumMemberString());
}

[Test]
public async Task TestUpdateMessageAsync()
{
Expand Down

0 comments on commit 44e5cb4

Please sign in to comment.