Skip to content

Commit

Permalink
Обновил апишки
Browse files Browse the repository at this point in the history
  • Loading branch information
VigersRay committed Jul 22, 2024
1 parent a69d084 commit 21fbd92
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Content.Client/Lobby/LobbyState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected override void Startup()

Lobby!.LocalChangelogBody.CleanChangelog();

var lobbyChangelogs = _cfg.GetCVar(SunriseCCVars.LobbyChangelogs).Split(',');
var lobbyChangelogs = _cfg.GetCVar(SunriseCCVars.LobbyChangelogsList).Split(',');

var changelogs = new List<ChangelogManager.Changelog>();
foreach (var lobbyChangelog in lobbyChangelogs)
Expand Down
45 changes: 14 additions & 31 deletions Content.Server/Administration/Managers/BanManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
using JetBrains.Annotations;
using Robust.Shared;
using CCVars = Content.Shared.CCVar.CCVars;
using Content.Sunrise.Interfaces.Server; // Sunrise-Edit

namespace Content.Server.Administration.Managers;

Expand All @@ -44,6 +45,8 @@ public sealed class BanManager : IBanManager, IPostInjectInit
[Dependency] private readonly ILogManager _logManager = default!;
[Dependency] private readonly IConfigurationManager _config = default!;

private IServerDiscordAuthManager? _discordAuth; // Sunrise-Edit

private ISawmill _sawmill = default!;
public const string SawmillId = "admin.bans";
public const string JobPrefix = "Job:";
Expand All @@ -54,8 +57,6 @@ public sealed class BanManager : IBanManager, IPostInjectInit
private WebhookData? _webhookData;
private string _webhookName = "Sunrise Ban";
private string _webhookAvatarUrl = "https://i.ibb.co/WfGqKtG/avatar.png";
private string _apiUrl = string.Empty;
private string _apiKey = string.Empty;
// Sunrise-end

private readonly Dictionary<NetUserId, HashSet<ServerRoleBanDef>> _cachedRoleBans = new();
Expand All @@ -68,8 +69,8 @@ public void Initialize()
// Sunrise-start
_config.OnValueChanged(SunriseCCVars.DiscordBanWebhook, OnWebhookChanged, true);
_config.OnValueChanged(CVars.GameHostName, OnServerNameChanged, true);
_cfg.OnValueChanged(SunriseCCVars.DiscordAuthApiUrl, v => _apiUrl = v, true);
_cfg.OnValueChanged(SunriseCCVars.DiscordAuthApiKey, v => _apiKey = v, true);

IoCManager.Instance!.TryResolveType(out _discordAuth);
// Sunrise-end
}

Expand Down Expand Up @@ -425,8 +426,13 @@ private async Task<WebhookPayload> GenerateJobBanPayload(ServerRoleBanDef banDef
foreach (var role in roles)
rolesString += $"\n> `{role}`";

var adminDiscordId = await GetDiscordUserId(banDef.BanningAdmin);
var targetDiscordId = await GetDiscordUserId(banDef.UserId);
string? adminDiscordId = null;
string? targetDiscordId = null;
if (_discordAuth != null)
{
adminDiscordId = await _discordAuth.GetDiscordUserId(banDef.BanningAdmin);
targetDiscordId = await _discordAuth.GetDiscordUserId(banDef.UserId);
}

var adminLink = "";
var targetLink = "";
Expand Down Expand Up @@ -530,29 +536,6 @@ private async Task<WebhookPayload> GenerateJobBanPayload(ServerRoleBanDef banDef
};
}

public async Task<string?> GetDiscordUserId(NetUserId? userId, CancellationToken cancel = default)
{
if (_apiUrl == string.Empty)
return null;

_sawmill.Debug($"Player {userId} check Discord username");

var requestUrl = $"{_apiUrl}/get_discord_user/?user_id={WebUtility.UrlEncode(userId.ToString())}&key={_apiKey}";
var response = await _httpClient.GetAsync(requestUrl, cancel);
if (response.StatusCode == HttpStatusCode.NotFound)
{
return null;
}
if (!response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
throw new Exception($"Verification API returned bad status code: {response.StatusCode}\nResponse: {content}");
}

var data = await response.Content.ReadFromJsonAsync<DiscordUserResponse>(cancellationToken: cancel);
return data!.UserId;
}

