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(@"]*)>", 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 : $""); } private static bool ResolvesToAnImage(string rawReference) => long.TryParse(rawReference, out var imageId) && imageId > 0; } }