2020-08-25 22:33:04 +02:00
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
|
using PluralKit.Core;
|
2020-06-18 17:08:36 +02:00
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
namespace PluralKit.Bot;
|
|
|
|
|
|
|
|
public static class ModelUtils
|
2020-06-18 17:08:36 +02:00
|
|
|
{
|
2021-11-26 21:10:56 -05:00
|
|
|
public static string NameFor(this PKMember member, Context ctx) =>
|
2021-12-06 00:32:54 -05:00
|
|
|
member.NameFor(ctx.LookupContextFor(member.System));
|
2020-06-18 17:08:36 +02:00
|
|
|
|
2022-01-14 22:30:02 -05:00
|
|
|
public static string NameFor(this PKGroup group, Context ctx) =>
|
|
|
|
group.NameFor(ctx.LookupContextFor(group.System));
|
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
public static string AvatarFor(this PKMember member, Context ctx) =>
|
2021-12-06 00:32:54 -05:00
|
|
|
member.AvatarFor(ctx.LookupContextFor(member.System)).TryGetCleanCdnUrl();
|
2020-06-20 16:00:50 +02:00
|
|
|
|
2022-01-14 22:30:02 -05:00
|
|
|
public static string IconFor(this PKGroup group, Context ctx) =>
|
|
|
|
group.IconFor(ctx.LookupContextFor(group.System)).TryGetCleanCdnUrl();
|
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
public static string DisplayName(this PKMember member) =>
|
|
|
|
member.DisplayName ?? member.Name;
|
2020-08-25 22:33:04 +02:00
|
|
|
|
2022-02-05 09:26:14 -05:00
|
|
|
public static string Reference(this PKMember member, Context ctx) => EntityReference(member.Hid, member.NameFor(ctx));
|
|
|
|
public static string Reference(this PKGroup group, Context ctx) => EntityReference(group.Hid, group.NameFor(ctx));
|
2020-08-25 22:33:04 +02:00
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
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
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
// If it's very long (>25 chars), always use hid
|
|
|
|
if (name.Length >= 25)
|
|
|
|
return hid;
|
2020-08-25 22:33:04 +02:00
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
// If name is "simple" just use that
|
|
|
|
if (IsSimple(name))
|
|
|
|
return name;
|
2021-08-27 11:03:47 -04:00
|
|
|
|
2021-11-26 21:10:56 -05: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
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
// Otherwise, just use hid
|
|
|
|
return hid;
|
2020-06-18 17:08:36 +02:00
|
|
|
}
|
|
|
|
}
|