11using System ;
2+ using System . Globalization ;
23using System . Net ;
34using System . Net . Http ;
45using System . Net . NetworkInformation ;
@@ -25,6 +26,7 @@ public class MainWindowViewModel : ViewModelBase {
2526 private bool _serverUp ;
2627 private bool _websiteUp ;
2728 private WindowState _windowState ;
29+ private string ? _chatTimestamp ;
2830
2931 /// <summary>
3032 /// Initializes a new instance of the <see cref="MainWindowViewModel" /> class.
@@ -51,6 +53,14 @@ public string? ServerAddress {
5153 }
5254 }
5355
56+ /// <summary>
57+ /// The timestamp of the last chat message that was received.
58+ /// </summary>
59+ public string ? ChatTimestamp {
60+ get => _chatTimestamp ;
61+ set => this . RaiseAndSetIfChanged ( ref _chatTimestamp , value ) ;
62+ }
63+
5464 /// <summary>
5565 /// True if the server is online.
5666 /// </summary>
@@ -110,7 +120,20 @@ private async Task PingSite() {
110120 WebsiteUp = await SendHeadRequest ( "https://nullinside.com" ) ;
111121 ApiUp = await SendHeadRequest ( "https://nullinside.com/api/v1/featureToggle" ) ;
112122 NullUp = await SendHeadRequest ( "https://nullinside.com/null/v1/database/migration" ) ;
113- if ( ( ! WebsiteUp || ! ApiUp || ! NullUp ) && IsMinimized ) {
123+ ChatTimestamp = await SendGetRequest ( "https://nullinside.com/twitch-bot/v1/bot/chat/timestamp" ) ;
124+ var chatNotUpdating = false ;
125+ if ( null != ChatTimestamp ) {
126+ var parsed = ChatTimestamp . Trim ( '"' ) ;
127+ if ( DateTime . TryParse ( parsed , out DateTime time ) ) {
128+ var timestamp = time . ToLocalTime ( ) . ToString ( CultureInfo . InvariantCulture ) ;
129+ var diff = DateTime . Now - time . ToLocalTime ( ) ;
130+ timestamp = $ "{ timestamp } ({ diff . Hours } h { diff . Minutes } m { diff . Seconds } s ago)";
131+ ChatTimestamp = timestamp ;
132+ chatNotUpdating = diff > TimeSpan . FromMinutes ( 5 ) ;
133+ }
134+ }
135+
136+ if ( ( ! WebsiteUp || ! ApiUp || ! NullUp || chatNotUpdating ) && IsMinimized ) {
114137 WindowState = WindowState . Normal ;
115138 }
116139
@@ -121,8 +144,8 @@ private async Task PingSite() {
121144 /// <summary>
122145 /// Sends a head request to ensure a URL is online.
123146 /// </summary>
124- /// <param name="address"></param>
125- /// <returns></returns>
147+ /// <param name="address">The address to send the request to. </param>
148+ /// <returns>True if successful, false otherwise. </returns>
126149 private async Task < bool > SendHeadRequest ( string address ) {
127150 try {
128151 var handler = new HttpClientHandler ( ) ;
@@ -137,6 +160,26 @@ private async Task<bool> SendHeadRequest(string address) {
137160 return false ;
138161 }
139162 }
163+
164+ /// <summary>
165+ /// Sends a head request to ensure a URL is online.
166+ /// </summary>
167+ /// <param name="address">The address to send the request to.</param>
168+ /// <returns>The content of the response.</returns>
169+ private async Task < string ? > SendGetRequest ( string address ) {
170+ try {
171+ var handler = new HttpClientHandler ( ) ;
172+ handler . AutomaticDecompression = ~ DecompressionMethods . None ;
173+ using var httpClient = new HttpClient ( handler ) ;
174+ using var request = new HttpRequestMessage ( HttpMethod . Get , address ) ;
175+ request . Headers . TryAddWithoutValidation ( "user-agent" , Constants . FAKE_USER_AGENT ) ;
176+ HttpResponseMessage response = await httpClient . SendAsync ( request ) ;
177+ return await response . Content . ReadAsStringAsync ( ) ;
178+ }
179+ catch {
180+ return null ;
181+ }
182+ }
140183
141184 private async Task PingServer ( ) {
142185 while ( true ) {
0 commit comments