Skip to content
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

81 feature sub alerts #148

Merged
merged 7 commits into from
Aug 20, 2023
Merged
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
13 changes: 13 additions & 0 deletions .github/workflows/release-docker-images.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ jobs:
docker_hub_username: ${{ secrets.DOCKER_HUB_USERNAME }}
docker_hub_token: ${{ secrets.DOCKER_HUB_TOKEN }}

twitch-alerts:
name: Publish Twitch Alert Agent
uses: rGunti/FloppyBot/.github/workflows/template-release-docker-image.yml@main
with:
dockerfile_path: src/FloppyBot.Aux.TwitchAlerts.Agent/Dockerfile
project_path: src/FloppyBot.Aux.TwitchAlerts.Agent/FloppyBot.Aux.TwitchAlerts.Agent.csproj
image_name: floppybot/twitch-alerts
image_tag: ${{ github.ref_name }}
push_latest: true
secrets:
docker_hub_username: ${{ secrets.DOCKER_HUB_USERNAME }}
docker_hub_token: ${{ secrets.DOCKER_HUB_TOKEN }}

webapi:
name: Publish Web API Agent
uses: rGunti/FloppyBot/.github/workflows/template-release-docker-image.yml@main
Expand Down
5 changes: 5 additions & 0 deletions src/FloppyBot.Aux.MessageCounter.Core/MessageCounter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FloppyBot.Base.Configuration;
using FloppyBot.Chat;
using FloppyBot.Chat.Entities;
using FloppyBot.Communication;
using Microsoft.Extensions.Configuration;
Expand Down Expand Up @@ -48,6 +49,10 @@ public void Stop()

private void OnMessageReceived(ChatMessage chatMessage)
{
if (chatMessage.EventName != SharedEventTypes.CHAT_MESSAGE)
{
return;
}
#if DEBUG
_logger.LogInformation("Received chat message to count: {@ChatMessage}", chatMessage);
#endif
Expand Down
5 changes: 5 additions & 0 deletions src/FloppyBot.Aux.TwitchAlerts.Agent/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM mcr.microsoft.com/dotnet/runtime:6.0 AS runtime
COPY ./out /app
COPY ./app-version /app/version
WORKDIR /app
ENTRYPOINT [ "dotnet", "FloppyBot.Aux.TwitchAlerts.Agent.dll" ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>dotnet-FloppyBot.Aux.TwitchAlerts.Agent-FE25745F-A00C-4D5C-B500-BD5A61D5841B</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FloppyBot.Aux.TwitchAlerts.Core\FloppyBot.Aux.TwitchAlerts.Core.csproj" />
<ProjectReference Include="..\FloppyBot.Base.Configuration\FloppyBot.Base.Configuration.csproj" />
<ProjectReference Include="..\FloppyBot.Base.Logging\FloppyBot.Base.Logging.csproj" />
<ProjectReference Include="..\FloppyBot.Base.Storage.MongoDb\FloppyBot.Base.Storage.MongoDb.csproj" />
<ProjectReference Include="..\FloppyBot.Communication.Redis\FloppyBot.Communication.Redis.csproj" />
<ProjectReference Include="..\FloppyBot.HealthCheck.Core\FloppyBot.HealthCheck.Core.csproj" />
<ProjectReference Include="..\FloppyBot.HealthCheck.KillSwitch\FloppyBot.HealthCheck.KillSwitch.csproj" />
</ItemGroup>
</Project>
27 changes: 27 additions & 0 deletions src/FloppyBot.Aux.TwitchAlerts.Agent/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using FloppyBot.Aux.TwitchAlerts.Agent;
using FloppyBot.Aux.TwitchAlerts.Core;
using FloppyBot.Base.Configuration;
using FloppyBot.Base.Logging;
using FloppyBot.Base.Storage.MongoDb;
using FloppyBot.Communication.Redis.Config;
using FloppyBot.HealthCheck.Core;
using FloppyBot.HealthCheck.KillSwitch;

IHostBuilder hostBuilder = Host.CreateDefaultBuilder(args).SetupConfiguration().SetupSerilog();

IHost host = hostBuilder
.ConfigureServices(services =>
{
services
.AddRedisCommunication()
.AddHealthCheck()
.AddKillSwitchTrigger()
.AddKillSwitch()
.AddMongoDbStorage()
.AddTwitchAlertService()
.AddTwitchAlertCore()
.AddHostedService<TwitchAlertHost>();
})
.Build();

await host.ArmKillSwitch().LogAndRun();
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"profiles": {
"FloppyBot.Aux.TwitchAlerts.Agent": {
"commandName": "Project",
"dotnetRunMessages": true,
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development"
}
}
}
}
36 changes: 36 additions & 0 deletions src/FloppyBot.Aux.TwitchAlerts.Agent/TwitchAlertHost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using FloppyBot.Aux.TwitchAlerts.Core;

