Skip to content

Commit

Permalink
feat: use markdown for URLs in Discord publications (#1684)
Browse files Browse the repository at this point in the history
  • Loading branch information
qixils authored Sep 4, 2023
1 parent 5f58cbb commit 49a27db
Show file tree
Hide file tree
Showing 32 changed files with 107 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,17 @@ private class DiscordMessage
public DiscordMessage(IPostable post)
{
var body = string.IsNullOrWhiteSpace(post.Body) ? "" : $" ({post.Body})";
var link = string.IsNullOrWhiteSpace(post.Link) ? "" : post.Type == PostType.Announcement ? $" {post.Link}" : $" <{post.Link}>";
Content = $"{post.Title}{body}{link}";
if (string.IsNullOrWhiteSpace(post.Link))
{
Content = $"{post.Title}{body}";
}
else
{
var link = post.Type == PostType.Announcement ? post.Link : $"<{post.Link}>";
Content = string.IsNullOrWhiteSpace(post.FormattedTitle)
? $"{post.Title}{body} {link}"
: $"{string.Format(post.FormattedTitle, link)}{body}";
}
}

[JsonPropertyName("content")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public string ToAbsolute(string relativeLink)

public static class ExternalMediaPublisherExtensions
{
public static async Task SendUserFile(this ExternalMediaPublisher publisher, bool unlisted, string title, string relativeLink, string body)
public static async Task SendUserFile(this ExternalMediaPublisher publisher, bool unlisted, string title, string formattedTitle, string relativeLink, string body)
{
await publisher.Send(new Post
{
Expand All @@ -51,6 +51,7 @@ await publisher.Send(new Post
: PostType.General,
Group = PostGroups.UserFiles,
Title = title,
FormattedTitle = formattedTitle,
Body = body,
Link = publisher.ToAbsolute(relativeLink)
});
Expand All @@ -64,19 +65,21 @@ await publisher.Send(new Post
Type = PostType.Announcement,
Group = PostGroups.Submission,
Title = $"New Submission! Go and see {title}",
FormattedTitle = $"New Submission! Go and see [{title}]({{0}})",
Body = "",
Link = publisher.ToAbsolute(relativeLink)
});
}

public static async Task SendSubmissionEdit(this ExternalMediaPublisher publisher, string title, string body, string relativeLink)
public static async Task SendSubmissionEdit(this ExternalMediaPublisher publisher, string title, string formattedTitle, string body, string relativeLink)
{
await publisher.Send(new Post
{
Announcement = "",
Type = PostType.General,
Group = PostGroups.Submission,
Title = title,
FormattedTitle = formattedTitle,
Body = body,
Link = publisher.ToAbsolute(relativeLink)
});
Expand All @@ -90,6 +93,7 @@ await publisher.Send(new Post
Type = PostType.Announcement,
Group = PostGroups.Submission,
Title = $"New movie published! Go and see {title}",
FormattedTitle = $"New movie published! Go and see [{title}]({{0}})",
Body = "",
Link = publisher.ToAbsolute(relativeLink)
});
Expand All @@ -103,6 +107,7 @@ await publisher.Send(new Post
Type = PostType.Announcement,
Group = PostGroups.Publication,
Title = "Publication Removed",
FormattedTitle = "[Publication]({0}) Removed",
Body = reason,
Link = publisher.ToAbsolute($"{id}M")
});
Expand All @@ -116,37 +121,40 @@ await publisher.Send(new Post
Type = PostType.Announcement,
Group = PostGroups.Submission,
Title = "Submission Removed",
FormattedTitle = "[Submission]({0}) Removed",
Link = publisher.ToAbsolute($"{id}S")
});
}

public static async Task SendPublicationEdit(this ExternalMediaPublisher publisher, string title, string body, string relativeLink)
public static async Task SendPublicationEdit(this ExternalMediaPublisher publisher, string title, string formattedTitle, string body, string relativeLink)
{
await publisher.Send(new Post
{
Announcement = "",
Type = PostType.General,
Group = PostGroups.Publication,
Title = title,
FormattedTitle = formattedTitle,
Body = body,
Link = publisher.ToAbsolute(relativeLink)
});
}

public static async Task AnnounceForum(this ExternalMediaPublisher publisher, string title, string body, string relativeLink)
public static async Task AnnounceForum(this ExternalMediaPublisher publisher, string title, string formattedTitle, string body, string relativeLink)
{
await publisher.Send(new Post
{
Announcement = "News Post!",
Type = PostType.Announcement,
Group = PostGroups.Forum,
Title = title,
FormattedTitle = formattedTitle,
Body = body,
Link = publisher.ToAbsolute(relativeLink)
});
}

