PluralKit/PluralKit.Bot/Utils/ModelUtils.cs

65 lines
2.8 KiB
C#
Raw Permalink Normal View History

using System.Text.RegularExpressions;
using PluralKit.Core;
namespace PluralKit.Bot;
public static class ModelUtils
{
public static string NameFor(this PKMember member, Context ctx) =>
member.NameFor(ctx.LookupContextFor(member.System));
public static string NameFor(this PKGroup group, Context ctx) =>
group.NameFor(ctx.LookupContextFor(group.System));
public static string NameFor(this PKSystem system, Context ctx) =>
system.NameFor(ctx.LookupContextFor(system.Id));
public static string AvatarFor(this PKMember member, Context ctx) =>
member.AvatarFor(ctx.LookupContextFor(member.System)).TryGetCleanCdnUrl();
2020-06-20 16:00:50 +02:00
public static string IconFor(this PKGroup group, Context ctx) =>
group.IconFor(ctx.LookupContextFor(group.System)).TryGetCleanCdnUrl();
public static string DisplayName(this PKMember member) =>
member.DisplayName ?? member.Name;
2024-04-28 15:46:06 +12:00
public static string Reference(this PKMember member, Context ctx) => EntityReference(member.DisplayHid(ctx.Config), member.NameFor(ctx));
public static string Reference(this PKGroup group, Context ctx) => EntityReference(group.DisplayHid(ctx.Config), group.NameFor(ctx));
2024-06-04 18:56:28 +09:00
public static string DisplayHid(this PKSystem system, SystemConfig? cfg = null, bool isList = false) => HidTransform(system.Hid, cfg, isList);
public static string DisplayHid(this PKGroup group, SystemConfig? cfg = null, bool isList = false) => HidTransform(group.Hid, cfg, isList);
public static string DisplayHid(this PKMember member, SystemConfig? cfg = null, bool isList = false) => HidTransform(member.Hid, cfg, isList);
private static string HidTransform(string hid, SystemConfig? cfg = null, bool isList = false) =>
HidUtils.HidTransform(
hid,
cfg != null && cfg.HidDisplaySplit,
2024-06-04 18:56:28 +09:00
cfg != null && cfg.HidDisplayCaps,
isList ? (cfg?.HidListPadding ?? SystemConfig.HidPadFormat.None) : SystemConfig.HidPadFormat.None // padding only on lists
);
private static string EntityReference(string hid, string name)
{
bool IsSimple(string s) =>
// No spaces, no symbols, allow single quote but not at the start
Regex.IsMatch(s, "^[\\w\\d\\-_'?]+$") && !s.StartsWith("'");
2021-08-27 11:03:47 -04:00
// If it's very long (>25 chars), always use hid
if (name.Length >= 25)
return hid;
// If name is "simple" just use that
if (IsSimple(name))
return name;
2021-08-27 11:03:47 -04:00
// If three or fewer "words" and they're all simple individually, quote them
var words = name.Split(' ');
if (words.Length <= 3 && words.All(w => w.Length > 0 && IsSimple(w)))
// Words with double quotes are never "simple" so we're safe to naive-quote here
return $"\"{name}\"";
2021-08-27 11:03:47 -04:00
// Otherwise, just use hid
return hid;
}
}