Move system updates to the same patch system as members

This commit is contained in:
Ske
2020-06-29 14:39:19 +02:00
parent c5697b33e2
commit 9c1efc7886
12 changed files with 172 additions and 111 deletions

View File

@@ -28,16 +28,5 @@ namespace PluralKit.Core
conn.QueryFirstAsync<MemberGuildSettings>(
"insert into member_guild (guild, member) values (@guild, @member) on conflict (guild, member) do update set guild = @guild, member = @member returning *",
new {guild, member});
public static Task<PKMember> UpdateMember(this IPKConnection conn, MemberId id, MemberPatch patch)
{
var (query, pms) = patch.Apply(new UpdateQueryBuilder("members", "id = @id"))
.WithConstant("id", id)
.Build("returning *");
return conn.QueryFirstAsync<PKMember>(query, pms);
}
public static Task DeleteMember(this IPKConnection conn, MemberId id) =>
conn.ExecuteAsync("delete from members where id = @Id", new {Id = id});
}
}

View File

@@ -10,18 +10,18 @@ namespace PluralKit.Core {
// Additions here should be mirrored in SystemStore::Save
[Key] public SystemId Id { get; }
public string Hid { get; }
public string Name { get; set; }
public string Description { get; set; }
public string Tag { get; set; }
public string AvatarUrl { get; set; }
public string Token { get; set; }
public string Name { get; }
public string Description { get; }
public string Tag { get; }
public string AvatarUrl { get; }
public string Token { get; }
public Instant Created { get; }
public string UiTz { get; set; }
public bool PingsEnabled { get; set; }
public PrivacyLevel DescriptionPrivacy { get; set; }
public PrivacyLevel MemberListPrivacy { get; set; }
public PrivacyLevel FrontPrivacy { get; set; }
public PrivacyLevel FrontHistoryPrivacy { get; set; }
public bool PingsEnabled { get; }
public PrivacyLevel DescriptionPrivacy { get; }
public PrivacyLevel MemberListPrivacy { get;}
public PrivacyLevel FrontPrivacy { get; }
public PrivacyLevel FrontHistoryPrivacy { get; }
[JsonIgnore] public DateTimeZone Zone => DateTimeZoneProviders.Tzdb.GetZoneOrNull(UiTz);
}

View File

@@ -4,7 +4,7 @@ using NodaTime;
namespace PluralKit.Core
{
public class MemberPatch: PatchObject<MemberId, PKMember>
public class MemberPatch: PatchObject
{
public Partial<string> Name { get; set; }
public Partial<string?> DisplayName { get; set; }

View File

@@ -0,0 +1,31 @@
using System.Threading.Tasks;
using Dapper;
namespace PluralKit.Core
{
public static class ModelPatchExt
{
public static Task<PKSystem> UpdateSystem(this IPKConnection conn, SystemId id, SystemPatch patch)
{
var (query, pms) = patch.Apply(new UpdateQueryBuilder("systems", "id = @id"))
.WithConstant("id", id)
.Build("returning *");
return conn.QueryFirstAsync<PKSystem>(query, pms);
}
public static Task DeleteSystem(this IPKConnection conn, SystemId id) =>
conn.ExecuteAsync("delete from systems where id = @Id", new {Id = id});
public static Task<PKMember> UpdateMember(this IPKConnection conn, MemberId id, MemberPatch patch)
{
var (query, pms) = patch.Apply(new UpdateQueryBuilder("members", "id = @id"))
.WithConstant("id", id)
.Build("returning *");
return conn.QueryFirstAsync<PKMember>(query, pms);
}
public static Task DeleteMember(this IPKConnection conn, MemberId id) =>
conn.ExecuteAsync("delete from members where id = @Id", new {Id = id});
}
}

View File

@@ -1,8 +1,6 @@
using PluralKit.Core;
namespace PluralKit.Core
namespace PluralKit.Core
{
public abstract class PatchObject<TKey, TObj>
public abstract class PatchObject
{
public abstract UpdateQueryBuilder Apply(UpdateQueryBuilder b);
}

View File

@@ -0,0 +1,31 @@
#nullable enable
namespace PluralKit.Core
{
public class SystemPatch: PatchObject
{
public Partial<string?> Name { get; set; }
public Partial<string?> Description { get; set; }
public Partial<string?> Tag { get; set; }
public Partial<string?> AvatarUrl { get; set; }
public Partial<string?> Token { get; set; }
public Partial<string> UiTz { get; set; }
public Partial<PrivacyLevel> DescriptionPrivacy { get; set; }
public Partial<PrivacyLevel> MemberListPrivacy { get; set; }
public Partial<PrivacyLevel> FrontPrivacy { get; set; }
public Partial<PrivacyLevel> FrontHistoryPrivacy { get; set; }
public Partial<bool> PingsEnabled { get; set; }
public override UpdateQueryBuilder Apply(UpdateQueryBuilder b) => b
.With("name", Name)
.With("description", Description)
.With("tag", Tag)
.With("avatar_url", AvatarUrl)
.With("token", Token)
.With("ui_tz", UiTz)
.With("description_privacy", DescriptionPrivacy)
.With("member_list_privacy", MemberListPrivacy)
.With("front_privacy", FrontPrivacy)
.With("front_history_privacy", FrontHistoryPrivacy)
.With("pings_enabled", PingsEnabled);
}
}

View File

@@ -116,16 +116,17 @@ namespace PluralKit.Core
await _data.AddAccount(system, accountId);
}
await using var conn = await _db.Obtain();
// Apply system info
system.Name = data.Name;
if (data.Description != null) system.Description = data.Description;
if (data.Tag != null) system.Tag = data.Tag;
if (data.AvatarUrl != null) system.AvatarUrl = data.AvatarUrl;
if (data.TimeZone != null) system.UiTz = data.TimeZone ?? "UTC";
await _data.SaveSystem(system);
var patch = new SystemPatch {Name = data.Name};
if (data.Description != null) patch.Description = data.Description;
if (data.Tag != null) patch.Tag = data.Tag;
if (data.AvatarUrl != null) patch.AvatarUrl = data.AvatarUrl;
if (data.TimeZone != null) patch.UiTz = data.TimeZone ?? "UTC";
await conn.UpdateSystem(system.Id, patch);
// -- Member/switch import --
await using var conn = await _db.Obtain();
await using (var imp = await BulkImporter.Begin(system, conn))
{
// Tally up the members that didn't exist before, and check member count on import

View File

@@ -102,20 +102,6 @@ namespace PluralKit.Core {
/// </summary>
/// <exception>Throws an exception (TODO: which?) if the given account is not linked to the given system.</exception>
Task RemoveAccount(PKSystem system, ulong accountToRemove);
/// <summary>
/// Saves the information within the given <see cref="PKSystem"/> struct to the data store.
/// </summary>
Task SaveSystem(PKSystem system);
/// <summary>
/// Deletes the given system from the database.
/// </summary>
/// <para>
/// This will also delete all the system's members, all system switches, and every message that has been proxied
/// by members in the system.
/// </para>
Task DeleteSystem(PKSystem system);
/// <summary>
/// Gets a member by its user-facing human ID.