1
1
using System ;
2
+ using System . Globalization ;
2
3
using System . Net ;
3
4
using System . Net . Http ;
4
5
using System . Net . NetworkInformation ;
@@ -25,6 +26,7 @@ public class MainWindowViewModel : ViewModelBase {
25
26
private bool _serverUp ;
26
27
private bool _websiteUp ;
27
28
private WindowState _windowState ;
29
+ private string ? _chatTimestamp ;
28
30
29
31
/// <summary>
30
32
/// Initializes a new instance of the <see cref="MainWindowViewModel" /> class.
@@ -51,6 +53,14 @@ public string? ServerAddress {
51
53
}
52
54
}
53
55
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
+
54
64
/// <summary>
55
65
/// True if the server is online.
56
66
/// </summary>
@@ -110,7 +120,20 @@ private async Task PingSite() {
110
120
WebsiteUp = await SendHeadRequest ( "https://nullinside.com" ) ;
111
121
ApiUp = await SendHeadRequest ( "https://nullinside.com/api/v1/featureToggle" ) ;
112
122
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 ) {
114
137
WindowState = WindowState . Normal ;
115
138
}
116
139
@@ -121,8 +144,8 @@ private async Task PingSite() {
121
144
/// <summary>
122
145
/// Sends a head request to ensure a URL is online.
123
146
/// </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>
126
149
private async Task < bool > SendHeadRequest ( string address ) {
127
150
try {
128
151
var handler = new HttpClientHandler ( ) ;
@@ -137,6 +160,26 @@ private async Task<bool> SendHeadRequest(string address) {
137
160
return false ;
138
161
}
139
162
}
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
+ }
140
183
141
184
private async Task PingServer ( ) {
142
185
while ( true ) {
0 commit comments