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

Develop #63

Merged
merged 91 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
91 commits
Select commit Hold shift + click to select a range
8ea3a5f
Refactoring
manuelmayer-dev Jan 15, 2023
83ae6e2
Add EntityFramework
manuelmayer-dev Jan 15, 2023
300803f
Add Command handling
manuelmayer-dev Jan 16, 2023
24f119b
Add Command handling
manuelmayer-dev Jan 16, 2023
ecf8873
Fix command handling
manuelmayer-dev Jan 16, 2023
e689926
Improve status check
manuelmayer-dev Jan 16, 2023
0d85542
Add json property names to models
manuelmayer-dev Jan 16, 2023
c67e969
Add Dockerfile
manuelmayer-dev Jan 17, 2023
9e2fac3
Add webhook
manuelmayer-dev Jan 18, 2023
3b767ae
Update webhook system
manuelmayer-dev Jan 19, 2023
883de0b
Refactoring
manuelmayer-dev Jan 19, 2023
56139d7
Fix webhook message
manuelmayer-dev Jan 19, 2023
8d66ba8
Remove unused file
manuelmayer-dev Jan 19, 2023
b16a4a0
Add Discord logger
manuelmayer-dev Jan 19, 2023
e5c34db
Update status check service to order the results
manuelmayer-dev Jan 19, 2023
70443df
Refactoring
manuelmayer-dev Jan 20, 2023
0d6745d
Fix error handling in DiscordService
manuelmayer-dev Jan 20, 2023
42af3b6
Merge pull request #3 from Macro-Deck-org/feature/add-dependency-inje…
manuelmayer-dev Jan 20, 2023
e4a6fec
Added authentication
manuelmayer-dev Jan 20, 2023
9002eb9
Merge pull request #4 from Macro-Deck-org/feature/webhook-authentication
manuelmayer-dev Jan 20, 2023
d2f2c1f
Update Discord logging
manuelmayer-dev Jan 20, 2023
bf829c9
Merge pull request #5 from Macro-Deck-org/feature/improve-discord-log…
manuelmayer-dev Jan 20, 2023
30f768d
Fix Discord logging
manuelmayer-dev Jan 20, 2023
dae2825
Fix logging
manuelmayer-dev Jan 20, 2023
2f276b5
Update README.md
manuelmayer-dev Jan 20, 2023
8125cf8
Delete dotnet-build.yml
manuelmayer-dev Jan 20, 2023
c8b1a87
Merge branch 'main' into develop
manuelmayer-dev Jan 21, 2023
06be6cb
Merge branch 'main' into develop
manuelmayer-dev Jan 21, 2023
1a5c8b1
Fix and improve status check (#8)
manuelmayer-dev Jan 22, 2023
b53f03e
Update MacroBotApp.csproj
manuelmayer-dev Jan 22, 2023
bb2f52a
Merge branch 'main' into develop
manuelmayer-dev Jan 22, 2023
3c7c089
Fix/fix member screening (#10)
manuelmayer-dev Jan 22, 2023
25a38fc
Merge remote-tracking branch 'origin/develop' into develop
manuelmayer-dev Jan 22, 2023
5af9913
Minor improvements
manuelmayer-dev Jan 22, 2023
634086e
Merge main into develop (#13)
manuelmayer-dev Jan 22, 2023
b012f5d
Merge branch 'main' into develop
manuelmayer-dev Jan 22, 2023
ca693ad
Minor changes
manuelmayer-dev Jan 22, 2023
531717f
Remove glitched character
manuelmayer-dev Jan 22, 2023
a238c45
Delete test.yml
manuelmayer-dev Jan 22, 2023
8175026
Fix message filter: replies can contain mentions now
manuelmayer-dev Jan 22, 2023
ac87bbe
Minor improvements
manuelmayer-dev Jan 22, 2023
7d20195
Merge branch 'main' into develop
manuelmayer-dev Jan 22, 2023
c863786
Changed loglevel of log messages in the BroadcastWebhookAsync method
manuelmayer-dev Jan 22, 2023
588e5dc
Implement old extension store API (and some other changes) (#7)
jbcarreon123 Jan 23, 2023
ae5bcb2
Update docker-deploy-develop.yml (#17)
manuelmayer-dev Jan 23, 2023
7c67fe8
Removed errors
manuelmayer-dev Jan 23, 2023
8ed680e
update plugin commands
jbcarreon123 Jan 24, 2023
2609af1
changes to the gitignore
jbcarreon123 Jan 24, 2023
58e5c5f
Merge pull request #18 from Macro-Deck-org/update-plugin-commands
jbcarreon123 Jan 24, 2023
aa13fa3
Update docker-deploy-develop.yml
manuelmayer-dev Jan 24, 2023
1aa12d0
Update MacroBotApp.csproj
manuelmayer-dev Jan 27, 2023
637a7a8
Update dependency Serilog.Extensions.Hosting to v4.2.0 (#33)
renovate[bot] Jan 31, 2023
cf3d9a9
Update dependency System.Data.SQLite to v1.0.117 (#32)
renovate[bot] Jan 31, 2023
ecbc2e7
Update dependency Octokit to v4.0.4 (#31)
renovate[bot] Jan 31, 2023
a3efb1a
Update docker/login-action digest to 3da7dc6 (#28)
renovate[bot] Jan 31, 2023
aac116f
Update dependency AutoMapper to v12.0.1 (#29)
renovate[bot] Jan 31, 2023
b8e4fef
Update dependency Newtonsoft.Json to v13.0.2 (#30)
renovate[bot] Jan 31, 2023
e3930f5
Update dotnet monorepo (#38)
renovate[bot] Feb 15, 2023
fc10f6f
Update dependency Serilog.Extensions.Hosting to v5 (#37)
renovate[bot] Feb 15, 2023
8fd0083
Update dependency Octokit to v5 (#36)
renovate[bot] Feb 15, 2023
7c85ff0
Update microsoft/setup-msbuild action to v1.3.1 (#35)
renovate[bot] Feb 15, 2023
1d6bedd
Update dependency Serilog.Sinks.Console to v4.1.0 (#34)
renovate[bot] Feb 15, 2023
95ccc03
Update dotnet monorepo to v7.0.4 (#43)
renovate[bot] Mar 19, 2023
d6ba585
Update docker/login-action digest to 219c305 (#39)
renovate[bot] Mar 19, 2023
422ee5b
Update appleboy/ssh-action action to v0.1.8 (#40)
renovate[bot] Mar 19, 2023
6c89cb8
Update dependency Newtonsoft.Json to v13.0.3 (#41)
renovate[bot] Mar 19, 2023
7f8e3d9
Improve status check (#44)
manuelmayer-dev Mar 19, 2023
3e227d6
Update dependency Octokit to v5.0.2 (#42)
renovate[bot] Mar 19, 2023
8880e9a
Merge branch 'main' into develop
manuelmayer-dev Mar 19, 2023
1f556a8
Update dotnet monorepo to v7.0.5 (#52)
renovate[bot] Apr 16, 2023
3100c72
Update dependency AutoMapper.Extensions.Microsoft.DependencyInjection…
renovate[bot] Apr 16, 2023
1d1976c
Update docker/login-action digest to 65b78e6 (#46)
renovate[bot] Apr 16, 2023
513959a
Update appleboy/ssh-action action to v0.1.10 (#48)
renovate[bot] Apr 16, 2023
876ee9b
Update dependency Octokit to v5.0.4 (#49)
renovate[bot] Apr 16, 2023
2880e85
Update dependency Discord.Net to v3.10.0 (#47)
renovate[bot] Apr 17, 2023
cb54a8b
Fix formatting
manuelmayer-dev Apr 27, 2023
4e7dbe1
Feature/buymeacoffee (#56)
manuelmayer-dev Apr 27, 2023
10d1936
Fix route
manuelmayer-dev Apr 27, 2023
8617b77
Fix route
manuelmayer-dev Apr 27, 2023
32903fe
Fix logging
manuelmayer-dev Apr 27, 2023
b87eb31
Fix route
manuelmayer-dev Apr 27, 2023
2d128b9
Update dependency Octokit to v5.1.0 (#57)
renovate[bot] May 1, 2023
6b915e5
Update docker/login-action digest to 40891eb (#55)
renovate[bot] May 1, 2023
4ce1bf9
Create Startup.cs (#58)
manuelmayer-dev May 1, 2023
76dac86
Feature/buymeacoffee (#59)
manuelmayer-dev May 2, 2023
d79f663
Fix route
manuelmayer-dev May 2, 2023
8389e8b
Update MacroBotApp.csproj
manuelmayer-dev May 2, 2023
3d7a5a5
Add KoFi webhooks (#61)
manuelmayer-dev May 3, 2023
65ebfe5
Merge branch 'main' into develop
manuelmayer-dev May 3, 2023
9389dcd
Bump version
manuelmayer-dev May 3, 2023
9eb4fad
Update workflow
manuelmayer-dev May 3, 2023
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
2 changes: 2 additions & 0 deletions .github/workflows/docker-deploy-develop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- uses: fjogeleit/http-request-action@v1
continue-on-error: true
with:
url: 'https://testbot.api.macro-deck.app/webhook/deploy'
method: 'POST'
Expand All @@ -33,6 +34,7 @@ jobs:
- name: Push the Docker image
run: docker push suchbyte/macro-bot:dev_${{ github.sha }}
- uses: fjogeleit/http-request-action@v1
continue-on-error: true
with:
url: 'https://testbot.api.macro-deck.app/webhook/deploy'
method: 'POST'
Expand Down
7 changes: 7 additions & 0 deletions MacroBotApp/Config/KoFiConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace MacroBot.Config;

public class KoFiConfig : LoadableConfig<KoFiConfig>
{
public string VerificationToken { get; set; } = string.Empty;
public ulong ChannelId { get; set; }
}
50 changes: 50 additions & 0 deletions MacroBotApp/Controllers/KoFiController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Text.Json;
using MacroBot.Config;
using MacroBot.Crypto;
using MacroBot.ManagerInterfaces;
using MacroBot.Models.KoFi;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace MacroBot.Controllers;

[Route("/kofi")]
public class KoFiController : ControllerBase
{
private readonly KoFiConfig _koFiConfig;
private readonly IKoFiManager _koFiManager;

public KoFiController(KoFiConfig koFiConfig,
IKoFiManager koFiManager)
{
_koFiConfig = koFiConfig;
_koFiManager = koFiManager;
}

[HttpPost("webhook")]
[AllowAnonymous]
[Consumes("application/x-www-form-urlencoded")]
public async Task<IActionResult> DonationCreated([FromForm] string data)
{
var koFiWebhookRequest = JsonSerializer.Deserialize<KoFiWebhookRequest>(data);
if (koFiWebhookRequest == null)
{
return new BadRequestResult();
}
var verificationToken = koFiWebhookRequest.VerificationToken;
if (string.IsNullOrWhiteSpace(verificationToken))
{
return new UnauthorizedResult();
}

if (!verificationToken.EqualsCryptographically(_koFiConfig.VerificationToken))
{
return new StatusCodeResult(StatusCodes.Status403Forbidden);
}

koFiWebhookRequest.VerificationToken = null;

await _koFiManager.HandleWebhook(koFiWebhookRequest);
return Ok();
}
}
20 changes: 20 additions & 0 deletions MacroBotApp/Crypto/CryptoExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Security.Cryptography;
using System.Text;

namespace MacroBot.Crypto;

public static class CryptoExtensions
{
public static bool EqualsCryptographically(this string str1, string str2)
{
var hash1 = SHA256.HashData(Encoding.UTF8.GetBytes(str1));
var hash2 = SHA256.HashData(Encoding.UTF8.GetBytes(str2));

if (hash1.Length != hash2.Length)
{
return false;
}

return !hash1.Where((t, i) => t != hash2[i]).Any();
}
}
7 changes: 7 additions & 0 deletions MacroBotApp/Enums/KoFiWebhookType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace MacroBot.Enums;

public enum KoFiWebhookType
{
Donation,
Subscription,
}
2 changes: 1 addition & 1 deletion MacroBotApp/MacroBotApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<Version>1.1.0</Version>
<Version>1.2.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
123 changes: 123 additions & 0 deletions MacroBotApp/Manager/KoFiManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using System.ComponentModel;
using MacroBot.Config;
using MacroBot.Enums;
using MacroBot.ManagerInterfaces;
using MacroBot.Models.KoFi;
using MacroBot.Models.Webhook;
using MacroBot.ServiceInterfaces;

namespace MacroBot.Manager;

public class KoFiManager : IKoFiManager
{
private readonly KoFiConfig _koFiConfig;
private readonly IDiscordService _discordService;

public KoFiManager(KoFiConfig koFiConfig, IDiscordService discordService)
{
_koFiConfig = koFiConfig;
_discordService = discordService;
}

public async Task HandleWebhook(KoFiWebhookRequest koFiWebhookRequest)
{
switch (koFiWebhookRequest.Type)
{
case KoFiWebhookType.Donation:
await HandleDonation(koFiWebhookRequest);
break;
case KoFiWebhookType.Subscription:
await HandleSubscription(koFiWebhookRequest);
break;
default:
throw new InvalidEnumArgumentException("Type not found");
}
}

private async Task BroadcastDiscord(WebhookRequestEmbed webhookRequestEmbed)
{
var webhookRequest = new WebhookRequest
{
Embed = webhookRequestEmbed
};

var webhook = new WebhookItem
{
Id = "Ko-Fi",
ChannelId = _koFiConfig.ChannelId
};

await _discordService.BroadcastWebhookAsync(webhook, webhookRequest);
}

private async Task HandleDonation(KoFiWebhookRequest koFiWebhookRequest)
{
var webhookRequestEmbed = BuildEmbed(koFiWebhookRequest);
webhookRequestEmbed.Title = "New donation 🎊";
await BroadcastDiscord(webhookRequestEmbed);
}

private async Task HandleSubscription(KoFiWebhookRequest koFiWebhookRequest)
{

var webhookRequestEmbed = BuildEmbed(koFiWebhookRequest);
webhookRequestEmbed.Title = koFiWebhookRequest.IsFirstSubscriptionPayment
? "🎉 New monthly subscription 🎉"
: "🎉🎉 Monthly subscription renewal 🎉🎉";
await BroadcastDiscord(webhookRequestEmbed);
}

private static WebhookRequestEmbed BuildEmbed(KoFiWebhookRequest koFiWebhookRequest)
{
var supporterName = koFiWebhookRequest.FromName ?? "Anonymous";
var amount = koFiWebhookRequest.Amount ?? "Hidden";
var currency = koFiWebhookRequest.Currency ?? string.Empty;
var message = koFiWebhookRequest.IsPublic
? koFiWebhookRequest.Message
: null;

var webHookRequestEmbed = new WebhookRequestEmbed
{
Color = new WebhookRequestEmbedColor
{
R = 0,
G = 1,
B = 0
},
Fields = new List<WebhookRequestEmbedField>()
{
new ()
{
Name = "From",
Value = supporterName,
Inline = true
},
new ()
{
Name = "Amount",
Value = $"{amount} {currency}",
Inline = true
}
}
};

if (!string.IsNullOrWhiteSpace(message))
{
webHookRequestEmbed.Fields.Add(new WebhookRequestEmbedField
{
Name = "Message",
Value = message,
Inline = false
});
}

webHookRequestEmbed.Fields.Add(new WebhookRequestEmbedField
{
Name = "Also want to support the project?",
Value = "https://ko-fi.com/manuelmayer",
Inline = false
});

return webHookRequestEmbed;
}
}
8 changes: 8 additions & 0 deletions MacroBotApp/ManagerInterfaces/IKoFiManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using MacroBot.Models.KoFi;

namespace MacroBot.ManagerInterfaces;

public interface IKoFiManager
{
public Task HandleWebhook(KoFiWebhookRequest koFiWebhookRequest);
}
58 changes: 58 additions & 0 deletions MacroBotApp/Models/KoFi/KoFiWebhookRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Text.Json.Serialization;
using MacroBot.Enums;

namespace MacroBot.Models.KoFi;

public class KoFiWebhookRequest
{
[JsonPropertyName("verification_token")]
public string? VerificationToken { get; set; }

[JsonPropertyName("message_id")]
public string? MessageId { get; set; }

[JsonPropertyName("timestamp")]
public DateTime Timestamp { get; set; }

[JsonPropertyName("type"), JsonConverter(typeof(JsonStringEnumConverter))]
public KoFiWebhookType? Type { get; set; }

[JsonPropertyName("is_public")]
public bool IsPublic { get; set; }

[JsonPropertyName("from_name")]
public string? FromName { get; set; }

[JsonPropertyName("message")]
public string? Message { get; set; }

[JsonPropertyName("amount")]
public string? Amount { get; set; }

[JsonPropertyName("url")]
public string? Url { get; set; }

[JsonPropertyName("email")]
public string? Email { get; set; }

[JsonPropertyName("currency")]
public string? Currency { get; set; }

[JsonPropertyName("is_subscription_payment")]
public bool IsSubscriptionPayment { get; set; }

[JsonPropertyName("is_first_subscription_payment")]
public bool IsFirstSubscriptionPayment { get; set; }

[JsonPropertyName("kofi_transaction_id")]
public string? KofiTransactionId { get; set; }

[JsonPropertyName("shop_items")]
public object? ShopItems { get; set; }

[JsonPropertyName("tier_name")]
public string? TierName { get; set; }

[JsonPropertyName("shipping")]
public object? Shipping { get; set; }
}
2 changes: 1 addition & 1 deletion MacroBotApp/Paths.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private static string MainDirectory
public static readonly string CommandsConfigPath = Path.Combine(MainDirectory, "Commands.json");
public static readonly string StatusCheckConfigPath = Path.Combine(MainDirectory, "StatusCheck.json");
public static readonly string DatabasePath = Path.Combine(MainDirectory, "Database.db");
public static readonly string BuyMeACoffeeConfigPath = Path.Combine(MainDirectory, "BuyMeACoffee.json");
public static readonly string KoFiConfigPath = Path.Combine(MainDirectory, "KoFi.json");

public static void EnsureDirectoriesCreated()
{
Expand Down
7 changes: 5 additions & 2 deletions MacroBotApp/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using MacroBot.DataAccess.RepositoryInterfaces;
using MacroBot.Discord.Modules.Tagging;
using MacroBot.Extensions;
using MacroBot.Manager;
using MacroBot.ManagerInterfaces;
using MacroBot.ServiceInterfaces;
using MacroBot.Services;
using MacroBot.StartupConfig;
Expand Down Expand Up @@ -45,6 +47,7 @@ public void ConfigureServices(IServiceCollection services)
services.AddInjectableHostedService<IDiscordService, DiscordService>();
services.AddInjectableHostedService<IStatusCheckService, StatusCheckService>();
services.AddInjectableHostedService<ITimerService, TimerService>();
services.AddScoped<IKoFiManager, KoFiManager>();
services.AddHttpClient();
services.AddSwagger();
services.AddControllers();
Expand All @@ -70,13 +73,13 @@ private static void LoadRegisterConfigs(IServiceCollection services)
{
Task.Run(async () =>
{
var buyMeACoffeeConfig = await BuyMeACoffeeConfig.LoadAsync(Paths.BuyMeACoffeeConfigPath);
var koFiConfig = await KoFiConfig.LoadAsync(Paths.KoFiConfigPath);
var botConfig = await BotConfig.LoadAsync(Paths.BotConfigPath);
var commandsConfig = await CommandsConfig.LoadAsync(Paths.CommandsConfigPath);
var statusCheckConfig = await StatusCheckConfig.LoadAsync(Paths.StatusCheckConfigPath);
var webhooksConfig = await WebhooksConfig.LoadAsync(Paths.WebhooksPath);

services.AddSingleton(buyMeACoffeeConfig);
services.AddSingleton(koFiConfig);
services.AddSingleton(statusCheckConfig);
services.AddSingleton(webhooksConfig);
services.AddSingleton(botConfig);
Expand Down