private async Task<WebhookPayload> GenerateBanPayload(ServerBanDef banDef, uint? minutes = null)
{
var hwidString = banDef.HWId != null
Expand All @@ -576,8 +559,8 @@ private async Task<WebhookPayload> GenerateBanPayload(ServerBanDef banDef, uint?
DateTime.UtcNow,
TimeZoneInfo.FindSystemTimeZoneById("Russian Standard Time"));

var adminDiscordId = await GetDiscordUserId(banDef.BanningAdmin);
var targetDiscordId = await GetDiscordUserId(banDef.UserId);
var adminDiscordId = await _discordAuth.GetDiscordUserId(banDef.BanningAdmin);

Check failure on line 562 in Content.Server/Administration/Managers/BanManager.cs

View workflow job for this annotation

GitHub Actions / Test Packaging

Dereference of a possibly null reference.

Check failure on line 562 in Content.Server/Administration/Managers/BanManager.cs

View workflow job for this annotation

GitHub Actions / Test Packaging

Dereference of a possibly null reference.

Check failure on line 562 in Content.Server/Administration/Managers/BanManager.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Dereference of a possibly null reference.

Check failure on line 562 in Content.Server/Administration/Managers/BanManager.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Dereference of a possibly null reference.

Check failure on line 562 in Content.Server/Administration/Managers/BanManager.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Dereference of a possibly null reference.

Check failure on line 562 in Content.Server/Administration/Managers/BanManager.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Dereference of a possibly null reference.
var targetDiscordId = await _discordAuth.GetDiscordUserId(banDef.UserId);

var adminLink = "";
var targetLink = "";
Expand Down
17 changes: 10 additions & 7 deletions Content.Shared/_Sunrise/SunriseCCVars/SunriseCCVars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ public sealed class SunriseCCVars
public static readonly CVarDef<string> DiscordAuthApiUrl =
CVarDef.Create("discord_auth.api_url", "", CVar.SERVERONLY);

public static readonly CVarDef<string> DiscordAuthApiToken =
CVarDef.Create("discord_auth.api_token", "", CVar.SERVERONLY | CVar.CONFIDENTIAL);

public static readonly CVarDef<bool> DiscordAuthCheckMember =
CVarDef.Create("discord_auth.check_member", false, CVar.SERVERONLY);

public static readonly CVarDef<string> DiscordAuthApiKey =
CVarDef.Create("discord_auth.api_key", "", CVar.SERVERONLY | CVar.CONFIDENTIAL);

/*
* GodMode RoundEnd
*/
Expand Down Expand Up @@ -102,9 +102,12 @@ public static readonly CVarDef<bool>
* Sponsor API
*/

public static readonly CVarDef<string> SponsorsApiUrl =
public static readonly CVarDef<string> SponsorApiUrl =
CVarDef.Create("sponsor.api_url", "", CVar.SERVERONLY);

public static readonly CVarDef<string> SponsorApiToken =
CVarDef.Create("sponsor.api_token", "", CVar.SERVERONLY | CVar.CONFIDENTIAL);

/*
* Greetings
*/
Expand All @@ -129,7 +132,7 @@ public static readonly CVarDef<bool>
CVarDef.Create("newlife.sponsor_only", false, CVar.SERVER | CVar.REPLICATED);

public static readonly CVarDef<int> NewLifeTimeout =
CVarDef.Create("newlife.timeout", 30, CVar.SERVERONLY);
CVarDef.Create("newlife.timeout", 5, CVar.SERVERONLY);

/*
* Servers Hub
Expand Down Expand Up @@ -200,8 +203,8 @@ public static readonly CVarDef<bool>
* Lobby Changelog
*/

public static readonly CVarDef<string> LobbyChangelogs =
CVarDef.Create("lobby_changelog.id", "ChangelogSunrise.yml,Changelog.yml", CVar.SERVER | CVar.REPLICATED);
public static readonly CVarDef<string> LobbyChangelogsList =
CVarDef.Create("lobby_changelog.list", "ChangelogSunrise.yml,Changelog.yml", CVar.SERVER | CVar.REPLICATED);

/*
* Cryoteleport
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Content.Sunrise.Interfaces.Shared;
using JetBrains.Annotations;
using Robust.Server.Player;
using Robust.Shared.Network;
using Robust.Shared.Player;

Expand All @@ -9,8 +7,10 @@ namespace Content.Sunrise.Interfaces.Server;
public interface IServerDiscordAuthManager : ISharedDiscordAuthManager
{
public event EventHandler<ICommonSession>? PlayerVerified;
public Task<DiscordGenerateLinkResponse> GenerateAuthLink(NetUserId userId, CancellationToken cancel);
public Task<DiscordGenerateLinkResponse> GenerateAuthLink(NetUserId userId, string username, CancellationToken cancel);
public Task<bool> IsVerified(NetUserId userId, CancellationToken cancel);
public Task<string?> GetDiscordUserId(NetUserId? userId, CancellationToken cancel = default);

}

public sealed record DiscordLinkResponse(string Url, byte[] Qrcode);
Expand Down

0 comments on commit 21fbd92

Please sign in to comment.