Files
QMAX/server/QMax.Api/Services/MessageTextSanitizer.cs
T
2026-07-06 09:10:31 +03:00

101 lines
3.1 KiB
C#

namespace QMax.Api.Services;
public static class MessageTextSanitizer
{
public static string? CleanChatPreview(string? value)
{
if (IsGenericMediaLabel(value))
{
return "\u041c\u0435\u0434\u0438\u0430";
}
return IsQuestionMarkArtifact(value)
? "\u0421\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435"
: value;
}
public static string? CleanMessageText(string? value, bool hasAttachments)
{
if (hasAttachments && IsGenericAttachmentLabel(value))
{
return null;
}
if (!IsQuestionMarkArtifact(value))
{
return value;
}
return hasAttachments
? null
: "\u0421\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435";
}
public static bool IsGenericMediaLabel(string? value)
{
return value?.Trim().ToLowerInvariant() switch
{
"\u043c\u0435\u0434\u0438\u0430" => true,
"media" => true,
"\u0444\u043e\u0442\u043e" => true,
"photo" => true,
"image" => true,
"gif" => true,
"\u0432\u0438\u0434\u0435\u043e" => true,
"video" => true,
"\u0430\u0443\u0434\u0438\u043e" => true,
"audio" => true,
"\u0433\u043e\u043b\u043e\u0441\u043e\u0432\u043e\u0435" => true,
"voice" => true,
"\u0441\u0442\u0438\u043a\u0435\u0440" => true,
"sticker" => true,
"\u044d\u043c\u043e\u0434\u0437\u0438" => true,
"emoji" => true,
"\u043f\u0440\u0438\u043a\u0440\u0435\u043f\u043b\u0435\u043d\u043d\u044b\u0435 \u0444\u043e\u0442\u043e" => true,
"\u043f\u0440\u0438\u043a\u0440\u0435\u043f\u043b\u0451\u043d\u043d\u044b\u0435 \u0444\u043e\u0442\u043e" => true,
"attached photos" => true,
_ => false
};
}
public static bool IsGenericAttachmentLabel(string? value)
{
if (IsGenericMediaLabel(value))
{
return true;
}
return value?.Trim().ToLowerInvariant() switch
{
"\u0441\u043a\u0430\u0447\u0430\u0442\u044c" => true,
"download" => true,
"\u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c" => true,
"file" => true,
"\u0444\u0430\u0439\u043b" => true,
"document" => true,
"\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442" => true,
"contact" => true,
"\u043a\u043e\u043d\u0442\u0430\u043a\u0442" => true,
_ => false
};
}
public static bool IsQuestionMarkArtifact(string? value)
{
var text = value?.Trim();
if (string.IsNullOrWhiteSpace(text))
{
return false;
}
var questionMarks = text.Count(c => c is '?' or '\uFFFD');
if (questionMarks < 4)
{
return false;
}
var meaningfulCharacters = text.Count(c => char.IsLetterOrDigit(c) && c != '?');
return meaningfulCharacters == 0;
}
}