2021-11-27 02:10:56 +00:00
|
|
|
using Newtonsoft.Json.Linq;
|
2021-09-27 03:18:17 +00:00
|
|
|
|
2021-08-27 15:03:47 +00:00
|
|
|
using NodaTime;
|
2020-06-29 21:51:12 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace PluralKit.Core;
|
2021-08-07 20:39:32 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public readonly struct GroupId: INumericId<GroupId, int>
|
2020-06-29 21:51:12 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
public int Value { get; }
|
2021-08-07 20:39:32 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public GroupId(int value)
|
|
|
|
{
|
|
|
|
Value = value;
|
|
|
|
}
|
2021-08-07 20:39:32 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public bool Equals(GroupId other) => Value == other.Value;
|
2021-08-07 20:39:32 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public override bool Equals(object obj) => obj is GroupId other && Equals(other);
|
2021-08-07 20:39:32 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public override int GetHashCode() => Value;
|
2021-08-07 20:39:32 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static bool operator ==(GroupId left, GroupId right) => left.Equals(right);
|
2021-08-07 20:39:32 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static bool operator !=(GroupId left, GroupId right) => !left.Equals(right);
|
2021-08-07 20:39:32 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public int CompareTo(GroupId other) => Value.CompareTo(other.Value);
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public override string ToString() => $"Group #{Value}";
|
|
|
|
}
|
2021-08-07 20:39:32 +00:00
|
|
|
|
|
|
|
#nullable enable
|
2021-11-27 02:10:56 +00:00
|
|
|
public class PKGroup
|
|
|
|
{
|
|
|
|
public GroupId Id { get; private set; }
|
|
|
|
public string Hid { get; private set; } = null!;
|
|
|
|
public Guid Uuid { get; private set; }
|
|
|
|
public SystemId System { get; private set; }
|
|
|
|
|
|
|
|
public string Name { get; private set; } = null!;
|
|
|
|
public string? DisplayName { get; private set; }
|
|
|
|
public string? Description { get; private set; }
|
|
|
|
public string? Icon { get; private set; }
|
|
|
|
public string? BannerImage { get; private set; }
|
|
|
|
public string? Color { get; private set; }
|
|
|
|
|
|
|
|
public PrivacyLevel DescriptionPrivacy { get; private set; }
|
|
|
|
public PrivacyLevel IconPrivacy { get; private set; }
|
|
|
|
public PrivacyLevel ListPrivacy { get; private set; }
|
|
|
|
public PrivacyLevel Visibility { get; private set; }
|
|
|
|
|
|
|
|
public Instant Created { get; private set; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class PKGroupExt
|
|
|
|
{
|
|
|
|
public static string? DescriptionFor(this PKGroup group, LookupContext ctx) =>
|
|
|
|
group.DescriptionPrivacy.Get(ctx, group.Description);
|
2021-09-22 01:42:41 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static string? IconFor(this PKGroup group, LookupContext ctx) =>
|
|
|
|
group.IconPrivacy.Get(ctx, group.Icon?.TryGetCleanCdnUrl());
|
2021-09-22 01:42:41 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static JObject ToJson(this PKGroup group, LookupContext ctx, string? systemStr = null,
|
|
|
|
bool needsMembersArray = false)
|
|
|
|
{
|
|
|
|
var o = new JObject();
|
2021-10-12 06:19:42 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
o.Add("id", group.Hid);
|
|
|
|
o.Add("uuid", group.Uuid.ToString());
|
|
|
|
o.Add("name", group.Name);
|
2021-10-12 06:19:42 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (systemStr != null)
|
|
|
|
o.Add("system", systemStr);
|
2021-09-22 01:42:41 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
o.Add("display_name", group.DisplayName);
|
|
|
|
o.Add("description", group.DescriptionPrivacy.Get(ctx, group.Description));
|
2022-01-08 14:00:57 +00:00
|
|
|
o.Add("icon", group.IconFor(ctx));
|
2021-11-27 02:10:56 +00:00
|
|
|
o.Add("banner", group.DescriptionPrivacy.Get(ctx, group.BannerImage));
|
|
|
|
o.Add("color", group.Color);
|
2021-09-22 01:42:41 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
o.Add("created", group.Created.FormatExport());
|
2021-09-22 01:42:41 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (needsMembersArray)
|
|
|
|
o.Add("members", new JArray());
|
2021-09-22 01:42:41 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (ctx == LookupContext.ByOwner)
|
|
|
|
{
|
|
|
|
var p = new JObject();
|
2021-09-22 01:42:41 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
p.Add("description_privacy", group.DescriptionPrivacy.ToJsonString());
|
|
|
|
p.Add("icon_privacy", group.IconPrivacy.ToJsonString());
|
|
|
|
p.Add("list_privacy", group.ListPrivacy.ToJsonString());
|
|
|
|
p.Add("visibility", group.Visibility.ToJsonString());
|
2021-09-22 01:42:41 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
o.Add("privacy", p);
|
2021-09-22 01:42:41 +00:00
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
o.Add("privacy", null);
|
|
|
|
}
|
|
|
|
|
|
|
|
return o;
|
2020-08-21 16:31:22 +00:00
|
|
|
}
|
2020-06-29 21:51:12 +00:00
|
|
|
}
|