namespace FloppyBot.Aux.TwitchAlerts.Agent;

public class TwitchAlertHost : BackgroundService
{
private readonly ILogger<TwitchAlertHost> _logger;
private readonly TwitchAlertListener _listener;

public TwitchAlertHost(ILogger<TwitchAlertHost> logger, TwitchAlertListener listener)
{
_logger = logger;
_listener = listener;
}

public override Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Starting up Twitch Alert Agent ...");
_listener.Start();

_logger.LogInformation("Awaiting new messages to count");
return Task.CompletedTask;
}

public override Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Shutting down Twitch Alert Agent ...");
_listener.Stop();
return base.StopAsync(cancellationToken);
}

protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
return Task.CompletedTask;
}
}
11 changes: 11 additions & 0 deletions src/FloppyBot.Aux.TwitchAlerts.Agent/floppybot.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Serilog": {
"MinimumLevel": {
"Default": "Verbose",
"Override": {
"FloppyBot.HealthCheck.Core.HealthCheckProducerCronJob": "Debug"
}
}
},
"InstanceName": "DEV"
}
17 changes: 17 additions & 0 deletions src/FloppyBot.Aux.TwitchAlerts.Agent/floppybot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ConnectionStrings": {
"MongoDb": "mongodb://localhost:27017/FloppyBot",
"Redis": "localhost",
"HealthCheck": "{Redis}|HealthCheck",
"KillSwitch": "{Redis}|KillSwitch",
"MessageInput": "{Redis}|Message.Received",
"MessageOutput": "{Redis}|Message.Responded.Twitch"
},
"InstanceName": "1"
}
19 changes: 19 additions & 0 deletions src/FloppyBot.Aux.TwitchAlerts.Core/DiSetup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using FloppyBot.Aux.TwitchAlerts.Core.Entities;
using Microsoft.Extensions.DependencyInjection;

namespace FloppyBot.Aux.TwitchAlerts.Core;

