Skip to content

Commit b53da06

Browse files
Merge pull request #45 from nullinside-development-group/feature/Poc2Prod
Abstracting TwitchApiProxy -> ITwitchApiProxy
2 parents 7ce25f8 + 6c759e1 commit b53da06

File tree

14 files changed

+313
-240
lines changed

14 files changed

+313
-240
lines changed

src/Nullinside.Api.Common.AspNetCore/Nullinside.Api.Common.AspNetCore.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2121
<PrivateAssets>all</PrivateAssets>
2222
</PackageReference>
23-
<PackageReference Include="MySql.EntityFrameworkCore" Version="8.0.8" />
24-
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.9.0" />
23+
<PackageReference Include="MySql.EntityFrameworkCore" Version="8.0.8"/>
24+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.9.0"/>
2525
</ItemGroup>
2626

2727
<ItemGroup>

src/Nullinside.Api.Common/Auth/AuthUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Nullinside.Api.Common.Auth;
44

55
/// <summary>
6-
/// Random utilities for authentication.
6+
/// Random utilities for authentication.
77
/// </summary>
88
public static class AuthUtils {
99
/// <summary>

src/Nullinside.Api.Common/Desktop/GitHubUpdateManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Nullinside.Api.Common.Desktop;
77
/// <summary>
88
/// Handles checking for updates to the application via GitHub releases.
99
/// </summary>
10-
public class GitHubUpdateManager {
10+
public static class GitHubUpdateManager {
1111
/// <summary>
1212
/// Gets the latest version number of the release.
1313
/// </summary>

src/Nullinside.Api.Common/Docker/DockerProxy.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,14 @@ public async Task<bool> TurnOnOffDockerCompose(string name, bool turnOn, Cancell
128128
(!turnOn && output.error.Contains("Stopped", StringComparison.InvariantCultureIgnoreCase));
129129
}
130130

131-
private async Task<(string output, string error)> ExecuteCommand(string command, CancellationToken token,
131+
/// <summary>
132+
/// Executes an SSH command in the given directory.
133+
/// </summary>
134+
/// <param name="command">The command to execute.</param>
135+
/// <param name="token">The cancellation token.</param>
136+
/// <param name="dir">The directory to navigate to before executing the command.</param>
137+
/// <returns>The STDOUT and STDERR of the command's execution.</returns>
138+
private async Task<(string output, string error)> ExecuteCommand(string command, CancellationToken token = new(),
132139
string? dir = null) {
133140
using SshClient client = new(_server!, _username!, _password!);
134141
await client.ConnectAsync(token);

src/Nullinside.Api.Common/Nullinside.Api.Common.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
</PropertyGroup>
1717

1818
<ItemGroup>
19-
<PackageReference Include="log4net" Version="2.0.17" />
20-
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.1" />
19+
<PackageReference Include="log4net" Version="2.0.17"/>
20+
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.1"/>
2121
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
22-
<PackageReference Include="SSH.NET" Version="2024.1.0" />
22+
<PackageReference Include="SSH.NET" Version="2024.1.0"/>
2323
<PackageReference Include="TwitchLib.Api" Version="3.9.0"/>
2424
<PackageReference Include="TwitchLib.Client" Version="3.3.1"/>
2525
</ItemGroup>

src/Nullinside.Api.Common/SqlScripts.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static class SqlScripts {
7171
""";
7272

7373
/// <summary>
74-
/// The inner function for the <see cref="LEVENSHTEIN_DISTANCE_SEARCH" />. Not meant to be called directly.
74+
/// Removes <see cref="LEVENSHTEIN_DISTANCE_SEARCH_INNER_FUNCTION" />.
7575
/// </summary>
7676
public const string LEVENSHTEIN_DISTANCE_SEARCH_INNER_FUNCTION_REMOVE =
7777
"""
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using Nullinside.Api.Common.Twitch.Json;
2+
3+
using TwitchLib.Api.Helix.Models.Chat.GetChatters;
4+
using TwitchLib.Api.Helix.Models.Moderation.BanUser;
5+
using TwitchLib.Api.Helix.Models.Moderation.GetModerators;
6+
7+
namespace Nullinside.Api.Common.Twitch;
8+
9+
/// <summary>
10+
/// The proxy for handling communication with Twitch.
11+
/// </summary>
12+
public interface ITwitchApiProxy {
13+
/// <summary>
14+
/// The Twitch access token. These are the credentials used for all requests.
15+
/// </summary>
16+
TwitchAccessToken? OAuth { get; set; }
17+
18+
/// <summary>
19+
/// Creates a new access token from a code using Twitch's OAuth workflow.
20+
/// </summary>
21+
/// <param name="code">The code from twitch to send back to twitch to generate a new access token.</param>
22+
/// <param name="token">The cancellation token.</param>
23+
/// <remarks>The object will have its <see cref="OAuth" /> updated with the new settings for the token.</remarks>
24+
/// <returns>The OAuth details if successful, null otherwise.</returns>
25+
Task<TwitchAccessToken?> CreateAccessToken(string code, CancellationToken token = new());
26+
27+
/// <summary>
28+
/// Refreshes the access token.
29+
/// </summary>
30+
/// <param name="token">The cancellation token.</param>
31+
/// <remarks>The object will have its <see cref="OAuth" /> updated with the new settings for the token.</remarks>
32+
/// <returns>The OAuth details if successful, null otherwise.</returns>
33+
Task<TwitchAccessToken?> RefreshAccessToken(CancellationToken token = new());
34+
35+
/// <summary>
36+
/// Determines if the <see cref="OAuth" /> is valid.
37+
/// </summary>
38+
/// <param name="token">The cancellation token.</param>
39+
/// <returns>True if valid, false otherwise.</returns>
40+
Task<bool> GetAccessTokenIsValid(CancellationToken token = new());
41+
42+
/// <summary>
43+
/// Gets the twitch id and username of the owner of the <see cref="OAuth" />.
44+
/// </summary>
45+
/// <param name="token">The cancellation token.</param>
46+
/// <returns>The twitch username if successful, null otherwise.</returns>
47+
Task<(string? id, string? username)> GetUser(CancellationToken token = new());
48+
49+
/// <summary>
50+
/// Gets the email address of the owner of the <see cref="OAuth" />.
51+
/// </summary>
52+
/// <param name="token">The cancellation token.</param>
53+
/// <returns>The email address if successful, null otherwise.</returns>
54+
Task<string?> GetUserEmail(CancellationToken token = new());
55+
56+
/// <summary>
57+
/// Gets the list of channels the user moderates for.
58+
/// </summary>
59+
/// <param name="userId">The twitch id to scan.</param>
60+
/// <returns>The list of channels the user moderates for.</returns>
61+
Task<IEnumerable<TwitchModeratedChannel>> GetUserModChannels(string userId);
62+
63+
/// <summary>
64+
/// Bans a list of users from a channel.
65+
/// </summary>
66+
/// <param name="channelId">The twitch id of the channel to ban the users from.</param>
67+
/// <param name="botId">The twitch id of the bot user, the one banning the users.</param>
68+
/// <param name="users">The list of users to ban.</param>
69+
/// <param name="reason">The reason for the ban.</param>
70+
/// <param name="token">The stopping token.</param>
71+
/// <returns>The users with confirmed bans.</returns>
72+
Task<IEnumerable<BannedUser>> BanChannelUsers(string channelId, string botId,
73+
IEnumerable<(string Id, string Username)> users, string reason, CancellationToken token = new());
74+
75+
/// <summary>
76+
/// Gets the list of mods for the channel.
77+
/// </summary>
78+
/// <param name="channelId">The twitch id of the channel to get mods for.</param>
79+
/// <param name="token">The cancellation token.</param>
80+
/// <returns>The collection of moderators.</returns>
81+
Task<IEnumerable<Moderator>> GetChannelMods(string channelId, CancellationToken token = new());
82+
83+
/// <summary>
84+
/// Gets the chatters currently in a channel.
85+
/// </summary>
86+
/// <param name="channelId">The twitch id of the channel that we are moderating.</param>
87+
/// <param name="botId">The twitch id of the bot.</param>
88+
/// <param name="token">The cancellation token.</param>
89+
/// <returns>The collection of chatters.</returns>
90+
Task<IEnumerable<Chatter>> GetChannelUsers(string channelId, string botId, CancellationToken token = new());
91+
92+
/// <summary>
93+
/// Checks if the supplied channels are live.
94+
/// </summary>
95+
/// <param name="userIds">The twitch ids of the channels.</param>
96+
/// <returns>The list of twitch channels that are currently live.</returns>
97+
Task<IEnumerable<string>> GetChannelsLive(IEnumerable<string> userIds);
98+
99+
/// <summary>
100+
/// Makes a user a moderator in a channel.
101+
/// </summary>
102+
/// <param name="channelId">The twitch id of the channel to add the mod to.</param>
103+
/// <param name="userId">The twitch id to give the moderator role.</param>
104+
/// <param name="token">The cancellation token.</param>
105+
/// <returns>True if successful, false otherwise.</returns>
106+
Task<bool> AddChannelMod(string channelId, string userId, CancellationToken token = new());
107+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace Nullinside.Api.Common.Twitch;
2+
3+
/// <summary>
4+
/// Represents an OAuth token in the Twitch workflow.
5+
/// </summary>
6+
public class TwitchAccessToken {
7+
/// <summary>
8+
/// The Twitch access token.
9+
/// </summary>
10+
public string? AccessToken { get; set; }
11+
12+
/// <summary>
13+
/// The refresh token.
14+
/// </summary>
15+
public string? RefreshToken { get; set; }
16+
17+
/// <summary>
18+
/// The UTC <see cref="DateTime" /> when the <see cref="AccessToken" /> expires.
19+
/// </summary>
20+
public DateTime? ExpiresUtc { get; set; }
21+
}

0 commit comments

Comments
 (0)