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