Files
Radicade/Server/Chat/ChatPhotoMessages.cs
2026-10-16 23:33:13 -07:00

40 lines
1.1 KiB
C#

using System.Text.RegularExpressions;
namespace RadicadeServer.API
{
public static class ChatPhotoMessages
{
public static readonly TimeSpan UploadWindow = TimeSpan.FromMinutes(15);
private static readonly Regex ImageReference = new(@"<Image:([^>]*)>", RegexOptions.Compiled);
private static readonly Regex PhotoEnvelope = new("\"Type\"\\s*:\\s*2(?![0-9])", RegexOptions.Compiled);
public static bool IsPhotoEnvelope(string? contents)
=> !string.IsNullOrEmpty(contents) && PhotoEnvelope.IsMatch(contents);
public static bool HasUnresolvedImageReference(string? contents)
{
if (string.IsNullOrEmpty(contents))
return false;
foreach (Match match in ImageReference.Matches(contents))
{
if (!ResolvesToAnImage(match.Groups[1].Value))
return true;
}
return false;
}
public static string ResolveImageReference(string contents, long imageId)
{
return ImageReference.Replace(contents, match =>
ResolvesToAnImage(match.Groups[1].Value) ? match.Value : $"<Image:{imageId}>");
}
private static bool ResolvesToAnImage(string rawReference)
=> long.TryParse(rawReference, out var imageId) && imageId > 0;
}
}