Exemple #1
0
            public async void OnNewFollowersDetected_Raised_When_NewFollower()
            {
                var usersFollowsResponseFirstUserJson = JMock.Of <GetUsersFollowsResponse>(o =>
                                                                                           o.Follows == new[]
                {
                    Mock.Of <Follow>(f => f.FromUserId == "FromFirstUserId")
                }
                                                                                           );

                var mockHandler = TwitchLibMock.HttpCallHandler(("https://api.twitch.tv/helix/users/follows", usersFollowsResponseFirstUserJson));

                _api = TwitchLibMock.TwitchApi(mockHandler);

                var eventExecuteCount = 0;

                _followerService = new FollowerService(_api);
                _followerService.SetChannelsById(Utils.CreateListWithEmptyString());
                _followerService.OnNewFollowersDetected += (sender, e) => eventExecuteCount++;

                await _followerService.UpdateLatestFollowersAsync();

                var usersFollowsResponseSecondUserJson = JMock.Of <GetUsersFollowsResponse>(o =>
                                                                                            o.Follows == new[]
                {
                    Mock.Of <Follow>(f => f.FromUserId == "FromSecondUserId")
                }
                                                                                            );

                TwitchLibMock.ResetHttpCallHandlerResponses(mockHandler, ("https://api.twitch.tv/helix/users/follows", usersFollowsResponseSecondUserJson));

                await _followerService.UpdateLatestFollowersAsync();

                Assert.Equal(2, eventExecuteCount);
            }
Exemple #2
0
            public async void OnNewFollowersDetected_NotRaised_When_NoNewFollowers()
            {
                var usersFollowsResponseJson = JMock.Of <GetUsersFollowsResponse>(o =>
                                                                                  o.Follows == new[]
                {
                    Mock.Of <Follow>(f => f.FromUserId == "FromUserId")
                }
                                                                                  );

                _api = TwitchLibMock.TwitchApi(
                    ("https://api.twitch.tv/helix/users/follows", usersFollowsResponseJson)
                    );

                var eventExcecutCount = 0;

                _followerService = new FollowerService(_api);
                _followerService.SetChannelsById(Utils.CreateListWithEmptyString());
                _followerService.OnNewFollowersDetected += (sender, e) => eventExcecutCount++;

                await _followerService.UpdateLatestFollowersAsync();

                await _followerService.UpdateLatestFollowersAsync();

                Assert.Equal(1, eventExcecutCount);
            }
Exemple #3
0
            public async void OnNewFollowersDetected_NotRaised_When_NewFollowerOlderThanLatest()
            {
                var usersFollowsResponseFirstUserJson = JMock.Of <GetUsersFollowsResponse>(o =>
                                                                                           o.Follows == new[]
                {
                    Mock.Of <Follow>(f => f.FromUserId == "FromFirstUserId" && f.FollowedAt == new DateTime(1))
                }
                                                                                           );

                var mockHandler = new Mock <IHttpCallHandler>();

                mockHandler
                .Setup(x => x.GeneralRequest(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Core.Enums.ApiVersion>(), It.IsAny <string>(), It.IsAny <string>()))
                .Returns(new KeyValuePair <int, string>(200, usersFollowsResponseFirstUserJson));

                _api = TwitchLibMock.TwitchApi(mockHandler);

                var eventExecuteCount = 0;

                _followerService = new FollowerService(_api);
                _followerService.SetChannelsById(Utils.CreateListWithEmptyString());
                _followerService.OnNewFollowersDetected += (sender, e) => eventExecuteCount++;

                await _followerService.UpdateLatestFollowersAsync();

                var usersFollowsResponseSecondUserJson = JMock.Of <GetUsersFollowsResponse>(o =>
                                                                                            o.Follows == new[]
                {
                    Mock.Of <Follow>(f => f.FromUserId == "FromSecondUserId" && f.FollowedAt == new DateTime(0))
                }
                                                                                            );

                mockHandler.Reset();

                mockHandler
                .Setup(x => x.GeneralRequest(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Core.Enums.ApiVersion>(), It.IsAny <string>(), It.IsAny <string>()))
                .Returns(new KeyValuePair <int, string>(200, usersFollowsResponseSecondUserJson));

                await _followerService.UpdateLatestFollowersAsync();

                Assert.Equal(1, eventExecuteCount);
            }
