Skip to content

Commit f55c8f8

Browse files
committed
Final adjustments post-reorg
- Twitch/Spotify tokens now form correct callback redirect URI based on server's host domain and port (if port is standard HTTPS port 443 then it is omitted) - EventSubClient will no longer throw when subscription fails - This needs to be further expanded to differentiate between different reasons why it fails - Minor fixes and unnecessary hack removals
1 parent cc2afea commit f55c8f8

10 files changed

Lines changed: 44 additions & 19 deletions

File tree

LukeBot.API/SpotifyToken.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using LukeBot.Common;
2-
using LukeBot.Config;
32

43

54
namespace LukeBot.API
@@ -14,7 +13,7 @@ public SpotifyToken(AuthFlow flow, string lbUser)
1413
"https://accounts.spotify.com/authorize",
1514
"https://accounts.spotify.com/api/token",
1615
"https://accounts.spotify.com/api/revoke",
17-
"http://" + Conf.Get<string>(Common.Constants.PROP_STORE_HTTPS_DOMAIN_PROP) + "/callback/spotify"
16+
"https://" + Utils.GetCallbackDomainAndPort() + "/callback/spotify"
1817
)
1918
{
2019
}

LukeBot.API/Token.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,6 @@ private void ExportToConfig()
6262

6363
public Token(string service, string lbUser, AuthFlow flow, string authURL, string refreshURL, string revokeURL, string callbackURL)
6464
{
65-
if (callbackURL.StartsWith("https://localhost"))
66-
{
67-
// HACK - localhost doesn't require https and it might break some logins sometimes
68-
callbackURL = callbackURL.Replace("https://localhost", "http://localhost");
69-
}
70-
7165
switch (flow)
7266
{
7367
case AuthFlow.AuthorizationCode:

LukeBot.API/TwitchToken.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using LukeBot.Common;
2-
using LukeBot.Config;
32

43

54
namespace LukeBot.API
@@ -14,7 +13,7 @@ public TwitchToken(AuthFlow flow, string lbUser)
1413
"https://id.twitch.tv/oauth2/authorize",
1514
"https://id.twitch.tv/oauth2/token",
1615
"https://id.twitch.tv/oauth2/revoke",
17-
"https://" + Conf.Get<string>(Common.Constants.PROP_STORE_HTTPS_DOMAIN_PROP) + "/callback/twitch"
16+
"https://" + Utils.GetCallbackDomainAndPort() + "/callback/twitch"
1817
)
1918
{
2019
}

LukeBot.API/Utils.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using LukeBot.Common;
3+
using LukeBot.Config;
4+
5+
6+
namespace LukeBot.API
7+
{
8+
internal class Utils
9+
{
10+
public static string GetCallbackDomainAndPort()
11+
{
12+
string domain = Conf.Get<string>(Constants.PROP_STORE_HTTPS_DOMAIN_PROP);
13+
14+
if (Conf.TryGet<int>(Constants.PROP_STORE_SERVER_PORT_PROP, out int port))
15+
{
16+
// assumes we use HTTPS always, so this "update" to returned domain
17+
// should only be done if the port is not standard HTTPS 443 port
18+
if (port != 443)
19+
{
20+
domain += String.Format(":{0}", port);
21+
}
22+
}
23+
24+
return domain;
25+
}
26+
}
27+
}

LukeBot.Twitch.Impl/EventSubClient.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ private void ProcessSubscriptionQueue()
510510

511511
if (mSubscriptions.ContainsKey(sub))
512512
{
513-
Logger.Log().Warning("{0}: Already subscribed to {1}, skipping", mLBUser, sub);
513+
Logger.Log().Warning("EventSubClient {0}: Already subscribed to {1}, skipping", mLBUser, sub);
514514
continue;
515515
}
516516

@@ -524,11 +524,13 @@ private void ProcessSubscriptionQueue()
524524

525525
if (!resp.IsSuccess)
526526
{
527-
throw new EventSubSubscriptionFailedException(sub, resp.code, resp.responseData.message);
527+
Logger.Log().Error("EventSubClient {0}: Failed to subscribe to {1}: {2} ({3}).", mLBUser, sub, resp.code, resp.responseData.message);
528+
Logger.Log().Error("EventSubClient {0}: Subscription will be skipped until next EventSubClient reconnect", mLBUser);
529+
continue;
528530
}
529531

530532
mSubscriptions.Add(resp.data[0].id, resp.data[0]);
531-
Logger.Log().Debug("EventSubClient {0}: Subscribed to {1}", mLBUser, sub);
533+
Logger.Log().Info("EventSubClient {0}: Subscribed to {1}", mLBUser, sub);
532534
}
533535
}
534536
}

