1
1
using System . Text ;
2
+ using Microsoft . Extensions . Options ;
2
3
using NTorSpectator . Services ;
3
4
4
5
namespace NTorSpectator . Mastodon ;
@@ -10,11 +11,14 @@ public class Reporter : IReporter
10
11
{
11
12
private readonly ILogger < Reporter > _logger ;
12
13
private readonly IMastodonClient _mastodonClient ;
14
+ private readonly int _messageLimit ;
15
+ private const string TRUNCATED_TAIL = "\n [TRUNCATED]" ;
13
16
14
- public Reporter ( ILogger < Reporter > logger , IMastodonClient mastodonClient )
17
+ public Reporter ( ILogger < Reporter > logger , IMastodonClient mastodonClient , IOptions < MastodonSettings > mastodonSettings )
15
18
{
16
19
_logger = logger ;
17
20
_mastodonClient = mastodonClient ;
21
+ _messageLimit = mastodonSettings . Value . MessageLimit ;
18
22
}
19
23
20
24
public async Task PublishReport ( IReadOnlyCollection < TorWatchResults > watchResults )
@@ -32,12 +36,20 @@ public async Task PublishReport(IReadOnlyCollection<TorWatchResults> watchResult
32
36
. AppendLine ( )
33
37
. AppendFormat ( " \u274c Down: {0}" , watchResults . Count ( x => ! x . IsOk ) ) ;
34
38
35
- sb . AppendLine ( ) . AppendLine ( ) ;
39
+ sb . AppendLine ( ) . AppendLine ( ) . AppendLine ( "Failed sites:" ) ;
36
40
foreach ( var failResult in watchResults . Where ( x => ! x . IsOk ) )
37
41
{
38
- sb . AppendFormat ( " - \U0001F4A5 {0} not found " , failResult . Site ) . AppendLine ( ) ;
42
+ sb . AppendFormat ( "\U0001F4A5 http:// {0} " , failResult . Site ) . AppendLine ( ) ;
39
43
}
40
44
}
45
+
46
+ if ( sb . Length > _messageLimit )
47
+ {
48
+ var tailBegin = _messageLimit - TRUNCATED_TAIL . Length ;
49
+ var tailLength = sb . Length - tailBegin ;
50
+ sb . Remove ( tailBegin , tailLength ) . Append ( TRUNCATED_TAIL ) ;
51
+ }
52
+
41
53
await _mastodonClient . Toot ( new ( sb . ToString ( ) ) ) ;
42
54
_logger . LogInformation ( "Posted a new status" ) ;
43
55
}
0 commit comments