Skip to content

Commit

Permalink
feat: add user custom events (#96)
Browse files Browse the repository at this point in the history
Co-authored-by: ferhat elmas <elmas.ferhat@gmail.com>
  • Loading branch information
peterdeme and ferhatelmas authored Feb 14, 2022
1 parent f358d3e commit e94416a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Clients/EventClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,11 @@ public async Task<SendEventResponse> SendEventAsync(string channelType, string c
HttpMethod.POST,
HttpStatusCode.Created,
new { @event = @event });

public async Task<ApiResponse> SendUserCustomEventAsync(string userId, UserCustomEvent @event)
=> await ExecuteRequestAsync<ApiResponse>($"users/{userId}/event",
HttpMethod.POST,
HttpStatusCode.Created,
new { @event = @event });
}
}
9 changes: 9 additions & 0 deletions src/Clients/IEventClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,14 @@ public interface IEventClient
/// </summary>
/// <remarks>https://getstream.io/chat/docs/dotnet-csharp/custom_events/?language=csharp</remarks>
Task<SendEventResponse> SendEventAsync(string channelType, string channelId, Event @event);

/// <summary>
/// <para>Sends a custom event to a user.</para>
/// This allows you to send custom events to a connected user. The event is delivered
/// to all connected clients for that user. It is only available with server-side authentication.
/// A copy of the event is sent via web-hooks if it is enabled.
/// </summary>
/// <remarks>https://getstream.io/chat/docs/dotnet-csharp/custom_events/?language=csharp</remarks>
Task<ApiResponse> SendUserCustomEventAsync(string userId, UserCustomEvent @event);
}
}
6 changes: 6 additions & 0 deletions src/Models/Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ public class Event : CustomDataBase
public DateTimeOffset? CreatedAt { get; set; }
}

public class UserCustomEvent : CustomDataBase
{
[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "type")]
public string Type { get; set; }
}

public class SendEventResponse : ApiResponse
{
public Event Event { get; set; }
Expand Down
12 changes: 12 additions & 0 deletions tests/EventClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Threading.Tasks;
using FluentAssertions;
using NUnit.Framework;
Expand Down Expand Up @@ -44,5 +45,16 @@ public async Task TestSendEventAsync()
resp.Event.User.Id.Should().BeEquivalentTo(_user.Id);
resp.Event.GetData<int[]>("foo")[0].Should().Be(expectedEvent.GetData<int[]>("foo")[0]);
}

[Test]
public async Task TestSendCustomUserEventAsync()
{
var customEvent = new UserCustomEvent { Type = "friendship_request" };
customEvent.SetData("text", "Test field");

Func<Task> eventTask = () => _eventClient.SendUserCustomEventAsync(_user.Id, customEvent);

await eventTask.Should().NotThrowAsync();
}
}
}

0 comments on commit e94416a

Please sign in to comment.