Finally retire the PKMember setters!

This commit is contained in:
Ske
2020-06-29 14:15:30 +02:00
parent 281b669391
commit c5697b33e2
9 changed files with 94 additions and 86 deletions

View File

@@ -9,27 +9,27 @@ namespace PluralKit.Core {
public class PKMember
{
public MemberId Id { get; }
public string Hid { get; set; }
public SystemId System { get; set; }
public string Color { get; set; }
public string AvatarUrl { get; set; }
public string Name { get; set; }
public string DisplayName { get; set; }
public LocalDate? Birthday { get; set; }
public string Pronouns { get; set; }
public string Description { get; set; }
public ICollection<ProxyTag> ProxyTags { get; set; }
public bool KeepProxy { get; set; }
public Instant Created { get; set; }
public int MessageCount { get; set; }
public string Hid { get; }
public SystemId System { get; }
public string Color { get; }
public string AvatarUrl { get; }
public string Name { get; }
public string DisplayName { get; }
public LocalDate? Birthday { get; }
public string Pronouns { get; }
public string Description { get; }
public ICollection<ProxyTag> ProxyTags { get; }
public bool KeepProxy { get; }
public Instant Created { get; }
public int MessageCount { get; }
public PrivacyLevel MemberVisibility { get; set; }
public PrivacyLevel DescriptionPrivacy { get; set; }
public PrivacyLevel AvatarPrivacy { get; set; }
public PrivacyLevel NamePrivacy { get; set; } //ignore setting if no display name is set
public PrivacyLevel BirthdayPrivacy { get; set; }
public PrivacyLevel PronounPrivacy { get; set; }
public PrivacyLevel MetadataPrivacy { get; set; }
public PrivacyLevel MemberVisibility { get; }
public PrivacyLevel DescriptionPrivacy { get; }
public PrivacyLevel AvatarPrivacy { get; }
public PrivacyLevel NamePrivacy { get; } //ignore setting if no display name is set
public PrivacyLevel BirthdayPrivacy { get; }
public PrivacyLevel PronounPrivacy { get; }
public PrivacyLevel MetadataPrivacy { get; }
// public PrivacyLevel ColorPrivacy { get; set; }
/// Returns a formatted string representing the member's birthday, taking into account that a year of "0001" or "0004" is hidden

View File

@@ -8,6 +8,7 @@ namespace PluralKit.Core
{
public Partial<string> Name { get; set; }
public Partial<string?> DisplayName { get; set; }
public Partial<string?> AvatarUrl { get; set; }
public Partial<string?> Color { get; set; }
public Partial<LocalDate?> Birthday { get; set; }
public Partial<string?> Pronouns { get; set; }
@@ -26,6 +27,7 @@ namespace PluralKit.Core
public override UpdateQueryBuilder Apply(UpdateQueryBuilder b) => b
.With("name", Name)
.With("display_name", DisplayName)
.With("avatar_url", AvatarUrl)
.With("color", Color)
.With("birthday", Birthday)
.With("pronouns", Pronouns)

View File

@@ -72,12 +72,10 @@ namespace PluralKit.Core
};
}
private PKMember ConvertMember(PKSystem system, DataFileMember fileMember)
private MemberPatch ToMemberPatch(DataFileMember fileMember)
{
var newMember = new PKMember
var newMember = new MemberPatch
{
Hid = fileMember.Id,
System = system.Id,
Name = fileMember.Name,
DisplayName = fileMember.DisplayName,
Description = fileMember.Description,
@@ -88,10 +86,10 @@ namespace PluralKit.Core
};
if (fileMember.Prefix != null || fileMember.Suffix != null)
newMember.ProxyTags = new List<ProxyTag> {new ProxyTag(fileMember.Prefix, fileMember.Suffix)};
newMember.ProxyTags = new[] {new ProxyTag(fileMember.Prefix, fileMember.Suffix)};
else
// Ignore proxy tags where both prefix and suffix are set to null (would be invalid anyway)
newMember.ProxyTags = (fileMember.ProxyTags ?? new ProxyTag[] { }).Where(tag => !tag.IsEmpty).ToList();
newMember.ProxyTags = (fileMember.ProxyTags ?? new ProxyTag[] { }).Where(tag => !tag.IsEmpty).ToArray();
if (fileMember.Birthday != null)
{
@@ -149,7 +147,7 @@ namespace PluralKit.Core
_logger.Debug(
"Importing member with identifier {FileId} to system {System} (is creating new member? {IsCreatingNewMember})",
fileMember.Id, system.Id, isCreatingNewMember);
var newMember = await imp.AddMember(fileMember.Id, ConvertMember(system, fileMember));
var newMember = await imp.AddMember(fileMember.Id, fileMember.Id, fileMember.Name, ToMemberPatch(fileMember));
if (isCreatingNewMember)
result.AddedNames.Add(newMember.Name);

View File

@@ -60,43 +60,45 @@ namespace PluralKit.Core
/// </summary>
/// <remarks>If an existing member exists in this system that matches this member in either HID or name, it'll overlay the member information on top of this instead.</remarks>
/// <param name="identifier">An opaque identifier string that refers to this member regardless of source. Is used when importing switches. Value is irrelevant, but should be consistent with the same member later.</param>
/// <param name="member">A member struct containing the data to apply to this member. Null fields will be ignored.</param>
/// <param name="potentialHid">When trying to match the member to an existing member, will use a member with this HID if present in system.</param>
/// <param name="potentialName">When trying to match the member to an existing member, will use a member with this name if present in system.</param>
/// <param name="patch">A member patch struct containing the data to apply to this member </param>
/// <returns>The inserted member object, which may or may not share an ID or HID with the input member.</returns>
public async Task<PKMember> AddMember(string identifier, PKMember member)
public async Task<PKMember> AddMember(string identifier, string potentialHid, string potentialName, MemberPatch patch)
{
// See if we can find a member that matches this one
// if not, roll a new hid and we'll insert one with that
// (we can't trust the hid given in the member, it might let us overwrite another system's members)
var existingMember = FindExistingMemberInSystem(member.Hid, member.Name);
var existingMember = FindExistingMemberInSystem(potentialHid, potentialName);
string newHid = existingMember?.Hid ?? await _conn.QuerySingleAsync<string>("find_free_member_hid", commandType: CommandType.StoredProcedure);
// Upsert member data and return the ID
QueryBuilder qb = QueryBuilder.Upsert("members", "hid")
.Constant("hid", "@Hid")
.Constant("system", "@System")
.Variable("name", "@Name")
.Variable("keep_proxy", "@KeepProxy");
.Constant("system", "@System");
if (member.DisplayName != null) qb.Variable("display_name", "@DisplayName");
if (member.Description != null) qb.Variable("description", "@Description");
if (member.Color != null) qb.Variable("color", "@Color");
if (member.AvatarUrl != null) qb.Variable("avatar_url", "@AvatarUrl");
if (member.ProxyTags != null) qb.Variable("proxy_tags", "@ProxyTags");
if (member.Birthday != null) qb.Variable("birthday", "@Birthday");
if (patch.Name.IsPresent) qb.Variable("name", "@Name");
if (patch.DisplayName.IsPresent) qb.Variable("display_name", "@DisplayName");
if (patch.Description.IsPresent) qb.Variable("description", "@Description");
if (patch.Color.IsPresent) qb.Variable("color", "@Color");
if (patch.AvatarUrl.IsPresent) qb.Variable("avatar_url", "@AvatarUrl");
if (patch.ProxyTags.IsPresent) qb.Variable("proxy_tags", "@ProxyTags");
if (patch.Birthday.IsPresent) qb.Variable("birthday", "@Birthday");
if (patch.KeepProxy.IsPresent) qb.Variable("keep_proxy", "@KeepProxy");
var newMember = await _conn.QueryFirstAsync<PKMember>(qb.Build("returning *"),
new
{
Hid = newHid,
System = _systemId,
member.Name,
member.DisplayName,
member.Description,
member.Color,
member.AvatarUrl,
member.KeepProxy,
member.ProxyTags,
member.Birthday
Name = patch.Name.Value,
DisplayName = patch.DisplayName.Value,
Description = patch.Description.Value,
Color = patch.Color.Value,
AvatarUrl = patch.AvatarUrl.Value,
KeepProxy = patch.KeepProxy.Value,
ProxyTags = patch.ProxyTags.Value,
Birthday = patch.Birthday.Value
});
// Log this member ID by the given identifier

View File

@@ -30,6 +30,8 @@ namespace PluralKit.Core
public IEnumerator<T> GetEnumerator() => ToArray().GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => ToArray().GetEnumerator();
public static implicit operator Partial<T>(T val) => Present(val);
}
public class PartialConverter: JsonConverter