Files
space-station-14/Content.Server/Administration/Notes/AdminNotesExtensions.cs
Pieter-Jan Briers 29b7fc4463 Stable to master (#42599)
Ban database refactor (#42495)

* Ban DB refactor seems to work at a basic level for PostgreSQL

* New ban creation API

Supports all the new functionality (multiple players/addresses/hwids/roles/rounds per ban).

* Make the migration irreversible

* Re-implement ban notifications

The server ID check is no longer done as admins may want to place bans spanning multiple rounds irrelevant of the source server.

* Fix some split query warnings

* Implement migration on SQLite

* More comments

* Remove required from ban reason

SS14.Admin changes would like this

* More missing AsSplitQuery() calls

* Fix missing ban type filter

* Fix old CreateServerBan API with permanent time

* Fix department and role ban commands with permanent time

* Re-add banhits navigation property

Dropped this on accident, SS14.Admin needs it.

* More ban API fixes.

* Don't fetch ban exemption info for role bans

Not relevant, reduces query performance

* Regenerate migrations

* Fix adminnotes command for players that never connected

Would blow up handling null player records. Not a new bug introduced by the refactor, but I ran into it.

* Great shame... I accidentally committed submodule update...

* Update GDPR scripts

* Fix sandbox violation

* Fix bans with duplicate info causing DB exceptions

Most notably happened with role bans, as multiple departments may include the same role.
2026-01-23 15:34:23 +01:00

78 lines
2.9 KiB
C#

using System.Collections.Immutable;
using System.Linq;
using Content.Server.Database;
using Content.Shared.Administration.Notes;
using Content.Shared.Database;
namespace Content.Server.Administration.Notes;
public static class AdminNotesExtensions
{
public static SharedAdminNote ToShared(this IAdminRemarksRecord note)
{
NoteSeverity? severity = null;
var secret = false;
NoteType type;
ImmutableArray<BanRoleDef>? bannedRoles = null;
string? unbannedByName = null;
DateTime? unbannedTime = null;
bool? seen = null;
switch (note)
{
case AdminNoteRecord adminNote:
type = NoteType.Note;
severity = adminNote.Severity;
secret = adminNote.Secret;
break;
case AdminWatchlistRecord:
type = NoteType.Watchlist;
secret = true;
break;
case AdminMessageRecord adminMessage:
type = NoteType.Message;
seen = adminMessage.Seen;
break;
case BanNoteRecord { Type: BanType.Server } ban:
type = NoteType.ServerBan;
severity = ban.Severity;
unbannedTime = ban.UnbanTime;
unbannedByName = ban.UnbanningAdmin?.LastSeenUserName ?? Loc.GetString("system-user");
break;
case BanNoteRecord { Type: BanType.Role } roleBan:
type = NoteType.RoleBan;
severity = roleBan.Severity;
bannedRoles = roleBan.Roles;
unbannedTime = roleBan.UnbanTime;
unbannedByName = roleBan.UnbanningAdmin?.LastSeenUserName ?? Loc.GetString("system-user");
break;
default:
throw new ArgumentOutOfRangeException(nameof(type), note.GetType(), "Unknown note type");
}
// There may be bans without a user, but why would we ever be converting them to shared notes?
if (note.Players.Length == 0)
throw new ArgumentNullException(nameof(note), "Player user ID cannot be empty for a note");
return new SharedAdminNote(
note.Id,
[..note.Players.Select(p => p.UserId)],
[..note.Rounds.Select(r => r.Id)],
note.Rounds.SingleOrDefault()?.Server.Name, // TODO: Show all server names?
note.PlaytimeAtNote,
type,
note.Message,
severity,
secret,
note.CreatedBy?.LastSeenUserName ?? Loc.GetString("system-user"),
note.LastEditedBy?.LastSeenUserName ?? string.Empty,
note.CreatedAt.UtcDateTime,
note.LastEditedAt?.UtcDateTime,
note.ExpirationTime?.UtcDateTime,
bannedRoles,
unbannedTime,
unbannedByName,
seen
);
}
}