public async Task <DiscordEmbed> CreateSystemEmbed(DiscordClient client, PKSystem system, LookupContext ctx) { await using var conn = await _db.Obtain(); // Fetch/render info for all accounts simultaneously var accounts = await conn.GetLinkedAccounts(system.Id); var users = await Task.WhenAll(accounts.Select(async uid => (await client.GetUser(uid))?.NameAndMention() ?? $"(deleted account {uid})")); var memberCount = await conn.GetSystemMemberCount(system.Id, PrivacyLevel.Public); var eb = new DiscordEmbedBuilder() .WithColor(DiscordUtils.Gray) .WithTitle(system.Name ?? null) .WithThumbnail(system.AvatarUrl) .WithFooter($"System ID: {system.Hid} | Created on {system.Created.FormatZoned(system)}"); var latestSwitch = await _data.GetLatestSwitch(system.Id); if (latestSwitch != null && system.FrontPrivacy.CanAccess(ctx)) { var switchMembers = await _data.GetSwitchMembers(latestSwitch).ToListAsync(); if (switchMembers.Count > 0) { eb.AddField("Fronter".ToQuantity(switchMembers.Count(), ShowQuantityAs.None), string.Join(", ", switchMembers.Select(m => m.NameFor(ctx)))); } } if (system.Tag != null) { eb.AddField("Tag", system.Tag.EscapeMarkdown()); } eb.AddField("Linked accounts", string.Join(", ", users).Truncate(1000), true); if (system.MemberListPrivacy.CanAccess(ctx)) { if (memberCount > 0) { eb.AddField($"Members ({memberCount})", $"(see `pk;system {system.Hid} list` or `pk;system {system.Hid} list full`)", true); } else { eb.AddField($"Members ({memberCount})", "Add one with `pk;member new`!", true); } } if (system.DescriptionFor(ctx) is { } desc) { eb.AddField("Description", desc.NormalizeLineEndSpacing().Truncate(1024), false); } return(eb.Build()); }
public async Task <Embed> CreateSystemEmbed(Context cctx, PKSystem system, LookupContext ctx) { await using var conn = await _db.Obtain(); // Fetch/render info for all accounts simultaneously var accounts = await _repo.GetSystemAccounts(conn, system.Id); var users = (await GetUsers(accounts)).Select(x => x.User?.NameAndMention() ?? $"(deleted account {x.Id})"); var memberCount = cctx.MatchPrivateFlag(ctx) ? await _repo.GetSystemMemberCount(conn, system.Id, PrivacyLevel.Public) : await _repo.GetSystemMemberCount(conn, system.Id); var eb = new EmbedBuilder() .Title(system.Name) .Thumbnail(new(system.AvatarUrl)) .Footer(new($"System ID: {system.Hid} | Created on {system.Created.FormatZoned(system)}")) .Color(DiscordUtils.Gray); var latestSwitch = await _repo.GetLatestSwitch(conn, system.Id); if (latestSwitch != null && system.FrontPrivacy.CanAccess(ctx)) { var switchMembers = await _repo.GetSwitchMembers(conn, latestSwitch.Id).ToListAsync(); if (switchMembers.Count > 0) { eb.Field(new("Fronter".ToQuantity(switchMembers.Count, ShowQuantityAs.None), string.Join(", ", switchMembers.Select(m => m.NameFor(ctx))))); } } if (system.Tag != null) { eb.Field(new("Tag", system.Tag.EscapeMarkdown())); } eb.Field(new("Linked accounts", string.Join("\n", users).Truncate(1000), true)); if (system.MemberListPrivacy.CanAccess(ctx)) { if (memberCount > 0) { eb.Field(new($"Members ({memberCount})", $"(see `pk;system {system.Hid} list` or `pk;system {system.Hid} list full`)", true)); } else { eb.Field(new($"Members ({memberCount})", "Add one with `pk;member new`!", true)); } } if (system.DescriptionFor(ctx) is { } desc) { eb.Field(new("Description", desc.NormalizeLineEndSpacing().Truncate(1024), false)); } return(eb.Build()); }
public static JObject ToJson(this PKSystem system, LookupContext ctx) { var o = new JObject(); o.Add("id", system.Hid); o.Add("name", system.Name); o.Add("description", system.DescriptionFor(ctx)); o.Add("tag", system.Tag); o.Add("avatar_url", system.AvatarUrl); o.Add("created", system.Created.FormatExport()); o.Add("tz", system.UiTz); o.Add("description_privacy", ctx == LookupContext.ByOwner ? system.DescriptionPrivacy.ToJsonString() : null); o.Add("member_list_privacy", ctx == LookupContext.ByOwner ? system.MemberListPrivacy.ToJsonString() : null); o.Add("front_privacy", ctx == LookupContext.ByOwner ? system.FrontPrivacy.ToJsonString() : null); o.Add("front_history_privacy", ctx == LookupContext.ByOwner ? system.FrontHistoryPrivacy.ToJsonString() : null); return(o); }
public static JObject ToJson(this PKSystem system, LookupContext ctx) { var o = new JObject(); o.Add("id", system.Hid); o.Add("uuid", system.Uuid.ToString()); o.Add("name", system.Name); o.Add("description", system.DescriptionFor(ctx)); o.Add("tag", system.Tag); o.Add("pronouns", system.PronounPrivacy.Get(ctx, system.Pronouns)); o.Add("avatar_url", system.AvatarUrl.TryGetCleanCdnUrl()); o.Add("banner", system.DescriptionPrivacy.Get(ctx, system.BannerImage).TryGetCleanCdnUrl()); o.Add("color", system.Color); o.Add("created", system.Created.FormatExport()); if (ctx == LookupContext.ByOwner) { // todo: should this be moved to a different JSON model? o.Add("webhook_url", system.WebhookUrl); // o.Add("webhook_token", system.WebhookToken); var p = new JObject(); p.Add("description_privacy", system.DescriptionPrivacy.ToJsonString()); p.Add("pronoun_privacy", system.PronounPrivacy.ToJsonString()); p.Add("member_list_privacy", system.MemberListPrivacy.ToJsonString()); p.Add("group_list_privacy", system.GroupListPrivacy.ToJsonString()); p.Add("front_privacy", system.FrontPrivacy.ToJsonString()); p.Add("front_history_privacy", system.FrontHistoryPrivacy.ToJsonString()); o.Add("privacy", p); } else { o.Add("privacy", null); } return(o); }
public async Task <Embed> CreateSystemEmbed(Context cctx, PKSystem system, LookupContext ctx) { // Fetch/render info for all accounts simultaneously var accounts = await _repo.GetSystemAccounts(system.Id); var users = (await GetUsers(accounts)).Select(x => x.User?.NameAndMention() ?? $"(deleted account {x.Id})"); var countctx = LookupContext.ByNonOwner; if (cctx.MatchFlag("a", "all")) { if (system.Id == cctx.System.Id) { countctx = LookupContext.ByOwner; } else { throw Errors.LookupNotAllowed; } } var memberCount = await _repo.GetSystemMemberCount(system.Id, countctx == LookupContext.ByOwner?null : PrivacyLevel.Public); uint color; try { color = system.Color?.ToDiscordColor() ?? DiscordUtils.Gray; } catch (ArgumentException) { // There's no API for system colors yet, but defaulting to a blank color in advance can't be a bad idea color = DiscordUtils.Gray; } var eb = new EmbedBuilder() .Title(system.Name) .Thumbnail(new Embed.EmbedThumbnail(system.AvatarUrl.TryGetCleanCdnUrl())) .Footer(new Embed.EmbedFooter( $"System ID: {system.Hid} | Created on {system.Created.FormatZoned(cctx.Zone)}")) .Color(color) .Url($"https://dash.pluralkit.me/profile/s/{system.Hid}"); if (system.DescriptionPrivacy.CanAccess(ctx)) { eb.Image(new Embed.EmbedImage(system.BannerImage)); } var latestSwitch = await _repo.GetLatestSwitch(system.Id); if (latestSwitch != null && system.FrontPrivacy.CanAccess(ctx)) { var switchMembers = await _db.Execute(conn => _repo.GetSwitchMembers(conn, latestSwitch.Id)).ToListAsync(); if (switchMembers.Count > 0) { eb.Field(new Embed.Field("Fronter".ToQuantity(switchMembers.Count, ShowQuantityAs.None), string.Join(", ", switchMembers.Select(m => m.NameFor(ctx))))); } } if (system.Tag != null) { eb.Field(new Embed.Field("Tag", system.Tag.EscapeMarkdown(), true)); } if (cctx.Guild != null) { var guildSettings = await _repo.GetSystemGuild(cctx.Guild.Id, system.Id); if (guildSettings.Tag != null && guildSettings.TagEnabled) { eb.Field(new Embed.Field($"Tag (in server '{cctx.Guild.Name}')", guildSettings.Tag .EscapeMarkdown(), true)); } if (!guildSettings.TagEnabled) { eb.Field(new Embed.Field($"Tag (in server '{cctx.Guild.Name}')", "*(tag is disabled in this server)*")); } } if (system.PronounPrivacy.CanAccess(ctx) && system.Pronouns != null) { eb.Field(new Embed.Field("Pronouns", system.Pronouns, true)); } if (!system.Color.EmptyOrNull()) { eb.Field(new Embed.Field("Color", $"#{system.Color}", true)); } eb.Field(new Embed.Field("Linked accounts", string.Join("\n", users).Truncate(1000), true)); if (system.MemberListPrivacy.CanAccess(ctx)) { if (memberCount > 0) { eb.Field(new Embed.Field($"Members ({memberCount})", $"(see `pk;system {system.Hid} list` or `pk;system {system.Hid} list full`)", true)); } else { eb.Field(new Embed.Field($"Members ({memberCount})", "Add one with `pk;member new`!", true)); } } if (system.DescriptionFor(ctx) is { } desc) { eb.Field(new Embed.Field("Description", desc.NormalizeLineEndSpacing().Truncate(1024))); } return(eb.Build()); }
public static JObject ToJson(this PKSystem system, LookupContext ctx, APIVersion v = APIVersion.V1) { var o = new JObject(); o.Add("id", system.Hid); if (v == APIVersion.V2) { o.Add("uuid", system.Uuid.ToString()); } o.Add("name", system.Name); o.Add("description", system.DescriptionFor(ctx)); o.Add("tag", system.Tag); o.Add("avatar_url", system.AvatarUrl.TryGetCleanCdnUrl()); o.Add("banner", system.DescriptionPrivacy.Get(ctx, system.BannerImage).TryGetCleanCdnUrl()); o.Add("color", system.Color); o.Add("created", system.Created.FormatExport()); switch (v) { case APIVersion.V1: { // this property was moved to SystemConfig // see notice in /api/legacy docs o.Add("tz", "UTC"); o.Add("description_privacy", ctx == LookupContext.ByOwner ? system.DescriptionPrivacy.ToJsonString() : null); o.Add("member_list_privacy", ctx == LookupContext.ByOwner ? system.MemberListPrivacy.ToJsonString() : null); o.Add("group_list_privacy", ctx == LookupContext.ByOwner ? system.GroupListPrivacy.ToJsonString() : null); o.Add("front_privacy", ctx == LookupContext.ByOwner ? system.FrontPrivacy.ToJsonString() : null); o.Add("front_history_privacy", ctx == LookupContext.ByOwner ? system.FrontHistoryPrivacy.ToJsonString() : null); break; } case APIVersion.V2: { if (ctx == LookupContext.ByOwner) { // todo: should this be moved to a different JSON model? o.Add("webhook_url", system.WebhookUrl); // o.Add("webhook_token", system.WebhookToken); var p = new JObject(); p.Add("description_privacy", system.DescriptionPrivacy.ToJsonString()); p.Add("member_list_privacy", system.MemberListPrivacy.ToJsonString()); p.Add("group_list_privacy", system.GroupListPrivacy.ToJsonString()); p.Add("front_privacy", system.FrontPrivacy.ToJsonString()); p.Add("front_history_privacy", system.FrontHistoryPrivacy.ToJsonString()); o.Add("privacy", p); } else { o.Add("privacy", null); } break; } } return(o); }