public static class DiSetup
{
public static IServiceCollection AddTwitchAlertCore(this IServiceCollection services)
{
return services.AddSingleton<TwitchAlertListener>();
}

public static IServiceCollection AddTwitchAlertService(this IServiceCollection services)
{
return services
.AddTransient<ITwitchAlertService, TwitchAlertService>()
.AddAutoMapper(typeof(TwitchAlertListener));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using AutoMapper;
using FloppyBot.Aux.TwitchAlerts.Core.Entities.Storage;
using FloppyBot.Base.Storage;

namespace FloppyBot.Aux.TwitchAlerts.Core.Entities;

public interface ITwitchAlertService
{
TwitchAlertSettings? GetAlertSettings(string channelId);
void StoreAlertSettings(TwitchAlertSettings settings);
}

public class TwitchAlertService : ITwitchAlertService
{
private readonly IRepository<TwitchAlertSettingsEo> _repository;
private readonly IMapper _mapper;

public TwitchAlertService(IRepositoryFactory repositoryFactory, IMapper mapper)
{
_mapper = mapper;
_repository = repositoryFactory.GetRepository<TwitchAlertSettingsEo>();
}

public TwitchAlertSettings? GetAlertSettings(string channelId)
{
return _mapper.Map<TwitchAlertSettings>(_repository.GetById(channelId));
}

public void StoreAlertSettings(TwitchAlertSettings settings)
{
_repository.Upsert(_mapper.Map<TwitchAlertSettingsEo>(settings));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using FloppyBot.Base.Storage;

namespace FloppyBot.Aux.TwitchAlerts.Core.Entities.Storage;

public record TwitchAlertSettingsEo(
string Id,
bool SubAlertsEnabled,
TwitchAlertMessageEo[] SubMessages,
TwitchAlertMessageEo[] ReSubMessages,
TwitchAlertMessageEo[] GiftSubMessages,
TwitchAlertMessageEo[] GiftSubCommunityMessages
) : IEntity<TwitchAlertSettingsEo>
{
public TwitchAlertSettingsEo WithId(string newId)
{
return this with { Id = newId };
}
}

public record TwitchAlertMessageEo(
string DefaultMessage,
string? Tier1Message,
string? Tier2Message,
string? Tier3Message,
string? PrimeMessage
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Collections.Immutable;
using AutoMapper;

namespace FloppyBot.Aux.TwitchAlerts.Core.Entities.Storage;

public class TwitchAlertStorageProfile : Profile
{
public TwitchAlertStorageProfile()
{
// dto -> eo
CreateMap<TwitchAlertSettings, TwitchAlertSettingsEo>()
.ConvertUsing(
(dto, _, ctx) =>
new TwitchAlertSettingsEo(
dto.Id,
dto.SubAlertsEnabled,
dto.SubMessage
.Select(msg => ctx.Mapper.Map<TwitchAlertMessageEo>(msg))
.ToArray(),
dto.ReSubMessage
.Select(msg => ctx.Mapper.Map<TwitchAlertMessageEo>(msg))
.ToArray(),
dto.GiftSubMessage
.Select(msg => ctx.Mapper.Map<TwitchAlertMessageEo>(msg))
.ToArray(),
dto.GiftSubCommunityMessage
.Select(msg => ctx.Mapper.Map<TwitchAlertMessageEo>(msg))
.ToArray()
)
);
CreateMap<TwitchAlertMessage, TwitchAlertMessageEo>();

// eo -> dto
CreateMap<TwitchAlertSettingsEo, TwitchAlertSettings>()
.ConvertUsing(
(eo, _, ctx) =>
new TwitchAlertSettings
{
Id = eo.Id,
SubAlertsEnabled = eo.SubAlertsEnabled,
SubMessage = eo.SubMessages
.Select(msg => ctx.Mapper.Map<TwitchAlertMessage>(msg))
.ToImmutableList(),
ReSubMessage = eo.ReSubMessages
.Select(msg => ctx.Mapper.Map<TwitchAlertMessage>(msg))
.ToImmutableList(),
GiftSubMessage = eo.GiftSubMessages
.Select(msg => ctx.Mapper.Map<TwitchAlertMessage>(msg))
.ToImmutableList(),
GiftSubCommunityMessage = eo.GiftSubCommunityMessages
.Select(msg => ctx.Mapper.Map<TwitchAlertMessage>(msg))
.ToImmutableList(),
}
);
CreateMap<TwitchAlertMessageEo, TwitchAlertMessage>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#pragma warning disable CS8618
using System.Collections.Immutable;
using FloppyBot.Base.EquatableCollections;
using FloppyBot.Base.Storage;

namespace FloppyBot.Aux.TwitchAlerts.Core.Entities;

public record TwitchAlertSettings : IEntity<TwitchAlertSettings>
{
private readonly IImmutableList<TwitchAlertMessage> _subMessages =
ImmutableList<TwitchAlertMessage>.Empty;
private readonly IImmutableList<TwitchAlertMessage> _reSubMessages =
ImmutableList<TwitchAlertMessage>.Empty;
private readonly IImmutableList<TwitchAlertMessage> _giftSubMessages =
ImmutableList<TwitchAlertMessage>.Empty;
private readonly IImmutableList<TwitchAlertMessage> _giftSubCommunityMessages =
ImmutableList<TwitchAlertMessage>.Empty;

public string Id { get; init; }

public bool SubAlertsEnabled { get; init; }

public IImmutableList<TwitchAlertMessage> SubMessage
{
get => _subMessages;
init => _subMessages = value.WithValueSemantics();
}

public IImmutableList<TwitchAlertMessage> ReSubMessage
{
get => _reSubMessages;
init => _reSubMessages = value.WithValueSemantics();
}

public IImmutableList<TwitchAlertMessage> GiftSubMessage
{
get => _giftSubMessages;
init => _giftSubMessages = value.WithValueSemantics();
}

public IImmutableList<TwitchAlertMessage> GiftSubCommunityMessage
{
get => _giftSubCommunityMessages;
init => _giftSubCommunityMessages = value.WithValueSemantics();
}

public TwitchAlertSettings WithId(string newId)
{
return this with { Id = newId };
}
}

public record TwitchAlertMessage(
string DefaultMessage,
string? Tier1Message = null,
string? Tier2Message = null,
string? Tier3Message = null,
string? PrimeMessage = null
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\FloppyBot.Base.Configuration\FloppyBot.Base.Configuration.csproj" />
<ProjectReference Include="..\FloppyBot.Base.Extensions\FloppyBot.Base.Extensions.csproj" />
<ProjectReference Include="..\FloppyBot.Base.Storage\FloppyBot.Base.Storage.csproj" />
<ProjectReference Include="..\FloppyBot.Chat.Twitch.Events\FloppyBot.Chat.Twitch.Events.csproj" />
<ProjectReference Include="..\FloppyBot.Chat\FloppyBot.Chat.csproj" />
<ProjectReference Include="..\FloppyBot.Communication\FloppyBot.Communication.csproj" />
</ItemGroup>



<ItemGroup>
<PackageReference Include="AutoMapper" Version="12.0.1" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
</ItemGroup>

</Project>
Loading
Loading