Skip to content

Commit 53370df

Browse files
committed
Respect maximum toot length on the instance
+semver: fix
1 parent c50b88e commit 53370df

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

NTorSpectator/Mastodon/MastodonSettings.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,9 @@ public class MastodonSettings
1414
/// API access token to use
1515
/// </summary>
1616
public required string Token { get; init; }
17+
18+
/// <summary>
19+
/// Maximum message length
20+
/// </summary>
21+
public required int MessageLimit { get; init; } = 500;
1722
}

NTorSpectator/Mastodon/Reporter.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Text;
2+
using Microsoft.Extensions.Options;
23
using NTorSpectator.Services;
34

45
namespace NTorSpectator.Mastodon;
@@ -10,11 +11,14 @@ public class Reporter : IReporter
1011
{
1112
private readonly ILogger<Reporter> _logger;
1213
private readonly IMastodonClient _mastodonClient;
14+
private readonly int _messageLimit;
15+
private const string TRUNCATED_TAIL = "\n[TRUNCATED]";
1316

14-
public Reporter(ILogger<Reporter> logger, IMastodonClient mastodonClient)
17+
public Reporter(ILogger<Reporter> logger, IMastodonClient mastodonClient, IOptions<MastodonSettings> mastodonSettings)
1518
{
1619
_logger = logger;
1720
_mastodonClient = mastodonClient;
21+
_messageLimit = mastodonSettings.Value.MessageLimit;
1822
}
1923

2024
public async Task PublishReport(IReadOnlyCollection<TorWatchResults> watchResults)
@@ -32,12 +36,20 @@ public async Task PublishReport(IReadOnlyCollection<TorWatchResults> watchResult
3236
.AppendLine()
3337
.AppendFormat(" \u274c Down: {0}", watchResults.Count(x => !x.IsOk));
3438

35-
sb.AppendLine().AppendLine();
39+
sb.AppendLine().AppendLine().AppendLine("Failed sites:");
3640
foreach (var failResult in watchResults.Where(x => !x.IsOk))
3741
{
38-
sb.AppendFormat(" -\U0001F4A5 {0} not found", failResult.Site).AppendLine();
42+
sb.AppendFormat("\U0001F4A5 http://{0} ", failResult.Site).AppendLine();
3943
}
4044
}
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+
4153
await _mastodonClient.Toot(new(sb.ToString()));
4254
_logger.LogInformation("Posted a new status");
4355
}

0 commit comments

Comments
 (0)