AdminNotesLine.xaml.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System.Text;
  2. using Content.Shared.Administration.Notes;
  3. using Content.Shared.Database;
  4. using Robust.Client.AutoGenerated;
  5. using Robust.Client.GameObjects;
  6. using Robust.Client.UserInterface;
  7. using Robust.Client.UserInterface.Controls;
  8. using Robust.Client.UserInterface.XAML;
  9. using Robust.Shared.Input;
  10. using Robust.Shared.Utility;
  11. namespace Content.Client.Administration.UI.Notes;
  12. [GenerateTypedNameReferences]
  13. public sealed partial class AdminNotesLine : BoxContainer
  14. {
  15. private readonly SpriteSystem _sprites;
  16. private const string AdminNotesTextureBase = "/Textures/Interface/AdminNotes/";
  17. private static readonly Dictionary<NoteSeverity, string> SeverityIcons = new()
  18. {
  19. { NoteSeverity.None, AdminNotesTextureBase + "none_button.png" },
  20. { NoteSeverity.Minor, AdminNotesTextureBase + "minor_button.png" },
  21. { NoteSeverity.Medium, AdminNotesTextureBase + "medium_button.png" },
  22. { NoteSeverity.High, AdminNotesTextureBase + "high_button.png" },
  23. };
  24. private static readonly Dictionary<NoteType, string> NoteTypeIcons = new()
  25. {
  26. { NoteType.Message, AdminNotesTextureBase + "message.png" },
  27. { NoteType.Watchlist, AdminNotesTextureBase + "watchlist.png" },
  28. };
  29. public AdminNotesLine(SpriteSystem sprites, SharedAdminNote note)
  30. {
  31. RobustXamlLoader.Load(this);
  32. _sprites = sprites;
  33. Note = note;
  34. MouseFilter = MouseFilterMode.Pass;
  35. Separator.Visible = true;
  36. Refresh();
  37. }
  38. public SharedAdminNote Note { get; private set; }
  39. public int Id => Note.Id;
  40. public event Func<AdminNotesLine, bool>? OnClicked;
  41. /// <summary>
  42. /// Attempts to refresh the current note line with new data. The note it draws data on is stored in <see cref="Note"/>
  43. /// </summary>
  44. private void Refresh()
  45. {
  46. string? iconPath;
  47. if (Note.NoteSeverity is not null && !NoteTypeIcons.ContainsKey(Note.NoteType))
  48. SeverityIcons.TryGetValue(Note.NoteSeverity.Value, out iconPath);
  49. else
  50. NoteTypeIcons.TryGetValue(Note.NoteType, out iconPath);
  51. if (iconPath is null)
  52. {
  53. SeverityRect.Visible = false;
  54. Logger.WarningS("admin.notes", $"Could not find an icon for note ID {Note.Id}");
  55. }
  56. else
  57. {
  58. SeverityRect.Texture = _sprites.Frame0(new SpriteSpecifier.Texture(new ResPath(iconPath)));
  59. }
  60. TimeLabel.Text = Note.CreatedAt.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss");
  61. ServerLabel.Text = Note.ServerName ?? "Unknown";
  62. RoundLabel.Text = Note.Round == null ? "Unknown round" : "Round " + Note.Round;
  63. AdminLabel.Text = Note.CreatedByName;
  64. PlaytimeLabel.Text = $"{Note.PlaytimeAtNote.TotalHours: 0.0}h";
  65. if (Note.Secret)
  66. {
  67. SecretSeparator.Visible = true;
  68. SecretLabel.Visible = true;
  69. }
  70. if (Note.UnbannedTime is not null)
  71. {
  72. ExtraLabel.Text = Loc.GetString("admin-notes-unbanned", ("admin", Note.UnbannedByName ?? "[error]"), ("date", Note.UnbannedTime));
  73. ExtraLabel.Visible = true;
  74. }
  75. else if (Note.ExpiryTime is not null)
  76. {
  77. // Notes should never be visible when expired, bans should
  78. if (Note.ExpiryTime.Value > DateTime.UtcNow)
  79. {
  80. ExpiresLabel.Text = Loc.GetString("admin-note-editor-expiry-label-params",
  81. ("date", Note.ExpiryTime.Value.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss")),
  82. ("expiresIn", (Note.ExpiryTime.Value - DateTime.UtcNow).ToString("d'd 'hh':'mm")));
  83. ExpiresLabel.Modulate = Color.FromHex("#86DC3D");
  84. }
  85. else
  86. {
  87. ExpiresLabel.Text = Loc.GetString("admin-note-editor-expiry-label-expired");
  88. }
  89. ExpiresLabel.Visible = true;
  90. }
  91. if (Note.LastEditedAt > Note.CreatedAt)
  92. {
  93. EditedLabel.Text = Loc.GetString("admin-notes-edited", ("author", Note.EditedByName), ("date", Note.LastEditedAt.Value.ToLocalTime()));
  94. EditedLabel.Visible = true;
  95. }
  96. switch (Note.NoteType)
  97. {
  98. case NoteType.ServerBan:
  99. NoteLabel.SetMessage(FormatBanMessage());
  100. break;
  101. case NoteType.RoleBan:
  102. NoteLabel.SetMessage(FormatRoleBanMessage());
  103. break;
  104. case NoteType.Note:
  105. case NoteType.Watchlist:
  106. case NoteType.Message:
  107. default:
  108. NoteLabel.SetMessage(Note.Message);
  109. break;
  110. }
  111. if (Note.Seen == true)
  112. {
  113. ExtraLabel.Text = Loc.GetString("admin-notes-message-seen");
  114. ExtraLabel.Visible = true;
  115. }
  116. }
  117. private string FormatBanMessage()
  118. {
  119. var banMessage = new StringBuilder($"{Loc.GetString("admin-notes-banned-from")} {Loc.GetString("admin-notes-the-server")} ");
  120. return FormatBanMessageCommon(banMessage);
  121. }
  122. private string FormatRoleBanMessage()
  123. {
  124. var banMessage = new StringBuilder($"{Loc.GetString("admin-notes-banned-from")} {string.Join(", ", Note.BannedRoles ?? new []{"unknown"})} ");
  125. return FormatBanMessageCommon(banMessage);
  126. }
  127. private string FormatBanMessageCommon(StringBuilder sb)
  128. {
  129. if (Note.ExpiryTime is null)
  130. {
  131. sb.Append(Loc.GetString("admin-notes-permanently"));
  132. }
  133. else
  134. {
  135. sb.Append("for ");
  136. var banLength = Note.ExpiryTime.Value - Note.CreatedAt;
  137. if (banLength.Days > 0)
  138. sb.Append(Loc.GetString("admin-notes-days", ("days", banLength.TotalDays.ToString(".00"))));
  139. else if (banLength.Hours > 0)
  140. sb.Append(Loc.GetString("admin-notes-hours", ("hours", banLength.TotalHours.ToString(".00"))));
  141. else
  142. sb.Append(Loc.GetString("admin-notes-minutes", ("minutes", banLength.TotalMinutes.ToString(".00"))));
  143. }
  144. sb.Append(" - ");
  145. sb.Append(Note.Message);
  146. return sb.ToString();
  147. }
  148. protected override void KeyBindDown(GUIBoundKeyEventArgs args)
  149. {
  150. base.KeyBindDown(args);
  151. if (args.Function != EngineKeyFunctions.UIRightClick &&
  152. args.Function != EngineKeyFunctions.UIClick)
  153. {
  154. return;
  155. }
  156. if (OnClicked?.Invoke(this) == true)
  157. {
  158. args.Handle();
  159. }
  160. }
  161. public void UpdateNote(SharedAdminNote note)
  162. {
  163. Note = note;
  164. Refresh();
  165. }
  166. protected override void Dispose(bool disposing)
  167. {
  168. base.Dispose(disposing);
  169. if (!disposing)
  170. {
  171. return;
  172. }
  173. OnClicked = null;
  174. }
  175. }