public static async Task SendForum(this ExternalMediaPublisher publisher, bool restricted, string title, string body, string relativeLink)
public static async Task SendForum(this ExternalMediaPublisher publisher, bool restricted, string title, string formattedTitle, string body, string relativeLink)
{
await publisher.Send(new Post
{
Expand All @@ -155,44 +163,48 @@ await publisher.Send(new Post
: PostType.General,
Group = PostGroups.Forum,
Title = title,
FormattedTitle = formattedTitle,
Body = body,
Link = publisher.ToAbsolute(relativeLink)
});
}

public static async Task SendGeneralWiki(this ExternalMediaPublisher publisher, string title, string body, string relativeLink)
public static async Task SendGeneralWiki(this ExternalMediaPublisher publisher, string title, string formattedTitle, string body, string relativeLink)
{
await publisher.Send(new Post
{
Announcement = "",
Type = PostType.General,
Group = PostGroups.Wiki,
Title = title,
FormattedTitle = formattedTitle,
Body = body,
Link = publisher.ToAbsolute(relativeLink)
});
}

public static async Task SendUserManagement(this ExternalMediaPublisher publisher, string title, string body, string relativeLink)
public static async Task SendUserManagement(this ExternalMediaPublisher publisher, string title, string formattedTitle, string body, string relativeLink)
{
await publisher.Send(new Post
{
Announcement = "",
Type = PostType.Administrative,
Group = PostGroups.UserManagement,
Title = title,
FormattedTitle = formattedTitle,
Body = body,
Link = publisher.ToAbsolute(relativeLink)
});
}

public static async Task SendGameManagement(this ExternalMediaPublisher publisher, string title, string body, string relativeLink)
public static async Task SendGameManagement(this ExternalMediaPublisher publisher, string title, string formattedTitle, string body, string relativeLink)
{
await publisher.Send(new Post
{
Type = PostType.General,
Group = PostGroups.Game,
Title = title,
FormattedTitle = formattedTitle,
Body = body,
Link = publisher.ToAbsolute(relativeLink)
});
Expand Down
7 changes: 7 additions & 0 deletions TASVideos.Core/Services/ExternalMediaPublisher/Post.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ public interface IPostable
/// Gets the post title
/// </summary>
string Title { get; }

/// <summary>
/// Gets the post title with markdown formatting.
/// Optionally may include "{0}" to be replaced with the <see cref="Link"/>.
/// </summary>
string FormattedTitle { get; }

/// <summary>
/// Gets a link that will direct a user to the resource or to more detailed information
Expand Down Expand Up @@ -51,6 +57,7 @@ public class Post : IPostable
{
public string Announcement { get; set; } = "";
public string Title { get; init; } = "";
public string FormattedTitle { get; init; } = "";
public string Link { get; init; } = "";
public string Body { get; init; } = "";
public string Group { get; init; } = "";
Expand Down
1 change: 1 addition & 0 deletions TASVideos/Pages/Account/ConfirmEmail.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public async Task<IActionResult> OnGet(string? userId, string? code)
await _signInManager.SignInAsync(user, isPersistent: false);
await _publisher.SendUserManagement(
$"User {user.UserName} activated",
$"User [{user.UserName}]({{0}}) activated",
"",
$"Users/Profile/{Uri.EscapeDataString(user.UserName)}");
await _userMaintenanceLogger.Log(user.Id, $"User activated from {IpAddress}");
Expand Down
1 change: 1 addition & 0 deletions TASVideos/Pages/Account/Register.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public async Task<IActionResult> OnPost()
await _signInManager.SignInAsync(user, isPersistent: false);
await _publisher.SendUserManagement(
$"New User registered! {user.UserName}",
$"New User registered! [{user.UserName}]({{0}})",
"",
$"Users/Profile/{Uri.EscapeDataString(user.UserName)}");
await _userMaintenanceLogger.Log(user.Id, $"New registration from {IpAddress}");
Expand Down
2 changes: 2 additions & 0 deletions TASVideos/Pages/Forum/Posts/Create.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ public async Task<IActionResult> OnPost()
{
await _publisher.AnnounceForum(
$"News Post by {user.UserName}{mood}",
$"[News Post]({{0}}) by {user.UserName}{mood}",
$"{topic.Forum.ShortName}: {topic.Title}{subject}",
$"Forum/Posts/{id}");
}
Expand All @@ -200,6 +201,7 @@ await _publisher.AnnounceForum(
await _publisher.SendForum(
topic.Forum.Restricted,
$"New Post by {user.UserName}{mood}",
$"[New Post]({{0}}) by {user.UserName}{mood}",
$"{topic.Forum.ShortName}: {topic.Title}{subject}",
$"Forum/Posts/{id}");
}
Expand Down
3 changes: 3 additions & 0 deletions TASVideos/Pages/Forum/Posts/Edit.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ public async Task<IActionResult> OnPost()
await _publisher.SendForum(
forumPost.Topic!.Forum!.Restricted,
$"Post edited by {User.Name()}",
$"[Post]({{0}}) edited by {User.Name()}",
$"{forumPost.Topic.Forum.ShortName}: {forumPost.Topic.Title}",
$"Forum/Posts/{Id}");
}
Expand Down Expand Up @@ -233,6 +234,7 @@ public async Task<IActionResult> OnPostDelete()
await _publisher.SendForum(
post.Topic!.Forum!.Restricted,
$"{(topicDeleted ? "Topic" : "Post")} DELETED by {User.Name()}",
$"[{(topicDeleted ? "Topic" : "Post")} DELETED]({{0}}) by {User.Name()}",
$"{post.Topic!.Forum!.ShortName}: {post.Topic.Title}",
topicDeleted ? "" : $"Forum/Topics/{post.Topic.Id}");
}
Expand Down Expand Up @@ -293,6 +295,7 @@ public async Task<IActionResult> OnPostSpam()
await _publisher.SendForum(
false,
$"{(topicDeleted ? "Topic" : "Post")} DELETED as SPAM, and user {post.Poster!.UserName} banned by {User.Name()}",
$"[{(topicDeleted ? "Topic" : "Post")} DELETED as SPAM]({{0}}), and user {post.Poster!.UserName} banned by {User.Name()}",
$"{oldForumShortName}: {oldTopicTitle}",
topicDeleted ? "" : $"Forum/Topics/{oldTopicId}");
}
Expand Down
1 change: 1 addition & 0 deletions TASVideos/Pages/Forum/Topics/Create.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ await _forumService.CreatePoll(
await _publisher.SendForum(
forum.Restricted,
$"New Topic by {User.Name()}",
$"[New Topic]({{0}}) by {User.Name()}",
$"{forum.ShortName}: {Topic.Title}",
$"Forum/Topics/{topic.Id}");

Expand Down
1 change: 1 addition & 0 deletions TASVideos/Pages/Forum/Topics/Index.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ public async Task<IActionResult> OnPostLock(string topicTitle, bool locked)
await _publisher.SendForum(
topic.Forum!.Restricted,
$"Topic {lockedState} by {User.Name()}",
$"[Topic]({{0}}) {lockedState} by {User.Name()}",
$"{topic.Forum.ShortName}: {topic.Title}",
$"Forum/Topics/{Id}");
}
Expand Down
1 change: 1 addition & 0 deletions TASVideos/Pages/Forum/Topics/Merge.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public async Task<IActionResult> OnPost()
await _publisher.SendForum(
originalTopic.Forum!.Restricted || destinationTopic.Forum!.Restricted,
$"Topics MERGED by {User.Name()}",
$"[Topics MERGED]({{0}}) by {User.Name()}",
$@"""{originalTopic.Title}"" into ""{destinationTopic.Title}""",
$"Forum/Topics/{destinationTopic.Id}");
}
Expand Down
1 change: 1 addition & 0 deletions TASVideos/Pages/Forum/Topics/Move.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public async Task<IActionResult> OnPost()
await _publisher.SendForum(
topicWasRestricted || forum.Restricted,
$"Topic MOVED by {User.Name()}",
$"[Topic]({{0}}) MOVED by {User.Name()}",
$@"""{Topic.TopicTitle}"" from {Topic.ForumName} to {forum.Name}",
$"Forum/Topics/{Id}");

Expand Down
1 change: 1 addition & 0 deletions TASVideos/Pages/Forum/Topics/Split.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public async Task<IActionResult> OnPost()
await _publisher.SendForum(
destinationForum.Restricted || topic.Forum!.Restricted,
$"Topic SPLIT by {User.Name()}",
$"[Topic]({{0}}) SPLIT by {User.Name()}",
$@"""{newTopic.Title}"" from ""{Topic.Title}""",
$"Forum/Topics/{newTopic.Id}");

Expand Down
18 changes: 15 additions & 3 deletions TASVideos/Pages/Games/Edit.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@ public async Task<IActionResult> OnPost()
var saveResult = await ConcurrentSave(_db, saveMessage, $"Unable to update Game {Id}");
if (saveResult && !Game.MinorEdit)
{
await _publisher.SendGameManagement($"{saveMessage} by {User.Name()}", "", $"{Id}G");
await _publisher.SendGameManagement(
$"{saveMessage} by {User.Name()}",
$"Game [{game.DisplayName}]({{0}}) updated by {User.Name()}",
"",
$"{Id}G");
}
}
else
Expand All @@ -152,7 +156,11 @@ public async Task<IActionResult> OnPost()
var saveResult = await ConcurrentSave(_db, saveMessage, "Unable to create game");
if (saveResult && !Game.MinorEdit)
{
await _publisher.SendGameManagement($"{saveMessage} by {User.Name()}", "", $"{game.Id}G");
await _publisher.SendGameManagement(
$"{saveMessage} by {User.Name()}",
$"Game [{game.DisplayName}]({{0}}) created by {User.Name()}",
"",
$"{game.Id}G");
}
}

Expand Down Expand Up @@ -195,7 +203,11 @@ public async Task<IActionResult> OnPostDelete()
var saveResult = await ConcurrentSave(_db, saveMessage, $"Unable to delete Game {Id}");
if (saveResult)
{
await _publisher.SendGameManagement($"{saveMessage} by {User.Name()}", "", $"{Id}G");
await _publisher.SendGameManagement(
$"{saveMessage} by {User.Name()}",
"",
"",
"");
}

return BasePageRedirect("List");
Expand Down
6 changes: 5 additions & 1 deletion TASVideos/Pages/Games/Rewire.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ public async Task<IActionResult> OnPost()
var result = await ConcurrentSave(_db, $"Rewired Game {FromGameId} into Game {IntoGameId}", $"Unable to rewire Game {FromGameId} into Game {IntoGameId}");
if (result)
{
await _publisher.SendGameManagement($"{IntoGameId}G edited by {User.Name()}", $"Rewired {FromGameId}G into {IntoGameId}G", $"{IntoGameId}G");
await _publisher.SendGameManagement(
$"{IntoGameId}G edited by {User.Name()}",
$"[{IntoGameId}G]({{0}}) edited by {User.Name()}",
$"Rewired {FromGameId}G into {IntoGameId}G",
$"{IntoGameId}G");
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions TASVideos/Pages/Publications/AdditionalMovies.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public async Task<IActionResult> OnPost()
{
await _publisher.SendPublicationEdit(
$"{Id}M edited by {User.Name()}",
$"[{Id}M]({{0}}) edited by {User.Name()}",
$"{log} | {PublicationTitle}",
$"{Id}M");
}
Expand All @@ -146,6 +147,7 @@ public async Task<IActionResult> OnPostDelete(int publicationFileId)
{
await _publisher.SendPublicationEdit(
$"{Id}M edited by {User.Name()}",
$"[{Id}M]({{0}}) edited by {User.Name()}",
$"{log}",
$"{Id}M");
}
Expand Down
1 change: 1 addition & 0 deletions TASVideos/Pages/Publications/Catalog.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ public async Task<IActionResult> OnPost()
{
await _publisher.SendGameManagement(
$"{Id}M Catalog edited by {User.Name()}",
$"[{Id}M]({{0}}) Catalog edited by {User.Name()}",
$"{string.Join(", ", externalMessages)} | {publication.Title}",
$"{Id}M");
}
Expand Down
1 change: 1 addition & 0 deletions TASVideos/Pages/Publications/Edit.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ await _youtubeSync.SyncYouTubeVideo(new YoutubeVideo(
{
await _publisher.SendPublicationEdit(
$"{Id}M edited by {User.Name()}",
$"[{Id}M]({{0}}) edited by {User.Name()}",
$"{string.Join(", ", externalMessages)} | {publication.Title}",
$"{Id}M");
}
Expand Down
1 change: 1 addition & 0 deletions TASVideos/Pages/Publications/EditClass.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public async Task<IActionResult> OnPost()
{
await _publisher.SendPublicationEdit(
$"{log} by {User.Name()}",
$"[{Id}M]({{0}}) Class changed from {originalClass} to {publicationClass.Name} by {User.Name()}",
Title,
$"{Id}M");
}
Expand Down
2 changes: 2 additions & 0 deletions TASVideos/Pages/Publications/EditFiles.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public async Task<IActionResult> OnPost()
await _publicationMaintenanceLogger.Log(Id, User.GetUserId(), log);
await _publisher.SendPublicationEdit(
$"{Id}M edited by {User.Name()}",
$"[{Id}M]({{0}}) edited by {User.Name()}",
$"{log} | {Title}",
$"{Id}M");

Expand All @@ -99,6 +100,7 @@ public async Task<IActionResult> OnPostDelete(int publicationFileId)
await _publicationMaintenanceLogger.Log(Id, User.GetUserId(), log);
await _publisher.SendPublicationEdit(
$"{Id}M edited by {User.Name()}",
$"[{Id}M]({{0}}) edited by {User.Name()}",
$"{log}",
$"{Id}M");
}
Expand Down
Loading

0 comments on commit 49a27db

Please sign in to comment.