Skip to content

Add EventHooks to AppSettingsBase #171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/Models/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,55 @@ public enum UniqueUsernameEnforcementPolicy
Team,
}

[JsonConverter(typeof(StringEnumConverter))]
public enum HookType
{
[EnumMember(Value = "webhook")]
Webhook,

[EnumMember(Value = "sqs")]
SQS,

[EnumMember(Value = "sns")]
SNS,
}

[JsonConverter(typeof(StringEnumConverter))]
public enum AuthType
{
[EnumMember(Value = "keys")]
Keys,

[EnumMember(Value = "role")]
Role,

[EnumMember(Value = "resource")]
Resource,
}

public class EventHook
{
public string Id { get; set; }
public HookType HookType { get; set; }
public bool Enabled { get; set; }
public List<string> EventTypes { get; set; }
public string WebhookUrl { get; set; }
public string SqsQueueUrl { get; set; }
public string SqsRegion { get; set; }
public AuthType? SqsAuthType { get; set; }
public string SqsKey { get; set; }
public string SqsSecret { get; set; }
public string SqsRoleArn { get; set; }
public string SnsTopicArn { get; set; }
public string SnsRegion { get; set; }
public AuthType? SnsAuthType { get; set; }
public string SnsKey { get; set; }
public string SnsSecret { get; set; }
public string SnsRoleArn { get; set; }
public DateTimeOffset CreatedAt { get; set; }
public DateTimeOffset UpdatedAt { get; set; }
}

public abstract class AppSettingsBase
{
[JsonProperty("disable_auth_checks")]
Expand Down Expand Up @@ -72,6 +121,7 @@ public abstract class AppSettingsBase
public DateTimeOffset? RevokeTokensIssuedBefore { get; set; }
public UniqueUsernameEnforcementPolicy? EnforceUniqueUsernames { get; set; }
public Dictionary<string, List<string>> Grants { get; set; }
public List<EventHook> EventHooks { get; set; }
}

public class AppSettingsRequest : AppSettingsBase
Expand Down
50 changes: 49 additions & 1 deletion tests/AppClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Threading.Tasks;
using FluentAssertions;
using NUnit.Framework;
using StreamChat;
using StreamChat.Exceptions;
using StreamChat.Models;

namespace StreamChatTests
Expand Down Expand Up @@ -153,5 +155,51 @@ public async Task TestWritingAppGrantsAsync()
List<string> GetUserGrants(Dictionary<string, List<string>> grants)
=> grants.First(g => g.Key == "user").Value;
}

[Test]
public async Task TestEventHooksAsync()
{
var eventHook = new EventHook
{
HookType = HookType.Webhook,
Enabled = true,
EventTypes = new List<string> { "message.new", "message.updated" },
WebhookUrl = "https://example.com/webhook",
};

var sqsHook = new EventHook
{
HookType = HookType.SQS,
Enabled = true,
EventTypes = new List<string> { "user.updated" },
SqsQueueUrl = "https://sqs.region.amazonaws.com/123456789012/queue-name",
SqsRegion = "us-east-1",
SqsAuthType = AuthType.Resource,
};

var snsHook = new EventHook
{
HookType = HookType.SNS,
Enabled = true,
EventTypes = new List<string> { "channel.updated" },
SnsTopicArn = "arn:aws:sns:us-east-1:123456789012:topic-name",
SnsRegion = "us-east-1",
SnsAuthType = AuthType.Resource,
};

var eventHooks = new List<EventHook> { eventHook, sqsHook, snsHook };

try
{
await _appClient.UpdateAppSettingsAsync(new AppSettingsRequest
{
EventHooks = eventHooks,
});
}
catch (StreamChatException ex)
{
ex.Message.Should().Contain("cannot set event hooks in hook v1 system");
}
}
}
}
}
Loading