mirror of
https://github.com/JogmansonReal/Radicade.git
synced 2026-10-27 07:59:03 -07:00
Radicade Public Code
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user