Exemple #4
0
            public async void KnownFollowers_Contains_UserId_When_ServiceUpdated()
            {
                var usersFollowsResponseJson = JMock.Of <GetUsersFollowsResponse>(o =>
                                                                                  o.Follows == new[]
                {
                    Mock.Of <Follow>(u => u.FromUserId == "UserId" && u.ToUserId == "Id")
                }
                                                                                  );

                _api = TwitchLibMock.TwitchApi(
                    ("https://api.twitch.tv/helix/users/follows", usersFollowsResponseJson)
                    );

                _followerService = new FollowerService(_api);
                _followerService.SetChannelsById(Utils.CreateListWithEmptyString());

                await _followerService.UpdateLatestFollowersAsync();

                Assert.NotNull(_followerService.KnownFollowers[string.Empty].FirstOrDefault(f => f.FromUserId == "UserId"));

                //Same check for SetChannelsByName
                var usersResponseJson = JMock.Of <GetUsersResponse>(o =>
                                                                    o.Users == new[]
                {
                    Mock.Of <User>(u => u.Id == "Id" && u.DisplayName == "DisplayName"),
                });

                _api = TwitchLibMock.TwitchApi(
                    ("https://api.twitch.tv/helix/users", usersResponseJson),
                    ("https://api.twitch.tv/helix/users/follows", usersFollowsResponseJson)
                    );

                _followerService = new FollowerService(_api);
                _followerService.SetChannelsByName(Utils.CreateListWithEmptyString());

                await _followerService.UpdateLatestFollowersAsync();

                Assert.NotNull(_followerService.KnownFollowers[string.Empty].FirstOrDefault(f => f.FromUserId == "UserId"));
            }
Exemple #5
0
            public async void OnNewFollowersDetected_Raised_When_LatestFollowersUpdated()
            {
                var usersFollowsResponseJson = JMock.Of <GetUsersFollowsResponse>(o =>
                                                                                  o.Follows == new[]
                {
                    Mock.Of <Follow>()
                }
                                                                                  );

                _api = TwitchLibMock.TwitchApi(
                    ("https://api.twitch.tv/helix/users/follows", usersFollowsResponseJson)
                    );

                var eventExcecuted = false;

                _followerService = new FollowerService(_api);
                _followerService.SetChannelsById(Utils.CreateListWithEmptyString());
                _followerService.OnNewFollowersDetected += (sender, e) => eventExcecuted = true;

                await _followerService.UpdateLatestFollowersAsync();

                Assert.True(eventExcecuted);
            }
        public void ConnectToTwitch()
        {
            try
            {
                if (string.IsNullOrWhiteSpace(Settings.Username))
                {
                    ShowMessage("Twitch Benutzername ist in den Einstellungen nicht gesetzt", new NotificationSetting(false, true));
                    return;
                }

                if (string.IsNullOrWhiteSpace(Settings.OAuthToken))
                {
                    ShowMessage("Twitch O Auth Token ist in den Einstellungen nicht gesetzt", new NotificationSetting(false, true));
                    return;
                }

                if (Settings.CheckForNewFollowers)
                {
                    if (string.IsNullOrWhiteSpace(Settings.ClientId))
                    {
                        ShowMessage("Twitch Api Client ID ist in den Einstellungen nicht gesetzt", new NotificationSetting(false, true));
                        return;
                    }

                    if (string.IsNullOrWhiteSpace(Settings.AccessToken))
                    {
                        ShowMessage("Twitch Api Access Token ist in den Einstellungen nicht gesetzt", new NotificationSetting(false, true));
                        return;
                    }
                }

                var loggerfactory = new LoggerFactory()
                                    .AddSerilog(
                    new LoggerConfiguration()
                    .WriteTo.File("logs\\twitch.log")
                    .CreateLogger());

                twitchAPI = new TwitchAPI(loggerfactory);
                twitchAPI.Settings.ClientId    = Settings.ClientId;
                twitchAPI.Settings.AccessToken = Settings.AccessToken;
                twitchAPI.Settings.Secret      = Settings.OAuthToken;
                twitchClient                     = new TwitchClient(logger: loggerfactory.CreateLogger <TwitchClient>());
                twitchClient.OnConnected        += TwitchClient_OnConnected;
                twitchClient.OnNewSubscriber    += TwitchClient_OnNewSubscriber;
                twitchClient.OnRaidNotification += TwitchClient_OnRaidNotification;
                twitchClient.OnUserJoined       += TwitchClient_OnUserJoined;
                twitchClient.OnUserLeft         += TwitchClient_OnUserLeft;
                twitchClient.OnBeingHosted      += TwitchClient_OnBeingHosted;
                twitchClient.OnMessageReceived  += TwitchClient_OnMessageReceived;

                twitchClient.Initialize(new ConnectionCredentials(Settings.Username, Settings.OAuthToken), Settings.ChannelToJoin);
                twitchClient.Connect();

                if (Settings.CheckForNewFollowers)
                {
                    followerService = new FollowerService(twitchAPI, checkIntervalInSeconds: 5);
                    followerService.SetChannelsByName(new List <string>()
                    {
                        Settings.ChannelToJoin
                    });
                    followerService.OnNewFollowersDetected += FollowerService_OnNewFollowersDetected;
                    followerService.UpdateLatestFollowersAsync(false);
                    followerService.Start();
                }

                IsTwitchConnected = true;
            }
            catch (Exception ex)
            {
                mainViewModel.ShowError(ex);
            }
        }