LukeBot.Twitch.Impl/TwitchService.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class TwitchService: ITwitchService, IUserModuleFactory
2121
private TwitchIRC mIRC;
2222
private API.Twitch.GetUserResponse mBotData;
2323
private Dictionary<string, TwitchUserModule> mUserModules = new();
24+
private List<string> mJoinedTwitchChannels = new();
2425

2526

2627
// Config interactions //
@@ -56,7 +57,8 @@ private TwitchUserModule JoinChannel(string lbUser)
5657
.Push(CommonConstants.PROP_STORE_LOGIN_PROP)
5758
);
5859

59-
if (mUserModules.ContainsKey(lbUser))
60+
if (mUserModules.ContainsKey(lbUser) ||
61+
mJoinedTwitchChannels.Exists((ch) => ch == channel))
6062
{
6163
throw new ChannelAlreadyJoinedException(lbUser);
6264
}
@@ -82,6 +84,7 @@ private TwitchUserModule JoinChannel(string lbUser)
8284
throw;
8385
}
8486

87+
mJoinedTwitchChannels.Add(channel);
8588
Logger.Log().Secure("Joined channel twitch ID: {0}", module.GetUserData().id);
8689
return module;
8790
}
@@ -92,6 +95,8 @@ private void PartChannel(string lbUser)
9295
{
9396
Logger.Log().Debug("Parting channel {0} for user {1}", module.GetChannelName(), lbUser);
9497

98+
mJoinedTwitchChannels.Remove(module.GetChannelName());
99+
95100
try
96101
{
97102
module.Dispose();

LukeBot.Twitch.Impl/TwitchUserModule.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,15 +308,15 @@ public void WaitForShutdown()
308308
mEventSub = null;
309309
}
310310

311-
ServiceUtils.GetEventService().User(mLBUser).RemoveEventDispatcher(Constants.QueuedDispatcherForUser(mLBUser));
312-
313311
if (mIRCChannel != null)
314312
{
315313
mIRC.PartChannel(mIRCChannel);
316314

317315
mIRCChannel = null;
318316
mIRC = null;
319317
}
318+
319+
ServiceUtils.GetEventService().User(mLBUser).RemoveEventDispatcher(Constants.QueuedDispatcherForUser(mLBUser));
320320
}
321321

322322
public string GetModuleType()

LukeBot/SpotifyCLIProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private void CheckForLogin(CLIMessageProxy CLI)
4848

4949
if (!Conf.TryGet<string>(path, out string login))
5050
{
51-
login = CLI.Query(false, "Spotify login for user " + CLI.GetCurrentUser());
51+
login = CLI.Query(false, "Spotify login for user " + CLI.GetCurrentUser().GetUsername());
5252
if (login.Length == 0)
5353
{
5454
throw new ArgumentException("No login provided");

LukeBot/TwitchCLIProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private void CheckForLogin(CLIMessageProxy CLI)
6767

6868
if (!Conf.TryGet<string>(path, out string login))
6969
{
70-
login = CLI.Query(false, "Twitch login for user " + CLI.GetCurrentUser());
70+
login = CLI.Query(false, "Twitch login for user " + CLI.GetCurrentUser().GetUsername());
7171
if (login.Length == 0)
7272
{
7373
throw new ArgumentException("No login provided");

LukeBot/UserCLIProcessor.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ private void HandleUpdateUserCommand(UserUpdateCommand args, out string msg)
218218
mCLI.Message("Permission level set to " + args.PermissionLevel.ToString());
219219

220220
msg = "Changes to user " + user.GetUsername() + " applied.";
221-
msg = "TODO";
222221
}
223222
catch (System.Exception e)
224223
{

0 commit comments

Comments
 (0)