Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions src/HuaJiBot.NET.Plugin.GitHubBridge/EventDispatch/GitHubPosts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ namespace HuaJiBot.NET.Plugin.GitHubBridge.EventDispatch;

internal static class GitHubPosts
{
internal const int ExcerptLimit = 280;

internal static Post Issue(
IssuesEventBody body,
Func<Task<string>> card,
Expand Down Expand Up @@ -51,23 +49,6 @@ Func<Task<SendingMessageBase[]>> fallback
Links = [new("查看改动", body.Compare.ToString())],
};

internal static string? Excerpt(string? body)
{
if (body is null)
return null;
var text = body.ReplaceLineEndings("\n").Trim();
var lines = text.Split('\n');
if (lines.Length <= 6 && text.Length <= ExcerptLimit)
return text;
var head = string.Join('\n', lines.Take(3));
if (lines.Length > 3 && head.Length < ExcerptLimit)
return head + '…';
var end = ExcerptLimit - 1;
if (char.IsLowSurrogate(head[end]))
end--;
return head[..end] + '…';
}

private static string Verb(string action) =>
action switch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ IssueCommentEventBody body
if (!ShouldBroadcast(body))
return;
using Cards cards = new();
var card = cards.Card(() => BuildCommentCardAsync(body));
var post = GitHubPosts.Comment(
body,
cards.Card(() => BuildCommentCardAsync(body, GitHubPosts.Excerpt(body.Comment.Body))),
card,
Cards.Once<SendingMessageBase[]>(async () =>
[
new ImageMessage(await cards.Card(() => BuildCommentCardAsync(body, body.Comment.Body))()),
new ImageMessage(await card()),
new LinkMessage("View comment", await plugin.OrRawAsync(body.Issue.HtmlUrl)),
]
)
Expand All @@ -51,12 +52,13 @@ public static async Task DispatchIssuesEventAsync(this PluginMain plugin, Issues
if (!ShouldBroadcast(body))
return;
using Cards cards = new();
var card = cards.Card(() => BuildIssueCardAsync(body));
var post = GitHubPosts.Issue(
body,
cards.Card(() => BuildIssueCardAsync(body, GitHubPosts.Excerpt(body.Issue.Body))),
card,
Cards.Once<SendingMessageBase[]>(async () =>
[
new ImageMessage(await cards.Card(() => BuildIssueCardAsync(body, body.Issue.Body))()),
new ImageMessage(await card()),
new LinkMessage(
$"Open #{body.Issue.Number}",
await plugin.OrRawAsync(body.Issue.HtmlUrl)
Expand All @@ -71,29 +73,23 @@ await Broadcast.SendAsync(
);
}

private static Task<TempFile.AutoDeleteFile> BuildCommentCardAsync(
IssueCommentEventBody body,
string? content
) =>
private static Task<TempFile.AutoDeleteFile> BuildCommentCardAsync(IssueCommentEventBody body) =>
BuildCardAsync(
body.Repository,
body.Issue,
body.Sender,
body.Issue.State,
content,
body.Comment.Body,
$"@{body.Sender.Login} {body.Action} comment."
);

private static Task<TempFile.AutoDeleteFile> BuildIssueCardAsync(
IssuesEventBody body,
string? content
) =>
private static Task<TempFile.AutoDeleteFile> BuildIssueCardAsync(IssuesEventBody body) =>
BuildCardAsync(
body.Repository,
body.Issue,
body.Sender,
body.Action is "opened" or "reopened" ? "open" : body.Action,
content,
body.Issue.Body,
$"@{body.Sender.Login} {body.Action} issue"
);

Expand Down
19 changes: 0 additions & 19 deletions src/HuaJiBot.NET.UnitTest/GitHubPostTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,4 @@ public void Push_CountsCommitsOnBranch()
Assert.That(post.Links.Single(), Is.EqualTo(new LinkMessage("查看改动", "https://github.com/nbtca/HuaJiBot.NET/compare/a...b")));
});
}

[Test]
public void Excerpt_KeepsShortBody() =>
Assert.That(GitHubPosts.Excerpt("one\ntwo"), Is.EqualTo("one\ntwo"));

[Test]
public void Excerpt_CutsManyLinesToThree() =>
Assert.That(GitHubPosts.Excerpt("1\n2\n3\n4\n5\n6\n7"), Is.EqualTo("1\n2\n3…"));

[Test]
public void Excerpt_CutsLongParagraph()
{
var excerpt = GitHubPosts.Excerpt(new string('a', 500))!;
Assert.Multiple(() =>
{
Assert.That(excerpt, Has.Length.EqualTo(GitHubPosts.ExcerptLimit));
Assert.That(excerpt, Does.EndWith("…"));
});
}
}
Loading